1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
; Altirra BASIC - I/O module
; Copyright (C) 2014 Avery Lee, All Rights Reserved.
;
; Copying and distribution of this file, with or without modification,
; are permitted in any medium without royalty provided the copyright
; notice and this notice are preserved. This file is offered as-is,
; without any warranty.
;==========================================================================
; Print a message from the message database.
;
; Entry:
; X = LSB of message pointer
;
.proc IoPrintMessageIOCB0
lda #0
sta iocbidx
.def :IoPrintMessage
stx inbuff
loop:
ldx inbuff
lda msg_base,x
beq printStringINBUFF.xit
jsr IoPutCharAndInc
bpl loop ;!! - unconditional
.endp
;==========================================================================
IoPrintInt:
jsr ifp
IoPrintNumber:
jsr fasc
.proc printStringINBUFF
loop:
ldy #0
lda (inbuff),y
pha
and #$7f
jsr IoPutCharAndInc
pla
bpl loop
xit:
rts
.endp
;==========================================================================
.proc IoConvNumToHex
php
jsr ldbufa
jsr fpi
ldy #0
plp
lda fr0+1
bcc force_16bit
beq print_8bit
force_16bit:
jsr print_digit_pair
print_8bit:
lda fr0
print_digit_pair:
pha
lsr
lsr
lsr
lsr
jsr print_digit
pla
and #$0f
print_digit:
cmp #10
scc:adc #6
adc #$30
sta (inbuff),y
iny
rts
.endp
;==========================================================================
.proc IoPutCharAndInc
inc inbuff
sne:inc inbuff+1
bne putchar ;! - unconditional
.endp
;==========================================================================
IoPutCharDirect = putchar.direct
IoPutNewline:
lda #$9b
dta {bit $0100}
IoPutSpace:
lda #' '
.proc putchar
dec ioPrintCol
bne not_tabstop
mvx ptabw ioPrintCol
not_tabstop:
direct:
ldx iocbidx
jsr dispatch
tya
.def :ioCheck = * ;requires iocbidx
bpl done
.def :IoCloseIOCB7AndThrowError
sty errno
;Check if we were using IOCB#7 and close it if so. It's intentional
;that we do this for any I/O error on #7 even if it's from explicit
;program usage, for compatibility.
lda iocbidx
eor #$70
bne not_iocb7
sta iocbexec ;clear ENTER
jsr IoClose
not_iocb7:
jmp errorDispatch
dispatch:
sta ciochr
lda icax1,x
sta icax1z
lda icpth,x
pha
lda icptl,x
pha
lda ciochr
done:
rts
.endp
;==========================================================================
.proc IoReadLineX
jsr IoSetupReadLineLDBUFA_SetIOCBX
jsr ciov
bpl putchar.done
cpy #$88
bne IoCloseIOCB7AndThrowError
rts
.endp
;==========================================================================
; Issue I/O call with a filename.
;
; Entry:
; A = command to run
; fr0 = Pointer to string info (ptr/len)
; iocbidx = IOCB to use
;
; ICBAL/ICBAH is automatically filled in by this fn. Because BASIC strings
; are not terminated, this routine temporarily overwrites the end of the
; string with an EOL, issues the CIO call, and then restores that byte.
; The string is limited to 255 characters.
;
; I/O errors are checked after calling CIO and the error handler is issued
; if one occurs.
;
IoDoOpenReadWithFilename:
lda #4
IoDoOpenWithFilename:
ldx iocbidx
sta icax1,x
lda #CIOCmdOpen
.proc IoDoWithFilename
;stash command
ldx iocbidx
pha
;call CIO
jsr IoTerminateString
jsr IoSetupBufferAddress
pla
jsr IoTryCmdX
jsr IoUnterminateString
;now we can check for errors and exit
jmp ioCheck
.endp
;==========================================================================
IoSetupIOCB7AndEval:
jsr IoSetupIOCB7
jmp evaluate
;==========================================================================
IoSetupIOCB7:
ldx #$70
stx iocbidx
IoCloseX = IoClose.with_IOCB_X
.proc IoClose
ldx iocbidx
with_IOCB_X:
lda #CIOCmdClose
.def :IoTryCmdX = *
sta iccmd,x
jmp ciov
.endp
;==========================================================================
; Open the cassette (C:) device or any other stock device.
;
; Entry (IoOpenCassette):
; None
;
; Entry (IoOpenStockDeviceIOCB7):
; A = AUX1 mode
; Y = Low byte of device name address in constant page
;
; Entry (IoOpenStockDeviceX):
; A = AUX1 mode
; X = IOCB #
; Y = Low byte of device name address in constant page
;
IoOpenCassette:
sec
ror icax2+$70
ldy #<devname_c
IoOpenStockDeviceIOCB7:
ldx #$70
IoOpenStockDeviceX:
stx iocbidx
sty stScratch4
pha
jsr IoCloseX
pla
sta icax1,x
lda stScratch4
ldy #>devname_c
jsr IoSetupBufferAddress
lda #CIOCmdOpen
IoDoCmd:
ldx iocbidx
sta iccmd,x
ioChecked: ;iocbidx clean
jsr ciov
jmp ioCheck
;==========================================================================
; Replace the byte after a string with an EOL terminator.
;
; Entry:
; FR0 = string pointer
; FR0+2 = string length (16-bit)
; Registers:
; A, Y modified; X preserved
;
; Exit:
; INBUFF = string pointer
;
; This is needed anywhere where a substring needs to be passed to a module
; that expects a terminated string, such as the math pack or CIO. This
; will temporarily munge the byte _after_ the string, which can be a
; following program token, the first byte of another string or array, or
; even the runtime stack. Therefore, the following byte MUST be restored
; ASAP.
;
; The length of the string is limited to 255 characters.
;
.proc IoTerminateString
;compute termination offset
ldy fr0+2
lda fr0+3
seq:ldy #$ff
sty ioTermOff
;save existing byte
lda (fr0),y
sta ioTermSave
inc ioTermFlag ;!! - must be first in case reset happens in between
;stomp it with an EOL
lda #$9b
sta (fr0),y
;copy term address
.def :IoSetInbuffFR0 = *
lda fr0
ldy fr0+1
.def :IoSetInbuffYA = *
sta inbuff
sty inbuff+1
rts
.endp
;==========================================================================
; Entry:
; INBUFF = string pointer
;
; Registers:
; Y, P.C preserved
; P.NZ set by Y
;
.proc IoUnterminateString
tya
pha
ldy ioTermOff
lda ioTermSave
sta (inbuff),y
dec ioTermFlag
pla
tay
rts
.endp
;==========================================================================
IoSetupReadLineLDBUFA_SetIOCBX:
stx iocbidx
IoSetupReadLineLDBUFA_X:
jsr ldbufa
.proc IoSetupReadLine
;we are using some pretty bad hacks here:
;- GET RECORD and >LBUFF are $05
;- <LBUFF is $80
ldy #$05
lda #$80
jsr IoSetupBufferAddress
sta iccmd,x
ldy #$ff
.def :IoSetupBufferLengthY
lda #$00
.def :IoSetupBufferLengthAY
sta icblh,x
tya
sta icbll,x
rts
.endp
|