16 /* Value used to fill the unused portions of the image */
17 /* FFh puts less stress on a EPROM/Flash */
18 #define FILLVALUE 0xFF
20 #define CARTSIZE ((unsigned long)nb_rom_banks*16UL*1024UL)
22 #define SEGSIZE (CARTSIZE/NBSEG)
24 #define ROMSIZE 0x8000UL
25 #define BANKSTART 0x4000UL
26 #define BANKSIZE 0x4000UL
28 unsigned char *cart[NBSEG];
34 char cart_name[16] = "";
36 patch* patches = NULL;
41 unsigned long pos, chk;
46 for(i = 0; i < NBSEG; i++) {
47 if((cart[i] = malloc(SEGSIZE)) == NULL) {
48 fprintf(stderr, "ERROR: can't allocate %dth segment of memory (%d bytes)\n", i, (int)SEGSIZE);
51 memset(cart[i], FILLVALUE, SEGSIZE);
58 pos = rtval[0] | (rtval[1]<<8);
60 pos = rtval[1] | (rtval[0]<<8);
62 /* Perform some validity checks */
64 fprintf(stderr, "ERROR: address overflow (addr %lx >= %lx)\n", pos, ROMSIZE);
67 if(current_rom_bank >= nb_rom_banks) {
68 fprintf(stderr, "ERROR: bank overflow (addr %x > %x)\n", current_rom_bank, nb_rom_banks);
71 if(current_rom_bank > 0 && pos < BANKSTART) {
72 fprintf(stderr, "ERROR: address underflow (addr %lx < %lx)\n", pos, BANKSTART);
75 if(nb_rom_banks == 2 && current_rom_bank > 0) {
76 fprintf(stderr, "ERROR: only 1 32kB segment with 2 bank\n");
79 if(current_rom_bank > 1)
80 pos += (current_rom_bank-1)*BANKSIZE;
81 for(i = 2; i < rtcnt; i++) {
84 if(cart[pos/SEGSIZE][pos%SEGSIZE] != FILLVALUE)
85 fprintf(stderr, "WARNING: possibly wrote twice at addr %lx (%02X->%02X)\n", pos, rtval[i], cart[pos/SEGSIZE][pos%SEGSIZE]);
86 cart[pos/SEGSIZE][pos%SEGSIZE] = rtval[i];
88 fprintf(stderr, "ERROR: cartridge size overflow (addr %lx >= %lx)\n", pos, CARTSIZE);
97 if(cart_name[0] == 0 && linkp->f_idp != NULL) {
98 for(i = strlen(linkp->f_idp);
99 i > 0 && (isalnum((unsigned char)linkp->f_idp[i-1]) || linkp->f_idp[i-1] == '.');
102 for(pos = 0; pos < 16 && linkp->f_idp[i] != '.'; pos++, i++)
103 cart_name[pos] = toupper((unsigned char)linkp->f_idp[i]);
107 for(pos = 0x0134, i = 0;
108 pos < 0x0144 && cart_name[i];
110 cart[pos/SEGSIZE][pos%SEGSIZE] = cart_name[i];
111 for(; pos < 0x0144; pos++)
112 cart[pos/SEGSIZE][pos%SEGSIZE] = 0;
113 cart[0x147/SEGSIZE][0x147%SEGSIZE] = mbc_type;
114 switch(nb_rom_banks) {
116 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 0;
119 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 1;
122 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 2;
125 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 3;
128 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 4;
131 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 5;
134 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 6;
137 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 7;
140 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 8;
143 fprintf(stderr, "WARNING: unsupported number of ROM banks (%d)\n", nb_rom_banks);
144 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 0;
147 switch(nb_ram_banks) {
149 cart[0x149/SEGSIZE][0x149%SEGSIZE] = 0;
152 cart[0x149/SEGSIZE][0x149%SEGSIZE] = 2;
155 cart[0x149/SEGSIZE][0x149%SEGSIZE] = 3;
158 cart[0x149/SEGSIZE][0x149%SEGSIZE] = 4;
161 fprintf(stderr, "WARNING: unsupported number of RAM banks (%d)\n", nb_ram_banks);
162 cart[0x149/SEGSIZE][0x149%SEGSIZE] = 0;
166 /* Patch before calculating the checksum */
168 for(p = patches; p; p = p->next)
169 cart[p->addr/SEGSIZE][p->addr%SEGSIZE] = p->value;
171 /* Update complement checksum */
173 for(pos = 0x0134; pos < 0x014D; pos++)
174 chk += cart[pos/SEGSIZE][pos%SEGSIZE];
175 cart[0x014D/SEGSIZE][0x014D%SEGSIZE] = (unsigned char)(0xE7 - (chk&0xFF));
176 /* Update checksum */
178 cart[0x014E/SEGSIZE][0x014E%SEGSIZE] = 0;
179 cart[0x014F/SEGSIZE][0x014F%SEGSIZE] = 0;
180 for(i = 0; i < NBSEG; i++)
181 for(pos = 0; pos < SEGSIZE; pos++)
183 cart[0x014E/SEGSIZE][0x014E%SEGSIZE] = (unsigned char)((chk>>8)&0xFF);
184 cart[0x014F/SEGSIZE][0x014F%SEGSIZE] = (unsigned char)(chk&0xFF);
186 for(i = 0; i < NBSEG; i++) {
188 res = fwrite(cart[i], 1, SEGSIZE, ofp);
189 assert(res == SEGSIZE);