system.c 4.0 KB

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