main.c 8.3 KB

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