sram.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #ifndef __SRAM_H__
  21. #define __SRAM_H__
  22. #include <stdlib.h>
  23. #include <stdint.h>
  24. #include <avr/io.h>
  25. /* ---------------------------- PORT A ---------------------------- */
  26. #define AVR_DATA_PORT PORTA
  27. #define AVR_DATA_DIR DDRA
  28. #define AVR_DATA_PIN PINA
  29. #define avr_data_in() ((AVR_DATA_DIR = 0x00),\
  30. (AVR_DATA_PORT = 0x00))
  31. #define avr_data_out() (AVR_DATA_DIR = 0xff)
  32. /* ---------------------------- PORT B ---------------------------- */
  33. #define AVR_PORT PORTB
  34. #define AVR_DIR DDRB
  35. #define AVR_RD_PORT PORTB
  36. #define AVR_RD_DIR DDRB
  37. #define AVR_RD_PIN PB2
  38. #define avr_rd_hi() (AVR_RD_PORT |= (1 << AVR_RD_PIN))
  39. #define avr_rd_lo() (AVR_RD_PORT &= ~(1 << AVR_RD_PIN))
  40. #define AVR_WR_PORT PORTB
  41. #define AVR_WR_DIR DDRB
  42. #define AVR_WR_PIN PB1
  43. #define avr_wr_hi() (AVR_WR_PORT |= (1 << AVR_WR_PIN))
  44. #define avr_wr_lo() (AVR_WR_PORT &= ~(1 << AVR_WR_PIN))
  45. #define AVR_CS_PORT PORTB
  46. #define AVR_CS_DIR DDRB
  47. #define AVR_CS_PIN PB0
  48. #define avr_cs_hi() (AVR_CS_PORT |= (1 << AVR_CS_PIN))
  49. #define avr_cs_lo() (AVR_CS_PORT &= ~(1 << AVR_CS_PIN))
  50. #define SNES_IRQ_PORT PORTB
  51. #define SNES_IRQ_DIR DDRB
  52. #define SNES_IRQ_PIN PB3
  53. #define snes_irq_on() (SNES_IRQ_DIR |= (1 << SNES_IRQ_PIN))
  54. #define snes_irq_hi() (SNES_IRQ_PORT |= (1 << SNES_IRQ_PIN))
  55. #define snes_irq_off() (SNES_IRQ_DIR &= ~(1 << SNES_IRQ_PIN))
  56. #define snes_irq_lo() (SNES_IRQ_PORT &= ~(1 << SNES_IRQ_PIN))
  57. /* ---------------------------- PORT C ---------------------------- */
  58. #define AVR_ADDR_PORT PORTC
  59. #define AVR_ADDR_DIR DDRC
  60. #define AVR_ADDR_LATCH_PORT PORTC
  61. #define AVR_ADDR_LATCH_DIR DDRC
  62. #define AVR_ADDR_LATCH_PIN PC6
  63. #define avr_addr_latch_hi() (AVR_ADDR_LATCH_PORT |= (1 << AVR_ADDR_LATCH_PIN)))
  64. #define avr_addr_latch_lo() (AVR_ADDR_LATCH_PORT &= ~(1 << AVR_ADDR_LATCH_PIN)))
  65. #define AVR_ADDR_SCK_PORT PORTC
  66. #define AVR_ADDR_SCK_DIR DDRC
  67. #define AVR_ADDR_SCK_PIN PC5
  68. #define avr_addr_sck_hi() (AVR_ADDR_SCK_PORT |= (1 << AVR_ADDR_SCK_PIN)))
  69. #define avr_addr_sck_lo() (AVR_ADDR_SCK_PORT &= ~(1 << AVR_ADDR_SCK_PIN)))
  70. #define AVR_ADDR_SER_PORT PORTC
  71. #define AVR_ADDR_SER_DIR DDRC
  72. #define AVR_ADDR_SER_PIN PC4
  73. #define avr_addr_ser_hi() (AVR_ADDR_SER_PORT |= (1 << AVR_ADDR_SER_PIN)))
  74. #define avr_addr_ser_lo() (AVR_ADDR_SER_PORT &= ~(1 << AVR_ADDR_SER_PIN)))
  75. #define AVR_ADDR_LOAD_PORT PORTC
  76. #define AVR_ADDR_LOAD_DIR DDRC
  77. #define AVR_ADDR_LOAD_PIN PC2
  78. #define counter_load() ((AVR_ADDR_LOAD_PORT &= ~(1 << AVR_ADDR_LOAD_PIN)),\
  79. (AVR_ADDR_LOAD_PORT |= (1 << AVR_ADDR_LOAD_PIN)))
  80. #define AVR_BTLDR_EN_PORT PORTC
  81. #define AVR_BTLDR_EN_DIR DDRC
  82. #define AVR_BTLDR_EN_PIN PC1
  83. #define btldr_down() ((AVR_BTLDR_EN_PORT &= ~(1 << AVR_BTLDR_EN_PIN)),\
  84. (AVR_BTLDR_EN_PORT |= (1 << AVR_BTLDR_EN_PIN)))
  85. #define AVR_ADDR_UP_PORT PORTC
  86. #define AVR_ADDR_UP_DIR DDRC
  87. #define AVR_ADDR_UP_PIN PC0
  88. #define counter_up() ((AVR_ADDR_UP_PORT &= ~(1 << AVR_ADDR_UP_PIN)),\
  89. (AVR_ADDR_UP_PORT |= (1 << AVR_ADDR_UP_PIN)))
  90. #define SNES_WR_PORT PORTC
  91. #define SNES_WR_DIR DDRC
  92. #define SNES_WR_PIN PC3
  93. #define LED_PORT PORTC
  94. #define LED_DIR DDRC
  95. #define LED_PIN PC7
  96. #define led_on() ((LED_PORT &=~ (1 << LED_PIN)),\
  97. (LED_DIR &=~ (1 << LED_PIN)))
  98. #define led_off() ((LED_PORT &=~ (1 << LED_PIN)),\
  99. (LED_DIR |= (1 << LED_PIN)))
  100. /* ---------------------------- PORT D ---------------------------- */
  101. #define AVR_SNES_PORT PORTD
  102. #define AVR_SNES_DIR DDRD
  103. #define AVR_SNES_SW_PORT PORTD
  104. #define AVR_SNES_SW_DIR DDRD
  105. #define AVR_SNES_SW_PIN PD5
  106. #define avr_bus_active() ((AVR_SNES_SW_PORT &= ~(1 << AVR_SNES_SW_PIN)),\
  107. (HI_LOROM_SW_PORT |= (1 << HI_LOROM_SW_PIN)),\
  108. (AVR_CS_DIR |= (1 << AVR_CS_PIN)))
  109. #define snes_bus_active() ((AVR_SNES_SW_PORT |= (1 << AVR_SNES_SW_PIN)),\
  110. (AVR_CS_DIR &= ~(1 << AVR_CS_PIN)),\
  111. (AVR_CS_PORT |= (1 << AVR_CS_PIN)))
  112. #define HI_LOROM_SW_PORT PORTD
  113. #define HI_LOROM_SW_DIR DDRD
  114. #define HI_LOROM_SW_PIN PD6
  115. #define snes_hirom() (HI_LOROM_SW_PORT &= ~(1 << HI_LOROM_SW_PIN))
  116. #define snes_lorom() (HI_LOROM_SW_PORT |= (1 << HI_LOROM_SW_PIN))
  117. #define SNES_WR_EN_PORT PORTD
  118. #define SNES_WR_EN_DIR DDRD
  119. #define SNES_WR_EN_PIN PD7
  120. #define snes_wr_disable() (SNES_WR_EN_PORT &= ~(1 << SNES_WR_EN_PIN))
  121. #define snes_wr_enable() (SNES_WR_EN_PORT |= (1 << SNES_WR_EN_PIN))
  122. #define SNES_RESET_PORT PORTD
  123. #define SNES_RESET_DIR DDRD
  124. #define SNES_RESET_PIN PD3
  125. #define SNES_RESET_INP PIND
  126. #define snes_reset_on() (SNES_RESET_DIR |= (1 << SNES_RESET_PIN))
  127. #define snes_reset_hi() (SNES_RESET_PORT |= (1 << SNES_RESET_PIN))
  128. #define snes_reset_off() (SNES_RESET_DIR &= ~(1 << SNES_RESET_PIN))
  129. #define snes_reset_lo() (SNES_RESET_PORT &= ~(1 << SNES_RESET_PIN))
  130. #define snes_reset_test() ((SNES_RESET_INP & (1 << SNES_RESET_PIN)) == 0)
  131. #define MMC_PORT PORTB
  132. #define MMC_DIR DDRB
  133. #define MMC_MISO_PIN PB6
  134. #define MMC_MOSI_PIN PB5
  135. #define MMC_SCK_PIN PB7
  136. #define MMC_CS_PIN PB4
  137. void system_init(void);
  138. void sreg_set(uint32_t addr);
  139. uint8_t sram_read(uint32_t addr);
  140. void sram_write(uint32_t addr, uint8_t data);
  141. void sram_bulk_read_start(uint32_t addr);
  142. inline void sram_bulk_read_next(void);
  143. inline void sram_bulk_read_end(void);
  144. uint8_t sram_bulk_read(void);
  145. void sram_bulk_write_start(uint32_t addr);
  146. inline void sram_bulk_write_next(void);
  147. inline void sram_bulk_write_end(void);
  148. void sram_bulk_write(uint8_t data);
  149. void sram_bulk_copy_from_buffer(uint32_t addr, uint8_t * src, uint32_t len);
  150. void sram_bulk_copy_into_buffer(uint32_t addr, uint8_t * dst, uint32_t len);
  151. void sram_bulk_set(uint32_t addr, uint32_t len,uint8_t value);
  152. inline void sram_bulk_addr_save();
  153. inline void sram_bulk_addr_restore();
  154. #endif