User Tools

Site Tools


base:reading_a_file_byte-by-byte
no way to compare when less than two revisions

Differences

This shows you the differences between two versions of the page.


base:reading_a_file_byte-by-byte [2015-04-17 04:33] (current) – created - external edit 127.0.0.1
Line 1: Line 1:
 +====== Reading from a file byte-by-byte ======
 +BASIC code:
 +<code>
 +10 LA=8192
 +20 OPEN 2,8,2,"JUST A FILENAME"
 +30 IF ST<>0 THEN GOTO 60
 +40 GET#2,A$:IF A$="" THEN A$=CHR$(0)
 +50 POKE LA,ASC(A$):LA=LA+1:GOTO 30
 +60 CLOSE 2
 +</code>
 +Assembler code:
 +<code>
 +load_address = $2000  ; just an example
  
 +        LDA #fname_end-fname
 +        LDX #<fname
 +        LDY #>fname
 +        JSR $FFBD     ; call SETNAM
 +
 +        LDA #$02      ; file number 2
 +        LDX $BA       ; last used device number
 +        BNE .skip
 +        LDX #$08      ; default to device 8
 +.skip   LDY #$02      ; secondary address 2
 +        JSR $FFBA     ; call SETLFS
 +
 +        JSR $FFC0     ; call OPEN
 +        BCS .error    ; if carry set, the file could not be opened
 +
 +        ; check drive error channel here to test for
 +        ; FILE NOT FOUND error etc.
 +
 +        LDX #$02      ; filenumber 2
 +        JSR $FFC6     ; call CHKIN (file 2 now used as input)
 +
 +        LDA #<load_address
 +        STA $AE
 +        LDA #>load_address
 +        STA $AF
 +
 +        LDY #$00
 +.loop   JSR $FFB7     ; call READST (read status byte)
 +        BNE .eof      ; either EOF or read error
 +        JSR $FFCF     ; call CHRIN (get a byte from file)
 +        STA ($AE),  ; write byte to memory
 +        INC $AE
 +        BNE .skip2
 +        INC $AF
 +.skip2  JMP .loop     ; next byte
 +
 +.eof
 +        AND #$40      ; end of file?
 +        BEQ .readerror
 +.close
 +        LDA #$02      ; filenumber 2
 +        JSR $FFC3     ; call CLOSE
 +
 +        JSR $FFCC     ; call CLRCHN
 +        RTS
 +.error
 +        ; Akkumulator contains BASIC error code
 +
 +        ; most likely errors:
 +        ; A = $05 (DEVICE NOT PRESENT)
 +
 +        ;... error handling for open errors ...
 +        JMP .close    ; even if OPEN failed, the file has to be closed
 +.readerror
 +        ; for further information, the drive error channel has to be read
 +
 +        ;... error handling for read errors ...
 +        JMP .close
 +
 +fname:  
 +!tx "JUST A FILENAME"
 +fname_end:
 +</code>
 +
 +You may open more than one file if you use different file numbers and secondary addresses for them. File numbers and secondary addresses should be in the range of 2 to 14. It's usually a good idea to use the same number for both to keep confusion low.
base/reading_a_file_byte-by-byte.txt · Last modified: 2015-04-17 04:33 by 127.0.0.1