[Coco] ML Disk I/O examples?

Bob Devries devries.bob at gmail.com
Mon Apr 16 18:25:33 EDT 2007


In my opinion, be it humble or not, the question about reading and writing 
files in disk Basic using assembler was not addressed very well. Here's my 2 
cents for what it's worth:

The ROM routine to open a file for input is $CA07 (DECB 1.0 = $C959)

The ROM routine to open a file for output is $CA04 (DECB 1.0 = $C956)

To use these routines, the following memory locations must have their 
correct values:

DNAMBF $094C is an 8 char buffer for the filename. Remember to pad with 
spaces to 8 characters.

DEXTBF $0954 is a 3 char buffer for the extension, padded as above.

For output files, these must also be set:

DFLTYP $0957 File type flag (0 = BASIC, 1 = DATA, 2 = BINARY, 3 = TEXT 
EDITOR

DASCFL $0958 Ascii flag (0 for binary data, $FF for text)

The two preceding locations will be filled automatically by the open routine 
if opened for input.

To write to the file, put the character in the A accumulator and jump to 
subroutine $CC24 (DECB 1.0 = $CB52)

To read from the file, jump to subroutine $C5C4 (DECB 1.0 = $C957) and 
expect the char in A.

To close the file, jump to subroutine $CAF9 (DECB 1.0 = $CA4B)

It is good to put your own error vector in place at $018E. Currently, that 
has a jump to $C6E4 (DECB = $C6B7).

So, as an example, say you want to write the data (only) of the first 4 
PMODE graphics screens to a disk file called PGRAPHICS.DAT. The data is 
$1800 bytes long, and it resides in memory starting at $0E00.

Please excuse the hard coding, it *is* an example only.

If there are any mistakes, please point them out. My assembler is a mite 
rusty.

;routine to write $1800 bytes to a disk file "GRAPHICS.DAT"
;some equates
DNAMBF equ $094C
DEXTBF equ $0954
DFLTYP equ $0957
DASCFL equ $0958
OPENO equ $CA04
CONSOUT equ $CC24
FCLOSE equ $CAF9
ERRVEC equ $018E
;end of equates

ORG $6000

vector lda [ERRVEC]
         sta errvec1,pcr
         ldx [ERRVEC+1]
         stx errvec2,pcr
         lda #$7E ;jump instruction
         sta ERRVEC
         leax error,pcr
         stx ERRVEC+1
picfil  leax filename,pcr ;point to filename
         ldb #8 ;8 characters
         ldy #DNAMBF
namlop lda ,x+
         sta ,y+
         decb
         bne namlop
         leax exten,pcr ;point to extension
         ldb #3 ;3 characters
         ldy #DEXTBF
extlop lda ,x+
         sta ,y+
         decb
         bne extlop
;name is now stored
         lda #1
         sta #DFLTYP
         lda #0
         sta #DASCFL
;file type and ascii flag stored
         jsr OPENO
;the file number will be stored in DEVNUM ($006E)
         ldx $0E00
         ldy $1800
wrtlop lda ,x+
         jsr CONSOUT
         leay -1,y
         bne wrtlop
;file data has been written
error jsr FCLOSE
;and file is closed
         lda errvec1,pcr
         sta ERRVEC
         ldx errvec2,pcr
         stx ERRVEC+1
;restored error vector
         rts
;return to BASIC
filename fcc /GRAPHICS/
exten fcc /DAT/
errvec1 rmb 1
errvec2 rmb 2

--
Regards, Bob Devries, Dalby, Queensland, Australia

Isaiah 50:4 The sovereign Lord has given me
the capacity to be his spokesman,
so that I know how to help the weary.

website: http://www.home.gil.com.au/~bdevasl
my blog: http://bdevries.invigorated.org/




More information about the Coco mailing list