forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Arithmetic seem to overwrite variable

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Thu Oct 01, 2009 8:58 pm    Post subject: Arithmetic seem to overwrite variable Reply with quote

Hi,

i've started PSP developemnt recently and are now faced with a strange issue, which I'm not sure how to call.

I've following situation:

many loops are calculation floats and double values. However, after a certain number of iterations this operation seem to overwrite different values.

Example:
Code:

int i = 0;
int n = 0;
douple d1;

int count = 0;
for (i=0;i<100;i++){
 for (n=0;n<100;n++){
   d1 = (i*i + 100) / n;
   count ++;
 }
}

I know this code makes no sence at all.However, if I debug some code which seem to be simmilar to this (a liitle bit more complex ;o) ) there is a very strange behavior:
1. after some runns the count will be set to 0;
2. if this happens the console displays that new thread was started and the debugger does not move through the code line, by line but is jumping more or less randomly and with every step the console display "New Thread"...

Has any one ever faced some simmilar issues ?

I'm using the WINSDK for PSP with eclipse and a psp-c++ compiler.

What worked so far was simple mathematics to draw pixel on the screen with random funtion and some addititions. But it seems as far as I'm introdusing float/double in my code the PSP does strange things. I'm not using gnu..

Thanks for any kinf od advice.

regards
AnMaBaGiMa


Last edited by anmabagima on Fri Oct 02, 2009 3:34 am; edited 2 times in total
Back to top
View user's profile Send private message
psPea



Joined: 01 Sep 2007
Posts: 64

PostPosted: Thu Oct 01, 2009 9:38 pm    Post subject: Reply with quote

douple = (i*i + 100) / j; ?
_________________
Click ME!
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Fri Oct 02, 2009 2:16 am    Post subject: Reply with quote

There's no such thing as a douple, and j isn't defined either.
Back to top
View user's profile Send private message AIM Address
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Fri Oct 02, 2009 3:27 am    Post subject: sorry error in code Reply with quote

Hi,

sorry - there was an tiny error in the code :o(

Code:
d1 = (i*i + 100) / n ;


sorry...
Back to top
View user's profile Send private message
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Fri Oct 02, 2009 4:51 am    Post subject: Resuable example Reply with quote

Hi,

I've tried to simplify my code as much as possible...

Know I give you my code snippets (the usual code which is in any example the same is not shown as I would not expect the issue there)

the main.cpp
Code:

int main(int argc, char* argv[])
{
   pspDebugScreenInit();
   // setup of common call backs
   setupCallbacks();

   Init( );


Init() is implemented in engine.cpp:
Code:

struct rayDelta {
   long rayDeltaX, rayDeltaY;
} ;

long   mapSize; //size of the Map (width)
rayDelta preDelta[480];

void Init( ){
   double rayDeltaX, rayDeltaY;

   double hx;

   mapSize = 512;
   int x;

   for (x=0;x<480;x++){
      hx = x - 240;
      rayDeltaX = ((hx / sqrt(hx*hx + 400*400))*1024);
      rayDeltaY = (400 / sqrt(hx*hx + 400*400)*1024);
      preDelta[x].rayDeltaX = rayDeltaX;
      preDelta[x].rayDeltaY = rayDeltaY;
   }
}


if you compile this and set the breakpoint to the to of the loop than I'm not able to debug into each line. The debugger jumps mainly to the last line. There are each time "New Thread" outputs in the console of the debugger. I've absolutely no clue whats going on.

however, I've done more and more simplification...
And it seem to me that this has nothing really to do with my first assumption that the calculation may the cause...after some tries..also bring all into the main.cpp the debugging console outputs the following:
[New Thread 76458591]
[Switching to Thread 76458591]
Current language: auto; currently c++
[New Thread 76458591]
[New Thread 76458591]
[New Thread 76458591]
[New Thread 76458591]

GDB is unable to find the start of the function at 0x883cb90
and thus can't determine the size of that function's stack frame.
This means that GDB may be unable to access that stack frame, or
the frames below it.
This problem is most likely caused by an invalid program counter or
stack pointer.
However, if you think GDB should simply search farther back
from 0x883cb90 for code which looks like the beginning of a
function, you can increase the range of the search using the `set
heuristic-fence-post' command.
warning: GDB can't find the start of the function at 0x883cb90.

Any further Ideas ? Something seem to change/move the memory addresses/pointers in the stack...but what ?

Many thanks for any kind of advise...
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Fri Oct 02, 2009 5:50 am    Post subject: Reply with quote

That code looks fine, but that's not your real code. It doesn't even compile: at the very least you need to change "rayDelta preDelta[480];" to "struct rayDelta preDelta[480];". If you show us your actual full code, we may be able to help.
Back to top
View user's profile Send private message
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Fri Oct 02, 2009 5:12 pm    Post subject: Code Reply with quote

Hi,
thanks for the reply. I 've further tried to simpliy and have put all code into main.cpp now. This is not "best practice" and once I know the root cause of the issue I would like to split the whole code accross several files again for better readability...However..It compiles as it is...

Please find the whole code now:
Code:

#include <psptypes.h>
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include <pspgu.h>
#include "../common/callbacks.h"

PSP_MODULE_INFO("PSP Demo", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

//color generator
#define RGB(r, g, b) ((r&255) | (g&255) << 8 | (b&255) << 16)

#define pg_vramtop ((unsigned char *)0x04000000)

struct rayDelta {
   long rayDeltaX, rayDeltaY;
} ;
long   mapSize; //size of the Map (width)
rayDelta preDelta[480];

void Init( ){
   double rayDeltaX, rayDeltaY;

   double hx;

   mapSize = 512;
   int x,y;
   for (x=0;x<480;x++){
      hx = x - 240;
      rayDeltaX = ((hx*1024 / sqrt(hx*hx + 400*400)));
      rayDeltaY = (400*1024 / sqrt(hx*hx + 400*400));

      preDelta[x].rayDeltaX = rayDeltaX;
      preDelta[x].rayDeltaY = rayDeltaY;
   }
}

int main(int argc, char* argv[])
{
   // setup of common call backs
   setupCallbacks();

   Init( );

   // run the application
   while(running()){
      sceDisplayWaitVblankStart();
   }

   sceKernelExitGame();
   return 0;
}


Thanks again...
Regards
AnMaBaGiMa
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Fri Oct 02, 2009 9:55 pm    Post subject: Reply with quote

Code:

struct rayDelta {
   long rayDeltaX, rayDeltaY;
} ;
...
rayDelta preDelta[480];


For the second time, how is this going to even compile?

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Fri Oct 02, 2009 10:35 pm    Post subject: Reply with quote

Quote:
For the second time, how is this going to even compile?


i do not know, but the compile does not throw any error...

However, I'v changed now to:
Code:
struct rayDelta preDelta[480];


It still compiles without error ;o) but the behavior is the same...
Here the console output after some debug steps within the main loop:
it's exciting ;o)

Code:
[New Thread 0]
Current language:  auto; currently c
[New Thread 76458563]
[Switching to Thread 76458563]
Current language:  auto; currently c++
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]
[New Thread 76458563]

    GDB is unable to find the start of the function at 0x8808728
and thus can't determine the size of that function's stack frame.
This means that GDB may be unable to access that stack frame, or
the frames below it.
    This problem is most likely caused by an invalid program counter or
stack pointer.
    However, if you think GDB should simply search farther back
from 0x8808728 for code which looks like the beginning of a
function, you can increase the range of the search using the `set
heuristic-fence-post' command.
warning: GDB can't find the start of the function at 0x8808728.
warning: GDB can't find the start of the function at 0x8808727.


Regards
AnMaBaGiMa
Back to top
View user's profile Send private message
ymg545



Joined: 26 Aug 2009
Posts: 1

PostPosted: Fri Oct 02, 2009 11:46 pm    Post subject: Reply with quote

Jim wrote:
Code:

struct rayDelta {
   long rayDeltaX, rayDeltaY;
} ;
...
rayDelta preDelta[480];


For the second time, how is this going to even compile?

Jim


In C++, u dont have to insert the keyword "struct" to the variable declaration. So the code should be compiled just fine
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat Oct 03, 2009 2:29 am    Post subject: Reply with quote

Uh, I've always needed to wrap main() and any callback stuff in extern "C" { } to get it to work in a C++ project. You might at least try that.
Back to top
View user's profile Send private message AIM Address
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Sat Oct 03, 2009 3:21 am    Post subject: Reply with quote

I still can't compile and test that code.
I don't have your "../common/callbacks.h" and there's no definition of "running()".

If you want us to help, give us a complete example (including the makefile) that we can build and test. Otherwise you're on your own.

How does the code behave when you run it outside gdb? gdb might just be getting confused by some of the types, I don't know how well it's been tested with floating point stuff.
Back to top
View user's profile Send private message
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Sat Oct 03, 2009 9:04 pm    Post subject: Reply with quote

Hi,

you are absolutely right. I should have posted as much as possible right in teh beginning to save some time ;o)

Here is the full stuff. To be able to visualize what the code is doing without debugging I've added some code to draw some pixel on the calculated values.

First of all the ../common/callback.h (+c)
Code:

#ifndef common_callbacks_h
#define common_callbacks_h

#ifdef __cplusplus
extern "C" {
#endif

int running();
int setupCallbacks(void);

#ifdef __cplusplus
}
#endif

#endif

Code:

/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * Copyright (c) 2005 Jesper Svennevid
 */

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>

static int exitRequest = 0;

int running()
{
   return !exitRequest;
}

int exitCallback(int arg1, int arg2, void *common)
{
   exitRequest = 1;
   return 0;
}

int callbackThread(SceSize args, void *argp)
{
   int cbid;

   cbid = sceKernelCreateCallback("Exit Callback", exitCallback, NULL);
   sceKernelRegisterExitCallback(cbid);

   sceKernelSleepThreadCB();

   return 0;
}

int setupCallbacks(void)
{
   int thid = 0;

   thid = sceKernelCreateThread("update_thread", callbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0)
   {
      sceKernelStartThread(thid, 0, 0);
   }

   return thid;
}


now again the main.cpp with the changes:
Code:

#include <psptypes.h>
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include <pspgu.h>
#include "../common/callbacks.h"

PSP_MODULE_INFO("PSP Demo", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)
#define PIXELSIZE   4            //in short
#define   LINESIZE   512            //in short
//color generator
#define RGB(r, g, b) ((r&255) | (g&255) << 8 | (b&255) << 16)

#define pg_vramtop ((unsigned char *)0x04000000)

struct rayDelta {
   long rayDeltaX, rayDeltaY;
} ;
long   mapSize; //size of the Map (width)
struct rayDelta preDelta[480];

unsigned char *pgGetVramAddr(unsigned long x,unsigned long y)
{

   return pg_vramtop+x*PIXELSIZE+y*LINESIZE*PIXELSIZE+0x40000000;
}

void setPixel(int x,int y, long color)
{
     unsigned short* vram = (unsigned short*) pgGetVramAddr(x, y);
     *vram = color;
}

void Init( ){
   double rayDeltaX, rayDeltaY;

   double hx;

   mapSize = 512;
   int x,y;
   for (x=0;x<480;x++){
      hx = x - 240;
      rayDeltaX = ((hx*1024 / sqrt(hx*hx + 400*400)));
      rayDeltaY = (400*1024 / sqrt(hx*hx + 400*400));

      preDelta[x].rayDeltaX = rayDeltaX;
      preDelta[x].rayDeltaY = rayDeltaY;
   }
}

int main(int argc, char* argv[])
{
   // setup of common call backs
   setupCallbacks();

   Init( );

   // run the application

   // do visualize some stuff to see what the hell the application does
   for (int x=0;x<480;x++){
      //make a point on the screen to visiualize the rayDeltaX
      //normed to a screen height of 272
      int posY = 272/2 + ((preDelta[x].rayDeltaX*272/2) >> 10);
      setPixel(x, posY, RGB(255,255,255));
   }
   while(running()){
      sceDisplayWaitVblankStart();
   }

   sceKernelExitGame();
   return 0;
}


And now the makefile:
Code:

TARGET = pspdemo
OBJS = main.o ../common/callbacks.o

INCDIR =
CFLAGS = -G0 -Wall -g
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
BUILD_PRX = 1

LIBDIR =
LDFLAGS =
LIBS= -lpspgum -lpspgu -lm

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = PSP Demo

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak


Today I was able to test the same stuff on a different PC. I guess the "only" difference here is that Eclipse is running with a different JRE version (1.5.0_11)...
However, the debugging seem to be a bit more stable. There is still many of "new threads" comming on the console during debuging step by step, but the stack issue seem not to occur again. I will try this now in parallel on the PC where this topic was raised for and will see whether the PSP draws a line from upper left to lower right when just deploying the EBOOT to the PSP and run without debugging mode...will update soon.

For now thanks for your help and support and if someone could try this code on his configuration I would be glad to hear about the results ;o)

Best regards
AnMaBaGiMa
Back to top
View user's profile Send private message
anmabagima



Joined: 01 Oct 2009
Posts: 96

PostPosted: Mon Oct 05, 2009 9:51 pm    Post subject: solved.....somehow Reply with quote

Hi all,

now I was able to compare an eclipse running on JRE1.4.2_13 and JRE1.5.0_11....

The good news: the result at the end seem to be compiled all right (as EBOOT). However, the strange compiler/debugger issue occures on JRE1.4.2_13 ... the debugger jumps between random code lines and initialize/overwrites variables as it likes...However, I started with a complete new setup of eclipse project on my machine with JRE1.5.0_11 and looking forward that this strange kind of things will not happen again ;o)

Thanks to all again for your support and tipps.
Regards
AnMaBaGiMa
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group