main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. static byte SQ1V = 0;
  278. static byte SQ2V = 0;
  279. static byte NOIV = 0;
  280. static unsigned short SQ1P = 0;
  281. static unsigned short SQ2P = 0;
  282. static unsigned short TRIP = 0;
  283. static unsigned short NOIP = 0;
  284. static byte Sq1_reg0 = 0;
  285. static byte Sq1_reg1 = 0;
  286. static byte Sq1_reg2 = 0;
  287. static byte Sq1_reg3 = 0;
  288. static byte Sq2_reg0 = 0;
  289. static byte Sq2_reg1 = 0;
  290. static byte Sq2_reg2 = 0;
  291. static byte Sq2_reg3 = 0;
  292. double SQ = 0.0;
  293. switch(addr)
  294. {
  295. case 0x00: /* DDLE NNNN */
  296. Sq1_reg0 = value;
  297. if (Sq1_reg0 & 0x10)
  298. {
  299. SQ1V = (0x04+(value&0x0F))& 0x0F;
  300. }
  301. else
  302. {
  303. SQ1V = value&0x0F;
  304. }
  305. break;
  306. case 0x01: /* EPPP NSSS */
  307. Sq1_reg1 = value;
  308. break;
  309. case 0x02:
  310. SQ1P = value | ((Sq1_reg3&0x7) << 8);
  311. SQ = APU_BASEFREQ * 1000 * 1000 / (SQ1P+1);
  312. Sq1_reg2 = value;
  313. break;
  314. case 0x03:
  315. Sq1_reg3 = value;
  316. SQ1P = Sq1_reg2 | ((value&0x7) << 8);
  317. SQ = APU_BASEFREQ * 1000 * 1000 / (SQ1P+1);
  318. break;
  319. case 0x04:
  320. Sq2_reg0 = value;
  321. if (Sq2_reg0 & 0x10)
  322. {
  323. SQ2V = (0x04+(value&0x0F))& 0x0F;
  324. }
  325. else
  326. {
  327. SQ2V = value&0x0F;
  328. }
  329. break;
  330. case 0x05:
  331. Sq2_reg1 = value;
  332. break;
  333. case 0x06:
  334. Sq2_reg2 = value;
  335. SQ2P = Sq2_reg2 | ((Sq2_reg3&0x7) << 8);
  336. SQ = APU_BASEFREQ * 1000 * 1000 / (SQ2P+1);
  337. break;
  338. case 0x07:
  339. Sq2_reg3 = value;
  340. SQ2P = Sq2_reg2 | ((Sq2_reg3&0x7) << 8);
  341. SQ = APU_BASEFREQ * 1000 * 1000 / (SQ2P+1);
  342. break;
  343. case 0x0A:
  344. TRIP = (TRIP & 0xFF00) | value;
  345. SQ = APU_BASEFREQ * 1000 * 1000 / TRIP;
  346. break;
  347. case 0x0B:
  348. TRIP = (TRIP & 0x00FF) | ((value&0x7) << 8);;
  349. SQ = APU_BASEFREQ * 1000 * 1000 / TRIP;
  350. break;
  351. case 0x0C:
  352. NOIV = value & 0x0F;
  353. break;
  354. case 0x0E:
  355. NOIP = value & 0x0F;
  356. SQ = APU_BASEFREQ * 1000 * 1000 / NOIP;
  357. break;
  358. case 0x0F:
  359. break;
  360. case 0x15:
  361. /* DMC, Noise, Triangle, Sq 2, Sq 1 */
  362. break;
  363. case 0x14:
  364. ppu_fillSprRamDMA(value);
  365. break;
  366. case 0x16:
  367. WritePaddle(&P1, value);
  368. //WritePaddle(&P2, value);
  369. break;
  370. case 0x17:
  371. if (value == 0x00)
  372. {
  373. quick6502_int(MainCPU, Q6502_IRQ_SIGNAL);
  374. }
  375. break;
  376. default:
  377. Page40[addr] = value;
  378. break;
  379. }
  380. }
  381. byte RdHook4000Multiplexer(byte addr)
  382. {
  383. byte ret;
  384. switch(addr)
  385. {
  386. case 0x16:
  387. ret = ReadPaddle(&P1);
  388. break;
  389. case 0x17:
  390. ret = 0x40;
  391. break;
  392. case 0x15:
  393. ret = 0x1F;
  394. break;
  395. default:
  396. ret = 0x42;
  397. }
  398. return ret;
  399. }
  400. void printUsage(int argc, char *argv[])
  401. {
  402. console_printf(Console_Default, "Usage : %s game.nes [-p number][-f][-b filename.pal][ filename.nes\n"
  403. " -p: to add plugin 'number'\n"
  404. " -f: to start in FDS mode\n"
  405. " -d: to start directily into the debugguer\n"
  406. " -b: to use palette file 'filename.pal'\n",
  407. argv[0]);
  408. exit(0);
  409. }
  410. int main(int argc, char *argv[])
  411. {
  412. int i;
  413. unsigned char *MemoryPage;
  414. quick6502_cpuconfig CpuConfig;
  415. /* Here we will fill the memory */
  416. /*
  417. --------------------------------------- $10000
  418. Upper Bank of Cartridge ROM
  419. --------------------------------------- $C000
  420. Lower Bank of Cartridge ROM
  421. --------------------------------------- $8000
  422. Cartridge RAM (may be battery-backed)
  423. --------------------------------------- $6000
  424. Expansion Modules
  425. --------------------------------------- $5000
  426. Input/Output
  427. --------------------------------------- $2000
  428. 2kB Internal RAM, mirrored 4 times
  429. --------------------------------------- $0000
  430. */
  431. console_init(Console_Debug);
  432. /* Print the banner */
  433. console_printf(Console_Default, "--------------------------------------------------------------------------------\n"
  434. "Welcome to peTI-NESulator v%d.%d.%d%s - by Godzil`\n"
  435. "Copyright 2003-2018 Manoel TRAPIER (petines@godzil.net)\n"
  436. "--------------------------------------------------------------------------------\n\n",
  437. V_MAJOR, V_MINOR, V_MICRO, V_TEXT);
  438. console_printf(Console_Default, "Install signal handlers...\t[");
  439. signal(SIGABRT, signalhandler);
  440. console_printf(Console_Default, "A");
  441. signal(SIGILL, signalhandler);
  442. console_printf(Console_Default, "I");
  443. /*signal(SIGINT, signalhandler);*/
  444. console_printf(Console_Default, ".");
  445. signal(SIGSEGV, signalhandler);
  446. console_printf(Console_Default, "S");
  447. signal(SIGTERM, signalhandler);
  448. console_printf(Console_Default, "T]\n");
  449. #ifdef RUN_COVERAGE
  450. signal(SIGALRM, alarmHandler);
  451. #endif
  452. /* */
  453. console_printf(Console_Default, "Initialize memory...\t\t");
  454. InitMemory();
  455. console_printf(Console_Default, "[ OK ]\n");
  456. console_printf(Console_Default, "Parsing parameters (%d)...\n", argc);
  457. /* Now we use a real argument parser ! */
  458. for(i = 1 ; (i < argc) && (argv[i][0]=='-'); i++)
  459. {
  460. switch(argv[i][1])
  461. {
  462. default: /* Option not recognized */
  463. case 'h': /* ask for help */
  464. printUsage(argc, argv);
  465. break;
  466. case 'p':
  467. if (atoi(argv[i+1]) != 0)
  468. {
  469. console_printf(Console_Default, "-Load plugin #%d...\n", atoi(argv[i+1]));
  470. if ( plugin_load(atoi(argv[i+1])) == -1)
  471. {
  472. plugin_list();
  473. exit(0);
  474. }
  475. i++;
  476. }
  477. else
  478. {
  479. plugin_list();
  480. exit(0);
  481. }
  482. break;
  483. case 'f':
  484. console_printf(Console_Default, "-Start with fds!\n");
  485. START_WITH_FDS = 1;
  486. break;
  487. case 'd':
  488. console_printf(Console_Default, "-Start with debug!\n");
  489. START_DEBUG = 1;
  490. break;
  491. case 'b':
  492. console_printf(Console_Default, "-Palette file is %s\n", argv[i+1]);
  493. PALETTE_FILENAME = argv[i+1];
  494. i++;
  495. break;
  496. }
  497. }
  498. CART_FILENAME = argv[argc-1];
  499. if (CART_FILENAME == NULL)
  500. printUsage(argc, argv);
  501. console_printf(Console_Default, "Allocating 6502 memory\t\t");
  502. /* Allocating first 0x7FF memory */
  503. MemoryPage = (unsigned char *)malloc (0x800);
  504. set_page_ptr_2k(0,MemoryPage);
  505. for (i = 0; i < 0x08; i++)
  506. {
  507. set_page_readable(i, true);
  508. set_page_writeable(i, true);
  509. }
  510. /* Set ghost starting from 0x800 */
  511. set_page_ghost(0x08,true,0x00);
  512. set_page_ghost(0x09,true,0x01);
  513. set_page_ghost(0x0A,true,0x02);
  514. set_page_ghost(0x0B,true,0x03);
  515. set_page_ghost(0x0C,true,0x04);
  516. set_page_ghost(0x0D,true,0x05);
  517. set_page_ghost(0x0E,true,0x06);
  518. set_page_ghost(0x0F,true,0x07);
  519. /* Set ghost starting from 0x1000 */
  520. set_page_ghost(0x10,true,0x00);
  521. set_page_ghost(0x11,true,0x01);
  522. set_page_ghost(0x12,true,0x02);
  523. set_page_ghost(0x13,true,0x03);
  524. set_page_ghost(0x14,true,0x04);
  525. set_page_ghost(0x15,true,0x05);
  526. set_page_ghost(0x16,true,0x06);
  527. set_page_ghost(0x17,true,0x07);
  528. /* Set ghost starting from 0x1800 */
  529. set_page_ghost(0x18,true,0x00);
  530. set_page_ghost(0x19,true,0x01);
  531. set_page_ghost(0x1A,true,0x02);
  532. set_page_ghost(0x1B,true,0x03);
  533. set_page_ghost(0x1C,true,0x04);
  534. set_page_ghost(0x1D,true,0x05);
  535. set_page_ghost(0x1E,true,0x06);
  536. set_page_ghost(0x1F,true,0x07);
  537. /* Set 0x4000 registers */
  538. /* "hack" : only page $40 is used by multiple devices, we need to multiplexe
  539. it*/
  540. set_page_wr_hook(0x40, WrHook4000Multiplexer);
  541. set_page_rd_hook(0x40, RdHook4000Multiplexer);
  542. set_page_readable(0x40, true);
  543. set_page_writeable(0x40, true);
  544. /* Exp ROM : Nothing to do actually */
  545. /* ROM ptr will be set by mapper */
  546. /* But we will set the readable bit */
  547. for (i = 0x80; i < 0x100; i++)
  548. {
  549. set_page_readable(i, true);
  550. set_page_writeable(i, false);
  551. }
  552. console_printf(Console_Default, "[ OK ]\n");
  553. #define Value(s) (((s%0xFF) + (rand()%0xFF-128) )%0xFF)
  554. #ifdef MEMORY_TEST
  555. console_printf(Console_Default, "Testing memory validity...\n");
  556. map_sram();
  557. console_printf(Console_Verbose, "Testing Page Zero\n");
  558. for( i = 0 ; i < 0x100 ; i++)
  559. {
  560. j = rand() % 0xFF;
  561. MemoryPage[i] = j;
  562. if ((k = MemoryPageZeroRead(i)) != j)
  563. console_printf(Console_Error, "Error MemoryPageZeroRead @ 0x%04X [j:%02X, should:%02X, is:%02X]\n", i, j, MemoryPage[i], k);
  564. j = rand() % 0xFF;
  565. MemoryPageZeroWrite(i, j);
  566. if ((k = MemoryPage[i]) != j)
  567. console_printf(Console_Error, "Error MemoryPageZeroWrite @ 0x%04X [j:%02X, should:%02X, is:%02X]\n", i, j, MemoryPage[i], k);
  568. MemoryPage[i] = 0;
  569. }
  570. console_printf(Console_Verbose, "Testing memory... (<0x2000)\n");
  571. for( i = 0 ; i < 0x2000 ; i++ )
  572. {
  573. j = Value(i);
  574. MemoryWrite(i, j);
  575. if ((k=MemoryRead(i)) != j)
  576. console_printf(Console_Error, "Error read/write @ 0x%X [w:%d,r:%d]\n", i, j, k);
  577. if ((k=MemoryOpCodeRead(i)) != j)
  578. console_printf(Console_Error, "Error opcode @ 0x%X [w:%d,r:%d]\n", i, j, k);
  579. }
  580. #endif
  581. /* SRAM (0x6000 : 0x2000 bytes ) */
  582. MemoryPage = (unsigned char *)malloc (0x2000);
  583. set_page_ptr_8k(0x60, MemoryPage);
  584. #ifdef MEMORY_TEST
  585. for(i = 0x6000; i < 0x8000; i ++)
  586. {
  587. if (MemoryPage[i-0x6000] != (k = MemoryRead(i)))
  588. console_printf(Console_Error, "Error MemoryRead @ 0x%X [should:%d,is:%d]\n", i, MemoryPage[i-0x6000], k);
  589. if (MemoryPage[i-0x6000] != (k = MemoryOpCodeRead(i)))
  590. console_printf(Console_Error, "Error MemoryOpCodeRead @ 0x%X [should:%d,is:%d]\n", i, MemoryPage[i-0x6000], k);
  591. }
  592. console_printf(Console_Verbose, "Testing memory... (0x6000-0x8000)\n");
  593. for(i=0x6000;i<0x8000;i++)
  594. {
  595. j = Value(i);
  596. MemoryWrite(i, j);
  597. if ((k=MemoryRead(i)) != j)
  598. console_printf(Console_Error, "Error read/write @ 0x%X [w:%d,r:%d]\n", i, j, k);
  599. if ((k=MemoryOpCodeRead(i)) != j)
  600. console_printf(Console_Error, "Error opcode @ 0x%X [w:%d,r:%d]\n", i, j, k);
  601. }
  602. console_printf(Console_Default, "Reseting main RAM...\t\t");
  603. /* Force the stack to be full of zero */
  604. for( i = 0x100 ; i < 0x200 ; i++ )
  605. {
  606. MemoryWrite(i, 0x00);
  607. }
  608. console_printf(Console_Default, "[ OK ]\n");
  609. #endif
  610. Cart = malloc( sizeof (NesCart));
  611. if (Cart == NULL)
  612. {
  613. console_printf(Console_Error, "Memory allocation error...\n");
  614. exit(-1);
  615. }
  616. if (START_WITH_FDS)
  617. {
  618. int fd;
  619. console_printf(Console_Default, "Loading FDS ROM...\t\t");
  620. fd = open("../data/disksys.rom", O_RDONLY);
  621. //fd = open("peTI-NESulator.app/Contents/Resources/disksys.rom", O_RDONLY);
  622. if (fd < 0)
  623. {
  624. console_printf(Console_Error, "Can't find FDS ROM...\n");
  625. exit(-1);
  626. }
  627. FDSRom = mmap(NULL, 8*1024, PROT_READ, MAP_PRIVATE, fd, 0);
  628. console_printf(Console_Default, "%p [ OK ]\n", FDSRom);
  629. close(fd);
  630. set_page_ptr_8k(0xE0, FDSRom);
  631. console_printf(Console_Default, "Allocating FDS RAM...\n");
  632. FDSRam = (byte*) malloc( (8+16) * 1024);
  633. if (FDSRam == NULL)
  634. {
  635. console_printf(Console_Error, "Allocation error\n");
  636. exit(-1);
  637. }
  638. for (i = 0x80; i < 0xE0; i++)
  639. {
  640. set_page_ptr(i, FDSRam + (i*0x100));
  641. set_page_readable(i, true);
  642. set_page_writeable(i, true);
  643. }
  644. Cart->MapperID = 100;
  645. }
  646. else
  647. {
  648. console_printf(Console_Default, "Please Wait while loading %s cartridge...\n", CART_FILENAME);
  649. if (LoadCart(CART_FILENAME, Cart) != 0)
  650. {
  651. console_printf(Console_Error, "Loading error...\n");
  652. exit(-1);
  653. }
  654. if (Cart->Flags & iNES_BATTERY)
  655. {
  656. LoadSaveRam(CART_FILENAME);
  657. }
  658. }
  659. unmap_sram();
  660. InitPaddle(&P1);
  661. console_printf(Console_Default, "Init PPU...\n");
  662. if (ppu_init() != 0)
  663. {
  664. console_printf(Console_Error, "PPU Initialisation error..\n");
  665. exit(-1);
  666. }
  667. DumpMemoryState(stdout);
  668. if (Cart->Flags & iNES_4SCREEN)
  669. {
  670. ppu_setScreenMode(PPU_SCMODE_FOURSC);
  671. }
  672. else
  673. {
  674. ppu_setScreenMode(PPU_SCMODE_NORMAL);
  675. ppu_setMirroring((Cart->Flags & iNES_MIRROR)?PPU_MIRROR_VERTICAL:PPU_MIRROR_HORIZTAL);
  676. }
  677. console_printf(Console_Default, "Init mapper...\t\t\t");
  678. if (mapper_init(Cart) == -1)
  679. return -1;
  680. console_printf(Console_Default, "[ OK ]\n");
  681. // set_palette(basicPalette);
  682. #ifdef USE_SOUND
  683. InitSound(44400,!0);
  684. SetSound(0, SND_RECTANGLE);
  685. SetSound(1, SND_RECTANGLE);
  686. SetSound(2, SND_TRIANGLE);
  687. SetSound(3, SND_NOISE);
  688. #endif
  689. // Actually no real debugguer...
  690. //console_printf(Console_Default, "Press ESC to pause emulation and jump to debugguer\n");
  691. ScanLine = 0;
  692. /* Initialize the CPU */
  693. CpuConfig.memory_read = MemoryRead;
  694. CpuConfig.memory_write = MemoryWrite;
  695. CpuConfig.memory_page0_read = MemoryPageZeroRead;
  696. CpuConfig.memory_page0_write = MemoryPageZeroWrite;
  697. CpuConfig.memory_stack_read = MemoryStackRead;
  698. CpuConfig.memory_stack_write = MemoryStackWrite;
  699. CpuConfig.memory_opcode_read = MemoryOpCodeRead;
  700. MainCPU = quick6502_init(&CpuConfig);
  701. quick6502_reset(MainCPU);
  702. /* No debugger actually
  703. MainCPU.Trace = 0;
  704. if (START_DEBUG)
  705. MainCPU.Trace = 1;
  706. */
  707. gettimeofday(&timeStart, NULL);
  708. #ifdef RUN_COVERAGE
  709. alarm(1 * 60); /* Run for 1 minutes */
  710. #endif
  711. while(!WantClosing)
  712. {
  713. ccount += quick6502_run(MainCPU, HBLANK_TIME);
  714. Loop6502(MainCPU);
  715. }
  716. if (Cart->Flags & iNES_BATTERY)
  717. {
  718. SaveSaveRam(CART_FILENAME);
  719. }
  720. return 0;
  721. }
  722. /* Access directly to Memory pages *HACKISH* */
  723. extern byte *memory_pages[0xFF];
  724. /* Memory functions */
  725. /* Read memory, general function */
  726. byte MemoryRead (unsigned short Addr)
  727. {
  728. return ReadMemory((Addr&0xFF00)>>8,Addr&0x00FF);
  729. }
  730. /* Read memory for opcode (need fast access) */
  731. byte MemoryOpCodeRead (unsigned short Addr)
  732. {
  733. byte *ptr;
  734. return ((ptr = memory_pages[(Addr&0xFF00)>>8])>(byte*)1)?ptr[Addr&0x00FF]:0xEA;
  735. }
  736. byte MemoryStackRead (unsigned short Addr)
  737. {
  738. byte *ptr = memory_pages[1];
  739. return ptr[Addr&0x00FF];
  740. }
  741. byte MemoryPageZeroRead (unsigned short Addr)
  742. {
  743. byte *ptr = memory_pages[0];
  744. return ptr[Addr&0x00FF];
  745. }
  746. /* Write to memory, general function */
  747. void MemoryWrite (unsigned short Addr, byte Value)
  748. {
  749. WriteMemory((Addr&0xFF00)>>8,Addr&0x00FF, Value);
  750. }
  751. void MemoryStackWrite (unsigned short Addr, byte Value)
  752. {
  753. byte *ptr = memory_pages[1];
  754. ptr[Addr&0x00FF] = Value;
  755. }
  756. void MemoryPageZeroWrite (unsigned short Addr, byte Value)
  757. {
  758. byte *ptr = memory_pages[0];
  759. ptr[Addr&0x00FF] = Value;
  760. }
  761. void Loop6502(quick6502_cpu *R)
  762. {
  763. byte ret;
  764. // short skey;
  765. long WaitTime;
  766. static long delta=0;
  767. ret = 0;
  768. if ( (mapper_irqloop) && ( mapper_irqloop (ScanLine) ) )
  769. {
  770. ret = Q6502_IRQ_SIGNAL;
  771. IRQScanHit = ScanLine;
  772. }
  773. if ( MapperWantIRQ == 1)
  774. {
  775. MapperWantIRQ = 0;
  776. ret = Q6502_IRQ_SIGNAL;
  777. }
  778. if ( ppu_hblank(ScanLine) != 0 )
  779. {
  780. ret = Q6502_NMI_SIGNAL;
  781. }
  782. if (ScanLine == (239 + VBLANK_TIME))
  783. { /* End of VBlank Time */
  784. frame++;
  785. SZHit = -1;
  786. IRQScanHit = -1;
  787. /* Sync at 60FPS */
  788. /* Get current time in microseconds */
  789. gettimeofday(&timeEnd, NULL);
  790. WaitTime = (timeEnd.tv_sec) - (timeStart.tv_sec);
  791. WaitTime *= 1000000;
  792. WaitTime += (timeEnd.tv_usec - timeStart.tv_usec);
  793. #if !ISPAL && ISNTSC
  794. /* Calculate the waiting time, 16666 is the time of one frame in microseconds at a 60Hz rate) */
  795. WaitTime = 16666 - WaitTime + delta;
  796. #elif ISPAL && !ISNTSC
  797. WaitTime = 20000 - WaitTime + delta;
  798. #endif
  799. /* If we press Page Up, we want to accelerate "time" */
  800. if (!getKeyStatus('Y'))
  801. if ((WaitTime >= 0) && (WaitTime < 100000))
  802. usleep(WaitTime);
  803. /* Now get the time after sleep */
  804. gettimeofday(&timeStart, NULL);
  805. /* Now calculate How many microseconds we really spend in sleep and
  806. calculate a delta for next iteration */
  807. delta = (timeStart.tv_sec) - (timeEnd.tv_sec);
  808. delta *= 1000000;
  809. delta += (timeStart.tv_usec - timeEnd.tv_usec);
  810. delta = WaitTime - delta;
  811. /* To avoid strange time warp when stopping emulation or using acceleration a lot */
  812. if ((delta > 10000) || (delta < -10000))
  813. delta = 0;
  814. }
  815. /* There is Two dummy scanline */
  816. if (ScanLine >= (239 + VBLANK_TIME + 4))
  817. ScanLine = 0;
  818. else
  819. ScanLine++;
  820. //console_printf(Console_Default, "SL:%d HBT:%d VbT:%d\n", ScanLine, HBLANK_TIME, VBLANK_TIME);
  821. // TODO: NO DEBUGER
  822. if (getKeyStatus(GLFW_KEY_ESCAPE))
  823. exit(0);
  824. #if 0
  825. if (skey == '9')
  826. {
  827. VBLANK_TIME += 2;
  828. console_printf(Console_Default, "VBLT: %d\n", VBLANK_TIME);
  829. }
  830. if (skey == '6')
  831. {
  832. VBLANK_TIME -= 2;
  833. console_printf(Console_Default, "VBLT: %d\n", VBLANK_TIME);
  834. }
  835. if (skey == '7')
  836. {
  837. HBLANK_TIME += 1;
  838. console_printf(Console_Default, "HBLT: %d\n", HBLANK_TIME);
  839. }
  840. if (skey == '4')
  841. {
  842. HBLANK_TIME -= 1;
  843. console_printf(Console_Default, "HBLT: %d\n", HBLANK_TIME);
  844. }
  845. #endif
  846. if (getKeyStatus('r') || getKeyStatus('R'))
  847. {
  848. /* Force the PPU to stop NMIs */
  849. MemoryWrite(0x2000, 0x00);
  850. quick6502_reset(R);
  851. }
  852. // plugin_keypress(skey);
  853. if (ret != 0)
  854. quick6502_int(R, ret);
  855. }