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"
19 #include "a314/a314.h"
21 //#define DEBUG_AMIGA_PLATFORM
23 #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);
32 extern int ac_z2_current_pic;
33 extern int ac_z2_done;
34 extern int ac_z2_pic_count;
35 extern int ac_z2_type[AC_PIC_LIMIT];
36 extern int ac_z2_index[AC_PIC_LIMIT];
38 extern int ac_z3_current_pic;
39 extern int ac_z3_pic_count;
40 extern int ac_z3_done;
41 extern int ac_z3_type[AC_PIC_LIMIT];
42 extern int ac_z3_index[AC_PIC_LIMIT];
43 extern uint8_t gayle_emulation_enabled;
45 char *z2_autoconf_id = "z2_autoconf_fast";
46 char *z2_autoconf_zap_id = "^2_autoconf_fast";
47 char *z3_autoconf_id = "z3_autoconf_fast";
48 char *z3_autoconf_zap_id = "^3_autoconf_fast";
50 extern const char *op_type_names[OP_TYPE_NUM];
51 extern uint8_t cdtv_mode;
52 extern uint8_t rtc_type;
53 extern unsigned char cdtv_sram[32 * SIZE_KILO];
54 extern unsigned int a314_base;
56 extern int kb_hook_enabled;
57 extern int mouse_hook_enabled;
59 #define min(a, b) (a < b) ? a : b
60 #define max(a, b) (a > b) ? a : b
62 uint8_t rtg_enabled = 0, piscsi_enabled = 0, pinet_enabled = 0, kick13_mode = 0, pistorm_dev_enabled = 1;
63 uint8_t a314_emulation_enabled = 0, a314_initialized = 0;
65 extern uint32_t piscsi_base, pistorm_dev_base;
67 extern void stop_cpu_emulation(uint8_t disasm_cur);
69 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
73 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
74 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
75 if (type == OP_TYPE_BYTE) {
76 *val = autoconfig_read_memory_8(cfg, addr - AC_Z2_BASE);
79 printf("Unexpected %s read from Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
82 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
83 uint32_t addr_ = addr - AC_Z2_BASE;
85 addr_ = (addr_ - 2) + 0x100;
87 if (type == OP_TYPE_BYTE) {
88 *val = autoconfig_read_memory_z3_8(cfg, addr_ - AC_Z2_BASE);
91 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
95 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
96 if (ac_z3_pic_count == 0) {
101 if (type == OP_TYPE_BYTE) {
102 *val = autoconfig_read_memory_z3_8(cfg, addr - AC_Z3_BASE);
105 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
109 if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
110 *val = handle_pistorm_dev_read(addr, type);
114 if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
115 //printf("[Amiga-Custom] %s read from PISCSI base @$%.8X.\n", op_type_names[type], addr);
116 //stop_cpu_emulation(1);
117 *val = handle_piscsi_read(addr, type);
121 if (a314_emulation_enabled && addr >= a314_base && addr < a314_base + (64 * SIZE_KILO)) {
122 //printf("%s read from A314 @$%.8X\n", op_type_names[type], addr);
125 *val = a314_read_memory_8(addr - a314_base);
129 *val = a314_read_memory_16(addr - a314_base);
132 case OP_TYPE_LONGWORD:
133 *val = a314_read_memory_32(addr - a314_base);
145 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
149 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
150 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
151 if (type == OP_TYPE_BYTE) {
152 autoconfig_write_memory_8(cfg, addr - AC_Z2_BASE, val);
155 printf("Unexpected %s write to Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
158 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
159 uint32_t addr_ = addr - AC_Z2_BASE;
161 addr_ = (addr_ - 2) + 0x100;
163 if (type == OP_TYPE_BYTE) {
164 autoconfig_write_memory_z3_8(cfg, addr_ - AC_Z2_BASE, val);
167 else if (type == OP_TYPE_WORD) {
168 autoconfig_write_memory_z3_16(cfg, addr_ - AC_Z2_BASE, val);
171 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
176 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
177 if (type == OP_TYPE_BYTE) {
178 if (ac_z3_pic_count == 0) {
183 //printf("Write to autoconf area.\n");
184 autoconfig_write_memory_z3_8(cfg, addr - AC_Z3_BASE, val);
187 else if (type == OP_TYPE_WORD) {
188 autoconfig_write_memory_z3_16(cfg, addr - AC_Z3_BASE, val);
192 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
197 if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
198 handle_pistorm_dev_write(addr, val, type);
202 if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
203 //printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
204 handle_piscsi_write(addr, val, type);
208 if (a314_emulation_enabled && addr >= a314_base && addr < a314_base + (64 * SIZE_KILO)) {
209 //printf("%s write to A314 @$%.8X: %d\n", op_type_names[type], addr, val);
212 a314_write_memory_8(addr - a314_base, val);
216 // Not implemented in a314.cc
217 //a314_write_memory_16(addr, val);
220 case OP_TYPE_LONGWORD:
221 // Not implemented in a314.cc
222 // a314_write_memory_32(addr, val);
233 void adjust_ranges_amiga(struct emulator_config *cfg) {
234 cfg->mapped_high = 0;
236 cfg->custom_high = 0;
239 // Set up the min/max ranges for mapped reads/writes
240 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
241 if (cfg->map_type[i] != MAPTYPE_NONE) {
242 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
243 cfg->mapped_low = cfg->map_offset[i];
244 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
245 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
249 if ((ac_z2_pic_count || ac_z3_pic_count) && (!ac_z2_done || !ac_z3_done)) {
250 if (cfg->custom_low == 0)
251 cfg->custom_low = AC_Z2_BASE;
253 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
254 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
256 /*if (ac_z3_pic_count && !ac_z3_done) {
257 if (cfg->custom_low == 0)
258 cfg->custom_low = AC_Z3_BASE;
260 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
261 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
264 if (cfg->custom_low == 0)
265 cfg->custom_low = PIGFX_RTG_BASE;
267 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
268 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
270 if (piscsi_enabled) {
271 if (cfg->custom_low == 0)
272 cfg->custom_low = PISCSI_OFFSET;
274 cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
275 cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
276 if (piscsi_base != 0) {
277 cfg->custom_low = min(cfg->custom_low, piscsi_base);
281 if (cfg->custom_low == 0)
282 cfg->custom_low = PINET_OFFSET;
284 cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
285 cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
288 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
289 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
292 int setup_platform_amiga(struct emulator_config *cfg) {
293 printf("Performing setup for Amiga platform.\n");
295 if (strlen(cfg->platform->subsys)) {
296 printf("Subsystem is [%s]\n", cfg->platform->subsys);
297 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
298 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
301 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
302 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
305 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
306 printf("Configuring platform for CDTV emulation.\n");
308 rtc_type = RTC_TYPE_MSM;
312 printf("No sub system specified.\n");
314 // Look for Z2 autoconf Fast RAM by id
315 int index = get_named_mapped_item(cfg, z2_autoconf_id);
318 // "Zap" config items as they are processed.
319 cfg->map_id[index][0] = '^';
321 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
322 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizing to 8MB.\n");
323 resize_data = 8 * SIZE_MEGA;
325 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
326 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
327 if (cfg->map_size[index] > 8 * SIZE_MEGA)
328 resize_data = 8 * SIZE_MEGA;
329 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
330 resize_data = 4 * SIZE_MEGA;
332 resize_data = 2 * SIZE_MEGA;
333 printf("%dMB.\n", resize_data / SIZE_MEGA);
336 free(cfg->map_data[index]);
337 cfg->map_size[index] = resize_data;
338 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
340 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
341 add_z2_pic(ACTYPE_MAPFAST_Z2, index);
342 //ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
343 //ac_z2_index[ac_z2_pic_count] = index;
347 printf("No Z2 Fast RAM configured.\n");
349 index = get_named_mapped_item(cfg, z2_autoconf_id);
353 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
354 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
355 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
356 cfg->map_id[i][0] = z2_autoconf_id[0];
360 index = get_named_mapped_item(cfg, z3_autoconf_id);
363 cfg->map_id[index][0] = '^';
364 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
365 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
366 ac_z3_index[ac_z3_pic_count] = index;
370 printf("No Z3 Fast RAM configured.\n");
371 index = get_named_mapped_item(cfg, z3_autoconf_id);
374 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
375 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
376 cfg->map_id[i][0] = z3_autoconf_id[0];
380 index = get_named_mapped_item(cfg, "cpu_slot_ram");
382 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
385 adjust_ranges_amiga(cfg);
388 FILE *in = fopen("data/cdtv.sram", "rb");
390 printf("Loaded CDTV SRAM.\n");
391 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
396 if (pistorm_dev_enabled) {
397 add_z2_pic(ACTYPE_PISTORM_DEV, 0);
403 #define CHKVAR(a) (strcmp(var, a) == 0)
405 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
409 if CHKVAR("enable_rtc_emulation") {
410 int8_t rtc_enabled = 0;
411 if (!val || strlen(val) == 0)
414 rtc_enabled = get_int(val);
416 if (rtc_enabled != -1) {
417 configure_rtc_emulation_amiga(rtc_enabled);
421 if (val && strlen(val) != 0)
422 set_hard_drive_image_file_amiga(0, val);
425 if (val && strlen(val) != 0)
426 set_hard_drive_image_file_amiga(1, val);
429 printf("[AMIGA] CDTV mode enabled.\n");
432 if (CHKVAR("rtg") && !rtg_enabled) {
433 if (init_rtg_data(cfg)) {
434 printf("[AMIGA] RTG Enabled.\n");
436 adjust_ranges_amiga(cfg);
439 printf("[AMIGA} Failed to enable RTG.\n");
441 if CHKVAR("kick13") {
442 printf("[AMIGA] Kickstart 1.3 mode enabled, Z3 PICs will not be enumerated.\n");
446 if (!a314_initialized) {
447 int32_t res = a314_init();
449 printf("[AMIGA] Failed to enable A314 emulation, error return code: %d.\n", res);
451 printf("[AMIGA] A314 emulation enabled.\n");
452 add_z2_pic(ACTYPE_A314, 0);
453 a314_emulation_enabled = 1;
454 a314_initialized = 1;
457 add_z2_pic(ACTYPE_A314, 0);
458 a314_emulation_enabled = 1;
461 if CHKVAR("a314_conf") {
462 if (val && strlen(val) != 0) {
463 a314_set_config_file(val);
468 if (CHKVAR("piscsi") && !piscsi_enabled) {
469 printf("[AMIGA] PISCSI Interface Enabled.\n");
472 add_z2_pic(ACTYPE_PISCSI, 0);
473 adjust_ranges_amiga(cfg);
475 if (piscsi_enabled) {
476 if CHKVAR("piscsi0") {
477 piscsi_map_drive(val, 0);
479 if CHKVAR("piscsi1") {
480 piscsi_map_drive(val, 1);
482 if CHKVAR("piscsi2") {
483 piscsi_map_drive(val, 2);
485 if CHKVAR("piscsi3") {
486 piscsi_map_drive(val, 3);
488 if CHKVAR("piscsi4") {
489 piscsi_map_drive(val, 4);
491 if CHKVAR("piscsi5") {
492 piscsi_map_drive(val, 5);
494 if CHKVAR("piscsi6") {
495 piscsi_map_drive(val, 6);
500 if (CHKVAR("pi-net")&& !pinet_enabled) {
501 printf("[AMIGA] PI-NET Interface Enabled.\n");
504 adjust_ranges_amiga(cfg);
507 if CHKVAR("no-pistorm-dev") {
508 pistorm_dev_enabled = 0;
509 printf("[AMIGA] Disabling PiStorm interaction device.\n");
513 if CHKVAR("rtc_type") {
514 if (val && strlen(val) != 0) {
515 if (strcmp(val, "msm") == 0) {
516 printf("[AMIGA] RTC type set to MSM.\n");
517 rtc_type = RTC_TYPE_MSM;
520 printf("[AMIGA] RTC type set to Ricoh.\n");
521 rtc_type = RTC_TYPE_RICOH;
527 void handle_reset_amiga(struct emulator_config *cfg) {
528 ac_z2_done = (ac_z2_pic_count == 0);
529 ac_z3_done = (ac_z3_pic_count == 0);
530 ac_z2_current_pic = 0;
531 ac_z3_current_pic = 0;
533 DEBUG("[AMIGA] Reset handler.\n");
534 DEBUG("[AMIGA] AC done - Z2: %d Z3: %d.\n", ac_z2_done, ac_z3_done);
537 piscsi_refresh_drives();
539 adjust_ranges_amiga(cfg);
542 void shutdown_platform_amiga(struct emulator_config *cfg) {
543 printf("[AMIGA] Performing Amiga platform shutdown.\n");
546 FILE *out = fopen("data/cdtv.sram", "wb+");
548 printf("Saving CDTV SRAM.\n");
549 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
553 printf("Failed to write CDTV SRAM to disk.\n");
556 if (cfg->platform->subsys) {
557 free(cfg->platform->subsys);
558 cfg->platform->subsys = NULL;
560 if (piscsi_enabled) {
571 if (a314_emulation_enabled) {
572 a314_emulation_enabled = 0;
575 mouse_hook_enabled = 0;
581 autoconfig_reset_all();
582 printf("[AMIGA] Platform shutdown completed.\n");
585 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
586 cfg->register_read = handle_register_read_amiga;
587 cfg->register_write = handle_register_write_amiga;
588 cfg->custom_read = custom_read_amiga;
589 cfg->custom_write = custom_write_amiga;
590 cfg->platform_initial_setup = setup_platform_amiga;
591 cfg->handle_reset = handle_reset_amiga;
592 cfg->shutdown = shutdown_platform_amiga;
594 cfg->setvar = setvar_amiga;
595 cfg->id = PLATFORM_AMIGA;
598 cfg->subsys = malloc(strlen(subsys) + 1);
599 strcpy(cfg->subsys, subsys);
600 for (unsigned int i = 0; i < strlen(cfg->subsys); i++) {
601 cfg->subsys[i] = tolower(cfg->subsys[i]);