main.c 24 KB

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