5 #include "../platforms.h"
6 #include "amiga-autoconf.h"
7 #include "amiga-registers.h"
8 #include "../shared/rtc.h"
11 int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val);
12 int handle_register_write_amiga(unsigned int addr, unsigned int value, unsigned char type);
15 extern int ac_z2_done;
16 extern int ac_z2_pic_count;
17 extern int ac_z2_type[AC_PIC_LIMIT];
18 extern int ac_z2_index[AC_PIC_LIMIT];
20 extern int ac_z3_pic_count;
21 extern int ac_z3_done;
22 extern int ac_z3_type[AC_PIC_LIMIT];
23 extern int ac_z3_index[AC_PIC_LIMIT];
24 extern int gayle_emulation_enabled;
26 char *z2_autoconf_id = "z2_autoconf_fast";
27 char *z2_autoconf_zap_id = "^2_autoconf_fast";
28 char *z3_autoconf_id = "z3_autoconf_fast";
29 char *z3_autoconf_zap_id = "^3_autoconf_fast";
31 extern const char *op_type_names[OP_TYPE_NUM];
32 extern uint8_t cdtv_mode;
33 extern uint8_t rtc_type;
34 extern unsigned char cdtv_sram[32 * SIZE_KILO];
36 #define min(a, b) (a < b) ? a : b
37 #define max(a, b) (a > b) ? a : b
39 static uint8_t rtg_enabled;
41 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
42 if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
43 if (ac_z2_pic_count == 0) {
48 if (type == OP_TYPE_BYTE) {
49 *val = autoconfig_read_memory_8(cfg, addr);
53 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
54 if (ac_z3_pic_count == 0) {
59 if (type == OP_TYPE_BYTE) {
60 *val = autoconfig_read_memory_z3_8(cfg, addr);
64 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
72 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
73 if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
74 if (type == OP_TYPE_BYTE) {
75 if (ac_z2_pic_count == 0) {
80 //printf("Write to Z2 autoconf area.\n");
81 autoconfig_write_memory_8(cfg, addr, val);
86 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
87 if (type == OP_TYPE_BYTE) {
88 if (ac_z3_pic_count == 0) {
93 //printf("Write to autoconf area.\n");
94 autoconfig_write_memory_z3_8(cfg, addr, val);
97 else if (type == OP_TYPE_WORD) {
98 autoconfig_write_memory_z3_16(cfg, addr, val);
102 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
110 void adjust_ranges_amiga(struct emulator_config *cfg) {
111 cfg->mapped_high = 0;
113 cfg->custom_high = 0;
116 // Set up the min/max ranges for mapped reads/writes
117 if (gayle_emulation_enabled) {
118 cfg->mapped_low = GAYLEBASE;
119 cfg->mapped_high = GAYLEBASE + GAYLESIZE;
121 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
122 if (cfg->map_type[i] != MAPTYPE_NONE) {
123 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
124 cfg->mapped_low = cfg->map_offset[i];
125 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
126 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
130 if (ac_z2_pic_count && !ac_z2_done) {
131 if (cfg->custom_low == 0)
132 cfg->custom_low = AC_Z2_BASE;
134 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
135 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
137 if (ac_z3_pic_count && !ac_z3_done) {
138 if (cfg->custom_low == 0)
139 cfg->custom_low = AC_Z3_BASE;
141 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
142 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
145 if (cfg->custom_low == 0)
146 cfg->custom_low = PIGFX_RTG_BASE;
148 cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
149 cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
152 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
153 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
156 int setup_platform_amiga(struct emulator_config *cfg) {
157 printf("Performing setup for Amiga platform.\n");
159 if (strlen(cfg->platform->subsys)) {
160 printf("Subsystem is [%s]\n", cfg->platform->subsys);
161 if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
162 printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
165 else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
166 printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
169 else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
170 printf("Configuring platform for CDTV emulation.\n");
172 rtc_type = RTC_TYPE_MSM;
176 printf("No sub system specified.\n");
178 // Look for Z2 autoconf Fast RAM by id
179 int index = get_named_mapped_item(cfg, z2_autoconf_id);
182 // "Zap" config items as they are processed.
183 cfg->map_id[index][0] = '^';
185 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
186 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
187 resize_data = 8 * SIZE_MEGA;
189 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
190 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
191 if (cfg->map_size[index] > 8 * SIZE_MEGA)
192 resize_data = 8 * SIZE_MEGA;
193 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
194 resize_data = 4 * SIZE_MEGA;
196 resize_data = 2 * SIZE_MEGA;
197 printf("%dMB.\n", resize_data / SIZE_MEGA);
200 free(cfg->map_data[index]);
201 cfg->map_size[index] = resize_data;
202 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
204 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
205 ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
206 ac_z2_index[ac_z2_pic_count] = index;
210 printf("No Z2 Fast RAM configured.\n");
212 index = get_named_mapped_item(cfg, z2_autoconf_id);
216 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
217 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
218 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
219 cfg->map_id[i][0] = z2_autoconf_id[0];
223 index = get_named_mapped_item(cfg, z3_autoconf_id);
226 cfg->map_id[index][0] = '^';
227 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
228 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
229 ac_z3_index[ac_z3_pic_count] = index;
233 printf("No Z3 Fast RAM configured.\n");
234 index = get_named_mapped_item(cfg, z3_autoconf_id);
237 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
238 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
239 cfg->map_id[i][0] = z3_autoconf_id[0];
243 index = get_named_mapped_item(cfg, "cpu_slot_ram");
245 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
248 adjust_ranges_amiga(cfg);
251 FILE *in = fopen("data/cdtv.sram", "rb");
253 printf("Loaded CDTV SRAM.\n");
254 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
262 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
266 if (strcmp(var, "enable_rtc_emulation") == 0) {
267 int8_t rtc_enabled = 0;
268 if (!val || strlen(val) == 0)
271 rtc_enabled = get_int(val);
273 if (rtc_enabled != -1) {
274 configure_rtc_emulation_amiga(rtc_enabled);
277 if (strcmp(var, "hdd0") == 0) {
278 if (val && strlen(val) != 0)
279 set_hard_drive_image_file_amiga(0, val);
281 if (strcmp(var, "hdd1") == 0) {
282 if (val && strlen(val) != 0)
283 set_hard_drive_image_file_amiga(1, val);
285 if (strcmp(var, "cdtv") == 0) {
286 printf("[AMIGA] CDTV mode enabled.\n");
289 if (strcmp(var, "rtg") == 0) {
290 if (init_rtg_data()) {
291 printf("[AMIGA] RTG Enabled.\n");
293 adjust_ranges_amiga(cfg);
296 printf("[AMIGA} Failed to enable RTG.\n");
298 if (strcmp(var, "rtc_type") == 0) {
299 if (val && strlen(val) != 0) {
300 if (strcmp(val, "msm") == 0) {
301 printf("[AMIGA] RTC type set to MSM.\n");
302 rtc_type = RTC_TYPE_MSM;
305 printf("[AMIGA] RTC type set to Ricoh.\n");
306 rtc_type = RTC_TYPE_RICOH;
312 void handle_reset_amiga(struct emulator_config *cfg) {
316 adjust_ranges_amiga(cfg);
319 void shutdown_platform_amiga(struct emulator_config *cfg) {
322 FILE *out = fopen("data/cdtv.sram", "wb+");
324 printf("Saving CDTV SRAM.\n");
325 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
329 printf("Failed to write CDTV SRAM to disk.\n");
334 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
335 cfg->register_read = handle_register_read_amiga;
336 cfg->register_write = handle_register_write_amiga;
337 cfg->custom_read = custom_read_amiga;
338 cfg->custom_write = custom_write_amiga;
339 cfg->platform_initial_setup = setup_platform_amiga;
340 cfg->handle_reset = handle_reset_amiga;
341 cfg->shutdown = shutdown_platform_amiga;
343 cfg->setvar = setvar_amiga;
344 cfg->id = PLATFORM_AMIGA;
347 cfg->subsys = malloc(strlen(subsys) + 1);
348 strcpy(cfg->subsys, subsys);
349 for (int i = 0; i < strlen(cfg->subsys); i++) {
350 cfg->subsys[i] = tolower(cfg->subsys[i]);