6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Mon May 20, 2024 10:59 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Jul 19, 2016 8:05 am 
Offline

Joined: Sat Aug 21, 2010 7:52 am
Posts: 231
Location: Arlington VA
This is for any PET/C=64/VIC-20. ?BUTTON returns the key value, or 0 (false) if neither button is pressed. It is limited to just the SPACE and RETURN characters, it should return the key value exactly once, when the key is first pressed, and key repeat is disabled. The key has to be released and pressed again to generate another 13 (or 32).

It should be possible to fit `?BUTTON` in fewer than 38 bytes but I'm too confused at the moment to golf it down. I'm pretty sure this is called "latched input." Please, have at it.
Code:
: foo
    begin
        ?button .
    ?terminal until ;  ok
foo 13 0 0 0 0 0 0 0 0 0 0 32 0 0 0 13 0
 0 0 0 0 0 0 32 0 0 0 0 0 13 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 13 0 0 0 0 0 0 0 0 0 0
0 0 13 0 0 0 0 0 0 0 0 0 0 0 32 0 0 0 0
0 0 0 0 0 0 0 0 0 0 32 0 0 0 0 0 0 0...

my rubbish, below. tested, working, bloated.
Code:
;--------------------------------------------------------------
#if 0
name=?BUTTON
stack=( -- keyval|false )
tags=mmm
Detects a leading edge transition of either spacebar or return
key and leaves either key value on the stack, otherwise [[FALSE]]

LSTX contains the ASCII value of the current keypress
or 255 if no key is pressed
#endif
qbutton
    lda LSTX        ; peek(151) hex $97 -- Current Key Pressed 255 = No Key
    ldy latch
    beq testkeys
    ldy #0
    cmp latch
    bne unlatch
testkeys
    cmp #13
    beq latchit
    cmp #32
    bne unlatch
latchit
    eor latch
    beq fail
    .byt $24        ; BIT zp
unlatch
    tya
    sta latch
    .byt $24        ; BIT zp
fail
    tya
    jmp pushya
latch
    .byt $0


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 20, 2016 5:37 am 
Offline

Joined: Fri Apr 15, 2016 1:03 am
Posts: 136
Maybe something like this...

Code:
qbutton:
   lda LSTX   ;get current button value, 255=none
   cmp latch   ;changed?
   beq @7
   sta latch   ;remember new state
   cmp #13   ;acceptable value?
   beq @8
   cmp #32
   beq @8
@7   lda #0   ;return false
@8   ldy #0   ;return A
   jmp pushya
latch .byt 255




Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 20, 2016 1:23 pm 
Offline

Joined: Sat Aug 21, 2010 7:52 am
Posts: 231
Location: Arlington VA
That's beautiful! Thank you! I moved 'ldy #0' to the top and changed 'lda #0' to 'tya' saving one more byte. Because there's always one more.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: