5 #include "../platforms.h"
6 #include "amiga-autoconf.h"
7 #include "amiga-registers.h"
8 #include "../shared/rtc.h"
9 #include "piscsi/piscsi.h"
10 #include "piscsi/piscsi-enums.h"
11 #include "net/pi-net.h"
12 #include "net/pi-net-enums.h"
15 int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val);
16 int handle_register_write_amiga(unsigned int addr, unsigned int value, unsigned char type);
19 extern int ac_z2_current_pic;
20 extern int ac_z2_done;
21 extern int ac_z2_pic_count;
22 extern int ac_z2_type[AC_PIC_LIMIT];
23 extern int ac_z2_index[AC_PIC_LIMIT];
25 extern int ac_z3_current_pic;
26 extern int ac_z3_pic_count;
27 extern int ac_z3_done;
28 extern int ac_z3_type[AC_PIC_LIMIT];
29 extern int ac_z3_index[AC_PIC_LIMIT];
30 extern int gayle_emulation_enabled;
32 char *z2_autoconf_id = "z2_autoconf_fast";
33 char *z2_autoconf_zap_id = "^2_autoconf_fast";
34 char *z3_autoconf_id = "z3_autoconf_fast";
35 char *z3_autoconf_zap_id = "^3_autoconf_fast";
37 extern const char *op_type_names[OP_TYPE_NUM];
38 extern uint8_t cdtv_mode;
39 extern uint8_t rtc_type;
40 extern unsigned char cdtv_sram[32 * SIZE_KILO];
42 #define min(a, b) (a < b) ? a : b
43 #define max(a, b) (a > b) ? a : b
45 static uint8_t rtg_enabled = 0, piscsi_enabled = 0, pinet_enabled = 0;
47 extern uint32_t piscsi_base;
48 extern uint8_t piscsi_diag_read = 0;
50 extern void stop_cpu_emulation(uint8_t disasm_cur);
52 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
53 if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
54 if (ac_z2_pic_count == 0) {
59 if (type == OP_TYPE_BYTE) {
60 *val = autoconfig_read_memory_8(cfg, addr);
64 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
65 if (ac_z3_pic_count == 0) {
70 if (type == OP_TYPE_BYTE) {
71 *val = autoconfig_read_memory_z3_8(cfg, addr);
75 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
80 if (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
81 printf("[Amiga-Custom] %s read from PISCSI base @$%.8X.\n", op_type_names[type], addr);
82 stop_cpu_emulation(1);
83 *val = handle_piscsi_read(addr, type);
90 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
91 if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
92 if (type == OP_TYPE_BYTE) {
93 if (ac_z2_pic_count == 0) {
98 //printf("Write to Z2 autoconf area.\n");
99 autoconfig_write_memory_8(cfg, addr, val);
104 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
105 if (type == OP_TYPE_BYTE) {
106 if (ac_z3_pic_count == 0) {
111 //printf("Write to autoconf area.\n");
112 autoconfig_write_memory_z3_8(cfg, addr, val);
115 else if (type == OP_TYPE_WORD) {
116 autoconfig_write_memory_z3_16(cfg, addr, val);
120 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
125 if (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
126 printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
127 handle_piscsi_write(addr, val, type);
134 void adjust_ranges_amiga(struct emulator_config *cfg) {
135 cfg->mapped_high = 0;
137 cfg->custom_high = 0;
140 // Set up the min/max ranges for mapped reads/writes
141 if (gayle_emulation_enabled) {
142 cfg->mapped_low = GAYLEBASE;
143 cfg->mapped_high = GAYLEBASE + GAYLESIZE;
145 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
146 if (cfg->map_type[i] != MAPTYPE_NONE) {
147 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
148 cfg->mapped_low = cfg->map_offset[i];
149 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
150 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
154 if (ac_z2_pic_count && !ac_z2_done) {
155 if (cfg->custom_low == 0)
156 cfg->custom_low = AC_Z2_BASE;
158 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
159 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
161 if (ac_z3_pic_count && !ac_z3_done) {
162 if (cfg->custom_low == 0)
163 cfg->custom_low = AC_Z3_BASE;
165 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
166 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
169 if (cfg->custom_low == 0)
170 cfg->custom_low = PIGFX_RTG_BASE;
172 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
173 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
175 if (piscsi_enabled) {
176 if (cfg->custom_low == 0)
177 cfg->custom_low = PISCSI_OFFSET;
179 cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
180 cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
181 if (piscsi_base != 0) {
182 cfg->custom_low = min(cfg->custom_low, piscsi_base);
186 if (cfg->custom_low == 0)
187 cfg->custom_low = PINET_OFFSET;
189 cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
190 cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
193 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
194 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
197 int setup_platform_amiga(struct emulator_config *cfg) {
198 printf("Performing setup for Amiga platform.\n");
200 if (strlen(cfg->platform->subsys)) {
201 printf("Subsystem is [%s]\n", cfg->platform->subsys);
202 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
203 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
206 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
207 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
210 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
211 printf("Configuring platform for CDTV emulation.\n");
213 rtc_type = RTC_TYPE_MSM;
217 printf("No sub system specified.\n");
219 // Look for Z2 autoconf Fast RAM by id
220 int index = get_named_mapped_item(cfg, z2_autoconf_id);
223 // "Zap" config items as they are processed.
224 cfg->map_id[index][0] = '^';
226 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
227 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
228 resize_data = 8 * SIZE_MEGA;
230 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
231 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
232 if (cfg->map_size[index] > 8 * SIZE_MEGA)
233 resize_data = 8 * SIZE_MEGA;
234 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
235 resize_data = 4 * SIZE_MEGA;
237 resize_data = 2 * SIZE_MEGA;
238 printf("%dMB.\n", resize_data / SIZE_MEGA);
241 free(cfg->map_data[index]);
242 cfg->map_size[index] = resize_data;
243 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
245 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
246 ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
247 ac_z2_index[ac_z2_pic_count] = index;
251 printf("No Z2 Fast RAM configured.\n");
253 index = get_named_mapped_item(cfg, z2_autoconf_id);
257 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
258 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
259 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
260 cfg->map_id[i][0] = z2_autoconf_id[0];
264 index = get_named_mapped_item(cfg, z3_autoconf_id);
267 cfg->map_id[index][0] = '^';
268 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
269 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
270 ac_z3_index[ac_z3_pic_count] = index;
274 printf("No Z3 Fast RAM configured.\n");
275 index = get_named_mapped_item(cfg, z3_autoconf_id);
278 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
279 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
280 cfg->map_id[i][0] = z3_autoconf_id[0];
284 index = get_named_mapped_item(cfg, "cpu_slot_ram");
286 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
289 adjust_ranges_amiga(cfg);
292 FILE *in = fopen("data/cdtv.sram", "rb");
294 printf("Loaded CDTV SRAM.\n");
295 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
303 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
307 if (strcmp(var, "enable_rtc_emulation") == 0) {
308 int8_t rtc_enabled = 0;
309 if (!val || strlen(val) == 0)
312 rtc_enabled = get_int(val);
314 if (rtc_enabled != -1) {
315 configure_rtc_emulation_amiga(rtc_enabled);
318 if (strcmp(var, "hdd0") == 0) {
319 if (val && strlen(val) != 0)
320 set_hard_drive_image_file_amiga(0, val);
322 if (strcmp(var, "hdd1") == 0) {
323 if (val && strlen(val) != 0)
324 set_hard_drive_image_file_amiga(1, val);
326 if (strcmp(var, "cdtv") == 0) {
327 printf("[AMIGA] CDTV mode enabled.\n");
330 if (strcmp(var, "rtg") == 0) {
331 if (init_rtg_data()) {
332 printf("[AMIGA] RTG Enabled.\n");
334 adjust_ranges_amiga(cfg);
337 printf("[AMIGA} Failed to enable RTG.\n");
341 if (strcmp(var, "piscsi") == 0) {
342 printf("[AMIGA] PISCSI Interface Enabled.\n");
345 ac_z2_type[ac_z2_pic_count] = ACTYPE_PSICSI;
347 adjust_ranges_amiga(cfg);
349 if (piscsi_enabled) {
350 if (strcmp(var, "piscsi0") == 0) {
351 piscsi_map_drive(val, 0);
353 if (strcmp(var, "piscsi1") == 0) {
354 piscsi_map_drive(val, 1);
356 if (strcmp(var, "piscsi2") == 0) {
357 piscsi_map_drive(val, 2);
359 if (strcmp(var, "piscsi3") == 0) {
360 piscsi_map_drive(val, 3);
362 if (strcmp(var, "piscsi4") == 0) {
363 piscsi_map_drive(val, 4);
365 if (strcmp(var, "piscsi5") == 0) {
366 piscsi_map_drive(val, 5);
368 if (strcmp(var, "piscsi6") == 0) {
369 piscsi_map_drive(val, 6);
374 if (strcmp(var, "pi-net") == 0) {
375 printf("[AMIGA] PI-NET Interface Enabled.\n");
378 adjust_ranges_amiga(cfg);
382 if (strcmp(var, "rtc_type") == 0) {
383 if (val && strlen(val) != 0) {
384 if (strcmp(val, "msm") == 0) {
385 printf("[AMIGA] RTC type set to MSM.\n");
386 rtc_type = RTC_TYPE_MSM;
389 printf("[AMIGA] RTC type set to Ricoh.\n");
390 rtc_type = RTC_TYPE_RICOH;
396 void handle_reset_amiga(struct emulator_config *cfg) {
399 ac_z2_current_pic = 0;
400 ac_z3_current_pic = 0;
401 piscsi_diag_read = 0;
403 adjust_ranges_amiga(cfg);
406 void shutdown_platform_amiga(struct emulator_config *cfg) {
409 FILE *out = fopen("data/cdtv.sram", "wb+");
411 printf("Saving CDTV SRAM.\n");
412 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
416 printf("Failed to write CDTV SRAM to disk.\n");
421 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
422 cfg->register_read = handle_register_read_amiga;
423 cfg->register_write = handle_register_write_amiga;
424 cfg->custom_read = custom_read_amiga;
425 cfg->custom_write = custom_write_amiga;
426 cfg->platform_initial_setup = setup_platform_amiga;
427 cfg->handle_reset = handle_reset_amiga;
428 cfg->shutdown = shutdown_platform_amiga;
430 cfg->setvar = setvar_amiga;
431 cfg->id = PLATFORM_AMIGA;
434 cfg->subsys = malloc(strlen(subsys) + 1);
435 strcpy(cfg->subsys, subsys);
436 for (int i = 0; i < strlen(cfg->subsys); i++) {
437 cfg->subsys[i] = tolower(cfg->subsys[i]);