]> git.sesse.net Git - pistorm/blob - platforms/amiga/amiga-platform.c
[WIP] Pile of stuff
[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 "piscsi/piscsi.h"
10 #include "piscsi/piscsi-enums.h"
11 #include "net/pi-net.h"
12 #include "net/pi-net-enums.h"
13 #include "rtg/rtg.h"
14
15 int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val);
16 int handle_register_write_amiga(unsigned int addr, unsigned int value, unsigned char type);
17 int init_rtg_data();
18
19 extern int ac_z2_current_pic;
20 extern int ac_z2_done;
21 extern int ac_z2_pic_count;
22 extern int ac_z2_type[AC_PIC_LIMIT];
23 extern int ac_z2_index[AC_PIC_LIMIT];
24
25 extern int ac_z3_current_pic;
26 extern int ac_z3_pic_count;
27 extern int ac_z3_done;
28 extern int ac_z3_type[AC_PIC_LIMIT];
29 extern int ac_z3_index[AC_PIC_LIMIT];
30 extern int gayle_emulation_enabled;
31
32 char *z2_autoconf_id = "z2_autoconf_fast";
33 char *z2_autoconf_zap_id = "^2_autoconf_fast";
34 char *z3_autoconf_id = "z3_autoconf_fast";
35 char *z3_autoconf_zap_id = "^3_autoconf_fast";
36
37 extern const char *op_type_names[OP_TYPE_NUM];
38 extern uint8_t cdtv_mode;
39 extern uint8_t rtc_type;
40 extern unsigned char cdtv_sram[32 * SIZE_KILO];
41
42 #define min(a, b) (a < b) ? a : b
43 #define max(a, b) (a > b) ? a : b
44
45 static uint8_t rtg_enabled = 0, piscsi_enabled = 0, pinet_enabled = 0;
46
47 extern uint32_t piscsi_base;
48
49 extern void stop_cpu_emulation(uint8_t disasm_cur);
50
51 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
52     if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
53         if (ac_z2_pic_count == 0) {
54             ac_z2_done = 1;
55             return -1;
56         }
57
58         if (type == OP_TYPE_BYTE) {
59             *val = autoconfig_read_memory_8(cfg, addr);
60             return 1;
61         }
62     }
63     if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
64         if (ac_z3_pic_count == 0) {
65             ac_z3_done = 1;
66             return -1;
67         }
68
69         if (type == OP_TYPE_BYTE) {
70             *val = autoconfig_read_memory_z3_8(cfg, addr);
71             return 1;
72         }
73         else {
74             printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
75             //stop_emulation();
76         }
77     }
78
79     if (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
80         //printf("[Amiga-Custom] %s read from PISCSI base @$%.8X.\n", op_type_names[type], addr);
81         //stop_cpu_emulation(1);
82         *val = handle_piscsi_read(addr, type);
83         return 1;
84     }
85
86     return -1;
87 }
88
89 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
90     if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
91         if (type == OP_TYPE_BYTE) {
92             if (ac_z2_pic_count == 0) {
93                 ac_z2_done = 1;
94                 return -1;
95             }
96
97             //printf("Write to Z2 autoconf area.\n");
98             autoconfig_write_memory_8(cfg, addr, val);
99             return 1;
100         }
101     }
102
103     if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
104         if (type == OP_TYPE_BYTE) {
105             if (ac_z3_pic_count == 0) {
106                 ac_z3_done = 1;
107                 return -1;
108             }
109
110             //printf("Write to autoconf area.\n");
111             autoconfig_write_memory_z3_8(cfg, addr, val);
112             return 1;
113         }
114         else if (type == OP_TYPE_WORD) {
115             autoconfig_write_memory_z3_16(cfg, addr, val);
116             return 1;
117         }
118         else {
119             printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
120             //stop_emulation();
121         }
122     }
123
124     if (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
125         //printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
126         handle_piscsi_write(addr, val, type);
127         return 1;
128     }
129
130     return -1;
131 }
132
133 void adjust_ranges_amiga(struct emulator_config *cfg) {
134     cfg->mapped_high = 0;
135     cfg->mapped_low = 0;
136     cfg->custom_high = 0;
137     cfg->custom_low = 0;
138
139     // Set up the min/max ranges for mapped reads/writes
140     if (gayle_emulation_enabled) {
141         cfg->mapped_low = GAYLEBASE;
142         cfg->mapped_high = GAYLEBASE + GAYLESIZE;
143     }
144     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
145         if (cfg->map_type[i] != MAPTYPE_NONE) {
146             if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
147                 cfg->mapped_low = cfg->map_offset[i];
148             if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
149                 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
150         }
151     }
152
153     if (ac_z2_pic_count && !ac_z2_done) {
154         if (cfg->custom_low == 0)
155             cfg->custom_low = AC_Z2_BASE;
156         else
157             cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
158         cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
159     }
160     if (ac_z3_pic_count && !ac_z3_done) {
161         if (cfg->custom_low == 0)
162             cfg->custom_low = AC_Z3_BASE;
163         else
164             cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
165         cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
166     }
167     if (rtg_enabled) {
168         if (cfg->custom_low == 0)
169             cfg->custom_low = PIGFX_RTG_BASE;
170         else
171             cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
172         cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
173     }
174     if (piscsi_enabled) {
175         if (cfg->custom_low == 0)
176             cfg->custom_low = PISCSI_OFFSET;
177         else
178             cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
179         cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
180         if (piscsi_base != 0) {
181             cfg->custom_low = min(cfg->custom_low, piscsi_base);
182         }
183     }
184     if (pinet_enabled) {
185         if (cfg->custom_low == 0)
186             cfg->custom_low = PINET_OFFSET;
187         else
188             cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
189         cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
190     }
191
192     printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
193     printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
194 }
195
196 int setup_platform_amiga(struct emulator_config *cfg) {
197     printf("Performing setup for Amiga platform.\n");
198
199     if (strlen(cfg->platform->subsys)) {
200         printf("Subsystem is [%s]\n", cfg->platform->subsys);
201         if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
202             printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
203             adjust_gayle_4000();
204         }
205         else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
206             printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
207             adjust_gayle_1200();
208         }
209         else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
210             printf("Configuring platform for CDTV emulation.\n");
211             cdtv_mode = 1;
212             rtc_type = RTC_TYPE_MSM;
213         }
214     }
215     else
216         printf("No sub system specified.\n");
217
218     // Look for Z2 autoconf Fast RAM by id
219     int index = get_named_mapped_item(cfg, z2_autoconf_id);
220     more_z2_fast:;
221     if (index != -1) {
222         // "Zap" config items as they are processed.
223         cfg->map_id[index][0] = '^';
224         int resize_data = 0;
225         if (cfg->map_size[index] > 8 * SIZE_MEGA) {
226             printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
227             resize_data = 8 * SIZE_MEGA;
228         }
229         else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
230             printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
231             if (cfg->map_size[index] > 8 * SIZE_MEGA)
232                 resize_data = 8 * SIZE_MEGA;
233             else if (cfg->map_size[index] > 4 * SIZE_MEGA)
234                 resize_data = 4 * SIZE_MEGA;
235             else
236                 resize_data = 2 * SIZE_MEGA;
237             printf("%dMB.\n", resize_data / SIZE_MEGA);
238         }
239         if (resize_data) {
240             free(cfg->map_data[index]);
241             cfg->map_size[index] = resize_data;
242             cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
243         }
244         printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
245         ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
246         ac_z2_index[ac_z2_pic_count] = index;
247         ac_z2_pic_count++;
248     }
249     else
250         printf("No Z2 Fast RAM configured.\n");
251
252     index = get_named_mapped_item(cfg, z2_autoconf_id);
253     if (index != -1)
254         goto more_z2_fast;
255     
256     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
257         // Restore any "zapped" autoconf items so they can be reinitialized if needed.
258         if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
259             cfg->map_id[i][0] = z2_autoconf_id[0];
260         }
261     }
262
263     index = get_named_mapped_item(cfg, z3_autoconf_id);
264     more_z3_fast:;
265     if (index != -1) {
266         cfg->map_id[index][0] = '^';
267         printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
268         ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
269         ac_z3_index[ac_z3_pic_count] = index;
270         ac_z3_pic_count++;
271     }
272     else
273         printf("No Z3 Fast RAM configured.\n");
274     index = get_named_mapped_item(cfg, z3_autoconf_id);
275     if (index != -1)
276         goto more_z3_fast;
277     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
278         if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
279             cfg->map_id[i][0] = z3_autoconf_id[0];
280         }
281     }
282
283     index = get_named_mapped_item(cfg, "cpu_slot_ram");
284     if (index != -1) {
285         m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
286     }
287
288     adjust_ranges_amiga(cfg);
289
290     if (cdtv_mode) {
291         FILE *in = fopen("data/cdtv.sram", "rb");
292         if (in != NULL) {
293             printf("Loaded CDTV SRAM.\n");
294             fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
295             fclose(in);
296         }
297     }
298     
299     return 0;
300 }
301
302 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
303     if (!var)
304         return;
305
306     if (strcmp(var, "enable_rtc_emulation") == 0) {
307         int8_t rtc_enabled = 0;
308         if (!val || strlen(val) == 0)
309             rtc_enabled = 1;
310         else {
311             rtc_enabled = get_int(val);
312         }
313         if (rtc_enabled != -1) {
314             configure_rtc_emulation_amiga(rtc_enabled);
315         }
316     }
317     if (strcmp(var, "hdd0") == 0) {
318         if (val && strlen(val) != 0)
319             set_hard_drive_image_file_amiga(0, val);
320     }
321     if (strcmp(var, "hdd1") == 0) {
322         if (val && strlen(val) != 0)
323             set_hard_drive_image_file_amiga(1, val);
324     }
325     if (strcmp(var, "cdtv") == 0) {
326         printf("[AMIGA] CDTV mode enabled.\n");
327         cdtv_mode = 1;
328     }
329     if (strcmp(var, "rtg") == 0) {
330         if (init_rtg_data()) {
331             printf("[AMIGA] RTG Enabled.\n");
332             rtg_enabled = 1;
333             adjust_ranges_amiga(cfg);
334         }
335         else
336             printf("[AMIGA} Failed to enable RTG.\n");
337     }
338
339     // PiSCSI stuff
340     if (strcmp(var, "piscsi") == 0) {
341         printf("[AMIGA] PISCSI Interface Enabled.\n");
342         piscsi_enabled = 1;
343         piscsi_init();
344         //ac_z2_type[ac_z2_pic_count] = ACTYPE_PISCSI;
345         //ac_z2_pic_count++;
346         adjust_ranges_amiga(cfg);
347     }
348     if (piscsi_enabled) {
349         if (strcmp(var, "piscsi0") == 0) {
350             piscsi_map_drive(val, 0);
351         }
352         if (strcmp(var, "piscsi1") == 0) {
353             piscsi_map_drive(val, 1);
354         }
355         if (strcmp(var, "piscsi2") == 0) {
356             piscsi_map_drive(val, 2);
357         }
358         if (strcmp(var, "piscsi3") == 0) {
359             piscsi_map_drive(val, 3);
360         }
361         if (strcmp(var, "piscsi4") == 0) {
362             piscsi_map_drive(val, 4);
363         }
364         if (strcmp(var, "piscsi5") == 0) {
365             piscsi_map_drive(val, 5);
366         }
367         if (strcmp(var, "piscsi6") == 0) {
368             piscsi_map_drive(val, 6);
369         }
370     }
371
372     // Pi-Net stuff
373     if (strcmp(var, "pi-net") == 0) {
374         printf("[AMIGA] PI-NET Interface Enabled.\n");
375         pinet_enabled = 1;
376         pinet_init(val);
377         adjust_ranges_amiga(cfg);
378     }
379
380     // RTC stuff
381     if (strcmp(var, "rtc_type") == 0) {
382         if (val && strlen(val) != 0) {
383             if (strcmp(val, "msm") == 0) {
384                 printf("[AMIGA] RTC type set to MSM.\n");
385                 rtc_type = RTC_TYPE_MSM;
386             }
387             else {
388                 printf("[AMIGA] RTC type set to Ricoh.\n");
389                 rtc_type = RTC_TYPE_RICOH;
390             }
391         }
392     }
393 }
394
395 void handle_reset_amiga(struct emulator_config *cfg) {
396     ac_z3_done = 0;
397     ac_z2_done = 0;
398     ac_z2_current_pic = 0;
399     ac_z3_current_pic = 0;
400
401     adjust_ranges_amiga(cfg);
402 }
403
404 void shutdown_platform_amiga(struct emulator_config *cfg) {
405     if (cfg) {}
406     if (cdtv_mode) {
407         FILE *out = fopen("data/cdtv.sram", "wb+");
408         if (out != NULL) {
409             printf("Saving CDTV SRAM.\n");
410             fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
411             fclose(out);
412         }
413         else {
414             printf("Failed to write CDTV SRAM to disk.\n");
415         }
416     }
417 }
418
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;
427
428     cfg->setvar = setvar_amiga;
429     cfg->id = PLATFORM_AMIGA;
430
431     if (subsys) {
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]);
436         }
437     }
438 }