main.c 30 KB

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