5 #include "amiga-autoconf.h"
6 #include "amiga-registers.h"
7 #include "hunk-reloc.h"
8 #include "net/pi-net-enums.h"
9 #include "net/pi-net.h"
10 #include "piscsi/piscsi-enums.h"
11 #include "piscsi/piscsi.h"
12 #include "platforms/platforms.h"
13 #include "platforms/shared/rtc.h"
16 //#define DEBUG_AMIGA_PLATFORM
18 #ifdef DEBUG_AMIGA_PLATFORM
25 int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val);
26 int handle_register_write_amiga(unsigned int addr, unsigned int value, unsigned char type);
29 extern int ac_z2_current_pic;
30 extern int ac_z2_done;
31 extern int ac_z2_pic_count;
32 extern int ac_z2_type[AC_PIC_LIMIT];
33 extern int ac_z2_index[AC_PIC_LIMIT];
35 extern int ac_z3_current_pic;
36 extern int ac_z3_pic_count;
37 extern int ac_z3_done;
38 extern int ac_z3_type[AC_PIC_LIMIT];
39 extern int ac_z3_index[AC_PIC_LIMIT];
40 extern int gayle_emulation_enabled;
42 char *z2_autoconf_id = "z2_autoconf_fast";
43 char *z2_autoconf_zap_id = "^2_autoconf_fast";
44 char *z3_autoconf_id = "z3_autoconf_fast";
45 char *z3_autoconf_zap_id = "^3_autoconf_fast";
47 extern const char *op_type_names[OP_TYPE_NUM];
48 extern uint8_t cdtv_mode;
49 extern uint8_t rtc_type;
50 extern unsigned char cdtv_sram[32 * SIZE_KILO];
52 #define min(a, b) (a < b) ? a : b
53 #define max(a, b) (a > b) ? a : b
55 uint8_t rtg_enabled = 0, piscsi_enabled = 0, pinet_enabled = 0, kick13_mode = 0;
57 extern uint32_t piscsi_base;
59 extern void stop_cpu_emulation(uint8_t disasm_cur);
61 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
65 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
66 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
67 if (type == OP_TYPE_BYTE) {
68 *val = autoconfig_read_memory_8(cfg, addr - AC_Z2_BASE);
71 printf("Unexpected %s read from Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
74 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
75 uint32_t addr_ = addr - AC_Z2_BASE;
77 addr_ = (addr_ - 2) + 0x100;
79 if (type == OP_TYPE_BYTE) {
80 *val = autoconfig_read_memory_z3_8(cfg, addr_ - AC_Z2_BASE);
83 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
87 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
88 if (ac_z3_pic_count == 0) {
93 if (type == OP_TYPE_BYTE) {
94 *val = autoconfig_read_memory_z3_8(cfg, addr - AC_Z3_BASE);
97 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
101 if (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
102 //printf("[Amiga-Custom] %s read from PISCSI base @$%.8X.\n", op_type_names[type], addr);
103 //stop_cpu_emulation(1);
104 *val = handle_piscsi_read(addr, type);
111 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
115 if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
116 if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
117 if (type == OP_TYPE_BYTE) {
118 autoconfig_write_memory_8(cfg, addr - AC_Z2_BASE, val);
121 printf("Unexpected %s write to Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
124 if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
125 uint32_t addr_ = addr - AC_Z2_BASE;
127 addr_ = (addr_ - 2) + 0x100;
129 if (type == OP_TYPE_BYTE) {
130 autoconfig_write_memory_z3_8(cfg, addr_ - AC_Z2_BASE, val);
133 else if (type == OP_TYPE_WORD) {
134 autoconfig_write_memory_z3_16(cfg, addr_ - AC_Z2_BASE, val);
137 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
142 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
143 if (type == OP_TYPE_BYTE) {
144 if (ac_z3_pic_count == 0) {
149 //printf("Write to autoconf area.\n");
150 autoconfig_write_memory_z3_8(cfg, addr - AC_Z3_BASE, val);
153 else if (type == OP_TYPE_WORD) {
154 autoconfig_write_memory_z3_16(cfg, addr - AC_Z3_BASE, val);
158 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
163 if (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
164 //printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
165 handle_piscsi_write(addr, val, type);
172 void adjust_ranges_amiga(struct emulator_config *cfg) {
173 cfg->mapped_high = 0;
175 cfg->custom_high = 0;
178 // Set up the min/max ranges for mapped reads/writes
179 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
180 if (cfg->map_type[i] != MAPTYPE_NONE) {
181 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
182 cfg->mapped_low = cfg->map_offset[i];
183 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
184 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
188 if (ac_z2_pic_count && (!ac_z2_done || !ac_z3_done)) {
189 if (cfg->custom_low == 0)
190 cfg->custom_low = AC_Z2_BASE;
192 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
193 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
195 if (ac_z3_pic_count && !ac_z3_done) {
196 if (cfg->custom_low == 0)
197 cfg->custom_low = AC_Z3_BASE;
199 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
200 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
203 if (cfg->custom_low == 0)
204 cfg->custom_low = PIGFX_RTG_BASE;
206 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
207 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
209 if (piscsi_enabled) {
210 if (cfg->custom_low == 0)
211 cfg->custom_low = PISCSI_OFFSET;
213 cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
214 cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
215 if (piscsi_base != 0) {
216 cfg->custom_low = min(cfg->custom_low, piscsi_base);
220 if (cfg->custom_low == 0)
221 cfg->custom_low = PINET_OFFSET;
223 cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
224 cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
227 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
228 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
231 int setup_platform_amiga(struct emulator_config *cfg) {
232 printf("Performing setup for Amiga platform.\n");
234 if (strlen(cfg->platform->subsys)) {
235 printf("Subsystem is [%s]\n", cfg->platform->subsys);
236 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
237 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
240 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
241 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
244 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
245 printf("Configuring platform for CDTV emulation.\n");
247 rtc_type = RTC_TYPE_MSM;
251 printf("No sub system specified.\n");
253 // Look for Z2 autoconf Fast RAM by id
254 int index = get_named_mapped_item(cfg, z2_autoconf_id);
257 // "Zap" config items as they are processed.
258 cfg->map_id[index][0] = '^';
260 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
261 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
262 resize_data = 8 * SIZE_MEGA;
264 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
265 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
266 if (cfg->map_size[index] > 8 * SIZE_MEGA)
267 resize_data = 8 * SIZE_MEGA;
268 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
269 resize_data = 4 * SIZE_MEGA;
271 resize_data = 2 * SIZE_MEGA;
272 printf("%dMB.\n", resize_data / SIZE_MEGA);
275 free(cfg->map_data[index]);
276 cfg->map_size[index] = resize_data;
277 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
279 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
280 ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
281 ac_z2_index[ac_z2_pic_count] = index;
285 printf("No Z2 Fast RAM configured.\n");
287 index = get_named_mapped_item(cfg, z2_autoconf_id);
291 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
292 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
293 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
294 cfg->map_id[i][0] = z2_autoconf_id[0];
298 index = get_named_mapped_item(cfg, z3_autoconf_id);
301 cfg->map_id[index][0] = '^';
302 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
303 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
304 ac_z3_index[ac_z3_pic_count] = index;
308 printf("No Z3 Fast RAM configured.\n");
309 index = get_named_mapped_item(cfg, z3_autoconf_id);
312 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
313 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
314 cfg->map_id[i][0] = z3_autoconf_id[0];
318 index = get_named_mapped_item(cfg, "cpu_slot_ram");
320 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
323 adjust_ranges_amiga(cfg);
326 FILE *in = fopen("data/cdtv.sram", "rb");
328 printf("Loaded CDTV SRAM.\n");
329 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
337 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
341 if (strcmp(var, "enable_rtc_emulation") == 0) {
342 int8_t rtc_enabled = 0;
343 if (!val || strlen(val) == 0)
346 rtc_enabled = get_int(val);
348 if (rtc_enabled != -1) {
349 configure_rtc_emulation_amiga(rtc_enabled);
352 if (strcmp(var, "hdd0") == 0) {
353 if (val && strlen(val) != 0)
354 set_hard_drive_image_file_amiga(0, val);
356 if (strcmp(var, "hdd1") == 0) {
357 if (val && strlen(val) != 0)
358 set_hard_drive_image_file_amiga(1, val);
360 if (strcmp(var, "cdtv") == 0) {
361 printf("[AMIGA] CDTV mode enabled.\n");
364 if (strcmp(var, "rtg") == 0 && !rtg_enabled) {
365 if (init_rtg_data()) {
366 printf("[AMIGA] RTG Enabled.\n");
368 adjust_ranges_amiga(cfg);
371 printf("[AMIGA} Failed to enable RTG.\n");
373 if (strcmp(var, "kick13") == 0) {
374 printf("[AMIGA] Kickstart 1.3 mode enabled, Z3 PICs will not be enumerated.\n");
379 if (strcmp(var, "piscsi") == 0 && !piscsi_enabled) {
380 printf("[AMIGA] PISCSI Interface Enabled.\n");
383 ac_z2_type[ac_z2_pic_count] = ACTYPE_PISCSI;
385 adjust_ranges_amiga(cfg);
387 if (piscsi_enabled) {
388 if (strcmp(var, "piscsi0") == 0) {
389 piscsi_map_drive(val, 0);
391 if (strcmp(var, "piscsi1") == 0) {
392 piscsi_map_drive(val, 1);
394 if (strcmp(var, "piscsi2") == 0) {
395 piscsi_map_drive(val, 2);
397 if (strcmp(var, "piscsi3") == 0) {
398 piscsi_map_drive(val, 3);
400 if (strcmp(var, "piscsi4") == 0) {
401 piscsi_map_drive(val, 4);
403 if (strcmp(var, "piscsi5") == 0) {
404 piscsi_map_drive(val, 5);
406 if (strcmp(var, "piscsi6") == 0) {
407 piscsi_map_drive(val, 6);
412 if (strcmp(var, "pi-net") == 0 && !pinet_enabled) {
413 printf("[AMIGA] PI-NET Interface Enabled.\n");
416 adjust_ranges_amiga(cfg);
420 if (strcmp(var, "rtc_type") == 0) {
421 if (val && strlen(val) != 0) {
422 if (strcmp(val, "msm") == 0) {
423 printf("[AMIGA] RTC type set to MSM.\n");
424 rtc_type = RTC_TYPE_MSM;
427 printf("[AMIGA] RTC type set to Ricoh.\n");
428 rtc_type = RTC_TYPE_RICOH;
434 void handle_reset_amiga(struct emulator_config *cfg) {
435 ac_z2_done = (ac_z2_pic_count == 0);
436 ac_z3_done = (ac_z3_pic_count == 0);
437 ac_z2_current_pic = 0;
438 ac_z3_current_pic = 0;
440 DEBUG("[AMIGA] Reset handler.\n");
441 DEBUG("[AMIGA] AC done - Z2: %d Z3: %d.\n", ac_z2_done, ac_z3_done);
444 piscsi_refresh_drives();
446 adjust_ranges_amiga(cfg);
449 void shutdown_platform_amiga(struct emulator_config *cfg) {
452 FILE *out = fopen("data/cdtv.sram", "wb+");
454 printf("Saving CDTV SRAM.\n");
455 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
459 printf("Failed to write CDTV SRAM to disk.\n");
464 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
465 cfg->register_read = handle_register_read_amiga;
466 cfg->register_write = handle_register_write_amiga;
467 cfg->custom_read = custom_read_amiga;
468 cfg->custom_write = custom_write_amiga;
469 cfg->platform_initial_setup = setup_platform_amiga;
470 cfg->handle_reset = handle_reset_amiga;
471 cfg->shutdown = shutdown_platform_amiga;
473 cfg->setvar = setvar_amiga;
474 cfg->id = PLATFORM_AMIGA;
477 cfg->subsys = malloc(strlen(subsys) + 1);
478 strcpy(cfg->subsys, subsys);
479 for (int i = 0; i < strlen(cfg->subsys); i++) {
480 cfg->subsys[i] = tolower(cfg->subsys[i]);