main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <stdlib.h>
  4. #include "uart.h"
  5. #include "mmc.h"
  6. #include "fat.h"
  7. // Debug
  8. #define debug(x, fmt) printf("%s:%u: %s=" fmt, __FILE__, __LINE__, #x, x)
  9. extern FILE uart_stdout;
  10. //SREG defines
  11. #define S_MOSI PB5
  12. #define S_MISO PB6
  13. #define S_SCK PB7
  14. #define S_LATCH PB4
  15. //DEBUG defines
  16. #define D_LED0 PD6
  17. //SRAM defines
  18. #define R_WR PB1
  19. #define R_RD PB0
  20. #define RAM_PORT PORTA
  21. #define RAM_DIR DDRA
  22. #define RAM_REG PINA
  23. #define CTRL_PORT PORTB
  24. #define CTR_DIR DDRB
  25. #define LATCH_PORT PORTB
  26. #define LATCH_DIR DDRB
  27. #define SPI_PORT PORTB
  28. #define SPI_DIR DDRB
  29. #define LED_PORT PORTD
  30. #define LED_DIR DDRD
  31. #define FILENAME "sprite.raw"
  32. #define BUFFER_SIZE 512
  33. #define BLOCKS 512
  34. #define MEMSIZE 0x80000
  35. uint8_t read_buffer[BUFFER_SIZE];
  36. void dump_packet(uint32_t addr,uint32_t len,uint8_t *packet){
  37. uint16_t i,j;
  38. uint16_t sum = 0;
  39. uint8_t clear=0;
  40. for (i=0;i<len;i+=16) {
  41. sum = 0;
  42. for (j=0;j<16;j++) {
  43. sum +=packet[i+j];
  44. }
  45. if (!sum){
  46. clear=1;
  47. continue;
  48. }
  49. if (clear){
  50. printf("*\n");
  51. clear = 0;
  52. }
  53. printf("%08lx:", addr + i);
  54. for (j=0;j<16;j++) {
  55. printf(" %02x", packet[i+j]);
  56. }
  57. printf(" |");
  58. for (j=0;j<16;j++) {
  59. if (packet[i+j]>=33 && packet[i+j]<=126 )
  60. printf("%c", packet[i+j]);
  61. else
  62. printf(".");
  63. }
  64. printf("|\n");
  65. }
  66. }
  67. void spi_init(void)
  68. {
  69. /* Set MOSI and SCK output, all others input */
  70. SPI_DIR |= ((1<<S_MOSI) | (1<<S_SCK) | (1<<S_LATCH));
  71. SPI_DIR &= ~(1<<S_MISO);
  72. SPI_PORT |= (1<<S_MISO);
  73. /* Enable SPI, Master*/
  74. SPCR = ((1<<SPE) | (1<<MSTR));
  75. }
  76. void spi_master_transmit(unsigned char cData)
  77. {
  78. /* Start transmission */
  79. SPDR = cData;
  80. /* Wait for transmission complete */
  81. while(!(SPSR & (1<<SPIF)));
  82. }
  83. uint8_t sram_read(uint32_t addr)
  84. {
  85. uint8_t byte;
  86. RAM_DIR = 0x00;
  87. RAM_PORT = 0xff;
  88. CTRL_PORT |= (1<<R_RD);
  89. CTRL_PORT |= (1<<R_WR);
  90. spi_master_transmit((uint8_t)(addr>>16));
  91. spi_master_transmit((uint8_t)(addr>>8));
  92. spi_master_transmit((uint8_t)(addr>>0));
  93. LATCH_PORT |= (1<<S_LATCH);
  94. LATCH_PORT &= ~(1<<S_LATCH);
  95. CTRL_PORT &= ~(1<<R_RD);
  96. asm volatile ("nop");
  97. asm volatile ("nop");
  98. asm volatile ("nop");
  99. asm volatile ("nop");
  100. asm volatile ("nop");
  101. asm volatile ("nop");
  102. asm volatile ("nop");
  103. asm volatile ("nop");
  104. byte = RAM_REG;
  105. CTRL_PORT |= (1<<R_RD);
  106. RAM_DIR =0x00;
  107. RAM_PORT =0x00;
  108. return byte;
  109. }
  110. void sram_write(uint32_t addr, uint8_t data)
  111. {
  112. RAM_DIR = 0xff;
  113. CTRL_PORT |= (1<<R_RD);
  114. CTRL_PORT |= (1<<R_WR);
  115. spi_master_transmit((uint8_t)(addr>>16));
  116. spi_master_transmit((uint8_t)(addr>>8));
  117. spi_master_transmit((uint8_t)(addr>>0));
  118. LATCH_PORT |= (1<<S_LATCH);
  119. LATCH_PORT &= ~(1<<S_LATCH);
  120. CTRL_PORT &= ~(1<<R_WR);
  121. RAM_PORT = data;
  122. CTRL_PORT |= (1<<R_WR);
  123. RAM_DIR = 0x00;
  124. RAM_PORT = 0x00;
  125. }
  126. void sram_init(void){
  127. RAM_DIR = 0x00;
  128. RAM_PORT = 0x00;
  129. CTR_DIR |= ((1<<R_WR) | (1<<R_RD));
  130. CTRL_PORT |= (1<<R_RD);
  131. CTRL_PORT |= (1<<R_WR);
  132. LED_PORT |= (1<<D_LED0);
  133. }
  134. void sram_snes_mode(void){
  135. CTRL_PORT |= (1<<R_WR);
  136. CTRL_PORT &= ~(1<<R_RD);
  137. }
  138. void sram_clear(uint32_t addr, uint32_t len){
  139. uint32_t i;
  140. for (i=addr; i<(addr + len);i++ ){
  141. if (0==i%0xfff)
  142. printf("sram_clear %lx\n\r",i);
  143. sram_write(i, 0x00);
  144. }
  145. }
  146. void sram_copy(uint32_t addr,uint8_t *src, uint32_t len){
  147. uint32_t i;
  148. uint8_t *ptr = src;
  149. for (i=addr; i<(addr + len);i++ )
  150. sram_write(i, *ptr++);
  151. }
  152. void sram_read_buffer(uint32_t addr,uint8_t *dst, uint32_t len){
  153. uint32_t i;
  154. uint8_t *ptr = dst;
  155. for (i=addr; i<(addr + len);i++ ){
  156. *ptr = sram_read(i);
  157. ptr++;
  158. }
  159. }
  160. uint8_t sram_check(uint8_t *buffer, uint32_t len){
  161. for (uint16_t cnt=0; cnt<len; cnt++)
  162. if (buffer[cnt])
  163. return 1;
  164. return 0;
  165. }
  166. int main(void)
  167. {
  168. uint16_t fat_cluster = 0;
  169. uint8_t fat_attrib = 0;
  170. uint32_t fat_size = 0;
  171. uint32_t rom_addr = 0;
  172. uint32_t skip_block = 0;
  173. uart_init();
  174. stdout = &uart_stdout;
  175. sram_init();
  176. printf("SRAM Init\n");
  177. spi_init();
  178. printf("SPI Init\n");
  179. //sram_clear(0x000000, 0x400000);
  180. //printf("sram_clear\n");
  181. //printf("read 0x0f0f\n");
  182. //sram_read(0x0f0f);
  183. //printf("write 0x0f0f\n");
  184. //sram_write(0x0f0f,0xaa);
  185. //while(1);
  186. while ( mmc_init() !=0) {
  187. printf("No sdcard...\n");
  188. }
  189. printf("MMC Init sone\n");
  190. fat_init(read_buffer);
  191. printf("FAT Init done.\n");
  192. rom_addr = 0x000000;
  193. printf("Look for %s\n",FILENAME);
  194. if (fat_search_file((uint8_t*)FILENAME,
  195. &fat_cluster,
  196. &fat_size,
  197. &fat_attrib,
  198. read_buffer) == 1) {
  199. for (uint16_t block_cnt=0; block_cnt<BLOCKS; block_cnt++) {
  200. fat_read_file (fat_cluster,read_buffer,block_cnt);
  201. if (block_cnt % 16 == 0)
  202. printf("Write Ram 0x%lx Skipped %li\n",rom_addr,skip_block);
  203. if (sram_check(read_buffer,512))
  204. sram_copy(rom_addr,read_buffer,512);
  205. else
  206. skip_block +=1;
  207. rom_addr += 512;
  208. }
  209. printf("Done 0x%lx Skipped %li\n",rom_addr,skip_block);
  210. }
  211. printf("Dump Memory\n\r");
  212. rom_addr = 0x000000;
  213. for (uint16_t block_cnt=0; block_cnt<BLOCKS; block_cnt++) {
  214. sram_read_buffer(rom_addr,read_buffer,512);
  215. dump_packet(rom_addr,512,read_buffer);
  216. rom_addr += 512;
  217. }
  218. printf("\nDone 0x%lx\n",rom_addr);
  219. sram_snes_mode();
  220. printf("\nEnter Snes mode\n");
  221. while(1);
  222. return 0 ;
  223. }