main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*
  2. * Main application source file - The peTI-NESulator Project
  3. * main.c
  4. *
  5. * Created by Manoël Trapier.
  6. * Copyright (c) 2002-2019 986-Studio.
  7. *
  8. */
  9. /* System includes */
  10. #if !defined(__TIGCC__) && !defined(__GCC4TI__) && !defined(__GTC__)
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <signal.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <sys/time.h>
  18. #include <time.h>
  19. #include <sys/mman.h>
  20. #include <ctype.h>
  21. #include <GLFW/glfw3.h>
  22. #else
  23. #define TIGCC_COMPAT
  24. #include <tigcclib.h>
  25. #endif
  26. /* peTI-NESulator modules includes */
  27. #include <os_dependent.h>
  28. #include <corecpu.h>
  29. #include <ppu/ppu.h>
  30. #include <NESCarts.h>
  31. #include <paddle.h>
  32. #include <mappers/manager.h>
  33. #include <memory/manager.h>
  34. #include <plugins/manager.h>
  35. #include <apu/apu.h>
  36. #if ISPAL && !ISNTSC
  37. int VBLANK_TIME = 70;
  38. int HBLANK_TIME = 103;
  39. double APU_BASEFREQ = 1.7734474;
  40. #elif !ISPAL && ISNTSC
  41. int VBLANK_TIME = 20;
  42. int HBLANK_TIME = 113;
  43. double APU_BASEFREQ = 1.7897725;
  44. #elif !ISPAL && !ISNTSC
  45. # error You MUST define one of ISPAL Xor ISNTSC
  46. #else
  47. # error Cannot use ISPAL with ISNTSC together !
  48. #endif
  49. //#define MEMORY_TEST
  50. /* peTI-NESulator Version */
  51. #if !defined(V_MAJOR) || !defined(V_MINOR) || !defined(V_MICRO)
  52. #error Something wrong with your building tools
  53. #endif
  54. #ifndef V_TEXT
  55. #define V_TEXT ""
  56. #endif
  57. #ifdef USE_SOUND
  58. #undef USE_SOUND
  59. #endif
  60. /*
  61. #define MAXLASTOP 42
  62. word latestop[MAXLASTOP];
  63. */
  64. /* NES specific variables */
  65. quick6502_cpu *MainCPU;
  66. NesCart *Cart;
  67. uint8_t *FDSRom;
  68. uint8_t *FDSRam;
  69. /* Command line options */
  70. uint8_t START_DEBUG = 0;
  71. uint8_t START_WITH_FDS = 0;
  72. char *CART_FILENAME = NULL;
  73. char *PALETTE_FILENAME = NULL;
  74. Paddle P1, P2;
  75. uint16_t ScanLine;
  76. volatile int frame = 0;
  77. volatile uint64_t ccount;
  78. char MapperWantIRQ = 0;
  79. char WantClosing = 0;
  80. struct timeval timeStart;
  81. struct timeval timeEnd;
  82. volatile uint32_t FPS, IPS;
  83. short IRQScanHit = -1;
  84. short SZHit = -1;
  85. /* palette */
  86. uint32_t ColorPalette[8 * 63];
  87. #define SET_RGB(_r, _g, _b) (((_r) << 16) | ((_g) << 8) | (_b) | 0xFF000000)
  88. /* Memory functions */
  89. uint8_t MemoryRead(uint16_t Addr);
  90. uint8_t MemoryOpCodeRead(uint16_t Addr);
  91. uint8_t MemoryStackRead(uint16_t Addr);
  92. uint8_t MemoryPageZeroRead(uint16_t Addr);
  93. void MemoryWrite(uint16_t Addr, uint8_t Value);
  94. void MemoryStackWrite(uint16_t Addr, uint8_t Value);
  95. void MemoryPageZeroWrite(uint16_t Addr, uint8_t Value);
  96. void Loop6502(quick6502_cpu *R);
  97. void CloseHook(void)
  98. {
  99. WantClosing = 1;
  100. }
  101. void SaveSaveRam(char *name)
  102. {
  103. FILE *fp;
  104. int i;
  105. char fname[512];
  106. strcpy(fname, name);
  107. strcat(fname, ".svt");
  108. if ((fp = fopen(fname, "wb")))
  109. {
  110. console_printf(Console_Default, "Saving savestate '%s'\n", fname);
  111. for (i = 0x60 ; i < 0x80 ; i++)
  112. {
  113. fwrite(get_page_ptr(i), 1, 0x100, fp);
  114. }
  115. fclose(fp);
  116. }
  117. }
  118. void LoadSaveRam(char *name)
  119. {
  120. FILE *fp;
  121. int i;
  122. char fname[512];
  123. strcpy(fname, name);
  124. strcat(fname, ".svt");
  125. if ((fp = fopen(fname, "rb")))
  126. {
  127. console_printf(Console_Default, "Loading savestate '%s'\n", fname);
  128. for (i = 0x60 ; i < 0x80 ; i++)
  129. {
  130. fread(get_page_ptr(i), 1, 0x0100, fp);
  131. }
  132. fclose(fp);
  133. }
  134. }
  135. void LoadPalette(char *filename, Palette *pal)
  136. {
  137. FILE *fp;
  138. uint8_t r, v, b, i;
  139. console_printf(Console_Default, "%s: try to load pallette file '%s'", __func__, filename);
  140. if ((fp = fopen(filename, "rb")) != NULL)
  141. {
  142. for (i = 0 ; i < 64 ; i++)
  143. {
  144. fread(&r, 1, 1, fp);
  145. fread(&v, 1, 1, fp);
  146. fread(&b, 1, 1, fp);
  147. /* r = (r * 64) / 255;
  148. v = (v * 64) / 255;
  149. b = (b * 64) / 255;*/
  150. #ifdef USE_24BITS
  151. ColorPalette[i + (0 * 63)] = SET_RGB(r,v,b);
  152. /* Red emphase */
  153. ColorPalette[i + (1 * 63)] = SET_RGB(r + 10, v - 05, b - 05);
  154. /* Green emphase */
  155. ColorPalette[i + (2 * 63)] = SET_RGB(r - 05, v + 10, b - 05);
  156. /* Red + green emphase */
  157. ColorPalette[i + (3 * 63)] = SET_RGB(r + 05, v + 05, b - 10);
  158. /* Blue emphase */
  159. ColorPalette[i + (4 * 63)] = SET_RGB(r - 05, v - 05, b + 10);
  160. /* Red + blue emphase */
  161. ColorPalette[i + (5 * 63)] = SET_RGB(r + 05, v - 10, b + 05);
  162. /* Blue + green emphase */
  163. ColorPalette[i + (6 * 63)] = SET_RGB(r - 10, v + 05, b + 05);
  164. /* Red + Green + Blue emphase */
  165. ColorPalette[i + (7 * 63)] = SET_RGB(r + 00, v + 00, b + 00);
  166. #else /* Else Use 8Bits */
  167. pal[i].r = r;
  168. pal[i].g = v;
  169. pal[i].b = b;
  170. pal[i + 64].r = r;
  171. pal[i + 64].g = v;
  172. pal[i + 64].b = b;
  173. pal[i + 128].r = r;
  174. pal[i + 128].g = v;
  175. pal[i + 128].b = b;
  176. pal[i + 192].r = r;
  177. pal[i + 192].g = v;
  178. pal[i + 192].b = b;
  179. #endif
  180. }
  181. fclose(fp);
  182. console_printf(Console_Default, " [ OK ]\n");
  183. }
  184. else
  185. {
  186. console_printf(Console_Error, "Error loading palette '%s'!\n", filename);
  187. exit(-1);
  188. }
  189. }
  190. void signalhandler(int sig)
  191. {
  192. static int state = 0;
  193. char name[512];
  194. static FILE *fp = NULL;
  195. sprintf(name, "crashdump-%d.txt", (int)time(NULL));
  196. if (state != 0)
  197. {
  198. console_printf(Console_Error, "\n\n\nCrashed within signal!\nEmergency exit\n");
  199. exit(42);
  200. }
  201. state = 1;
  202. if (fp == NULL)
  203. {
  204. fp = fopen(name, "wt");
  205. }
  206. state = 2;
  207. if (fp)
  208. {
  209. console_printf(Console_Error,
  210. "\n\n\n\n\n"
  211. "#sick# peTI-NESulator %d.%d.%d%s #sick#\n"
  212. "see %s for more information",
  213. V_MAJOR, V_MINOR, V_MICRO, V_TEXT,
  214. name);
  215. }
  216. if (!fp)
  217. {
  218. fp = stderr;
  219. }
  220. fprintf(fp, "\n\n\n\n\n"
  221. "#sick# peTI-NESulator %d.%d.%d%s #sick# signal: ",
  222. V_MAJOR, V_MINOR, V_MICRO, V_TEXT);
  223. switch (sig)
  224. {
  225. default:
  226. case SIGABRT:
  227. fprintf(fp, "Abnormal termination");
  228. break;
  229. case SIGILL:
  230. fprintf(fp, "Illegal instruction");
  231. break;
  232. case SIGINT:
  233. fprintf(fp, "CTRL+C signal");
  234. break;
  235. case SIGSEGV:
  236. fprintf(fp, "Segmentation fault");
  237. break;
  238. case SIGTERM:
  239. fprintf(fp, "Termination request");
  240. break;
  241. }
  242. fprintf(fp, "\nAn error occurred during the excution.\n Crash report information :\n");
  243. //quick6502_dump(cpu, fp);
  244. //showlastop(fp);
  245. // fprintf(fp, "PPU: CR1: 0x%02X (NT:%d AI:%d SP:%d BP:%d SS:%d NMI:%d)\n",ppu.ControlRegister1.b, ppu.ControlRegister1.s.NameTblAddr, ppu.ControlRegister1.s.AddrIncrmt, ppu.ControlRegister1.s.SptPattern, ppu.ControlRegister1.s.BgPattern, ppu.ControlRegister1.s.SpriteSize, ppu.ControlRegister1.s.VBlank_NMI);
  246. // fprintf(fp, "PPU: CR2: 0x%02X (FBC/CI:%d SV:%d BV:%d SC:%d BC:%d DT:%d)\n",ppu.ControlRegister2.b,ppu.ControlRegister2.s.Colour,ppu.ControlRegister2.s.SpriteVisibility,ppu.ControlRegister2.s.BgVisibility,ppu.ControlRegister2.s.SpriteClipping,ppu.ControlRegister2.s.BgClipping,ppu.ControlRegister2.s.DisplayType);
  247. // fprintf(fp, "PPU: SR: 0x%02X (VB:%d S0:%d SSC:%d VWF:%d)\n", ppu.StatusRegister.b,ppu.StatusRegister.s.VBlankOccur,ppu.StatusRegister.s.Sprite0Occur,ppu.StatusRegister.s.SprtCount,ppu.StatusRegister.s.VRAMProtect);
  248. // fprintf(fp, "PPU: M:%d ST:%d VRAMPtr:0x%04X T:0x%04X\n",ppu.MirrorDir,ppu.ScreenType,ppu.VRAMAddrReg2.W,ppu.TmpVRamPtr);
  249. //MapperDump(fp);
  250. #if 0
  251. for(I = 0; I < 0xFFFF; I += 0x10)
  252. fprintf(fp, "%04X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X | %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
  253. I,
  254. Rd6502(I+0x00), Rd6502(I+0x01), Rd6502(I+0x02), Rd6502(I+0x03),
  255. Rd6502(I+0x04), Rd6502(I+0x05), Rd6502(I+0x06), Rd6502(I+0x07),
  256. Rd6502(I+0x08), Rd6502(I+0x09), Rd6502(I+0x0A), Rd6502(I+0x0B),
  257. Rd6502(I+0x0C), Rd6502(I+0x0D), Rd6502(I+0x0E), Rd6502(I+0x0F),
  258. // --- //
  259. isprint(Rd6502(I+0x00))?Rd6502(I+0x00):'_',
  260. isprint(Rd6502(I+0x01))?Rd6502(I+0x01):'_',
  261. isprint(Rd6502(I+0x02))?Rd6502(I+0x02):'_',
  262. isprint(Rd6502(I+0x03))?Rd6502(I+0x03):'_',
  263. isprint(Rd6502(I+0x04))?Rd6502(I+0x04):'_',
  264. isprint(Rd6502(I+0x05))?Rd6502(I+0x05):'_',
  265. isprint(Rd6502(I+0x06))?Rd6502(I+0x06):'_',
  266. isprint(Rd6502(I+0x07))?Rd6502(I+0x07):'_',
  267. isprint(Rd6502(I+0x08))?Rd6502(I+0x08):'_',
  268. isprint(Rd6502(I+0x09))?Rd6502(I+0x09):'_',
  269. isprint(Rd6502(I+0x0A))?Rd6502(I+0x0A):'_',
  270. isprint(Rd6502(I+0x0B))?Rd6502(I+0x0B):'_',
  271. isprint(Rd6502(I+0x0C))?Rd6502(I+0x0C):'_',
  272. isprint(Rd6502(I+0x0D))?Rd6502(I+0x0D):'_',
  273. isprint(Rd6502(I+0x0E))?Rd6502(I+0x0E):'_',
  274. isprint(Rd6502(I+0x0F))?Rd6502(I+0x0F):'_');
  275. #endif
  276. DumpMemoryState(fp);
  277. console_printf(Console_Error, "\nPlease join this informations when submiting crash report\n");
  278. if (fp != stderr)
  279. {
  280. fclose(fp);
  281. }
  282. exit(-42);
  283. }
  284. uint8_t Page40[256];
  285. void WrHook4000Multiplexer(uint8_t addr, uint8_t value)
  286. {
  287. switch (addr)
  288. {
  289. case 0x14:
  290. ppu_fillSprRamDMA(value);
  291. break;
  292. case 0x16:
  293. WritePaddle(&P1, value);
  294. //WritePaddle(&P2, value);
  295. break;
  296. case 0x17:
  297. if (value == 0x00)
  298. {
  299. quick6502_int(MainCPU, Q6502_IRQ_SIGNAL);
  300. }
  301. break;
  302. default:
  303. Page40[addr] = value;
  304. break;
  305. }
  306. }
  307. uint8_t RdHook4000Multiplexer(uint8_t addr)
  308. {
  309. uint8_t ret;
  310. switch (addr)
  311. {
  312. case 0x16:
  313. ret = ReadPaddle(&P1);
  314. break;
  315. case 0x17:
  316. ret = 0x40;
  317. break;
  318. case 0x15:
  319. ret = 0x1F;
  320. break;
  321. default:
  322. ret = 0x42;
  323. }
  324. return ret;
  325. }
  326. void printUsage(int argc, char *argv[])
  327. {
  328. console_printf(Console_Default, "Usage : %s game.nes [-p number][-f][-b filename.pal][ filename.nes\n"
  329. " -p: to add plugin 'number'\n"
  330. " -f: to start in FDS mode\n"
  331. " -d: to start directily into the debugguer\n"
  332. " -b: to use palette file 'filename.pal'\n",
  333. argv[0]);
  334. exit(0);
  335. }
  336. #define Value(_s) (((_s)%0xFF) + (rand()%0xFF-128) )%0xFF)
  337. int main(int argc, char *argv[])
  338. {
  339. int i;
  340. uint8_t *MemoryPage;
  341. quick6502_cpuconfig CpuConfig;
  342. #ifdef RUN_COVERAGE
  343. uint64_t coverage_loops = 500000000UL;
  344. #endif
  345. /* Here we will fill the memory */
  346. /*
  347. --------------------------------------- $10000
  348. Upper Bank of Cartridge ROM
  349. --------------------------------------- $C000
  350. Lower Bank of Cartridge ROM
  351. --------------------------------------- $8000
  352. Cartridge RAM (may be battery-backed)
  353. --------------------------------------- $6000
  354. Expansion Modules
  355. --------------------------------------- $5000
  356. Input/Output
  357. --------------------------------------- $2000
  358. 2kB Internal RAM, mirrored 4 times
  359. --------------------------------------- $0000
  360. */
  361. console_init(Console_Debug);
  362. /* Print the banner */
  363. console_printf(Console_Default, "--------------------------------------------------------------------------------\n"
  364. "Welcome to peTI-NESulator v%d.%d.%d%s - by Godzil`\n"
  365. "Copyright (c) 2002-2019 986-Studio / Manoël Trapier (petines@godzil.net)\n"
  366. "--------------------------------------------------------------------------------\n\n",
  367. V_MAJOR, V_MINOR, V_MICRO, V_TEXT);
  368. console_printf(Console_Default, "Install signal handlers...\t[");
  369. signal(SIGABRT, signalhandler);
  370. console_printf(Console_Default, "A");
  371. signal(SIGILL, signalhandler);
  372. console_printf(Console_Default, "I");
  373. /*signal(SIGINT, signalhandler);*/
  374. console_printf(Console_Default, ".");
  375. signal(SIGSEGV, signalhandler);
  376. console_printf(Console_Default, "S");
  377. signal(SIGTERM, signalhandler);
  378. console_printf(Console_Default, "T]\n");
  379. /* */
  380. console_printf(Console_Default, "Initialize memory...\t\t");
  381. InitMemory();
  382. console_printf(Console_Default, "[ OK ]\n");
  383. console_printf(Console_Default, "Parsing parameters (%d)...\n", argc);
  384. /* Now we use a real argument parser ! */
  385. for (i = 1 ; (i < argc) && (argv[i][0] == '-') ; i++)
  386. {
  387. switch (argv[i][1])
  388. {
  389. default: /* Option not recognized */
  390. case 'h': /* ask for help */
  391. printUsage(argc, argv);
  392. break;
  393. case 'p':
  394. if (atoi(argv[i + 1]) != 0)
  395. {
  396. console_printf(Console_Default, "-Load plugin #%d...\n", atoi(argv[i + 1]));
  397. if (plugin_load(atoi(argv[i + 1])) == -1)
  398. {
  399. plugin_list();
  400. exit(0);
  401. }
  402. i++;
  403. }
  404. else
  405. {
  406. plugin_list();
  407. exit(0);
  408. }
  409. break;
  410. case 'f':
  411. console_printf(Console_Default, "-Start with fds!\n");
  412. START_WITH_FDS = 1;
  413. break;
  414. case 'd':
  415. console_printf(Console_Default, "-Start with debug!\n");
  416. START_DEBUG = 1;
  417. break;
  418. case 'b':
  419. console_printf(Console_Default, "-Palette file is %s\n", argv[i + 1]);
  420. PALETTE_FILENAME = argv[i + 1];
  421. i++;
  422. break;
  423. }
  424. }
  425. CART_FILENAME = argv[argc - 1];
  426. if (CART_FILENAME == NULL)
  427. {
  428. printUsage(argc, argv);
  429. }
  430. console_printf(Console_Default, "Allocating 6502 memory\t\t");
  431. /* Allocating first 0x7FF memory */
  432. MemoryPage = (uint8_t *)malloc(0x800);
  433. set_page_ptr_2k(0, MemoryPage);
  434. for (i = 0 ; i < 0x08 ; i++)
  435. {
  436. set_page_readable(i, true);
  437. set_page_writeable(i, true);
  438. }
  439. /* Set ghost starting from 0x800 */
  440. set_page_ghost(0x08, true, 0x00);
  441. set_page_ghost(0x09, true, 0x01);
  442. set_page_ghost(0x0A, true, 0x02);
  443. set_page_ghost(0x0B, true, 0x03);
  444. set_page_ghost(0x0C, true, 0x04);
  445. set_page_ghost(0x0D, true, 0x05);
  446. set_page_ghost(0x0E, true, 0x06);
  447. set_page_ghost(0x0F, true, 0x07);
  448. /* Set ghost starting from 0x1000 */
  449. set_page_ghost(0x10, true, 0x00);
  450. set_page_ghost(0x11, true, 0x01);
  451. set_page_ghost(0x12, true, 0x02);
  452. set_page_ghost(0x13, true, 0x03);
  453. set_page_ghost(0x14, true, 0x04);
  454. set_page_ghost(0x15, true, 0x05);
  455. set_page_ghost(0x16, true, 0x06);
  456. set_page_ghost(0x17, true, 0x07);
  457. /* Set ghost starting from 0x1800 */
  458. set_page_ghost(0x18, true, 0x00);
  459. set_page_ghost(0x19, true, 0x01);
  460. set_page_ghost(0x1A, true, 0x02);
  461. set_page_ghost(0x1B, true, 0x03);
  462. set_page_ghost(0x1C, true, 0x04);
  463. set_page_ghost(0x1D, true, 0x05);
  464. set_page_ghost(0x1E, true, 0x06);
  465. set_page_ghost(0x1F, true, 0x07);
  466. /* Set 0x4000 registers */
  467. /* "hack" : only page $40 is used by multiple devices, we need to multiplexe
  468. it*/
  469. set_page_wr_hook(0x40, WrHook4000Multiplexer);
  470. set_page_rd_hook(0x40, RdHook4000Multiplexer);
  471. set_page_readable(0x40, true);
  472. set_page_writeable(0x40, true);
  473. /* Exp ROM : Nothing to do actually */
  474. /* ROM ptr will be set by mapper */
  475. /* But we will set the readable bit */
  476. for (i = 0x80 ; i < 0x100 ; i++)
  477. {
  478. set_page_readable(i, true);
  479. set_page_writeable(i, false);
  480. }
  481. console_printf(Console_Default, "[ OK ]\n");
  482. #ifdef MEMORY_TEST
  483. console_printf(Console_Default, "Testing memory validity...\n");
  484. map_sram();
  485. console_printf(Console_Verbose, "Testing Page Zero\n");
  486. for( i = 0 ; i < 0x100 ; i++)
  487. {
  488. j = rand() % 0xFF;
  489. MemoryPage[i] = j;
  490. if ((k = MemoryPageZeroRead(i)) != j)
  491. console_printf(Console_Error, "Error MemoryPageZeroRead @ 0x%04X [j:%02X, should:%02X, is:%02X]\n", i, j, MemoryPage[i], k);
  492. j = rand() % 0xFF;
  493. MemoryPageZeroWrite(i, j);
  494. if ((k = MemoryPage[i]) != j)
  495. console_printf(Console_Error, "Error MemoryPageZeroWrite @ 0x%04X [j:%02X, should:%02X, is:%02X]\n", i, j, MemoryPage[i], k);
  496. MemoryPage[i] = 0;
  497. }
  498. console_printf(Console_Verbose, "Testing memory... (<0x2000)\n");
  499. for( i = 0 ; i < 0x2000 ; i++ )
  500. {
  501. j = Value(i);
  502. MemoryWrite(i, j);
  503. if ((k=MemoryRead(i)) != j)
  504. console_printf(Console_Error, "Error read/write @ 0x%X [w:%d,r:%d]\n", i, j, k);
  505. if ((k=MemoryOpCodeRead(i)) != j)
  506. console_printf(Console_Error, "Error opcode @ 0x%X [w:%d,r:%d]\n", i, j, k);
  507. }
  508. #endif
  509. /* SRAM (0x6000 : 0x2000 uint8_ts ) */
  510. MemoryPage = (uint8_t *)malloc(0x2000);
  511. set_page_ptr_8k(0x60, MemoryPage);
  512. #ifdef MEMORY_TEST
  513. for(i = 0x6000; i < 0x8000; i ++)
  514. {
  515. if (MemoryPage[i-0x6000] != (k = MemoryRead(i)))
  516. console_printf(Console_Error, "Error MemoryRead @ 0x%X [should:%d,is:%d]\n", i, MemoryPage[i-0x6000], k);
  517. if (MemoryPage[i-0x6000] != (k = MemoryOpCodeRead(i)))
  518. console_printf(Console_Error, "Error MemoryOpCodeRead @ 0x%X [should:%d,is:%d]\n", i, MemoryPage[i-0x6000], k);
  519. }
  520. console_printf(Console_Verbose, "Testing memory... (0x6000-0x8000)\n");
  521. for(i=0x6000;i<0x8000;i++)
  522. {
  523. j = Value(i);
  524. MemoryWrite(i, j);
  525. if ((k=MemoryRead(i)) != j)
  526. console_printf(Console_Error, "Error read/write @ 0x%X [w:%d,r:%d]\n", i, j, k);
  527. if ((k=MemoryOpCodeRead(i)) != j)
  528. console_printf(Console_Error, "Error opcode @ 0x%X [w:%d,r:%d]\n", i, j, k);
  529. }
  530. console_printf(Console_Default, "Reseting main RAM...\t\t");
  531. /* Force the stack to be full of zero */
  532. for( i = 0x100 ; i < 0x200 ; i++ )
  533. {
  534. MemoryWrite(i, 0x00);
  535. }
  536. console_printf(Console_Default, "[ OK ]\n");
  537. #endif
  538. Cart = malloc(sizeof(NesCart));
  539. if (Cart == NULL)
  540. {
  541. console_printf(Console_Error, "Memory allocation error...\n");
  542. exit(-1);
  543. }
  544. if (START_WITH_FDS)
  545. {
  546. int fd;
  547. console_printf(Console_Default, "Loading FDS ROM...\t\t");
  548. fd = open("../data/disksys.rom", O_RDONLY);
  549. //fd = open("peTI-NESulator.app/Contents/Resources/disksys.rom", O_RDONLY);
  550. if (fd < 0)
  551. {
  552. console_printf(Console_Error, "Can't find FDS ROM...\n");
  553. exit(-1);
  554. }
  555. FDSRom = mmap(NULL, 8 * 1024, PROT_READ, MAP_PRIVATE, fd, 0);
  556. console_printf(Console_Default, "%p [ OK ]\n", FDSRom);
  557. close(fd);
  558. set_page_ptr_8k(0xE0, FDSRom);
  559. console_printf(Console_Default, "Allocating FDS RAM...\n");
  560. FDSRam = (uint8_t *)malloc((8 + 16) * 1024);
  561. if (FDSRam == NULL)
  562. {
  563. console_printf(Console_Error, "Allocation error\n");
  564. exit(-1);
  565. }
  566. for (i = 0x80 ; i < 0xE0 ; i++)
  567. {
  568. set_page_ptr(i, FDSRam + (i * 0x100));
  569. set_page_readable(i, true);
  570. set_page_writeable(i, true);
  571. }
  572. Cart->MapperID = 100;
  573. }
  574. else
  575. {
  576. console_printf(Console_Default, "Please Wait while loading %s cartridge...\n", CART_FILENAME);
  577. if (LoadCart(CART_FILENAME, Cart) != 0)
  578. {
  579. console_printf(Console_Error, "Loading error...\n");
  580. exit(-1);
  581. }
  582. if (Cart->Flags & iNES_BATTERY)
  583. {
  584. LoadSaveRam(CART_FILENAME);
  585. }
  586. }
  587. unmap_sram();
  588. InitPaddle(&P1);
  589. console_printf(Console_Default, "Init PPU...\n");
  590. if (ppu_init() != 0)
  591. {
  592. console_printf(Console_Error, "PPU Initialisation error..\n");
  593. exit(-1);
  594. }
  595. //DumpMemoryState(stdout);
  596. //DumpCartProperties(stdout, Cart);
  597. if (Cart->Flags & iNES_4SCREEN)
  598. {
  599. ppu_setScreenMode(PPU_SCMODE_FOURSC);
  600. }
  601. else
  602. {
  603. ppu_setScreenMode(PPU_SCMODE_NORMAL);
  604. ppu_setMirroring((Cart->Flags & iNES_MIRROR) ? PPU_MIRROR_VERTICAL : PPU_MIRROR_HORIZTAL);
  605. }
  606. console_printf(Console_Default, "Init mapper...\t\t\t");
  607. if (mapper_init(Cart) == -1)
  608. {
  609. return -1;
  610. }
  611. console_printf(Console_Default, "[ OK ]\n");
  612. // set_palette(basicPalette);
  613. // Actually no real debugguer...
  614. //console_printf(Console_Default, "Press ESC to pause emulation and jump to debugguer\n");
  615. ScanLine = 0;
  616. /* Initialize the CPU */
  617. CpuConfig.memory_read = MemoryRead;
  618. CpuConfig.memory_write = MemoryWrite;
  619. CpuConfig.memory_page0_read = MemoryPageZeroRead;
  620. CpuConfig.memory_page0_write = MemoryPageZeroWrite;
  621. CpuConfig.memory_stack_read = MemoryStackRead;
  622. CpuConfig.memory_stack_write = MemoryStackWrite;
  623. CpuConfig.memory_opcode_read = MemoryOpCodeRead;
  624. MainCPU = quick6502_init(&CpuConfig);
  625. quick6502_reset(MainCPU);
  626. /* No debugger actually
  627. MainCPU.Trace = 0;
  628. if (START_DEBUG)
  629. MainCPU.Trace = 1;
  630. */
  631. gettimeofday(&timeStart, NULL);
  632. while (!WantClosing)
  633. {
  634. ccount += quick6502_run(MainCPU, HBLANK_TIME);
  635. Loop6502(MainCPU);
  636. #ifdef RUN_COVERAGE
  637. if (ccount > coverage_loops)
  638. {
  639. WantClosing = 1;
  640. }
  641. #endif
  642. }
  643. if (Cart->Flags & iNES_BATTERY)
  644. {
  645. SaveSaveRam(CART_FILENAME);
  646. }
  647. return 0;
  648. }
  649. /* Access directly to Memory pages *HACKISH* */
  650. extern uint8_t *memory_pages[0xFF];
  651. /* Memory functions */
  652. /* Read memory, general function */
  653. uint8_t MemoryRead(uint16_t Addr)
  654. {
  655. return ReadMemory((Addr & 0xFF00) >> 8, Addr & 0x00FF);
  656. }
  657. /* Read memory for opcode (need fast access) */
  658. uint8_t MemoryOpCodeRead(uint16_t Addr)
  659. {
  660. uint8_t *ptr;
  661. return ((ptr = memory_pages[(Addr & 0xFF00) >> 8]) > (uint8_t *)1) ? ptr[Addr & 0x00FF] : 0xEA;
  662. }
  663. uint8_t MemoryStackRead(uint16_t Addr)
  664. {
  665. uint8_t *ptr = memory_pages[1];
  666. return ptr[Addr & 0x00FF];
  667. }
  668. uint8_t MemoryPageZeroRead(uint16_t Addr)
  669. {
  670. uint8_t *ptr = memory_pages[0];
  671. return ptr[Addr & 0x00FF];
  672. }
  673. /* Write to memory, general function */
  674. void MemoryWrite(uint16_t Addr, uint8_t Value)
  675. {
  676. WriteMemory((Addr & 0xFF00) >> 8, Addr & 0x00FF, Value);
  677. }
  678. void MemoryStackWrite(uint16_t Addr, uint8_t Value)
  679. {
  680. uint8_t *ptr = memory_pages[1];
  681. ptr[Addr & 0x00FF] = Value;
  682. }
  683. void MemoryPageZeroWrite(uint16_t Addr, uint8_t Value)
  684. {
  685. uint8_t *ptr = memory_pages[0];
  686. ptr[Addr & 0x00FF] = Value;
  687. }
  688. void Loop6502(quick6502_cpu *R)
  689. {
  690. quick6502_signal cpuSignal;
  691. // short skey;
  692. cpuSignal = Q6502_NO_SIGNAL;
  693. if ((mapper_irqloop) && (mapper_irqloop(ScanLine)))
  694. {
  695. cpuSignal = Q6502_IRQ_SIGNAL;
  696. IRQScanHit = ScanLine;
  697. }
  698. if (MapperWantIRQ == 1)
  699. {
  700. MapperWantIRQ = 0;
  701. cpuSignal = Q6502_IRQ_SIGNAL;
  702. }
  703. if (ppu_hblank(ScanLine) != 0)
  704. {
  705. cpuSignal = Q6502_NMI_SIGNAL;
  706. }
  707. if (ScanLine == (239 + VBLANK_TIME))
  708. {
  709. /* End of VBlank Time */
  710. frame++;
  711. SZHit = -1;
  712. IRQScanHit = -1;
  713. if (!getKeyStatus('Y'))
  714. {
  715. vsync();
  716. }
  717. }
  718. /* There is Two dummy scanline */
  719. if (ScanLine >= (239 + VBLANK_TIME + 4))
  720. {
  721. ScanLine = 0;
  722. }
  723. else
  724. {
  725. ScanLine++;
  726. }
  727. //console_printf(Console_Default, "SL:%d HBT:%d VbT:%d\n", ScanLine, HBLANK_TIME, VBLANK_TIME);
  728. // TODO: NO DEBUGER
  729. if (getKeyStatus(GLFW_KEY_ESCAPE))
  730. {
  731. exit(0);
  732. }
  733. #if 0
  734. if (skey == '9')
  735. {
  736. VBLANK_TIME += 2;
  737. console_printf(Console_Default, "VBLT: %d\n", VBLANK_TIME);
  738. }
  739. if (skey == '6')
  740. {
  741. VBLANK_TIME -= 2;
  742. console_printf(Console_Default, "VBLT: %d\n", VBLANK_TIME);
  743. }
  744. if (skey == '7')
  745. {
  746. HBLANK_TIME += 1;
  747. console_printf(Console_Default, "HBLT: %d\n", HBLANK_TIME);
  748. }
  749. if (skey == '4')
  750. {
  751. HBLANK_TIME -= 1;
  752. console_printf(Console_Default, "HBLT: %d\n", HBLANK_TIME);
  753. }
  754. #endif
  755. if (getKeyStatus('r') || getKeyStatus('R'))
  756. {
  757. console_printf(Console_Default, "-- Reset triggered\n");
  758. /* Force the PPU to stop NMIs */
  759. MemoryWrite(0x2000, 0x00);
  760. quick6502_reset(R);
  761. }
  762. // plugin_keypress(skey);
  763. if (cpuSignal != 0)
  764. {
  765. quick6502_int(R, cpuSignal);
  766. }
  767. }