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 int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val);
17 int handle_register_write_amiga(unsigned int addr, unsigned int value, unsigned char type);
20 extern int ac_z2_current_pic;
21 extern int ac_z2_done;
22 extern int ac_z2_pic_count;
23 extern int ac_z2_type[AC_PIC_LIMIT];
24 extern int ac_z2_index[AC_PIC_LIMIT];
26 extern int ac_z3_current_pic;
27 extern int ac_z3_pic_count;
28 extern int ac_z3_done;
29 extern int ac_z3_type[AC_PIC_LIMIT];
30 extern int ac_z3_index[AC_PIC_LIMIT];
31 extern int gayle_emulation_enabled;
33 char *z2_autoconf_id = "z2_autoconf_fast";
34 char *z2_autoconf_zap_id = "^2_autoconf_fast";
35 char *z3_autoconf_id = "z3_autoconf_fast";
36 char *z3_autoconf_zap_id = "^3_autoconf_fast";
38 extern const char *op_type_names[OP_TYPE_NUM];
39 extern uint8_t cdtv_mode;
40 extern uint8_t rtc_type;
41 extern unsigned char cdtv_sram[32 * SIZE_KILO];
43 #define min(a, b) (a < b) ? a : b
44 #define max(a, b) (a > b) ? a : b
46 static uint8_t rtg_enabled = 0, piscsi_enabled = 0, pinet_enabled = 0;
48 extern uint32_t piscsi_base;
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 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
142 if (cfg->map_type[i] != MAPTYPE_NONE) {
143 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
144 cfg->mapped_low = cfg->map_offset[i];
145 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
146 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
150 if (ac_z2_pic_count && !ac_z2_done) {
151 if (cfg->custom_low == 0)
152 cfg->custom_low = AC_Z2_BASE;
154 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
155 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
157 if (ac_z3_pic_count && !ac_z3_done) {
158 if (cfg->custom_low == 0)
159 cfg->custom_low = AC_Z3_BASE;
161 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
162 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
165 if (cfg->custom_low == 0)
166 cfg->custom_low = PIGFX_RTG_BASE;
168 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
169 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
171 if (piscsi_enabled) {
172 if (cfg->custom_low == 0)
173 cfg->custom_low = PISCSI_OFFSET;
175 cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
176 cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
177 if (piscsi_base != 0) {
178 cfg->custom_low = min(cfg->custom_low, piscsi_base);
182 if (cfg->custom_low == 0)
183 cfg->custom_low = PINET_OFFSET;
185 cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
186 cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
189 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
190 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
193 int setup_platform_amiga(struct emulator_config *cfg) {
194 printf("Performing setup for Amiga platform.\n");
196 if (strlen(cfg->platform->subsys)) {
197 printf("Subsystem is [%s]\n", cfg->platform->subsys);
198 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
199 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
202 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
203 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
206 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
207 printf("Configuring platform for CDTV emulation.\n");
209 rtc_type = RTC_TYPE_MSM;
213 printf("No sub system specified.\n");
215 // Look for Z2 autoconf Fast RAM by id
216 int index = get_named_mapped_item(cfg, z2_autoconf_id);
219 // "Zap" config items as they are processed.
220 cfg->map_id[index][0] = '^';
222 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
223 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
224 resize_data = 8 * SIZE_MEGA;
226 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
227 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
228 if (cfg->map_size[index] > 8 * SIZE_MEGA)
229 resize_data = 8 * SIZE_MEGA;
230 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
231 resize_data = 4 * SIZE_MEGA;
233 resize_data = 2 * SIZE_MEGA;
234 printf("%dMB.\n", resize_data / SIZE_MEGA);
237 free(cfg->map_data[index]);
238 cfg->map_size[index] = resize_data;
239 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
241 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
242 ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
243 ac_z2_index[ac_z2_pic_count] = index;
247 printf("No Z2 Fast RAM configured.\n");
249 index = get_named_mapped_item(cfg, z2_autoconf_id);
253 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
254 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
255 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
256 cfg->map_id[i][0] = z2_autoconf_id[0];
260 index = get_named_mapped_item(cfg, z3_autoconf_id);
263 cfg->map_id[index][0] = '^';
264 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
265 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
266 ac_z3_index[ac_z3_pic_count] = index;
270 printf("No Z3 Fast RAM configured.\n");
271 index = get_named_mapped_item(cfg, z3_autoconf_id);
274 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
275 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
276 cfg->map_id[i][0] = z3_autoconf_id[0];
280 index = get_named_mapped_item(cfg, "cpu_slot_ram");
282 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
285 adjust_ranges_amiga(cfg);
288 FILE *in = fopen("data/cdtv.sram", "rb");
290 printf("Loaded CDTV SRAM.\n");
291 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
299 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
303 if (strcmp(var, "enable_rtc_emulation") == 0) {
304 int8_t rtc_enabled = 0;
305 if (!val || strlen(val) == 0)
308 rtc_enabled = get_int(val);
310 if (rtc_enabled != -1) {
311 configure_rtc_emulation_amiga(rtc_enabled);
314 if (strcmp(var, "hdd0") == 0) {
315 if (val && strlen(val) != 0)
316 set_hard_drive_image_file_amiga(0, val);
318 if (strcmp(var, "hdd1") == 0) {
319 if (val && strlen(val) != 0)
320 set_hard_drive_image_file_amiga(1, val);
322 if (strcmp(var, "cdtv") == 0) {
323 printf("[AMIGA] CDTV mode enabled.\n");
326 if (strcmp(var, "rtg") == 0) {
327 if (init_rtg_data()) {
328 printf("[AMIGA] RTG Enabled.\n");
330 adjust_ranges_amiga(cfg);
333 printf("[AMIGA} Failed to enable RTG.\n");
337 if (strcmp(var, "piscsi") == 0) {
338 printf("[AMIGA] PISCSI Interface Enabled.\n");
341 ac_z2_type[ac_z2_pic_count] = ACTYPE_PISCSI;
343 adjust_ranges_amiga(cfg);
345 if (piscsi_enabled) {
346 if (strcmp(var, "piscsi0") == 0) {
347 piscsi_map_drive(val, 0);
349 if (strcmp(var, "piscsi1") == 0) {
350 piscsi_map_drive(val, 1);
352 if (strcmp(var, "piscsi2") == 0) {
353 piscsi_map_drive(val, 2);
355 if (strcmp(var, "piscsi3") == 0) {
356 piscsi_map_drive(val, 3);
358 if (strcmp(var, "piscsi4") == 0) {
359 piscsi_map_drive(val, 4);
361 if (strcmp(var, "piscsi5") == 0) {
362 piscsi_map_drive(val, 5);
364 if (strcmp(var, "piscsi6") == 0) {
365 piscsi_map_drive(val, 6);
370 if (strcmp(var, "pi-net") == 0) {
371 printf("[AMIGA] PI-NET Interface Enabled.\n");
374 adjust_ranges_amiga(cfg);
378 if (strcmp(var, "rtc_type") == 0) {
379 if (val && strlen(val) != 0) {
380 if (strcmp(val, "msm") == 0) {
381 printf("[AMIGA] RTC type set to MSM.\n");
382 rtc_type = RTC_TYPE_MSM;
385 printf("[AMIGA] RTC type set to Ricoh.\n");
386 rtc_type = RTC_TYPE_RICOH;
392 void handle_reset_amiga(struct emulator_config *cfg) {
395 ac_z2_current_pic = 0;
396 ac_z3_current_pic = 0;
399 piscsi_refresh_drives();
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]);