Keyboard Input in Assembly - advice sought

Basic and Machine Language

Moderator: Moderators

Post Reply
MartinC
Vic 20 Drifter
Posts: 33
Joined: Tue Oct 25, 2022 12:18 pm
Website: https://winterfam.co.uk
Location: Kent,uk
Occupation: Author

Keyboard Input in Assembly - advice sought

Post by MartinC »

Hi all,

I'm still working on my Apollo DSKY on the 3k Vic 20 :D

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
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: Keyboard Input in Assembly - advice sought

Post 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.
Last edited by chysn on Sat Sep 16, 2023 10:27 am, edited 3 times in total.
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Keyboard Input in Assembly - advice sought

Post 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?
tlr
Vic 20 Nerd
Posts: 567
Joined: Mon Oct 04, 2004 10:53 am

Re: Keyboard Input in Assembly - advice sought

Post 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?
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: Keyboard Input in Assembly - advice sought

Post 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.
MartinC
Vic 20 Drifter
Posts: 33
Joined: Tue Oct 25, 2022 12:18 pm
Website: https://winterfam.co.uk
Location: Kent,uk
Occupation: Author

Re: Keyboard Input in Assembly - advice sought

Post by MartinC »

ChatGPT, no way - I'm just learning and making mistakes, I don't need a machine to "do" that for me. :D

"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.
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Keyboard Input in Assembly - advice sought

Post 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.
MartinC
Vic 20 Drifter
Posts: 33
Joined: Tue Oct 25, 2022 12:18 pm
Website: https://winterfam.co.uk
Location: Kent,uk
Occupation: Author

Re: Keyboard Input in Assembly - advice sought

Post 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 :D ) 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
User avatar
thegg
Vic 20 Amateur
Posts: 69
Joined: Mon Aug 30, 2021 4:49 am
Location: England
Occupation: retired

Re: Keyboard Input in Assembly - advice sought

Post 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. :wink:
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Keyboard Input in Assembly - advice sought

Post 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.
MartinC
Vic 20 Drifter
Posts: 33
Joined: Tue Oct 25, 2022 12:18 pm
Website: https://winterfam.co.uk
Location: Kent,uk
Occupation: Author

Re: Keyboard Input in Assembly - advice sought

Post 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
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: Keyboard Input in Assembly - advice sought

Post 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.
MartinC
Vic 20 Drifter
Posts: 33
Joined: Tue Oct 25, 2022 12:18 pm
Website: https://winterfam.co.uk
Location: Kent,uk
Occupation: Author

Re: Keyboard Input in Assembly - advice sought

Post by MartinC »

Hey chysm - you did and I have used it as a basis for what I'm doing. Many thanks.
MartinC
Vic 20 Drifter
Posts: 33
Joined: Tue Oct 25, 2022 12:18 pm
Website: https://winterfam.co.uk
Location: Kent,uk
Occupation: Author

Re: Keyboard Input in Assembly - advice sought

Post 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                
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: Keyboard Input in Assembly - advice sought

Post 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.
Post Reply