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 "platforms/platforms.h"
15 #include "platforms/shared/rtc.h"
18 //#define DEBUG_AMIGA_PLATFORM
20 #ifdef DEBUG_AMIGA_PLATFORM
27 int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val);
28 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;
59 extern uint32_t piscsi_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 (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
104 //printf("[Amiga-Custom] %s read from PISCSI base @$%.8X.\n", op_type_names[type], addr);
105 //stop_cpu_emulation(1);
106 *val = handle_piscsi_read(addr, type);
113 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
117 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
118 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
119 if (type == OP_TYPE_BYTE) {
120 autoconfig_write_memory_8(cfg, addr - AC_Z2_BASE, val);
123 printf("Unexpected %s write to Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
126 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
127 uint32_t addr_ = addr - AC_Z2_BASE;
129 addr_ = (addr_ - 2) + 0x100;
131 if (type == OP_TYPE_BYTE) {
132 autoconfig_write_memory_z3_8(cfg, addr_ - AC_Z2_BASE, val);
135 else if (type == OP_TYPE_WORD) {
136 autoconfig_write_memory_z3_16(cfg, addr_ - AC_Z2_BASE, val);
139 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
144 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
145 if (type == OP_TYPE_BYTE) {
146 if (ac_z3_pic_count == 0) {
151 //printf("Write to autoconf area.\n");
152 autoconfig_write_memory_z3_8(cfg, addr - AC_Z3_BASE, val);
155 else if (type == OP_TYPE_WORD) {
156 autoconfig_write_memory_z3_16(cfg, addr - AC_Z3_BASE, val);
160 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
165 if (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
166 //printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
167 handle_piscsi_write(addr, val, type);
174 void adjust_ranges_amiga(struct emulator_config *cfg) {
175 cfg->mapped_high = 0;
177 cfg->custom_high = 0;
180 // Set up the min/max ranges for mapped reads/writes
181 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
182 if (cfg->map_type[i] != MAPTYPE_NONE) {
183 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
184 cfg->mapped_low = cfg->map_offset[i];
185 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
186 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
190 if (ac_z2_pic_count && (!ac_z2_done || !ac_z3_done)) {
191 if (cfg->custom_low == 0)
192 cfg->custom_low = AC_Z2_BASE;
194 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
195 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
197 if (ac_z3_pic_count && !ac_z3_done) {
198 if (cfg->custom_low == 0)
199 cfg->custom_low = AC_Z3_BASE;
201 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
202 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
205 if (cfg->custom_low == 0)
206 cfg->custom_low = PIGFX_RTG_BASE;
208 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
209 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
211 if (piscsi_enabled) {
212 if (cfg->custom_low == 0)
213 cfg->custom_low = PISCSI_OFFSET;
215 cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
216 cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
217 if (piscsi_base != 0) {
218 cfg->custom_low = min(cfg->custom_low, piscsi_base);
222 if (cfg->custom_low == 0)
223 cfg->custom_low = PINET_OFFSET;
225 cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
226 cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
229 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
230 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
233 int setup_platform_amiga(struct emulator_config *cfg) {
234 printf("Performing setup for Amiga platform.\n");
236 if (strlen(cfg->platform->subsys)) {
237 printf("Subsystem is [%s]\n", cfg->platform->subsys);
238 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
239 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
242 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
243 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
246 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
247 printf("Configuring platform for CDTV emulation.\n");
249 rtc_type = RTC_TYPE_MSM;
253 printf("No sub system specified.\n");
255 // Look for Z2 autoconf Fast RAM by id
256 int index = get_named_mapped_item(cfg, z2_autoconf_id);
259 // "Zap" config items as they are processed.
260 cfg->map_id[index][0] = '^';
262 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
263 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
264 resize_data = 8 * SIZE_MEGA;
266 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
267 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
268 if (cfg->map_size[index] > 8 * SIZE_MEGA)
269 resize_data = 8 * SIZE_MEGA;
270 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
271 resize_data = 4 * SIZE_MEGA;
273 resize_data = 2 * SIZE_MEGA;
274 printf("%dMB.\n", resize_data / SIZE_MEGA);
277 free(cfg->map_data[index]);
278 cfg->map_size[index] = resize_data;
279 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
281 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
282 ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
283 ac_z2_index[ac_z2_pic_count] = index;
287 printf("No Z2 Fast RAM configured.\n");
289 index = get_named_mapped_item(cfg, z2_autoconf_id);
293 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
294 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
295 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
296 cfg->map_id[i][0] = z2_autoconf_id[0];
300 index = get_named_mapped_item(cfg, z3_autoconf_id);
303 cfg->map_id[index][0] = '^';
304 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
305 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
306 ac_z3_index[ac_z3_pic_count] = index;
310 printf("No Z3 Fast RAM configured.\n");
311 index = get_named_mapped_item(cfg, z3_autoconf_id);
314 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
315 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
316 cfg->map_id[i][0] = z3_autoconf_id[0];
320 index = get_named_mapped_item(cfg, "cpu_slot_ram");
322 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
325 adjust_ranges_amiga(cfg);
328 FILE *in = fopen("data/cdtv.sram", "rb");
330 printf("Loaded CDTV SRAM.\n");
331 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
339 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
343 if (strcmp(var, "enable_rtc_emulation") == 0) {
344 int8_t rtc_enabled = 0;
345 if (!val || strlen(val) == 0)
348 rtc_enabled = get_int(val);
350 if (rtc_enabled != -1) {
351 configure_rtc_emulation_amiga(rtc_enabled);
354 if (strcmp(var, "hdd0") == 0) {
355 if (val && strlen(val) != 0)
356 set_hard_drive_image_file_amiga(0, val);
358 if (strcmp(var, "hdd1") == 0) {
359 if (val && strlen(val) != 0)
360 set_hard_drive_image_file_amiga(1, val);
362 if (strcmp(var, "cdtv") == 0) {
363 printf("[AMIGA] CDTV mode enabled.\n");
366 if (strcmp(var, "rtg") == 0 && !rtg_enabled) {
367 if (init_rtg_data()) {
368 printf("[AMIGA] RTG Enabled.\n");
370 adjust_ranges_amiga(cfg);
373 printf("[AMIGA} Failed to enable RTG.\n");
375 if (strcmp(var, "kick13") == 0) {
376 printf("[AMIGA] Kickstart 1.3 mode enabled, Z3 PICs will not be enumerated.\n");
381 if (strcmp(var, "piscsi") == 0 && !piscsi_enabled) {
382 printf("[AMIGA] PISCSI Interface Enabled.\n");
385 ac_z2_type[ac_z2_pic_count] = ACTYPE_PISCSI;
387 adjust_ranges_amiga(cfg);
389 if (piscsi_enabled) {
390 if (strcmp(var, "piscsi0") == 0) {
391 piscsi_map_drive(val, 0);
393 if (strcmp(var, "piscsi1") == 0) {
394 piscsi_map_drive(val, 1);
396 if (strcmp(var, "piscsi2") == 0) {
397 piscsi_map_drive(val, 2);
399 if (strcmp(var, "piscsi3") == 0) {
400 piscsi_map_drive(val, 3);
402 if (strcmp(var, "piscsi4") == 0) {
403 piscsi_map_drive(val, 4);
405 if (strcmp(var, "piscsi5") == 0) {
406 piscsi_map_drive(val, 5);
408 if (strcmp(var, "piscsi6") == 0) {
409 piscsi_map_drive(val, 6);
414 if (strcmp(var, "pi-net") == 0 && !pinet_enabled) {
415 printf("[AMIGA] PI-NET Interface Enabled.\n");
418 adjust_ranges_amiga(cfg);
422 if (strcmp(var, "rtc_type") == 0) {
423 if (val && strlen(val) != 0) {
424 if (strcmp(val, "msm") == 0) {
425 printf("[AMIGA] RTC type set to MSM.\n");
426 rtc_type = RTC_TYPE_MSM;
429 printf("[AMIGA] RTC type set to Ricoh.\n");
430 rtc_type = RTC_TYPE_RICOH;
436 void handle_reset_amiga(struct emulator_config *cfg) {
437 ac_z2_done = (ac_z2_pic_count == 0);
438 ac_z3_done = (ac_z3_pic_count == 0);
439 ac_z2_current_pic = 0;
440 ac_z3_current_pic = 0;
442 DEBUG("[AMIGA] Reset handler.\n");
443 DEBUG("[AMIGA] AC done - Z2: %d Z3: %d.\n", ac_z2_done, ac_z3_done);
446 piscsi_refresh_drives();
448 adjust_ranges_amiga(cfg);
451 void shutdown_platform_amiga(struct emulator_config *cfg) {
454 FILE *out = fopen("data/cdtv.sram", "wb+");
456 printf("Saving CDTV SRAM.\n");
457 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
461 printf("Failed to write CDTV SRAM to disk.\n");
466 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
467 cfg->register_read = handle_register_read_amiga;
468 cfg->register_write = handle_register_write_amiga;
469 cfg->custom_read = custom_read_amiga;
470 cfg->custom_write = custom_write_amiga;
471 cfg->platform_initial_setup = setup_platform_amiga;
472 cfg->handle_reset = handle_reset_amiga;
473 cfg->shutdown = shutdown_platform_amiga;
475 cfg->setvar = setvar_amiga;
476 cfg->id = PLATFORM_AMIGA;
479 cfg->subsys = malloc(strlen(subsys) + 1);
480 strcpy(cfg->subsys, subsys);
481 for (unsigned int i = 0; i < strlen(cfg->subsys); i++) {
482 cfg->subsys[i] = tolower(cfg->subsys[i]);