snes.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. void prepare_reset() {
  39. set_mcu_ovr(1);
  40. snes_reset(1);
  41. delay_ms(1);
  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. /*
  63. * sets the SNES reset state.
  64. *
  65. * state: put SNES in reset state when 1, release when 0
  66. */
  67. void snes_reset(int state) {
  68. BITBAND(SNES_RESET_REG->FIODIR, SNES_RESET_BIT) = state;
  69. }
  70. /*
  71. * gets the SNES reset state.
  72. *
  73. * returns: 1 when reset, 0 when not reset
  74. */
  75. uint8_t get_snes_reset() {
  76. return !BITBAND(SNES_RESET_REG->FIOPIN, SNES_RESET_BIT);
  77. }
  78. /*
  79. * SD2SNES main loop.
  80. * monitors SRAM changes and other things
  81. */
  82. uint32_t diffcount = 0, samecount = 0, didnotsave = 0;
  83. uint8_t sram_valid = 0;
  84. void snes_main_loop() {
  85. if(!romprops.ramsize_bytes)return;
  86. if(initloop) {
  87. saveram_crc_old = calc_sram_crc(SRAM_SAVE_ADDR, romprops.ramsize_bytes);
  88. initloop=0;
  89. }
  90. saveram_crc = calc_sram_crc(SRAM_SAVE_ADDR, romprops.ramsize_bytes);
  91. sram_valid = sram_reliable();
  92. if(crc_valid && sram_valid) {
  93. if(saveram_crc != saveram_crc_old) {
  94. if(samecount) {
  95. diffcount=1;
  96. } else {
  97. diffcount++;
  98. didnotsave++;
  99. }
  100. samecount=0;
  101. }
  102. if(saveram_crc == saveram_crc_old) {
  103. samecount++;
  104. }
  105. if(diffcount>=1 && samecount==5) {
  106. printf("SaveRAM CRC: 0x%04lx; saving\n", saveram_crc);
  107. writeled(1);
  108. save_sram(file_lfn, romprops.ramsize_bytes, SRAM_SAVE_ADDR);
  109. writeled(0);
  110. didnotsave=0;
  111. }
  112. if(didnotsave>50) {
  113. printf("periodic save (sram contents keep changing...)\n");
  114. diffcount=0;
  115. writeled(1);
  116. save_sram(file_lfn, romprops.ramsize_bytes, SRAM_SAVE_ADDR);
  117. didnotsave=0;
  118. writeled(0);
  119. }
  120. saveram_crc_old = saveram_crc;
  121. }
  122. 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);
  123. }
  124. /*
  125. * SD2SNES menu loop.
  126. * monitors menu selection. return when selection was made.
  127. */
  128. uint8_t menu_main_loop() {
  129. uint8_t cmd = 0;
  130. sram_writebyte(0, SRAM_CMD_ADDR);
  131. while(!cmd) {
  132. if(!get_snes_reset()) {
  133. while(!sram_reliable())printf("hurr\n");
  134. cmd = sram_readbyte(SRAM_CMD_ADDR);
  135. }
  136. if(get_snes_reset()) {
  137. cmd = 0;
  138. }
  139. sleep_ms(20);
  140. cli_entrycheck();
  141. }
  142. return cmd;
  143. }
  144. void get_selected_name(uint8_t* fn) {
  145. uint32_t addr;
  146. addr = sram_readlong(SRAM_PARAM_ADDR);
  147. printf("fd addr=%lx\n", addr);
  148. sram_readblock(fn, addr + 7 + SRAM_MENU_ADDR, 256);
  149. }
  150. void snes_bootprint(void* msg) {
  151. set_mcu_ovr(1);
  152. sram_writeblock(msg, SRAM_CMD_ADDR, 33);
  153. set_mcu_ovr(0);
  154. delay_ms(30);
  155. set_mcu_ovr(1);
  156. }
  157. void snes_menu_errmsg(int err, void* msg) {
  158. set_mcu_ovr(1);
  159. sram_writeblock(msg, SRAM_CMD_ADDR+1, 64);
  160. sram_writebyte(err, SRAM_CMD_ADDR);
  161. }