1 /*******************************************************************************
2 Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
4 (c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com) and
5 Jerremy Koot (jkoot@snes9x.com)
7 (c) Copyright 2001 - 2004 John Weidman (jweidman@slip.net)
9 (c) Copyright 2002 - 2004 Brad Jorsch (anomie@users.sourceforge.net),
10 funkyass (funkyass@spam.shaw.ca),
11 Joel Yliluoma (http://iki.fi/bisqwit/)
12 Kris Bleakley (codeviolation@hotmail.com),
14 Nach (n-a-c-h@users.sourceforge.net),
15 Peter Bortas (peter@bortas.org) and
16 zones (kasumitokoduck@yahoo.com)
18 C4 x86 assembler and some C emulation code
19 (c) Copyright 2000 - 2003 zsKnight (zsknight@zsnes.com),
20 _Demo_ (_demo_@zsnes.com), and Nach
23 (c) Copyright 2003 Brad Jorsch
26 (c) Copyright 1998 - 2004 Ivar (ivar@snes9x.com), _Demo_, Gary Henderson,
27 John Weidman, neviksti (neviksti@hotmail.com),
28 Kris Bleakley, Andreas Naive
31 (c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and
32 Lord Nightmare (lord_nightmare@users.sourceforge.net
35 (c) Copyright 2001 - 2004 zsKnight, pagefault (pagefault@zsnes.com) and
37 Ported from x86 assembler to C by sanmaiwashi
39 SPC7110 and RTC C++ emulator code
40 (c) Copyright 2002 Matthew Kendora with research by
41 zsKnight, John Weidman, and Dark Force
44 (c) Copyright 2003 Brad Jorsch with research by
45 Andreas Naive and John Weidman
48 (c) Copyright 2001 John Weidman
50 ST010 C++ emulator code
51 (c) Copyright 2003 Feather, Kris Bleakley, John Weidman and Matthew Kendora
53 Super FX x86 assembler emulator code
54 (c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault
56 Super FX C emulator code
57 (c) Copyright 1997 - 1999 Ivar, Gary Henderson and John Weidman
60 SH assembler code partly based on x86 assembler code
61 (c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se)
64 Specific ports contains the works of other authors. See headers in
67 Snes9x homepage: http://www.snes9x.com
69 Permission to use, copy, modify and distribute Snes9x in both binary and
70 source form, for non-commercial purposes, is hereby granted without fee,
71 providing that this license information and copyright notice appear with
72 all copies and any derived work.
74 This software is provided 'as-is', without any express or implied
75 warranty. In no event shall the authors be held liable for any damages
76 arising from the use of this software.
78 Snes9x is freeware for PERSONAL USE only. Commercial users should
79 seek permission of the copyright holders first. Commercial use includes
80 charging money for Snes9x or software derived from Snes9x.
82 The copyright holders request that bug fixes and improvements to the code
83 should be forwarded to them so everyone can benefit from the modifications
86 Super NES and Super Nintendo Entertainment System are trademarks of
87 Nintendo Co., Limited and its subsidiary companies.
88 *******************************************************************************/
91 * Based on code and documentation by Andreas Naive, who deserves a great deal
92 * of thanks and credit for figuring this out.
95 * The author is greatly indebted with The Dumper, without whose help and
96 * patience providing him with real S-DD1 data the research had never been
97 * possible. He also wish to note that in the very beggining of his research,
98 * Neviksti had done some steps in the right direction. By last, the author is
99 * indirectly indebted to all the people that worked and contributed in the
100 * S-DD1 issue in the past.
107 static int valid_bits;
108 static uint16 in_stream;
109 static uint8 *in_buf;
110 static uint8 bit_ctr[8];
111 static uint8 context_states[32];
112 static int context_MPS[32];
113 static int bitplane_type;
114 static int high_context_bits;
115 static int low_context_bits;
116 static int prev_bits[8];
122 } evolution_table[] = {
158 static uint8 run_table[128] = {
159 128, 64, 96, 32, 112, 48, 80, 16, 120, 56, 88, 24, 104, 40, 72,
160 8, 124, 60, 92, 28, 108, 44, 76, 12, 116, 52, 84, 20, 100, 36,
161 68, 4, 126, 62, 94, 30, 110, 46, 78, 14, 118, 54, 86, 22, 102,
162 38, 70, 6, 122, 58, 90, 26, 106, 42, 74, 10, 114, 50, 82, 18,
163 98, 34, 66, 2, 127, 63, 95, 31, 111, 47, 79, 15, 119, 55, 87,
164 23, 103, 39, 71, 7, 123, 59, 91, 27, 107, 43, 75, 11, 115, 51,
165 83, 19, 99, 35, 67, 3, 125, 61, 93, 29, 109, 45, 77, 13, 117,
166 53, 85, 21, 101, 37, 69, 5, 121, 57, 89, 25, 105, 41, 73, 9,
167 113, 49, 81, 17, 97, 33, 65, 1
170 static inline uint8 GetCodeword(int bits){
174 in_stream|=*(in_buf++);
180 if(in_stream&0x8000) return 0x80+(1<<bits);
181 tmp=(in_stream>>8) | (0x7f>>bits);
185 in_stream |= (*(in_buf++))<<(-valid_bits);
188 return run_table[tmp];
191 static inline uint8 GolombGetBit(int code_size){
192 if(!bit_ctr[code_size]) bit_ctr[code_size]=GetCodeword(code_size);
193 bit_ctr[code_size]--;
194 if(bit_ctr[code_size]==0x80){
195 bit_ctr[code_size]=0;
196 return 2; /* secret code for 'last zero'. ones are always last. */
198 return (bit_ctr[code_size]==0)?1:0;
201 static inline uint8 ProbGetBit(uint8 context){
202 uint8 state=context_states[context];
203 uint8 bit=GolombGetBit(evolution_table[state].code_size);
206 context_states[context]=evolution_table[state].LPS_next;
208 context_MPS[context]^=1;
209 return context_MPS[context]; /* just inverted, so just return it */
211 return context_MPS[context]^1; /* we know bit is 1, so use a constant */
214 context_states[context]=evolution_table[state].MPS_next;
215 /* zero here, zero there, no difference so drop through. */
217 return context_MPS[context]; /* we know bit is 0, so don't bother xoring */
220 static inline uint8 GetBit(uint8 cur_bitplane){
223 bit=ProbGetBit(((cur_bitplane&1)<<4)
224 | ((prev_bits[cur_bitplane]&high_context_bits)>>5)
225 | (prev_bits[cur_bitplane]&low_context_bits));
227 prev_bits[cur_bitplane] <<= 1;
228 prev_bits[cur_bitplane] |= bit;
232 void SDD1_decompress(uint8 *out, uint8 *in, int len){
236 if(len==0) len=0x10000;
238 bitplane_type=in[0]>>6;
242 high_context_bits=0x01c0;
243 low_context_bits =0x0001;
246 high_context_bits=0x0180;
247 low_context_bits =0x0001;
250 high_context_bits=0x00c0;
251 low_context_bits =0x0001;
254 high_context_bits=0x0180;
255 low_context_bits =0x0003;
259 in_stream=(in[0]<<11) | (in[1]<<3);
262 memset(bit_ctr, 0, sizeof(bit_ctr));
263 memset(context_states, 0, sizeof(context_states));
264 memset(context_MPS, 0, sizeof(context_MPS));
265 memset(prev_bits, 0, sizeof(prev_bits));
267 switch(bitplane_type){
270 for(byte1=byte2=0, bit=0x80; bit; bit>>=1){
271 if(GetBit(0)) byte1 |= bit;
272 if(GetBit(1)) byte2 |= bit;
283 for(byte1=byte2=0, bit=0x80; bit; bit>>=1){
284 if(GetBit(plane)) byte1 |= bit;
285 if(GetBit(plane+1)) byte2 |= bit;
291 if(!(i+=32)) plane = (plane+2)&7;
297 for(byte1=byte2=0, bit=0x80; bit; bit>>=1){
298 if(GetBit(plane)) byte1 |= bit;
299 if(GetBit(plane+1)) byte2 |= bit;
305 if(!(i+=32)) plane ^= 2;
310 for(byte1=plane=0, bit=1; bit; bit<<=1, plane++){
311 if(GetBit(plane)) byte1 |= bit;
319 static uint8 cur_plane;
320 static uint8 num_bits;
321 static uint8 next_byte;
323 void SDD1_init(uint8 *in){
324 bitplane_type=in[0]>>6;
328 high_context_bits=0x01c0;
329 low_context_bits =0x0001;
332 high_context_bits=0x0180;
333 low_context_bits =0x0001;
336 high_context_bits=0x00c0;
337 low_context_bits =0x0001;
340 high_context_bits=0x0180;
341 low_context_bits =0x0003;
345 in_stream=(in[0]<<11) | (in[1]<<3);
348 memset(bit_ctr, 0, sizeof(bit_ctr));
349 memset(context_states, 0, sizeof(context_states));
350 memset(context_MPS, 0, sizeof(context_MPS));
351 memset(prev_bits, 0, sizeof(prev_bits));
357 uint8 SDD1_get_byte(void){
361 switch(bitplane_type){
366 for(bit=0x80; bit; bit>>=1){
367 if(GetBit(0)) byte |= bit;
368 if(GetBit(1)) next_byte |= bit;
379 for(bit=0x80; bit; bit>>=1){
380 if(GetBit(cur_plane)) byte |= bit;
381 if(GetBit(cur_plane+1)) next_byte |= bit;
385 if(!num_bits) cur_plane = (cur_plane+2)&7;
393 for(bit=0x80; bit; bit>>=1){
394 if(GetBit(cur_plane)) byte |= bit;
395 if(GetBit(cur_plane+1)) next_byte |= bit;
399 if(!num_bits) cur_plane ^= 2;
404 for(cur_plane=0, bit=1; bit; bit<<=1, cur_plane++){
405 if(GetBit(cur_plane)) byte |= bit;
410 /* should never happen */