gb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. gb.c - Game Boy support for uCON64
  3. Copyright (c) 1999 - 2001 NoisyB <noisyb@gmx.net>
  4. Copyright (c) 2001 - 2004 dbjh
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #include <string.h>
  28. #include "misc/file.h"
  29. #include "misc/misc.h"
  30. #include "misc/string.h"
  31. #ifdef USE_ZLIB
  32. #include "misc/archive.h"
  33. #endif
  34. #include "misc/getopt2.h" // st_getopt2_t
  35. #include "ucon64.h"
  36. #include "ucon64_misc.h"
  37. #include "console/nes.h"
  38. #include "backup/gbx.h"
  39. #include "backup/mgd.h"
  40. #include "backup/ssc.h"
  41. #include "patch/ips.h"
  42. #include "patch/bsl.h"
  43. #include "gb.h"
  44. #define GAMEBOY_HEADER_START 0x100
  45. #define GAMEBOY_HEADER_LEN (sizeof (st_gameboy_header_t))
  46. #define GB_NAME_LEN 15
  47. const st_getopt2_t gameboy_usage[] =
  48. {
  49. {
  50. NULL, 0, 0, 0,
  51. NULL, "Game Boy/(Super GB)/GB Pocket/Color GB/(GB Advance)"
  52. /*"1989/1994/1996/1998/2001 Nintendo http://www.nintendo.com"*/,
  53. NULL
  54. },
  55. {
  56. "gb", 0, 0, UCON64_GB,
  57. NULL, "force recognition",
  58. &ucon64_wf[WF_OBJ_GB_SWITCH]
  59. },
  60. {
  61. "n", 1, 0, UCON64_N,
  62. "NEW_NAME", "change internal ROM name to NEW_NAME",
  63. &ucon64_wf[WF_OBJ_ALL_DEFAULT]
  64. },
  65. {
  66. "logo", 0, 0, UCON64_LOGO,
  67. NULL, "restore ROM logo character data (offset: 0x104-0x134)",
  68. &ucon64_wf[WF_OBJ_ALL_DEFAULT]
  69. },
  70. {
  71. "mgd", 0, 0, UCON64_MGD,
  72. NULL, "convert to Multi Game*/MGD2/RAW",
  73. &ucon64_wf[WF_OBJ_ALL_DEFAULT]
  74. },
  75. {
  76. "ssc", 0, 0, UCON64_SSC,
  77. NULL, "convert to Super Smart Card/SSC",
  78. &ucon64_wf[WF_OBJ_GB_DEFAULT]
  79. },
  80. {
  81. "sgb", 0, 0, UCON64_SGB,
  82. NULL, "convert from GB Xchanger/GB/GBC to Super Backup Card/GX/GBX",
  83. &ucon64_wf[WF_OBJ_GB_DEFAULT]
  84. },
  85. {
  86. "gbx", 0, 0, UCON64_GBX,
  87. NULL, "convert from Super Backup Card/GX/GBX to GB Xchanger/GB/GBC",
  88. &ucon64_wf[WF_OBJ_GB_DEFAULT]
  89. },
  90. {
  91. "n2gb", 1, 0, UCON64_N2GB,
  92. "NESROM", "KAMI's FC EMUlator (NES emulator);\n"
  93. "ROM should be KAMI's FC Emulator ROM image\n"
  94. "NESROM should contain 16 kB of PRG data and 8 kB of CHR data",
  95. &ucon64_wf[WF_OBJ_GB_DEFAULT]
  96. },
  97. {
  98. "chk", 0, 0, UCON64_CHK,
  99. NULL, "fix ROM checksum",
  100. &ucon64_wf[WF_OBJ_ALL_DEFAULT]
  101. },
  102. {NULL, 0, 0, 0, NULL, NULL, NULL}
  103. };
  104. /*
  105. 0148 ROM size:
  106. 0 - 256kBit = 32kB = 2 banks
  107. 1 - 512kBit = 64kB = 4 banks
  108. 2 - 1Mb = 128kB = 8 banks
  109. 3 - 2Mb = 256kB = 16 banks
  110. 4 - 4Mb = 512kB = 32 banks
  111. 0149 RAM size:
  112. 0 - None
  113. 1 - 16kBit = 2kB = 1 bank
  114. 2 - 64kBit = 8kB = 1 bank
  115. 3 - 256kBit = 32kB = 4 banks
  116. */
  117. typedef struct st_gameboy_header
  118. {
  119. unsigned char opcode1; // 0x00 usually 0x00, NOP
  120. unsigned char opcode2; // 0x01 usually 0xc3, JP
  121. unsigned char start_low; // 0x02 first byte of parameter of JP
  122. unsigned char start_high; // 0x03 second byte
  123. unsigned char logo[GB_LOGODATA_LEN]; // 0x04
  124. unsigned char name[GB_NAME_LEN]; // 0x34
  125. unsigned char gb_type; // 0x43
  126. unsigned char maker_high; // 0x44
  127. unsigned char maker_low; // 0x45
  128. unsigned char pad;
  129. unsigned char rom_type; // 0x47
  130. unsigned char rom_size; // 0x48
  131. unsigned char sram_size; // 0x49
  132. unsigned char country; // 0x4a
  133. unsigned char maker; // 0x4b
  134. unsigned char version; // 0x4c
  135. unsigned char header_checksum; // 0x4d
  136. unsigned char checksum_high; // 0x4e
  137. unsigned char checksum_low; // 0x4f
  138. } st_gameboy_header_t;
  139. static st_gameboy_header_t gameboy_header;
  140. const unsigned char gb_logodata[] = // Note: not a static variable
  141. {
  142. 0xce, 0xed, 0x66, 0x66, 0xcc, 0x0d, 0x00, 0x0b,
  143. 0x03, 0x73, 0x00, 0x83, 0x00, 0x0c, 0x00, 0x0d,
  144. 0x00, 0x08, 0x11, 0x1f, 0x88, 0x89, 0x00, 0x0e,
  145. 0xdc, 0xcc, 0x6e, 0xe6, 0xdd, 0xdd, 0xd9, 0x99,
  146. 0xbb, 0xbb, 0x67, 0x63, 0x6e, 0x0e, 0xec, 0xcc,
  147. 0xdd, 0xdc, 0x99, 0x9f, 0xbb, 0xb9, 0x33, 0x3e
  148. },
  149. rocket_logodata[] = // idem
  150. {
  151. 0x11, 0x23, 0xf1, 0x1e, 0x01, 0x22, 0xf0, 0x00,
  152. 0x08, 0x99, 0x78, 0x00, 0x08, 0x11, 0x9a, 0x48,
  153. 0x11, 0x23, 0xf0, 0x0e, 0x70, 0x01, 0xf8, 0x80,
  154. 0x22, 0x44, 0x44, 0x22, 0x22, 0x21, 0x00, 0x1e,
  155. 0x99, 0x10, 0x00, 0x1e, 0x19, 0x22, 0x44, 0x22,
  156. 0x22, 0x47, 0x00, 0x0e, 0x11, 0x22, 0x00, 0x00
  157. };
  158. typedef struct st_gameboy_chksum
  159. {
  160. unsigned short value;
  161. unsigned char header;
  162. } st_gameboy_chksum_t;
  163. static st_gameboy_chksum_t checksum;
  164. static st_gameboy_chksum_t gameboy_chksum (st_rominfo_t *rominfo);
  165. int
  166. gameboy_logo (st_rominfo_t *rominfo)
  167. {
  168. char dest_name[FILENAME_MAX];
  169. strcpy (dest_name, ucon64.rom);
  170. ucon64_file_handler (dest_name, NULL, 0);
  171. fcopy (ucon64.rom, 0, ucon64.file_size, dest_name, "wb");
  172. ucon64_fwrite ((unsigned char *)
  173. ((gameboy_header.rom_type >= 0x97 && gameboy_header.rom_type <= 0x99) ?
  174. rocket_logodata : gb_logodata),
  175. rominfo->buheader_len + GAMEBOY_HEADER_START + 4, GB_LOGODATA_LEN,
  176. dest_name, "r+b");
  177. printf (ucon64_msg[WROTE], dest_name);
  178. return 0;
  179. }
  180. int
  181. gameboy_n2gb (st_rominfo_t *rominfo, const char *nesrom)
  182. {
  183. #define EMULATOR_LEN 0x10000
  184. st_ines_header_t ines_header;
  185. int n = 0, crc = 0;
  186. unsigned char *buf;
  187. char dest_name[FILENAME_MAX];
  188. if (ucon64.file_size - rominfo->buheader_len != EMULATOR_LEN)
  189. {
  190. fprintf (stderr, "ERROR: %s does not appear to be KAMI's FC emulator\n", ucon64.rom);
  191. return -1;
  192. }
  193. ucon64_fread (&ines_header, 0, INES_HEADER_LEN, nesrom);
  194. if (memcmp (ines_header.signature, INES_SIG_S, 4))
  195. {
  196. fprintf (stderr, "ERROR: Only NES ROMs with iNES header are supported\n");
  197. return -1;
  198. }
  199. if (ines_header.prg_size != 1 || ines_header.chr_size != 1)
  200. {
  201. fprintf (stderr,
  202. "ERROR: Only NES ROMs with 0.1250 Mb of ROM (PRG) and 0.0625 Mb of VROM (CHR)\n"
  203. " are supported by KAMI's FC emulator\n");
  204. return -1;
  205. }
  206. if (!(buf = (unsigned char *) malloc (ucon64.file_size)))
  207. {
  208. fprintf (stderr, ucon64_msg[ROM_BUFFER_ERROR], ucon64.file_size);
  209. return -1;
  210. }
  211. ucon64_fread (buf, 0, ucon64.file_size, ucon64.rom);
  212. ucon64_fread (rominfo->buheader_len + buf + 0x4000, INES_HEADER_LEN,
  213. 0x4000 + 0x2000, nesrom); // read PRG & CHR data
  214. for (n = 0; n < ucon64.file_size - rominfo->buheader_len; n++)
  215. {
  216. if ((n == GAMEBOY_HEADER_START + 0x4e) || (n == GAMEBOY_HEADER_START + 0x4f))
  217. continue;
  218. else
  219. crc += buf[rominfo->buheader_len + n];
  220. }
  221. buf[rominfo->buheader_len + GAMEBOY_HEADER_START + 0x4e] = crc >> 8;
  222. buf[rominfo->buheader_len + GAMEBOY_HEADER_START + 0x4f] = crc;
  223. strcpy (dest_name, ucon64.rom);
  224. ucon64_file_handler (dest_name, NULL, 0);
  225. ucon64_fwrite (buf, 0, ucon64.file_size, dest_name, "wb");
  226. free (buf);
  227. printf (ucon64_msg[WROTE], dest_name);
  228. return 0;
  229. }
  230. static int
  231. gameboy_convert_data (st_rominfo_t *rominfo, unsigned char *conversion_table,
  232. const char *suffix)
  233. {
  234. char dest_name[FILENAME_MAX], src_name[FILENAME_MAX];
  235. unsigned char buf[MAXBUFSIZE];
  236. int x, n, n_bytes;
  237. strcpy (src_name, ucon64.rom);
  238. strcpy (dest_name, ucon64.rom);
  239. set_suffix (dest_name, suffix);
  240. ucon64_file_handler (dest_name, src_name, 0);
  241. x = rominfo->buheader_len;
  242. while ((n_bytes = ucon64_fread (buf, x, MAXBUFSIZE, src_name)))
  243. {
  244. for (n = 0; n < n_bytes; n++)
  245. buf[n] = conversion_table[(int) buf[n]];
  246. ucon64_fwrite (buf, x, n_bytes, dest_name, x == rominfo->buheader_len ? "wb" : "ab");
  247. x += n_bytes;
  248. }
  249. printf (ucon64_msg[WROTE], dest_name);
  250. return 0;
  251. }
  252. int
  253. gameboy_gbx (st_rominfo_t *rominfo)
  254. {
  255. unsigned char gbx2gbc[] =
  256. {
  257. 0xB4, 0xBC, 0xA4, 0xAC, 0x94, 0x9C, 0x84, 0x8C,
  258. 0xF4, 0xFC, 0xE4, 0xEC, 0xD4, 0xDC, 0xC4, 0xCC,
  259. 0x34, 0x3C, 0x24, 0x2C, 0x14, 0x1C, 0x04, 0x0C,
  260. 0x74, 0x7C, 0x64, 0x6C, 0x54, 0x5C, 0x44, 0x4C,
  261. 0xB5, 0xBD, 0xA5, 0xAD, 0x95, 0x9D, 0x85, 0x8D,
  262. 0xF5, 0xFD, 0xE5, 0xED, 0xD5, 0xDD, 0xC5, 0xCD,
  263. 0x35, 0x3D, 0x25, 0x2D, 0x15, 0x1D, 0x05, 0x0D,
  264. 0x75, 0x7D, 0x65, 0x6D, 0x55, 0x5D, 0x45, 0x4D,
  265. 0xB6, 0xBE, 0xA6, 0xAE, 0x96, 0x9E, 0x86, 0x8E,
  266. 0xF6, 0xFE, 0xE6, 0xEE, 0xD6, 0xDE, 0xC6, 0xCE,
  267. 0x36, 0x3E, 0x26, 0x2E, 0x16, 0x1E, 0x06, 0x0E,
  268. 0x76, 0x7E, 0x66, 0x6E, 0x56, 0x5E, 0x46, 0x4E,
  269. 0xB7, 0xBF, 0xA7, 0xAF, 0x97, 0x9F, 0x87, 0x8F,
  270. 0xF7, 0xFF, 0xE7, 0xEF, 0xD7, 0xDF, 0xC7, 0xCF,
  271. 0x37, 0x3F, 0x27, 0x2F, 0x17, 0x1F, 0x07, 0x0F,
  272. 0x77, 0x7F, 0x67, 0x6F, 0x57, 0x5F, 0x47, 0x4F,
  273. 0xB0, 0xB8, 0xA0, 0xA8, 0x90, 0x98, 0x80, 0x88,
  274. 0xF0, 0xF8, 0xE0, 0xE8, 0xD0, 0xD8, 0xC0, 0xC8,
  275. 0x30, 0x38, 0x20, 0x28, 0x10, 0x18, 0x00, 0x08,
  276. 0x70, 0x78, 0x60, 0x68, 0x50, 0x58, 0x40, 0x48,
  277. 0xB1, 0xB9, 0xA1, 0xA9, 0x91, 0x99, 0x81, 0x89,
  278. 0xF1, 0xF9, 0xE1, 0xE9, 0xD1, 0xD9, 0xC1, 0xC9,
  279. 0x31, 0x39, 0x21, 0x29, 0x11, 0x19, 0x01, 0x09,
  280. 0x71, 0x79, 0x61, 0x69, 0x51, 0x59, 0x41, 0x49,
  281. 0xB2, 0xBA, 0xA2, 0xAA, 0x92, 0x9A, 0x82, 0x8A,
  282. 0xF2, 0xFA, 0xE2, 0xEA, 0xD2, 0xDA, 0xC2, 0xCA,
  283. 0x32, 0x3A, 0x22, 0x2A, 0x12, 0x1A, 0x02, 0x0A,
  284. 0x72, 0x7A, 0x62, 0x6A, 0x52, 0x5A, 0x42, 0x4A,
  285. 0xB3, 0xBB, 0xA3, 0xAB, 0x93, 0x9B, 0x83, 0x8B,
  286. 0xF3, 0xFB, 0xE3, 0xEB, 0xD3, 0xDB, 0xC3, 0xCB,
  287. 0x33, 0x3B, 0x23, 0x2B, 0x13, 0x1B, 0x03, 0x0B,
  288. 0x73, 0x7B, 0x63, 0x6B, 0x53, 0x5B, 0x43, 0x4B
  289. };
  290. const char *old_suffix = get_suffix (ucon64.rom), *new_suffix;
  291. new_suffix = stricmp (old_suffix, ".GBX") ? ".GB" : ".GBC";
  292. return gameboy_convert_data (rominfo, gbx2gbc, new_suffix);
  293. }
  294. int
  295. gameboy_sgb (st_rominfo_t *rominfo)
  296. {
  297. unsigned char gbc2gbx[] =
  298. {
  299. 0x96, 0xB6, 0xD6, 0xF6, 0x16, 0x36, 0x56, 0x76,
  300. 0x97, 0xB7, 0xD7, 0xF7, 0x17, 0x37, 0x57, 0x77,
  301. 0x94, 0xB4, 0xD4, 0xF4, 0x14, 0x34, 0x54, 0x74,
  302. 0x95, 0xB5, 0xD5, 0xF5, 0x15, 0x35, 0x55, 0x75,
  303. 0x92, 0xB2, 0xD2, 0xF2, 0x12, 0x32, 0x52, 0x72,
  304. 0x93, 0xB3, 0xD3, 0xF3, 0x13, 0x33, 0x53, 0x73,
  305. 0x90, 0xB0, 0xD0, 0xF0, 0x10, 0x30, 0x50, 0x70,
  306. 0x91, 0xB1, 0xD1, 0xF1, 0x11, 0x31, 0x51, 0x71,
  307. 0x9E, 0xBE, 0xDE, 0xFE, 0x1E, 0x3E, 0x5E, 0x7E,
  308. 0x9F, 0xBF, 0xDF, 0xFF, 0x1F, 0x3F, 0x5F, 0x7F,
  309. 0x9C, 0xBC, 0xDC, 0xFC, 0x1C, 0x3C, 0x5C, 0x7C,
  310. 0x9D, 0xBD, 0xDD, 0xFD, 0x1D, 0x3D, 0x5D, 0x7D,
  311. 0x9A, 0xBA, 0xDA, 0xFA, 0x1A, 0x3A, 0x5A, 0x7A,
  312. 0x9B, 0xBB, 0xDB, 0xFB, 0x1B, 0x3B, 0x5B, 0x7B,
  313. 0x98, 0xB8, 0xD8, 0xF8, 0x18, 0x38, 0x58, 0x78,
  314. 0x99, 0xB9, 0xD9, 0xF9, 0x19, 0x39, 0x59, 0x79,
  315. 0x86, 0xA6, 0xC6, 0xE6, 0x06, 0x26, 0x46, 0x66,
  316. 0x87, 0xA7, 0xC7, 0xE7, 0x07, 0x27, 0x47, 0x67,
  317. 0x84, 0xA4, 0xC4, 0xE4, 0x04, 0x24, 0x44, 0x64,
  318. 0x85, 0xA5, 0xC5, 0xE5, 0x05, 0x25, 0x45, 0x65,
  319. 0x82, 0xA2, 0xC2, 0xE2, 0x02, 0x22, 0x42, 0x62,
  320. 0x83, 0xA3, 0xC3, 0xE3, 0x03, 0x23, 0x43, 0x63,
  321. 0x80, 0xA0, 0xC0, 0xE0, 0x00, 0x20, 0x40, 0x60,
  322. 0x81, 0xA1, 0xC1, 0xE1, 0x01, 0x21, 0x41, 0x61,
  323. 0x8E, 0xAE, 0xCE, 0xEE, 0x0E, 0x2E, 0x4E, 0x6E,
  324. 0x8F, 0xAF, 0xCF, 0xEF, 0x0F, 0x2F, 0x4F, 0x6F,
  325. 0x8C, 0xAC, 0xCC, 0xEC, 0x0C, 0x2C, 0x4C, 0x6C,
  326. 0x8D, 0xAD, 0xCD, 0xED, 0x0D, 0x2D, 0x4D, 0x6D,
  327. 0x8A, 0xAA, 0xCA, 0xEA, 0x0A, 0x2A, 0x4A, 0x6A,
  328. 0x8B, 0xAB, 0xCB, 0xEB, 0x0B, 0x2B, 0x4B, 0x6B,
  329. 0x88, 0xA8, 0xC8, 0xE8, 0x08, 0x28, 0x48, 0x68,
  330. 0x89, 0xA9, 0xC9, 0xE9, 0x09, 0x29, 0x49, 0x69
  331. };
  332. const char *old_suffix = get_suffix (ucon64.rom), *new_suffix;
  333. new_suffix = stricmp (old_suffix, ".GBC") ? ".GX" : ".GBX";
  334. return gameboy_convert_data (rominfo, gbc2gbx, new_suffix);
  335. }
  336. int
  337. gameboy_n (st_rominfo_t *rominfo, const char *name)
  338. {
  339. char buf[GB_NAME_LEN], dest_name[FILENAME_MAX];
  340. memset (buf, 0, GB_NAME_LEN);
  341. strncpy (buf, name, GB_NAME_LEN);
  342. strcpy (dest_name, ucon64.rom);
  343. ucon64_file_handler (dest_name, NULL, 0);
  344. fcopy (ucon64.rom, 0, ucon64.file_size, dest_name, "wb");
  345. ucon64_fwrite (buf, rominfo->buheader_len + GAMEBOY_HEADER_START + 0x34,
  346. GB_NAME_LEN, dest_name, "r+b");
  347. printf (ucon64_msg[WROTE], dest_name);
  348. return 0;
  349. }
  350. int
  351. gameboy_chk (st_rominfo_t *rominfo)
  352. {
  353. char buf[4], dest_name[FILENAME_MAX];
  354. strcpy (dest_name, ucon64.rom);
  355. ucon64_file_handler (dest_name, NULL, 0);
  356. fcopy (ucon64.rom, 0, ucon64.file_size, dest_name, "wb");
  357. buf[0] = checksum.header;
  358. buf[1] = rominfo->current_internal_crc >> 8;
  359. buf[2] = rominfo->current_internal_crc;
  360. ucon64_fwrite (buf, rominfo->buheader_len + GAMEBOY_HEADER_START + 0x4d, 3,
  361. dest_name, "r+b");
  362. dumper (stdout, buf, 3, GAMEBOY_HEADER_START + rominfo->buheader_len + 0x4d, DUMPER_HEX);
  363. printf (ucon64_msg[WROTE], dest_name);
  364. return 0;
  365. }
  366. int
  367. gameboy_mgd (st_rominfo_t *rominfo)
  368. // TODO: convert the ROM data
  369. {
  370. char src_name[FILENAME_MAX], dest_name[FILENAME_MAX];
  371. int size = ucon64.file_size - rominfo->buheader_len;
  372. strcpy (src_name, ucon64.rom);
  373. mgd_make_name (ucon64.rom, UCON64_GB, size, dest_name);
  374. ucon64_file_handler (dest_name, src_name, OF_FORCE_BASENAME);
  375. fcopy (src_name, rominfo->buheader_len, size, dest_name, "wb");
  376. printf (ucon64_msg[WROTE], dest_name);
  377. remove_temp_file ();
  378. mgd_write_index_file ((char *) basename2 (dest_name), 1);
  379. return 0;
  380. }
  381. int
  382. gameboy_ssc (st_rominfo_t *rominfo)
  383. // TODO: convert the ROM data
  384. {
  385. st_unknown_header_t header;
  386. char src_name[FILENAME_MAX], dest_name[FILENAME_MAX];
  387. const char *p = NULL;
  388. int size = ucon64.file_size - rominfo->buheader_len;
  389. memset (&header, 0, UNKNOWN_HEADER_LEN);
  390. header.size_low = size / 8192;
  391. header.size_high = size / 8192 >> 8;
  392. header.id1 = 0xaa;
  393. header.id2 = 0xbb;
  394. #if 0 // TODO: find out correct value. 2 is used for Magic Super Griffin
  395. header.type = 2;
  396. #endif
  397. strcpy (src_name, ucon64.rom);
  398. p = basename2 (ucon64.rom);
  399. // TODO: find out if this is correct (giving the file name a prefix)
  400. if ((p[0] == 'G' || p[0] == 'g') && (p[1] == 'B' || p[1] == 'b'))
  401. strcpy (dest_name, p);
  402. else
  403. sprintf (dest_name, "gb%s", p);
  404. set_suffix (dest_name, ".gb");
  405. ucon64_file_handler (dest_name, src_name, 0);
  406. ucon64_fwrite (&header, 0, UNKNOWN_HEADER_LEN, dest_name, "wb");
  407. fcopy (src_name, rominfo->buheader_len, size, dest_name, "ab");
  408. printf (ucon64_msg[WROTE], dest_name);
  409. remove_temp_file ();
  410. return 0;
  411. }
  412. int
  413. gameboy_init (st_rominfo_t *rominfo)
  414. {
  415. int result = -1, value, x;
  416. char buf[MAXBUFSIZE];
  417. static const char *gameboy_romtype1[0x20] =
  418. {
  419. "ROM only",
  420. "ROM + MBC1",
  421. "ROM + MBC1 + RAM",
  422. "ROM + MBC1 + RAM + Battery",
  423. NULL,
  424. "ROM + MBC2",
  425. "ROM + MBC2 + Battery",
  426. NULL,
  427. "ROM + RAM", // correct? - dbjh
  428. "ROM + RAM + Battery", // correct? - dbjh
  429. NULL,
  430. "ROM + MMM01",
  431. "ROM + MMM01 + SRAM",
  432. "ROM + MMM01 + SRAM + Battery",
  433. NULL,
  434. "ROM + MBC3 + Battery + Timer",
  435. "ROM + MBC3 + RAM + Battery + Timer",
  436. "ROM + MBC3",
  437. "ROM + MBC3 + RAM",
  438. "ROM + MBC3 + RAM + Battery",
  439. NULL,
  440. NULL,
  441. NULL,
  442. NULL,
  443. NULL,
  444. "ROM + MBC5",
  445. "ROM + MBC5 + RAM",
  446. "ROM + MBC5 + RAM + Battery",
  447. "ROM + MBC5 + Rumble",
  448. "ROM + MBC5 + SRAM + Rumble",
  449. "ROM + MBC5 + SRAM + Battery + Rumble",
  450. "Nintendo Pocket Camera"
  451. },
  452. *gameboy_romtype2[3] =
  453. {
  454. "Rocket Games",
  455. NULL,
  456. "Rocket Games 2-in-1"
  457. },
  458. *gameboy_romtype3[3] =
  459. {
  460. "Bandai TAMA5",
  461. "Hudson HuC-3",
  462. "Hudson HuC-1"
  463. },
  464. *str;
  465. rominfo->buheader_len = UCON64_ISSET (ucon64.buheader_len) ? ucon64.buheader_len : 0;
  466. ucon64_fread (&gameboy_header, rominfo->buheader_len + GAMEBOY_HEADER_START,
  467. GAMEBOY_HEADER_LEN, ucon64.rom);
  468. if (gameboy_header.opcode1 == 0x00 && gameboy_header.opcode2 == 0xc3)
  469. result = 0;
  470. else
  471. {
  472. rominfo->buheader_len = UCON64_ISSET (ucon64.buheader_len) ?
  473. ucon64.buheader_len : (int) SSC_HEADER_LEN;
  474. ucon64_fread (&gameboy_header, rominfo->buheader_len + GAMEBOY_HEADER_START,
  475. GAMEBOY_HEADER_LEN, ucon64.rom);
  476. if (gameboy_header.opcode1 == 0x00 && gameboy_header.opcode2 == 0xc3)
  477. result = 0;
  478. else
  479. result = -1;
  480. }
  481. if (ucon64.console == UCON64_GB)
  482. result = 0;
  483. rominfo->header_start = GAMEBOY_HEADER_START;
  484. rominfo->header_len = GAMEBOY_HEADER_LEN;
  485. rominfo->header = &gameboy_header;
  486. // internal ROM name
  487. strncpy (rominfo->name, (const char *) gameboy_header.name, GB_NAME_LEN);
  488. rominfo->name[GB_NAME_LEN] = 0; // terminate string
  489. // ROM maker
  490. if (gameboy_header.maker == 0x33)
  491. {
  492. int ih = gameboy_header.maker_high <= '9' ?
  493. gameboy_header.maker_high - '0' : gameboy_header.maker_high - 'A' + 10,
  494. il = gameboy_header.maker_low <= '9' ?
  495. gameboy_header.maker_low - '0' : gameboy_header.maker_low - 'A' + 10;
  496. x = ih * 36 + il;
  497. }
  498. else
  499. x = (gameboy_header.maker >> 4) * 36 + (gameboy_header.maker & 0x0f);
  500. /*
  501. I added the first if statement, because I didn't want to expand
  502. nintendo_maker by a large amount for only one publisher and because index 0
  503. is used when the publisher code is unknown (so it shouldn't be set to
  504. "Rocket Games"). - dbjh
  505. */
  506. if (x == 33 * 36 + 33 || x == 0) // publisher code XX/00
  507. x = 2; // Rocket Games
  508. else if (x < 0 || x >= NINTENDO_MAKER_LEN)
  509. x = 0;
  510. rominfo->maker = NULL_TO_UNKNOWN_S (nintendo_maker[x]);
  511. // ROM country
  512. rominfo->country = gameboy_header.country == 0 ? "Japan" : "U.S.A. & Europe";
  513. // misc stuff
  514. // Don't move division by 4 to shift parameter (gameboy_header.rom_size can be < 2)
  515. sprintf (buf, "Internal size: %.4f Mb\n", (1 << gameboy_header.rom_size) / 4.0f);
  516. strcat (rominfo->misc, buf);
  517. if (gameboy_header.rom_type <= 0x1f)
  518. str = NULL_TO_UNKNOWN_S (gameboy_romtype1[gameboy_header.rom_type]);
  519. else if (gameboy_header.rom_type >= 0x97 && gameboy_header.rom_type <= 0x99)
  520. str = gameboy_romtype2[gameboy_header.rom_type - 0x97];
  521. else if (gameboy_header.rom_type >= 0xfd)
  522. str = gameboy_romtype3[gameboy_header.rom_type - 0xfd];
  523. else
  524. str = "Unknown";
  525. sprintf (buf, "ROM type: %s\n", str);
  526. strcat (rominfo->misc, buf);
  527. if (!gameboy_header.sram_size)
  528. sprintf (buf, "Save RAM: No\n");
  529. else
  530. {
  531. value = (gameboy_header.sram_size & 7) << 1; // 0/1/2/4/5
  532. if (value)
  533. value = 1 << (value - 1);
  534. sprintf (buf, "Save RAM: Yes, %d kBytes\n", value);
  535. }
  536. strcat (rominfo->misc, buf);
  537. sprintf (buf, "Version: 1.%d\n", gameboy_header.version);
  538. strcat (rominfo->misc, buf);
  539. sprintf (buf, "Game Boy type: %s\n",
  540. (gameboy_header.gb_type == 0x80) ? "Color" :
  541. // (OFFSET (gameboy_header, 0x46) == 0x03) ? "Super" :
  542. "Standard (4 colors)");
  543. strcat (rominfo->misc, buf);
  544. value = gameboy_header.start_high << 8;
  545. value += gameboy_header.start_low;
  546. sprintf (buf, "Start address: 0x%04x\n", value);
  547. strcat (rominfo->misc, buf);
  548. strcat (rominfo->misc, "Logo data: ");
  549. if (memcmp (gameboy_header.logo,
  550. (gameboy_header.rom_type >= 0x97 && gameboy_header.rom_type <= 0x99) ?
  551. rocket_logodata : gb_logodata,
  552. GB_LOGODATA_LEN) == 0)
  553. {
  554. #ifdef USE_ANSI_COLOR
  555. if (ucon64.ansi_color)
  556. strcat (rominfo->misc, "\x1b[01;32mOk\x1b[0m");
  557. else
  558. #endif
  559. strcat (rominfo->misc, "Ok");
  560. }
  561. else
  562. {
  563. #ifdef USE_ANSI_COLOR
  564. if (ucon64.ansi_color)
  565. strcat (rominfo->misc, "\x1b[01;31mBad\x1b[0m");
  566. else
  567. #endif
  568. strcat (rominfo->misc, "Bad");
  569. }
  570. if (!UCON64_ISSET (ucon64.do_not_calc_crc) && result == 0)
  571. {
  572. rominfo->has_internal_crc = 1;
  573. rominfo->internal_crc_len = 2;
  574. checksum = gameboy_chksum (rominfo);
  575. rominfo->current_internal_crc = checksum.value;
  576. rominfo->internal_crc = (gameboy_header.checksum_high << 8) +
  577. gameboy_header.checksum_low;
  578. x = gameboy_header.header_checksum;
  579. sprintf (rominfo->internal_crc2,
  580. "Header checksum: %s, 0x%02x (calculated) %c= 0x%02x (internal)",
  581. #ifdef USE_ANSI_COLOR
  582. ucon64.ansi_color ?
  583. ((checksum.header == x) ?
  584. "\x1b[01;32mOk\x1b[0m" : "\x1b[01;31mBad\x1b[0m")
  585. :
  586. ((checksum.header == x) ? "Ok" : "Bad"),
  587. #else
  588. (checksum.header == x) ? "Ok" : "Bad",
  589. #endif
  590. checksum.header, (checksum.header == x) ? '=' : '!', x);
  591. }
  592. rominfo->console_usage = gameboy_usage[0].help;
  593. rominfo->copier_usage = (!rominfo->buheader_len ? mgd_usage[0].help : ssc_usage[0].help);
  594. return result;
  595. }
  596. st_gameboy_chksum_t
  597. gameboy_chksum (st_rominfo_t *rominfo)
  598. {
  599. st_gameboy_chksum_t sum = { 0, 0 };
  600. unsigned char *rom_buffer;
  601. int size = ucon64.file_size - rominfo->buheader_len, i;
  602. if (!(rom_buffer = (unsigned char *) malloc (size)))
  603. {
  604. fprintf (stderr, ucon64_msg[ROM_BUFFER_ERROR], size);
  605. return sum;
  606. }
  607. ucon64_fread (rom_buffer, rominfo->buheader_len, size, ucon64.rom);
  608. for (i = GAMEBOY_HEADER_START + 0x34; i < GAMEBOY_HEADER_START + 0x4d; i++)
  609. sum.header += ~rom_buffer[i];
  610. for (i = 0; i < size; i++)
  611. sum.value += rom_buffer[i];
  612. sum.value -= (rom_buffer[GAMEBOY_HEADER_START + 0x4d] - sum.header) +
  613. rom_buffer[GAMEBOY_HEADER_START + 0x4e] +
  614. rom_buffer[GAMEBOY_HEADER_START + 0x4f];
  615. free (rom_buffer);
  616. return sum;
  617. }