main.c 30 KB

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