add rle encoded loader rom

This commit is contained in:
David Voswinkel 2009-07-26 12:39:08 +02:00
parent 8670300642
commit 6cab377087
7 changed files with 2450 additions and 60 deletions

View File

@ -7,7 +7,7 @@ AVRDUDE = avrdude -c usbasp -p $(DEVICE)
SIZE = avr-size
CFLAGS = -Iusbdrv -I. -DDEBUG_LEVEL=0
OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o usb_bulk.o uart.o fifo.o sram.o crc.o debug.o dump.o timer.o watchdog.o
OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o usb_bulk.o uart.o fifo.o sram.o crc.o debug.o dump.o timer.o watchdog.o huffman-decode.o rle.c loader.o
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE)

View File

@ -0,0 +1,263 @@
/* avr-huffman-decode.c */
/*
This file is part of the AVR-Huffman.
Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "huffman-decode.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef DEBUG
#undef DEBUG
#endif
#define DEBUG 1
#if DEBUG
#include <avr/pgmspace.h>
#endif
#define V_NODE (-2)
#define V_EOF (-1)
#define PREFIX_SIZE_B 32
#define ALLOC_ERROR {}
#undef BLOCK_ALLOC 1
typedef struct {
int16_t value;
void* left;
void* right;
} node_t;
#if HUFFMAN_USE_ADDR_16
void huffman_dec_init(huffman_dec_ctx_t* ctx, uint16_t(*rb_func)(uint16_t)){
#else
void huffman_dec_init(huffman_dec_ctx_t* ctx, uint16_t(*rb_func)(uint32_t)){
#endif
ctx->tree = NULL;
ctx->addr = 0;
ctx->read_byte = rb_func;
ctx->rbuffer_index = 8;
}
#if HUFFMAN_USE_ADDR_16
void huffman_dec_set_addr(huffman_dec_ctx_t* ctx, uint16_t addr){
#else
void huffman_dec_set_addr(huffman_dec_ctx_t* ctx, uint32_t addr){
#endif
ctx->addr = addr;
}
static inline void prefix_increment(uint8_t* prefix){
uint8_t i;
for(i=0; i<PREFIX_SIZE_B; ++i){
prefix[i] += 1;
if(prefix[i]!=0)
return;
}
}
static inline void prefix_shiftleft(uint8_t* prefix){
uint8_t i;
uint8_t c[2]={0,0};
uint8_t ci=0;
for(i=0; i<PREFIX_SIZE_B; ++i){
c[ci] = (prefix[i])>>7;
prefix[i]<<=1;
ci ^= 1;
prefix[i]|=c[ci];
}
}
static inline void set_last_to_eof(node_t* start){
node_t* current = start;
while(current->value==V_NODE){
current=current->right;
}
current->value=V_EOF;
}
#if DEBUG
void print_tree(node_t* node){
if(node->value==V_NODE){
printf("\n%p --> node->left=%p node->right=%p",node,node->left, node->right);
print_tree(node->left);
print_tree(node->right);
}else{
printf("\n%p => %i",node,node->value);
}
}
#endif
uint8_t build_tree(huffman_dec_ctx_t* ctx){
uint16_t treesize;
uint16_t treeindex=1;
int8_t i,t;
if(ctx->read_byte(ctx->addr++)!=0xC0)
return 1;
if(((treesize=ctx->read_byte(ctx->addr++))&0xFE)!=0xDE)
return 1;
treesize = (treesize&1)<<8;
treesize += ctx->read_byte(ctx->addr++);
if(treesize>0x1ff)
return 2;
#if BLOCK_ALLOC
ctx->tree = calloc(2*treesize-1, sizeof(node_t));
#else
ctx->tree = calloc(1, sizeof(node_t));
#endif
((node_t*)(ctx->tree))->value = V_NODE;
uint16_t depth=0;
uint16_t count=0;
uint16_t v;
uint8_t prefix[PREFIX_SIZE_B];
uint8_t cdepth=0;
node_t* current=ctx->tree;
current->value = V_NODE;
memset(prefix, 0, PREFIX_SIZE_B);
do{
while(count==0){
depth++;
count= ctx->read_byte(ctx->addr++);
if(count==255)
count += ctx->read_byte(ctx->addr++);
}
v = ctx->read_byte(ctx->addr++);
if(v>0xff)
return 3;
--count;
for(;cdepth<depth;++cdepth){
prefix_shiftleft(prefix);
}
#if DEBUG
printf("\n value %x => ",v);
#endif
current=ctx->tree;
for(i=depth-1; i>=0; --i){
t=(prefix[i/8])&(1<<(i%8));
if(t==0){
#if DEBUG
printf("0");
#endif
if(current->left==NULL){
#if BLOCK_ALLOC
current->left=&(((node_t*)(ctx->tree))[treeindex++]);
#else
current->left=calloc(1, sizeof(node_t));
#endif
((node_t*)(current->left))->value = V_NODE;
}
current = current->left;
} else {
#if DEBUG
printf("1");
#endif
if(current->right==NULL){
#if BLOCK_ALLOC
current->right=&(((node_t*)(ctx->tree))[treeindex++]);
#else
current->right=calloc(1, sizeof(node_t));
#endif
((node_t*)(current->right))->value=V_NODE;
}
current = current->right;
}
}
#if !BLOCK_ALLOC
if(current==NULL)
ALLOC_ERROR
#endif
current->value=v;
prefix_increment(prefix);
}while(!(prefix[depth/8]&(1<<(depth%8))));
#if DEBUG
print_tree(ctx->tree);
#endif
set_last_to_eof(ctx->tree);
return 0;
}
void free_tree(node_t* node){
#if !BLOCK_ALLOC
if(node->value==V_NODE){
free_tree(node->left);
free_tree(node->right);
}
#endif
free(node);
}
static uint8_t read_bit(huffman_dec_ctx_t* ctx){
uint16_t x;
uint8_t t;
if(ctx->rbuffer_index==8){
x=ctx->read_byte(ctx->addr);
ctx->addr++;
if(t>0xff)
return 0xFF;
ctx->rbuffer = (uint8_t)x;
ctx->rbuffer_index=0;
}
t=(ctx->rbuffer)>>7;
ctx->rbuffer<<=1;
ctx->rbuffer_index++;
return t;
}
uint16_t huffman_dec_byte(huffman_dec_ctx_t* ctx){
node_t* current=ctx->tree;
uint8_t t;
if(current==NULL){
#if DEBUG
printf("\nbuild tree");
#endif
t=build_tree(ctx);
if(t!=0){
#if DEBUG
printf("\n!!! building tree failed !!!\r\n");
#endif
return 0xFFFF;
}
#if DEBUG
printf("\ntree build successful");
#endif
current=ctx->tree;
}
while(current->value==V_NODE){
t=read_bit(ctx);
if(t==0xFF)
goto eof_detected;
if(t==0){
current=current->left;
} else {
current=current->right;
}
}
if(current->value!=V_EOF){
return current->value;
}
eof_detected:
free_tree(ctx->tree);
ctx->tree = NULL;
return 0xFFFF;
}

View File

@ -0,0 +1,50 @@
/* avr-huffman-decode.h */
/*
This file is part of the AVR-Huffman.
Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef AVR_HUFFMAN_DECODE_H_
#define AVR_HUFFMAN_DECODE_H_
#include <stdint.h>
#define HUFFMAN_USE_ADDR_16 1
typedef struct {
void* tree;
uint8_t rbuffer;
uint8_t rbuffer_index;
#if HUFFMAN_USE_ADDR_16
uint16_t(*read_byte)(uint16_t addr);
uint16_t addr;
#else
uint16_t(*read_byte)(uint32_t addr);
uint32_t addr;
#endif
} huffman_dec_ctx_t;
#if HUFFMAN_USE_ADDR_16
void huffman_dec_init(huffman_dec_ctx_t* ctx, uint16_t(*rb_func)(uint16_t));
void huffman_dec_set_addr(huffman_dec_ctx_t* ctx,uint16_t addr);
#else
void huffman_dec_init(huffman_dec_ctx_t* ctx, uint16_t(*rb_func)(uint32_t));
void huffman_dec_set_addr(huffman_dec_ctx_t* ctx,uint32_t addr);
#endif
uint16_t huffman_dec_byte(huffman_dec_ctx_t* ctx);
#endif /* AVR_HUFFMAN_DECODE_H_ */

1893
avr/usbload/loader.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -24,6 +24,7 @@
#include <util/delay.h> /* for _delay_ms() */
#include <stdlib.h>
#include <avr/pgmspace.h> /* required by usbdrv.h */
#include <avr/eeprom.h>
#include "usbdrv.h"
#include "oddebug.h" /* This is also an example for using debug
@ -38,7 +39,11 @@
#include "usb_bulk.h"
#include "timer.h"
#include "watchdog.h"
#include "huffman-decode.h"
#include "rle.h"
extern const char _rom[] PROGMEM;
extern FILE uart_stdout;
uint8_t debug_level = ( DEBUG | DEBUG_USB | DEBUG_CRC );
@ -338,17 +343,155 @@ void test_crc(){
test_non_zero_memory(0x000000,0x10000);
}
uint16_t read_byte_pgm(uint16_t addr){
return pgm_read_byte((PGM_VOID_P)addr);
}
uint16_t read_byte_ee(uint16_t addr){
return eeprom_read_byte((uint8_t*)addr);
}
void decompress(PGM_VOID_P addr, uint16_t(*fp)(uint16_t)){
uint16_t c;
uint32_t i = 0;
huffman_dec_ctx_t ctx;
huffman_dec_init(&ctx, fp);
huffman_dec_set_addr(&ctx, (uint16_t)addr);
while(1){
i++;
c=huffman_dec_byte(&ctx);
if (i%1024==0)
printf(".");
if(c>0xff){
return;
}
c&=0xff;
sram_bulk_write(c);
}
}
void decompress_huffman(void){
printf("Decompress Rom %p to 0x000000\n",(void*)_rom);
sram_bulk_write_start(0x000000);
decompress(&_rom,read_byte_pgm);
sram_bulk_write_end();
printf("Done\n");
}
void decompress_rle(void){
rle_decode(&_rom,30180,0x000000);
printf("Done\n");
}
void boot_startup_rom(){
uint8_t i = 0;
printf("Activate AVR bus\n");
avr_bus_active();
printf("IRQ off\n");
snes_irq_lo();
snes_irq_off();
printf("Set Snes hirom\n");
snes_hirom();
printf("Disable snes WR\n");
snes_wr_disable();
printf("IRQ off\n");
snes_irq_lo();
snes_irq_off();
decompress_rle();
//dump_memory(0x00000, 0x000100);
//dump_memory(0x10000 - 0x100, 0x10000);
decompress_rle();
dump_memory(0x00000, 0x000100);
dump_memory(0x10000 - 0x100, 0x10000);
//crc_check_bulk_memory(0x00000, 0x10000, 0x8000);
snes_bus_active();
printf("Activate Snes bus\n");
_delay_ms(100);
printf("Reset Snes\n");
snes_reset_on();
snes_reset_lo();
_delay_ms(2);
snes_reset_hi();
snes_reset_off();
i = 20;
printf("Wait");
while (--i){
_delay_ms(500);
printf(".");
}
printf("\n");
}
void send_reset(){
printf("Reset Snes\n");
snes_reset_on();
snes_reset_lo();
_delay_ms(2);
snes_reset_hi();
snes_reset_off();
}
void send_irq(){
snes_irq_on();
snes_irq_lo();
_delay_us(20);
snes_irq_hi();
snes_irq_off();
}
void set_rom_mode(){
if (req_bank_size == 0x8000){
snes_lorom();
printf("Set Snes lowrom \n");
} else {
snes_hirom();
printf("Set Snes hirom \n");
}
}
void usb_connect(){
uint8_t i = 0;
printf("USB init\n");
usbDeviceDisconnect(); /* enforce re-enumeration, do this while */
cli();
printf("USB disconnect\n");
i = 10;
while (--i) { /* fake USB disconnect for > 250 ms */
led_on();
_delay_ms(35);
led_off();
_delay_ms(65);
}
led_on();
usbDeviceConnect();
printf("USB connect\n");
}
int main(void)
{
uint8_t i;
uint8_t c;
uint16_t irq_count = 0;
uart_init();
stdout = &uart_stdout;
system_init();
printf("Sytem start\n");
system_init();
printf("Boot startup rom\n");
boot_startup_rom();
#if 0
wdt_init();
@ -363,98 +506,56 @@ int main(void)
#endif
usbInit();
printf("USB init\n");
usbDeviceDisconnect(); /* enforce re-enumeration, do this while
* interrupts are disabled! */
cli();
printf("USB disconnect\n");
i = 10;
while (--i) { /* fake USB disconnect for > 250 ms */
led_on();
_delay_ms(35);
led_off();
_delay_ms(65);
}
led_on();
usbDeviceConnect();
printf("USB connect\n");
usb_connect();
while (1){
avr_bus_active();
printf("Activate AVR bus\n");
printf("IRQ off\n");
snes_irq_lo();
snes_irq_off();
printf("Set Snes lowrom\n");
snes_lorom();
printf("Disable snes WR\n");
snes_wr_disable();
sei();
printf("USB poll\n");
while (req_state != REQ_STATUS_SNES){
usbPoll();
}
printf("USB poll done\n");
#if 0
crc_check_bulk_memory(0x000000, req_addr_end, req_bank_size);
#endif
snes_reset_hi();
snes_reset_off();
snes_irq_lo();
snes_irq_off();
printf("IRQ off\n");
if (req_bank_size == 0x8000){
snes_lorom();
printf("Set Snes lowrom \n");
} else {
snes_hirom();
printf("Set Snes hirom \n");
}
set_rom_mode();
snes_wr_disable();
printf("Disable snes WR\n");
snes_bus_active();
printf("Activate Snes bus\n");
_delay_ms(100);
printf("Reset Snes\n");
snes_reset_on();
snes_reset_lo();
_delay_ms(2);
snes_reset_hi();
snes_reset_off();
send_reset();
printf("Poll\n");
while (req_state != REQ_STATUS_AVR){
usbPoll();
#if 0
i = 20;
#if 1
i = 10;
while (--i) { /* fake USB disconnect for > 250 ms */
_delay_ms(500);
printf("Wait to switch to avr mode %i\n", i);
_delay_ms(100);
}
printf("Send IRQ\n");
//snes_irq_lo();
//snes_irq_on();
//_delay_ms(1);
printf("Send IRQ %i\n",++irq_count);
send_irq();
#endif
#if 0
avr_bus_active();
sram_bulk_read_start(0x003000);
c = sram_bulk_read();
i = 5;
while (--i) { /* fake USB disconnect for > 250 ms */
while (--i) {
_delay_ms(500);
printf("Wait to switch to snes mode %i\n", i);
}
@ -468,10 +569,8 @@ int main(void)
}
snes_wr_disable();
printf("Disable snes WR\n");
snes_bus_active();
printf("Activate Snes bus\n");
printf("Read 0x3000=%c\n",c);
#endif
}

82
avr/usbload/rle.c Normal file
View File

@ -0,0 +1,82 @@
#include <avr/io.h>
#include <stdlib.h>
#include <stdio.h>
#include <avr/pgmspace.h> /* required by usbdrv.h */
#include <util/delay.h> /* for _delay_ms() */
#include <avr/interrupt.h> /* for sei() */
#include "sram.h"
#include "debug.h"
#define RUNCHAR 0x90
uint8_t rle_decode(PGM_VOID_P 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;
printf("RLE decode len=%li addr=0x%08lx\n",in_len,out_addr);
out_len_left = out_len;
sram_bulk_write_start(out_addr);
#define INBYTE(b) \
do { \
if ( --in_len < 0 ) { \
return 1; \
} \
cli();\
b = pgm_read_byte((PGM_VOID_P)in_addr++); \
sei();\
} while(0)
#define OUTBYTE(b) \
do { \
sram_bulk_write(b);\
sram_bulk_write_next();\
out_addr++;\
} while(0)
/*
** Handle first byte separately (since we have to get angry
** in case of an orphaned RLE code).
*/
INBYTE(in_byte);
if (in_byte == RUNCHAR) {
INBYTE(in_repeat);
if (in_repeat != 0) {
/* Note Error, not Incomplete (which is at the end
** of the string only). This is a programmer error.
*/
printf("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)
printf(".");
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;
}
sram_bulk_write_end();
return 0;
}

3
avr/usbload/rle.h Normal file
View File

@ -0,0 +1,3 @@
#include <avr/pgmspace.h>
uint8_t rle_decode(PGM_VOID_P in_addr,uint32_t in_len, uint32_t out_addr);