4

I want to compare keystrokes in assembly (CCS64). If I type in the same key in a row I want to do something example: A A = do this

but if I type this: A B = do something else

Suggestions?

rsjaffe
  • 5,600
  • 7
  • 27
  • 39
Oakin
  • 143
  • 2
  • 15

2 Answers2

9

I prepared an example to you just like you've wanted. If you press same keys in a row twice, border color changes to red, otherwise stays black.

Warning! This example uses kernal routines. There is nothing wrong with that. But there is also a lower level way to do this without using $ffd2 (Output Vector, chrout) and $ffe4 (Get From Keyboad) kernal calls. But since it is much more complicated to understand, I prefered this example.

If you want to know what is happening behind the scenes (kernal calls), you can easily trace the kernal ROM codes from AAY64 documentation. Here is the links:

Main AAY Page: http://www.the-dreams.de/aay.html

AAY64 Online HTML Version: http://unusedino.de/ec64/technical/aay/c64/

Kernal ROM Listing: http://unusedino.de/ec64/technical/aay/c64/krnromma.htm

$ffd2 (Output Vector, chrout): http://unusedino.de/ec64/technical/aay/c64/romffd2.htm

$ffe4 (Get From Keyboad): http://unusedino.de/ec64/technical/aay/c64/romffe4.htm

You can browse deeper by pressing links on the opcodes and addresses.

Here goes the example code. You can compile this code using ACME Crossassembler which you can find here -> http://www.esw-heim.tu-clausthal.de/~marco/smorbrod/acme/

        !to "keycomp.prg",cbm

        zpBuffer = $fa  ; $fa-$fb are reserved for 2 bytes of key buffer

        * = $0801
        !byte $0c, $08, $00, $00, $9e, $32, $30, $36, $31, $00, $00, $00

        * = $080d

        ; key buffer initialization
        ldx #$f0        ; initialize key buffer
        stx zpBuffer    ; with two different
        inx             ; values to avoid instant
        stx zpBuffer+1  ; match at the beginning

        ; border color initialization
        lda #$00        ; set startup border color to black
        sta $d020       ; which means "no match"

        ; main loop
mainloop
        lda zpBuffer    ; shift key buffer
        sta zpBuffer+1  ; by one
readKey
        jsr $ffe4       ; read key
        beq readKey     ; if no key pressed loop forever
        jsr $ffd2       ; show key on the screen
        sta zpBuffer    ; store the key to key buffer

        lda zpBuffer    ; compare the last stored key
        cmp zpBuffer+1  ; with the old key value
        beq cmpMatch    ; if there is a match jmp to cmpMatch

        lda #$00        ; if two pressed keys are different
        sta $d020       ; change border color to black

        jmp cmpOut      ; skip the other condition code block
cmpMatch
        lda #$02        ; if there is a repeated key
        sta $d020       ; change border color to red
cmpOut
        jmp mainloop    ; wait for the next key
Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57
  • Excellent description and source code sample! Thank you very much @Emir Akaydın. –  Nov 25 '11 at 08:11
  • Akaydin thank you! Inspired by http://10print.org/ am trying to run some C64 assembly on VICE emulator on ubuntu 12.04. Still not sure how to compile "10 PRINT" example on page 234 of the book, but successfully able to use acme to compile your code above. Creates the "keycomp.prg" program, which can be run with VICE using command x64 keycomp.prg. Thank you! – Steve Koch Apr 20 '13 at 04:09
  • EDIT: The "10 PRINT" example also works, just after loading into emulator, need to run "SYS 4096" at the READY prompt for the maze program to start – Steve Koch Apr 20 '13 at 04:33
  • 1
    Steve, you're welcome. My code has a start-up byte sequence at $0801 address which is "0 SYS 2061" basic code in byte representation which points to $080d address. So, my code example autoruns. I deliberately added that line, so you wouldn't need to contend with that "SYS" basic line. :) – Emir Akaydın Apr 20 '13 at 15:56
  • 1
    And I learn even more! Now if I could only send this information back to my 8-year old self in 1982 :) – Steve Koch Apr 20 '13 at 16:46
3

I'm not a C64 person but I know 6502 assembly. You need to know two things to achieve your goal. The first is to learn 6502 assembly language if you don't already know about it. This page has excellent resources for example.

The second is to learn about C64 architecture and OS. It's called Kernal in Commodore speak, a quick google should point you in the right direction.

But there is an alternative. You can always use cc65, an excellent freeware package consisting of an almost-ISO-complient C compiler, a 6502 assembler, a linker and couple of other 6502 related tools. It has support for every popular 6502 platform including Atari 8-bit, Apple II and, of course, Commodore 64. It has a good amount of documentation and the people in the mailing list are nice and helpful. As a hint, keyboard entry and screen output functions are defined in conio.h.

cyco130
  • 4,654
  • 25
  • 34