Compare commits

...

1 Commits

Author SHA1 Message Date
David Voswinkel
ac85195bb8 add huffman encoding ontop of the rle 2009-08-03 22:18:34 +02:00
4 changed files with 77 additions and 32 deletions

View File

@ -18,12 +18,12 @@
*/ */
#include "huffman-decode.h"
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "huffman-decode.h"
#include "info.h" #include "info.h"
#include "debug.h" #include "debug.h"
@ -44,7 +44,7 @@
#define ALLOC_ERROR {} #define ALLOC_ERROR {}
#undef BLOCK_ALLOC 1 #undef BLOCK_ALLOC
typedef struct { typedef struct {
int16_t value; int16_t value;
@ -125,9 +125,9 @@ uint8_t build_tree(huffman_dec_ctx_t* ctx){
if(treesize>0x1ff) if(treesize>0x1ff)
return 2; return 2;
#if BLOCK_ALLOC #if BLOCK_ALLOC
ctx->tree = calloc(2*treesize-1, sizeof(node_t)); ctx->tree = malloc((2*treesize-1) * sizeof(node_t));
#else #else
ctx->tree = calloc(1, sizeof(node_t)); ctx->tree = malloc(sizeof(node_t));
#endif #endif
((node_t*)(ctx->tree))->value = V_NODE; ((node_t*)(ctx->tree))->value = V_NODE;
uint16_t depth=0; uint16_t depth=0;
@ -166,7 +166,7 @@ uint8_t build_tree(huffman_dec_ctx_t* ctx){
#if BLOCK_ALLOC #if BLOCK_ALLOC
current->left=&(((node_t*)(ctx->tree))[treeindex++]); current->left=&(((node_t*)(ctx->tree))[treeindex++]);
#else #else
current->left=calloc(1, sizeof(node_t)); current->left=malloc(sizeof(node_t));
#endif #endif
((node_t*)(current->left))->value = V_NODE; ((node_t*)(current->left))->value = V_NODE;
} }
@ -179,7 +179,7 @@ uint8_t build_tree(huffman_dec_ctx_t* ctx){
#if BLOCK_ALLOC #if BLOCK_ALLOC
current->right=&(((node_t*)(ctx->tree))[treeindex++]); current->right=&(((node_t*)(ctx->tree))[treeindex++]);
#else #else
current->right=calloc(1, sizeof(node_t)); current->right=malloc( sizeof(node_t));
#endif #endif
((node_t*)(current->right))->value=V_NODE; ((node_t*)(current->right))->value=V_NODE;
} }

View File

@ -370,18 +370,17 @@ uint16_t read_byte_pgm(uint16_t addr){
return pgm_read_byte((PGM_VOID_P)addr); return pgm_read_byte((PGM_VOID_P)addr);
} }
uint16_t read_byte_ee(uint16_t addr){ void decompress_huffman(PGM_VOID_P addr, uint16_t(*fp)(uint16_t)){
return eeprom_read_byte((uint8_t*)addr);
}
void decompress(PGM_VOID_P addr, uint16_t(*fp)(uint16_t)){
uint16_t c; uint16_t c;
uint32_t i = 0; uint32_t i = 0;
huffman_dec_ctx_t ctx; huffman_dec_ctx_t ctx;
info("ok1\n");
huffman_dec_init(&ctx, fp); huffman_dec_init(&ctx, fp);
info("ok2\n");
huffman_dec_set_addr(&ctx, (uint16_t)addr); huffman_dec_set_addr(&ctx, (uint16_t)addr);
info("ok3\n");
while(1){ while(1){
info("ok4\n");
i++; i++;
c=huffman_dec_byte(&ctx); c=huffman_dec_byte(&ctx);
if (i%1024==0) if (i%1024==0)
@ -394,15 +393,6 @@ void decompress(PGM_VOID_P addr, uint16_t(*fp)(uint16_t)){
} }
} }
void decompress_huffman(void){
info("Decompress Rom %p to 0x000000\n",(void*)_rom);
sram_bulk_write_start(0x000000);
decompress(&_rom,read_byte_pgm);
sram_bulk_write_end();
info("Done\n");
}
void send_reset(){ void send_reset(){
info("Reset Snes\n"); info("Reset Snes\n");
@ -464,18 +454,13 @@ void boot_startup_rom(){
snes_lorom(); snes_lorom();
info("Set Snes lowrom \n"); info("Set Snes lowrom \n");
/*
info("Set Snes hirom\n");
snes_hirom();
info("Disable snes WR\n"); info("Huffman decompress to 0x010000\n",(void*)_rom);
snes_wr_disable(); sram_bulk_write_start(0x010000);
decompress_huffman(&_rom,read_byte_pgm);
info("IRQ off\n"); sram_bulk_write_end();
snes_irq_lo(); info("RLE decompress to 0x000000\n",(void*)_rom);
snes_irq_off(); rle_decode_sram(0x010000, ROM_RLE_SIZE, 0x000000);
*/
rle_decode(&_rom, ROM_SIZE, 0x000000);
dump_memory(0x10000 - 0x100, 0x10000); dump_memory(0x10000 - 0x100, 0x10000);
snes_reset_hi(); snes_reset_hi();

View File

@ -96,3 +96,62 @@ uint8_t rle_decode(PGM_VOID_P in_addr, int32_t in_len, uint32_t out_addr)
return 0; return 0;
} }
uint8_t rle_decode_sram(uint32_t in_addr, int32_t in_len, uint32_t out_addr)
{
uint8_t in_byte, in_repeat, last_byte;
uint32_t out_len, out_len_left;
info("RLE decode len=%li addr=0x%08lx\n",in_len,out_addr);
last_byte = 0;
out_len_left = out_len;
#define INBYTE(b) \
do { \
if ( --in_len < 0 ) { \
return 1; \
} \
b = sram_read(in_addr);\
in_addr++;\
} while(0)
#define OUTBYTE(b) \
do { \
sram_write(out_addr,b);\
out_addr++;\
} while(0)
INBYTE(in_byte);
if (in_byte == RUNCHAR) {
INBYTE(in_repeat);
if (in_repeat != 0) {
info("Orphaned RLE code at start\n");
return 1;
}
OUTBYTE(RUNCHAR);
} else {
OUTBYTE(in_byte);
}
while( in_len > 0 ) {
INBYTE(in_byte);
if (in_len%1024==0)
info(".");
if (in_byte == RUNCHAR) {
INBYTE(in_repeat);
if ( in_repeat == 0 ) {
/* Just an escaped RUNCHAR value */
OUTBYTE(RUNCHAR);
} else {
/* Pick up value and output a sequence of it */
in_byte = last_byte; //;out_data[-1];
while ( --in_repeat > 0 )
OUTBYTE(in_byte);
}
} else {
/* Normal byte */
OUTBYTE(in_byte);
}
last_byte = in_byte;
}
return 0;
}

View File

@ -24,5 +24,6 @@
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
uint8_t rle_decode(PGM_VOID_P in_addr,uint32_t in_len, uint32_t out_addr); uint8_t rle_decode(PGM_VOID_P in_addr,uint32_t in_len, uint32_t out_addr);
uint8_t rle_decode_sram(uint32_t in_addr, int32_t in_len, uint32_t out_addr);
#endif #endif