main.c 30 KB

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