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