snes.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* sd2snes - SD card based universal cartridge for the SNES
  2. Copyright (C) 2009-2010 Maximilian Rehkopf <otakon@gmx.net>
  3. AVR firmware portion
  4. Inspired by and based on code from sd2iec, written by Ingo Korb et al.
  5. See sdcard.c|h, config.h.
  6. FAT file system access based on code by ChaN, Jim Brain, Ingo Korb,
  7. see ff.c|h.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; version 2 of the License only.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. snes.c: SNES hardware control and monitoring
  19. */
  20. #include <arm/NXP/LPC17xx/LPC17xx.h>
  21. #include "bits.h"
  22. #include "config.h"
  23. #include "uart.h"
  24. #include "snes.h"
  25. #include "memory.h"
  26. #include "fileops.h"
  27. #include "ff.h"
  28. #include "led.h"
  29. #include "smc.h"
  30. #include "timer.h"
  31. #include "cli.h"
  32. #include "fpga.h"
  33. #include "fpga_spi.h"
  34. uint8_t initloop=1;
  35. uint32_t saveram_crc, saveram_crc_old;
  36. extern snes_romprops_t romprops;
  37. volatile int reset_changed;
  38. volatile int reset_pressed;
  39. void prepare_reset() {
  40. snes_reset(1);
  41. delay_ms(SNES_RESET_PULSELEN_MS);
  42. if(romprops.ramsize_bytes && fpga_test() == FPGA_TEST_TOKEN) {
  43. writeled(1);
  44. save_sram(file_lfn, romprops.ramsize_bytes, SRAM_SAVE_ADDR);
  45. writeled(0);
  46. }
  47. rdyled(1);
  48. readled(1);
  49. writeled(1);
  50. snes_reset(0);
  51. while(get_snes_reset());
  52. snes_reset(1);
  53. fpga_dspx_reset(1);
  54. delay_ms(200);
  55. }
  56. void snes_init() {
  57. /* put reset level on reset pin */
  58. BITBAND(SNES_RESET_REG->FIOCLR, SNES_RESET_BIT) = 1;
  59. /* reset the SNES */
  60. snes_reset(1);
  61. }
  62. void snes_reset_pulse() {
  63. snes_reset(1);
  64. delay_ms(SNES_RESET_PULSELEN_MS);
  65. snes_reset(0);
  66. }
  67. /*
  68. * sets the SNES reset state.
  69. *
  70. * state: put SNES in reset state when 1, release when 0
  71. */
  72. void snes_reset(int state) {
  73. if (state == 0)
  74. printf("Releasing SNES RESET\n");
  75. else
  76. printf("Pull SNES RESET\n");
  77. BITBAND(SNES_RESET_REG->FIODIR, SNES_RESET_BIT) = state;
  78. }
  79. /*
  80. * gets the SNES reset state.
  81. *
  82. * returns: 1 when reset, 0 when not reset
  83. */
  84. uint8_t get_snes_reset() {
  85. return !BITBAND(SNES_RESET_REG->FIOPIN, SNES_RESET_BIT);
  86. }
  87. /*
  88. * SD2SNES main loop.
  89. * monitors SRAM changes and other things
  90. */
  91. uint32_t diffcount = 0, samecount = 0, didnotsave = 0;
  92. uint8_t sram_valid = 0;
  93. void snes_main_loop() {
  94. if(!romprops.ramsize_bytes)return;
  95. if(initloop) {
  96. saveram_crc_old = calc_sram_crc(SRAM_SAVE_ADDR, romprops.ramsize_bytes);
  97. initloop=0;
  98. }
  99. saveram_crc = calc_sram_crc(SRAM_SAVE_ADDR, romprops.ramsize_bytes);
  100. sram_valid = sram_reliable();
  101. if(crc_valid && sram_valid) {
  102. if(saveram_crc != saveram_crc_old) {
  103. if(samecount) {
  104. diffcount=1;
  105. } else {
  106. diffcount++;
  107. didnotsave++;
  108. }
  109. samecount=0;
  110. }
  111. if(saveram_crc == saveram_crc_old) {
  112. samecount++;
  113. }
  114. if(diffcount>=1 && samecount==5) {
  115. printf("SaveRAM CRC: 0x%04lx; saving %s\n", saveram_crc, file_lfn);
  116. writeled(1);
  117. save_sram(file_lfn, romprops.ramsize_bytes, SRAM_SAVE_ADDR);
  118. writeled(0);
  119. didnotsave=0;
  120. }
  121. if(didnotsave>50) {
  122. printf("periodic save (sram contents keep changing...)\n");
  123. diffcount=0;
  124. writeled(1);
  125. save_sram(file_lfn, romprops.ramsize_bytes, SRAM_SAVE_ADDR);
  126. didnotsave=0;
  127. writeled(1);
  128. }
  129. saveram_crc_old = saveram_crc;
  130. }
  131. printf("crc=%lx crc_valid=%d sram_valid=%d diffcount=%ld samecount=%ld, didnotsave=%ld\n", saveram_crc, crc_valid, sram_valid, diffcount, samecount, didnotsave);
  132. }
  133. /*
  134. * SD2SNES menu loop.
  135. * monitors menu selection. return when selection was made.
  136. */
  137. uint8_t menu_main_loop() {
  138. uint8_t cmd = 0;
  139. sram_writebyte(0, SRAM_CMD_ADDR);
  140. while(!cmd) {
  141. if(!get_snes_reset()) {
  142. while(!sram_reliable())printf("hurr\n");
  143. cmd = sram_readbyte(SRAM_CMD_ADDR);
  144. }
  145. if(get_snes_reset()) {
  146. cmd = 0;
  147. }
  148. sleep_ms(20);
  149. cli_entrycheck();
  150. }
  151. return cmd;
  152. }
  153. void get_selected_name(uint8_t* fn) {
  154. uint32_t addr;
  155. addr = sram_readlong(SRAM_PARAM_ADDR);
  156. printf("fd addr=%lx\n", addr);
  157. sram_readblock(fn, addr + 7 + SRAM_MENU_ADDR, 256);
  158. }
  159. void snes_bootprint(void* msg)
  160. {
  161. printf("%s\n", (char*)msg);
  162. sram_writeblock(msg, SRAM_CMD_ADDR, 33);
  163. }
  164. void snes_menu_errmsg(int err, void* msg)
  165. {
  166. printf("%d: %s\n", err, (char*)msg);
  167. sram_writeblock(msg, SRAM_CMD_ADDR+1, 64);
  168. sram_writebyte(err, SRAM_CMD_ADDR);
  169. }