main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "ff.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "data.h";
  6. #include "pad.h";
  7. #include "event.h";
  8. #include "myEvents.h";
  9. #include "ressource.h";
  10. #include "PPU.h"
  11. #include "debug.h"
  12. #include "crc.h"
  13. #define ROM_NAME "TURRICAN.SMC"
  14. #define BLOCK_SIZE 512
  15. #define BANK_SIZE 32768L
  16. #define BANK_COUNT 16
  17. #define BASE_ADDR 0x008000
  18. typedef void (*FUNC) (void);
  19. padStatus pad1;
  20. DWORD acc_size; /* Work register for fs command */
  21. WORD acc_files, acc_dirs;
  22. FILINFO finfo;
  23. FATFS fatfs[2]; /* File system object for each logical * drive */
  24. // BYTE Buff[512]; /* Working buffer */
  25. DWORD p1, p2, p3;
  26. DWORD addr;
  27. DWORD crc_addr;
  28. UINT crc;
  29. BYTE res, bank;
  30. WORD w1;
  31. UINT s1, s2, cnt;
  32. FATFS *fs;
  33. DIR dir; /* Directory object */
  34. FIL file1, file2;
  35. void initInternalRegisters(void)
  36. {
  37. characterLocation[0] = 0x0000;
  38. characterLocation[1] = 0x0000;
  39. characterLocation[2] = 0x0000;
  40. characterLocation[3] = 0x0000;
  41. debug_init();
  42. }
  43. void preInit(void)
  44. {
  45. // For testing purpose ...
  46. // Insert code here to be executed before register init
  47. } DWORD get_fattime()
  48. {
  49. time_t rawtime;
  50. struct tm *ptm;
  51. // time ( &rawtime );
  52. ptm = gmtime(&rawtime);
  53. return ((DWORD) (ptm->tm_year - 80) << 25)
  54. | ((DWORD) (ptm->tm_mon + 1) << 21)
  55. | ((DWORD) ptm->tm_mday << 16)
  56. | ((DWORD) ptm->tm_hour << 11)
  57. | ((DWORD) ptm->tm_min << 5) | ((DWORD) ptm->tm_sec >> 1);
  58. }
  59. void halt(void)
  60. {
  61. while (1);
  62. }
  63. void put_rc(FRESULT rc)
  64. {
  65. const char *p;
  66. static const char str[] =
  67. "OK\0" "NOT_READY\0" "NO_FILE\0" "NO_PATH\0" "INVALID_NAME\0"
  68. "INVALID_DRIVE\0" "DENIED\0" "EXIST\0" "RW_ERROR\0"
  69. "WRITE_PROTECTED\0" "NOT_ENABLED\0" "NO_FILESYSTEM\0"
  70. "INVALID_OBJECT\0" "MKFS_ABORTED\0";
  71. FRESULT i;
  72. for (p = str, i = 0; i != rc && *p; i++) {
  73. while (*p++);
  74. }
  75. printfc("rc=%u FR_%s\n", (WORD) rc, p);
  76. }
  77. FRESULT scan_files(char *path)
  78. {
  79. DIR dirs;
  80. FRESULT res;
  81. int i;
  82. if ((res = f_opendir(&dirs, path)) == FR_OK) {
  83. i = strlen(path);
  84. while (((res = f_readdir(&dirs, &finfo)) == FR_OK)
  85. && finfo.fname[0]) {
  86. if (finfo.fattrib & AM_DIR) {
  87. acc_dirs++;
  88. *(path + i) = '/';
  89. strcpy(path + i + 1, &finfo.fname[0]);
  90. res = scan_files(path);
  91. *(path + i) = '\0';
  92. if (res != FR_OK)
  93. break;
  94. } else {
  95. acc_files++;
  96. acc_size += finfo.fsize;
  97. }
  98. }
  99. }
  100. return res;
  101. }
  102. void wait(void)
  103. {
  104. printfc("SNES::wait: press A to continue\n");
  105. enablePad();
  106. pad1 = readPad((byte) 0);
  107. while (!pad1.A) {
  108. waitForVBlank();
  109. pad1 = readPad((byte) 0);
  110. }
  111. printfc("SNES::wait: done\n");
  112. }
  113. void boot(DWORD addr)
  114. {
  115. FUNC fn;
  116. printfc("SNES::boot addr=%lx\n", addr);
  117. fn = (FUNC) addr;
  118. fn();
  119. } void main(void)
  120. {
  121. word i, j;
  122. BYTE res;
  123. initInternalRegisters();
  124. *(byte *) 0x2105 = 0x01; // MODE 1 value
  125. *(byte *) 0x212c = 0x01; // Plane 0 (bit one) enable register
  126. *(byte *) 0x212d = 0x00; // All subPlane disable
  127. *(byte *) 0x2100 = 0x0f; // enable background
  128. debug_enable();
  129. printfc("SNES::main: Try to init disk\n");
  130. put_rc(f_mount((BYTE) 0, &fatfs[0]));
  131. printfs(0, "FATFS OPTIXX.ORG ");
  132. printfc("SNES::main: Try to get free\n");
  133. res = f_getfree("", &p2, &fs);
  134. if (res)
  135. put_rc(res);
  136. printfc("SNES::main: printf fs results\n");
  137. printfs(1,
  138. "FAT TYPE = %u\nBYTES/CLUSTER = %lu\nNUMBER OF FATS = %u\n"
  139. "ROOT DIR ENTRIES = %u\nSECTORS/FAT = %lu\nNUMBER OF CLUSTERS = %lu\n"
  140. "FAT START = %lu\nDIR START LBA,CLUSTER = %lu\nDATA START LBA = %lu\n",
  141. (WORD) fs->fs_type, (DWORD) fs->csize * 512,
  142. (WORD) fs->n_fats, fs->n_rootdir, (DWORD) fs->sects_fat,
  143. (DWORD) fs->max_clust - 2, fs->fatbase, fs->dirbase, fs->database);
  144. acc_size = acc_files = acc_dirs = 0;
  145. printfc("SNES::main: scan files\n");
  146. res = scan_files("");
  147. if (res)
  148. put_rc(res);
  149. printfs(12, "%u FILES, %lu BYTES\n%u FOLDERS\n"
  150. "%lu KB TOTAK DISK SPACE\n%lu KB AVAILABLE\n", acc_files,
  151. acc_size, acc_dirs, (fs->max_clust - 2) * (fs->csize / 2),
  152. p2 * (fs->csize / 2));
  153. res = f_opendir(&dir, "");
  154. if (res)
  155. put_rc(res);
  156. p1 = s1 = s2 = 0;
  157. cnt = 0;
  158. wait();
  159. clears();
  160. #if 0
  161. printfc("SNES::main: read dir\n");
  162. for (;;) {
  163. res = f_readdir(&dir, &finfo);
  164. if ((res != FR_OK) || !finfo.fname[0])
  165. break;
  166. if (finfo.fattrib & AM_DIR) {
  167. s2++;
  168. } else {
  169. s1++;
  170. p1 += finfo.fsize;
  171. }
  172. ;
  173. printfs(cnt, "%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu\n%s\n",
  174. (finfo.fattrib & AM_DIR) ? 'D' : '-',
  175. (finfo.fattrib & AM_RDO) ? 'R' : '-',
  176. (finfo.fattrib & AM_HID) ? 'H' : '-',
  177. (finfo.fattrib & AM_SYS) ? 'S' : '-',
  178. (finfo.fattrib & AM_ARC) ? 'A' : '-',
  179. (finfo.fdate >> 9) + 1980, (finfo.fdate >> 5) & 15,
  180. finfo.fdate & 31, (finfo.ftime >> 11),
  181. (finfo.ftime >> 5) & 63, finfo.fsize, &(finfo.fname[0]));
  182. cnt += 2;
  183. /*
  184. * printfs(cnt,"%u/%02u/%02u %02u:%02u %9lu\n%s\n", (finfo.fdate >> 9) + 1980, (finfo.fdate >> 5) & 15, finfo.fdate & 31,
  185. * (finfo.ftime >> 11), (finfo.ftime >> 5) & 63, finfo.fsize, &(finfo.fname[0]));
  186. */
  187. if (cnt && cnt == 20) {
  188. cnt = 0;
  189. wait();
  190. clears();
  191. }
  192. }
  193. printfs(20, "%4u FILES\n%10lu BYTES TOTAL\n%4u DIRS", s1, p1, s2);
  194. if (f_getfree("", &p1, &fs) == FR_OK)
  195. printfs(23, "%10luK BYTES FREE\n", p1 * fs->csize / 2);
  196. wait();
  197. clears();
  198. #endif /* */
  199. printfc("SNES::main: open %s \n", ROM_NAME);
  200. printfs(0, "OPEN %s", ROM_NAME);
  201. put_rc(f_open(&file1, ROM_NAME, (BYTE) FA_READ));
  202. p1 = BANK_SIZE * BANK_COUNT;
  203. p2 = 0;
  204. p3 = 0;
  205. cnt = 0;
  206. bank = 0;
  207. addr = BASE_ADDR;
  208. crc_addr = BASE_ADDR;
  209. while (p1) {
  210. cnt = BLOCK_SIZE;
  211. p1 -= BLOCK_SIZE;
  212. res = f_read(&file1, (byte *) (addr), cnt, &s2);
  213. if (res != FR_OK) {
  214. printfc("SNES::main: read failed\n");
  215. put_rc(res);
  216. break;
  217. }
  218. p2 += s2;
  219. if (cnt != s2) {
  220. printfc("SNES::main: read cnt=%i s2=%i\n", cnt, s2);
  221. break;
  222. }
  223. #if 0
  224. printc_packet(addr, 512, (byte *) (addr));
  225. wait();
  226. #endif
  227. addr += s2;
  228. if (addr % 0x10000 == 0) {
  229. printfc("SNES::main: read cnt=%i p1=%li p2=%li s2=%i \n", cnt,
  230. p1, p2, s2);
  231. #if 0
  232. crc = crc_update_mem(crc_addr, 0x8000);
  233. printfc("SNES::main: crc_addr=%lx crc=%x\n", crc_addr, crc);
  234. crc_addr += 0x8000;
  235. #endif
  236. printfs((1 + bank), "BANK %i ADDR 0X%lx ", bank, addr);
  237. addr += 0x8000;
  238. bank++;
  239. }
  240. }
  241. put_rc(f_close(&file1));
  242. addr = *(byte *) 0xFFFD;
  243. addr = addr << 8;
  244. addr |= (*(byte *) 0xFFFC);
  245. boot(addr);
  246. while (1) {
  247. wait();
  248. }
  249. while (1);
  250. }
  251. void IRQHandler(void)
  252. {
  253. }
  254. void NMIHandler(void)
  255. {
  256. // processEvents();
  257. }