2 ; Ullrich von Bassewitz, 17.11.2002
4 ; Handle disk command channels
9 .export closecmdchannel
13 .import SETLFS, SETNAM, OPEN, CLOSE, BSOUT, BASIN
14 .import CHKIN, CKOUT, CLRCH
18 .include "filedes.inc"
20 ;--------------------------------------------------------------------------
21 ; isdisk: Return carry clear if the unit number in X is a disk, return
26 cpx #FIRST_DRIVE ; Disk unit?
27 bcc @L1 ; Branch if no disk
28 cpx #FIRST_DRIVE+MAX_DRIVES
36 ;--------------------------------------------------------------------------
37 ; Open the command channel for the disk unit in X. The function returns an
38 ; error code in A and sets the flags according to the contents of A.
42 jsr isdisk ; Disk unit?
45 ; Is this channel already open?
47 ldy opentab-FIRST_DRIVE,x
50 ; Open the command channel, Carry is still clear
52 stx tmp1 ; Save the unit number
54 adc #(LFN_OFFS+MAX_FDS-FIRST_DRIVE)
55 ldy #15 ; Secondary address for cmd channel
59 jsr SETNAM ; No name supplied to OPEN
62 bcs done ; Error, code is in A
64 ; Command channel is open now. Increment the count
66 ldx tmp1 ; Unit number
67 ldy opentab-FIRST_DRIVE,x
70 sta opentab-FIRST_DRIVE,x
72 ; Done, return success
75 done: cmp #$00 ; Set flags for return code
79 ;--------------------------------------------------------------------------
80 ; closecmdchannel: Decrement the counter for the disk command channel and
81 ; close the channel if the counter drops to zero. The function expects the
82 ; drive number in X and returns an error code in A. The flags for the return
83 ; code are set when the function returns.
87 jsr isdisk ; Disk unit?
90 ; Is this channel really open?
92 ldy opentab-FIRST_DRIVE,x
93 beq success ; OOPS! Channel is not open
95 ; Decrement the count and stor it back
99 sta opentab-FIRST_DRIVE,x
101 ; If the counter is now zero, close the channel. We still have carry clear
105 txa ; Make LFN from drive number
106 adc #(LFN_OFFS+MAX_FDS-FIRST_DRIVE)
111 ;--------------------------------------------------------------------------
112 ; readdiskerror: Read a disk error from an already open command channel.
113 ; Returns an error code in A, which may either be the code read from the
114 ; command channel, or another error when accessing the command channel failed.
121 ; Read the command channel. We won't check the status after the channel is
122 ; open, because this seems to be unnecessary in most cases.
125 clc ; Make LFN from drive number
126 adc #(LFN_OFFS+MAX_FDS-FIRST_DRIVE)
128 jsr CHKIN ; Make the command channel input
129 bcs done ; Bail out with error code in A
132 and #$0F ; Make digit value from PETSCII
135 asl a ; * 4, carry clear
141 and #$0F ; Make digit value from PETSCII
145 ; Errors below 20 are not real errors. Fix that
152 ; Read the remainder of the message and throw it away
158 ; Close the input channel
162 ; Restore the error code (will also set the flags) and return
167 ;--------------------------------------------------------------------------
168 ; writediskcmd: Gets pointer to data in ptr1, length in A. Writes all data
169 ; to the command channel of the given drive. Returns an error code in A,
170 ; flags are set according to the contents of A.
177 ; Remember the length
181 ; Write to the command channel.
184 clc ; Make LFN from drive number
185 adc #(LFN_OFFS+MAX_FDS-FIRST_DRIVE)
187 jsr CKOUT ; Make the command channel output
188 bcs done ; Bail out with error code in A
209 ;--------------------------------------------------------------------------
214 opentab: .res MAX_DRIVES, 0