main.c 31 KB

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