main.c 25 KB

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