2 ; Ullrich von Bassewitz, 2002-11-17, 2009-02-22
4 ; Handle disk command channels
9 .export closecmdchannel
12 .export writefndiskcmd
14 .import SETLFS, SETNAM, OPEN, CLOSE, BSOUT, BASIN
15 .import CHKIN, CKOUT, CLRCH
16 .import fncmd, fnlen, fnunit
20 .include "filedes.inc"
22 ;--------------------------------------------------------------------------
23 ; isdisk: Return carry clear if the unit number in X is a disk, return
28 cpx #FIRST_DRIVE ; Disk unit?
29 bcc @L1 ; Branch if no disk
30 cpx #FIRST_DRIVE+MAX_DRIVES
38 ;--------------------------------------------------------------------------
39 ; Open the command channel for the disk unit in X. The function returns an
40 ; error code in A and sets the flags according to the contents of A.
44 jsr isdisk ; Disk unit?
47 ; Is this channel already open?
49 ldy opentab-FIRST_DRIVE,x
52 ; Open the command channel, Carry is still clear
54 stx tmp1 ; Save the unit number
56 adc #(LFN_OFFS+MAX_FDS-FIRST_DRIVE)
57 ldy #15 ; Secondary address for cmd channel
61 jsr SETNAM ; No name supplied to OPEN
64 bcs done ; Error, code is in A
66 ; Command channel is open now. Increment the count
68 ldx tmp1 ; Unit number
69 ldy opentab-FIRST_DRIVE,x
72 sta opentab-FIRST_DRIVE,x
74 ; Done, return success
77 done: cmp #$00 ; Set flags for return code
81 ;--------------------------------------------------------------------------
82 ; closecmdchannel: Decrement the counter for the disk command channel and
83 ; close the channel if the counter drops to zero. The function expects the
84 ; drive number in X and returns an error code in A. The flags for the return
85 ; code are set when the function returns.
89 jsr isdisk ; Disk unit?
92 ; Is this channel really open?
94 ldy opentab-FIRST_DRIVE,x
95 beq success ; OOPS! Channel is not open
97 ; Decrement the count and stor it back
101 sta opentab-FIRST_DRIVE,x
103 ; If the counter is now zero, close the channel. We still have carry clear
107 txa ; Make LFN from drive number
108 adc #(LFN_OFFS+MAX_FDS-FIRST_DRIVE)
113 ;--------------------------------------------------------------------------
114 ; readdiskerror: Read a disk error from an already open command channel.
115 ; Returns an error code in A, which may either be the code read from the
116 ; command channel, or another error when accessing the command channel failed.
123 ; Read the command channel. We won't check the status after the channel is
124 ; open, because this seems to be unnecessary in most cases.
127 clc ; Make LFN from drive number
128 adc #(LFN_OFFS+MAX_FDS-FIRST_DRIVE)
130 jsr CHKIN ; Make the command channel input
131 bcs done ; Bail out with error code in A
134 and #$0F ; Make digit value from PETSCII
137 asl a ; * 4, carry clear
143 and #$0F ; Make digit value from PETSCII
147 ; Errors below 20 are not real errors. Fix that
154 ; Read the remainder of the message and throw it away
160 ; Close the input channel
164 ; Restore the error code (will also set the flags) and return
169 ;--------------------------------------------------------------------------
170 ; writefndiskcmd: Write the contents of fncmd to the command channel of the
171 ; drive in fnunit. Returns an error code in A, flags are set according to
181 inx ; Account for command char in fncmd
182 txa ; Length of name into A
185 ; Run directly into writediskcmd
189 ;--------------------------------------------------------------------------
190 ; writediskcmd: Gets pointer to data in ptr1, length in A. Writes all data
191 ; to the command channel of the drive in X. Returns an error code in A,
192 ; flags are set according to the contents of A.
197 bcs success ; No disk - already done
199 ; Remember the length
203 ; Write to the command channel.
206 clc ; Make LFN from drive number
207 adc #(LFN_OFFS+MAX_FDS-FIRST_DRIVE)
209 jsr CKOUT ; Make the command channel output
210 bcs done ; Bail out with error code in A
230 ;--------------------------------------------------------------------------
235 opentab: .res MAX_DRIVES, 0