]> git.sur5r.net Git - cc65/blob - libsrc/atari/diopncls.s
8995e79e615e7621b3c6459deff87f84f0e18e99
[cc65] / libsrc / atari / diopncls.s
1 ;
2 ; Christian Groessler, February 2005
3 ;
4 ; This file provides the _dio_open and _dio_close functions
5 ; Since on the Atari no real open and close is necessary, they
6 ; do not open or close something. The _dio_open sets the sector
7 ; size of the drive which is later used by the _dio_read and
8 ; _dio_write functions. To query the sector size, the _dio_open
9 ; accesses the disk drive.
10 ;
11 ; dhandle_t     __fastcall__ dio_open  (driveid_t drive_id);
12 ; unsigned char __fastcall__ dio_close (dhandle_t handle);
13 ;
14
15         .export         _dio_open, _dio_close
16         .export         sectsizetab
17         .import         __oserror, __sio_call, _dio_read
18         .import         pushax, addysp, subysp
19         .importzp       ptr2, tmp1, sp
20         .include        "atari.inc"
21
22
23 .bss
24
25 sectsizetab:
26         .res    NUMDRVS * sst_size
27
28 .code
29
30
31 .proc   _dio_open
32
33         cmp     #NUMDRVS        ; valid drive id?
34         bcs     _inv_drive
35         tay                     ; drive #
36         asl     a               ; make index from drive id
37         asl     a
38         tax
39         lda     #128            ; preset sectsize, will be overridden by query_sectorsize
40         sta     sectsizetab+sst_sectsize,x
41         sta     sectsizetab+sst_flag,x          ; set flag that drive is "open"
42         lda     #0
43         sta     sectsizetab+sst_sectsize+1,x
44         sta     __oserror                       ; success
45         tya
46         sta     sectsizetab+sst_driveno,x
47         stx     tmp1
48         lda     #<sectsizetab
49         clc
50         adc     tmp1
51         sta     tmp1
52         lda     #>sectsizetab
53         adc     #0
54         tax
55         lda     tmp1
56         jmp     query_sectorsize                ; query drive for current sector size
57 ;       rts
58
59 _inv_drive:
60         lda     #NONDEV         ; non-existent device
61         sta     __oserror
62         lda     #0
63         tax
64         rts                     ; return NULL
65
66 .endproc
67
68 .proc   _dio_close
69
70         sta     ptr2
71         stx     ptr2+1
72         lda     #0
73         ldy     #sst_flag
74         sta     (ptr2),y
75         sta     __oserror       ; success
76         tax
77         rts                     ; return no error
78
79 .endproc
80
81 ; query drive for current sector size
82 ; procedure:
83 ;   - read sector #4 (SIO command $54) to update drive status
84 ;     read length is 128 bytes, buffer is below the stack pointer,
85 ;          sector data is ignored
86 ;     command status is ignored, we will get an error with a DD disk
87 ;          anyway (read size 128 vs. sector size 256)
88 ;   - issue SIO command $53 (get status) to retrieve the sector size
89 ;     if the command returns with an error, we set sector size to 128
90 ;          bytes
91 ;
92 ; AX - handle
93 ;
94 .proc   query_sectorsize
95
96         sta     ptr2
97         stx     ptr2+1          ; remember pointer to sectsizetab entry
98
99 ;       jsr     pushax          ; handle for subsequent __sio_call
100
101         ldy     #128
102         jsr     subysp          ; use buffer on the stack
103
104         lda     sp
105         pha
106         lda     sp+1
107         pha
108
109         lda     ptr2
110         ldx     ptr2+1
111
112         jsr     pushax          ; handle
113         ldx     #0
114         lda     #4
115         jsr     pushax          ; sect_num
116
117 ;       ldy     #128
118 ;       jsr     subysp          ; use buffer on the stack
119 ;       lda     sp
120 ;       ldx     sp+1
121         pla
122         tax
123         pla
124 ;       jsr     pushax          ; buffer address (not pushed, _dio_read is __fastcall__)
125
126         jsr     _dio_read       ; read sector to update status
127
128         ldy     #128
129         jsr     addysp          ; discard stack buffer
130
131
132         lda     ptr2
133         ldx     ptr2+1
134         jsr     pushax          ; handle for subsequent __sio_call
135
136
137         ldx     #0
138         lda     #4
139         jsr     pushax          ; dummy sector #
140
141         ldx     #>DVSTAT
142         lda     #<DVSTAT
143         jsr     pushax          ; buffer address
144
145         ldy     #sst_sectsize
146         lda     #4
147         sta     (ptr2),y        ; 4 bytes transfer
148
149         ldx     #%01000000      ; direction value
150         lda     #SIO_STAT       ; get status
151
152         jsr     __sio_call
153
154         bmi     error
155
156         ldy     #sst_sectsize
157         lda     DVSTAT
158         and     #%100000
159         beq     s128
160
161         lda     #0
162         sta     (ptr2),y
163         iny
164         lda     #1
165 ;       sta     (ptr2),y
166 ;       bne     fini2
167
168 finish: sta     (ptr2),y        ; set default sector size       
169 fini2:  lda     ptr2
170         ldx     ptr2+1
171         rts
172
173 error:  ldy     #sst_sectsize
174 s128:   lda     #128
175         bne     finish
176
177 .endproc