1;
2;       ZX IF1 & Microdrive functions
3;
4;       Stefano Bodrato - Oct. 2004
5;
6;
7;       if1_rommap:
8;        - detect the shadow rom version
9;        - init the jump table
10;
11;       MAKE_M can't be called with the 'hook code' system because
12;       the first issue of the interface one just doesn't have it.
13;
14;       $Id: if1_rommap.asm,v 1.4 2016-07-01 22:08:20 dom Exp $
15;
16
17		SECTION   code_clib
18                PUBLIC    if1_rommap
19
20                PUBLIC    MAKE_M
21                PUBLIC    CLOSE_M
22                PUBLIC    FETCH_H
23                PUBLIC    MOTOR
24                PUBLIC    RD_BUFF
25                PUBLIC    ERASEM
26;                PUBLIC    ADD_RECD
27                PUBLIC    DEL_S_1
28
29                PUBLIC    mdvbuffer
30
31if1_rommap:     ; start creating an 'M' channel
32
33                rst     8
34                defb    31h             ; Create Interface 1 system vars if required
35
36                ld      hl,paged
37                ld      (5CEDh),hl      ; Location for hook 32h to jump to
38
39                rst     8               ; Call 'paged' with shadow paged in
40                defb    32h             ; (in other words: page in the shadow ROM)
41
42paged:
43                set     0,(iy+7Ch)      ; FLAGS3: reset the "executing extended command" flag
44
45        ; update jump table
46                ld      a,(10A5h)
47                or      a
48                jr      z,rom1
49                ld      hl,rom2tab      ; JP table for ROM 2
50
51                ld		a,(1d79h)		; basing on an old paper, it should be the
52										; first byte of SCAN_M in issue 3 IF1
53                cp		205
54                jr		z,rom2
55
56                ld		hl,rom3tab
57
58rom2:
59                ld      bc,24           ; 8 jumps * 3 bytes
60                ;ld     bc,30           ; 10 jumps * 3 bytes
61                ld      de,jptab        ; JP table dest addr
62                ldir
63
64
65rom1:
66
67                pop     bc              ; throw away some garbage
68                pop     bc              ; ... from the stack
69
70                ret
71                ;jp     MAKE_M
72
73
74; Jump table (ROM1 is the default)
75
76jptab:
77
78MAKE_M:         JP 0FE8h        ; set temporary "M" channel
79CLOSE_M:        JP 12A9h        ; close file (pointed by IX)
80FETCH_H:        JP 12C4h        ; fetch header
81
82IF OLDIF1MOTOR
83  MOTOR:        JP 17F7h        ; select drive motor
84ELSE
85  MOTOR2:       JP 17F7h        ; select drive motor
86ENDIF
87
88RD_BUFF:        JP 18A9h        ; get buffer
89ERASEM:         JP 1D6Eh        ; delete a file from cartridge
90FREESECT:       JP 1D38h        ; add a record to file
91DEL_S_1:        JP 1867h        ; 1ms delay
92
93;LDBYTS:                JP 15ACh        ;
94;SVBYTS:                JP 14EEh        ;
95
96
97; Jump table image (rom2)
98;
99
100rom2tab:
101                JP 10A5h
102                JP 138Eh
103                JP 13A9h
104                JP 1532h ;MOTOR
105                JP 15EBh ;RD_BUFF
106                JP 1D79h ;ERASEM
107                JP 1D43h ;FREESECT
108                JP 15A2h ;DEL_S_1
109
110                ;JP 199Dh ;LDBYTS
111                ;JP 18DFh ;SVBYTS
112
113; Jump table image (rom3)  ..based on the Pennel's book
114;
115
116rom3tab:
117                JP 10A5h
118                JP 138Eh
119                JP 13A9h
120                JP 1532h ;MOTOR
121                JP 15EBh ;RD_BUFF
122                JP 1D7Bh ;ERASEM
123                JP 1D45h ;FREESECT	; - ?? we just suppose this one
124                JP 15A2h ;DEL_S_1
125
126                ;JP 199Dh ;LDBYTS ???
127                ;JP 18DFh ;SVBYTS ???
128
129IF !OLDIF1MOTOR
130
131; New MOTOR routine.
132; This one traps the 'microdrive not present' error
133; Mostly from the A. Pennel's Microdrive book
134
135MOTOR:
136        and	a               ; need to stop a motor ?
137        jp      z,MOTOR2        ; use the original ROM routine
138
139        push    hl
140        di
141        ;; jr      sw_motor
142
143sw_motor:
144        ;push    de
145        ld      de,$0100        ; d=1, e=0
146        neg                     ; negate the drive number
147        add     9               ; a = 9 - drive number
148        ld      c,a             ; drive selected
149        ld      b,8             ; drive counter
150
151all_motors:
152        dec     c               ; decrement the drive selected
153        jr      nz,off_motor    ; jump if not the one that is
154                                ; under investigation
155        ; put a motor ON
156        ld      a,d
157        ld      ($f7),a         ; send 1 (ON)
158        ld      a,$ee
159        out     ($ef),a
160        call    DEL_S_1         ; wait 1 ms
161        ld      a,$ec
162        out     ($ef),a
163        call    DEL_S_1         ; wait 1 ms
164        jr      nxt_motor       ; do the next one
165
166off_motor:
167        ; switch a drive OFF
168        ld      a,$ef
169        out     ($ef),a
170        ld      a,e
171        out     ($f7),a         ; send 0: OFF
172        call    DEL_S_1         ; wait 1 ms
173        ld      a,$ed
174        out     ($ef),a
175        call    DEL_S_1         ; wait 1 ms
176nxt_motor:
177        djnz    all_motors
178
179        ld      a,d
180        out     ($f7),a         ; send 1
181        ld      a,$ee
182        out     ($ef),a
183        ;; jr      turned_on
184
185        ; now drive is on, check its status
186turned_on:
187        ld      hl,5000         ; delay counter
188ton_delay:
189        dec     hl
190        ld      a,l
191        or      h
192        jr      nz,ton_delay
193
194        ld      hl,5000         ; 'check' loop counter
195reptest:
196        ld      b,6             ; six times
197
198        ; here the original ROM checks for BREAK,
199        ; but we removed it as Pennel did
200chk_pres:
201        in      a,($ef)         ;
202        and     4               ; bit 2 only
203        jr      nz,nopres       ; jp if drive not present
204        djnz    chk_pres
205
206        pop     hl              ; *CARTRIDGE PRESENT*, return..
207        ret                     ;.. with zero flag set
208
209nopres:
210        dec     hl              ; decrement counter
211        ld      a,h
212        or      l
213        jr      nz,reptest
214
215        inc     a               ; *CARTRIDGE NOT PRESENT*, reset zero flag...
216        pop     hl
217        ret                     ; ...and return
218
219ENDIF
220		SECTION bss_clib
221mdvbuffer:      defw    0
222