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