Nop90 Posted February 8, 2019 Share Posted February 8, 2019 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 Quote Link to comment Share on other sites More sharing options...
LordKraken Posted February 8, 2019 Share Posted February 8, 2019 (edited) 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 February 8, 2019 by LordKraken Quote Link to comment Share on other sites More sharing options...
+karri Posted February 8, 2019 Share Posted February 8, 2019 (edited) 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 February 8, 2019 by karri Quote Link to comment Share on other sites More sharing options...
42bs Posted February 8, 2019 Share Posted February 8, 2019 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? Quote Link to comment Share on other sites More sharing options...
Nop90 Posted February 8, 2019 Author Share Posted February 8, 2019 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. Quote Link to comment Share on other sites More sharing options...
Nop90 Posted February 8, 2019 Author Share Posted February 8, 2019 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 1 Quote Link to comment Share on other sites More sharing options...
42bs Posted February 9, 2019 Share Posted February 9, 2019 (edited) 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 February 9, 2019 by 42bs Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.