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