Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libsnes/bsnes/snes/chip/sdd1/decomp.cpp
2 views
1
//S-DD1 decompression algorithm implementation
2
//original code written by Andreas Naive (public domain license)
3
//bsnes port written by byuu
4
5
//note: decompression module does not need to be serialized with bsnes
6
//this is because decompression only runs during DMA, and bsnes will complete
7
//any pending DMA transfers prior to serialization.
8
9
//input manager
10
11
void SDD1::Decomp::IM::init(unsigned offset_) {
12
offset = offset_;
13
bit_count = 4;
14
}
15
16
uint8 SDD1::Decomp::IM::get_codeword(uint8 code_length) {
17
uint8 codeword;
18
uint8 comp_count;
19
20
codeword = self.rom_read(offset) << bit_count;
21
bit_count++;
22
23
if(codeword & 0x80) {
24
codeword |= self.rom_read(offset + 1) >> (9 - bit_count);
25
bit_count += code_length;
26
}
27
28
if(bit_count & 0x08) {
29
offset++;
30
bit_count &= 0x07;
31
}
32
33
return codeword;
34
}
35
36
//golomb-code decoder
37
38
const uint8 SDD1::Decomp::GCD::run_count[] = {
39
0x00, 0x00, 0x01, 0x00, 0x03, 0x01, 0x02, 0x00,
40
0x07, 0x03, 0x05, 0x01, 0x06, 0x02, 0x04, 0x00,
41
0x0f, 0x07, 0x0b, 0x03, 0x0d, 0x05, 0x09, 0x01,
42
0x0e, 0x06, 0x0a, 0x02, 0x0c, 0x04, 0x08, 0x00,
43
0x1f, 0x0f, 0x17, 0x07, 0x1b, 0x0b, 0x13, 0x03,
44
0x1d, 0x0d, 0x15, 0x05, 0x19, 0x09, 0x11, 0x01,
45
0x1e, 0x0e, 0x16, 0x06, 0x1a, 0x0a, 0x12, 0x02,
46
0x1c, 0x0c, 0x14, 0x04, 0x18, 0x08, 0x10, 0x00,
47
0x3f, 0x1f, 0x2f, 0x0f, 0x37, 0x17, 0x27, 0x07,
48
0x3b, 0x1b, 0x2b, 0x0b, 0x33, 0x13, 0x23, 0x03,
49
0x3d, 0x1d, 0x2d, 0x0d, 0x35, 0x15, 0x25, 0x05,
50
0x39, 0x19, 0x29, 0x09, 0x31, 0x11, 0x21, 0x01,
51
0x3e, 0x1e, 0x2e, 0x0e, 0x36, 0x16, 0x26, 0x06,
52
0x3a, 0x1a, 0x2a, 0x0a, 0x32, 0x12, 0x22, 0x02,
53
0x3c, 0x1c, 0x2c, 0x0c, 0x34, 0x14, 0x24, 0x04,
54
0x38, 0x18, 0x28, 0x08, 0x30, 0x10, 0x20, 0x00,
55
0x7f, 0x3f, 0x5f, 0x1f, 0x6f, 0x2f, 0x4f, 0x0f,
56
0x77, 0x37, 0x57, 0x17, 0x67, 0x27, 0x47, 0x07,
57
0x7b, 0x3b, 0x5b, 0x1b, 0x6b, 0x2b, 0x4b, 0x0b,
58
0x73, 0x33, 0x53, 0x13, 0x63, 0x23, 0x43, 0x03,
59
0x7d, 0x3d, 0x5d, 0x1d, 0x6d, 0x2d, 0x4d, 0x0d,
60
0x75, 0x35, 0x55, 0x15, 0x65, 0x25, 0x45, 0x05,
61
0x79, 0x39, 0x59, 0x19, 0x69, 0x29, 0x49, 0x09,
62
0x71, 0x31, 0x51, 0x11, 0x61, 0x21, 0x41, 0x01,
63
0x7e, 0x3e, 0x5e, 0x1e, 0x6e, 0x2e, 0x4e, 0x0e,
64
0x76, 0x36, 0x56, 0x16, 0x66, 0x26, 0x46, 0x06,
65
0x7a, 0x3a, 0x5a, 0x1a, 0x6a, 0x2a, 0x4a, 0x0a,
66
0x72, 0x32, 0x52, 0x12, 0x62, 0x22, 0x42, 0x02,
67
0x7c, 0x3c, 0x5c, 0x1c, 0x6c, 0x2c, 0x4c, 0x0c,
68
0x74, 0x34, 0x54, 0x14, 0x64, 0x24, 0x44, 0x04,
69
0x78, 0x38, 0x58, 0x18, 0x68, 0x28, 0x48, 0x08,
70
0x70, 0x30, 0x50, 0x10, 0x60, 0x20, 0x40, 0x00,
71
};
72
73
void SDD1::Decomp::GCD::get_run_count(uint8 code_number, uint8 &mps_count, bool &lps_index) {
74
uint8 codeword = self.im.get_codeword(code_number);
75
76
if(codeword & 0x80) {
77
lps_index = 1;
78
mps_count = run_count[codeword >> (code_number ^ 0x07)];
79
} else {
80
mps_count = 1 << code_number;
81
}
82
}
83
84
//bits generator
85
86
void SDD1::Decomp::BG::init() {
87
mps_count = 0;
88
lps_index = 0;
89
}
90
91
uint8 SDD1::Decomp::BG::get_bit(bool &end_of_run) {
92
if(!(mps_count || lps_index)) self.gcd.get_run_count(code_number, mps_count, lps_index);
93
94
uint8 bit;
95
if(mps_count) {
96
bit = 0;
97
mps_count--;
98
} else {
99
bit = 1;
100
lps_index = 0;
101
}
102
103
end_of_run = !(mps_count || lps_index);
104
return bit;
105
}
106
107
//probability estimation module
108
109
const SDD1::Decomp::PEM::State SDD1::Decomp::PEM::evolution_table[33] = {
110
{ 0, 25, 25 },
111
{ 0, 2, 1 },
112
{ 0, 3, 1 },
113
{ 0, 4, 2 },
114
{ 0, 5, 3 },
115
{ 1, 6, 4 },
116
{ 1, 7, 5 },
117
{ 1, 8, 6 },
118
{ 1, 9, 7 },
119
{ 2, 10, 8 },
120
{ 2, 11, 9 },
121
{ 2, 12, 10 },
122
{ 2, 13, 11 },
123
{ 3, 14, 12 },
124
{ 3, 15, 13 },
125
{ 3, 16, 14 },
126
{ 3, 17, 15 },
127
{ 4, 18, 16 },
128
{ 4, 19, 17 },
129
{ 5, 20, 18 },
130
{ 5, 21, 19 },
131
{ 6, 22, 20 },
132
{ 6, 23, 21 },
133
{ 7, 24, 22 },
134
{ 7, 24, 23 },
135
{ 0, 26, 1 },
136
{ 1, 27, 2 },
137
{ 2, 28, 4 },
138
{ 3, 29, 8 },
139
{ 4, 30, 12 },
140
{ 5, 31, 16 },
141
{ 6, 32, 18 },
142
{ 7, 24, 22 },
143
};
144
145
void SDD1::Decomp::PEM::init() {
146
for(unsigned i = 0; i < 32; i++) {
147
context_info[i].status = 0;
148
context_info[i].mps = 0;
149
}
150
}
151
152
uint8 SDD1::Decomp::PEM::get_bit(uint8 context) {
153
ContextInfo &info = context_info[context];
154
uint8 current_status = info.status;
155
uint8 current_mps = info.mps;
156
const State &s = SDD1::Decomp::PEM::evolution_table[current_status];
157
158
uint8 bit;
159
bool end_of_run;
160
switch(s.code_number) {
161
case 0: bit = self.bg0.get_bit(end_of_run); break;
162
case 1: bit = self.bg1.get_bit(end_of_run); break;
163
case 2: bit = self.bg2.get_bit(end_of_run); break;
164
case 3: bit = self.bg3.get_bit(end_of_run); break;
165
case 4: bit = self.bg4.get_bit(end_of_run); break;
166
case 5: bit = self.bg5.get_bit(end_of_run); break;
167
case 6: bit = self.bg6.get_bit(end_of_run); break;
168
case 7: bit = self.bg7.get_bit(end_of_run); break;
169
}
170
171
if(end_of_run) {
172
if(bit) {
173
if(!(current_status & 0xfe)) info.mps ^= 0x01;
174
info.status = s.next_if_lps;
175
} else {
176
info.status = s.next_if_mps;
177
}
178
}
179
180
return bit ^ current_mps;
181
}
182
183
//context model
184
185
void SDD1::Decomp::CM::init(unsigned offset) {
186
bitplanes_info = self.rom_read(offset) & 0xc0;
187
context_bits_info = self.rom_read(offset) & 0x30;
188
bit_number = 0;
189
for(unsigned i = 0; i < 8; i++) previous_bitplane_bits[i] = 0;
190
switch(bitplanes_info) {
191
case 0x00: current_bitplane = 1; break;
192
case 0x40: current_bitplane = 7; break;
193
case 0x80: current_bitplane = 3; break;
194
}
195
}
196
197
uint8 SDD1::Decomp::CM::get_bit() {
198
switch(bitplanes_info) {
199
case 0x00:
200
current_bitplane ^= 0x01;
201
break;
202
case 0x40:
203
current_bitplane ^= 0x01;
204
if(!(bit_number & 0x7f)) current_bitplane = ((current_bitplane + 2) & 0x07);
205
break;
206
case 0x80:
207
current_bitplane ^= 0x01;
208
if(!(bit_number & 0x7f)) current_bitplane ^= 0x02;
209
break;
210
case 0xc0:
211
current_bitplane = bit_number & 0x07;
212
break;
213
}
214
215
uint16 &context_bits = previous_bitplane_bits[current_bitplane];
216
uint8 current_context = (current_bitplane & 0x01) << 4;
217
switch(context_bits_info) {
218
case 0x00: current_context |= ((context_bits & 0x01c0) >> 5) | (context_bits & 0x0001); break;
219
case 0x10: current_context |= ((context_bits & 0x0180) >> 5) | (context_bits & 0x0001); break;
220
case 0x20: current_context |= ((context_bits & 0x00c0) >> 5) | (context_bits & 0x0001); break;
221
case 0x30: current_context |= ((context_bits & 0x0180) >> 5) | (context_bits & 0x0003); break;
222
}
223
224
uint8 bit = self.pem.get_bit(current_context);
225
context_bits <<= 1;
226
context_bits |= bit;
227
bit_number++;
228
return bit;
229
}
230
231
//output logic
232
233
void SDD1::Decomp::OL::init(unsigned offset) {
234
bitplanes_info = self.rom_read(offset) & 0xc0;
235
r0 = 0x01;
236
}
237
238
uint8 SDD1::Decomp::OL::decompress() {
239
switch(bitplanes_info) {
240
case 0x00: case 0x40: case 0x80:
241
if(r0 == 0) {
242
r0 = ~r0;
243
return r2;
244
}
245
for(r0 = 0x80, r1 = 0, r2 = 0; r0; r0 >>= 1) {
246
if(self.cm.get_bit()) r1 |= r0;
247
if(self.cm.get_bit()) r2 |= r0;
248
}
249
return r1;
250
case 0xc0:
251
for(r0 = 0x01, r1 = 0; r0; r0 <<= 1) {
252
if(self.cm.get_bit()) r1 |= r0;
253
}
254
return r1;
255
}
256
}
257
258
//core
259
260
void SDD1::Decomp::init(unsigned offset) {
261
im.init(offset);
262
bg0.init();
263
bg1.init();
264
bg2.init();
265
bg3.init();
266
bg4.init();
267
bg5.init();
268
bg6.init();
269
bg7.init();
270
pem.init();
271
cm.init(offset);
272
ol.init(offset);
273
}
274
275
uint8 SDD1::Decomp::read() {
276
return ol.decompress();
277
}
278
279
uint8 SDD1::Decomp::rom_read(unsigned offset) {
280
return sdd1.rom_read(offset);
281
}
282
283
SDD1::Decomp::Decomp() : im(*this), gcd(*this),
284
bg0(*this, 0), bg1(*this, 1), bg2(*this, 2), bg3(*this, 3),
285
bg4(*this, 4), bg5(*this, 5), bg6(*this, 6), bg7(*this, 7),
286
pem(*this), cm(*this), ol(*this) {
287
}
288
289