|
- ; =============================================================
- ; kernal-io.asm - screen, keyboard, and disk I/O through the Kernal (ca65)
- ;
- ; Reads a line from the keyboard, echoes it, writes a SEQ file to drive 8,
- ; and prints the drive's status message.
- ;
- ; c64 run examples/kernal-io.asm
- ; c64 test examples/kernal-io.asm -Keys "hello\n" -Until main_done -Expect "YOU TYPED: HELLO"
- ; =============================================================
-
- CINT = $FF81
- SETLFS = $FFBA
- SETNAM = $FFBD
- OPEN = $FFC0
- CLOSE = $FFC3
- CHKIN = $FFC6
- CHKOUT = $FFC9
- CLRCHN = $FFCC
- CHRIN = $FFCF
- CHROUT = $FFD2
- GETIN = $FFE4
- READST = $FFB7
-
- ptr = $FB ; free zero page
-
- .segment "CODE"
- .proc main
- jsr CINT
-
- lda #<msg_welcome
- ldx #>msg_welcome
- jsr print_str
-
- jsr read_line ; text in inbuf, length in inlen
-
- lda #<msg_you_typed
- ldx #>msg_you_typed
- jsr print_str
- lda #<inbuf
- ldx #>inbuf
- jsr print_str
- lda #$0D
- jsr CHROUT
-
- jsr write_file
- jsr print_drive_status
- .endproc
- main_done: ; label for `c64 test -Until`
- rts
-
- ; ---------------------------------------------------------------
- ; print_str: print a zero-terminated string; A = low, X = high byte
- ; ---------------------------------------------------------------
- .proc print_str
- sta ptr
- stx ptr+1
- ldy #0
- loop: lda (ptr),y
- beq done
- jsr CHROUT
- iny
- bne loop
- done: rts
- .endproc
-
- ; ---------------------------------------------------------------
- ; read_line: read keys until RETURN; handles DEL; max 79 characters
- ; ---------------------------------------------------------------
- .proc read_line
- lda #0
- sta inlen ; the index lives in memory: GETIN clobbers X and Y
- loop: jsr GETIN ; 0 = no key yet
- beq loop
- ldx inlen
- cmp #$0D ; RETURN
- beq done
- cmp #$14 ; DEL
- beq del
- cpx #79 ; buffer full: ignore further keys
- beq loop
- sta inbuf,x
- inc inlen
- jsr CHROUT ; echo (CHROUT preserves X and Y)
- jmp loop
- del: cpx #0
- beq loop
- dec inlen
- jsr CHROUT ; A is still $14: erase on screen
- jmp loop
- done: lda #0
- sta inbuf,x
- lda #$0D
- jmp CHROUT
- .endproc
-
- ; ---------------------------------------------------------------
- ; write_file: OPEN 2,8,2,"@0:OUTPUT,S,W" and print data into it
- ; ---------------------------------------------------------------
- .proc write_file
- lda #2 ; logical file 2, device 8, secondary 2
- ldx #8
- ldy #2
- jsr SETLFS
- lda #outname_len
- ldx #<outname
- ldy #>outname
- jsr SETNAM
- jsr OPEN
- bcs open_error ; carry set = Kernal error (A = error number)
-
- ldx #2
- jsr CHKOUT ; send CHROUT output to file 2
- bcs chkout_error
-
- ldx #0
- loop: lda filedata,x
- beq close
- jsr CHROUT
- inx
- bne loop
-
- close: jsr CLRCHN ; output back to the screen
- lda #2
- jmp CLOSE
-
- chkout_error:
- jsr CLRCHN
- lda #2
- jsr CLOSE
- lda #<msg_chkout_error
- ldx #>msg_chkout_error
- jmp print_str
- open_error:
- lda #<msg_open_error
- ldx #>msg_open_error
- jmp print_str
- .endproc
-
- ; ---------------------------------------------------------------
- ; print_drive_status: read the command channel (15) and print it,
- ; e.g. "00, OK,00,00" or "74,DRIVE NOT READY,00,00"
- ; ---------------------------------------------------------------
- .proc print_drive_status
- lda #15
- ldx #8
- ldy #15
- jsr SETLFS
- lda #0 ; no filename
- jsr SETNAM
- jsr OPEN
- bcs done
- ldx #15
- jsr CHKIN
- bcs close
- loop: jsr CHRIN
- pha
- jsr READST ; status bit 6 = EOI after the last byte
- tax
- pla
- jsr CHROUT
- txa
- and #$40
- beq loop
- close: jsr CLRCHN
- lda #15
- jsr CLOSE
- done: rts
- .endproc
-
- .segment "RODATA"
- ; Lower-case source text displays as upper case on the C64 (ca65 PETSCII charmap).
- msg_welcome: .byte $93, "kernal i/o demo", $0D, "type something and press return:", $0D, 0
- msg_you_typed: .byte "you typed: ", 0
- msg_open_error: .byte "error: could not open file", $0D, 0
- msg_chkout_error: .byte "error: could not redirect output", $0D, 0
- outname: .byte "@0:output,s,w"
- outname_len = * - outname
- filedata: .byte "data line 1", $0D, "data line 2", $0D, 0
-
- .segment "BSS"
- inbuf: .res 80
- inlen: .res 1
|