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