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);
34 extern int ac_z2_current_pic;
35 extern int ac_z2_done;
36 extern int ac_z2_pic_count;
37 extern int ac_z2_type[AC_PIC_LIMIT];
38 extern int ac_z2_index[AC_PIC_LIMIT];
40 extern int ac_z3_current_pic;
41 extern int ac_z3_pic_count;
42 extern int ac_z3_done;
43 extern int ac_z3_type[AC_PIC_LIMIT];
44 extern int ac_z3_index[AC_PIC_LIMIT];
45 extern int gayle_emulation_enabled;
47 char *z2_autoconf_id = "z2_autoconf_fast";
48 char *z2_autoconf_zap_id = "^2_autoconf_fast";
49 char *z3_autoconf_id = "z3_autoconf_fast";
50 char *z3_autoconf_zap_id = "^3_autoconf_fast";
52 extern const char *op_type_names[OP_TYPE_NUM];
53 extern uint8_t cdtv_mode;
54 extern uint8_t rtc_type;
55 extern unsigned char cdtv_sram[32 * SIZE_KILO];
57 #define min(a, b) (a < b) ? a : b
58 #define max(a, b) (a > b) ? a : b
60 uint8_t rtg_enabled = 0, piscsi_enabled = 0, pinet_enabled = 0, kick13_mode = 0, pistorm_dev_enabled = 1;
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);
121 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
125 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
126 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
127 if (type == OP_TYPE_BYTE) {
128 autoconfig_write_memory_8(cfg, addr - AC_Z2_BASE, val);
131 printf("Unexpected %s write to Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
134 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
135 uint32_t addr_ = addr - AC_Z2_BASE;
137 addr_ = (addr_ - 2) + 0x100;
139 if (type == OP_TYPE_BYTE) {
140 autoconfig_write_memory_z3_8(cfg, addr_ - AC_Z2_BASE, val);
143 else if (type == OP_TYPE_WORD) {
144 autoconfig_write_memory_z3_16(cfg, addr_ - AC_Z2_BASE, val);
147 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
152 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
153 if (type == OP_TYPE_BYTE) {
154 if (ac_z3_pic_count == 0) {
159 //printf("Write to autoconf area.\n");
160 autoconfig_write_memory_z3_8(cfg, addr - AC_Z3_BASE, val);
163 else if (type == OP_TYPE_WORD) {
164 autoconfig_write_memory_z3_16(cfg, addr - AC_Z3_BASE, val);
168 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
173 if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
174 handle_pistorm_dev_write(addr, val, type);
178 if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
179 //printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
180 handle_piscsi_write(addr, val, type);
187 void adjust_ranges_amiga(struct emulator_config *cfg) {
188 cfg->mapped_high = 0;
190 cfg->custom_high = 0;
193 // Set up the min/max ranges for mapped reads/writes
194 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
195 if (cfg->map_type[i] != MAPTYPE_NONE) {
196 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
197 cfg->mapped_low = cfg->map_offset[i];
198 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
199 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
203 if ((ac_z2_pic_count || ac_z3_pic_count) && (!ac_z2_done || !ac_z3_done)) {
204 if (cfg->custom_low == 0)
205 cfg->custom_low = AC_Z2_BASE;
207 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
208 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
210 /*if (ac_z3_pic_count && !ac_z3_done) {
211 if (cfg->custom_low == 0)
212 cfg->custom_low = AC_Z3_BASE;
214 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
215 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
218 if (cfg->custom_low == 0)
219 cfg->custom_low = PIGFX_RTG_BASE;
221 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
222 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
224 if (piscsi_enabled) {
225 if (cfg->custom_low == 0)
226 cfg->custom_low = PISCSI_OFFSET;
228 cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
229 cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
230 if (piscsi_base != 0) {
231 cfg->custom_low = min(cfg->custom_low, piscsi_base);
235 if (cfg->custom_low == 0)
236 cfg->custom_low = PINET_OFFSET;
238 cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
239 cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
242 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
243 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
246 int setup_platform_amiga(struct emulator_config *cfg) {
247 printf("Performing setup for Amiga platform.\n");
249 if (strlen(cfg->platform->subsys)) {
250 printf("Subsystem is [%s]\n", cfg->platform->subsys);
251 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
252 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
255 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
256 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
259 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
260 printf("Configuring platform for CDTV emulation.\n");
262 rtc_type = RTC_TYPE_MSM;
266 printf("No sub system specified.\n");
268 // Look for Z2 autoconf Fast RAM by id
269 int index = get_named_mapped_item(cfg, z2_autoconf_id);
272 // "Zap" config items as they are processed.
273 cfg->map_id[index][0] = '^';
275 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
276 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizing to 8MB.\n");
277 resize_data = 8 * SIZE_MEGA;
279 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
280 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
281 if (cfg->map_size[index] > 8 * SIZE_MEGA)
282 resize_data = 8 * SIZE_MEGA;
283 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
284 resize_data = 4 * SIZE_MEGA;
286 resize_data = 2 * SIZE_MEGA;
287 printf("%dMB.\n", resize_data / SIZE_MEGA);
290 free(cfg->map_data[index]);
291 cfg->map_size[index] = resize_data;
292 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
294 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
295 add_z2_pic(ACTYPE_MAPFAST_Z2, index);
296 //ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
297 //ac_z2_index[ac_z2_pic_count] = index;
301 printf("No Z2 Fast RAM configured.\n");
303 index = get_named_mapped_item(cfg, z2_autoconf_id);
307 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
308 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
309 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
310 cfg->map_id[i][0] = z2_autoconf_id[0];
314 index = get_named_mapped_item(cfg, z3_autoconf_id);
317 cfg->map_id[index][0] = '^';
318 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
319 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
320 ac_z3_index[ac_z3_pic_count] = index;
324 printf("No Z3 Fast RAM configured.\n");
325 index = get_named_mapped_item(cfg, z3_autoconf_id);
328 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
329 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
330 cfg->map_id[i][0] = z3_autoconf_id[0];
334 index = get_named_mapped_item(cfg, "cpu_slot_ram");
336 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
339 adjust_ranges_amiga(cfg);
342 FILE *in = fopen("data/cdtv.sram", "rb");
344 printf("Loaded CDTV SRAM.\n");
345 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
350 if (pistorm_dev_enabled) {
351 add_z2_pic(ACTYPE_PISTORM_DEV, 0);
357 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
361 if (strcmp(var, "enable_rtc_emulation") == 0) {
362 int8_t rtc_enabled = 0;
363 if (!val || strlen(val) == 0)
366 rtc_enabled = get_int(val);
368 if (rtc_enabled != -1) {
369 configure_rtc_emulation_amiga(rtc_enabled);
372 if (strcmp(var, "hdd0") == 0) {
373 if (val && strlen(val) != 0)
374 set_hard_drive_image_file_amiga(0, val);
376 if (strcmp(var, "hdd1") == 0) {
377 if (val && strlen(val) != 0)
378 set_hard_drive_image_file_amiga(1, val);
380 if (strcmp(var, "cdtv") == 0) {
381 printf("[AMIGA] CDTV mode enabled.\n");
384 if (strcmp(var, "rtg") == 0 && !rtg_enabled) {
385 if (init_rtg_data()) {
386 printf("[AMIGA] RTG Enabled.\n");
388 adjust_ranges_amiga(cfg);
391 printf("[AMIGA} Failed to enable RTG.\n");
393 if (strcmp(var, "kick13") == 0) {
394 printf("[AMIGA] Kickstart 1.3 mode enabled, Z3 PICs will not be enumerated.\n");
399 if (strcmp(var, "piscsi") == 0 && !piscsi_enabled) {
400 printf("[AMIGA] PISCSI Interface Enabled.\n");
403 add_z2_pic(ACTYPE_PISCSI, 0);
404 //ac_z2_type[ac_z2_pic_count] = ACTYPE_PISCSI;
406 adjust_ranges_amiga(cfg);
408 if (piscsi_enabled) {
409 if (strcmp(var, "piscsi0") == 0) {
410 piscsi_map_drive(val, 0);
412 if (strcmp(var, "piscsi1") == 0) {
413 piscsi_map_drive(val, 1);
415 if (strcmp(var, "piscsi2") == 0) {
416 piscsi_map_drive(val, 2);
418 if (strcmp(var, "piscsi3") == 0) {
419 piscsi_map_drive(val, 3);
421 if (strcmp(var, "piscsi4") == 0) {
422 piscsi_map_drive(val, 4);
424 if (strcmp(var, "piscsi5") == 0) {
425 piscsi_map_drive(val, 5);
427 if (strcmp(var, "piscsi6") == 0) {
428 piscsi_map_drive(val, 6);
433 if (strcmp(var, "pi-net") == 0 && !pinet_enabled) {
434 printf("[AMIGA] PI-NET Interface Enabled.\n");
437 adjust_ranges_amiga(cfg);
440 if (strcmp(var, "no-pistorm-dev") == 0) {
441 pistorm_dev_enabled = 0;
442 printf("[AMIGA] Disabling PiStorm interaction device.\n");
446 if (strcmp(var, "rtc_type") == 0) {
447 if (val && strlen(val) != 0) {
448 if (strcmp(val, "msm") == 0) {
449 printf("[AMIGA] RTC type set to MSM.\n");
450 rtc_type = RTC_TYPE_MSM;
453 printf("[AMIGA] RTC type set to Ricoh.\n");
454 rtc_type = RTC_TYPE_RICOH;
460 void handle_reset_amiga(struct emulator_config *cfg) {
461 ac_z2_done = (ac_z2_pic_count == 0);
462 ac_z3_done = (ac_z3_pic_count == 0);
463 ac_z2_current_pic = 0;
464 ac_z3_current_pic = 0;
466 DEBUG("[AMIGA] Reset handler.\n");
467 DEBUG("[AMIGA] AC done - Z2: %d Z3: %d.\n", ac_z2_done, ac_z3_done);
470 piscsi_refresh_drives();
472 adjust_ranges_amiga(cfg);
475 void shutdown_platform_amiga(struct emulator_config *cfg) {
478 FILE *out = fopen("data/cdtv.sram", "wb+");
480 printf("Saving CDTV SRAM.\n");
481 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
485 printf("Failed to write CDTV SRAM to disk.\n");
488 if (cfg->platform->subsys) {
489 free(cfg->platform->subsys);
491 if (piscsi_enabled) {
505 autoconfig_reset_all();
506 printf("[AMIGA] Platform shutdown completed.\n");
509 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
510 cfg->register_read = handle_register_read_amiga;
511 cfg->register_write = handle_register_write_amiga;
512 cfg->custom_read = custom_read_amiga;
513 cfg->custom_write = custom_write_amiga;
514 cfg->platform_initial_setup = setup_platform_amiga;
515 cfg->handle_reset = handle_reset_amiga;
516 cfg->shutdown = shutdown_platform_amiga;
518 cfg->setvar = setvar_amiga;
519 cfg->id = PLATFORM_AMIGA;
522 cfg->subsys = malloc(strlen(subsys) + 1);
523 strcpy(cfg->subsys, subsys);
524 for (unsigned int i = 0; i < strlen(cfg->subsys); i++) {
525 cfg->subsys[i] = tolower(cfg->subsys[i]);