1 // SPDX-License-Identifier: MIT
3 #include "platforms/platforms.h"
4 #include "pistorm-dev/pistorm-dev-enums.h"
5 #include "amiga-autoconf.h"
12 #define Z2_BOOTROM 0x1
14 // PiStorm Zorro II AutoConfig Fast RAM ROM
15 static unsigned char ac_fast_ram_rom[] = {
16 Z2_Z2 | Z2_FAST, AC_MEM_SIZE_8MB, // 00/02, link into memory free list, 8 MB
17 0x6, 0x9, // 06/09, product id
18 0x8, 0x0, // 08/0a, preference to 8 MB space
19 0x0, 0x0, // 0c/0e, reserved
20 PISTORM_AC_MANUF_ID, // Manufacturer ID
21 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x2, 0x0, // 18/.../26, serial
22 0x0, 0x0, 0x0, 0x0, // Optional BOOT ROM vector
25 // PiSCSI AutoConfig Device ROM
26 unsigned char ac_piscsi_rom[] = {
27 Z2_Z2 | Z2_BOOTROM, AC_MEM_SIZE_64KB, // 00/01, Z2, bootrom, 64 KB
28 0x6, 0xA, // 06/0A, product id
29 0x0, 0x0, // 00/0a, any space where it fits
30 0x0, 0x0, // 0c/0e, reserved
31 PISTORM_AC_MANUF_ID, // Manufacturer ID
32 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x2, 0x1, // 18/.../26, serial
33 0x4, 0x0, 0x0, 0x0, // Optional BOOT ROM vector
36 // PiStorm Device Interaction ROM
37 unsigned char ac_pistorm_rom[] = {
38 Z2_Z2, AC_MEM_SIZE_64KB, // 00/01, Z2, bootrom, 64 KB
39 0x6, 0xB, // 06/0A, product id
40 0x0, 0x0, // 00/0a, any space where it fits
41 0x0, 0x0, // 0c/0e, reserved
42 PISTORM_AC_MANUF_ID, // Manufacturer ID
43 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x2, 0x2, // 18/.../26, serial
44 0x4, 0x0, 0x0, 0x0, // Optional BOOT ROM vector
47 // A314 Emulation ROM (currently unused)
48 static unsigned char ac_a314_rom[] = {
49 0xc, AC_MEM_SIZE_64KB, // 00/02, 64 kB
50 0xa, 0x3, // 04/06, product id
51 0x0, 0x0, // 08/0a, any space okay
52 0x0, 0x0, // 0c/0e, reserved
53 0x0, 0x7, 0xd, 0xb, // 10/12/14/16, mfg id
54 0xa, 0x3, 0x1, 0x4, 0x0, 0x0, 0x0, 0x0, // 18/.../26, serial
55 0x0, 0x0, 0x0, 0x0, // Optional BOOT ROM vector
58 int ac_z2_current_pic = 0;
59 int ac_z2_pic_count = 0;
61 int ac_z2_type[AC_PIC_LIMIT];
62 int ac_z2_index[AC_PIC_LIMIT];
63 unsigned int ac_base[AC_PIC_LIMIT];
65 int ac_z3_current_pic = 0;
66 int ac_z3_pic_count = 0;
68 int ac_z3_type[AC_PIC_LIMIT];
69 int ac_z3_index[AC_PIC_LIMIT];
71 uint32_t piscsi_base = 0, pistorm_dev_base = 0;
72 extern uint8_t *piscsi_rom_ptr;
74 unsigned char get_autoconf_size(int size) {
75 if (size == 8 * SIZE_MEGA)
76 return AC_MEM_SIZE_8MB;
77 if (size == 4 * SIZE_MEGA)
78 return AC_MEM_SIZE_4MB;
79 if (size == 2 * SIZE_MEGA)
80 return AC_MEM_SIZE_2MB;
82 return AC_MEM_SIZE_64KB;
85 unsigned char get_autoconf_size_ext(int size) {
86 if (size == 16 * SIZE_MEGA)
87 return AC_MEM_SIZE_EXT_16MB;
88 if (size == 32 * SIZE_MEGA)
89 return AC_MEM_SIZE_EXT_32MB;
90 if (size == 64 * SIZE_MEGA)
91 return AC_MEM_SIZE_EXT_64MB;
92 if (size == 128 * SIZE_MEGA)
93 return AC_MEM_SIZE_EXT_128MB;
94 if (size == 256 * SIZE_MEGA)
95 return AC_MEM_SIZE_EXT_256MB;
96 if (size == 512 * SIZE_MEGA)
97 return AC_MEM_SIZE_EXT_512MB;
98 if (size == 1024 * SIZE_MEGA)
99 return AC_MEM_SIZE_EXT_1024MB;
101 return AC_MEM_SIZE_EXT_64MB;
104 extern void adjust_ranges_amiga(struct emulator_config *cfg);
106 unsigned int autoconfig_read_memory_z3_8(struct emulator_config *cfg, unsigned int address) {
107 int index = ac_z3_index[ac_z3_current_pic];
108 unsigned char val = 0;
110 if ((address & 0xFF) >= AC_Z3_REG_RES50 && (address & 0xFF) <= AC_Z3_REG_RES7C) {
114 switch(address & 0xFF) {
115 case AC_Z3_REG_ER_TYPE:
117 if (cfg->map_type[index] == MAPTYPE_RAM)
118 val |= BOARDTYPE_FREEMEM;
119 if (cfg->map_size[index] > 8 * SIZE_MEGA)
120 val |= get_autoconf_size_ext(cfg->map_size[index]);
122 val |= get_autoconf_size(cfg->map_size[index]);
123 if (ac_z3_current_pic + 1 < ac_z3_pic_count)
124 val |= BOARDTYPE_LINKED;
125 // Pre-invert this value, since it's the only value not physically complemented
129 case AC_Z3_REG_ER_PRODUCT:
133 case AC_Z3_REG_ER_FLAGS:
134 if (cfg->map_type[index] == MAPTYPE_RAM)
135 val |= Z3_FLAGS_MEMORY;
136 if (cfg->map_size[index] > 8 * SIZE_MEGA)
137 val |= Z3_FLAGS_EXTENSION;
138 val |= Z3_FLAGS_RESERVED;
139 // Bottom four bits are zero, useless unles you want really odd RAM sizes.
141 // Manufacturer ID low/high bytes.
142 case AC_Z3_REG_MAN_LO:
143 val = PISTORM_MANUF_ID & 0x00FF;
145 case AC_Z3_REG_MAN_HI:
146 val = (PISTORM_MANUF_ID >> 8);
148 case AC_Z3_REG_SER_BYTE0:
149 case AC_Z3_REG_SER_BYTE1:
150 case AC_Z3_REG_SER_BYTE2:
151 case AC_Z3_REG_SER_BYTE3:
152 // Expansion board serial assigned by manufacturer.
155 case AC_Z3_REG_INIT_DIAG_VEC_LO:
156 case AC_Z3_REG_INIT_DIAG_VEC_HI:
157 // 16-bit offset to boot ROM in assigned memory range.
160 // Additional reserved/unused registers.
161 case AC_Z3_REG_ER_RES03:
162 case AC_Z3_REG_ER_RES0D:
163 case AC_Z3_REG_ER_RES0E:
164 case AC_Z3_REG_ER_RES0F:
165 case AC_Z3_REG_ER_Z2_INT:
173 //printf("Read byte %d from Z3 autoconf for PIC %d (%.2X).\n", address, ac_z3_current_pic, val);
174 return (address & 0x100) ? (val << 4) ^ 0xFF : (val & 0xF0) ^ 0xFF;
179 void autoconfig_write_memory_z3_8(struct emulator_config *cfg, unsigned int address, unsigned int value) {
180 int index = ac_z3_index[ac_z3_current_pic];
181 unsigned char val = (unsigned char)value;
184 switch(address & 0xFF) {
185 case AC_Z3_REG_WR_ADDR_LO:
187 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFF0F0000) | ((val & 0xF0) << 16);
191 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFF000000) | (val << 16);
193 case AC_Z3_REG_WR_ADDR_HI:
195 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x0FFF0000) | ((val & 0xF0) << 24);
198 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x00FF0000) | (val << 24);
201 case AC_Z3_REG_WR_ADDR_NIB_LO:
202 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFFF00000) | ((val & 0xF0) << 12);
205 case AC_Z3_REG_WR_ADDR_NIB_HI:
206 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xF0FF0000) | ((val & 0xF0) << 20);
209 case AC_Z3_REG_SHUTUP:
210 //printf("Write to Z3 shutup register for PIC %d.\n", ac_z3_current_pic);
219 printf("[AUTOCONF] Address of Z3 autoconf RAM assigned to $%.8x [B]\n", ac_base[ac_z3_current_pic]);
220 cfg->map_offset[index] = ac_base[ac_z3_current_pic];
221 cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
222 m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
224 if (ac_z3_current_pic == ac_z3_pic_count) {
226 adjust_ranges_amiga(cfg);
233 void autoconfig_write_memory_z3_16(struct emulator_config *cfg, unsigned int address, unsigned int value) {
234 int index = ac_z3_index[ac_z3_current_pic];
235 unsigned short val = (unsigned short)value;
238 switch(address & 0xFF) {
239 case AC_Z3_REG_WR_ADDR_HI:
240 // This is, as far as I know, the only register it should write a 16-bit value to.
241 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x00000000) | (val << 16);
245 printf("Unknown WORD write to Z3 autoconf address $%.2X", address & 0xFF);
246 //stop_cpu_emulation();
251 printf("[AUTOCONF] Address of Z3 autoconf RAM assigned to $%.8x [W]\n", ac_base[ac_z3_current_pic]);
252 cfg->map_offset[index] = ac_base[ac_z3_current_pic];
253 cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
254 m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
256 if (ac_z3_current_pic == ac_z3_pic_count) {
258 adjust_ranges_amiga(cfg);
265 void add_z2_pic(uint8_t type, uint8_t index) {
266 if (ac_z2_pic_count < AC_PIC_LIMIT) {
267 ac_z2_type[ac_z2_pic_count] = type;
268 ac_z2_index[ac_z2_pic_count] = index;
272 printf("[AUTOCONF] Failed to add Z2 PIC of type %d, limit exceeded.\n", type);
275 void remove_z2_pic(uint8_t type, uint8_t index) {
276 uint8_t pic_found = 0;
279 for (uint32_t i = 0; i < ac_z2_pic_count; i++) {
280 if (ac_z2_type[i] == type && !pic_found) {
283 if (pic_found && i < AC_PIC_LIMIT - 1) {
284 ac_z2_type[i] = ac_z2_type[i + 1];
285 ac_z2_index[i] = ac_z2_index[ i + 1];
290 ac_z2_type[AC_PIC_LIMIT - 1] = ACTYPE_NONE;
291 ac_z2_index[AC_PIC_LIMIT - 1] = 0;
295 printf("[AUTOCONF] Tried to remove Z2 PIC of type %d, but it wasn't found.\n", type);
299 unsigned int autoconfig_read_memory_8(struct emulator_config *cfg, unsigned int address) {
300 unsigned char *rom = NULL;
301 unsigned char val = 0;
303 switch(ac_z2_type[ac_z2_current_pic]) {
304 case ACTYPE_MAPFAST_Z2:
305 rom = ac_fast_ram_rom;
313 case ACTYPE_PISTORM_DEV:
314 rom = ac_pistorm_rom;
322 if ((address & 1) == 0 && (address / 2) < (int)sizeof(ac_fast_ram_rom)) {
323 if (ac_z2_type[ac_z2_current_pic] == ACTYPE_MAPFAST_Z2 && address / 2 == 1) {
324 val = get_autoconf_size(cfg->map_size[ac_z2_index[ac_z2_current_pic]]);
325 if (ac_z2_current_pic + 1 < ac_z2_pic_count)
326 val |= BOARDTYPE_LINKED;
329 val = rom[address / 2];
330 //printf("Read byte %d from Z2 autoconf for PIC %d (%.2X).\n", address/2, ac_z2_current_pic, val);
333 if (address != 0 && address != 2 && address != 0x40 && address != 0x42)
336 return (unsigned int)val;
339 void autoconfig_write_memory_8(struct emulator_config *cfg, unsigned int address, unsigned int value) {
341 int index = ac_z2_index[ac_z2_current_pic];
343 unsigned int *base = NULL;
345 switch(ac_z2_type[ac_z2_current_pic]) {
346 case ACTYPE_MAPFAST_Z2:
347 base = &ac_base[ac_z2_current_pic];
355 case ACTYPE_PISTORM_DEV:
356 base = &pistorm_dev_base;
363 //printf("Failed to set up the base for autoconfig PIC %d.\n", ac_z2_current_pic);
367 if (address == 0x4a) { // base[19:16]
368 *base = (value & 0xf0) << (16 - 4);
369 } else if (address == 0x48) { // base[23:20]
371 *base |= (value & 0xf0) << (20 - 4);
373 if (ac_z2_type[ac_z2_current_pic] == ACTYPE_MAPFAST_Z2) { // fast ram
374 //a314_set_mem_base_size(*base, cfg->map_size[ac_index[ac_z2_current_pic]]);
377 } else if (address == 0x4c) { // shut up
378 //printf("Write to Z2 shutup register for PIC %d.\n", ac_z2_current_pic);
384 switch (ac_z2_type[ac_z2_current_pic]) {
385 case ACTYPE_MAPFAST_Z2:
386 cfg->map_offset[index] = ac_base[ac_z2_current_pic];
387 cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
388 printf("[AUTOCONF] Address of Z2 autoconf RAM assigned to $%.8X\n", ac_base[ac_z2_current_pic]);
389 m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
390 printf("[AUTOCONF] Z2 PIC %d at $%.8lX-%.8lX, Size: %d MB\n", ac_z2_current_pic, cfg->map_offset[index], cfg->map_high[index], cfg->map_size[index] / SIZE_MEGA);
393 printf("[AUTOCONF] PiSCSI Z2 device assigned to $%.8X\n", piscsi_base);
394 //m68k_add_rom_range(piscsi_base + (16 * SIZE_KILO), piscsi_base + (32 * SIZE_KILO), piscsi_rom_ptr);
396 case ACTYPE_PISTORM_DEV:
397 printf("[AUTOCONF] PiStorm Interaction Z2 Device assigned to $%.8X\n", pistorm_dev_base);
403 if (ac_z2_current_pic == ac_z2_pic_count) {
405 adjust_ranges_amiga(cfg);