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