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