Author Topic: Doh, assembly is not like a bicycle  (Read 868 times)

0 Members and 1 Guest are viewing this topic.

Offline menegator

  • CPC464
  • **
  • Posts: 13
  • Country: gr
    • index.php?action=treasury
  • Liked: 5
Doh, assembly is not like a bicycle
« on: 13:07, 19 March 17 »
Hello,


My last assembly program in z80 was written almost 25 years ago and for the love of God I cannot figure out why the above returns to basic without a key being pressed


Code: [Select]
org &4000
call &bb03


init
 ld B,78


loop
 ld a,0
 call &bb1e
 jr nz,up
 ld a,b
 call &bb1e
 jr nz,key
 djnz loop


jp init


.up
 ld hl,value
 ld (hl),0
 ret


.key
 ld hl,value
 ld a, b
 ld (hl),a
 ret


.value
 defb 99


In theory if a key is not pressed then it should jumb to start, but it doesn't happen and I can't figure out why.


How did I make it work some 25 years ago, I don't remebmer... Z80 assembly is not like a bicycle, if you leave it it leaves you :(

Offline fgbrain

  • CPC6128
  • ****
  • Posts: 192
  • Country: gr
    • index.php?action=treasury
    • Chaos CPC Homepage
  • Liked: 82
Re: Doh, assembly is not like a bicycle
« Reply #1 on: 13:56, 19 March 17 »

Most likely... I guess the  firmware routine corrupts your registers

You need storing them somehow (try PUSH and POP afterwards)

Futhermore, did you notice that even if B is not corrupted,
Code: [Select]
djnz loop
will count for 78 times (as you set register B)
_____

6128 (UK keyboard, Crtc type 0/2), 6128+ (UK keyboard), 3.5" and 5.25" drives, Reset switch and Digiblaster (selfmade), Inicron Romram box, Bryce Megaflash, SVideo & PS/2 mouse, , Magnum Lightgun, X-MEM, X4 Board, C4CPC, Multiface2 X4, RTC X4 and Gotek USB Floppy emulator.

Offline menegator

  • CPC464
  • **
  • Posts: 13
  • Country: gr
    • index.php?action=treasury
  • Liked: 5
Re: Doh, assembly is not like a bicycle
« Reply #2 on: 14:09, 19 March 17 »
Most likely... I guess the  firmware routine corrupts your registers

You need storing them somehow (try PUSH and POP afterwards)
The firmware call KM TEST KEY according to manual preserves all registers except A, C, HL.


Futhermore, did you notice that even if B is not corrupted,
Code: [Select]
djnz loop
will count for 78 times (as you set register B)
To my understanding the loop is going either to break, thus a key was pressed or will be exhausted without a key press, thus the jp to start.
« Last Edit: 14:15, 19 March 17 by menegator »

Offline fgbrain

  • CPC6128
  • ****
  • Posts: 192
  • Country: gr
    • index.php?action=treasury
    • Chaos CPC Homepage
  • Liked: 82
Re: Doh, assembly is not like a bicycle
« Reply #3 on: 14:30, 19 March 17 »
why dont you just omit DJNZ  and B ? seems you want an endless loop here..

Code: [Select]
loop
 ld a,0
 call &bb1e
 jr nz,up
 ld a,78
 call &bb1e
 jr nz,key
jp loop



and keep in mind :  C register lets you know if ctrl/shift was pressed
Quote
in: A=keyno, out: nz=pressed, C=ctrl/shift flags
_____

6128 (UK keyboard, Crtc type 0/2), 6128+ (UK keyboard), 3.5" and 5.25" drives, Reset switch and Digiblaster (selfmade), Inicron Romram box, Bryce Megaflash, SVideo & PS/2 mouse, , Magnum Lightgun, X-MEM, X4 Board, C4CPC, Multiface2 X4, RTC X4 and Gotek USB Floppy emulator.

Offline menegator

  • CPC464
  • **
  • Posts: 13
  • Country: gr
    • index.php?action=treasury
  • Liked: 5
Re: Doh, assembly is not like a bicycle
« Reply #4 on: 14:36, 19 March 17 »
I realized that I checked for key_up in each loop. I modified the code
Code: [Select]

org &4000
call &bb03


init
 ld B,78


loop
 ld a,b
 call &bb1e
 jr nz,key
 djnz loop


 ld a,0
 call &bb1e
 jr nz,up


jp init


.up
 ld hl,value
 ld (hl),0
 ret


.key
 ld hl,value
 ld a, b
 ld (hl),a
 ret


.value
 defb 99
but it still doesn't work and exits whether a key was pressed or not.




Offline menegator

  • CPC464
  • **
  • Posts: 13
  • Country: gr
    • index.php?action=treasury
  • Liked: 5
Re: Doh, assembly is not like a bicycle
« Reply #5 on: 14:40, 19 March 17 »
why dont you just omit DJNZ  and B ? seems you want an endless loop here..
Because I want to check if any key (and not only 78 or 0) if is pressed. This is used to a basic procedure to define keys. It could be done with a for loop in basic but it's too slow.


What I basically want is to test ANY key if it's pressed and if yes, then write the keycode to memory and return to basic. I don't want to use CALL &BB06 or CALL &BB18 because using these calls I can't capture for use control, shift and caps lock.
« Last Edit: 14:44, 19 March 17 by menegator »

Online Bryce

  • The Hardware Guy.
  • Supporter
  • 6128 Plus
  • *
  • Posts: 10.086
  • Country: wf
  • It's not broken, it just hasn't been fixed yet.
    • index.php?action=treasury
  • Liked: 3397
Re: Doh, assembly is not like a bicycle
« Reply #6 on: 10:45, 20 March 17 »
Hmmmm, that'll explain why the stabilisers didn't help when I was trying to get back into assembly programming.  ::)

Bryce.

Offline MacDeath

  • Chaotic post writer and mock-up specialist
  • Supporter
  • 6128 Plus
  • *
  • Posts: 4.030
  • Country: fr
  • Liked: 1093
Re: Doh, assembly is not like a bicycle
« Reply #7 on: 12:42, 20 March 17 »
Dear Bryce.

YOU truelly ARE THE most massive hardware dude, with a magnitue of density in the inventivity that makes no compromise and take no prisoners.

Your sense of design is so unparralleled, even unreachable to us mundane mortal joes.
We, the poor brained average humans, cannot even grasp the beginning of the concepts you would create effortlessly.
Awe is spillt from us as you rip into the very fabric of the universe like the fist of an angry god.

 :laugh:

Also we have even more proofs that the CPC was such a stable system.

Now shut up and take my money !!!
 [ Invalid Attachment ]

Is there a PLUS or a 6128 version ?
« Last Edit: 12:49, 20 March 17 by MacDeath »

Offline Axelay

  • 6128 Plus
  • ******
  • Posts: 530
  • Country: au
  • Liked: 330
Re: Doh, assembly is not like a bicycle
« Reply #8 on: 12:43, 20 March 17 »
I'm not familiar with this firmware routine, but I noticed after the call immediately returns, the 'value' memory location holds 18, which is the key for enter, the last key you press to issue the call &4000.  So maybe the problem is the key presses are being queued with that call?  I've chucked together a little variation on your routine that splits the key checking loop out of the overall loop, and then first calls it repeatedly until no key is detected as pressed (or queued, perhaps), and then repeatedly calls the same code again until a key is pressed.  It seems to do something more like you described wanting, though as I mentioned, I'm not familiar with the firmware routine so I'm only guessing at the problem.


I also changed the djnz to a dec b/jp p so the separate check for key 0 was unnecessary, and it then allowed the loop exit to flow through with A being loaded with an invalid key number (255) to indicate no key press to the outer loop.



Code: [Select]


org &4000
call &bb03


GetKey
; first check no key is currently pressed or queued
 call CheckKeys
 rla ; a returns holding key pressed value, or 255 if no key pressed, so check bit 7
 jr nc,GetKey ; repeat check while a key is being pressed or queued
; now check for a key press
FindKey
 call CheckKeys
 rla
 jr c,FindKey ; if bit 7 set in a then no key was pressed, repeat loop
 ret ; foud a key and it has been stored at value


CheckKeys
 ld b,78
keycheckloop
 ld a,b
 call &bb1e
 jr nz,keypressed
 dec b
 jp p,keycheckloop ; if b not reached 255, repeat
; b is 255, use as indicator that no key was pressed and flow through to standard exit
.keypressed
 ld a,b
 ld (value),a
 ret




.value
 defb 99
« Last Edit: 12:48, 20 March 17 by Axelay »

Offline menegator

  • CPC464
  • **
  • Posts: 13
  • Country: gr
    • index.php?action=treasury
  • Liked: 5
Re: Doh, assembly is not like a bicycle
« Reply #9 on: 18:32, 20 March 17 »
Thank you Axelay, your code did the trick!


I had the call to "&BB03" to clear the keyboard buffer in order to avoid what you mentioned (the key press was queued from basic) but apparently it didn't work.

Offline opqa

  • CPC664
  • ***
  • Posts: 73
  • Country: es
  • Liked: 89
Re: Doh, assembly is not like a bicycle
« Reply #10 on: 22:57, 20 March 17 »
This is so typical... There is nothing wrong with your original code, it runs well, "too" well in fact.

The problem is not related with key queuing, the firmware indeed maintains a keypress queue, but &bb1e doesn't care about it, it just informs you whether the indicated key is pressed or not. The only "problem" is just that your code is "too" fast. When you hit enter after having written "call &4000" the code starts executing immediately, it doesn't wait for you to release the Enter key. When the loop arrives at the point of checking this key, few milliseconds later, your finger is still there, you just haven't got enough time to lift it.

You can solve this "problem" in many ways, you can just add a long delay at the beginning of execution to give you time to release the key, you can try to detect a "not pressed -> pressed " transition as in Alexay variant instead of just a key pressed condition as in yours, you can test the routine from WinAPE by running it from the Assembler menu instead of calling it from BASIC...

Or you can do nothing, because depending on when and how you call this routine from your program this problem may or may not arise, it's your choice.
« Last Edit: 08:22, 21 March 17 by opqa »