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;
49 extern void stop_cpu_emulation(uint8_t disasm_cur);
51 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
52 if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
53 if (ac_z2_pic_count == 0) {
58 if (type == OP_TYPE_BYTE) {
59 *val = autoconfig_read_memory_8(cfg, addr);
63 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
64 if (ac_z3_pic_count == 0) {
69 if (type == OP_TYPE_BYTE) {
70 *val = autoconfig_read_memory_z3_8(cfg, addr);
74 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
79 if (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
80 //printf("[Amiga-Custom] %s read from PISCSI base @$%.8X.\n", op_type_names[type], addr);
81 //stop_cpu_emulation(1);
82 *val = handle_piscsi_read(addr, type);
89 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
90 if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
91 if (type == OP_TYPE_BYTE) {
92 if (ac_z2_pic_count == 0) {
97 //printf("Write to Z2 autoconf area.\n");
98 autoconfig_write_memory_8(cfg, addr, val);
103 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
104 if (type == OP_TYPE_BYTE) {
105 if (ac_z3_pic_count == 0) {
110 //printf("Write to autoconf area.\n");
111 autoconfig_write_memory_z3_8(cfg, addr, val);
114 else if (type == OP_TYPE_WORD) {
115 autoconfig_write_memory_z3_16(cfg, addr, val);
119 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
124 if (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
125 //printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
126 handle_piscsi_write(addr, val, type);
133 void adjust_ranges_amiga(struct emulator_config *cfg) {
134 cfg->mapped_high = 0;
136 cfg->custom_high = 0;
139 // Set up the min/max ranges for mapped reads/writes
140 if (gayle_emulation_enabled) {
141 cfg->mapped_low = GAYLEBASE;
142 cfg->mapped_high = GAYLEBASE + GAYLESIZE;
144 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
145 if (cfg->map_type[i] != MAPTYPE_NONE) {
146 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
147 cfg->mapped_low = cfg->map_offset[i];
148 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
149 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
153 if (ac_z2_pic_count && !ac_z2_done) {
154 if (cfg->custom_low == 0)
155 cfg->custom_low = AC_Z2_BASE;
157 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
158 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
160 if (ac_z3_pic_count && !ac_z3_done) {
161 if (cfg->custom_low == 0)
162 cfg->custom_low = AC_Z3_BASE;
164 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
165 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
168 if (cfg->custom_low == 0)
169 cfg->custom_low = PIGFX_RTG_BASE;
171 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
172 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
174 if (piscsi_enabled) {
175 if (cfg->custom_low == 0)
176 cfg->custom_low = PISCSI_OFFSET;
178 cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
179 cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
180 if (piscsi_base != 0) {
181 cfg->custom_low = min(cfg->custom_low, piscsi_base);
185 if (cfg->custom_low == 0)
186 cfg->custom_low = PINET_OFFSET;
188 cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
189 cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
192 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
193 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
196 int setup_platform_amiga(struct emulator_config *cfg) {
197 printf("Performing setup for Amiga platform.\n");
199 if (strlen(cfg->platform->subsys)) {
200 printf("Subsystem is [%s]\n", cfg->platform->subsys);
201 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
202 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
205 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
206 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
209 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
210 printf("Configuring platform for CDTV emulation.\n");
212 rtc_type = RTC_TYPE_MSM;
216 printf("No sub system specified.\n");
218 // Look for Z2 autoconf Fast RAM by id
219 int index = get_named_mapped_item(cfg, z2_autoconf_id);
222 // "Zap" config items as they are processed.
223 cfg->map_id[index][0] = '^';
225 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
226 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
227 resize_data = 8 * SIZE_MEGA;
229 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
230 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
231 if (cfg->map_size[index] > 8 * SIZE_MEGA)
232 resize_data = 8 * SIZE_MEGA;
233 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
234 resize_data = 4 * SIZE_MEGA;
236 resize_data = 2 * SIZE_MEGA;
237 printf("%dMB.\n", resize_data / SIZE_MEGA);
240 free(cfg->map_data[index]);
241 cfg->map_size[index] = resize_data;
242 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
244 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
245 ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
246 ac_z2_index[ac_z2_pic_count] = index;
250 printf("No Z2 Fast RAM configured.\n");
252 index = get_named_mapped_item(cfg, z2_autoconf_id);
256 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
257 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
258 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
259 cfg->map_id[i][0] = z2_autoconf_id[0];
263 index = get_named_mapped_item(cfg, z3_autoconf_id);
266 cfg->map_id[index][0] = '^';
267 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
268 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
269 ac_z3_index[ac_z3_pic_count] = index;
273 printf("No Z3 Fast RAM configured.\n");
274 index = get_named_mapped_item(cfg, z3_autoconf_id);
277 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
278 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
279 cfg->map_id[i][0] = z3_autoconf_id[0];
283 index = get_named_mapped_item(cfg, "cpu_slot_ram");
285 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
288 adjust_ranges_amiga(cfg);
291 FILE *in = fopen("data/cdtv.sram", "rb");
293 printf("Loaded CDTV SRAM.\n");
294 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
302 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
306 if (strcmp(var, "enable_rtc_emulation") == 0) {
307 int8_t rtc_enabled = 0;
308 if (!val || strlen(val) == 0)
311 rtc_enabled = get_int(val);
313 if (rtc_enabled != -1) {
314 configure_rtc_emulation_amiga(rtc_enabled);
317 if (strcmp(var, "hdd0") == 0) {
318 if (val && strlen(val) != 0)
319 set_hard_drive_image_file_amiga(0, val);
321 if (strcmp(var, "hdd1") == 0) {
322 if (val && strlen(val) != 0)
323 set_hard_drive_image_file_amiga(1, val);
325 if (strcmp(var, "cdtv") == 0) {
326 printf("[AMIGA] CDTV mode enabled.\n");
329 if (strcmp(var, "rtg") == 0) {
330 if (init_rtg_data()) {
331 printf("[AMIGA] RTG Enabled.\n");
333 adjust_ranges_amiga(cfg);
336 printf("[AMIGA} Failed to enable RTG.\n");
340 if (strcmp(var, "piscsi") == 0) {
341 printf("[AMIGA] PISCSI Interface Enabled.\n");
344 //ac_z2_type[ac_z2_pic_count] = ACTYPE_PISCSI;
346 adjust_ranges_amiga(cfg);
348 if (piscsi_enabled) {
349 if (strcmp(var, "piscsi0") == 0) {
350 piscsi_map_drive(val, 0);
352 if (strcmp(var, "piscsi1") == 0) {
353 piscsi_map_drive(val, 1);
355 if (strcmp(var, "piscsi2") == 0) {
356 piscsi_map_drive(val, 2);
358 if (strcmp(var, "piscsi3") == 0) {
359 piscsi_map_drive(val, 3);
361 if (strcmp(var, "piscsi4") == 0) {
362 piscsi_map_drive(val, 4);
364 if (strcmp(var, "piscsi5") == 0) {
365 piscsi_map_drive(val, 5);
367 if (strcmp(var, "piscsi6") == 0) {
368 piscsi_map_drive(val, 6);
373 if (strcmp(var, "pi-net") == 0) {
374 printf("[AMIGA] PI-NET Interface Enabled.\n");
377 adjust_ranges_amiga(cfg);
381 if (strcmp(var, "rtc_type") == 0) {
382 if (val && strlen(val) != 0) {
383 if (strcmp(val, "msm") == 0) {
384 printf("[AMIGA] RTC type set to MSM.\n");
385 rtc_type = RTC_TYPE_MSM;
388 printf("[AMIGA] RTC type set to Ricoh.\n");
389 rtc_type = RTC_TYPE_RICOH;
395 void handle_reset_amiga(struct emulator_config *cfg) {
398 ac_z2_current_pic = 0;
399 ac_z3_current_pic = 0;
401 adjust_ranges_amiga(cfg);
404 void shutdown_platform_amiga(struct emulator_config *cfg) {
407 FILE *out = fopen("data/cdtv.sram", "wb+");
409 printf("Saving CDTV SRAM.\n");
410 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
414 printf("Failed to write CDTV SRAM to disk.\n");
419 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
420 cfg->register_read = handle_register_read_amiga;
421 cfg->register_write = handle_register_write_amiga;
422 cfg->custom_read = custom_read_amiga;
423 cfg->custom_write = custom_write_amiga;
424 cfg->platform_initial_setup = setup_platform_amiga;
425 cfg->handle_reset = handle_reset_amiga;
426 cfg->shutdown = shutdown_platform_amiga;
428 cfg->setvar = setvar_amiga;
429 cfg->id = PLATFORM_AMIGA;
432 cfg->subsys = malloc(strlen(subsys) + 1);
433 strcpy(cfg->subsys, subsys);
434 for (int i = 0; i < strlen(cfg->subsys); i++) {
435 cfg->subsys[i] = tolower(cfg->subsys[i]);