NXC: CurrentTick() bug, wrong NBC temp variable type
Posted: 04 Feb 2011, 00:08
by spillerrec
I was trying my NXT RPG code again which I haven't touched for a half year (but with a new compiler and firmware). It compiled and ran, but when entering a specific part it froze.
Found the bug while writing this post, the spoiler is the text I wrote up to that point:
unsigned long t1=CurrentTick();
Input_Reset(); //TODO: make this work again somehow...
Draw_Level(Direction_Moved);//Draw map normally
unsigned long t2=CurrentTick();
//React on automatic events
Events_Handling(p1.pos_x, p1.pos_y, true);
unsigned long t3=CurrentTick();
//Display time used (and other debug values)
NumOut(0,LCD_LINE1, CurrentTick()-t0);
NumOut(0,LCD_LINE2, t1-t0);
NumOut(0,LCD_LINE3, t0);
NumOut(0,LCD_LINE4, t1);
NumOut(0,LCD_LINE5, t2);
The interesting part is the variable t1 (and t2). It is given a value from CurrentTick(). Then a few function calls happen that can't affect it since it is a local variable. It is used in two calculations which shouldn't affect the value and then displayed on the screen. And the value was 1... Actually it corresponded to the variable "p1.direction" or "input.dpad" (or some other variable that might be linked to these). So when I was pressing the move buttons and changed direction of the player, t1 (and also t2) changed correspondingly...
Just found the issue and it looks strangely familiar...
The compiled NBC code:
gettick stores it in the temp signed variable, but the temp unsigned variable is used to retrieve the value. And the last time it had been used was with input.dpad... (I didn't notice they where different variables at first, thank God for Notepad++ highlighting identical identifiers, otherwise I would never have noticed...)
I know there was a similar case which was fixed, but I'm using the compiler from test_release20101103.zip. Anything newer than that?
I tried changing it to signed long, but the issues persist, it still tries to use a unsigned temp variable...
Re: NXC: CurrentTick() bug, wrong NBC temp variable type
Posted: 04 Feb 2011, 03:50
by muntoo
spillerrec wrote:thank God for Notepad++ highlighting identical identifiers
So you think Don_Ho == God???
Also, could you post a larger* sample of the NBC code? Those few lines aren't enough for a NBC newbie like me.
*By that, I mean like 20 lines of code max.
Re: NXC: CurrentTick() bug, wrong NBC temp variable type
Posted: 04 Feb 2011, 04:04
by afanofosc
I need a small program that demonstrates this bug. I don't see it at all here:
time_0 works fine, however time_1 contains 1 which is func1()'s return value. The issue disappears if I change func1() to a void return type, however the original program used void functions.
muntoo, some NBC code for you: (of the example program)
If you can try that out as an alternative to CurrentTick and let me know if you see any cases where it is getting confused about register types I would be grateful. The difference between the above and the definition of CurrentTick in NXCDefs.h is __URETVAL__ instead of __RETVAL__. The compiler replaces these tokens in ASM blocks with the signed or unsigned register variable name.
John Hansen
Re: NXC: CurrentTick() bug, wrong NBC temp variable type
Posted: 04 Feb 2011, 23:46
by spillerrec
I tried replacing all the CurrentTick() calls in my program including those which were working fine (10 in total) and everything seems to be working as it should. The animation is fluid and the timers returns the values I expect.
I will try it in some of my other programs later.