1;
2; 2001-07-02, Maciej 'YTM/Elysium' Witkowiak
3; 2015-08-27, Greg King
4;
5; unsigned char __fastcall__ dio_log_to_phys (dhandle_t handle,
6;                                             unsigned *sectnum,        /* input */
7;                                             dio_phys_pos *physpos);   /* output */
8;
9
10            .export _dio_log_to_phys
11            .importzp ptr1,ptr2,ptr3,tmp1,tmp2
12            .import popax,__oserror
13            .import sectab_1541_l, sectab_1541_h
14
15            .include "dio.inc"
16            .include "geossym.inc"
17            .include "const.inc"
18
19_dio_log_to_phys:
20; check device type
21        sta ptr1
22        stx ptr1+1              ; pointer to result (struct dio_phys_pos)
23
24        jsr popax
25        sta ptr2
26        stx ptr2+1              ; pointer to input structure (pointer to int)
27
28        jsr popax
29        sta ptr3
30        stx ptr3+1              ; pointer to handle
31
32        ldy #sst_flag
33        lda (ptr3),y
34        and #128
35        beq _inv_hand           ; handle not open or invalid
36
37; fill in all we have
38        ldy #diopp_head
39        lda #0                  ; head 0
40        sta (ptr1),y
41        ldy #diopp_track+1
42        sta (ptr1),y            ; track <256
43        ldy #diopp_sector+1
44        sta (ptr1),y            ; sector <256
45
46        ldy #0
47        lda (ptr2),y
48        sta tmp1
49        iny
50        lda (ptr2),y
51        sta tmp2
52
53; get drive info
54        ldy #sst_driveno
55        lda (ptr3),y
56        tay
57        lda driveType,y
58        and #%00001111          ; remove ramDisk flags
59        cmp #DRV_1541
60        beq dio_stc1541
61        cmp #DRV_1571
62        beq dio_stc1571
63        cmp #DRV_1581
64        beq dio_stc1581
65
66        lda #INCOMPATIBLE       ; unsupported device
67        ldx #0
68        beq _ret
69
70dio_stcend:
71        ldy #diopp_track
72        lda tmp1
73        sta (ptr1),y
74        ldy #diopp_sector
75        lda tmp2
76        sta (ptr1),y
77
78        ldx #0
79        txa
80_ret:
81        sta __oserror
82        rts                     ; return success
83
84; errors
85_inv_data:
86        lda #INV_TRACK
87        .byte $2c
88_inv_hand:
89        lda #DEV_NOT_FOUND
90        ldx #0
91        beq _ret
92
93dio_stc1541:
94; if 1541:
95; - compare with table to find track
96; - subtract and find sector
97
98        ldx #0                  ; index=(track-1)
99_loop41:
100        lda tmp2
101        cmp sectab_1541_h+1,x
102        bne _nxt
103        lda tmp1
104        cmp sectab_1541_l+1,x
105_nxt:   bcc _found
106        inx
107        cpx #35
108        bne _loop41
109        beq _inv_data
110
111_found:
112        lda tmp1
113        sec
114        sbc sectab_1541_l,x
115        sta tmp2
116_fndend:
117        inx
118        stx tmp1
119        jmp dio_stcend
120
121dio_stc1571:
122; if 1571:
123; - check size, if too big - subtract and add 35 to track
124; - fall down to 1541
125        lda tmp2
126        cmp #>683
127        bne _if71
128        lda tmp1
129        cmp #<683
130_if71:  bcc dio_stc1541
131
132        lda tmp1
133        sec
134        sbc #<683
135        sta tmp1
136        lda tmp2
137        sbc #>683
138        sta tmp2
139        jsr dio_stc1541         ; will fall through here
140        tay
141        bne _ret                ; result beyond track 70
142
143        ldy #diopp_track
144        lda (ptr1),y
145        clc
146        adc #35
147        sta (ptr1),y
148        lda #0
149        beq _ret
150
151; if 1581:
152; - subtract 40 in loop (at most 80 times) to find track
153; - the remainder is sector
154dio_stc1581:
155        ldx #0                  ; index=(track-1)
156_loop81:
157        lda tmp2
158        bne _sub81
159        lda tmp1
160        cmp #40
161        bcc _got81
162_sub81: lda tmp1
163        sec
164        sbc #40
165        sta tmp1
166        lda tmp2
167        sbc #0
168        sta tmp2
169        inx
170        cpx #80
171        bne _loop81
172        beq _inv_data
173
174_got81: lda tmp1
175        sta tmp2
176        inx
177        stx tmp1
178        jmp dio_stcend
179