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