snes.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <avr/io.h>
  21. #include "avrcompat.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. uint8_t initloop=1;
  31. uint32_t saveram_crc, saveram_crc_old;
  32. void snes_init() {
  33. DDRD |= _BV(PD5); // PD5 = RESET_DIR
  34. DDRD |= _BV(PD6); // PD6 = RESET
  35. snes_reset(1);
  36. }
  37. /*
  38. * sets the SNES reset state.
  39. *
  40. * state: put SNES in reset state when 1, release when 0
  41. */
  42. void snes_reset(int state) {
  43. if(state) {
  44. DDRD |= _BV(PD6); // /RESET pin -> out
  45. PORTD &= ~_BV(PD6); // /RESET = 0
  46. PORTD |= _BV(PD5); // RESET_DIR = 1;
  47. } else {
  48. PORTD &= ~_BV(PD5); // RESET_DIR = 0;
  49. DDRD &= ~_BV(PD6); // /RESET pin -> in
  50. PORTD |= _BV(PD6); // /RESET = pullup
  51. }
  52. }
  53. /*
  54. * gets the SNES reset state.
  55. *
  56. * returns: 1 when reset, 0 when not reset
  57. */
  58. uint8_t get_snes_reset() {
  59. // DDRD &= ~_BV(PD6); // /RESET pin -> in
  60. // PORTD &= ~_BV(PD5); // RESET_DIR (external buffer) = 0
  61. return !(PIND & _BV(PD6));
  62. }
  63. /*
  64. * SD2SNES main loop.
  65. * monitors SRAM changes and other things
  66. */
  67. uint32_t diffcount = 0, samecount = 0, didnotsave = 0;
  68. uint8_t sram_valid = 0;
  69. void snes_main_loop() {
  70. if(!romprops.ramsize_bytes)return;
  71. if(initloop) {
  72. saveram_crc_old = calc_sram_crc(SRAM_SAVE_ADDR, romprops.ramsize_bytes);
  73. initloop=0;
  74. }
  75. saveram_crc = calc_sram_crc(SRAM_SAVE_ADDR, romprops.ramsize_bytes);
  76. sram_valid = sram_reliable();
  77. if(crc_valid && sram_valid) {
  78. if(saveram_crc != saveram_crc_old) {
  79. if(samecount) {
  80. diffcount=1;
  81. } else {
  82. diffcount++;
  83. didnotsave++;
  84. }
  85. samecount=0;
  86. }
  87. if(saveram_crc == saveram_crc_old) {
  88. samecount++;
  89. }
  90. if(diffcount>=1 && samecount==5) {
  91. uart_putc('U');
  92. uart_puthexshort(saveram_crc);
  93. uart_putcrlf();
  94. set_busy_led(1);
  95. save_sram(file_lfn, romprops.ramsize_bytes, SRAM_SAVE_ADDR);
  96. set_busy_led(0);
  97. didnotsave=0;
  98. }
  99. if(didnotsave>50) {
  100. diffcount=0;
  101. uart_putc('V');
  102. set_busy_led(1);
  103. save_sram(file_lfn, romprops.ramsize_bytes, SRAM_SAVE_ADDR);
  104. didnotsave=0;
  105. set_busy_led(0);
  106. }
  107. saveram_crc_old = saveram_crc;
  108. }
  109. dprintf("crc_valid=%d sram_valid=%d diffcount=%ld samecount=%ld, didnotsave=%ld\n", crc_valid, sram_valid, diffcount, samecount, didnotsave);
  110. }
  111. /*
  112. * SD2SNES menu loop.
  113. * monitors menu selection. return when selection was made.
  114. */
  115. uint8_t menu_main_loop() {
  116. uint8_t cmd = 0;
  117. sram_writebyte(0, SRAM_CMD_ADDR);
  118. while(!cmd) {
  119. if(!get_snes_reset()) {
  120. while(!sram_reliable());
  121. cmd = sram_readbyte(SRAM_CMD_ADDR);
  122. }
  123. if(get_snes_reset()) {
  124. cmd = 0;
  125. }
  126. }
  127. return cmd;
  128. }
  129. void get_selected_name(uint8_t* fn) {
  130. uint32_t addr = sram_readlong(SRAM_FD_ADDR);
  131. dprintf("fd addr=%lX\n", addr);
  132. sram_readblock(fn, addr+0x41+SRAM_MENU_ADDR, 256);
  133. }