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
28 int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val);
29 int handle_register_write_amiga(unsigned int addr, unsigned int value, unsigned char type);
31 extern int ac_z2_current_pic;
32 extern int ac_z2_done;
33 extern int ac_z2_pic_count;
34 extern int ac_z2_type[AC_PIC_LIMIT];
35 extern int ac_z2_index[AC_PIC_LIMIT];
37 extern int ac_z3_current_pic;
38 extern int ac_z3_pic_count;
39 extern int ac_z3_done;
40 extern int ac_z3_type[AC_PIC_LIMIT];
41 extern int ac_z3_index[AC_PIC_LIMIT];
42 extern int gayle_emulation_enabled;
44 char *z2_autoconf_id = "z2_autoconf_fast";
45 char *z2_autoconf_zap_id = "^2_autoconf_fast";
46 char *z3_autoconf_id = "z3_autoconf_fast";
47 char *z3_autoconf_zap_id = "^3_autoconf_fast";
49 extern const char *op_type_names[OP_TYPE_NUM];
50 extern uint8_t cdtv_mode;
51 extern uint8_t rtc_type;
52 extern unsigned char cdtv_sram[32 * SIZE_KILO];
54 #define min(a, b) (a < b) ? a : b
55 #define max(a, b) (a > b) ? a : b
57 uint8_t rtg_enabled = 0, piscsi_enabled = 0, pinet_enabled = 0, kick13_mode = 0, pistorm_dev_enabled = 1;
59 extern uint32_t piscsi_base, pistorm_dev_base;
61 extern void stop_cpu_emulation(uint8_t disasm_cur);
63 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
67 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
68 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
69 if (type == OP_TYPE_BYTE) {
70 *val = autoconfig_read_memory_8(cfg, addr - AC_Z2_BASE);
73 printf("Unexpected %s read from Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
76 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
77 uint32_t addr_ = addr - AC_Z2_BASE;
79 addr_ = (addr_ - 2) + 0x100;
81 if (type == OP_TYPE_BYTE) {
82 *val = autoconfig_read_memory_z3_8(cfg, addr_ - AC_Z2_BASE);
85 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
89 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
90 if (ac_z3_pic_count == 0) {
95 if (type == OP_TYPE_BYTE) {
96 *val = autoconfig_read_memory_z3_8(cfg, addr - AC_Z3_BASE);
99 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
103 if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
104 *val = handle_pistorm_dev_read(addr, type);
108 if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
109 //printf("[Amiga-Custom] %s read from PISCSI base @$%.8X.\n", op_type_names[type], addr);
110 //stop_cpu_emulation(1);
111 *val = handle_piscsi_read(addr, type);
118 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
122 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
123 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
124 if (type == OP_TYPE_BYTE) {
125 autoconfig_write_memory_8(cfg, addr - AC_Z2_BASE, val);
128 printf("Unexpected %s write to Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
131 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
132 uint32_t addr_ = addr - AC_Z2_BASE;
134 addr_ = (addr_ - 2) + 0x100;
136 if (type == OP_TYPE_BYTE) {
137 autoconfig_write_memory_z3_8(cfg, addr_ - AC_Z2_BASE, val);
140 else if (type == OP_TYPE_WORD) {
141 autoconfig_write_memory_z3_16(cfg, addr_ - AC_Z2_BASE, val);
144 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
149 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
150 if (type == OP_TYPE_BYTE) {
151 if (ac_z3_pic_count == 0) {
156 //printf("Write to autoconf area.\n");
157 autoconfig_write_memory_z3_8(cfg, addr - AC_Z3_BASE, val);
160 else if (type == OP_TYPE_WORD) {
161 autoconfig_write_memory_z3_16(cfg, addr - AC_Z3_BASE, val);
165 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
170 if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
171 handle_pistorm_dev_write(addr, val, type);
175 if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
176 //printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
177 handle_piscsi_write(addr, val, type);
184 void adjust_ranges_amiga(struct emulator_config *cfg) {
185 cfg->mapped_high = 0;
187 cfg->custom_high = 0;
190 // Set up the min/max ranges for mapped reads/writes
191 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
192 if (cfg->map_type[i] != MAPTYPE_NONE) {
193 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
194 cfg->mapped_low = cfg->map_offset[i];
195 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
196 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
200 if ((ac_z2_pic_count || ac_z3_pic_count) && (!ac_z2_done || !ac_z3_done)) {
201 if (cfg->custom_low == 0)
202 cfg->custom_low = AC_Z2_BASE;
204 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
205 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
207 /*if (ac_z3_pic_count && !ac_z3_done) {
208 if (cfg->custom_low == 0)
209 cfg->custom_low = AC_Z3_BASE;
211 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
212 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
215 if (cfg->custom_low == 0)
216 cfg->custom_low = PIGFX_RTG_BASE;
218 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
219 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
221 if (piscsi_enabled) {
222 if (cfg->custom_low == 0)
223 cfg->custom_low = PISCSI_OFFSET;
225 cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
226 cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
227 if (piscsi_base != 0) {
228 cfg->custom_low = min(cfg->custom_low, piscsi_base);
232 if (cfg->custom_low == 0)
233 cfg->custom_low = PINET_OFFSET;
235 cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
236 cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
239 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
240 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
243 int setup_platform_amiga(struct emulator_config *cfg) {
244 printf("Performing setup for Amiga platform.\n");
246 if (strlen(cfg->platform->subsys)) {
247 printf("Subsystem is [%s]\n", cfg->platform->subsys);
248 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
249 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
252 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
253 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
256 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
257 printf("Configuring platform for CDTV emulation.\n");
259 rtc_type = RTC_TYPE_MSM;
263 printf("No sub system specified.\n");
265 // Look for Z2 autoconf Fast RAM by id
266 int index = get_named_mapped_item(cfg, z2_autoconf_id);
269 // "Zap" config items as they are processed.
270 cfg->map_id[index][0] = '^';
272 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
273 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizing to 8MB.\n");
274 resize_data = 8 * SIZE_MEGA;
276 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
277 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
278 if (cfg->map_size[index] > 8 * SIZE_MEGA)
279 resize_data = 8 * SIZE_MEGA;
280 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
281 resize_data = 4 * SIZE_MEGA;
283 resize_data = 2 * SIZE_MEGA;
284 printf("%dMB.\n", resize_data / SIZE_MEGA);
287 free(cfg->map_data[index]);
288 cfg->map_size[index] = resize_data;
289 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
291 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
292 add_z2_pic(ACTYPE_MAPFAST_Z2, index);
293 //ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
294 //ac_z2_index[ac_z2_pic_count] = index;
298 printf("No Z2 Fast RAM configured.\n");
300 index = get_named_mapped_item(cfg, z2_autoconf_id);
304 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
305 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
306 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
307 cfg->map_id[i][0] = z2_autoconf_id[0];
311 index = get_named_mapped_item(cfg, z3_autoconf_id);
314 cfg->map_id[index][0] = '^';
315 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
316 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
317 ac_z3_index[ac_z3_pic_count] = index;
321 printf("No Z3 Fast RAM configured.\n");
322 index = get_named_mapped_item(cfg, z3_autoconf_id);
325 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
326 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
327 cfg->map_id[i][0] = z3_autoconf_id[0];
331 index = get_named_mapped_item(cfg, "cpu_slot_ram");
333 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
336 adjust_ranges_amiga(cfg);
339 FILE *in = fopen("data/cdtv.sram", "rb");
341 printf("Loaded CDTV SRAM.\n");
342 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
347 if (pistorm_dev_enabled) {
348 add_z2_pic(ACTYPE_PISTORM_DEV, 0);
354 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
358 if (strcmp(var, "enable_rtc_emulation") == 0) {
359 int8_t rtc_enabled = 0;
360 if (!val || strlen(val) == 0)
363 rtc_enabled = get_int(val);
365 if (rtc_enabled != -1) {
366 configure_rtc_emulation_amiga(rtc_enabled);
369 if (strcmp(var, "hdd0") == 0) {
370 if (val && strlen(val) != 0)
371 set_hard_drive_image_file_amiga(0, val);
373 if (strcmp(var, "hdd1") == 0) {
374 if (val && strlen(val) != 0)
375 set_hard_drive_image_file_amiga(1, val);
377 if (strcmp(var, "cdtv") == 0) {
378 printf("[AMIGA] CDTV mode enabled.\n");
381 if (strcmp(var, "rtg") == 0 && !rtg_enabled) {
382 if (init_rtg_data()) {
383 printf("[AMIGA] RTG Enabled.\n");
385 adjust_ranges_amiga(cfg);
388 printf("[AMIGA} Failed to enable RTG.\n");
390 if (strcmp(var, "kick13") == 0) {
391 printf("[AMIGA] Kickstart 1.3 mode enabled, Z3 PICs will not be enumerated.\n");
396 if (strcmp(var, "piscsi") == 0 && !piscsi_enabled) {
397 printf("[AMIGA] PISCSI Interface Enabled.\n");
400 add_z2_pic(ACTYPE_PISCSI, 0);
401 //ac_z2_type[ac_z2_pic_count] = ACTYPE_PISCSI;
403 adjust_ranges_amiga(cfg);
405 if (piscsi_enabled) {
406 if (strcmp(var, "piscsi0") == 0) {
407 piscsi_map_drive(val, 0);
409 if (strcmp(var, "piscsi1") == 0) {
410 piscsi_map_drive(val, 1);
412 if (strcmp(var, "piscsi2") == 0) {
413 piscsi_map_drive(val, 2);
415 if (strcmp(var, "piscsi3") == 0) {
416 piscsi_map_drive(val, 3);
418 if (strcmp(var, "piscsi4") == 0) {
419 piscsi_map_drive(val, 4);
421 if (strcmp(var, "piscsi5") == 0) {
422 piscsi_map_drive(val, 5);
424 if (strcmp(var, "piscsi6") == 0) {
425 piscsi_map_drive(val, 6);
430 if (strcmp(var, "pi-net") == 0 && !pinet_enabled) {
431 printf("[AMIGA] PI-NET Interface Enabled.\n");
434 adjust_ranges_amiga(cfg);
437 if (strcmp(var, "no-pistorm-dev") == 0) {
438 pistorm_dev_enabled = 0;
439 printf("[AMIGA] Disabling PiStorm interaction device.\n");
443 if (strcmp(var, "rtc_type") == 0) {
444 if (val && strlen(val) != 0) {
445 if (strcmp(val, "msm") == 0) {
446 printf("[AMIGA] RTC type set to MSM.\n");
447 rtc_type = RTC_TYPE_MSM;
450 printf("[AMIGA] RTC type set to Ricoh.\n");
451 rtc_type = RTC_TYPE_RICOH;
457 void handle_reset_amiga(struct emulator_config *cfg) {
458 ac_z2_done = (ac_z2_pic_count == 0);
459 ac_z3_done = (ac_z3_pic_count == 0);
460 ac_z2_current_pic = 0;
461 ac_z3_current_pic = 0;
463 DEBUG("[AMIGA] Reset handler.\n");
464 DEBUG("[AMIGA] AC done - Z2: %d Z3: %d.\n", ac_z2_done, ac_z3_done);
467 piscsi_refresh_drives();
469 adjust_ranges_amiga(cfg);
472 void shutdown_platform_amiga(struct emulator_config *cfg) {
475 FILE *out = fopen("data/cdtv.sram", "wb+");
477 printf("Saving CDTV SRAM.\n");
478 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
482 printf("Failed to write CDTV SRAM to disk.\n");
485 if (cfg->platform->subsys) {
486 free(cfg->platform->subsys);
488 if (piscsi_enabled) {
502 autoconfig_reset_all();
503 printf("[AMIGA] Platform shutdown completed.\n");
506 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
507 cfg->register_read = handle_register_read_amiga;
508 cfg->register_write = handle_register_write_amiga;
509 cfg->custom_read = custom_read_amiga;
510 cfg->custom_write = custom_write_amiga;
511 cfg->platform_initial_setup = setup_platform_amiga;
512 cfg->handle_reset = handle_reset_amiga;
513 cfg->shutdown = shutdown_platform_amiga;
515 cfg->setvar = setvar_amiga;
516 cfg->id = PLATFORM_AMIGA;
519 cfg->subsys = malloc(strlen(subsys) + 1);
520 strcpy(cfg->subsys, subsys);
521 for (unsigned int i = 0; i < strlen(cfg->subsys); i++) {
522 cfg->subsys[i] = tolower(cfg->subsys[i]);