From: Alvaro Alonso [alvarito_alonso@myrealbox.com]
Sent: Thursday, May 11, 2000 3:28 PM
To: gtm@videocam.net.au
Subject: Writing sequential files under GEOS

Bogota, D.C., 2000-05-11

There is 3 steps to write sequential files under GEOS,
extracted from my program 'txt2ibm' using the GEOS Kernal.
Think in the way BASIC write a secuential file.

1. Open the file
2. Write bytes one after one
3. Close file
4. Notes

Explained:
1. Open the file
   ; -----------------------------------------------------
   ; Open an output document
   ; prepare:       device selected
   ;                disk is opened by GEOS
   ;                seqHeader, a block of 256 bytes describing the
   ;                           a GEOS header block for the sequential file
   ; ram variables: outBuffer, 256 bytes long, temporary buffer of bytes to
be write
   ;                           on disk
   ;                outBlock,  2 bytes long, count number of block
   ;                outTS, 2 byts long, track and sector where to put the
current block
   ;                                    in memory
   ; input:         r6: = pointer to filename
   ;                a4: = pointer to output buffer
   ; actions:       search the file in the current disk
   ;                delete if it is founded
   ;                create a new file, with GEOS header block
   ; output         c=0, everything is OK and ready
   OpenWrFile:
        PushW r6
        jsr FindFile
        cpx #FILE_NOT_FOUND
        beq 10$              ; file is new in the current disk
        cpx #0
        beq 5$               ; file found
        jmp ViewDkErr        ; a subroutine to handle disk errors
   5$   PopW r6              ; delete old file
        PushW r6
        MoveW r6,r0
        jsr DeleteFile
   10$  PopW r6     
        LoadW r9,seqHeader  
        ldy #0              ; transfer pointer to filename
        lda r6L
        sta (r9),y
        iny
        lda r6H
        sta (r9),y
        LoadB r10L,1
        jsr SaveFile       ; create new file
        cpx #0
        beq 20$
        jmp ViewDkErr
   20$  MoveW dirEntryBuf+OFF_DE_TR_SC,outTS       ;start track/sector
        LoadW outBlock,1           ; 1 block allocated in the disk
        LoadW a4,#outBuffer
        lda #0                     ;clear output buffer
        tay
   25$  sta (a4),y
        iny
        bne 25$
        clc
        rts


2. Write bytes one after one
   ; prepare:       device selected
   ;                disk is opened by GEOS
   ;                rutine OpenWrFile was called before use this one
   ; ram variables: outBuffer, 256 bytes long, temporary buffer of bytes to
be write
   ;                           on disk
   ;                outBlock,  2 bytes long, count number of block
   ;                outTS, 2 byts long, track and sector where to put the
current block
   ;                                    in memory
   ; input:         a4:= pointer to output buffer outBuffer
   ; output:        x=0, z=1, everything is OK
   ;                otherwise, some disk error
   WriteByte:
        pha
   1$   ldy #1         ; use byte 1 as a pointer to the output buffer
        lda (a4),y     ;apuntador dentro del buffer
        bne 10$
        lda #1         ; write from byte 2
   10$  tay
        iny
        beq 15$        ;end of the buffer
        pla
        sta (a4),y
        tya
        ldy #1
        sta (a4),y
        ldx #0
        rts

   15$  MoveW outTS,r3  ; next available block
        jsr SetNextFree
        cpx #0
        bne 30$
        ldy #0
        lda r3L         ;next track
        sta (a4),y
        iny
        lda r3H         ;next sector
        sta (a4),y
        MoveW outTS,r1  ;track and sector to wrtie to
        MoveW a4,r4     ;apuntador a los datos
        jsr PutBlock
        ldy #0          ;next track and sector
        lda (a4),y
        sta outTS
        iny
        lda (a4),y
        sta outTS+1
        inc outBlock
        bne 24$
        inc outBlock+1
   24$  lda #0
        tay
   25$  sta (a4),y     ;clear output buffer
        iny
        bne 25$
        beq 1$         ; write delay byte
   
   30$  jmp ViewDkErr  ; display what disk error is
                       ; or you can change to your error handling subroutine
   

3. Close file
   ; ------------
   ; Close sequential file
   ; copy last sector of the file, that it is memory
   ; upgrade the count of blocks
   ;
   ; prepare:       device selected
   ;                disk is opened by GEOS
   ;                rutine OpenWrFile was called before use this one
   ; ram variables: outBuffer, 256 bytes long, temporary buffer of bytes to
be write
   ;                           on disk
   ;                outBlock,  2 bytes long, count number of block
   ;                outTS, 2 byts long, track and sector where to put the
current block
   ;                                    in memory
   ; input:         a4:= pointer to output buffer outBuffer
   ; output:        x=0, z=1, everything is OK
   ;                otherwise, some disk error
   CloseWrFile:
        MoveW a4,r4
        MoveW outTS,r1
        jsr PutBlock
        LoadW r6,outName
        jsr FindFile
        clc
        lda r5
        adc #OFF_SIZE
        tay
        lda outBlock
        sta diskBlkBuf,y
        iny
        lda outBlock+1
        sta diskBlkBuf,y
        LoadW r4,#diskBlkBuf
        jsr PutBlock
        jsr PutDirHead
        rts
   ; --------


4. Notes

   a. No change the current device until done all the writes of the bytes.
   b. To change device, is necessary some extra code, I'm still thinking of.
   c. Only 1 file at the time

Bye

   Alvaro Alonso
   City: Bogota, D.C.
   Country: Colombia (South America)


Alvaro Alonso G.
email: alvarito_alonso@myrealbox.com
Ciudad: Bogotá, D.C.
País  : Colombia (Sur América)