diff --git a/avr/usbload/Makefile b/avr/usbload/Makefile index 608d70e..26ff6ab 100644 --- a/avr/usbload/Makefile +++ b/avr/usbload/Makefile @@ -16,6 +16,7 @@ # Author: Christian Starkjohann # ===================================================================================== +DEBUG = 1 TTY = /dev/tty.PL2303-00002126 DEVICE = atmega644 F_CPU = 20000000 # in Hz @@ -23,16 +24,21 @@ TARGET = main AVRDUDE = avrdude -c usbasp -p $(DEVICE) SIZE = avr-size -#LDFLAGS = -Wl,-u,vfprintf -lprintf_flt -#CFLAGS = -Iusbdrv -I. -DDEBUG_LEVEL=0 -LDFLAGS = -Wl,-u,vfprintf -CFLAGS = -Iusbdrv -I. -DDEBUG_LEVEL=0 -DNO_DEBUG -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 info.o + +ifeq ($(DEBUG),1) + LDFLAGS = -Wl,-u,vfprintf -lprintf_flt + 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 huffman-decode.o rle.c loader.o info.o shared_memory.o +else + LDFLAGS = -Wl,-u + CFLAGS = -Iusbdrv -I. -DDEBUG_LEVEL=0 -DNO_DEBUG -DNO_INFO + 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 rle.c loader.o info.o shared_memory.o +endif + COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE) - ############################################################################## # Fuse values for particular devices ############################################################################## diff --git a/avr/usbload/crc.c b/avr/usbload/crc.c index 73eb882..b8f6123 100644 --- a/avr/usbload/crc.c +++ b/avr/usbload/crc.c @@ -27,6 +27,7 @@ #include "config.h" #include "sram.h" #include "debug.h" +#include "info.h" extern FILE uart_stdout; diff --git a/avr/usbload/dump.c b/avr/usbload/dump.c index aac4c48..46df814 100644 --- a/avr/usbload/dump.c +++ b/avr/usbload/dump.c @@ -23,6 +23,7 @@ #include #include "debug.h" +#include "info.h" #include "uart.h" #include "sram.h" @@ -46,21 +47,21 @@ void dump_packet(uint32_t addr, uint32_t len, uint8_t * packet) continue; } if (clear) { - printf("*\n"); + info("*\n"); clear = 0; } - printf("%08lx:", addr + i); + info("%08lx:", addr + i); for (j = 0; j < 16; j++) { - printf(" %02x", packet[i + j]); + info(" %02x", packet[i + j]); } - printf(" |"); + info(" |"); for (j = 0; j < 16; j++) { if (packet[i + j] >= 33 && packet[i + j] <= 126) - printf("%c", packet[i + j]); + info("%c", packet[i + j]); else - printf("."); + info("."); } - printf("|\n"); + info("|\n"); } } @@ -71,11 +72,11 @@ void dump_memory(uint32_t bottom_addr, uint32_t top_addr) sram_bulk_read_start(bottom_addr); for ( addr = bottom_addr; addr < top_addr; addr++) { if (addr%0x10 == 0) - printf("\n%08lx:", addr); + info("\n%08lx:", addr); byte = sram_bulk_read(); sram_bulk_read_next(); - printf(" %02x", byte); + info(" %02x", byte); } - printf("\n"); + info("\n"); sram_bulk_read_end(); } diff --git a/avr/usbload/huffman-decode.c b/avr/usbload/huffman-decode.c index 0aa9d4e..89f5c48 100644 --- a/avr/usbload/huffman-decode.c +++ b/avr/usbload/huffman-decode.c @@ -18,12 +18,15 @@ */ -#include "huffman-decode.h" #include #include #include #include +#include "huffman-decode.h" +#include "info.h" +#include "debug.h" + #ifdef DEBUG #undef DEBUG #endif @@ -100,11 +103,11 @@ static inline void set_last_to_eof(node_t* start){ #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); + info("\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); + info("\n%p => %i",node,node->value); } } #endif diff --git a/avr/usbload/main.c b/avr/usbload/main.c index 3e14940..6db74a1 100644 --- a/avr/usbload/main.c +++ b/avr/usbload/main.c @@ -35,6 +35,7 @@ #include "uart.h" #include "sram.h" #include "debug.h" +#include "info.h" #include "dump.h" #include "crc.h" #include "usb_bulk.h" @@ -43,6 +44,7 @@ #include "huffman-decode.h" #include "rle.h" #include "loader.h" +#include "shared_memory.h" extern const char _rom[] PROGMEM; @@ -89,6 +91,7 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) req_bank_size = (uint32_t)1 << rq->wValue.word; sync_errors = 0; crc = 0; + shared_memory_put(SHARED_MEM_CMD_UPLOAD_START,0); debug(DEBUG_USB,"USB_UPLOAD_INIT: bank_size=0x%08lx\n", req_bank_size); /* @@ -121,6 +124,7 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) req_bank, req_addr); req_bank++; + shared_memory_put(SHARED_MEM_CMD_UPLOAD_PROGESS,req_bank); } ret_len = USB_MAX_TRANS; /* @@ -150,6 +154,7 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) debug(DEBUG_USB,"USB_BULK_UPLOAD_INIT: bank_size=0x%08lx bank_cnt=0x%x end_addr=0x%08lx\n", req_bank_size, req_bank_cnt, req_addr_end); + shared_memory_put(SHARED_MEM_CMD_UPLOAD_START,0); if (req_addr == 0x000000){ timer_start(); } @@ -206,6 +211,7 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) req_bank, req_addr,timer_stop_int()); #endif req_bank++; + shared_memory_put(SHARED_MEM_CMD_UPLOAD_PROGESS,req_bank); timer_start(); } @@ -221,7 +227,9 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) debug(DEBUG_USB,"USB_BULK_UPLOAD_END:\n"); req_state = REQ_STATUS_IDLE; sram_bulk_write_end(); + shared_memory_put(SHARED_MEM_CMD_UPLOAD_END,0); ret_len = 0; + /* * ------------------------------------------------------------------------- */ @@ -298,7 +306,7 @@ void test_read_write(){ } addr = 0x000000; while (addr++ <= 0x0000ff){ - printf("read addr=0x%08lx %x\n",addr,sram_read(addr)); + info("read addr=0x%08lx %x\n",addr,sram_read(addr)); } } @@ -321,7 +329,7 @@ void test_bulk_read_write(){ addr = 0x000000; sram_bulk_read_start(addr); while (addr <= 0x8000){ - printf("addr=0x%08lx %x\n",addr,sram_bulk_read()); + info("addr=0x%08lx %x\n",addr,sram_bulk_read()); sram_bulk_read_next(); addr++; } @@ -337,7 +345,7 @@ void test_non_zero_memory(uint32_t bottom_addr,uint32_t top_addr) for (addr = bottom_addr; addr < top_addr; addr++) { c = sram_bulk_read(); if (c!=0xff) - printf("addr=0x%08lx c=0x%x\n",addr,c); + info("addr=0x%08lx c=0x%x\n",addr,c); sram_bulk_read_next(); } sram_bulk_read_end(); @@ -346,12 +354,12 @@ void test_non_zero_memory(uint32_t bottom_addr,uint32_t top_addr) void test_crc(){ - printf("test_crc: clear\n"); + info("test_crc: clear\n"); avr_bus_active(); sram_bulk_set(0x000000,0x10000,0xff); - printf("test_crc: crc\n"); + info("test_crc: crc\n"); crc_check_bulk_memory(0x000000,0x10000,0x8000); - printf("test_crc: check\n"); + info("test_crc: check\n"); test_non_zero_memory(0x000000,0x10000); } @@ -374,7 +382,7 @@ void decompress(PGM_VOID_P addr, uint16_t(*fp)(uint16_t)){ i++; c=huffman_dec_byte(&ctx); if (i%1024==0) - printf("."); + info("."); if(c>0xff){ return; } @@ -384,17 +392,17 @@ void decompress(PGM_VOID_P addr, uint16_t(*fp)(uint16_t)){ } void decompress_huffman(void){ - printf("Decompress Rom %p to 0x000000\n",(void*)_rom); + info("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"); + info("Done\n"); } void send_reset(){ - printf("Reset Snes\n"); + info("Reset Snes\n"); snes_reset_on(); snes_reset_lo(); _delay_ms(2); @@ -413,20 +421,20 @@ void send_irq(){ void set_rom_mode(){ if (req_bank_size == 0x8000){ snes_lorom(); - printf("Set Snes lowrom \n"); + info("Set Snes lowrom \n"); } else { snes_hirom(); - printf("Set Snes hirom \n"); + info("Set Snes hirom \n"); } } void usb_connect(){ uint8_t i = 0; - printf("USB init\n"); + info("USB init\n"); usbDeviceDisconnect(); /* enforce re-enumeration, do this while */ cli(); - printf("USB disconnect\n"); + info("USB disconnect\n"); i = 10; while (--i) { /* fake USB disconnect for > 250 ms */ led_on(); @@ -436,7 +444,7 @@ void usb_connect(){ } led_on(); usbDeviceConnect(); - printf("USB connect\n"); + info("USB connect\n"); } @@ -444,28 +452,27 @@ void boot_startup_rom(){ uint8_t i = 0; - printf("Activate AVR bus\n"); + info("Activate AVR bus\n"); avr_bus_active(); - printf("IRQ off\n"); + info("IRQ off\n"); snes_irq_lo(); snes_irq_off(); snes_lorom(); - printf("Set Snes lowrom \n"); + info("Set Snes lowrom \n"); /* - printf("Set Snes hirom\n"); + info("Set Snes hirom\n"); snes_hirom(); - printf("Disable snes WR\n"); + info("Disable snes WR\n"); snes_wr_disable(); - printf("IRQ off\n"); + info("IRQ off\n"); snes_irq_lo(); snes_irq_off(); */ - rle_decode(&_rom, ROM_SIZE, 0x000000); dump_memory(0x10000 - 0x100, 0x10000); @@ -473,23 +480,24 @@ void boot_startup_rom(){ snes_reset_off(); snes_irq_lo(); snes_irq_off(); - printf("IRQ off\n"); + info("IRQ off\n"); snes_hirom(); snes_wr_disable(); - printf("Disable snes WR\n"); + info("Disable snes WR\n"); snes_bus_active(); - printf("Activate Snes bus\n"); + info("Activate Snes bus\n"); _delay_ms(100); - printf("Reset Snes\n"); + info("Reset Snes\n"); send_reset(); +#if 0 i = 20; - printf("Wait"); + info("Wait"); while (--i){ _delay_ms(500); - printf("."); + info("."); } - printf("\n"); - + info("\n"); +#endif } int main(void) @@ -500,7 +508,7 @@ int main(void) uart_init(); stdout = &uart_stdout; - printf("Sytem start\n"); + info("Sytem start\n"); system_init(); #if 0 @@ -510,7 +518,7 @@ int main(void) while(1); #endif - printf("Boot startup rom\n"); + info("Boot startup rom\n"); boot_startup_rom(); usbInit(); @@ -518,43 +526,45 @@ int main(void) while (1){ avr_bus_active(); - printf("Activate AVR bus\n"); - printf("IRQ off\n"); + info("Activate AVR bus\n"); + info("IRQ off\n"); snes_irq_lo(); snes_irq_off(); - printf("Set Snes lowrom\n"); + info("Set Snes lowrom\n"); snes_lorom(); - printf("Disable snes WR\n"); + info("Disable snes WR\n"); snes_wr_disable(); sei(); - printf("USB poll\n"); + info("USB poll\n"); while (req_state != REQ_STATUS_SNES){ usbPoll(); } - printf("USB poll done\n"); + shared_memory_put(SHARED_MEM_CMD_TERMINATE,0); + info("USB poll done\n"); snes_reset_hi(); snes_reset_off(); snes_irq_lo(); snes_irq_off(); - printf("IRQ off\n"); + info("IRQ off\n"); set_rom_mode(); snes_wr_disable(); - printf("Disable snes WR\n"); + info("Disable snes WR\n"); snes_bus_active(); - printf("Activate Snes bus\n"); + info("Activate Snes bus\n"); _delay_ms(100); - printf("Reset Snes\n"); + info("Reset Snes\n"); send_reset(); - printf("Poll\n"); + info("Poll\n"); while (req_state != REQ_STATUS_AVR){ + usbPoll(); #ifdef DO_IRQ i = 10; while (--i) { /* fake USB disconnect for > 250 ms */ _delay_ms(100); } - printf("Send IRQ %i\n",++irq_count); + info("Send IRQ %i\n",++irq_count); send_irq(); #endif @@ -565,21 +575,21 @@ int main(void) i = 5; while (--i) { _delay_ms(500); - printf("Wait to switch to snes mode %i\n", i); + info("Wait to switch to snes mode %i\n", i); } if (req_bank_size == 0x8000){ snes_lorom(); - printf("Set Snes lowrom \n"); + info("Set Snes lowrom \n"); } else { snes_hirom(); - printf("Set Snes hirom \n"); + info("Set Snes hirom \n"); } snes_wr_disable(); - printf("Disable snes WR\n"); + info("Disable snes WR\n"); snes_bus_active(); - printf("Activate Snes bus\n"); - printf("Read 0x3000=%c\n",c); + info("Activate Snes bus\n"); + info("Read 0x3000=%c\n",c); #endif } } diff --git a/avr/usbload/rle.c b/avr/usbload/rle.c index dbe5164..e9f2e86 100644 --- a/avr/usbload/rle.c +++ b/avr/usbload/rle.c @@ -28,6 +28,7 @@ #include "sram.h" #include "debug.h" +#include "info.h" #define RUNCHAR 0x90 @@ -35,7 +36,7 @@ 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); + info("RLE decode len=%li addr=0x%08lx\n",in_len,out_addr); last_byte = 0; out_len_left = out_len; @@ -62,7 +63,7 @@ uint8_t rle_decode(PGM_VOID_P in_addr, int32_t in_len, uint32_t out_addr) if (in_byte == RUNCHAR) { INBYTE(in_repeat); if (in_repeat != 0) { - printf("Orphaned RLE code at start\n"); + info("Orphaned RLE code at start\n"); return 1; } OUTBYTE(RUNCHAR); @@ -73,7 +74,7 @@ uint8_t rle_decode(PGM_VOID_P in_addr, int32_t in_len, uint32_t out_addr) while( in_len > 0 ) { INBYTE(in_byte); if (in_len%1024==0) - printf("."); + info("."); if (in_byte == RUNCHAR) { INBYTE(in_repeat); if ( in_repeat == 0 ) { diff --git a/avr/usbload/shared_memory.c b/avr/usbload/shared_memory.c new file mode 100644 index 0000000..16d885e --- /dev/null +++ b/avr/usbload/shared_memory.c @@ -0,0 +1,98 @@ + + +/* + * ===================================================================================== + * + * ________ .__ __ ________ ____ ________ + * \_____ \ __ __|__| ____ | | __\______ \ _______ _/_ |/ _____/ + * / / \ \| | \ |/ ___\| |/ / | | \_/ __ \ \/ /| / __ \ + * / \_/. \ | / \ \___| < | ` \ ___/\ / | \ |__\ \ + * \_____\ \_/____/|__|\___ >__|_ \/_______ /\___ >\_/ |___|\_____ / + * \__> \/ \/ \/ \/ \/ + * + * www.optixx.org + * + * + * Version: 1.0 + * Created: 07/21/2009 03:32:16 PM + * Author: david@optixx.org + * + * ===================================================================================== + */ + + +#include +#include +#include + +#include "shared_memory.h" +#include "config.h" +#include "sram.h" +#include "debug.h" +#include "info.h" + +uint8_t irq_addr_lo; +uint8_t irq_addr_hi; + +uint8_t scratchpad_state; +uint8_t scratchpad_cmd; +uint8_t scratchpad_payload; + +void shared_memory_scratchpad_save(){ + scratchpad_state = sram_read(SHARED_MEM_LOC_STATE); + scratchpad_cmd = sram_read(SHARED_MEM_LOC_CMD); + scratchpad_payload = sram_read(SHARED_MEM_LOC_PAYLOAD); +} + +void shared_memory_scratchpad_restore(){ + sram_write(SHARED_MEM_LOC_STATE, scratchpad_state); + sram_write(SHARED_MEM_LOC_CMD, scratchpad_cmd); + sram_write(SHARED_MEM_LOC_PAYLOAD, scratchpad_payload); +} + +void shared_memory_irq_hook(){ + irq_addr_lo = sram_read(SHARED_IRQ_LOC_LO); + irq_addr_hi = sram_read(SHARED_IRQ_LOC_HI); + sram_write(SHARED_IRQ_HANDLER_LO, 0); + sram_write(SHARED_IRQ_HANDLER_HI, 0); +} + +void shared_memory_irq_restore(){ + sram_write(SHARED_IRQ_LOC_LO, irq_addr_lo); + sram_write(SHARED_IRQ_LOC_HI, irq_addr_hi); +} + +void shared_memory_put(uint8_t cmd, uint8_t value){ + + info("Write shared memory %06lx=%02x %06lx=%02x \n",SHARED_MEM_LOC_CMD,cmd,SHARED_MEM_LOC_PAYLOAD,value); + + shared_memory_scratchpad_save(); + shared_memory_irq_hook(); + + sram_write(SHARED_MEM_LOC_STATE,1); + sram_write(SHARED_MEM_LOC_CMD,cmd); + sram_write(SHARED_MEM_LOC_PAYLOAD,value); + + snes_irq_lo(); + snes_irq_off(); + snes_hirom(); + snes_wr_disable(); + snes_bus_active(); + + //snes_irq_on(); + //snes_irq_lo(); + //_delay_us(20); + + //snes_irq_hi(); + //snes_irq_off(); + + avr_bus_active(); + snes_irq_off(); + snes_irq_lo(); + snes_lorom(); + + shared_memory_scratchpad_restore(); + shared_memory_irq_restore(); + +} + diff --git a/avr/usbload/shared_memory.h b/avr/usbload/shared_memory.h new file mode 100644 index 0000000..b470eb9 --- /dev/null +++ b/avr/usbload/shared_memory.h @@ -0,0 +1,42 @@ +/* + * ===================================================================================== + * + * ________ .__ __ ________ ____ ________ + * \_____ \ __ __|__| ____ | | __\______ \ _______ _/_ |/ _____/ + * / / \ \| | \ |/ ___\| |/ / | | \_/ __ \ \/ /| / __ \ + * / \_/. \ | / \ \___| < | ` \ ___/\ / | \ |__\ \ + * \_____\ \_/____/|__|\___ >__|_ \/_______ /\___ >\_/ |___|\_____ / + * \__> \/ \/ \/ \/ \/ + * + * www.optixx.org + * + * + * Version: 1.0 + * Created: 07/21/2009 03:32:16 PM + * Author: david@optixx.org + * + * ===================================================================================== + */ + +#ifndef __SHARED_MEMORY_H__ +#define __SHARED_MEMORY_H__ + +#define SHARED_MEM_CMD_UPLOAD_START 1 +#define SHARED_MEM_CMD_UPLOAD_END 2 +#define SHARED_MEM_CMD_UPLOAD_PROGESS 3 +#define SHARED_MEM_CMD_TERMINATE 4 + +#define SHARED_MEM_LOC_STATE 0x000000 +#define SHARED_MEM_LOC_CMD 0x000001 +#define SHARED_MEM_LOC_PAYLOAD 0x000002 + +#define SHARED_IRQ_LOC_LO 0x00fffe +#define SHARED_IRQ_LOC_HI 0x00ffff + + +#define SHARED_IRQ_HANDLER_LO 0x12 +#define SHARED_IRQ_HANDLER_HI 0x34 + +void shared_memory_put(uint8_t cmd, uint8_t value); + +#endif diff --git a/avr/usbload/sram.c b/avr/usbload/sram.c index 7e71337..7baf527 100644 --- a/avr/usbload/sram.c +++ b/avr/usbload/sram.c @@ -29,6 +29,7 @@ #include "sram.h" #include "uart.h" #include "debug.h" +#include "info.h" void system_init(void) { diff --git a/avr/usbload/timer.c b/avr/usbload/timer.c index 65ba380..7034a08 100644 --- a/avr/usbload/timer.c +++ b/avr/usbload/timer.c @@ -28,6 +28,7 @@ #include /* for sei() */ #include "debug.h" +#include "info.h" #ifndef OCR1A #define OCR1A OCR1 // 2313 support diff --git a/avr/usbload/usb_bulk.c b/avr/usbload/usb_bulk.c index d003cc9..7357711 100644 --- a/avr/usbload/usb_bulk.c +++ b/avr/usbload/usb_bulk.c @@ -34,6 +34,8 @@ #include "uart.h" #include "sram.h" #include "debug.h" +#include "info.h" + #include "crc.h" #include "usb_bulk.h" @@ -54,7 +56,7 @@ uint8_t usbFunctionWrite(uint8_t * data, uint8_t len) uint8_t i; if (len > rx_remaining) { - printf("ERROR:usbFunctionWrite more data than expected remain: %i len: %i\n", + info("ERROR:usbFunctionWrite more data than expected remain: %i len: %i\n", rx_remaining, len); len = rx_remaining; }