nes_embed.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*===================================================================*/
  2. #include <InfoNES.h>
  3. #include <InfoNES_Mapper.h>
  4. #include <InfoNES_pAPU.h>
  5. #include <InfoNES_System.h>
  6. #include <K6502.h>
  7. /* */
  8. /* nes_port.cpp : port File */
  9. /* */
  10. /*===================================================================*/
  11. /*-------------------------------------------------------------------*/
  12. /* Include files */
  13. /*-------------------------------------------------------------------*/
  14. //User include file
  15. #include <nes_cfg.h>
  16. #include <nes_file_port.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <stdarg.h>
  21. /*-------------------------------------------------------------------*/
  22. /* ROM image file information */
  23. /*-------------------------------------------------------------------*/
  24. // Palette data
  25. WORD NesPalette[ 64 ] =
  26. {
  27. 0x39ce, 0x1071, 0x0015, 0x2013, 0x440e, 0x5402, 0x5000, 0x3c20,
  28. 0x20a0, 0x0100, 0x0140, 0x00e2, 0x0ceb, 0x0000, 0x0000, 0x0000,
  29. 0x5ef7, 0x01dd, 0x10fd, 0x401e, 0x5c17, 0x700b, 0x6ca0, 0x6521,
  30. 0x45c0, 0x0240, 0x02a0, 0x0247, 0x0211, 0x0000, 0x0000, 0x0000,
  31. 0x7fff, 0x1eff, 0x2e5f, 0x223f, 0x79ff, 0x7dd6, 0x7dcc, 0x7e67,
  32. 0x7ae7, 0x4342, 0x2769, 0x2ff3, 0x03bb, 0x0000, 0x0000, 0x0000,
  33. 0x7fff, 0x579f, 0x635f, 0x6b3f, 0x7f1f, 0x7f1b, 0x7ef6, 0x7f75,
  34. 0x7f94, 0x73f4, 0x57d7, 0x5bf9, 0x4ffe, 0x0000, 0x0000, 0x0000
  35. };
  36. //User
  37. void nesmain(const char *name)
  38. {
  39. if (0 != InfoNES_Load(name))
  40. {
  41. InfoNES_ReleaseRom();
  42. return;
  43. }
  44. InfoNES_Main();
  45. }
  46. /*===================================================================*/
  47. /* */
  48. /* InfoNES_Menu() : Menu screen */
  49. /* */
  50. /*===================================================================*/
  51. int InfoNES_Menu()
  52. {
  53. /*
  54. * Menu screen
  55. *
  56. * Return values
  57. * 0 : Normally
  58. * -1 : Exit InfoNES
  59. */
  60. if (PAD_PUSH(PAD_System, PAD_SYS_QUIT))
  61. return -1;
  62. // Nothing to do here
  63. return 0;
  64. }
  65. /*===================================================================*/
  66. /* */
  67. /* InfoNES_ReadRom() : Read ROM image file */
  68. /* */
  69. /*===================================================================*/
  70. int InfoNES_ReadRom(const char *pszFileName)
  71. {
  72. /*
  73. * Read ROM image file
  74. *
  75. * Parameters
  76. * const char *pszFileName (Read)
  77. *
  78. * Return values
  79. * 0 : Normally
  80. * -1 : Error
  81. */
  82. #ifdef PKG_NES_DOUBLE_FRAMEBUFFER
  83. DoubleFrame[0] = malloc(NES_DISP_WIDTH * NES_DISP_HEIGHT * sizeof(WORD));
  84. if (DoubleFrame[0] == NULL)
  85. return -1;
  86. DoubleFrame[1] = malloc(NES_DISP_WIDTH * NES_DISP_HEIGHT * sizeof(WORD));
  87. if (DoubleFrame[1] == NULL)
  88. return -1;
  89. #else
  90. WorkFrame = malloc(NES_DISP_WIDTH * NES_DISP_HEIGHT * sizeof(WORD));
  91. if (WorkFrame == NULL)
  92. return -1;
  93. #endif
  94. SRAM = malloc(SRAM_SIZE * sizeof(BYTE));
  95. if (SRAM == NULL)
  96. return -1;
  97. PPURAM = malloc(PPURAM_SIZE * sizeof(BYTE));
  98. if (PPURAM == NULL)
  99. return -1;
  100. SPRRAM = malloc(SPRRAM_SIZE * sizeof(BYTE));
  101. if (SPRRAM == NULL)
  102. return -1;
  103. /* Open ROM file */
  104. if (nesOpenFile(pszFileName) < 0)
  105. return -1;
  106. /* Read ROM Header */
  107. nesReadFile(&NesHeader, sizeof NesHeader, 1);
  108. if (memcmp(NesHeader.byID, "NES\x1a", 4) != 0)
  109. {
  110. nesCloseFile();
  111. return -1;
  112. }
  113. /* Clear SRAM */
  114. memset(SRAM, 0, SRAM_SIZE);
  115. /* If trainer presents Read Triner at 0x7000-0x71ff */
  116. if (NesHeader.byInfo1 & 4)
  117. nesReadFile(&SRAM[ 0x1000 ], 512, 1);
  118. /* Allocate Memory for ROM Image */
  119. ROM = (BYTE *)malloc(NesHeader.byRomSize * 0x4000);
  120. if (ROM == NULL)
  121. return -1;
  122. /* Read ROM Image */
  123. nesReadFile(ROM, 0x4000, NesHeader.byRomSize);
  124. VROM = NULL;
  125. if (NesHeader.byVRomSize > 0)
  126. {
  127. /* Allocate Memory for VROM Image */
  128. VROM = (BYTE *)malloc(NesHeader.byVRomSize * 0x2000);
  129. if (VROM == NULL)
  130. return -1;
  131. /* Read VROM Image */
  132. nesReadFile(VROM, 0x2000, NesHeader.byVRomSize);
  133. }
  134. /* File close */
  135. nesCloseFile();
  136. /* Successful */
  137. return 0;
  138. }
  139. /*===================================================================*/
  140. /* */
  141. /* InfoNES_ReleaseRom() : Release a memory for ROM */
  142. /* */
  143. /*===================================================================*/
  144. void InfoNES_ReleaseRom()
  145. {
  146. /*
  147. * Release a memory for ROM
  148. *
  149. */
  150. #ifdef PKG_NES_DOUBLE_FRAMEBUFFER
  151. if (DoubleFrame[0])
  152. {
  153. free(DoubleFrame[0]);
  154. DoubleFrame[0] = NULL;
  155. }
  156. if (DoubleFrame[1])
  157. {
  158. free(DoubleFrame[1]);
  159. DoubleFrame[1] = NULL;
  160. }
  161. WorkFrame = NULL;
  162. WorkFrameIdx = 0;
  163. #else
  164. if (WorkFrame)
  165. {
  166. free(WorkFrame);
  167. WorkFrame = NULL;
  168. }
  169. #endif
  170. if (SRAM)
  171. {
  172. free(SRAM);
  173. SRAM = NULL;
  174. }
  175. if (PPURAM)
  176. {
  177. free(PPURAM);
  178. PPURAM = NULL;
  179. }
  180. if (SPRRAM)
  181. {
  182. free(SPRRAM);
  183. SPRRAM = NULL;
  184. }
  185. if (ROM)
  186. {
  187. free(ROM);
  188. ROM = NULL;
  189. }
  190. if (VROM)
  191. {
  192. free(VROM);
  193. VROM = NULL;
  194. }
  195. }
  196. /*===================================================================*/
  197. /* */
  198. /* InfoNES_MemoryCopy() : memcpy */
  199. /* */
  200. /*===================================================================*/
  201. void *InfoNES_MemoryCopy(void *dest, const void *src, int count)
  202. {
  203. /*
  204. * memcpy
  205. *
  206. * Parameters
  207. * void *dest (Write)
  208. * Points to the starting address of the copied block's destination
  209. *
  210. * const void *src (Read)
  211. * Points to the starting address of the block of memory to copy
  212. *
  213. * int count (Read)
  214. * Specifies the size, in bytes, of the block of memory to copy
  215. *
  216. * Return values
  217. * Pointer of destination
  218. */
  219. memcpy(dest, src, count);
  220. return dest;
  221. }
  222. /*===================================================================*/
  223. /* */
  224. /* InfoNES_MemorySet() : memset */
  225. /* */
  226. /*===================================================================*/
  227. void *InfoNES_MemorySet(void *dest, int c, int count)
  228. {
  229. /*
  230. * memset
  231. *
  232. * Parameters
  233. * void *dest (Write)
  234. * Points to the starting address of the block of memory to fill
  235. *
  236. * int c (Read)
  237. * Specifies the byte value with which to fill the memory block
  238. *
  239. * int count (Read)
  240. * Specifies the size, in bytes, of the block of memory to fill
  241. *
  242. * Return values
  243. * Pointer of destination
  244. */
  245. memset(dest, c, count);
  246. return dest;
  247. }
  248. /*===================================================================*/
  249. /* */
  250. /* InfoNES_Wait() : Wait Emulation if required */
  251. /* */
  252. /*===================================================================*/
  253. void InfoNES_Wait()
  254. {
  255. }
  256. /*===================================================================*/
  257. /* */
  258. /* InfoNES_MessageBox() : Print System Message */
  259. /* */
  260. /*===================================================================*/
  261. void InfoNES_MessageBox(char *pszMsg, ...)
  262. {
  263. va_list args;
  264. va_start(args, pszMsg);
  265. printf(pszMsg, args);
  266. va_end(args);
  267. }