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/0B, 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 void autoconfig_reset_all() {
107 printf("[AUTOCONF] Resetting all autoconf data.\n");
108 for (int i = 0; i < AC_PIC_LIMIT; i++) {
109 ac_z2_type[i] = ACTYPE_NONE;
110 ac_z3_type[i] = ACTYPE_NONE;
116 ac_z2_current_pic = 0;
117 ac_z3_current_pic = 0;
120 unsigned int autoconfig_read_memory_z3_8(struct emulator_config *cfg, unsigned int address) {
121 int index = ac_z3_index[ac_z3_current_pic];
122 unsigned char val = 0;
124 if ((address & 0xFF) >= AC_Z3_REG_RES50 && (address & 0xFF) <= AC_Z3_REG_RES7C) {
128 switch(address & 0xFF) {
129 case AC_Z3_REG_ER_TYPE:
131 if (cfg->map_type[index] == MAPTYPE_RAM)
132 val |= BOARDTYPE_FREEMEM;
133 if (cfg->map_size[index] > 8 * SIZE_MEGA)
134 val |= get_autoconf_size_ext(cfg->map_size[index]);
136 val |= get_autoconf_size(cfg->map_size[index]);
137 if (ac_z3_current_pic + 1 < ac_z3_pic_count)
138 val |= BOARDTYPE_LINKED;
139 // Pre-invert this value, since it's the only value not physically complemented
143 case AC_Z3_REG_ER_PRODUCT:
147 case AC_Z3_REG_ER_FLAGS:
148 if (cfg->map_type[index] == MAPTYPE_RAM)
149 val |= Z3_FLAGS_MEMORY;
150 if (cfg->map_size[index] > 8 * SIZE_MEGA)
151 val |= Z3_FLAGS_EXTENSION;
152 val |= Z3_FLAGS_RESERVED;
153 // Bottom four bits are zero, useless unles you want really odd RAM sizes.
155 // Manufacturer ID low/high bytes.
156 case AC_Z3_REG_MAN_LO:
157 val = PISTORM_MANUF_ID & 0x00FF;
159 case AC_Z3_REG_MAN_HI:
160 val = (PISTORM_MANUF_ID >> 8);
162 case AC_Z3_REG_SER_BYTE0:
163 case AC_Z3_REG_SER_BYTE1:
164 case AC_Z3_REG_SER_BYTE2:
165 case AC_Z3_REG_SER_BYTE3:
166 // Expansion board serial assigned by manufacturer.
169 case AC_Z3_REG_INIT_DIAG_VEC_LO:
170 case AC_Z3_REG_INIT_DIAG_VEC_HI:
171 // 16-bit offset to boot ROM in assigned memory range.
174 // Additional reserved/unused registers.
175 case AC_Z3_REG_ER_RES03:
176 case AC_Z3_REG_ER_RES0D:
177 case AC_Z3_REG_ER_RES0E:
178 case AC_Z3_REG_ER_RES0F:
179 case AC_Z3_REG_ER_Z2_INT:
187 //printf("Read byte %d from Z3 autoconf for PIC %d (%.2X).\n", address, ac_z3_current_pic, val);
188 return (address & 0x100) ? (val << 4) ^ 0xFF : (val & 0xF0) ^ 0xFF;
193 void autoconfig_write_memory_z3_8(struct emulator_config *cfg, unsigned int address, unsigned int value) {
194 int index = ac_z3_index[ac_z3_current_pic];
195 unsigned char val = (unsigned char)value;
198 switch(address & 0xFF) {
199 case AC_Z3_REG_WR_ADDR_LO:
201 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFF0F0000) | ((val & 0xF0) << 16);
205 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFF000000) | (val << 16);
207 case AC_Z3_REG_WR_ADDR_HI:
209 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x0FFF0000) | ((val & 0xF0) << 24);
212 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x00FF0000) | (val << 24);
215 case AC_Z3_REG_WR_ADDR_NIB_LO:
216 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFFF00000) | ((val & 0xF0) << 12);
219 case AC_Z3_REG_WR_ADDR_NIB_HI:
220 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xF0FF0000) | ((val & 0xF0) << 20);
223 case AC_Z3_REG_SHUTUP:
224 //printf("Write to Z3 shutup register for PIC %d.\n", ac_z3_current_pic);
233 printf("[AUTOCONF] Address of Z3 autoconf RAM assigned to $%.8x [B]\n", ac_base[ac_z3_current_pic]);
234 cfg->map_offset[index] = ac_base[ac_z3_current_pic];
235 cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
236 m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
238 if (ac_z3_current_pic == ac_z3_pic_count) {
240 adjust_ranges_amiga(cfg);
247 void autoconfig_write_memory_z3_16(struct emulator_config *cfg, unsigned int address, unsigned int value) {
248 int index = ac_z3_index[ac_z3_current_pic];
249 unsigned short val = (unsigned short)value;
252 switch(address & 0xFF) {
253 case AC_Z3_REG_WR_ADDR_HI:
254 // This is, as far as I know, the only register it should write a 16-bit value to.
255 ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x00000000) | (val << 16);
259 printf("Unknown WORD write to Z3 autoconf address $%.2X", address & 0xFF);
260 //stop_cpu_emulation();
265 printf("[AUTOCONF] Address of Z3 autoconf RAM assigned to $%.8x [W]\n", ac_base[ac_z3_current_pic]);
266 cfg->map_offset[index] = ac_base[ac_z3_current_pic];
267 cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
268 m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
270 if (ac_z3_current_pic == ac_z3_pic_count) {
272 adjust_ranges_amiga(cfg);
279 void add_z2_pic(uint8_t type, uint8_t index) {
280 if (ac_z2_pic_count < AC_PIC_LIMIT) {
281 ac_z2_type[ac_z2_pic_count] = type;
282 ac_z2_index[ac_z2_pic_count] = index;
286 printf("[AUTOCONF] Failed to add Z2 PIC of type %d, limit exceeded.\n", type);
289 void remove_z2_pic(uint8_t type, uint8_t index) {
290 uint8_t pic_found = 0;
293 for (uint32_t i = 0; i < ac_z2_pic_count; i++) {
294 if (ac_z2_type[i] == type && !pic_found) {
297 if (pic_found && i < AC_PIC_LIMIT - 1) {
298 ac_z2_type[i] = ac_z2_type[i + 1];
299 ac_z2_index[i] = ac_z2_index[ i + 1];
304 ac_z2_type[AC_PIC_LIMIT - 1] = ACTYPE_NONE;
305 ac_z2_index[AC_PIC_LIMIT - 1] = 0;
309 printf("[AUTOCONF] Tried to remove Z2 PIC of type %d, but it wasn't found.\n", type);
313 unsigned int autoconfig_read_memory_8(struct emulator_config *cfg, unsigned int address) {
314 unsigned char *rom = NULL;
315 unsigned char val = 0;
317 switch(ac_z2_type[ac_z2_current_pic]) {
318 case ACTYPE_MAPFAST_Z2:
319 rom = ac_fast_ram_rom;
327 case ACTYPE_PISTORM_DEV:
328 rom = ac_pistorm_rom;
336 if ((address & 1) == 0 && (address / 2) < (int)sizeof(ac_fast_ram_rom)) {
337 if (ac_z2_type[ac_z2_current_pic] == ACTYPE_MAPFAST_Z2 && address / 2 == 1) {
338 val = get_autoconf_size(cfg->map_size[ac_z2_index[ac_z2_current_pic]]);
339 if (ac_z2_current_pic + 1 < ac_z2_pic_count)
340 val |= BOARDTYPE_LINKED;
343 val = rom[address / 2];
344 //printf("Read byte %d from Z2 autoconf for PIC %d (%.2X).\n", address/2, ac_z2_current_pic, val);
347 if (address != 0 && address != 2 && address != 0x40 && address != 0x42)
350 return (unsigned int)val;
353 void autoconfig_write_memory_8(struct emulator_config *cfg, unsigned int address, unsigned int value) {
355 int index = ac_z2_index[ac_z2_current_pic];
357 unsigned int *base = NULL;
359 switch(ac_z2_type[ac_z2_current_pic]) {
360 case ACTYPE_MAPFAST_Z2:
361 base = &ac_base[ac_z2_current_pic];
369 case ACTYPE_PISTORM_DEV:
370 base = &pistorm_dev_base;
377 //printf("Failed to set up the base for autoconfig PIC %d.\n", ac_z2_current_pic);
381 if (address == 0x4a) { // base[19:16]
382 *base = (value & 0xf0) << (16 - 4);
383 } else if (address == 0x48) { // base[23:20]
385 *base |= (value & 0xf0) << (20 - 4);
387 if (ac_z2_type[ac_z2_current_pic] == ACTYPE_MAPFAST_Z2) { // fast ram
388 //a314_set_mem_base_size(*base, cfg->map_size[ac_index[ac_z2_current_pic]]);
391 } else if (address == 0x4c) { // shut up
392 //printf("Write to Z2 shutup register for PIC %d.\n", ac_z2_current_pic);
398 switch (ac_z2_type[ac_z2_current_pic]) {
399 case ACTYPE_MAPFAST_Z2:
400 cfg->map_offset[index] = ac_base[ac_z2_current_pic];
401 cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
402 printf("[AUTOCONF] Address of Z2 autoconf RAM assigned to $%.8X\n", ac_base[ac_z2_current_pic]);
403 m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
404 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);
407 printf("[AUTOCONF] PiSCSI Z2 device assigned to $%.8X\n", piscsi_base);
408 //m68k_add_rom_range(piscsi_base + (16 * SIZE_KILO), piscsi_base + (32 * SIZE_KILO), piscsi_rom_ptr);
410 case ACTYPE_PISTORM_DEV:
411 printf("[AUTOCONF] PiStorm Interaction Z2 Device assigned to $%.8X\n", pistorm_dev_base);
417 if (ac_z2_current_pic == ac_z2_pic_count) {
419 adjust_ranges_amiga(cfg);