Skip to content

SNA to LROS cartridge converter

1 message · 2011-08-11 → 2011-08-11 · Yahoo Group era · View archive on archive.org

Participants: cheveron

Preserved from the Timex/Sinclair 2068 Yahoo Group (2001–2019), which is no longer online. Text reproduced from the archive.org archive; email addresses masked.

Messages

1. SNA to LROS cartridge converter

cheveron · Thu, 11 Aug 2011 11:44

Let me know if you need any further explanation.

; SNA to LROS cart converter
; (c) 2011 Andrew Owen

; packed file must fit in 16K or less and the .dck header be adjusted to suit
; the header should be 00 02 02 00 00 00 00 00 00 for a 16K cart or
; 00 02 00 00 00 00 00 00 00 for an 8K cart

; the data must be packed with Bitbuster Extreme:
; http://www.west.co.tt/matt/speccy/apology/

; the top 16 bytes of RAM must be excluded as they will be used during depacking

; the values for the following labels can be found in the SNA header:

i_reg       equ $3f
hl1_reg     equ $2b16
de1_reg     equ $3ab6
bc1_reg     equ $8070
af1_reg     equ $0093
hl_reg      equ $5ae1
de_reg      equ $0001
bc_reg      equ $0117
iy_reg      equ $5c3a
ix_reg      equ $255e
;interrupt  ; remove final EI if set to disabled
r_reg       equ $28
af_reg      equ $0044
sp_reg      equ 61e0h
;im_mode    ; edit the im mode line manually
border      equ 1

org 0

start:
    defb    0       ; LROS header
    defb    1
    defw    5
    defb    $fe

    di              ; disable interrupts

    ld  a, border   ; set border
    out ($fe), a    ;

    ld  sp, 0       ; set a stack

    ld  hl, data    ; source
    ld  de, $4000   ; destination

    call    depack

resume:
    ld  hl, prog    ; copy prog
    ld  de, 5B00h
    ld  bc, 0005h
    ldir

    im  1       ; set the interrupt mode

    ld  a, i_reg    ; restore registers
    ld  i, a
    ld  hl, hl1_reg
    ld  de, de1_reg
    ld  bc, af1_reg
    push    bc
    pop af
    ld  bc, bc1_reg
    exx
    ld  hl, hl_reg
    ld  de, de_reg
    ld  iy, iy_reg
    ld  ix, ix_reg
    ld  a, r_reg
    ld  r, a
    ld  bc, af_reg
    push    bc
    pop af
    ld  bc, bc_reg
    ld  sp, sp_reg  ; return address

    jp  5B00h       ; jump to prog in RAM

prog:
    xor a           ; page out ROM
    out (00F4h), a
    ei
    ret 

depack:
    ld  a,128

    exx
    ld  de,1
    exx

depack_loop:
    call getbits
    jr  c,output_compressed ;if set, we got lz77 compression
    ldi             ;copy byte from compressed data to destination (literal byte)
    jr  depack_loop


;handle compressed data
output_compressed:
    ld  c,(hl)      ;get lowest 7 bits of offset, plus offset extension bit
    inc hl      ;to next byte in compressed data

output_match:
    ld  b,0
    bit 7,c
    jr  z,output_match1 ;no need to get extra bits if carry not set

    call getbits
    call rlbgetbits
    call rlbgetbits
    call rlbgetbits

    jr  c,output_match1 ;since extension mark already makes bit 7 set 
    res 7,c     ;only clear it if the bit should be cleared
output_match1:
    inc bc


;return a gamma-encoded value
;length returned in HL
    exx         ;to second register set!
    ld  h,d
    ld  l,e             ;initial length to 1
    ld  b,e     ;bitcount to 1

;determine number of bits used to encode value
get_gamma_value_size:
    exx
    call getbits
    exx
    jr  nc,get_gamma_value_size_end ;if bit not set, bitlength of remaining is known
    inc b               ;increase bitcount
    jr  get_gamma_value_size        ;repeat...

get_gamma_value_bits:
    exx
    call getbits
    exx

    adc hl,hl               ;insert new bit in HL
get_gamma_value_size_end:
    djnz    get_gamma_value_bits        ;repeat if more bits to go

get_gamma_value_end:
    inc hl      ;length was stored as length-2 so correct this
    exx         ;back to normal register set

    ret c
;HL' = length

    push    hl      ;address compressed data on stack

    exx
    push    hl      ;match length on stack
    exx

    ld  h,d
    ld  l,e     ;destination address in HL...
    sbc hl,bc       ;calculate source address

    pop bc      ;match length from stack

    ldir            ;transfer data

    pop hl      ;address compressed data back from stack

    jr  depack_loop

rlbgetbits
    rl b
getbits
    add a,a
    ret nz
    ld  a,(hl)
    inc hl
    rla
    ret

data:
incbin "data.pck"

defs 2048, $ff  ; file should either be $2009 or $4009 bytes with the header.

Indexed under

Cartridges, EPROM & dumping · Utilities & applications · Spectrum emulation & software