sys_return: equ $d003 sys_call: equ $d006 console_status: equ $7fe5 console_output: equ $7fe8 mon_pdata: equ $c009 mon_out2hs: equ $c012 mon_out4hs: equ $c015 mon_pcrlf: equ $c021 org $a100 ;;; Go to first argument str_skp_next: ldaa ,x beq str_skp_next_e cmpa #$20 beq str_skp_next_1 inx bra str_skp_next str_skp_next_1: ldaa ,x beq str_skp_next_e cmpa #$20 bne str_skp_next_e inx bra str_skp_next_1 str_skp_next_e: ;;; Pointer to filename now in X jsr fopen ; open file bne prog_end ;;; File now open clr sum clr sum+1 loop: jsr fread bcs prog_read_end tab clra addd sum std sum bra loop prog_read_end: cmpa #0 beq prog_end_result ; EOF ldx #txt_read_error jsr mon_pdata bra prog_end prog_end_result: ldx #sum jsr mon_out4hs jsr mon_pcrlf prog_end: jmp sys_return ;;; ;;; Open file for reading ;;; In: X - pointer to file name ;;; Out: Z set on success, error code in A ;;; fopen: stx fname0 stx fname1 ;;; Get file info ldx #fcb0 jsr sys_call tst fcb0+1 ; read error code bne fopen_not_found ;;; Check if directory ldaa fcb0+4 ; get flags bita #$40 bne fopen_is_directory ;;; Setup file for reading ldx #0 stx fcnth stx fcntl clra rts fopen_not_found: ldx #txt_not_found jsr mon_pdata ldaa fcb0+1 ; read error code rts fopen_is_directory: ldx #txt_is_directory jsr mon_pdata ldaa #$ff rts ;;; ;;; Read one byte from file (must call fopen first) ;;; Out: Byte in A, carry set on error or eof ;;; fread: ;;; Check if we have reached end of file ldx fcnth cpx fsizeh bhi fread_eof ldx fcntl cpx fsizel beq fread_eof ;;; We have bytes to read ldd fcntl ; file byte bointer LSB anda #$01 ; mask away upper 7 bits from D xgdx stx xtemp ; save sector data pointer cpx #0 ; first byte of new sector? bne fread_getbyte ;;; Load a new sector into buffer ldx #fcb1 jsr sys_call tst fcb1+1 ; read error code bne fread_error ldx fsect inx stx fsect ;;; Pull one byte from buffer fread_getbyte: ldd xtemp ; calculate addd #secbuf xgdx ldaa ,x ; load byte from sector buffer psha ;;; Increase file byte counter ldd fcntl addd #1 std fcntl bcc fread_getbyte1 ldd fcnth addd #1 std fcnth fread_getbyte1: pula clc rts fread_eof: clra fread_error: sec rts ;;; ;;; File variables ;;; fcb0: fcb $01 ; command fcb 0 ; error code fname0: fdb 0 ; file name fcb 0 ; flags fdb 0 ; sectors fsizeh: fdb 0 ; size MSB fsizel: fdb 0 ; size LSB fdb 0 ; year fcb 0 ; month fcb 0 ; day fcb 0 ; hours fcb 0 ; minutes fcb 0 ; seconds * fcb1: fcb $11 ; command fcb 0 ; error code fname1: fdb 0 ; file name fdb secbuf ; destination address fsect: fdb 0 ; sector number * fcnth: fdb 0 ; bytes read MSB fcntl: fdb 0 ; bytes read LSB * txt_not_found: fcc "File not found" fcb $0d,$0a,$04 txt_read_error: fcc "File read error" fcb $0d,$0a,$04 txt_is_directory: fcc "Entry is a directory" fcb $0d,$0a,$04 * xtemp: fdb 0 temp: fcb 0 sum: fdb 0 * *tstnam: * fcc "testfile" * fcb 0 * secbuf: rmb 512