Page 1 of 1
Keyboard Input in Assembly - advice sought
Posted: Fri Sep 15, 2023 9:16 am
by MartinC
Hi all,
I'm still working on my Apollo DSKY on the 3k Vic 20
I'm trying to. Move the cursor to a specific row/col , get a 2 character input and check that it is made up of numbers.
Here's my code - suffice to say it doesn't work quite right...
Code: Select all
verb_input jsr t_clbuff ; clear kb buffer
lda #0
sta TMPCH ; initialise good input flag
lda #13 ; set input cursor location column
sta $00ca
lda #6 ; set input cursor location row
sta $00c9
sta $00cc ; turn cursor on
vi_getchr jsr $f20e ; call CHRIN
lda $00c6 ; how many keys pressed
cmp #2 ; verbs are 2 characters long
bne vi_getchr ; go back to get next char
ldx #2 ; we'll loop twice as their should be 2 chars
ldy #10 ; we'll scan for 0 to 9 in the kb buffer
vi_readbuf lda $277,x ; read char from kb buffer
vi_ckval cmp VALIDKEYS,y ; check the key is numeric
beq vi_goodkey ; if we detect a numeric set a flag
vi_cont dey ; decrement y
bne vi_ckval ; if not done loop for next key check
lda TMPCH ; was a good input made? if so TMPCH will be 1
beq vi_exit ; if not exit
dex ; loop to next character
bne vi_readbuf ; loop back for next char
vi_exit lda #1 ; tidy up
sta $00cc ; turn cursor off
jsr t_clbuff ; clear kb buffer
rts ; done
vi_goodkey lda #1 ; flag that a good key was pressed
sta TMPCH ; save the flag as a 1
jmp vi_cont ; continue processing
Any advice for a noob or alternate approaches?
Thanks
Re: Keyboard Input in Assembly - advice sought
Posted: Fri Sep 15, 2023 9:43 am
by chysn
I had to do something
fairly close to this. That is, position the cursor in a specific place, then get a certain number of valid characters. I had to accept letters instead of numbers, and five characters instead of two, but these are easy changes to make.
See this source file:
https://github.com/Chysn/VIC20-20RDLE/b ... 20rdle.asm
Specifically, look at the Main routine (line 154) that gets and validates the input. Also see the Pos routine (line 341) that uses the KERNAL's PLOT to position and advance the cursor on the screen; just setting those zero-page locations won't do it.
Note that PLOT uses the X register for the y coordinate and the Y register for the x coordinate. Commodore wanted to keep us on our toes!
My code is pretty well-commented, but I welcome questions about what's going on.
Re: Keyboard Input in Assembly - advice sought
Posted: Fri Sep 15, 2023 9:50 am
by Mike
Some remarks:
- "it doesn't work quite right" is not exactly helpful to describe what you might think is wrong with the routine.
- What is "jsr t_clbuff" doing?
- Why the need to throw wrenches into the gears of the KERNAL between "verb_input" and "vi_getchr" instead of cleanly calling $FFF0 to set the cursor position?
- Same applies to switching on the cursor. Why?
- Same applies to $F20E. Call GETIN with the KERNAL jump table please.
- Why $00xx to access the zeropage?
- You thrash the key GETIN read when reading $00C6.
- You read from the keyboard buffer but it's actually (sort of) empty when all pending keys have been read. What you want, you get from JSR $FFE4.
In short, you waste quite a lot of instructions at things that are not actually related to the problem at hand. You simply want to call JSR $FFE4, compare the return value in A against the digit range, store up to two digits away, and return upon two digits found. Perhaps with some extra handling for the STOP key to abort input. Having a blinking cursor somewhere is just fluff.
The code suspiciously looks like inquired from ChatGPT first, ChatGPT failed at it, and now you ask here to sort things out?
Re: Keyboard Input in Assembly - advice sought
Posted: Sat Sep 16, 2023 8:31 am
by tlr
I agree with Mike. This looks overly complicated and has all sorts of incorrect assumptions built in to it.
It's seldom a good strategy to code a complete solution and then debug it. Why not start to get a simple two key input working using $FFE4 and then add the cursor + positioning?
Re: Keyboard Input in Assembly - advice sought
Posted: Sun Sep 17, 2023 11:25 am
by chysn
I spent some time playing around with the cursor. My original thought was that you should reset the cursor countdown after each keypress. But no, that's not how it works; the cursor keeps the same beat as you type. But you do need to reset the cursor's phase ($cf) after each character, otherwise it phases in and out with each keypress:
Code: Select all
lda #0
sta $cc
wait: jsr $ffe4
cmp #0
beq wait
jsr $ffd2
lda #0
sta $cf
jmp wait
This code is not complete, because it will leave "cursors" behind as you press RETURN or cursor control keys. But it sounds like you'll be filtering those kinds of events out anyway.
Re: Keyboard Input in Assembly - advice sought
Posted: Fri Sep 22, 2023 9:16 am
by MartinC
ChatGPT, no way - I'm just learning and making mistakes, I don't need a machine to "do" that for me.
"it doesn't work quite right" is not exactly helpful to describe what you might think is wrong with the routine.
Ok, so I think I'm probably calling the wrong routines to get keyboard input. The cursor positioning doesn't work as expected (just goes to top left corner). Plus, I have to terminate input with return before the validation runs, I believe is what is happening.
What is "jsr t_clbuff" doing?
Code: Select all
t_clbuff LDA #0
STA $c6 ; clear keyboard buffer
rts
Why the need to throw wrenches into the gears of the KERNAL between "verb_input" and "vi_getchr" instead of cleanly calling $FFF0 to set the cursor position?
Because I'm a learner - I'll work out how to use $FFF0 instead
Same applies to switching on the cursor. Why?
I wanted some indication that input was happening in the right spot.
Same applies to $F20E. Call GETIN with the KERNAL jump table please.
Why $00xx to access the zeropage?
Newbie mistakes.
You thrash the key GETIN read when reading $00C6.
Newbie mistakes.
You read from the keyboard buffer but it's actually (sort of) empty when all pending keys have been read. What you want, you get from JSR $FFE4.
Newbie mistakes.
Mike, you seem ticked off with me, not sure why, but thanks to you and everyone else for the responses. I'll review your feedback and adjust my code.
Re: Keyboard Input in Assembly - advice sought
Posted: Fri Sep 22, 2023 9:47 am
by Mike
MartinC wrote:Mike, you seem ticked off with me, not sure why, but thanks to you and everyone else for the responses. I'll review your feedback and adjust my code.
Please note the most part of my previous reply concerns a critical judgement of the code, and not of your person in any kind.
The concluding statement highlights my thoughts about that the code snippet you present here is actually so badly written code, that I seriously cannot imagine it being written by a human who had at least a little exposure in the matter of programming, machine code for the 65xx in particular. You have been working on the DSKY prop for several months now, so there is not much of a "newbie bonus" left. You write there is no inquiry of yours to an AI involved, I am happy with that.
Do you have a working version (i.e. up to spec) of the DSKY program in BASIC? That means, a program which exactly displays what you want, reads the keyboard, and also changes the display according to what you want?
If so, post the program here, and we can go together to make a 1:1 translation to machine code in a fraction of the time you already spent on this.
Re: Keyboard Input in Assembly - advice sought
Posted: Fri Sep 29, 2023 9:43 am
by MartinC
Hey Mike,
I'm not working on this very often, it's a hobby project I revisit on Fridays after work (you'll be be pleased to know I'm not a programmer

) When I have time.
I don't have a BASIC version as I'm using this project to learn Assembly language.
The responses from the others in the thread have been helpful. I'll keep trying to get better referring to their examples. Thanks for the feedback.
Thanks
Martin
Re: Keyboard Input in Assembly - advice sought
Posted: Fri Sep 29, 2023 10:46 am
by thegg
Martin,
Do you have some sort of specification for your project? For example, some text describing the requirements, diagrams, or anything that is guiding your development.
It would be helpful for anybody wanting to help you if you can describe the functionality of the code you are struggling with and how it fits into your overall program.

Re: Keyboard Input in Assembly - advice sought
Posted: Fri Sep 29, 2023 11:42 am
by Mike
MartinC wrote:[...] it's a hobby project [...]
You will need to spend considerably more time on this project than just your Friday evenings after work if you really want a result to see the light of day:
I don't have a BASIC version [...]
That is very ... unfortunate.
Let me put this straight: you need some kind of high level description what the program is supposed to do. Either a working BASIC program or a complete specification in plain text. Either would indicate you have things thought out in sufficient detail to attempt a translation to machine language. Without these preparations, any help you ask for just means asking others to effectively design and write that program for you.
Re: Keyboard Input in Assembly - advice sought
Posted: Fri Oct 06, 2023 9:10 am
by MartinC
Hey mike,
Here's some pseudo pseudo code of what I am trying to achieve:
When 'V' is pressed
Loop:
Move cursor to a specific column and row on screen
Read a key
Verify it is numeric
If not numeric discard and jump to loop
If numeric display the entered number at the cursor location
Increment the cursor location
Increment loop counter
If second time through the loop exit else jump to loop
Wait for Return to be pressed
End
Thanks for your patience.
Martin
Re: Keyboard Input in Assembly - advice sought
Posted: Fri Oct 06, 2023 2:29 pm
by chysn
I already gave you a link to 6502 code for VIC-20 that does all this, but (1) letters instead of numbers and (2) five characters instead of two, both of which would be trivial to modify. Includes the sensible addition of handling the DELete key if someone makes a mistake.
You can damn near just copy and paste.
Re: Keyboard Input in Assembly - advice sought
Posted: Fri Oct 13, 2023 9:03 am
by MartinC
Hey chysm - you did and I have used it as a basis for what I'm doing. Many thanks.
Re: Keyboard Input in Assembly - advice sought
Posted: Thu Oct 19, 2023 9:58 am
by MartinC
Thanks for the pointers, I managed to achieve what I wanted. Posting in case it helps anyone learning in future.
Code: Select all
; process keyboard input of verbs or noun codes
proc_input jsr t_clbuff ; clear kb buffer
jsr clear_nv ; clear the current value from screen
lda #0
sta TMP ; init our input count
pi_plot ldx CROW ; set input cursor location row
ldy CCOL ; set input cursor location col
pi_getchr clc
jsr PLOT ; set cursor position
jsr GETIN ; call GETIN
cmp #0 ; was a key pressed?
beq pi_getchr ; if not loop back
pi_ckval cmp #"0" ; if <0 ignore
bcc pi_getchr ; ''
cmp #":" ; if > 9 ignore
bcs pi_getchr ; ''
inc TMP ; increment good input char count
pi_print pha ; save the char a moment
lda #159 ; set colour cyan
jsr CHROUT
pla ; get the char back
jsr CHROUT ; print character at current CCOL,CROW
inc CCOL ;move to next column
pi_exit lda TMP ; have we read 2 characters?
cmp #2 ; ''
bne pi_plot ; continue processing
rts
Re: Keyboard Input in Assembly - advice sought
Posted: Thu Oct 19, 2023 1:17 pm
by chysn
Just took a quick look, but consider:
Code: Select all
clc
jsr PLOT ; set cursor position
pi_getchr jsr GETIN ; call GETIN
cmp #0 ; was a key pressed?
beq pi_getchr ; if not loop back
There's no need to continuously perform PLOT.