Subversion Repositories Spectranet

[/] [branches/] [gnubinutils/] [modules/] [basext/] [tapetrap.asm] - Blame information for rev 380

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 222 winston
;The MIT License
2
;
3
;Copyright (c) 2009 Dylan Smith
4
;
5
;Permission is hereby granted, free of charge, to any person obtaining a copy
6
;of this software and associated documentation files (the "Software"), to deal
7
;in the Software without restriction, including without limitation the rights
8
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
;copies of the Software, and to permit persons to whom the Software is
10
;furnished to do so, subject to the following conditions:
11
;
12
;The above copyright notice and this permission notice shall be included in
13
;all copies or substantial portions of the Software.
14
;
15
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
;THE SOFTWARE.
22 380 winston
.include        "defs.inc"
23
.include        "spectranet.inc"
24
.include        "fcntl.inc"
25
.include        "sysvars.inc"
26
.text
27 222 winston
; Initialize and handle tape traps.
28
 
29
;---------------------------------------------------------------------------
30
; F_settrap: Sets up tape traps for the named file.
31
; This must be called immediately after ZX_STK_FETCH to get the string.
32 380 winston
.globl F_settrap
33
F_settrap:
34 222 winston
        ld hl, INTERPWKSPC
35
        call F_basstrcpy        ; get the string from the interpreter
36
 
37
        ld a, (v_trapfd)        ; File already open?
38
        and a                   ; if so this will be nonzero
39
        call nz, F_releasetrap  ; and the old file should be closed.
40
 
41
        ld hl, INTERPWKSPC      ; try to open the file
42
        ld d, 0x00              ; no flags
43
        ld e, O_RDONLY          ; read only
44
        call OPEN
45
        ret c                   ; exit now on error
46
        ld (v_trapfd), a        ; save the file descriptor
47
 
48
        ld de, v_trap_blklen    ; read the TAP block length to "prime the
49
        ld bc, 2                ; system" as it were.
50
        call READ
51
        jp c, J_cleanup_traperr ; clean up if an error occurs (EOF is
52
                                ; considered an error at this point too)
53
 
54
        ld hl, TAPETRAPBLOCK    ; Program the CPLD NMI trap
55
        call SETTRAP
56
        ret
57
 
58 380 winston
TAPETRAPBLOCK:
59 222 winston
        defb    0xFF            ; this page
60
        defw    F_loadbytes     ; function to call
61 223 winston
        defw    0x0564          ; address at time of NMI
62
        defw    0x0562          ; address to trap with NMI
63 222 winston
 
64 380 winston
DEFAULTTRAPBLOCK:
65 239 winston
        defb    0xFF            ; this page
66
        defw    F_bootfile      ; function to call
67
        defw    0x0564          ; address at time of NMI
68
        defw    0x0562          ; address to trap with NMI
69
 
70 222 winston
;---------------------------------------------------------------------------
71
; F_releasetrap: Closes the file associated with the tape trap and disables
72
; the trap itself.
73 380 winston
.globl F_releasetrap
74
F_releasetrap:
75 222 winston
        push ix
76
        ld a, 1
77
        out (254), a
78
        ld a, (v_trapfd)
79
        call VCLOSE             ; close the file
80
        call DISABLETRAP        ; disable the tape trap address
81
        xor a
82
        ld (v_trapfd), a        ; clear the fd variable
83
        pop ix
84
        ret
85
 
86
;---------------------------------------------------------------------------
87
; F_loadbytes: Does the job of LD-BYTES in the main Spectrum ROM, but loading
88
; from a filesystem instead of tape.
89
; Parameters:           IX - address to load with data
90
;                       DE - how many bytes to load
91
;                       A - 0x00 for a header, 0xFF for data
92
;                       Carry flag is set for loading, reset for verify
93
; On return, the carry flag is the *opposite* to normal Spectranet code -
94
; the routines that call loadbytes expect carry to be set if there's NOT
95
; an error.
96 380 winston
.globl F_loadbytes
97
F_loadbytes:
98 222 winston
        push ix                 ; store address
99 380 winston
.globl F_loadbytes_nopush
100
F_loadbytes_nopush:             ; (if IX has already been pushed)
101 222 winston
        ld ix, 4                ; point IX at the stacked registers
102
        add ix, sp
103
 
104 242 winston
        ld hl, (NMISTACK)       ; get the old stack pointer
105
        ld (hl), 0x3F           ; and set the return address to
106
        inc hl
107
        ld (hl), 0x05           ; 0x053F SA/LD-RET in the Spectrum ROM
108
 
109 222 winston
        ld a, (v_trapfd)        ; this could be better optimized...
110
        ld bc, 1
111
        ld de, INTERPWKSPC
112
        push ix
113
        call READ               ; get the "flag" byte.
114
        pop ix
115 380 winston
        jr c, .cleanuperror14
116 222 winston
 
117
        ld a, (INTERPWKSPC)     ; compare the flag byte read with the flag
118 223 winston
        cp (ix+1)               ; passed in A to LD-BYTES
119 239 winston
 
120 380 winston
        jr nz, .skip4           ; if they aren't the same skip the block
121 222 winston
 
122
        ld c, (ix+6)            ; get the requested length into
123
        ld b, (ix+7)            ; BC
124
        pop de                  ; and the address into DE
125
        ld h, b                 ; remaining length in HL
126
        ld l, c
127 380 winston
.readloop4:
128 222 winston
        ld a, (v_trapfd)
129
        push hl
130
        push ix
131
        call READ
132
        pop ix
133
        pop hl
134 380 winston
        jr c, .cleanuperror24
135 222 winston
        sbc hl, bc              ; decrement length remaining
136
        ld b, h                 ; update requested bytes
137
        ld c, l
138 380 winston
        jr nz, .readloop4       ; continue until 0 bytes remain
139 222 winston
 
140
        ld a, (v_trapfd)
141
        ld bc, 3                ; now read the "check sum" + next block length
142
        ld de, INTERPWKSPC
143
        push ix
144
        call READ
145
        pop ix
146 380 winston
        jr c, .checkeof4                ; EOF?
147 222 winston
        ld hl, (INTERPWKSPC+1)  ; Get the length of the next block and copy
148
        ld (v_trap_blklen), hl  ; to the length storage.
149 380 winston
.success4:
150 222 winston
        ld (ix+2), 1            ; set carry flag in return stack
151
        jp PAGETRAPRETURN
152
 
153 380 winston
.checkeof4:
154 222 winston
        cp EOF                  ; End of file - not an error condition
155 380 winston
        jr nz, .cleanuperror24
156 222 winston
        call F_releasetrap      ; Close the file and release the trap
157
        ld (ix+2), 1            ; Signal success to ROM
158
        jp PAGETRAPRETURN
159
 
160 380 winston
.cleanuperror14:
161 222 winston
        pop hl                  ; restore the stack (originally ix)
162 380 winston
.cleanuperror24:
163 222 winston
        call F_releasetrap      ; Release the trap and close files
164
        ld (ix+2), 0            ; reset the carry flag to signal error
165
        jp PAGETRAPRETURN
166
 
167 380 winston
.skip4:                         ; TODO: when seek() is working...
168 222 winston
        call F_releasetrap
169
        pop de                  ; restore stack
170
        pop ix
171
        ld a, 2
172
        out (254), a            ; for debugging
173
        ld (ix+2), 0            ; ensure C is cleared
174
        jp PAGETRAPRETURN
175
 
176 380 winston
J_trapreturn:
177 222 winston
 
178
;---------------------------------------------------------------------------
179 380 winston
J_cleanup_traperr:
180 222 winston
        push af
181
        ld a, (v_trapfd)
182
        call VCLOSE
183
        pop af
184 239 winston
        ret
185
 
186
;--------------------------------------------------------------------------
187
; F_initbootfile: Sets up LOAD ""
188 380 winston
.globl F_initbootfile
189
F_initbootfile:
190 239 winston
        ld hl, DEFAULTTRAPBLOCK ; Program the CPLD NMI trap
191
        call SETTRAP
192
        ret
193
 
194
;--------------------------------------------------------------------------
195
; F_bootfile
196
; Handles the case of an unsolicited LOAD "". It will try to load the file
197
; "boot" in the current working directory. If the file doesn't exist, it
198
; releases the trap and returns control to the tape loader.
199 380 winston
.globl F_bootfile
200
F_bootfile:
201 239 winston
        push ix
202
        ld hl, DEFAULTFILE      ; first copy the filename into memory
203
        ld de, INTERPWKSPC      ; that's accessable by the FS module
204
        ld bc, DEFAULTFILELEN
205
        ldir
206
 
207
        ld hl, INTERPWKSPC      ; try to open the file
208
        ld d, 0x00              ; no flags
209
        ld e, O_RDONLY          ; read only
210
        call OPEN
211 380 winston
        jr c, .usetape6         ; On error, simply pass control back to the
212 239 winston
                                ; ROM tape loader.
213
        ld (v_trapfd), a        ; save the file descriptor
214
 
215
        ld de, v_trap_blklen    ; read the TAP block length to "prime the
216
        ld bc, 2                ; system" as it were.
217
        call READ
218 380 winston
        jr c, .cleanupusetape6  ; any errors, then clean up and use tape. (EOF
219 239 winston
                                ; considered an error at this point too)
220
 
221
        ld hl, TAPETRAPBLOCK    ; Reprogram the NMI trap.
222
        call SETTRAP
223
        jp F_loadbytes_nopush   ; and go to loadbytes to do the work.
224
 
225 380 winston
.cleanupusetape6:
226 239 winston
        ld a, (v_trapfd)
227
        call VCLOSE             ; close the file
228 380 winston
.usetape6:
229 239 winston
        pop ix
230
        jp PAGETRAPRETURN
231
 
232 380 winston
DEFAULTFILE:
233
        defb    "boot.zx6",0
234
DEFAULTFILELEN: equ $-DEFAULTFILE
235 239 winston