main.c 7.5 KB

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