Jump to content
IGNORED

A little piece of code that is driving me crazy


Nop90

Recommended Posts

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.

 

 

test.zip

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

 

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...