15 /* Value used to fill the unused portions of the image */
16 /* FFh puts less stress on a EPROM/Flash */
17 #define FILLVALUE 0xFF
19 #define CARTSIZE ((unsigned long)nb_rom_banks*16UL*1024UL)
21 #define SEGSIZE (CARTSIZE/NBSEG)
23 #define ROMSIZE 0x8000UL
24 #define BANKSTART 0x4000UL
25 #define BANKSIZE 0x4000UL
27 unsigned char *cart[NBSEG];
33 char cart_name[16] = "";
35 patch* patches = NULL;
40 unsigned long pos, chk;
45 for(i = 0; i < NBSEG; i++) {
46 if((cart[i] = malloc(SEGSIZE)) == NULL) {
47 fprintf(stderr, "ERROR: can't allocate %dth segment of memory (%d bytes)\n", i, (int)SEGSIZE);
50 memset(cart[i], FILLVALUE, SEGSIZE);
57 pos = rtval[0] | (rtval[1]<<8);
59 pos = rtval[1] | (rtval[0]<<8);
61 /* Perform some validity checks */
63 fprintf(stderr, "ERROR: address overflow (addr %lx >= %lx)\n", pos, ROMSIZE);
66 if(current_rom_bank >= nb_rom_banks) {
67 fprintf(stderr, "ERROR: bank overflow (addr %x > %x)\n", current_rom_bank, nb_rom_banks);
70 if(current_rom_bank > 0 && pos < BANKSTART) {
71 fprintf(stderr, "ERROR: address underflow (addr %lx < %lx)\n", pos, BANKSTART);
74 if(nb_rom_banks == 2 && current_rom_bank > 0) {
75 fprintf(stderr, "ERROR: only 1 32kB segment with 2 bank\n");
78 if(current_rom_bank > 1)
79 pos += (current_rom_bank-1)*BANKSIZE;
80 for(i = 2; i < rtcnt; i++) {
83 if(cart[pos/SEGSIZE][pos%SEGSIZE] != FILLVALUE)
84 fprintf(stderr, "WARNING: possibly wrote twice at addr %lx (%02X->%02X)\n", pos, rtval[i], cart[pos/SEGSIZE][pos%SEGSIZE]);
85 cart[pos/SEGSIZE][pos%SEGSIZE] = rtval[i];
87 fprintf(stderr, "ERROR: cartridge size overflow (addr %lx >= %lx)\n", pos, CARTSIZE);
96 if(cart_name[0] == 0 && linkp->f_idp != NULL) {
97 for(i = strlen(linkp->f_idp);
98 i > 0 && (isalnum(linkp->f_idp[i-1]) || linkp->f_idp[i-1] == '.');
101 for(pos = 0; pos < 16 && linkp->f_idp[i] != '.'; pos++, i++)
102 cart_name[pos] = toupper(linkp->f_idp[i]);
106 for(pos = 0x0134, i = 0;
107 pos < 0x0144 && cart_name[i];
109 cart[pos/SEGSIZE][pos%SEGSIZE] = cart_name[i];
110 for(; pos < 0x0144; pos++)
111 cart[pos/SEGSIZE][pos%SEGSIZE] = 0;
112 cart[0x147/SEGSIZE][0x147%SEGSIZE] = mbc_type;
113 switch(nb_rom_banks) {
115 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 0;
118 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 1;
121 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 2;
124 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 3;
127 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 4;
130 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 5;
133 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 6;
136 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 7;
139 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 8;
142 fprintf(stderr, "WARNING: unsupported number of ROM banks (%d)\n", nb_rom_banks);
143 cart[0x148/SEGSIZE][0x148%SEGSIZE] = 0;
146 switch(nb_ram_banks) {
148 cart[0x149/SEGSIZE][0x149%SEGSIZE] = 0;
151 cart[0x149/SEGSIZE][0x149%SEGSIZE] = 2;
154 cart[0x149/SEGSIZE][0x149%SEGSIZE] = 3;
157 cart[0x149/SEGSIZE][0x149%SEGSIZE] = 4;
160 fprintf(stderr, "WARNING: unsupported number of RAM banks (%d)\n", nb_ram_banks);
161 cart[0x149/SEGSIZE][0x149%SEGSIZE] = 0;
165 /* Patch before calculating the checksum */
167 for(p = patches; p; p = p->next)
168 cart[p->addr/SEGSIZE][p->addr%SEGSIZE] = p->value;
170 /* Update complement checksum */
172 for(pos = 0x0134; pos < 0x014D; pos++)
173 chk += cart[pos/SEGSIZE][pos%SEGSIZE];
174 cart[0x014D/SEGSIZE][0x014D%SEGSIZE] = 0xE7 - (chk&0xFF);
175 /* Update checksum */
177 cart[0x014E/SEGSIZE][0x014E%SEGSIZE] = 0;
178 cart[0x014F/SEGSIZE][0x014F%SEGSIZE] = 0;
179 for(i = 0; i < NBSEG; i++)
180 for(pos = 0; pos < SEGSIZE; pos++)
182 cart[0x014E/SEGSIZE][0x014E%SEGSIZE] = (chk>>8)&0xFF;
183 cart[0x014F/SEGSIZE][0x014F%SEGSIZE] = chk&0xFF;
185 for(i = 0; i < NBSEG; i++)
186 fwrite(cart[i], 1, SEGSIZE, ofp);