1 // SPDX-License-Identifier: MIT
7 #include "amiga-autoconf.h"
8 #include "amiga-registers.h"
9 #include "hunk-reloc.h"
10 #include "net/pi-net-enums.h"
11 #include "net/pi-net.h"
12 #include "piscsi/piscsi-enums.h"
13 #include "piscsi/piscsi.h"
14 #include "pistorm-dev/pistorm-dev-enums.h"
15 #include "pistorm-dev/pistorm-dev.h"
16 #include "platforms/platforms.h"
17 #include "platforms/shared/rtc.h"
20 //#define DEBUG_AMIGA_PLATFORM
22 #ifdef DEBUG_AMIGA_PLATFORM
29 int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val);
30 int handle_register_write_amiga(unsigned int addr, unsigned int value, unsigned char type);
33 extern int ac_z2_current_pic;
34 extern int ac_z2_done;
35 extern int ac_z2_pic_count;
36 extern int ac_z2_type[AC_PIC_LIMIT];
37 extern int ac_z2_index[AC_PIC_LIMIT];
39 extern int ac_z3_current_pic;
40 extern int ac_z3_pic_count;
41 extern int ac_z3_done;
42 extern int ac_z3_type[AC_PIC_LIMIT];
43 extern int ac_z3_index[AC_PIC_LIMIT];
44 extern int gayle_emulation_enabled;
46 char *z2_autoconf_id = "z2_autoconf_fast";
47 char *z2_autoconf_zap_id = "^2_autoconf_fast";
48 char *z3_autoconf_id = "z3_autoconf_fast";
49 char *z3_autoconf_zap_id = "^3_autoconf_fast";
51 extern const char *op_type_names[OP_TYPE_NUM];
52 extern uint8_t cdtv_mode;
53 extern uint8_t rtc_type;
54 extern unsigned char cdtv_sram[32 * SIZE_KILO];
56 #define min(a, b) (a < b) ? a : b
57 #define max(a, b) (a > b) ? a : b
59 uint8_t rtg_enabled = 0, piscsi_enabled = 0, pinet_enabled = 0, kick13_mode = 0, pistorm_dev_enabled = 1;
61 extern uint32_t piscsi_base, pistorm_dev_base;
63 extern void stop_cpu_emulation(uint8_t disasm_cur);
65 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
69 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
70 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
71 if (type == OP_TYPE_BYTE) {
72 *val = autoconfig_read_memory_8(cfg, addr - AC_Z2_BASE);
75 printf("Unexpected %s read from Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
78 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
79 uint32_t addr_ = addr - AC_Z2_BASE;
81 addr_ = (addr_ - 2) + 0x100;
83 if (type == OP_TYPE_BYTE) {
84 *val = autoconfig_read_memory_z3_8(cfg, addr_ - AC_Z2_BASE);
87 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
91 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
92 if (ac_z3_pic_count == 0) {
97 if (type == OP_TYPE_BYTE) {
98 *val = autoconfig_read_memory_z3_8(cfg, addr - AC_Z3_BASE);
101 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
105 if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
106 *val = handle_pistorm_dev_read(addr, type);
110 if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
111 //printf("[Amiga-Custom] %s read from PISCSI base @$%.8X.\n", op_type_names[type], addr);
112 //stop_cpu_emulation(1);
113 *val = handle_piscsi_read(addr, type);
120 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
124 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
125 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
126 if (type == OP_TYPE_BYTE) {
127 autoconfig_write_memory_8(cfg, addr - AC_Z2_BASE, val);
130 printf("Unexpected %s write to Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
133 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
134 uint32_t addr_ = addr - AC_Z2_BASE;
136 addr_ = (addr_ - 2) + 0x100;
138 if (type == OP_TYPE_BYTE) {
139 autoconfig_write_memory_z3_8(cfg, addr_ - AC_Z2_BASE, val);
142 else if (type == OP_TYPE_WORD) {
143 autoconfig_write_memory_z3_16(cfg, addr_ - AC_Z2_BASE, val);
146 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
151 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
152 if (type == OP_TYPE_BYTE) {
153 if (ac_z3_pic_count == 0) {
158 //printf("Write to autoconf area.\n");
159 autoconfig_write_memory_z3_8(cfg, addr - AC_Z3_BASE, val);
162 else if (type == OP_TYPE_WORD) {
163 autoconfig_write_memory_z3_16(cfg, addr - AC_Z3_BASE, val);
167 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
172 if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
173 handle_pistorm_dev_write(addr, val, type);
177 if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
178 //printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
179 handle_piscsi_write(addr, val, type);
186 void adjust_ranges_amiga(struct emulator_config *cfg) {
187 cfg->mapped_high = 0;
189 cfg->custom_high = 0;
192 // Set up the min/max ranges for mapped reads/writes
193 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
194 if (cfg->map_type[i] != MAPTYPE_NONE) {
195 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
196 cfg->mapped_low = cfg->map_offset[i];
197 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
198 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
202 if ((ac_z2_pic_count || ac_z3_pic_count) && (!ac_z2_done || !ac_z3_done)) {
203 if (cfg->custom_low == 0)
204 cfg->custom_low = AC_Z2_BASE;
206 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
207 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
209 /*if (ac_z3_pic_count && !ac_z3_done) {
210 if (cfg->custom_low == 0)
211 cfg->custom_low = AC_Z3_BASE;
213 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
214 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
217 if (cfg->custom_low == 0)
218 cfg->custom_low = PIGFX_RTG_BASE;
220 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
221 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
223 if (piscsi_enabled) {
224 if (cfg->custom_low == 0)
225 cfg->custom_low = PISCSI_OFFSET;
227 cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
228 cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
229 if (piscsi_base != 0) {
230 cfg->custom_low = min(cfg->custom_low, piscsi_base);
234 if (cfg->custom_low == 0)
235 cfg->custom_low = PINET_OFFSET;
237 cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
238 cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
241 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
242 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
245 int setup_platform_amiga(struct emulator_config *cfg) {
246 printf("Performing setup for Amiga platform.\n");
248 if (strlen(cfg->platform->subsys)) {
249 printf("Subsystem is [%s]\n", cfg->platform->subsys);
250 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
251 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
254 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
255 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
258 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
259 printf("Configuring platform for CDTV emulation.\n");
261 rtc_type = RTC_TYPE_MSM;
265 printf("No sub system specified.\n");
267 // Look for Z2 autoconf Fast RAM by id
268 int index = get_named_mapped_item(cfg, z2_autoconf_id);
271 // "Zap" config items as they are processed.
272 cfg->map_id[index][0] = '^';
274 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
275 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizing to 8MB.\n");
276 resize_data = 8 * SIZE_MEGA;
278 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
279 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
280 if (cfg->map_size[index] > 8 * SIZE_MEGA)
281 resize_data = 8 * SIZE_MEGA;
282 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
283 resize_data = 4 * SIZE_MEGA;
285 resize_data = 2 * SIZE_MEGA;
286 printf("%dMB.\n", resize_data / SIZE_MEGA);
289 free(cfg->map_data[index]);
290 cfg->map_size[index] = resize_data;
291 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
293 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
294 add_z2_pic(ACTYPE_MAPFAST_Z2, index);
295 //ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
296 //ac_z2_index[ac_z2_pic_count] = index;
300 printf("No Z2 Fast RAM configured.\n");
302 index = get_named_mapped_item(cfg, z2_autoconf_id);
306 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
307 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
308 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
309 cfg->map_id[i][0] = z2_autoconf_id[0];
313 index = get_named_mapped_item(cfg, z3_autoconf_id);
316 cfg->map_id[index][0] = '^';
317 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
318 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
319 ac_z3_index[ac_z3_pic_count] = index;
323 printf("No Z3 Fast RAM configured.\n");
324 index = get_named_mapped_item(cfg, z3_autoconf_id);
327 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
328 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
329 cfg->map_id[i][0] = z3_autoconf_id[0];
333 index = get_named_mapped_item(cfg, "cpu_slot_ram");
335 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
338 adjust_ranges_amiga(cfg);
341 FILE *in = fopen("data/cdtv.sram", "rb");
343 printf("Loaded CDTV SRAM.\n");
344 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
349 if (pistorm_dev_enabled) {
350 add_z2_pic(ACTYPE_PISTORM_DEV, 0);
356 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
360 if (strcmp(var, "enable_rtc_emulation") == 0) {
361 int8_t rtc_enabled = 0;
362 if (!val || strlen(val) == 0)
365 rtc_enabled = get_int(val);
367 if (rtc_enabled != -1) {
368 configure_rtc_emulation_amiga(rtc_enabled);
371 if (strcmp(var, "hdd0") == 0) {
372 if (val && strlen(val) != 0)
373 set_hard_drive_image_file_amiga(0, val);
375 if (strcmp(var, "hdd1") == 0) {
376 if (val && strlen(val) != 0)
377 set_hard_drive_image_file_amiga(1, val);
379 if (strcmp(var, "cdtv") == 0) {
380 printf("[AMIGA] CDTV mode enabled.\n");
383 if (strcmp(var, "rtg") == 0 && !rtg_enabled) {
384 if (init_rtg_data()) {
385 printf("[AMIGA] RTG Enabled.\n");
387 adjust_ranges_amiga(cfg);
390 printf("[AMIGA} Failed to enable RTG.\n");
392 if (strcmp(var, "kick13") == 0) {
393 printf("[AMIGA] Kickstart 1.3 mode enabled, Z3 PICs will not be enumerated.\n");
398 if (strcmp(var, "piscsi") == 0 && !piscsi_enabled) {
399 printf("[AMIGA] PISCSI Interface Enabled.\n");
402 add_z2_pic(ACTYPE_PISCSI, 0);
403 //ac_z2_type[ac_z2_pic_count] = ACTYPE_PISCSI;
405 adjust_ranges_amiga(cfg);
407 if (piscsi_enabled) {
408 if (strcmp(var, "piscsi0") == 0) {
409 piscsi_map_drive(val, 0);
411 if (strcmp(var, "piscsi1") == 0) {
412 piscsi_map_drive(val, 1);
414 if (strcmp(var, "piscsi2") == 0) {
415 piscsi_map_drive(val, 2);
417 if (strcmp(var, "piscsi3") == 0) {
418 piscsi_map_drive(val, 3);
420 if (strcmp(var, "piscsi4") == 0) {
421 piscsi_map_drive(val, 4);
423 if (strcmp(var, "piscsi5") == 0) {
424 piscsi_map_drive(val, 5);
426 if (strcmp(var, "piscsi6") == 0) {
427 piscsi_map_drive(val, 6);
432 if (strcmp(var, "pi-net") == 0 && !pinet_enabled) {
433 printf("[AMIGA] PI-NET Interface Enabled.\n");
436 adjust_ranges_amiga(cfg);
439 if (strcmp(var, "no-pistorm-dev") == 0) {
440 pistorm_dev_enabled = 0;
441 printf("[AMIGA] Disabling PiStorm interaction device.\n");
445 if (strcmp(var, "rtc_type") == 0) {
446 if (val && strlen(val) != 0) {
447 if (strcmp(val, "msm") == 0) {
448 printf("[AMIGA] RTC type set to MSM.\n");
449 rtc_type = RTC_TYPE_MSM;
452 printf("[AMIGA] RTC type set to Ricoh.\n");
453 rtc_type = RTC_TYPE_RICOH;
459 void handle_reset_amiga(struct emulator_config *cfg) {
460 ac_z2_done = (ac_z2_pic_count == 0);
461 ac_z3_done = (ac_z3_pic_count == 0);
462 ac_z2_current_pic = 0;
463 ac_z3_current_pic = 0;
465 DEBUG("[AMIGA] Reset handler.\n");
466 DEBUG("[AMIGA] AC done - Z2: %d Z3: %d.\n", ac_z2_done, ac_z3_done);
469 piscsi_refresh_drives();
471 adjust_ranges_amiga(cfg);
474 void shutdown_platform_amiga(struct emulator_config *cfg) {
477 FILE *out = fopen("data/cdtv.sram", "wb+");
479 printf("Saving CDTV SRAM.\n");
480 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
484 printf("Failed to write CDTV SRAM to disk.\n");
489 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
490 cfg->register_read = handle_register_read_amiga;
491 cfg->register_write = handle_register_write_amiga;
492 cfg->custom_read = custom_read_amiga;
493 cfg->custom_write = custom_write_amiga;
494 cfg->platform_initial_setup = setup_platform_amiga;
495 cfg->handle_reset = handle_reset_amiga;
496 cfg->shutdown = shutdown_platform_amiga;
498 cfg->setvar = setvar_amiga;
499 cfg->id = PLATFORM_AMIGA;
502 cfg->subsys = malloc(strlen(subsys) + 1);
503 strcpy(cfg->subsys, subsys);
504 for (unsigned int i = 0; i < strlen(cfg->subsys); i++) {
505 cfg->subsys[i] = tolower(cfg->subsys[i]);