Path: blob/master/libsnes/bsnes/snes/chip/sdd1/decomp.cpp
2 views
//S-DD1 decompression algorithm implementation1//original code written by Andreas Naive (public domain license)2//bsnes port written by byuu34//note: decompression module does not need to be serialized with bsnes5//this is because decompression only runs during DMA, and bsnes will complete6//any pending DMA transfers prior to serialization.78//input manager910void SDD1::Decomp::IM::init(unsigned offset_) {11offset = offset_;12bit_count = 4;13}1415uint8 SDD1::Decomp::IM::get_codeword(uint8 code_length) {16uint8 codeword;17uint8 comp_count;1819codeword = self.rom_read(offset) << bit_count;20bit_count++;2122if(codeword & 0x80) {23codeword |= self.rom_read(offset + 1) >> (9 - bit_count);24bit_count += code_length;25}2627if(bit_count & 0x08) {28offset++;29bit_count &= 0x07;30}3132return codeword;33}3435//golomb-code decoder3637const uint8 SDD1::Decomp::GCD::run_count[] = {380x00, 0x00, 0x01, 0x00, 0x03, 0x01, 0x02, 0x00,390x07, 0x03, 0x05, 0x01, 0x06, 0x02, 0x04, 0x00,400x0f, 0x07, 0x0b, 0x03, 0x0d, 0x05, 0x09, 0x01,410x0e, 0x06, 0x0a, 0x02, 0x0c, 0x04, 0x08, 0x00,420x1f, 0x0f, 0x17, 0x07, 0x1b, 0x0b, 0x13, 0x03,430x1d, 0x0d, 0x15, 0x05, 0x19, 0x09, 0x11, 0x01,440x1e, 0x0e, 0x16, 0x06, 0x1a, 0x0a, 0x12, 0x02,450x1c, 0x0c, 0x14, 0x04, 0x18, 0x08, 0x10, 0x00,460x3f, 0x1f, 0x2f, 0x0f, 0x37, 0x17, 0x27, 0x07,470x3b, 0x1b, 0x2b, 0x0b, 0x33, 0x13, 0x23, 0x03,480x3d, 0x1d, 0x2d, 0x0d, 0x35, 0x15, 0x25, 0x05,490x39, 0x19, 0x29, 0x09, 0x31, 0x11, 0x21, 0x01,500x3e, 0x1e, 0x2e, 0x0e, 0x36, 0x16, 0x26, 0x06,510x3a, 0x1a, 0x2a, 0x0a, 0x32, 0x12, 0x22, 0x02,520x3c, 0x1c, 0x2c, 0x0c, 0x34, 0x14, 0x24, 0x04,530x38, 0x18, 0x28, 0x08, 0x30, 0x10, 0x20, 0x00,540x7f, 0x3f, 0x5f, 0x1f, 0x6f, 0x2f, 0x4f, 0x0f,550x77, 0x37, 0x57, 0x17, 0x67, 0x27, 0x47, 0x07,560x7b, 0x3b, 0x5b, 0x1b, 0x6b, 0x2b, 0x4b, 0x0b,570x73, 0x33, 0x53, 0x13, 0x63, 0x23, 0x43, 0x03,580x7d, 0x3d, 0x5d, 0x1d, 0x6d, 0x2d, 0x4d, 0x0d,590x75, 0x35, 0x55, 0x15, 0x65, 0x25, 0x45, 0x05,600x79, 0x39, 0x59, 0x19, 0x69, 0x29, 0x49, 0x09,610x71, 0x31, 0x51, 0x11, 0x61, 0x21, 0x41, 0x01,620x7e, 0x3e, 0x5e, 0x1e, 0x6e, 0x2e, 0x4e, 0x0e,630x76, 0x36, 0x56, 0x16, 0x66, 0x26, 0x46, 0x06,640x7a, 0x3a, 0x5a, 0x1a, 0x6a, 0x2a, 0x4a, 0x0a,650x72, 0x32, 0x52, 0x12, 0x62, 0x22, 0x42, 0x02,660x7c, 0x3c, 0x5c, 0x1c, 0x6c, 0x2c, 0x4c, 0x0c,670x74, 0x34, 0x54, 0x14, 0x64, 0x24, 0x44, 0x04,680x78, 0x38, 0x58, 0x18, 0x68, 0x28, 0x48, 0x08,690x70, 0x30, 0x50, 0x10, 0x60, 0x20, 0x40, 0x00,70};7172void SDD1::Decomp::GCD::get_run_count(uint8 code_number, uint8 &mps_count, bool &lps_index) {73uint8 codeword = self.im.get_codeword(code_number);7475if(codeword & 0x80) {76lps_index = 1;77mps_count = run_count[codeword >> (code_number ^ 0x07)];78} else {79mps_count = 1 << code_number;80}81}8283//bits generator8485void SDD1::Decomp::BG::init() {86mps_count = 0;87lps_index = 0;88}8990uint8 SDD1::Decomp::BG::get_bit(bool &end_of_run) {91if(!(mps_count || lps_index)) self.gcd.get_run_count(code_number, mps_count, lps_index);9293uint8 bit;94if(mps_count) {95bit = 0;96mps_count--;97} else {98bit = 1;99lps_index = 0;100}101102end_of_run = !(mps_count || lps_index);103return bit;104}105106//probability estimation module107108const SDD1::Decomp::PEM::State SDD1::Decomp::PEM::evolution_table[33] = {109{ 0, 25, 25 },110{ 0, 2, 1 },111{ 0, 3, 1 },112{ 0, 4, 2 },113{ 0, 5, 3 },114{ 1, 6, 4 },115{ 1, 7, 5 },116{ 1, 8, 6 },117{ 1, 9, 7 },118{ 2, 10, 8 },119{ 2, 11, 9 },120{ 2, 12, 10 },121{ 2, 13, 11 },122{ 3, 14, 12 },123{ 3, 15, 13 },124{ 3, 16, 14 },125{ 3, 17, 15 },126{ 4, 18, 16 },127{ 4, 19, 17 },128{ 5, 20, 18 },129{ 5, 21, 19 },130{ 6, 22, 20 },131{ 6, 23, 21 },132{ 7, 24, 22 },133{ 7, 24, 23 },134{ 0, 26, 1 },135{ 1, 27, 2 },136{ 2, 28, 4 },137{ 3, 29, 8 },138{ 4, 30, 12 },139{ 5, 31, 16 },140{ 6, 32, 18 },141{ 7, 24, 22 },142};143144void SDD1::Decomp::PEM::init() {145for(unsigned i = 0; i < 32; i++) {146context_info[i].status = 0;147context_info[i].mps = 0;148}149}150151uint8 SDD1::Decomp::PEM::get_bit(uint8 context) {152ContextInfo &info = context_info[context];153uint8 current_status = info.status;154uint8 current_mps = info.mps;155const State &s = SDD1::Decomp::PEM::evolution_table[current_status];156157uint8 bit;158bool end_of_run;159switch(s.code_number) {160case 0: bit = self.bg0.get_bit(end_of_run); break;161case 1: bit = self.bg1.get_bit(end_of_run); break;162case 2: bit = self.bg2.get_bit(end_of_run); break;163case 3: bit = self.bg3.get_bit(end_of_run); break;164case 4: bit = self.bg4.get_bit(end_of_run); break;165case 5: bit = self.bg5.get_bit(end_of_run); break;166case 6: bit = self.bg6.get_bit(end_of_run); break;167case 7: bit = self.bg7.get_bit(end_of_run); break;168}169170if(end_of_run) {171if(bit) {172if(!(current_status & 0xfe)) info.mps ^= 0x01;173info.status = s.next_if_lps;174} else {175info.status = s.next_if_mps;176}177}178179return bit ^ current_mps;180}181182//context model183184void SDD1::Decomp::CM::init(unsigned offset) {185bitplanes_info = self.rom_read(offset) & 0xc0;186context_bits_info = self.rom_read(offset) & 0x30;187bit_number = 0;188for(unsigned i = 0; i < 8; i++) previous_bitplane_bits[i] = 0;189switch(bitplanes_info) {190case 0x00: current_bitplane = 1; break;191case 0x40: current_bitplane = 7; break;192case 0x80: current_bitplane = 3; break;193}194}195196uint8 SDD1::Decomp::CM::get_bit() {197switch(bitplanes_info) {198case 0x00:199current_bitplane ^= 0x01;200break;201case 0x40:202current_bitplane ^= 0x01;203if(!(bit_number & 0x7f)) current_bitplane = ((current_bitplane + 2) & 0x07);204break;205case 0x80:206current_bitplane ^= 0x01;207if(!(bit_number & 0x7f)) current_bitplane ^= 0x02;208break;209case 0xc0:210current_bitplane = bit_number & 0x07;211break;212}213214uint16 &context_bits = previous_bitplane_bits[current_bitplane];215uint8 current_context = (current_bitplane & 0x01) << 4;216switch(context_bits_info) {217case 0x00: current_context |= ((context_bits & 0x01c0) >> 5) | (context_bits & 0x0001); break;218case 0x10: current_context |= ((context_bits & 0x0180) >> 5) | (context_bits & 0x0001); break;219case 0x20: current_context |= ((context_bits & 0x00c0) >> 5) | (context_bits & 0x0001); break;220case 0x30: current_context |= ((context_bits & 0x0180) >> 5) | (context_bits & 0x0003); break;221}222223uint8 bit = self.pem.get_bit(current_context);224context_bits <<= 1;225context_bits |= bit;226bit_number++;227return bit;228}229230//output logic231232void SDD1::Decomp::OL::init(unsigned offset) {233bitplanes_info = self.rom_read(offset) & 0xc0;234r0 = 0x01;235}236237uint8 SDD1::Decomp::OL::decompress() {238switch(bitplanes_info) {239case 0x00: case 0x40: case 0x80:240if(r0 == 0) {241r0 = ~r0;242return r2;243}244for(r0 = 0x80, r1 = 0, r2 = 0; r0; r0 >>= 1) {245if(self.cm.get_bit()) r1 |= r0;246if(self.cm.get_bit()) r2 |= r0;247}248return r1;249case 0xc0:250for(r0 = 0x01, r1 = 0; r0; r0 <<= 1) {251if(self.cm.get_bit()) r1 |= r0;252}253return r1;254}255}256257//core258259void SDD1::Decomp::init(unsigned offset) {260im.init(offset);261bg0.init();262bg1.init();263bg2.init();264bg3.init();265bg4.init();266bg5.init();267bg6.init();268bg7.init();269pem.init();270cm.init(offset);271ol.init(offset);272}273274uint8 SDD1::Decomp::read() {275return ol.decompress();276}277278uint8 SDD1::Decomp::rom_read(unsigned offset) {279return sdd1.rom_read(offset);280}281282SDD1::Decomp::Decomp() : im(*this), gcd(*this),283bg0(*this, 0), bg1(*this, 1), bg2(*this, 2), bg3(*this, 3),284bg4(*this, 4), bg5(*this, 5), bg6(*this, 6), bg7(*this, 7),285pem(*this), cm(*this), ol(*this) {286}287288289