-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcd_kosinski.asm
158 lines (127 loc) · 4.96 KB
/
mcd_kosinski.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
; ----------------------------------------------------------------------
; Mega CD Mode 1 Library
; ----------------------------------------------------------------------
; Kosinski decompression
; Format details: https://segaretro.org/Kosinski_compression
; ----------------------------------------------------------------------
; Copyright (c) 2024 Devon Artmeier
;
; Permission to use, copy, modify, and/or distribute this software
; for any purpose with or without fee is hereby granted.
;
; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIE
; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
; DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
; PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
; TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
; PERFORMANCE OF THIS SOFTWARE.
; ----------------------------------------------------------------------
; ----------------------------------------------------------------------
; Decompress Kosinski data
; ----------------------------------------------------------------------
; PARAMETERS:
; a0.l - Pointer to source data
; a1.l - Pointer to destination buffer
; ----------------------------------------------------------------------
; RETURNS:
; a0.l - Pointer to end of source data
; a1.l - Pointer to end of destination buffer
; ----------------------------------------------------------------------
McdKosDec:
movem.l d0-d3/a2,-(sp) ; Save registers
move.b (a0)+,-(sp) ; Read from data stream
move.b (a0)+,-(sp)
move.w (sp)+,d1
move.b (sp)+,d1
moveq #16-1,d0 ; 16 bits to process
; ----------------------------------------------------------------------
McdKosDec_GetCode:
lsr.w #1,d1 ; Get code
bcc.s McdKosDec_Code0x ; If it's 0, branch
; ----------------------------------------------------------------------
McdKosDec_Code1:
dbf d0,McdKosDec_CopyUncByte ; Decrement bits left to process
move.b (a0)+,-(sp) ; Read from data stream
move.b (a0)+,-(sp)
move.w (sp)+,d1
move.b (sp)+,d1
moveq #16-1,d0 ; 16 bits to process
McdKosDec_CopyUncByte:
move.b (a0)+,(a1)+ ; Copy uncompressed byte
bra.s McdKosDec_GetCode ; Process next code
; ----------------------------------------------------------------------
McdKosDec_Code0x:
dbf d0,McdKosDec_PrepareCopy ; Decrement bits left to process
move.b (a0)+,-(sp) ; Read from data stream
move.b (a0)+,-(sp)
move.w (sp)+,d1
move.b (sp)+,d1
moveq #16-1,d0 ; 16 bits to process
McdKosDec_PrepareCopy:
moveq #$FFFFFFFF,d2 ; Copy offsets are always negative
moveq #0,d3 ; Reset copy counter
lsr.w #1,d1 ; Get 2nd code bit
bcs.s McdKosDec_Code01 ; If the full code is 01, branch
; ----------------------------------------------------------------------
McdKosDec_Code00:
dbf d0,McdKosDec_GetNumBytesH ; Decrement bits left to process
move.b (a0)+,-(sp) ; Read from data stream
move.b (a0)+,-(sp)
move.w (sp)+,d1
move.b (sp)+,d1
moveq #16-1,d0 ; 16 bits to process
McdKosDec_GetNumBytesH:
lsr.w #1,d1 ; Get number of bytes to copy (upper)
addx.w d3,d3
dbf d0,McdKosDec_GetNumBytesL ; Decrement bits left to process
move.b (a0)+,-(sp) ; Read from data stream
move.b (a0)+,-(sp)
move.w (sp)+,d1
move.b (sp)+,d1
moveq #16-1,d0 ; 16 bits to process
McdKosDec_GetNumBytesL:
lsr.w #1,d1 ; Get number of bytes to copy (lower)
addx.w d3,d3
dbf d0,McdKosDec_GetCopyOffset00 ; Decrement bits left to process
move.b (a0)+,-(sp) ; Read from data stream
move.b (a0)+,-(sp)
move.w (sp)+,d1
move.b (sp)+,d1
moveq #16-1,d0 ; 16 bits to process
McdKosDec_GetCopyOffset00:
move.b (a0)+,d2 ; Get copy offset
; ----------------------------------------------------------------------
McdKosDec_Copy:
lea (a1,d2.w),a2 ; Get copy address
move.b (a2)+,(a1)+ ; Copy a byte
McdKosDec_CopyLoop:
move.b (a2)+,(a1)+ ; Copy a byte
dbf d3,McdKosDec_CopyLoop ; Loop until bytes are copied
bra.w McdKosDec_GetCode ; Process next code
; ----------------------------------------------------------------------
McdKosDec_Code01:
dbf d0,McdKosDec_GetCopyOffset01 ; Decrement bits left to process
move.b (a0)+,-(sp) ; Read from data stream
move.b (a0)+,-(sp)
move.w (sp)+,d1
move.b (sp)+,d1
moveq #16-1,d0 ; 16 bits to process
McdKosDec_GetCopyOffset01:
move.b (a0)+,-(sp) ; Get copy offset
move.b (a0)+,d2
move.b d2,d3
lsl.w #5,d2
move.b (sp)+,d2
andi.w #7,d3 ; Get 3-bit copy count
bne.s McdKosDec_Copy ; If this is a 3-bit copy count, branch
move.b (a0)+,d3 ; Get 8-bit copy count
beq.s McdKosDec_End ; If it's 0, we are done decompressing
subq.b #1,d3 ; Is it 1?
bne.s McdKosDec_Copy ; If not, start copying
bra.w McdKosDec_GetCode ; Process next code
McdKosDec_End:
movem.l (sp)+,d0-d3/a2 ; Restore registers
rts
; ----------------------------------------------------------------------