Jump to content

Photo

A little piece of code that is driving me crazy


6 replies to this topic

#1 Nop90 OFFLINE  

Nop90

    Star Raider

  • 71 posts
  • Location:Italy

Posted Fri Feb 8, 2019 2:53 PM

While implementing screen flipping I encountered some problems swapping the direction keys. I'm using a variable named "flipped" to trace the screen state and change the key behavior, but that variable wasn't changing state. 

 

Playing with the code I made it work in a very strange way that can't explain. Here is the piece of code, the wrong part is where the flipped var is checked:

 
unsigned char flipped = 0;
 
...
 
unsigned char checkInput(void)
{
    do
    {
        if (kbhit())
        {
            switch (cgetc())
            {
            case 'F':
                if(!flipped)  //without the ! operator this doesn't work. At the first try i used the compact form flipped=1-flipped; that doesn't work too 
                    flipped = 0; //instead of (!flipped), also used (flipped==0). Same result 
                else
                    flipped = 1;
                tgi_flip();
                break;
            case 'P':
                if(halted)
                {
                    halted = 0;
                }
                else
                {
                    halted = 1;
                }
                break;
            case 'R':
                reset=1;
                break;

            default:
                break;
            }
        }
    } while(halted && !reset);
    return joy_read(JOY_1);
}
 

Attached there is a full code example that when compiled works fine, despite the nonsense behaviour of the "flipped" variable.

 

Don't know if I'm very tired and I can't see a trivial reason for this, or if this is a weird compiler problem.

 

 

Attached Files

  • Attached File  test.zip   12.35KB   14 downloads


#2 LordKraken OFFLINE  

LordKraken

    Chopper Commander

  • 144 posts
  • Location:Sverige

Posted Fri Feb 8, 2019 3:30 PM

Code seems correct to me. Could you try:

1) having curly braces after each case

2) using the form flipped = !flipped

 

then the code would be something like:

case 'F': {
    flipped = !flipped;
    tgi_flip();
} break;
case 'P': {
    halted = !halted;
} break;


Edited by LordKraken, Fri Feb 8, 2019 3:31 PM.


#3 karri ONLINE  

karri

    River Patroller

  • 2,592 posts
  • Location:Espoo, Finland

Posted Fri Feb 8, 2019 3:34 PM

The code you wrote works exactly as you have written.

if(!flipped) // Means flipped==0
    flipped = 0; // So if flipped is already 0 set it to 0
else
    flipped = 1;

I don't know why you want to know if the screen is flipped or not. Flipping the screen does not affect any button operations.

 

If you try to manually change up with down button or left with right you are just messing up your code.


Edited by karri, Fri Feb 8, 2019 3:38 PM.


#4 42bs OFFLINE  

42bs

    Moonsweeper

  • 303 posts
  • Location:Germany/Southest West

Posted Fri Feb 8, 2019 4:05 PM

This sounds like a compiler bug.

In the first case you do not use curly braces for the if else.

Did you check the generated code?



#5 Nop90 OFFLINE  

Nop90

    Star Raider

  • Topic Starter
  • 71 posts
  • Location:Italy

Posted Fri Feb 8, 2019 4:45 PM

The code you wrote works exactly as you have written.

if(!flipped) // Means flipped==0
    flipped = 0; // So if flipped is already 0 set it to 0
else
    flipped = 1;

I don't know why you want to know if the screen is flipped or not. Flipping the screen does not affect any button operations.

 

If you try to manually change up with down button or left with right you are just messing up your code.

 

I use that variable this way:

 

#define UP_BUTTON (flipped?64:128)

 

than to check dircrection pad respect the screen orientation with (checkInput&UP_BUTTON).

 

This sounds like a compiler bug.

In the first case you do not use curly braces for the if else.

Did you check the generated code?

 

braces don't change the situation. They are not needed with a sigle instruction, but usually I add them, and inddeed they are present in the xump code from where the example code is taken.

 

I checked the generated code and it seems to do what is written in c, i.e. doesen't swap the variable value:

 

L0028:    lda     _flipped
    beq     L00AE
    lda     #$01
L00AE:    sta     _flipped
    lda     #$01
    jsr     pusha
    ldx     #$00
    txa
    jsr     _tgi_ioctl
    bra     L0026
 
 

 

doesn't seems a compiler bug, maybe a problem with the linker?

 

Please try to compile the code to check if it happens to you too. I had a broken version of cc65 that should have been fixed installing the package posted by karri, but maybe there is something else to fix.



#6 Nop90 OFFLINE  

Nop90

    Star Raider

  • Topic Starter
  • 71 posts
  • Location:Italy

Posted Fri Feb 8, 2019 5:04 PM


If you try to manually change up with down button or left with right you are just messing up your code.

 

Ok, I got the point. The code is correct because i don't need to swap keys.

 

Probably I'm really too tired, because i thought to have checked the key behavior and that a kwy swap was neded.

 

Sorry



#7 42bs OFFLINE  

42bs

    Moonsweeper

  • 303 posts
  • Location:Germany/Southest West

Posted Sat Feb 9, 2019 2:59 AM

 


I checked the generated code and it seems to do what is written in c, i.e. doesen't swap the variable value:

L0028:    lda     _flipped
    beq     L00AE
    lda     #$01
L00AE:    sta     _flipped
    lda     #$01
    jsr     pusha
    ldx     #$00
    txa
    jsr     _tgi_ioctl
    bra     L0026
 
 

 

doesn't seems a compiler bug, maybe a problem with the linker?

 

I mean: Check the code from the _not_ working version.

Did you try it w/o optimization? Sometimes the optimizer is too aggressive.

 

My cc65 (cc65 V2.17 - Git 88d1d20c) produces for the if (flipped) version correct code. Though different code for the if (halted) !?!?

 

BTW: a = 1-a; is better than a = !a; the latter use a subroutine.

 

Edit: Best is: a ^= 1; ;-)


Edited by 42bs, Sat Feb 9, 2019 3:28 AM.





0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users