2000-05-28 13:40:48 +00:00
|
|
|
;
|
2005-07-22 22:48:43 +00:00
|
|
|
; Christian Groessler, Jul-2005
|
2000-05-28 13:40:48 +00:00
|
|
|
;
|
2002-12-06 00:24:35 +00:00
|
|
|
; int __fastcall__ read(int fd,void *buf,int count)
|
2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.include "atari.inc"
|
2022-08-28 19:52:53 +00:00
|
|
|
.import __rwsetup,__do_oserror,__inviocb,___oserror
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _read
|
|
|
|
|
|
|
|
_read: jsr __rwsetup ; do common setup for read and write
|
|
|
|
beq done ; if size 0, it's a no-op
|
|
|
|
cpx #$FF ; invalid iocb?
|
|
|
|
beq _inviocb
|
|
|
|
|
|
|
|
.ifdef LINEBUF
|
|
|
|
; E: should be always at IOCB #0
|
|
|
|
; fixme: what happens when user closes and reopens stdin?
|
|
|
|
cpx #0 ; E: handler (line oriented keyboard input)?
|
|
|
|
beq do_line
|
2004-05-28 22:01:14 +00:00
|
|
|
.endif
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #GETCHR ; iocb command code
|
|
|
|
sta ICCOM,x
|
|
|
|
jsr CIOV ; read it
|
|
|
|
bpl done
|
|
|
|
cpy #EOFERR ; eof is treated specially
|
|
|
|
beq done
|
|
|
|
jmp __do_oserror ; update errno
|
|
|
|
|
|
|
|
done: lda ICBLL,x ; buf len lo
|
|
|
|
pha ; save
|
|
|
|
lda ICBLH,x ; get buf len hi
|
|
|
|
tax ; to X
|
|
|
|
okdone: lda #0
|
2022-08-28 20:37:33 +00:00
|
|
|
sta ___oserror ; clear system dependend error code
|
2013-05-09 11:56:54 +00:00
|
|
|
pla ; get buf len lo
|
|
|
|
rts
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
_inviocb:
|
2013-05-09 11:56:54 +00:00
|
|
|
jmp __inviocb
|
2004-05-28 22:01:14 +00:00
|
|
|
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.ifdef LINEBUF
|
2004-05-28 22:01:14 +00:00
|
|
|
|
|
|
|
; line oriented input
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.segment "EXTZP" : zeropage
|
2006-04-09 11:43:03 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
index: .res 1 ; index into line buffer
|
|
|
|
cbs: .res 1 ; current buffer size: buflen - index
|
|
|
|
dataptr:.res 2 ; temp pointer to user buffer
|
|
|
|
copylen:.res 1 ; temp counter
|
2006-04-09 11:43:03 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.bss
|
2006-04-09 11:43:03 +00:00
|
|
|
|
2014-02-19 21:38:57 +00:00
|
|
|
buflen: .res 1 ; length of used part of buffer
|
2013-05-09 11:56:54 +00:00
|
|
|
linebuf:.res LINEBUF ; the line buffer
|
2006-04-09 11:43:03 +00:00
|
|
|
|
|
|
|
.code
|
|
|
|
|
2004-05-28 22:01:14 +00:00
|
|
|
do_line:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda buflen ; line buffer active?
|
|
|
|
bne use_buf ; yes, get data from there
|
|
|
|
|
|
|
|
; save user buffer address & length
|
|
|
|
; update IOCB to point to line buffer
|
|
|
|
lda ICBLL,x
|
|
|
|
pha
|
|
|
|
lda #LINEBUF
|
|
|
|
sta ICBLL,x
|
|
|
|
;--------
|
|
|
|
lda ICBLH,x
|
|
|
|
pha
|
|
|
|
lda #0
|
|
|
|
sta ICBLH,x
|
|
|
|
;--------
|
|
|
|
lda ICBAL,x
|
|
|
|
pha
|
|
|
|
lda #<linebuf
|
|
|
|
sta ICBAL,x
|
|
|
|
;--------
|
|
|
|
lda ICBAH,x
|
|
|
|
pha
|
|
|
|
lda #>linebuf
|
|
|
|
sta ICBAH,x
|
|
|
|
|
|
|
|
lda #GETREC
|
|
|
|
sta ICCOM,x
|
|
|
|
jsr CIOV ; read input data
|
|
|
|
bpl newbuf
|
|
|
|
cpy #EOFERR ; eof is treated specially
|
|
|
|
beq newbuf
|
|
|
|
pla ; fix stack
|
|
|
|
pla
|
|
|
|
pla
|
|
|
|
pla
|
|
|
|
jmp __do_oserror ; update errno
|
2004-05-28 22:01:14 +00:00
|
|
|
|
|
|
|
newbuf:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda ICBLL,x ; get # of bytes read
|
|
|
|
sta buflen
|
|
|
|
lda #0
|
|
|
|
sta index ; fresh buffer
|
|
|
|
|
|
|
|
; restore user buffer address & length
|
|
|
|
pla
|
|
|
|
sta ICBAH,x
|
|
|
|
;--------
|
|
|
|
pla
|
|
|
|
sta ICBAL,x
|
|
|
|
;--------
|
|
|
|
pla
|
|
|
|
sta ICBLH,x
|
|
|
|
;--------
|
|
|
|
pla
|
|
|
|
sta ICBLL,x
|
|
|
|
|
|
|
|
; fall into use_buf
|
|
|
|
lda buflen
|
2004-05-28 22:01:14 +00:00
|
|
|
|
|
|
|
; return bytes from line buffer
|
|
|
|
; use buflen and index to access buffer
|
|
|
|
; update index
|
|
|
|
; use dataptr as a temporary pointer
|
|
|
|
|
|
|
|
use_buf:
|
2013-05-09 11:56:54 +00:00
|
|
|
sec
|
|
|
|
sbc index ; size of unread data in the buffer
|
|
|
|
sta cbs
|
2004-05-28 22:01:14 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda ICBLL,x ; buf len lo
|
|
|
|
cmp cbs ; larger than buffer size?
|
|
|
|
beq bl1
|
|
|
|
bcs btsmall ; yes, adjust length
|
2004-05-28 22:01:14 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
bl1: lda ICBLH,x ; get buf len hi
|
|
|
|
bne btsmall ; buffer too small: buffer contents < read size
|
2004-05-28 22:01:14 +00:00
|
|
|
|
|
|
|
; copy ICBLL,x bytes
|
|
|
|
|
|
|
|
icbll_copy:
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda ICBAL,x ; buffer address
|
|
|
|
sta dataptr
|
|
|
|
lda ICBAH,x ; buffer address
|
|
|
|
sta dataptr+1
|
|
|
|
lda ICBLL,x
|
|
|
|
sta copylen
|
|
|
|
pha ; remember for return value
|
|
|
|
ldy #0
|
|
|
|
ldx index
|
|
|
|
|
|
|
|
copy: lda linebuf,x
|
|
|
|
sta (dataptr),y
|
|
|
|
iny
|
|
|
|
inx
|
|
|
|
dec copylen
|
|
|
|
bne copy
|
|
|
|
|
|
|
|
pla ; length
|
|
|
|
pha ; save length to return at okdone
|
|
|
|
|
|
|
|
clc
|
|
|
|
adc index
|
|
|
|
sta index
|
|
|
|
cmp buflen ; buffer used up?
|
|
|
|
bcc c1 ; not yet
|
|
|
|
|
|
|
|
lda #0
|
|
|
|
sta buflen ; indicate empty line buffer
|
|
|
|
|
|
|
|
c1: ldx #0
|
|
|
|
jmp okdone ; return to caller
|
2004-05-28 22:01:14 +00:00
|
|
|
|
|
|
|
btsmall:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda cbs
|
|
|
|
sta ICBLL,x
|
|
|
|
bpl icbll_copy
|
2004-05-28 22:01:14 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.endif ; .ifdef LINEBUF
|
2004-05-28 22:01:14 +00:00
|
|
|
|