]> git.sesse.net Git - pistorm/blob - platforms/amiga/amiga-platform.c
5eac5d7cb3335ab852a0b97cf1d719b35d03319f
[pistorm] / platforms / amiga / amiga-platform.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "../platforms.h"
5 #include "amiga-autoconf.h"
6 #include "amiga-registers.h"
7 #include "../shared/rtc.h"
8
9 int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val);
10 int handle_register_write_amiga(unsigned int addr, unsigned int value, unsigned char type);
11
12 extern int ac_z2_done;
13 extern int ac_z2_pic_count;
14 extern int ac_z2_type[AC_PIC_LIMIT];
15 extern int ac_z2_index[AC_PIC_LIMIT];
16
17 extern int ac_z3_pic_count;
18 extern int ac_z3_done;
19 extern int ac_z3_type[AC_PIC_LIMIT];
20 extern int ac_z3_index[AC_PIC_LIMIT];
21 extern int gayle_emulation_enabled;
22
23 char *z2_autoconf_id = "z2_autoconf_fast";
24 char *z2_autoconf_zap_id = "^2_autoconf_fast";
25 char *z3_autoconf_id = "z3_autoconf_fast";
26 char *z3_autoconf_zap_id = "^3_autoconf_fast";
27
28 extern const char *op_type_names[OP_TYPE_NUM];
29 extern uint8_t cdtv_mode;
30 extern uint8_t rtc_type;
31 extern unsigned char cdtv_sram[32 * SIZE_KILO];
32
33 #define min(a, b) (a < b) ? a : b
34 #define max(a, b) (a > b) ? a : b
35
36 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
37     if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
38         if (ac_z2_pic_count == 0) {
39             ac_z2_done = 1;
40             return -1;
41         }
42
43         if (type == OP_TYPE_BYTE) {
44             *val = autoconfig_read_memory_8(cfg, addr);
45             return 1;
46         }
47     }
48     if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
49         if (ac_z3_pic_count == 0) {
50             ac_z3_done = 1;
51             return -1;
52         }
53
54         if (type == OP_TYPE_BYTE) {
55             *val = autoconfig_read_memory_z3_8(cfg, addr);
56             return 1;
57         }
58         else {
59             printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
60             //stop_emulation();
61         }
62     }
63
64     return -1;
65 }
66
67 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
68     if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
69         if (type == OP_TYPE_BYTE) {
70             if (ac_z2_pic_count == 0) {
71                 ac_z2_done = 1;
72                 return -1;
73             }
74
75             //printf("Write to Z2 autoconf area.\n");
76             autoconfig_write_memory_8(cfg, addr, val);
77             return 1;
78         }
79     }
80
81     if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
82         if (type == OP_TYPE_BYTE) {
83             if (ac_z3_pic_count == 0) {
84                 ac_z3_done = 1;
85                 return -1;
86             }
87
88             //printf("Write to autoconf area.\n");
89             autoconfig_write_memory_z3_8(cfg, addr, val);
90             return 1;
91         }
92         else if (type == OP_TYPE_WORD) {
93             autoconfig_write_memory_z3_16(cfg, addr, val);
94             return 1;
95         }
96         else {
97             printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
98             //stop_emulation();
99         }
100     }
101
102     return -1;
103 }
104
105 void adjust_ranges_amiga(struct emulator_config *cfg) {
106     cfg->mapped_high = 0;
107     cfg->mapped_low = 0;
108     cfg->custom_high = 0;
109     cfg->custom_low = 0;
110
111     // Set up the min/max ranges for mapped reads/writes
112     if (gayle_emulation_enabled) {
113         cfg->mapped_low = GAYLEBASE;
114         cfg->mapped_high = GAYLEBASE + GAYLESIZE;
115     }
116     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
117         if (cfg->map_type[i] != MAPTYPE_NONE) {
118             if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
119                 cfg->mapped_low = cfg->map_offset[i];
120             if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
121                 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
122         }
123     }
124
125     if (ac_z2_pic_count && !ac_z2_done) {
126         if (cfg->custom_low == 0)
127             cfg->custom_low = AC_Z2_BASE;
128         else
129             cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
130         cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
131     }
132     if (ac_z3_pic_count && !ac_z3_done) {
133         if (cfg->custom_low == 0)
134             cfg->custom_low = AC_Z3_BASE;
135         else
136             cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
137         cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
138     }
139
140     printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
141     printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
142 }
143
144 int setup_platform_amiga(struct emulator_config *cfg) {
145     printf("Performing setup for Amiga platform.\n");
146     // Look for Z2 autoconf Fast RAM by id
147     int index = get_named_mapped_item(cfg, z2_autoconf_id);
148     more_z2_fast:;
149     if (index != -1) {
150         // "Zap" config items as they are processed.
151         cfg->map_id[index][0] = '^';
152         int resize_data = 0;
153         if (cfg->map_size[index] > 8 * SIZE_MEGA) {
154             printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
155             resize_data = 8 * SIZE_MEGA;
156         }
157         else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
158             printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
159             if (cfg->map_size[index] > 8 * SIZE_MEGA)
160                 resize_data = 8 * SIZE_MEGA;
161             else if (cfg->map_size[index] > 4 * SIZE_MEGA)
162                 resize_data = 4 * SIZE_MEGA;
163             else
164                 resize_data = 2 * SIZE_MEGA;
165             printf("%dMB.\n", resize_data / SIZE_MEGA);
166         }
167         if (resize_data) {
168             free(cfg->map_data[index]);
169             cfg->map_size[index] = resize_data;
170             cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
171         }
172         printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
173         ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
174         ac_z2_index[ac_z2_pic_count] = index;
175         ac_z2_pic_count++;
176     }
177     else
178         printf("No Z2 Fast RAM configured.\n");
179
180     index = get_named_mapped_item(cfg, z2_autoconf_id);
181     if (index != -1)
182         goto more_z2_fast;
183     
184     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
185         // Restore any "zapped" autoconf items so they can be reinitialized if needed.
186         if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
187             cfg->map_id[i][0] = z2_autoconf_id[0];
188         }
189     }
190
191     index = get_named_mapped_item(cfg, z3_autoconf_id);
192     more_z3_fast:;
193     if (index != -1) {
194         cfg->map_id[index][0] = '^';
195         printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
196         ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
197         ac_z3_index[ac_z3_pic_count] = index;
198         ac_z3_pic_count++;
199     }
200     else
201         printf("No Z3 Fast RAM configured.\n");
202     index = get_named_mapped_item(cfg, z3_autoconf_id);
203     if (index != -1)
204         goto more_z3_fast;
205     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
206         if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
207             cfg->map_id[i][0] = z3_autoconf_id[0];
208         }
209     }
210
211     index = get_named_mapped_item(cfg, "cpu_slot_ram");
212     if (index != -1) {
213         m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
214     }
215
216     adjust_ranges_amiga(cfg);
217
218     if (cdtv_mode) {
219         FILE *in = fopen("data/cdtv.sram", "rb");
220         if (in != NULL) {
221             printf("Loaded CDTV SRAM.\n");
222             fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
223             fclose(in);
224         }
225     }
226     
227     return 0;
228 }
229
230 void setvar_amiga(char *var, char *val) {
231     if (!var)
232         return;
233
234     if (strcmp(var, "enable_rtc_emulation") == 0) {
235         int8_t rtc_enabled = 0;
236         if (!val || strlen(val) == 0)
237             rtc_enabled = 1;
238         else {
239             rtc_enabled = get_int(val);
240         }
241         if (rtc_enabled != -1) {
242             configure_rtc_emulation_amiga(rtc_enabled);
243         }
244     }
245     if (strcmp(var, "hdd0") == 0) {
246         if (val && strlen(val) != 0)
247             set_hard_drive_image_file_amiga(0, val);
248     }
249     if (strcmp(var, "cdtv") == 0) {
250         printf("[AMIGA] CDTV mode enabled.\n");
251         cdtv_mode = 1;
252     }
253     if (strcmp(var, "rtc_type") == 0) {
254         if (val && strlen(val) != 0) {
255             if (strcmp(val, "msm") == 0) {
256                 printf("[AMIGA] RTC type set to MSM.\n");
257                 rtc_type = RTC_TYPE_MSM;
258             }
259             else {
260                 printf("[AMIGA] RTC type set to Ricoh.\n");
261                 rtc_type = RTC_TYPE_RICOH;
262             }
263         }
264     }
265 }
266
267 void handle_reset_amiga(struct emulator_config *cfg) {
268     ac_z3_done = 0;
269     ac_z2_done = 0;
270
271     adjust_ranges_amiga(cfg);
272 }
273
274 void shutdown_platform_amiga(struct emulator_config *cfg) {
275     if (cfg) {}
276     if (cdtv_mode) {
277         FILE *out = fopen("data/cdtv.sram", "wb+");
278         if (out != NULL) {
279             printf("Saving CDTV SRAM.\n");
280             fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
281             fclose(out);
282         }
283         else {
284             printf("Failed to write CDTV SRAM to disk.\n");
285         }
286     }
287 }
288
289 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
290     cfg->register_read = handle_register_read_amiga;
291     cfg->register_write = handle_register_write_amiga;
292     cfg->custom_read = custom_read_amiga;
293     cfg->custom_write = custom_write_amiga;
294     cfg->platform_initial_setup = setup_platform_amiga;
295     cfg->handle_reset = handle_reset_amiga;
296     cfg->shutdown = shutdown_platform_amiga;
297
298     cfg->setvar = setvar_amiga;
299     cfg->id = PLATFORM_AMIGA;
300
301     if (subsys) {
302         cfg->subsys = malloc(strlen(subsys) + 1);
303         strcpy(cfg->subsys, subsys);
304     }
305 }