* * Simple I/O library: * * OPEN - open file * CLOSE- close file * READ - read bytes from file * WRITE- write bytes to file * * SLJ 1/20/06 * IDE64 fast read/write Soci/Singular 2/20/06 * *------------------------------- * * Test code * do 0 ubyte lastdev@$ba; ubyte inbuf(255)@$0400 sprint "writing..." open(2,lastdev,2,#'@0:simpletest,p,w') write(2,#testmsg,300) ;write 300 bytes sprintln "done." close(2) sprint "reading..." open(3,lastdev,3,#'simpletest,p,r') read(3,#inbuf,110) ;read 110 bytes to screen sprintln "done." close(3) done testmsg txt 'This is some example text to test out ' txt 'the simpleio routines. For dark is ' txt 'the suede that mows like a harvest.' fin * * *------------------------------- * * Open file * * Like BASIC: OPEN(2,8,2,#"filename") * sub Open(@a, @x, @y, uint name@$bb)_ubyte error jsr $ffba ;setlfs error=0 ldy #$ff :x iny lda ($bb),y bne :x sty $b7 ;setnam jsr $ffc0 ;open bcc :ok inc error pha sprint "simpleio: open error #" pla ora #$30 jsr $ffd2 lda #13 jsr $ffd2 :ok endsub * * Close file * * Syntax: Close(fileno) * sub asm Close@$ffc3(@a) * * READ: Read N bytes from file and * copy to buffer. * * Syntax: * Read(file, #buffer, N) * * On exit: * Read_bytesin = number of bytes read (reads until EOF) * * Default i/o channels restored * sub Read(@x, @ay, uint N)_uint bytesin, ubyte error sta $bb sty $bc lda #0 sta bytesin sta bytesin+1 sta error lda $de60 cmp #$49 bne :ni lda $de61 cmp #$44 bne :ni lda $de62 cmp #$45 bne :ni stx :ni2+1 jsr $ffc6 ;chkin ldy N+1 beq :tooshort ;not worth the trouble... ldx N lda #$bb jsr $def4 ;read bcc :done2 cmp #9 ;illegal device? bne :err :ni2 ldx #0 :ni jsr $ffc6 ;chkin bcs :err :tooshort ldy #0 :loop lda bytesin cmp N lda bytesin+1 sbc N+1 bcs :done jsr $ffcf ;chrin :s sta ($bb),y inc bytesin bne :x inc bytesin+1 :x ldx $90 ;status? bne :done iny bne :loop inc $bc jmp :loop :done2 stx bytesin sty bytesin+1 :done jmp $ffcc ;clrchn :err inc error pha jsr $ffcc ;clrchn sprint "simpleio: read error #" pla ora #$30 jsr $ffd2 lda #13 jsr $ffd2 endsub * * Write N bytes to file * * Syntax: * Write(file, #buffer, N) * * On exit: * Write_bytesout = number of bytes written * * Default i/o channels restored * sub Write(@x, @ay, uint N)_uint bytesout, ubyte error sta $bb sty $bc lda #0 sta bytesout sta bytesout+1 sta error lda $de60 cmp #$49 bne :ni lda $de61 cmp #$44 bne :ni lda $de62 cmp #$45 bne :ni stx :ni2+1 jsr $ffc9 ;chkout ldy N+1 beq :tooshort ;not worth the trouble... ldx N lda #$bb jsr $def1 ;write bcc :done2 cmp #9 ;illegal device? bne :err :ni2 ldx #0 :ni jsr $ffc9 ;chkout bcs :err :tooshort ldy #0 :loop lda bytesout cmp N lda bytesout+1 sbc N+1 bcs :done :l lda ($bb),y jsr $ffd2 ldx $90 ;status? bne :done inc bytesout bne :x inc bytesout+1 :x iny bne :loop inc $bc jmp :loop :done2 stx bytesout sty bytesout+1 :done jmp $ffcc ;clrchn :err inc error pha jsr $ffcc ;clrchn sprint "simpleio: write error #" pla ora #$30 jsr $ffd2 lda #13 jsr $ffd2 endsub