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 int 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)) {
121 *val = a314_read_memory_8(addr);
125 *val = a314_read_memory_16(addr);
128 case OP_TYPE_LONGWORD:
129 *val = a314_read_memory_32(addr);
141 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
145 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
146 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
147 if (type == OP_TYPE_BYTE) {
148 autoconfig_write_memory_8(cfg, addr - AC_Z2_BASE, val);
151 printf("Unexpected %s write to Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
154 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
155 uint32_t addr_ = addr - AC_Z2_BASE;
157 addr_ = (addr_ - 2) + 0x100;
159 if (type == OP_TYPE_BYTE) {
160 autoconfig_write_memory_z3_8(cfg, addr_ - AC_Z2_BASE, val);
163 else if (type == OP_TYPE_WORD) {
164 autoconfig_write_memory_z3_16(cfg, addr_ - AC_Z2_BASE, val);
167 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
172 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
173 if (type == OP_TYPE_BYTE) {
174 if (ac_z3_pic_count == 0) {
179 //printf("Write to autoconf area.\n");
180 autoconfig_write_memory_z3_8(cfg, addr - AC_Z3_BASE, val);
183 else if (type == OP_TYPE_WORD) {
184 autoconfig_write_memory_z3_16(cfg, addr - AC_Z3_BASE, val);
188 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
193 if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
194 handle_pistorm_dev_write(addr, val, type);
198 if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
199 //printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
200 handle_piscsi_write(addr, val, type);
204 if (a314_emulation_enabled && addr >= a314_base && addr < a314_base + (64 * SIZE_KILO)) {
207 a314_write_memory_8(addr, val);
211 // Not implemented in a314.cc
212 //a314_write_memory_16(addr, val);
215 case OP_TYPE_LONGWORD:
216 // Not implemented in a314.cc
217 // a314_write_memory_32(addr, val);
228 void adjust_ranges_amiga(struct emulator_config *cfg) {
229 cfg->mapped_high = 0;
231 cfg->custom_high = 0;
234 // Set up the min/max ranges for mapped reads/writes
235 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
236 if (cfg->map_type[i] != MAPTYPE_NONE) {
237 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
238 cfg->mapped_low = cfg->map_offset[i];
239 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
240 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
244 if ((ac_z2_pic_count || ac_z3_pic_count) && (!ac_z2_done || !ac_z3_done)) {
245 if (cfg->custom_low == 0)
246 cfg->custom_low = AC_Z2_BASE;
248 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
249 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
251 /*if (ac_z3_pic_count && !ac_z3_done) {
252 if (cfg->custom_low == 0)
253 cfg->custom_low = AC_Z3_BASE;
255 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
256 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
259 if (cfg->custom_low == 0)
260 cfg->custom_low = PIGFX_RTG_BASE;
262 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
263 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
265 if (piscsi_enabled) {
266 if (cfg->custom_low == 0)
267 cfg->custom_low = PISCSI_OFFSET;
269 cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
270 cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
271 if (piscsi_base != 0) {
272 cfg->custom_low = min(cfg->custom_low, piscsi_base);
276 if (cfg->custom_low == 0)
277 cfg->custom_low = PINET_OFFSET;
279 cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
280 cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
283 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
284 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
287 int setup_platform_amiga(struct emulator_config *cfg) {
288 printf("Performing setup for Amiga platform.\n");
290 if (strlen(cfg->platform->subsys)) {
291 printf("Subsystem is [%s]\n", cfg->platform->subsys);
292 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
293 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
296 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
297 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
300 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
301 printf("Configuring platform for CDTV emulation.\n");
303 rtc_type = RTC_TYPE_MSM;
307 printf("No sub system specified.\n");
309 // Look for Z2 autoconf Fast RAM by id
310 int index = get_named_mapped_item(cfg, z2_autoconf_id);
313 // "Zap" config items as they are processed.
314 cfg->map_id[index][0] = '^';
316 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
317 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizing to 8MB.\n");
318 resize_data = 8 * SIZE_MEGA;
320 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
321 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
322 if (cfg->map_size[index] > 8 * SIZE_MEGA)
323 resize_data = 8 * SIZE_MEGA;
324 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
325 resize_data = 4 * SIZE_MEGA;
327 resize_data = 2 * SIZE_MEGA;
328 printf("%dMB.\n", resize_data / SIZE_MEGA);
331 free(cfg->map_data[index]);
332 cfg->map_size[index] = resize_data;
333 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
335 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
336 add_z2_pic(ACTYPE_MAPFAST_Z2, index);
337 //ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
338 //ac_z2_index[ac_z2_pic_count] = index;
342 printf("No Z2 Fast RAM configured.\n");
344 index = get_named_mapped_item(cfg, z2_autoconf_id);
348 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
349 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
350 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
351 cfg->map_id[i][0] = z2_autoconf_id[0];
355 index = get_named_mapped_item(cfg, z3_autoconf_id);
358 cfg->map_id[index][0] = '^';
359 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
360 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
361 ac_z3_index[ac_z3_pic_count] = index;
365 printf("No Z3 Fast RAM configured.\n");
366 index = get_named_mapped_item(cfg, z3_autoconf_id);
369 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
370 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
371 cfg->map_id[i][0] = z3_autoconf_id[0];
375 index = get_named_mapped_item(cfg, "cpu_slot_ram");
377 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
380 adjust_ranges_amiga(cfg);
383 FILE *in = fopen("data/cdtv.sram", "rb");
385 printf("Loaded CDTV SRAM.\n");
386 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
391 if (pistorm_dev_enabled) {
392 add_z2_pic(ACTYPE_PISTORM_DEV, 0);
398 #define CHKVAR(a) (strcmp(var, a) == 0)
400 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
404 if CHKVAR("enable_rtc_emulation") {
405 int8_t rtc_enabled = 0;
406 if (!val || strlen(val) == 0)
409 rtc_enabled = get_int(val);
411 if (rtc_enabled != -1) {
412 configure_rtc_emulation_amiga(rtc_enabled);
416 if (val && strlen(val) != 0)
417 set_hard_drive_image_file_amiga(0, val);
420 if (val && strlen(val) != 0)
421 set_hard_drive_image_file_amiga(1, val);
424 printf("[AMIGA] CDTV mode enabled.\n");
427 if (CHKVAR("rtg") && !rtg_enabled) {
428 if (init_rtg_data(cfg)) {
429 printf("[AMIGA] RTG Enabled.\n");
431 adjust_ranges_amiga(cfg);
434 printf("[AMIGA} Failed to enable RTG.\n");
436 if CHKVAR("kick13") {
437 printf("[AMIGA] Kickstart 1.3 mode enabled, Z3 PICs will not be enumerated.\n");
441 int32_t res = a314_init();
443 printf("[AMIGA] Failed to enable A314 emulation, error return code: %d.\n", res);
445 printf("[AMIGA] A314 emulation enabled.\n");
446 add_z2_pic(ACTYPE_A314, 0);
447 a314_emulation_enabled = 1;
450 if CHKVAR("a314conf") {
451 if (val && strlen(val) != 0) {
452 a314_set_config_file(val);
457 if (CHKVAR("piscsi") && !piscsi_enabled) {
458 printf("[AMIGA] PISCSI Interface Enabled.\n");
461 add_z2_pic(ACTYPE_PISCSI, 0);
462 adjust_ranges_amiga(cfg);
464 if (piscsi_enabled) {
465 if CHKVAR("piscsi0") {
466 piscsi_map_drive(val, 0);
468 if CHKVAR("piscsi1") {
469 piscsi_map_drive(val, 1);
471 if CHKVAR("piscsi2") {
472 piscsi_map_drive(val, 2);
474 if CHKVAR("piscsi3") {
475 piscsi_map_drive(val, 3);
477 if CHKVAR("piscsi4") {
478 piscsi_map_drive(val, 4);
480 if CHKVAR("piscsi5") {
481 piscsi_map_drive(val, 5);
483 if CHKVAR("piscsi6") {
484 piscsi_map_drive(val, 6);
489 if (CHKVAR("pi-net")&& !pinet_enabled) {
490 printf("[AMIGA] PI-NET Interface Enabled.\n");
493 adjust_ranges_amiga(cfg);
496 if CHKVAR("no-pistorm-dev") {
497 pistorm_dev_enabled = 0;
498 printf("[AMIGA] Disabling PiStorm interaction device.\n");
502 if CHKVAR("rtc_type") {
503 if (val && strlen(val) != 0) {
504 if (strcmp(val, "msm") == 0) {
505 printf("[AMIGA] RTC type set to MSM.\n");
506 rtc_type = RTC_TYPE_MSM;
509 printf("[AMIGA] RTC type set to Ricoh.\n");
510 rtc_type = RTC_TYPE_RICOH;
516 void handle_reset_amiga(struct emulator_config *cfg) {
517 ac_z2_done = (ac_z2_pic_count == 0);
518 ac_z3_done = (ac_z3_pic_count == 0);
519 ac_z2_current_pic = 0;
520 ac_z3_current_pic = 0;
522 DEBUG("[AMIGA] Reset handler.\n");
523 DEBUG("[AMIGA] AC done - Z2: %d Z3: %d.\n", ac_z2_done, ac_z3_done);
526 piscsi_refresh_drives();
528 adjust_ranges_amiga(cfg);
531 void shutdown_platform_amiga(struct emulator_config *cfg) {
534 FILE *out = fopen("data/cdtv.sram", "wb+");
536 printf("Saving CDTV SRAM.\n");
537 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
541 printf("Failed to write CDTV SRAM to disk.\n");
544 if (cfg->platform->subsys) {
545 free(cfg->platform->subsys);
547 if (piscsi_enabled) {
558 if (a314_emulation_enabled) {
559 a314_emulation_enabled = 0;
564 autoconfig_reset_all();
565 printf("[AMIGA] Platform shutdown completed.\n");
568 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
569 cfg->register_read = handle_register_read_amiga;
570 cfg->register_write = handle_register_write_amiga;
571 cfg->custom_read = custom_read_amiga;
572 cfg->custom_write = custom_write_amiga;
573 cfg->platform_initial_setup = setup_platform_amiga;
574 cfg->handle_reset = handle_reset_amiga;
575 cfg->shutdown = shutdown_platform_amiga;
577 cfg->setvar = setvar_amiga;
578 cfg->id = PLATFORM_AMIGA;
581 cfg->subsys = malloc(strlen(subsys) + 1);
582 strcpy(cfg->subsys, subsys);
583 for (unsigned int i = 0; i < strlen(cfg->subsys); i++) {
584 cfg->subsys[i] = tolower(cfg->subsys[i]);