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 #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;
60 uint8_t a314_emulation_enabled = 0;
62 extern uint32_t piscsi_base, pistorm_dev_base;
64 extern void stop_cpu_emulation(uint8_t disasm_cur);
66 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
70 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
71 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
72 if (type == OP_TYPE_BYTE) {
73 *val = autoconfig_read_memory_8(cfg, addr - AC_Z2_BASE);
76 printf("Unexpected %s read from Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
79 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
80 uint32_t addr_ = addr - AC_Z2_BASE;
82 addr_ = (addr_ - 2) + 0x100;
84 if (type == OP_TYPE_BYTE) {
85 *val = autoconfig_read_memory_z3_8(cfg, addr_ - AC_Z2_BASE);
88 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
92 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
93 if (ac_z3_pic_count == 0) {
98 if (type == OP_TYPE_BYTE) {
99 *val = autoconfig_read_memory_z3_8(cfg, addr - AC_Z3_BASE);
102 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
106 if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
107 *val = handle_pistorm_dev_read(addr, type);
111 if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
112 //printf("[Amiga-Custom] %s read from PISCSI base @$%.8X.\n", op_type_names[type], addr);
113 //stop_cpu_emulation(1);
114 *val = handle_piscsi_read(addr, type);
118 if (a314_emulation_enabled && addr >= a314_base && addr < a314_base + (64 * SIZE_KILO)) {
119 //printf("%s read from A314 @$%.8X\n", op_type_names[type], addr);
122 *val = a314_read_memory_8(addr - a314_base);
126 *val = a314_read_memory_16(addr - a314_base);
129 case OP_TYPE_LONGWORD:
130 *val = a314_read_memory_32(addr - a314_base);
142 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
146 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
147 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
148 if (type == OP_TYPE_BYTE) {
149 autoconfig_write_memory_8(cfg, addr - AC_Z2_BASE, val);
152 printf("Unexpected %s write to Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
155 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
156 uint32_t addr_ = addr - AC_Z2_BASE;
158 addr_ = (addr_ - 2) + 0x100;
160 if (type == OP_TYPE_BYTE) {
161 autoconfig_write_memory_z3_8(cfg, addr_ - AC_Z2_BASE, val);
164 else if (type == OP_TYPE_WORD) {
165 autoconfig_write_memory_z3_16(cfg, addr_ - AC_Z2_BASE, val);
168 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
173 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
174 if (type == OP_TYPE_BYTE) {
175 if (ac_z3_pic_count == 0) {
180 //printf("Write to autoconf area.\n");
181 autoconfig_write_memory_z3_8(cfg, addr - AC_Z3_BASE, val);
184 else if (type == OP_TYPE_WORD) {
185 autoconfig_write_memory_z3_16(cfg, addr - AC_Z3_BASE, val);
189 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
194 if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
195 handle_pistorm_dev_write(addr, val, type);
199 if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
200 //printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
201 handle_piscsi_write(addr, val, type);
205 if (a314_emulation_enabled && addr >= a314_base && addr < a314_base + (64 * SIZE_KILO)) {
206 //printf("%s write to A314 @$%.8X: %d\n", op_type_names[type], addr, val);
209 a314_write_memory_8(addr - a314_base, val);
213 // Not implemented in a314.cc
214 //a314_write_memory_16(addr, val);
217 case OP_TYPE_LONGWORD:
218 // Not implemented in a314.cc
219 // a314_write_memory_32(addr, val);
230 void adjust_ranges_amiga(struct emulator_config *cfg) {
231 cfg->mapped_high = 0;
233 cfg->custom_high = 0;
236 // Set up the min/max ranges for mapped reads/writes
237 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
238 if (cfg->map_type[i] != MAPTYPE_NONE) {
239 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
240 cfg->mapped_low = cfg->map_offset[i];
241 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
242 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
246 if ((ac_z2_pic_count || ac_z3_pic_count) && (!ac_z2_done || !ac_z3_done)) {
247 if (cfg->custom_low == 0)
248 cfg->custom_low = AC_Z2_BASE;
250 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
251 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
253 /*if (ac_z3_pic_count && !ac_z3_done) {
254 if (cfg->custom_low == 0)
255 cfg->custom_low = AC_Z3_BASE;
257 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
258 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
261 if (cfg->custom_low == 0)
262 cfg->custom_low = PIGFX_RTG_BASE;
264 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
265 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
267 if (piscsi_enabled) {
268 if (cfg->custom_low == 0)
269 cfg->custom_low = PISCSI_OFFSET;
271 cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
272 cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
273 if (piscsi_base != 0) {
274 cfg->custom_low = min(cfg->custom_low, piscsi_base);
278 if (cfg->custom_low == 0)
279 cfg->custom_low = PINET_OFFSET;
281 cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
282 cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
285 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
286 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
289 int setup_platform_amiga(struct emulator_config *cfg) {
290 printf("Performing setup for Amiga platform.\n");
292 if (strlen(cfg->platform->subsys)) {
293 printf("Subsystem is [%s]\n", cfg->platform->subsys);
294 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
295 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
298 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
299 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
302 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
303 printf("Configuring platform for CDTV emulation.\n");
305 rtc_type = RTC_TYPE_MSM;
309 printf("No sub system specified.\n");
311 // Look for Z2 autoconf Fast RAM by id
312 int index = get_named_mapped_item(cfg, z2_autoconf_id);
315 // "Zap" config items as they are processed.
316 cfg->map_id[index][0] = '^';
318 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
319 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizing to 8MB.\n");
320 resize_data = 8 * SIZE_MEGA;
322 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
323 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
324 if (cfg->map_size[index] > 8 * SIZE_MEGA)
325 resize_data = 8 * SIZE_MEGA;
326 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
327 resize_data = 4 * SIZE_MEGA;
329 resize_data = 2 * SIZE_MEGA;
330 printf("%dMB.\n", resize_data / SIZE_MEGA);
333 free(cfg->map_data[index]);
334 cfg->map_size[index] = resize_data;
335 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
337 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
338 add_z2_pic(ACTYPE_MAPFAST_Z2, index);
339 //ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
340 //ac_z2_index[ac_z2_pic_count] = index;
344 printf("No Z2 Fast RAM configured.\n");
346 index = get_named_mapped_item(cfg, z2_autoconf_id);
350 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
351 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
352 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
353 cfg->map_id[i][0] = z2_autoconf_id[0];
357 index = get_named_mapped_item(cfg, z3_autoconf_id);
360 cfg->map_id[index][0] = '^';
361 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
362 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
363 ac_z3_index[ac_z3_pic_count] = index;
367 printf("No Z3 Fast RAM configured.\n");
368 index = get_named_mapped_item(cfg, z3_autoconf_id);
371 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
372 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
373 cfg->map_id[i][0] = z3_autoconf_id[0];
377 index = get_named_mapped_item(cfg, "cpu_slot_ram");
379 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
382 adjust_ranges_amiga(cfg);
385 FILE *in = fopen("data/cdtv.sram", "rb");
387 printf("Loaded CDTV SRAM.\n");
388 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
393 if (pistorm_dev_enabled) {
394 add_z2_pic(ACTYPE_PISTORM_DEV, 0);
400 #define CHKVAR(a) (strcmp(var, a) == 0)
402 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
406 if CHKVAR("enable_rtc_emulation") {
407 int8_t rtc_enabled = 0;
408 if (!val || strlen(val) == 0)
411 rtc_enabled = get_int(val);
413 if (rtc_enabled != -1) {
414 configure_rtc_emulation_amiga(rtc_enabled);
418 if (val && strlen(val) != 0)
419 set_hard_drive_image_file_amiga(0, val);
422 if (val && strlen(val) != 0)
423 set_hard_drive_image_file_amiga(1, val);
426 printf("[AMIGA] CDTV mode enabled.\n");
429 if (CHKVAR("rtg") && !rtg_enabled) {
430 if (init_rtg_data(cfg)) {
431 printf("[AMIGA] RTG Enabled.\n");
433 adjust_ranges_amiga(cfg);
436 printf("[AMIGA} Failed to enable RTG.\n");
438 if CHKVAR("kick13") {
439 printf("[AMIGA] Kickstart 1.3 mode enabled, Z3 PICs will not be enumerated.\n");
443 int32_t res = a314_init();
445 printf("[AMIGA] Failed to enable A314 emulation, error return code: %d.\n", res);
447 printf("[AMIGA] A314 emulation enabled.\n");
448 add_z2_pic(ACTYPE_A314, 0);
449 a314_emulation_enabled = 1;
452 if CHKVAR("a314_conf") {
453 if (val && strlen(val) != 0) {
454 a314_set_config_file(val);
459 if (CHKVAR("piscsi") && !piscsi_enabled) {
460 printf("[AMIGA] PISCSI Interface Enabled.\n");
463 add_z2_pic(ACTYPE_PISCSI, 0);
464 adjust_ranges_amiga(cfg);
466 if (piscsi_enabled) {
467 if CHKVAR("piscsi0") {
468 piscsi_map_drive(val, 0);
470 if CHKVAR("piscsi1") {
471 piscsi_map_drive(val, 1);
473 if CHKVAR("piscsi2") {
474 piscsi_map_drive(val, 2);
476 if CHKVAR("piscsi3") {
477 piscsi_map_drive(val, 3);
479 if CHKVAR("piscsi4") {
480 piscsi_map_drive(val, 4);
482 if CHKVAR("piscsi5") {
483 piscsi_map_drive(val, 5);
485 if CHKVAR("piscsi6") {
486 piscsi_map_drive(val, 6);
491 if (CHKVAR("pi-net")&& !pinet_enabled) {
492 printf("[AMIGA] PI-NET Interface Enabled.\n");
495 adjust_ranges_amiga(cfg);
498 if CHKVAR("no-pistorm-dev") {
499 pistorm_dev_enabled = 0;
500 printf("[AMIGA] Disabling PiStorm interaction device.\n");
504 if CHKVAR("rtc_type") {
505 if (val && strlen(val) != 0) {
506 if (strcmp(val, "msm") == 0) {
507 printf("[AMIGA] RTC type set to MSM.\n");
508 rtc_type = RTC_TYPE_MSM;
511 printf("[AMIGA] RTC type set to Ricoh.\n");
512 rtc_type = RTC_TYPE_RICOH;
518 void handle_reset_amiga(struct emulator_config *cfg) {
519 ac_z2_done = (ac_z2_pic_count == 0);
520 ac_z3_done = (ac_z3_pic_count == 0);
521 ac_z2_current_pic = 0;
522 ac_z3_current_pic = 0;
524 DEBUG("[AMIGA] Reset handler.\n");
525 DEBUG("[AMIGA] AC done - Z2: %d Z3: %d.\n", ac_z2_done, ac_z3_done);
528 piscsi_refresh_drives();
530 adjust_ranges_amiga(cfg);
533 void shutdown_platform_amiga(struct emulator_config *cfg) {
536 FILE *out = fopen("data/cdtv.sram", "wb+");
538 printf("Saving CDTV SRAM.\n");
539 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
543 printf("Failed to write CDTV SRAM to disk.\n");
546 if (cfg->platform->subsys) {
547 free(cfg->platform->subsys);
549 if (piscsi_enabled) {
560 if (a314_emulation_enabled) {
561 a314_emulation_enabled = 0;
566 autoconfig_reset_all();
567 printf("[AMIGA] Platform shutdown completed.\n");
570 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
571 cfg->register_read = handle_register_read_amiga;
572 cfg->register_write = handle_register_write_amiga;
573 cfg->custom_read = custom_read_amiga;
574 cfg->custom_write = custom_write_amiga;
575 cfg->platform_initial_setup = setup_platform_amiga;
576 cfg->handle_reset = handle_reset_amiga;
577 cfg->shutdown = shutdown_platform_amiga;
579 cfg->setvar = setvar_amiga;
580 cfg->id = PLATFORM_AMIGA;
583 cfg->subsys = malloc(strlen(subsys) + 1);
584 strcpy(cfg->subsys, subsys);
585 for (unsigned int i = 0; i < strlen(cfg->subsys); i++) {
586 cfg->subsys[i] = tolower(cfg->subsys[i]);