main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. spi_none();
  117. snes_reset(1);
  118. uart_init();
  119. sei(); // suspected to reset the AVR when inserting an SD card
  120. _delay_ms(100);
  121. disk_init();
  122. snes_init();
  123. timer_init();
  124. uart_puts_P(PSTR("\nsd2snes " VERSION));
  125. uart_putcrlf();
  126. file_init();
  127. FATFS fatfs;
  128. f_mount(0,&fatfs);
  129. set_busy_led(1);
  130. uart_putc('W');
  131. fpga_init();
  132. fpga_pgm("/sd2snes/main.bit");
  133. _delay_ms(100);
  134. fpga_spi_init();
  135. uart_putc('!');
  136. _delay_ms(100);
  137. set_avr_ena(0);
  138. snes_reset(1);
  139. *fs_path=0;
  140. uint16_t curr_dir_id = scan_dir(fs_path, 0); // generate files footprint
  141. dprintf("curr dir id = %x\n", curr_dir_id);
  142. uint16_t saved_dir_id;
  143. if((get_db_id(&saved_dir_id) != FR_OK) // no database?
  144. || saved_dir_id != curr_dir_id) { // files changed?
  145. dprintf("saved dir id = %x\n", saved_dir_id);
  146. _delay_ms(50);
  147. dprintf("rebuilding database...");
  148. _delay_ms(50);
  149. curr_dir_id = scan_dir(fs_path, 1); // then rebuild database
  150. sram_writeblock(&curr_dir_id, SRAM_WORK_ADDR, 2);
  151. uint32_t endaddr;
  152. sram_readblock(&endaddr, SRAM_WORK_ADDR+4, 4);
  153. dprintf("%lx\n", endaddr);
  154. save_sram("/sd2snes/sd2snes.db", endaddr-SRAM_WORK_ADDR, SRAM_WORK_ADDR);
  155. dprintf("done\n");
  156. }
  157. uart_putc('[');
  158. load_sram("/test.srm");
  159. uart_putc(']');
  160. uart_putc('(');
  161. load_rom("/test.smc");
  162. uart_putc(')');
  163. set_busy_led(0);
  164. set_avr_ena(1);
  165. _delay_ms(100);
  166. uart_puts_P(PSTR("SNES GO!\n"));
  167. snes_reset(0);
  168. while(1) {
  169. snes_main_loop();
  170. }
  171. /* HERE BE LIONS */
  172. while(1) {
  173. set_avr_addr(0x600000);
  174. spi_fpga();
  175. spiTransferByte(0x81); // read w/ increment... hopefully
  176. spiTransferByte(0x00); // 1 dummy read
  177. uart_putcrlf();
  178. uint8_t buff[21];
  179. for(uint8_t cnt=0; cnt<21; cnt++) {
  180. uint8_t data=spiTransferByte(0x00);
  181. buff[cnt]=data;
  182. }
  183. for(uint8_t cnt=0; cnt<21; cnt++) {
  184. uint8_t data = buff[cnt];
  185. _delay_ms(2);
  186. if(data>=0x20 && data <= 0x7a) {
  187. uart_putc(data);
  188. } else {
  189. // uart_putc('.');
  190. uart_putc("0123456789ABCDEF"[data>>4]);
  191. uart_putc("0123456789ABCDEF"[data&15]);
  192. uart_putc(' ');
  193. }
  194. // set_avr_bank(3);
  195. }
  196. spi_none();
  197. }
  198. while(1);
  199. }