123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 |
- /*
- * =====================================================================================
- *
- * ________ .__ __ ________ ____ ________
- * \_____ \ __ __|__| ____ | | __\______ \ _______ _/_ |/ _____/
- * / / \ \| | \ |/ ___\| |/ / | | \_/ __ \ \/ /| / __ \
- * / \_/. \ | / \ \___| < | ` \ ___/\ / | \ |__\ \
- * \_____\ \_/____/|__|\___ >__|_ \/_______ /\___ >\_/ |___|\_____ /
- * \__> \/ \/ \/ \/ \/
- * ___.
- * __ __ _____\_ |__
- * | | \/ ___/| __ \
- * | | /\___ \ | \_\ \
- * |____//____ >|___ /
- * \/ \/
- *
- * www.optixx.org
- *
- *
- * Version: 1.0
- * Created: 07/21/2009 03:32:16 PM
- * Author: david@optixx.org
- *
- * =====================================================================================
- */
- #include <avr/io.h>
- #include <avr/interrupt.h> /* for sei() */
- #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
- * macros */
- #include "config.h"
- #include "requests.h" /* The custom request numbers we use */
- #include "uart.h"
- #include "sram.h"
- #include "debug.h"
- #include "dump.h"
- #include "crc.h"
- #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 );
- uint8_t read_buffer[TRANSFER_BUFFER_SIZE];
- uint32_t req_addr = 0;
- uint32_t req_addr_end = 0;
- uint32_t req_size;
- uint8_t req_bank;
- uint32_t req_bank_size;
- uint16_t req_bank_cnt;
- uint8_t req_state = REQ_STATUS_IDLE;
- uint8_t rx_remaining = 0;
- uint8_t tx_remaining = 0;
- uint16_t sync_errors = 0;
- uint8_t tx_buffer[32];
- uint8_t data_buffer[4];
- uint32_t addr;
- uint16_t crc = 0;
- usbMsgLen_t usbFunctionSetup(uchar data[8])
- {
- usbRequest_t *rq = (void *) data;
- uint8_t ret_len = 0;
- /*
- * -------------------------------------------------------------------------
- */
- if (rq->bRequest == USB_UPLOAD_INIT) {
- if (req_state != REQ_STATUS_IDLE){
- debug(DEBUG_USB,"USB_UPLOAD_INIT: ERROR state is not REQ_STATUS_IDLE\n");
- return 0;
- }
- req_bank = 0;
- rx_remaining = 0;
- req_bank_size = (uint32_t)1 << rq->wValue.word;
- sync_errors = 0;
- crc = 0;
- debug(DEBUG_USB,"USB_UPLOAD_INIT: bank_size=0x%08lx\n", req_bank_size);
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_UPLOAD_ADDR) {
-
- req_state = REQ_STATUS_UPLOAD;
- req_addr = rq->wValue.word;
- req_addr = req_addr << 16;
- req_addr = req_addr | rq->wIndex.word;
- if (rx_remaining) {
- sync_errors++;
- debug
- (DEBUG_USB,"USB_UPLOAD_ADDR: Out of sync addr=0x%lx remain=%i packet=%i sync_error=%i\n",
- req_addr, rx_remaining, rq->wLength.word, sync_errors);
- ret_len = 0;
- }
- rx_remaining = rq->wLength.word;
- ret_len = USB_MAX_TRANS;
-
- if (req_addr && (req_addr % 0x1000) == 0) {
- debug(DEBUG_USB,"USB_UPLOAD_ADDR: bank=0x%02x addr=0x%08lx crc=%04x\n",
- req_bank, req_addr,crc_check_bulk_memory(req_addr - 0x1000,req_addr,req_bank_size));
-
- }
- if (req_addr && req_addr % req_bank_size == 0) {
- debug(DEBUG_USB,"USB_UPLOAD_ADDR: req_bank=0x%02x addr=0x%08lx\n",
- req_bank, req_addr);
- req_bank++;
- }
- ret_len = USB_MAX_TRANS;
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_DOWNLOAD_INIT) {
- debug(DEBUG_USB,"USB_DOWNLOAD_INIT\n");
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_DOWNLOAD_ADDR) {
- debug(DEBUG_USB,"USB_DOWNLOAD_ADDR\n");
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_BULK_UPLOAD_INIT) {
- req_bank = 0;
- rx_remaining = 0;
- debug(DEBUG_USB,"USB_BULK_UPLOAD_INIT: %i %i\n",rq->wValue.word, rq->wIndex.word);
- req_bank_size = (uint32_t)(1L << rq->wValue.word);
- req_bank_cnt = rq->wIndex.word;
- req_addr_end = (uint32_t)req_bank_size * req_bank_cnt;
-
- sync_errors = 0;
- 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);
-
- if (req_addr == 0x000000){
- timer_start();
- }
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_BULK_UPLOAD_ADDR) {
- req_state = REQ_STATUS_BULK_UPLOAD;
- req_addr = rq->wValue.word;
- req_addr = req_addr << 16;
- req_addr = req_addr | rq->wIndex.word;
- sram_bulk_write_start(req_addr);
- rx_remaining = rq->wLength.word;
-
- if (req_addr && req_addr % req_bank_size == 0) {
- debug(DEBUG_USB,"USB_BULK_UPLOAD_ADDR: req_bank=0x%02x addr=0x%08lx time=%.4f\n",
- req_bank, req_addr,timer_stop());
- req_bank++;
- timer_start();
-
- }
- ret_len = USB_MAX_TRANS;
-
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_BULK_UPLOAD_NEXT) {
- req_state = REQ_STATUS_BULK_UPLOAD;
- req_addr = rq->wValue.word;
- req_addr = req_addr << 16;
- req_addr = req_addr | rq->wIndex.word;
- rx_remaining = rq->wLength.word;
- #if 0
- if (req_addr && (req_addr % 0x1000) == 0) {
- debug(DEBUG_USB,"USB_BULK_UPLOAD_NEXT: bank=0x%02x addr=0x%08lx crc=%04x\n",
- req_bank, req_addr,crc_check_bulk_memory(req_addr - 0x1000,req_addr,req_bank_size));
-
- }
- sram_bulk_write_start(req_addr);
- #endif
- if (req_addr && ( req_addr % req_bank_size) == 0) {
- debug(DEBUG_USB,"USB_BULK_UPLOAD_NEXT: req_bank=0x%02x addr=0x%08lx time=%.4f\n",
- req_bank, req_addr,timer_stop());
- req_bank++;
- timer_start();
-
- }
- ret_len = USB_MAX_TRANS;
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_BULK_UPLOAD_END) {
- if (req_state != REQ_STATUS_BULK_UPLOAD){
- debug(DEBUG_USB,"USB_BULK_UPLOAD_END: ERROR state is not REQ_STATUS_BULK_UPLOAD\n");
- return 0;
- }
- debug(DEBUG_USB,"USB_BULK_UPLOAD_END:\n");
- req_state = REQ_STATUS_IDLE;
- sram_bulk_write_end();
- ret_len = 0;
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_CRC) {
- req_addr = rq->wValue.word;
- req_addr = req_addr << 16;
- req_addr = req_addr | rq->wIndex.word;
- debug(DEBUG_USB,"USB_CRC: addr=0x%08lx \n", req_addr);
- crc_check_bulk_memory(0x000000, req_addr, req_bank_size);
- ret_len = 0;
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_MODE_SNES) {
- req_state = REQ_STATUS_SNES;
- debug(DEBUG_USB,"USB_MODE_SNES:\n");
- ret_len = 0;
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_MODE_AVR) {
- req_state = REQ_STATUS_AVR;
- debug(DEBUG_USB,"USB_MODE_AVR:\n");
- ret_len = 0;
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_AVR_RESET) {
- debug(DEBUG_USB,"USB_AVR_RESET:\n");
- soft_reset();
- ret_len = 0;
- /*
- * -------------------------------------------------------------------------
- */
- } else if (rq->bRequest == USB_CRC_ADDR) {
- req_state = REQ_STATUS_CRC;
- req_addr = rq->wValue.word;
- req_addr = req_addr << 16;
- req_addr = req_addr | rq->wIndex.word;
- debug(DEBUG_USB,"USB_CRC_ADDR: addr=0x%lx size=%i\n", req_addr,
- rq->wLength.word);
- req_size = rq->wLength.word;
- req_size = req_size << 2;
- tx_remaining = 2;
- debug(DEBUG_USB,"USB_CRC_ADDR: addr=0x%lx size=%li\n", req_addr, req_size);
- crc = crc_check_memory_range(req_addr, req_size, read_buffer);
- tx_buffer[0] = crc & 0xff;
- tx_buffer[1] = (crc >> 8) & 0xff;
- ret_len = 2;
- req_state = REQ_STATUS_IDLE;
- }
- usbMsgPtr = data_buffer;
- return ret_len; /* default for not implemented requests: return
- * no data back to host */
- }
- /*
- * -------------------------------------------------------------------------
- */
- void test_read_write(){
-
- uint8_t i;
- uint32_t addr;
- avr_bus_active();
- addr = 0x000000;
- i = 1;
- while (addr++ <= 0x0000ff){
- sram_write(addr,i++);
- }
- addr = 0x000000;
- while (addr++ <= 0x0000ff){
- printf("read addr=0x%08lx %x\n",addr,sram_read(addr));
- }
- }
- void test_bulk_read_write(){
-
- uint8_t i;
- uint32_t addr;
- avr_bus_active();
- addr = 0x000000;
- i = 0;
- sram_bulk_write_start(addr);
- while (addr++ <= 0x8000){
- sram_bulk_write(i++);
- sram_bulk_write_next();
- }
- sram_bulk_write_end();
- addr = 0x000000;
- sram_bulk_read_start(addr);
- while (addr <= 0x8000){
- printf("addr=0x%08lx %x\n",addr,sram_bulk_read());
- sram_bulk_read_next();
- addr++;
- }
- sram_bulk_read_end();
- }
- void test_non_zero_memory(uint32_t bottom_addr,uint32_t top_addr)
- {
- uint32_t addr = 0;
- uint8_t c;
- sram_bulk_read_start(bottom_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);
- sram_bulk_read_next();
- }
- sram_bulk_read_end();
- }
- void test_crc(){
- printf("test_crc: clear\n");
- avr_bus_active();
- sram_bulk_set(0x000000,0x10000,0xff);
- printf("test_crc: crc\n");
- crc_check_bulk_memory(0x000000,0x10000,0x8000);
- printf("test_crc: check\n");
- 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();
- snes_lorom();
- printf("Set Snes lowrom \n");
- /*
- 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(0x10000 - 0x100, 0x10000);
- //crc_check_bulk_memory(0x00000, 0x10000, 0x8000);
-
- snes_reset_hi();
- snes_reset_off();
- snes_irq_lo();
- snes_irq_off();
- printf("IRQ off\n");
- snes_hirom();
- snes_wr_disable();
- printf("Disable snes WR\n");
- snes_bus_active();
- printf("Activate Snes bus\n");
- _delay_ms(100);
- printf("Reset Snes\n");
- send_reset();
- 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;
- uint16_t irq_count = 0;
-
- uart_init();
- stdout = &uart_stdout;
-
- printf("Sytem start\n");
- system_init();
- #if 0
- test_read_write();
- test_bulk_read_write();
- test_crc();
- while(1);
- #endif
-
- printf("Boot startup rom\n");
- boot_startup_rom();
-
- usbInit();
- 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");
- snes_reset_hi();
- snes_reset_off();
- snes_irq_lo();
- snes_irq_off();
- printf("IRQ off\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");
- send_reset();
- printf("Poll\n");
- while (req_state != REQ_STATUS_AVR){
- usbPoll();
- #if 1
- i = 10;
- while (--i) { /* fake USB disconnect for > 250 ms */
- _delay_ms(100);
- }
- 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) {
- _delay_ms(500);
- printf("Wait to switch to snes mode %i\n", i);
- }
-
- if (req_bank_size == 0x8000){
- snes_lorom();
- printf("Set Snes lowrom \n");
- } else {
- snes_hirom();
- printf("Set Snes hirom \n");
- }
- snes_wr_disable();
- printf("Disable snes WR\n");
- snes_bus_active();
- printf("Activate Snes bus\n");
- printf("Read 0x3000=%c\n",c);
- #endif
- }
- }
-
- return 0;
- }
|