Subversion Repositories Spectranet

[/] [branches/] [gnubinutils/] [modules/] [snapman/] [savesna.asm] - Diff between revs 371 and 372

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 371 Rev 372
Line 19... Line 19...
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;THE SOFTWARE.
;THE SOFTWARE.
 
 
; Save snapshots in SNA format.
; Save snapshots in SNA format.
 
.include        "fcntl.inc"
 
.include        "snapheader.inc"
 
.include        "snapman.inc"
 
.include        "spectranet.inc"
 
.include        "sysvars.inc"
 
 
;--------------------------------------------------------------------------
;--------------------------------------------------------------------------
; F_savesna48
; F_savesna48
; Save a 48K snapshot. Null terminated filename pointed to by HL.
; Save a 48K snapshot. Null terminated filename pointed to by HL.
UNPAGE  equ     0x007C
UNPAGE  equ     0x007C
SAVERAM equ     0x3600
SAVERAM equ     0x3600
MAINSPSAVE equ  0x35FE
MAINSPSAVE equ  0x35FE
TEMPSP  equ     0x81FE
TEMPSP  equ     0x81FE
 
 
F_snaptest
.globl F_snaptest
 
F_snaptest:
        ld hl, STR_saving
        ld hl, STR_saving
        call PRINT42
        call PRINT42
 
 
        ld hl, STR_filename
        ld hl, STR_filename
        ld de, 0x3000
        ld de, 0x3000
        xor a
        xor a
.loop
.loop1:
        ldi
        ldi
        cp (hl)
        cp (hl)
        jr nz, .loop
        jr nz,  .loop1
.done
.done1:
        ld (de), a
        ld (de), a
        ld hl, 0x3000
        ld hl, 0x3000
        call F_savesna
        call F_savesna
        jr c, .borked
        jr c,  .borked1
 
 
        ld a, (v_border)                ; Restore the border
        ld a, (v_border)                ; Restore the border
        out (254), a
        out (254), a
        call F_restorescreen            ; Restore the screen
        call F_restorescreen            ; Restore the screen
        ld a, (SNA_EIDI)                ; Re.enable interrupts?
        ld a, (SNA_EIDI)                ; Re.enable interrupts?
        and a                           ; No
        and a                           ; No
        jr nz, .retei                   ; If yes, then EI on ret
        jr nz,  .retei1                 ; If yes, then EI on ret
        ld sp, NMISTACK-14              ; Set SP to where the stack was
        ld sp, NMISTACK-14              ; Set SP to where the stack was
        pop af
        pop af
        ex af, af'
        ex af, af'
        pop af
        pop af
        pop bc
        pop bc
        pop de
        pop de
        pop hl
        pop hl
        ld sp, (NMISTACK)
        ld sp, (NMISTACK)
        jp UNPAGE                       ; RET
        jp UNPAGE                       ; RET
.borked
.borked1:
        ld a, 2
        ld a, 2
        out (254), a
        out (254), a
        ret
        ret
 
 
.retei
.retei1:
        ld sp, NMISTACK-14
        ld sp, NMISTACK-14
        pop af
        pop af
        ex af, af'
        ex af, af'
        pop af
        pop af
        pop bc
        pop bc
Line 80... Line 86...
 
 
;------------------------------------------------------------------------
;------------------------------------------------------------------------
; HL = pointer to filename string
; HL = pointer to filename string
; Entry to the NMI saves the folowing at NMISTACK-4:
; Entry to the NMI saves the folowing at NMISTACK-4:
; hl, de, bc, af, af'
; hl, de, bc, af, af'
F_savesna
.globl F_savesna
 
F_savesna:
        ld d, O_CREAT | O_TRUNC         ; Flags
        ld d, O_CREAT | O_TRUNC         ; Flags
        ld e, O_WRONLY                  ; File mode
        ld e, O_WRONLY                  ; File mode
        call OPEN                       ; Open the snapshot file
        call OPEN                       ; Open the snapshot file
        ret c
        ret c
        ld (v_snapfd), a                ; save the file descriptor
        ld (v_snapfd), a                ; save the file descriptor
Line 114... Line 121...
        ld (SNA_DEALT), de
        ld (SNA_DEALT), de
        ld (SNA_BCALT), bc
        ld (SNA_BCALT), bc
        exx
        exx
        ld a, i                         ; also sets P/V flag if IFF2 is
        ld a, i                         ; also sets P/V flag if IFF2 is
        ld (SNA_I), a                   ; set (interrupts enabled)
        ld (SNA_I), a                   ; set (interrupts enabled)
        call pe, .setei
        call pe,  .setei2
 
 
        ld a, (v_border)                ; Copy the border colour into
        ld a, (v_border)                ; Copy the border colour into
        ld (SNA_BORDER), a              ; the snapshot header.
        ld (SNA_BORDER), a              ; the snapshot header.
 
 
        ; Now something must be done to detect interrupt mode
        ; Now something must be done to detect interrupt mode
        ld hl, 0x3600
        ld hl, 0x3600
        ld de, 0x3601
        ld de, 0x3601
        ld bc, 258                      ; table is actually 0x100 bytes
        ld bc, 258                      ; table is actually 0x100 bytes
        ld (hl), F_im2 % 256            ; LSB of IM 2 routine.
        ld (hl), F_im2_lsb              ; LSB of IM 2 routine.
        ldir
        ldir
        ld a, 0x36                      ; MSB of the table
        ld a, 0x36                      ; MSB of the table
        ld i, a                         ; set the vector table
        ld i, a                         ; set the vector table
 
 
        ld hl, 0
        ld hl, 0
        ld (v_intcount), hl             ; reset IM 1 counter to detect IM 1
        ld (v_intcount), hl             ; reset IM 1 counter to detect IM 1
        ld hl, .continue                ; "Return" address
        ld hl,  .continue2              ; "Return" address
        push hl                         ; on the stack so RETN
        push hl                         ; on the stack so RETN
        retn                            ; takes us just to the next addr.
        retn                            ; takes us just to the next addr.
 
 
.continue
.continue2:
        xor a
        xor a
        ld (SNA_IM), a                  ; clear IM x
        ld (SNA_IM), a                  ; clear IM x
        ei
        ei
        halt                            ; wait for an interrupt
        halt                            ; wait for an interrupt
        ld a, (SNA_IM)                  ; Did the IM 2 routine update
        ld a, (SNA_IM)                  ; Did the IM 2 routine update
        and a                           ; the interrupt mode?
        and a                           ; the interrupt mode?
        jr nz, .donewithinterrupts      ; Done.
        jr nz,  .donewithinterrupts2    ; Done.
 
 
        inc a
        inc a
        ld (SNA_IM), a                  ; set interrupt mode 1
        ld (SNA_IM), a                  ; set interrupt mode 1
 
 
        ; Now it's all over bar the shouting.
        ; Now it's all over bar the shouting.
.donewithinterrupts
.donewithinterrupts2:
        di
        di
 
 
        ; Restore the I register
        ; Restore the I register
        ld a, (SNA_I)
        ld a, (SNA_I)
        ld i, a
        ld i, a
 
 
        ; Detect whether we're saving a 48K or a 128K snapshot
        ; Detect whether we're saving a 48K or a 128K snapshot
        ld a, (v_machinetype)
        ld a, (v_machinetype)
        and 1                           ; Bit 1 set = 128K
        and 1                           ; Bit 1 set = 128K
        jr nz, .save128snap
        jr nz,  .save128snap2
 
 
        call .savemain                  ; save header and memory
        call  .savemain2                        ; save header and memory
.writedone
.writedone2:
        ld a, (v_snapfd)                ; close the file
        ld a, (v_snapfd)                ; close the file
        call VCLOSE
        call VCLOSE
        ret
        ret
 
 
.writeerr
.writeerr2:
        push af
        push af
        ld a, (v_snapfd)
        ld a, (v_snapfd)
        call VCLOSE
        call VCLOSE
        pop af
        pop af
        ret
        ret
.setei
.setei2:
        ld a, 4
        ld a, 4
        ld (SNA_EIDI), a
        ld (SNA_EIDI), a
        ret
        ret
 
 
.save128snap
.save128snap2:
        ; copy port 0x7FFD value to the header
        ; copy port 0x7FFD value to the header
        ld a, (v_port7ffd)
        ld a, (v_port7ffd)
        ld (SNA_7FFD), a
        ld (SNA_7FFD), a
 
 
        ; adjust header suitably for 128K snapshots
        ; adjust header suitably for 128K snapshots
Line 191... Line 198...
        inc hl
        inc hl
        ld (SNA_SP), hl                 ; Set the snapshot SP.
        ld (SNA_SP), hl                 ; Set the snapshot SP.
        ld (SNA_PC), de                 ; Set the PC
        ld (SNA_PC), de                 ; Set the PC
        xor a
        xor a
        ld (SNA_TRDOS), a               ; TRDOS always 0
        ld (SNA_TRDOS), a               ; TRDOS always 0
        call .savemain                  ; the header + 48K
        call  .savemain2                        ; the header + 48K
        jr c, .writeerr
        jr c,  .writeerr2
 
 
        ld hl, SNA_PC                   ; Save the 128K second header
        ld hl, SNA_PC                   ; Save the 128K second header
        ld bc, 4                        ; (4 bytes long)
        ld bc, 4                        ; (4 bytes long)
        ld a, (v_snapfd)
        ld a, (v_snapfd)
        call WRITE
        call WRITE
        jr c, .writeerr
        jr c,  .writeerr2
 
 
        ; Save the 128K pages at 0xC000.
        ; Save the 128K pages at 0xC000.
        ld a, (SNA_7FFD)                ; Skip the page we've aready
        ld a, (SNA_7FFD)                ; Skip the page we've aready
        and 7                           ; saved (as it was in 0xC000)
        and 7                           ; saved (as it was in 0xC000)
        ld e, a
        ld e, a
        xor a                           ; start from page 0
        xor a                           ; start from page 0
.save128loop
.save128loop2:
        cp e                            ; skip the page that was already
        cp e                            ; skip the page that was already
        jr z, .next                     ; saved, also skip pages 2 and 5
        jr z,  .next2                   ; saved, also skip pages 2 and 5
        cp 0x02
        cp 0x02
        jr z, .next
        jr z,  .next2
        cp 0x05
        cp 0x05
        jr z, .next
        jr z,  .next2
        ld bc, 0x7FFD                   ; set the page we want to save
        ld bc, 0x7FFD                   ; set the page we want to save
        out (c), a
        out (c), a
 
 
        push af
        push af
        push de
        push de
        ld hl, 0xC000
        ld hl, 0xC000
        ld bc, 0x4000
        ld bc, 0x4000
        ld a, (v_snapfd)
        ld a, (v_snapfd)
        call WRITE
        call WRITE
        jr c, .writeerr128
        jr c,  .writeerr1282
        pop de
        pop de
        pop af
        pop af
.next
.next2:
        inc a
        inc a
        cp 0x08                         ; all pages have been saved
        cp 0x08                         ; all pages have been saved
        jr nz, .save128loop
        jr nz,  .save128loop2
        ld a, (SNA_7FFD)                ; restore 128K paging but with
        ld a, (SNA_7FFD)                ; restore 128K paging but with
        and 0xF7                        ; but ensure the normal screen
        and 0xF7                        ; but ensure the normal screen
        ld bc, 0x7FFD                   ; is in use for the NMI menu
        ld bc, 0x7FFD                   ; is in use for the NMI menu
        out (c), a
        out (c), a
        jr .writedone
        jr  .writedone2
 
 
.writeerr128
.writeerr1282:
        pop de
        pop de
        pop de                          ; don't overwrite AF
        pop de                          ; don't overwrite AF
        jr .writeerr
        jr  .writeerr2
 
 
.savemain
.savemain2:
        ; TODO: Something ought to be done with the R register, really.
        ; TODO: Something ought to be done with the R register, really.
        ; Save the interrupts-and-stuff block.
        ; Save the interrupts-and-stuff block.
        ld hl, HEADER
        ld hl, HEADER
        ld bc, HEADERSZ
        ld bc, HEADERSZ
        ld a, (v_snapfd)
        ld a, (v_snapfd)
Line 260... Line 267...
        ret
        ret
 
 
;----------------------------------------------------------------------
;----------------------------------------------------------------------
; F_savesavedscreen
; F_savesavedscreen
; Save the screen that was copied to 0xDA:000 to 0xDB:AFF
; Save the screen that was copied to 0xDA:000 to 0xDB:AFF
F_savesavedscreen
.globl F_savesavedscreen
 
F_savesavedscreen:
        ld a, 0xDA                      ; first page
        ld a, 0xDA                      ; first page
        call PUSHPAGEA                  ; set it and save current page
        call PUSHPAGEA                  ; set it and save current page
        ld hl, 0x1000
        ld hl, 0x1000
        ld bc, 0x1000
        ld bc, 0x1000
        ld a, (v_snapfd)
        ld a, (v_snapfd)
        call WRITE
        call WRITE
        jr c, .restoreerr
        jr c,  .restoreerr3
        ld a, 0xDB
        ld a, 0xDB
        call SETPAGEA
        call SETPAGEA
        ld hl, 0x1000
        ld hl, 0x1000
        ld bc, 0xB00
        ld bc, 0xB00
        ld a, (v_snapfd)
        ld a, (v_snapfd)
        call WRITE
        call WRITE
.restoreerr
.restoreerr3:
        call POPPAGEA                   ; restore original page
        call POPPAGEA                   ; restore original page
        ret
        ret
 
 
F_restorescreen
.globl F_restorescreen
 
F_restorescreen:
        ld a, 0xDA
        ld a, 0xDA
        call PUSHPAGEA
        call PUSHPAGEA
        ld hl, 0x1000
        ld hl, 0x1000
        ld de, 0x4000
        ld de, 0x4000
        ld bc, 0x1000
        ld bc, 0x1000