Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

183 lines
5.1KB

  1. ; =============================================================
  2. ; kernal-io.asm - screen, keyboard, and disk I/O through the Kernal (ca65)
  3. ;
  4. ; Reads a line from the keyboard, echoes it, writes a SEQ file to drive 8,
  5. ; and prints the drive's status message.
  6. ;
  7. ; c64 run examples/kernal-io.asm
  8. ; c64 test examples/kernal-io.asm -Keys "hello\n" -Until main_done -Expect "YOU TYPED: HELLO"
  9. ; =============================================================
  10. CINT = $FF81
  11. SETLFS = $FFBA
  12. SETNAM = $FFBD
  13. OPEN = $FFC0
  14. CLOSE = $FFC3
  15. CHKIN = $FFC6
  16. CHKOUT = $FFC9
  17. CLRCHN = $FFCC
  18. CHRIN = $FFCF
  19. CHROUT = $FFD2
  20. GETIN = $FFE4
  21. READST = $FFB7
  22. ptr = $FB ; free zero page
  23. .segment "CODE"
  24. .proc main
  25. jsr CINT
  26. lda #<msg_welcome
  27. ldx #>msg_welcome
  28. jsr print_str
  29. jsr read_line ; text in inbuf, length in inlen
  30. lda #<msg_you_typed
  31. ldx #>msg_you_typed
  32. jsr print_str
  33. lda #<inbuf
  34. ldx #>inbuf
  35. jsr print_str
  36. lda #$0D
  37. jsr CHROUT
  38. jsr write_file
  39. jsr print_drive_status
  40. .endproc
  41. main_done: ; label for `c64 test -Until`
  42. rts
  43. ; ---------------------------------------------------------------
  44. ; print_str: print a zero-terminated string; A = low, X = high byte
  45. ; ---------------------------------------------------------------
  46. .proc print_str
  47. sta ptr
  48. stx ptr+1
  49. ldy #0
  50. loop: lda (ptr),y
  51. beq done
  52. jsr CHROUT
  53. iny
  54. bne loop
  55. done: rts
  56. .endproc
  57. ; ---------------------------------------------------------------
  58. ; read_line: read keys until RETURN; handles DEL; max 79 characters
  59. ; ---------------------------------------------------------------
  60. .proc read_line
  61. lda #0
  62. sta inlen ; the index lives in memory: GETIN clobbers X and Y
  63. loop: jsr GETIN ; 0 = no key yet
  64. beq loop
  65. ldx inlen
  66. cmp #$0D ; RETURN
  67. beq done
  68. cmp #$14 ; DEL
  69. beq del
  70. cpx #79 ; buffer full: ignore further keys
  71. beq loop
  72. sta inbuf,x
  73. inc inlen
  74. jsr CHROUT ; echo (CHROUT preserves X and Y)
  75. jmp loop
  76. del: cpx #0
  77. beq loop
  78. dec inlen
  79. jsr CHROUT ; A is still $14: erase on screen
  80. jmp loop
  81. done: lda #0
  82. sta inbuf,x
  83. lda #$0D
  84. jmp CHROUT
  85. .endproc
  86. ; ---------------------------------------------------------------
  87. ; write_file: OPEN 2,8,2,"@0:OUTPUT,S,W" and print data into it
  88. ; ---------------------------------------------------------------
  89. .proc write_file
  90. lda #2 ; logical file 2, device 8, secondary 2
  91. ldx #8
  92. ldy #2
  93. jsr SETLFS
  94. lda #outname_len
  95. ldx #<outname
  96. ldy #>outname
  97. jsr SETNAM
  98. jsr OPEN
  99. bcs open_error ; carry set = Kernal error (A = error number)
  100. ldx #2
  101. jsr CHKOUT ; send CHROUT output to file 2
  102. bcs chkout_error
  103. ldx #0
  104. loop: lda filedata,x
  105. beq close
  106. jsr CHROUT
  107. inx
  108. bne loop
  109. close: jsr CLRCHN ; output back to the screen
  110. lda #2
  111. jmp CLOSE
  112. chkout_error:
  113. jsr CLRCHN
  114. lda #2
  115. jsr CLOSE
  116. lda #<msg_chkout_error
  117. ldx #>msg_chkout_error
  118. jmp print_str
  119. open_error:
  120. lda #<msg_open_error
  121. ldx #>msg_open_error
  122. jmp print_str
  123. .endproc
  124. ; ---------------------------------------------------------------
  125. ; print_drive_status: read the command channel (15) and print it,
  126. ; e.g. "00, OK,00,00" or "74,DRIVE NOT READY,00,00"
  127. ; ---------------------------------------------------------------
  128. .proc print_drive_status
  129. lda #15
  130. ldx #8
  131. ldy #15
  132. jsr SETLFS
  133. lda #0 ; no filename
  134. jsr SETNAM
  135. jsr OPEN
  136. bcs done
  137. ldx #15
  138. jsr CHKIN
  139. bcs close
  140. loop: jsr CHRIN
  141. pha
  142. jsr READST ; status bit 6 = EOI after the last byte
  143. tax
  144. pla
  145. jsr CHROUT
  146. txa
  147. and #$40
  148. beq loop
  149. close: jsr CLRCHN
  150. lda #15
  151. jsr CLOSE
  152. done: rts
  153. .endproc
  154. .segment "RODATA"
  155. ; Lower-case source text displays as upper case on the C64 (ca65 PETSCII charmap).
  156. msg_welcome: .byte $93, "kernal i/o demo", $0D, "type something and press return:", $0D, 0
  157. msg_you_typed: .byte "you typed: ", 0
  158. msg_open_error: .byte "error: could not open file", $0D, 0
  159. msg_chkout_error: .byte "error: could not redirect output", $0D, 0
  160. outname: .byte "@0:output,s,w"
  161. outname_len = * - outname
  162. filedata: .byte "data line 1", $0D, "data line 2", $0D, 0
  163. .segment "BSS"
  164. inbuf: .res 80
  165. inlen: .res 1

Powered by TurnKey Linux.