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