system.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * =====================================================================================
  3. *
  4. * ________ .__ __ ________ ____ ________
  5. * \_____ \ __ __|__| ____ | | __\______ \ _______ _/_ |/ _____/
  6. * / / \ \| | \ |/ ___\| |/ / | | \_/ __ \ \/ /| / __ \
  7. * / \_/. \ | / \ \___| < | ` \ ___/\ / | \ |__\ \
  8. * \_____\ \_/____/|__|\___ >__|_ \/_______ /\___ >\_/ |___|\_____ /
  9. * \__> \/ \/ \/ \/ \/
  10. *
  11. * www.optixx.org
  12. *
  13. *
  14. * Version: 1.0
  15. * Created: 07/21/2009 03:32:16 PM
  16. * Author: david@optixx.org
  17. *
  18. * =====================================================================================
  19. */
  20. #include <stdlib.h>
  21. #include <stdint.h>
  22. #include <util/delay.h> /* for _delay_ms() */
  23. #include <avr/interrupt.h>
  24. #include "config.h"
  25. #include "sram.h"
  26. #include "system.h"
  27. #include "uart.h"
  28. #include "debug.h"
  29. #include "info.h"
  30. #include "requests.h"
  31. #include "irq.h"
  32. system_t my_system;
  33. void system_init(void)
  34. {
  35. snes_reset_hi();
  36. snes_reset_off();
  37. my_system.reset_line = RESET_OFF;
  38. snes_irq_hi();
  39. snes_irq_off();
  40. my_system.irq_line = IRQ_OFF;
  41. snes_wr_disable();
  42. my_system.wr_line = WR_DISABLE;
  43. avr_bus_active();
  44. my_system.bus_mode = MODE_AVR;
  45. snes_lorom();
  46. my_system.rom_mode = LOROM;
  47. my_system.snes_reset_count = 0;
  48. my_system.avr_reset_count = 0;
  49. my_system.reset_irq = RESET_IRQ_OFF;
  50. }
  51. void system_send_snes_reset()
  52. {
  53. info_P(PSTR("Reset SNES\n"));
  54. cli();
  55. snes_reset_on();
  56. snes_reset_lo();
  57. _delay_ms(2);
  58. snes_reset_hi();
  59. snes_reset_off();
  60. sei();
  61. my_system.snes_reset_count++;
  62. }
  63. void system_send_snes_irq()
  64. {
  65. snes_irq_on();
  66. snes_irq_lo();
  67. _delay_us(20);
  68. snes_irq_hi();
  69. snes_irq_off();
  70. }
  71. void system_snes_irq_off()
  72. {
  73. snes_irq_off();
  74. my_system.irq_line = IRQ_OFF;
  75. }
  76. void system_snes_irq_on()
  77. {
  78. snes_irq_on();
  79. my_system.irq_line = IRQ_ON;
  80. }
  81. void system_set_bus_avr()
  82. {
  83. avr_bus_active();
  84. info_P(PSTR("Activate AVR bus\n"));
  85. my_system.bus_mode = MODE_AVR;
  86. }
  87. void system_set_wr_disable()
  88. {
  89. snes_wr_disable();
  90. my_system.wr_line = WR_DISABLE;
  91. info_P(PSTR("Disable SNES WR\n"));
  92. }
  93. void system_set_wr_enable()
  94. {
  95. snes_wr_enable();
  96. my_system.wr_line = WR_ENABLE;
  97. info_P(PSTR("Enable SNES WR\n"));
  98. }
  99. void system_set_bus_snes()
  100. {
  101. snes_bus_active();
  102. my_system.bus_mode = MODE_SNES;
  103. info_P(PSTR("Activate SNES bus\n"));
  104. }
  105. void system_set_rom_mode(usb_transaction_t * usb_trans)
  106. {
  107. if (usb_trans->req_bank_size == 0x8000) {
  108. snes_lorom();
  109. my_system.rom_mode = LOROM;
  110. info_P(PSTR("Set SNES lorom \n"));
  111. } else {
  112. snes_hirom();
  113. my_system.rom_mode = HIROM;
  114. info_P(PSTR("Set SNES hirom \n"));
  115. }
  116. }
  117. void system_set_rom_lorom()
  118. {
  119. snes_lorom();
  120. my_system.rom_mode = LOROM;
  121. info_P(PSTR("Set SNES lorom \n"));
  122. }
  123. void system_set_rom_hirom()
  124. {
  125. snes_hirom();
  126. my_system.rom_mode = HIROM;
  127. info_P(PSTR("Set SNES hirom \n"));
  128. }
  129. char *system_status_helper(uint8_t val)
  130. {
  131. if (val)
  132. return "ON";
  133. else
  134. return "OFF";
  135. }
  136. char *system_status_bus(uint8_t val)
  137. {
  138. if (val)
  139. return "SNES";
  140. else
  141. return "AVR";
  142. }
  143. char *system_status_rom(uint8_t val)
  144. {
  145. if (val)
  146. return "HIROM";
  147. else
  148. return "LOROM";
  149. }
  150. void system_status()
  151. {
  152. info_P(PSTR("\nBus Mode %s\n"), system_status_bus(my_system.bus_mode));
  153. info_P(PSTR("Rom Mode %s\n"), system_status_rom(my_system.rom_mode));
  154. info_P(PSTR("Reset Line %s\n"),
  155. system_status_helper(my_system.reset_line));
  156. info_P(PSTR("IRQ Line %s\n"), system_status_helper(my_system.irq_line));
  157. info_P(PSTR("WR Line %s\n"), system_status_helper(my_system.wr_line));
  158. info_P(PSTR("Reset IRQ %s\n"), system_status_helper(my_system.reset_irq));
  159. info_P(PSTR("SNES Reset 0x%02x\n"), my_system.snes_reset_count);
  160. info_P(PSTR("AVR Reset 0x%02x\n"), my_system.avr_reset_count);
  161. }