main.c 30 KB

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