1 /*
2 * blackbox8.c - Cartridge handling, Blackbox v8 cart.
3 *
4 * Written by
5 * groepaz <groepaz@gmx.net>
6 *
7 * This file is part of VICE, the Versatile Commodore Emulator.
8 * See README for copyright notice.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 * 02111-1307 USA.
24 *
25 */
26
27 #include "vice.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #define CARTRIDGE_INCLUDE_SLOTMAIN_API
34 #include "c64cartsystem.h"
35 #undef CARTRIDGE_INCLUDE_SLOTMAIN_API
36 #include "c64mem.h"
37 #include "cartio.h"
38 #include "cartridge.h"
39 #include "export.h"
40 #include "blackbox8.h"
41 #include "monitor.h"
42 #include "snapshot.h"
43 #include "types.h"
44 #include "util.h"
45 #include "crt.h"
46
47 /*
48 Black Box V8
49
50 32k or 64k, 2 or 4 16K banks
51
52 writing to IO2 sets the cartridge config:
53 A0 - EXROM
54 A1 - GAME
55 A2 - bank lsb
56 A3 - bank msb
57 */
58
59 static int bb8_rom_banks = 4;
60 static uint8_t regval = 0;
61
62 /* some prototypes are needed */
63 static void blackbox8_io2_store(uint16_t addr, uint8_t value);
64 static int blackbox8_dump(void);
65
66 static io_source_t final3_io2_device = {
67 CARTRIDGE_NAME_BLACKBOX8, /* name of the device */
68 IO_DETACH_CART, /* use cartridge ID to detach the device when involved in a read-collision */
69 IO_DETACH_NO_RESOURCE, /* does not use a resource for detach */
70 0xdf00, 0xdfff, 0xff, /* range for the device, regs:$df00-$dfff */
71 1, /* read is always valid */
72 blackbox8_io2_store, /* store function */
73 NULL, /* NO poke function */
74 NULL, /* NO read function */
75 NULL, /* NO peek function */
76 blackbox8_dump, /* device state information dump function */
77 CARTRIDGE_BLACKBOX8, /* cartridge ID */
78 IO_PRIO_NORMAL, /* normal priority, device read needs to be checked for collisions */
79 0 /* insertion order, gets filled in by the registration function */
80 };
81
82 static io_source_list_t *final3_io2_list_item = NULL;
83
84 static const export_resource_t export_res_v3 = {
85 CARTRIDGE_NAME_BLACKBOX8, 1, 1, NULL, &final3_io2_device, CARTRIDGE_BLACKBOX8
86 };
87
88 /* ---------------------------------------------------------------------*/
89
blackbox8_io2_store(uint16_t addr,uint8_t value)90 void blackbox8_io2_store(uint16_t addr, uint8_t value)
91 {
92 uint8_t mode, bank;
93
94 regval = addr & 0x0f;
95
96 /* printf("io2 write %04x %02x\n", addr, value); */
97 mode = (addr & 3) ^ 1;
98 bank = ((addr >> 2) & 3) ^ 3;
99 bank &= (bb8_rom_banks - 1);
100 roml_bank = romh_bank = bank;
101 /* printf("switch to mode: %s bank: %d\n", cart_config_string(mode), bank); */
102 cart_config_changed_slotmain(mode, mode | (bank << CMODE_BANK_SHIFT), CMODE_WRITE);
103 }
104
blackbox8_dump(void)105 static int blackbox8_dump(void)
106 {
107 mon_out("Bank: %d of %d, mode: %s\n",
108 ((regval >> 2) ^ 3) & (bb8_rom_banks - 1), bb8_rom_banks,
109 cart_config_string((regval & 3) ^ 1));
110 return 0;
111 }
112
113 /* ---------------------------------------------------------------------*/
114
blackbox8_config_init(void)115 void blackbox8_config_init(void)
116 {
117 roml_bank = romh_bank = (bb8_rom_banks - 1);
118 cart_config_changed_slotmain(CMODE_16KGAME, CMODE_16KGAME | (roml_bank << CMODE_BANK_SHIFT), CMODE_READ);
119 }
120
blackbox8_config_setup(uint8_t * rawcart)121 void blackbox8_config_setup(uint8_t *rawcart)
122 {
123 int i;
124 for (i = 0; i <= bb8_rom_banks; i++) {
125 memcpy(&roml_banks[0x2000 * i], &rawcart[0x0000 + (0x4000 * i)], 0x2000);
126 memcpy(&romh_banks[0x2000 * i], &rawcart[0x2000 + (0x4000 * i)], 0x2000);
127 }
128 roml_bank = romh_bank = (bb8_rom_banks - 1);
129 cart_config_changed_slotmain(CMODE_16KGAME, CMODE_16KGAME | (roml_bank << CMODE_BANK_SHIFT), CMODE_READ);
130 }
131
132 /* ---------------------------------------------------------------------*/
133
blackbox8_common_attach(void)134 static int blackbox8_common_attach(void)
135 {
136 if (export_add(&export_res_v3) < 0) {
137 return -1;
138 }
139
140 final3_io2_list_item = io_source_register(&final3_io2_device);
141
142 return 0;
143 }
144
blackbox8_bin_attach(const char * filename,uint8_t * rawcart)145 int blackbox8_bin_attach(const char *filename, uint8_t *rawcart)
146 {
147 bb8_rom_banks = 2;
148 if (util_file_load(filename, rawcart, 0x8000, UTIL_FILE_LOAD_SKIP_ADDRESS) < 0) {
149 if (util_file_load(filename, rawcart, 0x10000, UTIL_FILE_LOAD_SKIP_ADDRESS) < 0) {
150 return -1;
151 }
152 bb8_rom_banks = 4;
153 }
154
155 return blackbox8_common_attach();
156 }
157
blackbox8_crt_attach(FILE * fd,uint8_t * rawcart)158 int blackbox8_crt_attach(FILE *fd, uint8_t *rawcart)
159 {
160 crt_chip_header_t chip;
161 int i, banks = 0;
162
163 for (i = 0; i <= 4; i++) {
164 if (crt_read_chip_header(&chip, fd)) {
165 break;
166 }
167
168 if (chip.bank > 4 || chip.size != 0x4000) {
169 break;
170 }
171
172 if (crt_read_chip(rawcart, chip.bank << 14, &chip, fd)) {
173 break;
174 }
175 ++banks;
176 }
177
178 if ((banks != 2) && (banks != 4)) {
179 return -1;
180 }
181 bb8_rom_banks = banks;
182
183 return blackbox8_common_attach();
184 }
185
blackbox8_detach(void)186 void blackbox8_detach(void)
187 {
188 export_remove(&export_res_v3);
189 io_source_unregister(final3_io2_list_item);
190 final3_io2_list_item = NULL;
191 }
192
193 /* ---------------------------------------------------------------------*/
194
195 /* CARTBB8 snapshot module format:
196
197 type | name | version | description
198 -------------------------------------------
199 BYTE | ROML banks | 1.1 | amount of ROML banks
200 BYTE | register | 1.1 | register
201 ARRAY | ROML | 1.1 | 32768 or 65536 BYTES of ROML data
202 ARRAY | ROMH | 1.1 | 32768 or 65536 BYTES of ROML data
203
204 */
205
206 static const char snap_module_name[] = "CARTBB8";
207 #define SNAP_MAJOR 1
208 #define SNAP_MINOR 1
209
blackbox8_snapshot_write_module(snapshot_t * s)210 int blackbox8_snapshot_write_module(snapshot_t *s)
211 {
212 snapshot_module_t *m;
213
214 m = snapshot_module_create(s, snap_module_name, SNAP_MAJOR, SNAP_MINOR);
215
216 if (m == NULL) {
217 return -1;
218 }
219
220 if (0
221 || SMW_B(m, (uint8_t)bb8_rom_banks) < 0
222 || SMW_B(m, regval) < 0
223 || SMW_BA(m, roml_banks, 0x2000 * bb8_rom_banks) < 0
224 || SMW_BA(m, romh_banks, 0x2000 * bb8_rom_banks) < 0) {
225 snapshot_module_close(m);
226 return -1;
227 }
228
229 return snapshot_module_close(m);
230 }
231
blackbox8_snapshot_read_module(snapshot_t * s)232 int blackbox8_snapshot_read_module(snapshot_t *s)
233 {
234 uint8_t vmajor, vminor;
235 snapshot_module_t *m;
236
237 m = snapshot_module_open(s, snap_module_name, &vmajor, &vminor);
238
239 if (m == NULL) {
240 return -1;
241 }
242
243 /* Do not accept versions higher than current */
244 if (snapshot_version_is_bigger(vmajor, vminor, SNAP_MAJOR, SNAP_MINOR)) {
245 snapshot_set_error(SNAPSHOT_MODULE_HIGHER_VERSION);
246 goto fail;
247 }
248
249 if (0
250 || SMR_B_INT(m, &bb8_rom_banks) < 0
251 || SMR_B(m, ®val) < 0
252 || SMR_BA(m, roml_banks, 0x2000 * bb8_rom_banks) < 0
253 || SMR_BA(m, romh_banks, 0x2000 * bb8_rom_banks) < 0) {
254 goto fail;
255 }
256
257 snapshot_module_close(m);
258
259 return blackbox8_common_attach();
260
261 fail:
262 snapshot_module_close(m);
263 return -1;
264 }
265