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