main.c 27 KB

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