Added the compression routine

+Use of SA1; used for render calculations and compression
This commit is contained in:
Morten Delenk 2015-04-06 17:24:15 -05:00
parent 0a2a2ae622
commit 4231d94b4a
3 changed files with 533 additions and 1 deletions

267
decompression.asm Normal file
View file

@ -0,0 +1,267 @@
.BANK 0 SLOT 0
.ORG 0
.SECTION "Decompression"
;decompReq SRC, DST
;Source must be in ROM, IRAM, BWRAM
;Destination must be in IRAM, BWRAM
.MACRO decompReq
ldx.W \1
stx $3011
lda.B :\1
sta $3013
ldx.W \2
stx $3014
lda.B :\2
sta $3016
lda #$02
sta $3010
lda #$0F
sta $2200
lda #$01
jsl WaitForSA1
.ENDM
Decomp:
php
phb
sep #$20
rep #$10
lda $13
pha
plb
stz $18
ldy #$0000
MainDecompLoop:
phx
ldx $11
lda $0000,X
inx
bne DC_NoOF1
jsr SBOF_Correction
DC_NoOF1:
stx $11
plx
sta $19
cmp #$FF ;End of File?
bne DC_Cont
lda #$01
sta $2209
plb
plp
ply
plx
pla
rtl
DC_Cont:
and #$E0
cmp #$E0
bne DC_NormalCommand
;Command 111: Extended
lda $19 ;Get Byte Value
asl
asl
asl ;Shift out command
and #$E0
pha
lda $19
and #$03
xba ;B=High bits of length
phx
ldx $11
lda $0000,X
inx
bne DC_NoOF2
jsr SBOF_Correction
DC_NoOF2:
stx $11
plx
bra DC_Main
DC_NormalCommand:
pha
lda #$00
xba ;B=0
lda $19
and #$1F
DC_Main:
tax
inx ;X=Counter+1
pla ;A=Command
cmp #$00
bpl DCC_plus
jmp DCC_minus
DCC_plus:
cmp #$20
beq DCC_Bytefill
cmp #$40
beq DCC_Wordfill
cmp #$60
beq DCC_Incfill
DCC_Copy:
phx
ldx $11
lda $0000,X
inx
bne DC_NoOF3
jsr SBOF_Correction
DC_NoOF3:
stx $11
plx
sta [$14],Y
iny ;Destination++=A
dex
bne DCC_Copy
beq MainDecompLoop ;Next!
DCC_Bytefill:
phx
ldx $11
lda $0000,X
inx
bne DC_NoOF4
jsr SBOF_Correction
DC_NoOF4:
stx $11
plx
DCC_Bytefill_Loop:
sta [$14],Y
iny
dex
bne DCC_Bytefill_Loop
jmp MainDecompLoop
DCC_Wordfill:
phx
ldx $11
lda $0000,X
inx
bne DC_NoOF5
jsr SBOF_Correction
DC_NoOF5:
xba
lda $0000,X
inx
bne DC_NoOF6
jsr SBOF_Correction
DC_NoOF6:
stx $11
plx
xba
DCC_Wordfill_Loop:
sta [$14],Y
xba
iny
dex
beq DCC_Wordfill_Out
sta [$14],Y
xba
iny
dex
bne DCC_Wordfill_Loop
DCC_Wordfill_Out:
jmp MainDecompLoop
DCC_Incfill:
phx
ldx $11
lda $0000,X
inx
bne DC_NoOF7
jsr SBOF_Correction
DC_NoOF7:
stx $11
plx
DCC_Incfill_Loop:
sta [$14],Y
ina
iny
dex
bne DCC_Incfill_Loop
jmp MainDecompLoop
DCC_minus:
cmp #$C0
bcs DCC_C0plus
DictionaryCopy:
and #$20
sta $18 ;Inverted bit
phx
ldx $11
lda $0000,X
inx
bne DC_NoOF8
jsr SBOF_Correction
DC_NoOF8:
sta $19
lda $0000,X
inx
bne DC_NoOF9
jsr SBOF_Correction
DC_NoOF9:
stx $11
plx
sta $1A
DictionaryCopyStart:
sep #$20
DictionaryCopyLoop:
phx
phy
ldy $19
lda [$14],Y
iny
sty $19
ply
xba
lda $18
beq DictionaryCopyNoInvert
xba
eor #$FF
xba
DictionaryCopyNoInvert:
xba
sta [$14],Y
iny
plx
dex
bne DictionaryCopyLoop
jmp MainDecompLoop
DCC_C0plus:
and #$20
sta $18 ;Inverted bit
phx
ldx $11
lda $0000,X
inx
bne DC_NoOF10
jsr SBOF_Correction
DC_NoOF10:
stx $11
plx
sta $19
stz $1A
rep #$20
tya
sec
sbc $4A
sta $4A
bra DictionaryCopyStart
SBOF_Correction:
pha
phb
pla
cmp #$3F
bmi BOF_case1
beq BOF_case2
cmp #$BF
bmi BOF_case1
ina
ldx #$0000
BOF_back:
pha
plb
pla
rts
BOF_case1:
ina
ldx #$8000
bra BOF_back
BOF_case2:
lda #$80
ldx #$8000
bra BOF_back
.ENDS

5
gfx.asm Normal file
View file

@ -0,0 +1,5 @@
.BANK $15 SLOT 0
.ORG $00D8
.SECTION "GFX"
.incbin "orig.smc" SKIP 688128 READ 32768
.ENDS

262
main.asm
View file

@ -1,4 +1,6 @@
.INCLUDE "header.asm"
.INCLUDE "decompression.asm"
.INCLUDE "gfx.asm"
.BANK 0
.ORG 0
.SECTION "main" SEMIFREE
@ -7,5 +9,263 @@ NMI:
IRQ:
rti
Reset:
;This code is a optimized copy from Kirby Super Star
sei
clc
xce
sep #$20
.ACCU 8
rep #$10
.INDEX 16
ldx #$1FFF
ldy #$0000
txs
phk
plb
lda #$01
sta $4200
rep #$20
lda #$2100
tcd
sep #$20
ldx #$038F
stx $00
ldx #$8000
stx $02
stz $04
stz $04
lda #$01
sta $05
stz $06
sty $0D
sty $0D
sty $0F
sty $0F
sty $11
sty $11
sty $13
sty $13
lda #$80
sta $15
sty $16
stz $1A
sty $1B
iny
sty $1B
dey
sty $1D
sty $1D
sty $1F
sty $1F
stz $21
sty $23
sty $25
sty $27
sty $29
ldx #$1000
stx $2B
sty $2E
ldx #$0030
stx $30
ldx #$00E0
stx $32
rep #$20
lda #$4200
tcd
ldx #$FF00
stx $00
sty $02
sty $04
sty $06
sty $08
sty $0A
sty $0C
lda #$2200
tcd
sep #$20
ldx #$0020
stx $00 ;Reset SA1 and disable SA1-Ints
lda #$A0
sta $02 ;Clear all sa1 ints
lda #$04
sta $20
ina
sta $21
ina
sta $22
ina
sta $23 ;Mapped banks
stz $24
dea
dea
sta $28
lda #$80
sta $26
lda #$FF
sta $29
sty $3000 ;3000=Command
stz $3002 ;3001-3002=Argument
.INDEX 16
ldx.w SA1Reset
stx $03 ;Reset addr
stz $00 ;Release SA1
;That section is widely undocumented. What it does is, that every register is cleared and the write protection is turned off
;MB1 is mapped to $00-$1F:$8000-$FFFF
;MB2 is mapped to $20-$3F:$8000-$FFFF
;MB3 is mapped to $80-$9F:$8000-$FFFF
;MB4 is mapped to $A0-$BF:$8000-$FFFF
;MB5 is mapped to $C0-$CF:$0000-$FFFF
;MB6 is mapped to $D0-$DF:$0000-$FFFF
;MB7 is mapped to $E0-$EF:$0000-$FFFF
;MB8 is mapped to $F0-$FF:$0000-$FFFF
lda #$FF
loop:
dea
bne loop
ldx #$80D8
stx $3011
lda #$15
sta $3013
ldx #$0100
stx $3014
lda #$00
sta $3016
lda #$02
sta $3010
lda #$0F
sta $2200
lda #$01
jsl WaitForSA1
deadloop:
bra deadloop
SA1Reset:
sei
clc
xce
rep #$30
lda #$0000
tcd
stz $00
stz $02
stz $04
stz $06
stz $2209 ;Clear the snes control register
stz $2225 ;Map area 0 of bwram
lda #$80
sta $2227 ;Turn off bwram protection
stz $222A ;Turn off IRAM-Protection
stz $2230 ;Turn off DMA
rep #$30
;Init Game Variables
stz $0590
jsl ClearOAMXSize
jsl ClearUnusedOAM
stz $071D
stz $071F
stz $0721
stz $00
stz $01
stz $85
lda #$0003
sta $52
lda #$0180
sta $54
stz $57
stz $59
stz $5C
stz $5E
stz $60
stz $62
stz $64
stz $66
lda #$1000
sta $68
sta $6B
sta $6D
stz $71
lda #$4020
sta $74
lda #$0095
sta $76
sep #$20
;Sync CPU with SA1
lda #$0F
jsl WaitForSNES
jmp deadloop ;Write code for it later!
ClearOAMXSize: ;Going to be SA1
phx
php
rep #$30
ldx #$001E
stz $0570
OAMXSizeClearLoop:
stz $0570,X
dex
dex
bne OAMXSizeClearLoop
plp
plx
rtl
ClearUnusedOAM: ;Going to be SA1
php
rep #$30
lda $0590
cmp.W #$0200
bcs ClearUnusedOAMDone
adc #$0370
tax
ClearUnusedOAMLoop:
stz $00,X
inx
inx
inx
inx
cmp.W #$0571
bne ClearUnusedOAMLoop
ClearUnusedOAMDone:
stz $0590
plp
rtl
WaitForSNES:;SA1
pha
phx
phy
sep #$20
rep #$10
WaitForSNES_Loop:
cmp $2301 ;Wait until message for SA1 equals
bne WaitForSNES_Loop
ldy $3011
lda $3010
tax
jmp (CommandList,X) ;Sadly only in bank 0
WaitForSA1: ;This routine will be copied to wram $7E:0000
;The RAM variant is for actions that the sa1 should to with full 10 mhz
;Calling this function directly is for 5MHz (or 10MHz if SA1 is in IRAM or BWRAM)
pha
phx
phy
php
sep #$20
rep #$10
WaitForSA1_Loop:
cmp $2300 ;Wait until message for SNES equals to F
bne WaitForSA1_Loop
ldy $3001
lda $3000
asl
tax
jmp (CommandList,X) ;Sadly only in bank 0
Return:
plp
plx
plx
pla
rtl
Jumpto:
jmp ($3001)
CommandList:
.dw Return
.dw Jumpto
.dw Decomp
.ENDS