]> git.sesse.net Git - pistorm/blob - platforms/amiga/amiga-platform.c
8a36cc64cacefe02c339208c63d374ed0eaf84ac
[pistorm] / platforms / amiga / amiga-platform.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include "../platforms.h"
6 #include "amiga-autoconf.h"
7 #include "amiga-registers.h"
8 #include "../shared/rtc.h"
9 #include "rtg/rtg.h"
10
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);
13 int init_rtg_data();
14
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];
19
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;
25
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";
30
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];
35
36 #define min(a, b) (a < b) ? a : b
37 #define max(a, b) (a > b) ? a : b
38
39 static uint8_t rtg_enabled;
40
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) {
44             ac_z2_done = 1;
45             return -1;
46         }
47
48         if (type == OP_TYPE_BYTE) {
49             *val = autoconfig_read_memory_8(cfg, addr);
50             return 1;
51         }
52     }
53     if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
54         if (ac_z3_pic_count == 0) {
55             ac_z3_done = 1;
56             return -1;
57         }
58
59         if (type == OP_TYPE_BYTE) {
60             *val = autoconfig_read_memory_z3_8(cfg, addr);
61             return 1;
62         }
63         else {
64             printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
65             //stop_emulation();
66         }
67     }
68
69     return -1;
70 }
71
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) {
76                 ac_z2_done = 1;
77                 return -1;
78             }
79
80             //printf("Write to Z2 autoconf area.\n");
81             autoconfig_write_memory_8(cfg, addr, val);
82             return 1;
83         }
84     }
85
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) {
89                 ac_z3_done = 1;
90                 return -1;
91             }
92
93             //printf("Write to autoconf area.\n");
94             autoconfig_write_memory_z3_8(cfg, addr, val);
95             return 1;
96         }
97         else if (type == OP_TYPE_WORD) {
98             autoconfig_write_memory_z3_16(cfg, addr, val);
99             return 1;
100         }
101         else {
102             printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
103             //stop_emulation();
104         }
105     }
106
107     return -1;
108 }
109
110 void adjust_ranges_amiga(struct emulator_config *cfg) {
111     cfg->mapped_high = 0;
112     cfg->mapped_low = 0;
113     cfg->custom_high = 0;
114     cfg->custom_low = 0;
115
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;
120     }
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];
127         }
128     }
129
130     if (ac_z2_pic_count && !ac_z2_done) {
131         if (cfg->custom_low == 0)
132             cfg->custom_low = AC_Z2_BASE;
133         else
134             cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
135         cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
136     }
137     if (ac_z3_pic_count && !ac_z3_done) {
138         if (cfg->custom_low == 0)
139             cfg->custom_low = AC_Z3_BASE;
140         else
141             cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
142         cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
143     }
144     if (rtg_enabled) {
145         if (cfg->custom_low == 0)
146             cfg->custom_low = PIGFX_RTG_BASE;
147         else
148             cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
149         cfg->custom_high = max(cfg->custom_high, PIGFX_RTG_BASE + PIGFX_RTG_SIZE);
150     }
151
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);
154 }
155
156 int setup_platform_amiga(struct emulator_config *cfg) {
157     printf("Performing setup for Amiga platform.\n");
158
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");
163             adjust_gayle_4000();
164         }
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");
167             adjust_gayle_1200();
168         }
169         else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
170             printf("Configuring platform for CDTV emulation.\n");
171             cdtv_mode = 1;
172             rtc_type = RTC_TYPE_MSM;
173         }
174     }
175     else
176         printf("No sub system specified.\n");
177
178     // Look for Z2 autoconf Fast RAM by id
179     int index = get_named_mapped_item(cfg, z2_autoconf_id);
180     more_z2_fast:;
181     if (index != -1) {
182         // "Zap" config items as they are processed.
183         cfg->map_id[index][0] = '^';
184         int resize_data = 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;
188         }
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;
195             else
196                 resize_data = 2 * SIZE_MEGA;
197             printf("%dMB.\n", resize_data / SIZE_MEGA);
198         }
199         if (resize_data) {
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]);
203         }
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;
207         ac_z2_pic_count++;
208     }
209     else
210         printf("No Z2 Fast RAM configured.\n");
211
212     index = get_named_mapped_item(cfg, z2_autoconf_id);
213     if (index != -1)
214         goto more_z2_fast;
215     
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];
220         }
221     }
222
223     index = get_named_mapped_item(cfg, z3_autoconf_id);
224     more_z3_fast:;
225     if (index != -1) {
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;
230         ac_z3_pic_count++;
231     }
232     else
233         printf("No Z3 Fast RAM configured.\n");
234     index = get_named_mapped_item(cfg, z3_autoconf_id);
235     if (index != -1)
236         goto more_z3_fast;
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];
240         }
241     }
242
243     index = get_named_mapped_item(cfg, "cpu_slot_ram");
244     if (index != -1) {
245         m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
246     }
247
248     adjust_ranges_amiga(cfg);
249
250     if (cdtv_mode) {
251         FILE *in = fopen("data/cdtv.sram", "rb");
252         if (in != NULL) {
253             printf("Loaded CDTV SRAM.\n");
254             fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
255             fclose(in);
256         }
257     }
258     
259     return 0;
260 }
261
262 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
263     if (!var)
264         return;
265
266     if (strcmp(var, "enable_rtc_emulation") == 0) {
267         int8_t rtc_enabled = 0;
268         if (!val || strlen(val) == 0)
269             rtc_enabled = 1;
270         else {
271             rtc_enabled = get_int(val);
272         }
273         if (rtc_enabled != -1) {
274             configure_rtc_emulation_amiga(rtc_enabled);
275         }
276     }
277     if (strcmp(var, "hdd0") == 0) {
278         if (val && strlen(val) != 0)
279             set_hard_drive_image_file_amiga(0, val);
280     }
281     if (strcmp(var, "cdtv") == 0) {
282         printf("[AMIGA] CDTV mode enabled.\n");
283         cdtv_mode = 1;
284     }
285     if (strcmp(var, "rtg") == 0) {
286         if (init_rtg_data()) {
287             printf("[AMIGA] RTG Enabled.\n");
288             rtg_enabled = 1;
289             adjust_ranges_amiga(cfg);
290         }
291         else
292             printf("[AMIGA} Failed to enable RTG.\n");
293     }
294     if (strcmp(var, "rtc_type") == 0) {
295         if (val && strlen(val) != 0) {
296             if (strcmp(val, "msm") == 0) {
297                 printf("[AMIGA] RTC type set to MSM.\n");
298                 rtc_type = RTC_TYPE_MSM;
299             }
300             else {
301                 printf("[AMIGA] RTC type set to Ricoh.\n");
302                 rtc_type = RTC_TYPE_RICOH;
303             }
304         }
305     }
306 }
307
308 void handle_reset_amiga(struct emulator_config *cfg) {
309     ac_z3_done = 0;
310     ac_z2_done = 0;
311
312     adjust_ranges_amiga(cfg);
313 }
314
315 void shutdown_platform_amiga(struct emulator_config *cfg) {
316     if (cfg) {}
317     if (cdtv_mode) {
318         FILE *out = fopen("data/cdtv.sram", "wb+");
319         if (out != NULL) {
320             printf("Saving CDTV SRAM.\n");
321             fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
322             fclose(out);
323         }
324         else {
325             printf("Failed to write CDTV SRAM to disk.\n");
326         }
327     }
328 }
329
330 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
331     cfg->register_read = handle_register_read_amiga;
332     cfg->register_write = handle_register_write_amiga;
333     cfg->custom_read = custom_read_amiga;
334     cfg->custom_write = custom_write_amiga;
335     cfg->platform_initial_setup = setup_platform_amiga;
336     cfg->handle_reset = handle_reset_amiga;
337     cfg->shutdown = shutdown_platform_amiga;
338
339     cfg->setvar = setvar_amiga;
340     cfg->id = PLATFORM_AMIGA;
341
342     if (subsys) {
343         cfg->subsys = malloc(strlen(subsys) + 1);
344         strcpy(cfg->subsys, subsys);
345         for (int i = 0; i < strlen(cfg->subsys); i++) {
346             cfg->subsys[i] = tolower(cfg->subsys[i]);
347         }
348     }
349 }