Rev 49 | Blame | Compare with Previous | Last modification | View Log | Download
_finEXPORT _finDW 0_foutEXPORT _foutDW 0_waseofEXPORT _waseofDB 0IOFCB=0;db 0 ;0 = free, 'r' = read, 'w' = writeIOFCB=IOFCB+1;;;;;;;;;;;;;;; copy of TR-DOS descriptor ;;;;;;;;;;;;;;IOFCB.fn=IOFCB;db "12345678c" ;filenameIOFCB=IOFCB+9IOFCB.block=IOFCB;db 0 ;cur block for write, next block for read;db 0x60 ;(padding for "start")IOFCB=IOFCB+2IOFCB.lastlen=IOFCB;dw 0 ;length of blockIOFCB=IOFCB+2IOFCB.secinblk=IOFCB;db 0 ;sectors remained in block (for read), block size (for write)IOFCB=IOFCB+1IOFCB.firstsector=IOFCB;dw 0 ;first sector of block (for write)IOFCB=IOFCB+2;;;;;;;;;;;;;;; end of TR-DOS descriptor ;;;;;;;;;;;;;;IOFCB.cursector=IOFCB;dw 0 ;cur sector for write, next sector for readIOFCB=IOFCB+2IOFCB.remain=IOFCB;db 0xff ;remaining bytes in buf (for read)IOFCB=IOFCB+1IOFCB.secwritten=IOFCB;db 0 ;sectors written (for write)IOFCB=IOFCB+1IOFCB.descpos=IOFCB;db 0 ;position of descriptor (for write) 0..127IOFCB=IOFCB+1IOFCB.buf=256;$-IOFCB;ds 256 ;buffer for last sector;hl = poi to filename in stringfindlastslash.nfopenfnslash.ld d,hld e,l ;de = after last slash;find last slashnfopenfnslash0.ld a,[hl]inc hlor ajr z,nfopenfnslashq.cp '/'jr nz,nfopenfnslash0.jr nfopenfnslash.nfopenfnslashq.;de = after last slashrettopenwr.db "wb"db 0openwriteEXPORT openwriteld hl,topenwr.ld [nfopen.B.],hlopenwrite.A.=$+1EXPORT openwrite.A.ld hl,0 ;filenameld [nfopen.A.],hlnfopenEXPORT nfopenxor ald hl,FCB1-0x0200inc hinc hcp [hl]jr nz,$-3;nfopenFCBnfopen.B.=$+1EXPORT nfopen.B.ld a,[0] ;poi to mode ("wb"/"rb")ld [hl],a ;mode in FCBnfopen.A.=$+1EXPORT nfopen.A.ld de,0 ;poi to filename in string;яЁютхЁшЄ№, ўЄю Їрщы ёє∙хёЄтєхЄ, шэрўх тхЁэєЄ№ ю°шсъє ш юётюсюфшЄ№ FCBpush hlld l,0ld a,(hl)cp 'w'jr nz,nfopen_noopenwrite;OS_CREATEHANDLEld c,CMD_CREATEHANDLE;0x44call BDOS;0x0005jr nfopen_openwriteqnfopen_noopenwrite;OS_OPENHANDLEld c,CMD_OPENHANDLE;0x43call BDOS;0x0005nfopen_openwriteqpop hlor ajr nz,nfopen_fail.ld l,0x80ld [hl],b ;handlexor ald l,IOFCB.lastlenld [hl],a;0 ;lastlen LSB ;for writeinc hlld [hl],a;0 ;lastlen HSB ;for writeinc hlld [hl],a;0 ;sectors remained in block (for read), block size (for write)ld l,IOFCB.remainld [hl],0xff ;remain (0xff = no data) for readinc hlld [hl],a;0 ;sectors written (for write)ld l,a;0;FCB.moderet ;hl = poi to FCBnfopen_fail.ld l,0ld (hl),l;0 ;empty FCBld h,l ;hl=0retclosewriteEXPORT closewritefcloseEXPORT fcloseclosewrite.A.=$+1EXPORT closewrite.A.fclose.A.=$+1EXPORT fclose.A.ld hl,0 ;poi to FCBld a,[hl]ld [hl],l;0cp 'w';jr nz,fcloseq ;ret nz ;hl!=0;hl = poi to FCBcall z,fwriteflush;fcloseqld l,0x80ld b,[hl];OS_CLOSEHANDLEld c,CMD_CLOSEHANDLE;0x45call BDOS;0x0005retreadfEXPORT readfreadf.A.=$+1EXPORT readf.A.ld hl,0 ;poi to FCBjr fread1. ;a=data ;keeps bcreadfinEXPORT readfinld hl,[_fin] ;poi to FCBfread1.;a=data;;if ok then inc de?;keeps bcld l,IOFCB.remain ;0xff = no data, 0xfe = 1 byte, ... 0x00 = 255 bytesld e,[hl]ld d,hinc dinc eld a,[de]ld [hl],e ;inc [hl] ;remainret nz;ld e,0ld [hl],e;de=IOFCB+256push bcpush hlld l,0x80ld b,[hl]ld hl,256 ;TODO 512;OS_READHANDLEld c,CMD_READHANDLE;0x48call BDOS;0x0005ex de,hlpop hlpop bc;or a;jr z,fread1ok.ld a,dor ejr z,fread1EOF.dec djr z,fread1ok.ld a,e ;1..256;a=len;last sector in block: shift data to the end of bufld l,IOFCB.lastlen ;???ld [hl],apush bcld c,[hl] ;1..256ld b,0dec c ;0..255 for 1..256ld l,c ;end of datainc hinc bc ;1..256ld d,hld e,0xfflddrpop bcld l,IOFCB.remainld [hl],ejr fread1.fread1ok.ld l,0inc hld a,[hl]retfread1EOF.ld a,TRUEld [_waseof],ald a,'\n' ;EOF returns '\n';set vars as they were (next time EOF again)ld l,IOFCB.remaindec [hl] ;remain = 0xffretfreadEXPORT freadfread.A.=$+1EXPORT fread.A.ld hl,0 ;poi to data;ld de,0 ;total processed bytesfread.C.=$+1EXPORT fread.C.ld bc,0 ;countfread0.push bcfread.B.=$+1EXPORT fread.B.ld bc,0 ;sizefread00.push hlfread.D.=$+1EXPORT fread.D.ld hl,0 ;poi to FCBcall fread1. ;a=data ;keeps bcpop hlld [hl],ald a,[_waseof]or a ;FALSEjr nz,freadpopret.;cpidb 0xed,0xa1jp pe,fread00.pop bcdec hl;cpidb 0xed,0xa1jp pe,fread0.;ex de,hl ;hl = total processed bytesretfreadpopret.pop bcretwritebyteEXPORT writebytewritebyte.A.=$+1EXPORT writebyte.A.ld hl,0 ;filewritebyte.B.=$+1EXPORT writebyte.B.ld a,0jp fwrite1.writefoutEXPORT writefoutld hl,[_fout]writefout.A.=$+1EXPORT writefout.A.ld a,0;hl = poi to IOFCB;a=data;keeps bcfwrite1.ld l,IOFCB.lastlenld e,[hl]ld d,hinc dld [de],ainc [hl] ;lastlen (LSB)ret nzinc e ;ld e,0 ;de=IOFCB+256push bcpush hlld l,0x80ld b,[hl]ld hl,256fwrite1q;OS_WRITEHANDLEld c,CMD_WRITEHANDLE;0x49call BDOS;0x0005pop hlpop bcretfwriteflush;hl = poi to IOFCB;de undefinedld l,IOFCB.lastlen;push hl; ld a,[hl] ;0..255; rst 16; ld a,0x0d; rst 16; ld a,0x0a; rst 16;pop hlld a,[hl] ;0..255or aret zld d,hinc dld e,0ld [hl],e ;lastlen=0;de=IOFCB+256;a=lastlen = 0..255push bcpush hlld l,0x80ld b,[hl]ld l,ald h,0 ;hl=0..255jr fwrite1qfwriteEXPORT fwritefwrite.A.=$+1EXPORT fwrite.A.ld hl,0 ;poi to data;ld de,0 ;total processed bytes TODOfwrite.C.=$+1EXPORT fwrite.C.ld bc,0 ;countfwrite0.push bcfwrite.B.=$+1EXPORT fwrite.B.ld bc,0 ;sizefwrite00.ld a,[hl]push hl;push defwrite.D.=$+1ld hl,0 ;poi to IOFCBcall fwrite1. ;a=data ;keeps bc;pop de;inc depop hl;cpidb 0xed,0xa1jp pe,fwrite00.pop bcdec hl;cpidb 0xed,0xa1jp pe,fwrite0.;ex de,hl ;hl = total processed bytesretfputsEXPORT fputs;todo optimizefputs.A.=$+1EXPORT fputs.A.ld bc,0 ;poi to stringjp fputsgo.fputs0.fputs.B.=$+1EXPORT fputs.B.ld hl,0 ;poi to IOFCBcall fwrite1. ;a=datainc bcfputsgo.ld a,[bc]or ajp nz,fputs0.ret ;end of string ;(TODO hl = non-negative value)loadfileloadfile.A.=$+1 ;filenameld hl,0ld [nfopen.A.],hlld hl,loadfile.mode.ld [nfopen.B.],hlcall nfopenld a,hor lret zld [fclose.A.],hl ;IOFCBld [fread.D.],hl ;IOFCBloadfile.B.=$+1 ;addrld hl,0ld [fread.A.],hl ;addrld hl,1ld [fread.B.],hl ;sizeld hl,0xffffld [fread.C.],hl ;countld a,FALSEld [_waseof],acall freadpush hlcall fclosepop hlretloadfile.mode.db "rb"db 0