Subversion Repositories Spectranet

[/] [branches/] [gnubinutils/] [modules/] [snapman/] [savesna.asm] - Blame information for rev 372

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 255 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
 
23
; Save snapshots in SNA format.
24 372 winston
.include        "fcntl.inc"
25
.include        "snapheader.inc"
26
.include        "snapman.inc"
27
.include        "spectranet.inc"
28
.include        "sysvars.inc"
29 255 winston
 
30
;--------------------------------------------------------------------------
31
; F_savesna48
32
; Save a 48K snapshot. Null terminated filename pointed to by HL.
33 256 winston
UNPAGE  equ     0x007C
34 263 winston
SAVERAM equ     0x3600
35
MAINSPSAVE equ  0x35FE
36
TEMPSP  equ     0x81FE
37
 
38 372 winston
.globl F_snaptest
39
F_snaptest:
40 255 winston
        ld hl, STR_saving
41
        call PRINT42
42
 
43
        ld hl, STR_filename
44
        ld de, 0x3000
45
        xor a
46 372 winston
.loop1:
47 255 winston
        ldi
48
        cp (hl)
49 372 winston
        jr nz,  .loop1
50
.done1:
51 255 winston
        ld (de), a
52
        ld hl, 0x3000
53 263 winston
        call F_savesna
54 372 winston
        jr c,  .borked1
55 255 winston
 
56 269 winston
        ld a, (v_border)                ; Restore the border
57
        out (254), a
58 272 winston
        call F_restorescreen            ; Restore the screen
59 256 winston
        ld a, (SNA_EIDI)                ; Re.enable interrupts?
60
        and a                           ; No
61 372 winston
        jr nz,  .retei1                 ; If yes, then EI on ret
62 256 winston
        ld sp, NMISTACK-14              ; Set SP to where the stack was
63
        pop af
64
        ex af, af'
65
        pop af
66
        pop bc
67
        pop de
68
        pop hl
69
        ld sp, (NMISTACK)
70
        jp UNPAGE                       ; RET
71 372 winston
.borked1:
72 255 winston
        ld a, 2
73
        out (254), a
74
        ret
75
 
76 372 winston
.retei1:
77 256 winston
        ld sp, NMISTACK-14
78
        pop af
79
        ex af, af'
80
        pop af
81
        pop bc
82
        pop de
83
        pop hl
84
        ld sp, (NMISTACK)
85
        jp UNPAGE-1                     ; EI; RET
86
 
87 263 winston
;------------------------------------------------------------------------
88
; HL = pointer to filename string
89 255 winston
; Entry to the NMI saves the folowing at NMISTACK-4:
90
; hl, de, bc, af, af'
91 372 winston
.globl F_savesna
92
F_savesna:
93 255 winston
        ld d, O_CREAT | O_TRUNC         ; Flags
94
        ld e, O_WRONLY                  ; File mode
95
        call OPEN                       ; Open the snapshot file
96
        ret c
97
        ld (v_snapfd), a                ; save the file descriptor
98
 
99
        ld hl, HEADER                   ; Clear down the header information
100
        ld de, HEADER+1                 ; temporary storage.
101
        ld bc, HEADERSZ-1
102
        ld (hl), l
103
        ldir
104
 
105
        ld hl, (STACK_HL)               ; Start by saving the registers we can
106
        ld (SNA_HL), hl
107
        ld hl, (STACK_DE)
108
        ld (SNA_DE), hl
109
        ld hl, (STACK_BC)
110
        ld (SNA_BC), hl
111
        ld hl, (STACK_AFALT)
112
        ld (SNA_AFALT), hl
113
        ld hl, (STACK_AF)
114
        ld (SNA_AF), hl
115
        ld hl, (NMISTACK)               ; SP when NMI happened
116
        ld (SNA_SP), hl
117
        ld (SNA_IX), ix
118
        ld (SNA_IY), iy
119
        exx                             ; do the alternate set
120
        ld (SNA_HLALT), hl
121
        ld (SNA_DEALT), de
122
        ld (SNA_BCALT), bc
123
        exx
124 263 winston
        ld a, i                         ; also sets P/V flag if IFF2 is
125
        ld (SNA_I), a                   ; set (interrupts enabled)
126 372 winston
        call pe,  .setei2
127 269 winston
 
128
        ld a, (v_border)                ; Copy the border colour into
129
        ld (SNA_BORDER), a              ; the snapshot header.
130 261 winston
 
131 263 winston
        ; Now something must be done to detect interrupt mode
132 255 winston
        ld hl, 0x3600
133
        ld de, 0x3601
134 257 winston
        ld bc, 258                      ; table is actually 0x100 bytes
135 372 winston
        ld (hl), F_im2_lsb              ; LSB of IM 2 routine.
136 255 winston
        ldir
137 257 winston
        ld a, 0x36                      ; MSB of the table
138 255 winston
        ld i, a                         ; set the vector table
139
 
140
        ld hl, 0
141
        ld (v_intcount), hl             ; reset IM 1 counter to detect IM 1
142 372 winston
        ld hl,  .continue2              ; "Return" address
143 255 winston
        push hl                         ; on the stack so RETN
144
        retn                            ; takes us just to the next addr.
145
 
146 372 winston
.continue2:
147 266 winston
        xor a
148
        ld (SNA_IM), a                  ; clear IM x
149 255 winston
        ei
150 266 winston
        halt                            ; wait for an interrupt
151
        ld a, (SNA_IM)                  ; Did the IM 2 routine update
152
        and a                           ; the interrupt mode?
153 372 winston
        jr nz,  .donewithinterrupts2    ; Done.
154 255 winston
 
155 266 winston
        inc a
156 255 winston
        ld (SNA_IM), a                  ; set interrupt mode 1
157
 
158
        ; Now it's all over bar the shouting.
159 372 winston
.donewithinterrupts2:
160 255 winston
        di
161
 
162 256 winston
        ; Restore the I register
163
        ld a, (SNA_I)
164
        ld i, a
165
 
166 263 winston
        ; Detect whether we're saving a 48K or a 128K snapshot
167 265 winston
        ld a, (v_machinetype)
168
        and 1                           ; Bit 1 set = 128K
169 372 winston
        jr nz,  .save128snap2
170 263 winston
 
171 372 winston
        call  .savemain2                        ; save header and memory
172
.writedone2:
173 263 winston
        ld a, (v_snapfd)                ; close the file
174
        call VCLOSE
175
        ret
176
 
177 372 winston
.writeerr2:
178 263 winston
        push af
179
        ld a, (v_snapfd)
180
        call VCLOSE
181
        pop af
182
        ret
183 372 winston
.setei2:
184 263 winston
        ld a, 4
185
        ld (SNA_EIDI), a
186
        ret
187
 
188 372 winston
.save128snap2:
189 265 winston
        ; copy port 0x7FFD value to the header
190
        ld a, (v_port7ffd)
191
        ld (SNA_7FFD), a
192
 
193 263 winston
        ; adjust header suitably for 128K snapshots
194
        ld hl, (SNA_SP)                 ; The stack pointer must be
195
        ld e, (hl)                      ; adjusted and the PC must be
196
        inc hl                          ; obtained.
197
        ld d, (hl)                      ; PC now in DE
198
        inc hl
199
        ld (SNA_SP), hl                 ; Set the snapshot SP.
200
        ld (SNA_PC), de                 ; Set the PC
201
        xor a
202
        ld (SNA_TRDOS), a               ; TRDOS always 0
203 372 winston
        call  .savemain2                        ; the header + 48K
204
        jr c,  .writeerr2
205 263 winston
 
206
        ld hl, SNA_PC                   ; Save the 128K second header
207
        ld bc, 4                        ; (4 bytes long)
208
        ld a, (v_snapfd)
209
        call WRITE
210 372 winston
        jr c,  .writeerr2
211 263 winston
 
212
        ; Save the 128K pages at 0xC000.
213
        ld a, (SNA_7FFD)                ; Skip the page we've aready
214
        and 7                           ; saved (as it was in 0xC000)
215
        ld e, a
216
        xor a                           ; start from page 0
217 372 winston
.save128loop2:
218 263 winston
        cp e                            ; skip the page that was already
219 372 winston
        jr z,  .next2                   ; saved, also skip pages 2 and 5
220 263 winston
        cp 0x02
221 372 winston
        jr z,  .next2
222 263 winston
        cp 0x05
223 372 winston
        jr z,  .next2
224 263 winston
        ld bc, 0x7FFD                   ; set the page we want to save
225
        out (c), a
226
 
227
        push af
228
        push de
229
        ld hl, 0xC000
230
        ld bc, 0x4000
231 305 winston
        ld a, (v_snapfd)
232
        call WRITE
233 372 winston
        jr c,  .writeerr1282
234 263 winston
        pop de
235
        pop af
236 372 winston
.next2:
237 263 winston
        inc a
238
        cp 0x08                         ; all pages have been saved
239 372 winston
        jr nz,  .save128loop2
240 265 winston
        ld a, (SNA_7FFD)                ; restore 128K paging but with
241
        and 0xF7                        ; but ensure the normal screen
242
        ld bc, 0x7FFD                   ; is in use for the NMI menu
243 263 winston
        out (c), a
244 372 winston
        jr  .writedone2
245 263 winston
 
246 372 winston
.writeerr1282:
247 263 winston
        pop de
248
        pop de                          ; don't overwrite AF
249 372 winston
        jr  .writeerr2
250 263 winston
 
251 372 winston
.savemain2:
252 255 winston
        ; TODO: Something ought to be done with the R register, really.
253
        ; Save the interrupts-and-stuff block.
254
        ld hl, HEADER
255
        ld bc, HEADERSZ
256
        ld a, (v_snapfd)
257
        call WRITE
258 263 winston
        ret c
259 255 winston
 
260 272 winston
        ; Save screen RAM.
261
        call F_savesavedscreen
262 255 winston
 
263 272 winston
        ld hl, 23296                    ; Start saving from this address
264
        ld bc, 42240                    ; For this many bytes
265 255 winston
        ld a, (v_snapfd)
266
        call WRITE
267
        ret
268
 
269 272 winston
;----------------------------------------------------------------------
270
; F_savesavedscreen
271
; Save the screen that was copied to 0xDA:000 to 0xDB:AFF
272 372 winston
.globl F_savesavedscreen
273
F_savesavedscreen:
274 272 winston
        ld a, 0xDA                      ; first page
275
        call PUSHPAGEA                  ; set it and save current page
276
        ld hl, 0x1000
277
        ld bc, 0x1000
278 305 winston
        ld a, (v_snapfd)
279
        call WRITE
280 372 winston
        jr c,  .restoreerr3
281 272 winston
        ld a, 0xDB
282
        call SETPAGEA
283
        ld hl, 0x1000
284
        ld bc, 0xB00
285 305 winston
        ld a, (v_snapfd)
286
        call WRITE
287 372 winston
.restoreerr3:
288 272 winston
        call POPPAGEA                   ; restore original page
289
        ret
290
 
291 372 winston
.globl F_restorescreen
292
F_restorescreen:
293 272 winston
        ld a, 0xDA
294
        call PUSHPAGEA
295
        ld hl, 0x1000
296
        ld de, 0x4000
297
        ld bc, 0x1000
298
        ldir
299
        ld a, 0xDB
300
        call SETPAGEA
301
        ld hl, 0x1000
302
        ld de, 0x5000
303
        ld bc, 0xB00
304
        ldir
305
        call POPPAGEA
306
        ret
307