main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* sd2iec - SD/MMC to Commodore serial bus interface/controller
  2. Copyright (C) 2007-2009 Ingo Korb <ingo@akana.de>
  3. Inspiration and low-level SD/MMC access based on code from MMC2IEC
  4. by Lars Pontoppidan et al., see sdcard.c|h and config.h.
  5. FAT filesystem access based on code from ChaN and Jim Brain, see ff.c|h.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; version 2 of the License only.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. main.c: Lots of init calls for the submodules
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <avr/boot.h>
  21. #include <avr/interrupt.h>
  22. #include <avr/pgmspace.h>
  23. #include <avr/power.h>
  24. #include <avr/wdt.h>
  25. #include <util/delay.h>
  26. #include "config.h"
  27. #include "diskio.h"
  28. #include "ff.h"
  29. #include "led.h"
  30. #include "timer.h"
  31. #include "fpga.h"
  32. #include "uart.h"
  33. #include "ustring.h"
  34. #include "utils.h"
  35. #include "snes.h"
  36. #include "fileops.h"
  37. #include "memory.h"
  38. #include "fpga_spi.h"
  39. #include "spi.h"
  40. #include "avrcompat.h"
  41. #include "filetypes.h"
  42. /* Make sure the watchdog is disabled as soon as possible */
  43. /* Copy this code to your bootloader if you use one and your */
  44. /* MCU doesn't disable the WDT after reset! */
  45. void get_mcusr(void) \
  46. __attribute__((naked)) \
  47. __attribute__((section(".init3")));
  48. void get_mcusr(void)
  49. {
  50. MCUSR = 0;
  51. wdt_disable();
  52. }
  53. #ifdef CONFIG_MEMPOISON
  54. void poison_memory(void) \
  55. __attribute__((naked)) \
  56. __attribute__((section(".init1")));
  57. void poison_memory(void) {
  58. register uint16_t i;
  59. register uint8_t *ptr;
  60. asm("clr r1\n");
  61. /* There is no RAMSTARt variable =( */
  62. if (RAMEND > 2048 && RAMEND < 4096) {
  63. /* 2K memory */
  64. ptr = (void *)RAMEND-2047;
  65. for (i=0;i<2048;i++)
  66. ptr[i] = 0x55;
  67. } else if (RAMEND > 4096 && RAMEND < 8192) {
  68. /* 4K memory */
  69. ptr = (void *)RAMEND-4095;
  70. for (i=0;i<4096;i++)
  71. ptr[i] = 0x55;
  72. } else {
  73. /* Assume 8K memory */
  74. ptr = (void *)RAMEND-8191;
  75. for (i=0;i<8192;i++)
  76. ptr[i] = 0x55;
  77. }
  78. }
  79. #endif
  80. void avr_goto_addr(const uint32_t val) {
  81. AVR_ADDR_RESET();
  82. for(uint32_t i=0; i<val; i++) {
  83. AVR_NEXTADDR();
  84. }
  85. }
  86. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 1)
  87. int main(void) __attribute__((OS_main));
  88. #endif
  89. int main(void) {
  90. #if defined __AVR_ATmega644__ || defined __AVR_ATmega644P__ || defined __AVR_ATmega2561__
  91. asm volatile("in r24, %0\n"
  92. "ori r24, 0x80\n"
  93. "out %0, r24\n"
  94. "out %0, r24\n"
  95. :
  96. : "I" (_SFR_IO_ADDR(MCUCR))
  97. : "r24"
  98. );
  99. #elif defined __AVR_ATmega32__
  100. asm volatile ("in r24, %0\n"
  101. "ori r24, 0x80\n"
  102. "out %0, r24\n"
  103. "out %0, r24\n"
  104. :
  105. : "I" (_SFR_IO_ADDR(MCUCSR))
  106. : "r24"
  107. );
  108. #elif defined __AVR_ATmega128__ || defined __AVR_ATmega1281__
  109. /* Just assume that JTAG doesn't hurt us on the m128 */
  110. #else
  111. # error Unknown chip!
  112. #endif
  113. #ifdef CLOCK_PRESCALE
  114. clock_prescale_set(CLOCK_PRESCALE);
  115. #endif
  116. set_pwr_led(0);
  117. set_busy_led(1);
  118. spi_none();
  119. snes_reset(1);
  120. uart_init();
  121. sei(); // suspected to reset the AVR when inserting an SD card
  122. _delay_ms(100);
  123. disk_init();
  124. snes_init();
  125. timer_init();
  126. uart_puts_P(PSTR("\nsd2snes " VERSION));
  127. uart_putcrlf();
  128. file_init();
  129. FATFS fatfs;
  130. f_mount(0,&fatfs);
  131. uart_putc('W');
  132. fpga_init();
  133. fpga_pgm((uint8_t*)"/sd2snes/main.bit");
  134. _delay_ms(100);
  135. set_pwr_led(1);
  136. fpga_spi_init();
  137. uart_putc('!');
  138. _delay_ms(100);
  139. set_avr_ena(0);
  140. snes_reset(1);
  141. *fs_path=0;
  142. uint16_t saved_dir_id;
  143. get_db_id(&saved_dir_id);
  144. uint16_t mem_dir_id = sram_readshort(SRAM_DIRID);
  145. uint32_t mem_magic = sram_readlong(SRAM_SCRATCHPAD);
  146. led_pwm();
  147. if((mem_magic != 0x12345678) || (mem_dir_id != saved_dir_id)) {
  148. uint16_t curr_dir_id = scan_dir(fs_path, 0, 0); // generate files footprint
  149. dprintf("curr dir id = %x\n", curr_dir_id);
  150. led_pwm();
  151. if((get_db_id(&saved_dir_id) != FR_OK) // no database?
  152. || saved_dir_id != curr_dir_id) { // files changed? // XXX
  153. dprintf("saved dir id = %x\n", saved_dir_id);
  154. _delay_ms(50);
  155. dprintf("rebuilding database...");
  156. _delay_ms(50);
  157. curr_dir_id = scan_dir(fs_path, 1, 0); // then rebuild database
  158. sram_writeblock(&curr_dir_id, SRAM_DB_ADDR, 2);
  159. uint32_t endaddr, direndaddr;
  160. sram_readblock(&endaddr, SRAM_DB_ADDR+4, 4);
  161. sram_readblock(&direndaddr, SRAM_DB_ADDR+8, 4);
  162. dprintf("%lx %lx\n", endaddr, direndaddr);
  163. save_sram((uint8_t*)"/sd2snes/sd2snes.db", endaddr-SRAM_DB_ADDR, SRAM_DB_ADDR);
  164. save_sram((uint8_t*)"/sd2snes/sd2snes.dir", direndaddr-(SRAM_DIR_ADDR), SRAM_DIR_ADDR);
  165. dprintf("done\n");
  166. sram_hexdump(SRAM_DB_ADDR, 0x400);
  167. } else {
  168. dprintf("different card, consistent db, loading db...\n");
  169. load_sram((uint8_t*)"/sd2snes/sd2snes.db", SRAM_DB_ADDR);
  170. load_sram((uint8_t*)"/sd2snes/sd2snes.dir", SRAM_DIR_ADDR);
  171. }
  172. // save_sram((uint8_t*)"/debug.smc", 0x400000, 0);
  173. // uart_putc('[');
  174. // load_sram((uint8_t*)"/test.srm", SRAM_SAVE_ADDR);
  175. // uart_putc(']');
  176. sram_writeshort(curr_dir_id, SRAM_DIRID);
  177. sram_writelong(0x12345678, SRAM_SCRATCHPAD);
  178. } else {
  179. dprintf("same card, loading db...\n");
  180. load_sram((uint8_t*)"/sd2snes/sd2snes.db", SRAM_DB_ADDR);
  181. load_sram((uint8_t*)"/sd2snes/sd2snes.dir", SRAM_DIR_ADDR);
  182. }
  183. uart_putc('(');
  184. load_rom((uint8_t*)"/sd2snes/menu.bin");
  185. set_rom_mask(0x3fffff); // force mirroring off
  186. uart_putc(')');
  187. sram_writebyte(0, SRAM_CMD_ADDR);
  188. set_busy_led(0);
  189. set_avr_ena(1);
  190. _delay_ms(100);
  191. uart_puts_P(PSTR("SNES GO!\n"));
  192. snes_reset(0);
  193. uint8_t cmd = 0;
  194. while(!sram_reliable());
  195. while(!cmd) {
  196. cmd=menu_main_loop();
  197. switch(cmd) {
  198. case 0x01: // SNES_CMD_LOADROM:
  199. get_selected_name(file_lfn);
  200. _delay_ms(100);
  201. // snes_reset(1);
  202. set_avr_ena(0);
  203. // dprintf("Selected name: %s\n", file_lfn);
  204. load_rom(file_lfn);
  205. set_avr_ena(1);
  206. snes_reset(1);
  207. _delay_ms(100);
  208. snes_reset(0);
  209. break;
  210. default:
  211. break;
  212. }
  213. }
  214. dprintf("cmd was %x, going to snes main loop\n", cmd);
  215. cmd=0;
  216. while(1) {
  217. if(get_snes_reset()) {
  218. dprintf("RESET\n");
  219. }
  220. snes_main_loop();
  221. }
  222. /* HERE BE LIONS */
  223. while(1) {
  224. set_avr_addr(0x600000);
  225. spi_fpga();
  226. spiTransferByte(0x81); // read w/ increment... hopefully
  227. spiTransferByte(0x00); // 1 dummy read
  228. uart_putcrlf();
  229. uint8_t buff[21];
  230. for(uint8_t cnt=0; cnt<21; cnt++) {
  231. uint8_t data=spiTransferByte(0x00);
  232. buff[cnt]=data;
  233. }
  234. for(uint8_t cnt=0; cnt<21; cnt++) {
  235. uint8_t data = buff[cnt];
  236. _delay_ms(2);
  237. if(data>=0x20 && data <= 0x7a) {
  238. uart_putc(data);
  239. } else {
  240. // uart_putc('.');
  241. uart_putc("0123456789ABCDEF"[data>>4]);
  242. uart_putc("0123456789ABCDEF"[data&15]);
  243. uart_putc(' ');
  244. }
  245. // set_avr_bank(3);
  246. }
  247. spi_none();
  248. }
  249. while(1);
  250. }