]> git.sesse.net Git - pistorm/blob - platforms/amiga/amiga-platform.c
[WIP] PiSCSI custom file system experiments
[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     if (gayle_emulation_enabled) {
142         cfg->mapped_low = GAYLEBASE;
143         cfg->mapped_high = GAYLEBASE + GAYLESIZE;
144     }
145     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
146         if (cfg->map_type[i] != MAPTYPE_NONE) {
147             if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
148                 cfg->mapped_low = cfg->map_offset[i];
149             if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
150                 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
151         }
152     }
153
154     if (ac_z2_pic_count && !ac_z2_done) {
155         if (cfg->custom_low == 0)
156             cfg->custom_low = AC_Z2_BASE;
157         else
158             cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
159         cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
160     }
161     if (ac_z3_pic_count && !ac_z3_done) {
162         if (cfg->custom_low == 0)
163             cfg->custom_low = AC_Z3_BASE;
164         else
165             cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
166         cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
167     }
168     if (rtg_enabled) {
169         if (cfg->custom_low == 0)
170             cfg->custom_low = PIGFX_RTG_BASE;
171         else
172             cfg->custom_low = min(cfg->custom_low, PIGFX_RTG_BASE);
173         cfg->custom_high = max(cfg->custom_high, PIGFX_UPPER);
174     }
175     if (piscsi_enabled) {
176         if (cfg->custom_low == 0)
177             cfg->custom_low = PISCSI_OFFSET;
178         else
179             cfg->custom_low = min(cfg->custom_low, PISCSI_OFFSET);
180         cfg->custom_high = max(cfg->custom_high, PISCSI_UPPER);
181         if (piscsi_base != 0) {
182             cfg->custom_low = min(cfg->custom_low, piscsi_base);
183         }
184     }
185     if (pinet_enabled) {
186         if (cfg->custom_low == 0)
187             cfg->custom_low = PINET_OFFSET;
188         else
189             cfg->custom_low = min(cfg->custom_low, PINET_OFFSET);
190         cfg->custom_high = max(cfg->custom_high, PINET_UPPER);
191     }
192
193     printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
194     printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
195 }
196
197 int setup_platform_amiga(struct emulator_config *cfg) {
198     printf("Performing setup for Amiga platform.\n");
199
200     if (strlen(cfg->platform->subsys)) {
201         printf("Subsystem is [%s]\n", cfg->platform->subsys);
202         if (strcmp(cfg->platform->subsys, "4000") == 0 || strcmp(cfg->platform->subsys, "3000") == 0) {
203             printf("Adjusting Gayle accesses for A3000/4000 Kickstart.\n");
204             adjust_gayle_4000();
205         }
206         else if (strcmp(cfg->platform->subsys, "1200") == 0 || strcmp(cfg->platform->subsys, "cd32") == 0) {
207             printf("Adjusting Gayle accesses for A1200/CD32 Kickstart.\n");
208             adjust_gayle_1200();
209         }
210         else if (strcmp(cfg->platform->subsys, "cdtv") == 0) {
211             printf("Configuring platform for CDTV emulation.\n");
212             cdtv_mode = 1;
213             rtc_type = RTC_TYPE_MSM;
214         }
215     }
216     else
217         printf("No sub system specified.\n");
218
219     // Look for Z2 autoconf Fast RAM by id
220     int index = get_named_mapped_item(cfg, z2_autoconf_id);
221     more_z2_fast:;
222     if (index != -1) {
223         // "Zap" config items as they are processed.
224         cfg->map_id[index][0] = '^';
225         int resize_data = 0;
226         if (cfg->map_size[index] > 8 * SIZE_MEGA) {
227             printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
228             resize_data = 8 * SIZE_MEGA;
229         }
230         else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
231             printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
232             if (cfg->map_size[index] > 8 * SIZE_MEGA)
233                 resize_data = 8 * SIZE_MEGA;
234             else if (cfg->map_size[index] > 4 * SIZE_MEGA)
235                 resize_data = 4 * SIZE_MEGA;
236             else
237                 resize_data = 2 * SIZE_MEGA;
238             printf("%dMB.\n", resize_data / SIZE_MEGA);
239         }
240         if (resize_data) {
241             free(cfg->map_data[index]);
242             cfg->map_size[index] = resize_data;
243             cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
244         }
245         printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
246         ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
247         ac_z2_index[ac_z2_pic_count] = index;
248         ac_z2_pic_count++;
249     }
250     else
251         printf("No Z2 Fast RAM configured.\n");
252
253     index = get_named_mapped_item(cfg, z2_autoconf_id);
254     if (index != -1)
255         goto more_z2_fast;
256     
257     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
258         // Restore any "zapped" autoconf items so they can be reinitialized if needed.
259         if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
260             cfg->map_id[i][0] = z2_autoconf_id[0];
261         }
262     }
263
264     index = get_named_mapped_item(cfg, z3_autoconf_id);
265     more_z3_fast:;
266     if (index != -1) {
267         cfg->map_id[index][0] = '^';
268         printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
269         ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
270         ac_z3_index[ac_z3_pic_count] = index;
271         ac_z3_pic_count++;
272     }
273     else
274         printf("No Z3 Fast RAM configured.\n");
275     index = get_named_mapped_item(cfg, z3_autoconf_id);
276     if (index != -1)
277         goto more_z3_fast;
278     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
279         if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
280             cfg->map_id[i][0] = z3_autoconf_id[0];
281         }
282     }
283
284     index = get_named_mapped_item(cfg, "cpu_slot_ram");
285     if (index != -1) {
286         m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
287     }
288
289     adjust_ranges_amiga(cfg);
290
291     if (cdtv_mode) {
292         FILE *in = fopen("data/cdtv.sram", "rb");
293         if (in != NULL) {
294             printf("Loaded CDTV SRAM.\n");
295             fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
296             fclose(in);
297         }
298     }
299     
300     return 0;
301 }
302
303 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
304     if (!var)
305         return;
306
307     if (strcmp(var, "enable_rtc_emulation") == 0) {
308         int8_t rtc_enabled = 0;
309         if (!val || strlen(val) == 0)
310             rtc_enabled = 1;
311         else {
312             rtc_enabled = get_int(val);
313         }
314         if (rtc_enabled != -1) {
315             configure_rtc_emulation_amiga(rtc_enabled);
316         }
317     }
318     if (strcmp(var, "hdd0") == 0) {
319         if (val && strlen(val) != 0)
320             set_hard_drive_image_file_amiga(0, val);
321     }
322     if (strcmp(var, "hdd1") == 0) {
323         if (val && strlen(val) != 0)
324             set_hard_drive_image_file_amiga(1, val);
325     }
326     if (strcmp(var, "cdtv") == 0) {
327         printf("[AMIGA] CDTV mode enabled.\n");
328         cdtv_mode = 1;
329     }
330     if (strcmp(var, "rtg") == 0) {
331         if (init_rtg_data()) {
332             printf("[AMIGA] RTG Enabled.\n");
333             rtg_enabled = 1;
334             adjust_ranges_amiga(cfg);
335         }
336         else
337             printf("[AMIGA} Failed to enable RTG.\n");
338     }
339
340     // PiSCSI stuff
341     if (strcmp(var, "piscsi") == 0) {
342         printf("[AMIGA] PISCSI Interface Enabled.\n");
343         piscsi_enabled = 1;
344         piscsi_init();
345         ac_z2_type[ac_z2_pic_count] = ACTYPE_PISCSI;
346         ac_z2_pic_count++;
347         adjust_ranges_amiga(cfg);
348     }
349     if (piscsi_enabled) {
350         if (strcmp(var, "piscsi0") == 0) {
351             piscsi_map_drive(val, 0);
352         }
353         if (strcmp(var, "piscsi1") == 0) {
354             piscsi_map_drive(val, 1);
355         }
356         if (strcmp(var, "piscsi2") == 0) {
357             piscsi_map_drive(val, 2);
358         }
359         if (strcmp(var, "piscsi3") == 0) {
360             piscsi_map_drive(val, 3);
361         }
362         if (strcmp(var, "piscsi4") == 0) {
363             piscsi_map_drive(val, 4);
364         }
365         if (strcmp(var, "piscsi5") == 0) {
366             piscsi_map_drive(val, 5);
367         }
368         if (strcmp(var, "piscsi6") == 0) {
369             piscsi_map_drive(val, 6);
370         }
371     }
372
373     // Pi-Net stuff
374     if (strcmp(var, "pi-net") == 0) {
375         printf("[AMIGA] PI-NET Interface Enabled.\n");
376         pinet_enabled = 1;
377         pinet_init(val);
378         adjust_ranges_amiga(cfg);
379     }
380
381     // RTC stuff
382     if (strcmp(var, "rtc_type") == 0) {
383         if (val && strlen(val) != 0) {
384             if (strcmp(val, "msm") == 0) {
385                 printf("[AMIGA] RTC type set to MSM.\n");
386                 rtc_type = RTC_TYPE_MSM;
387             }
388             else {
389                 printf("[AMIGA] RTC type set to Ricoh.\n");
390                 rtc_type = RTC_TYPE_RICOH;
391             }
392         }
393     }
394 }
395
396 void handle_reset_amiga(struct emulator_config *cfg) {
397     ac_z3_done = 0;
398     ac_z2_done = 0;
399     ac_z2_current_pic = 0;
400     ac_z3_current_pic = 0;
401
402     if (piscsi_enabled)
403         piscsi_refresh_drives();
404
405     adjust_ranges_amiga(cfg);
406 }
407
408 void shutdown_platform_amiga(struct emulator_config *cfg) {
409     if (cfg) {}
410     if (cdtv_mode) {
411         FILE *out = fopen("data/cdtv.sram", "wb+");
412         if (out != NULL) {
413             printf("Saving CDTV SRAM.\n");
414             fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
415             fclose(out);
416         }
417         else {
418             printf("Failed to write CDTV SRAM to disk.\n");
419         }
420     }
421 }
422
423 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
424     cfg->register_read = handle_register_read_amiga;
425     cfg->register_write = handle_register_write_amiga;
426     cfg->custom_read = custom_read_amiga;
427     cfg->custom_write = custom_write_amiga;
428     cfg->platform_initial_setup = setup_platform_amiga;
429     cfg->handle_reset = handle_reset_amiga;
430     cfg->shutdown = shutdown_platform_amiga;
431
432     cfg->setvar = setvar_amiga;
433     cfg->id = PLATFORM_AMIGA;
434
435     if (subsys) {
436         cfg->subsys = malloc(strlen(subsys) + 1);
437         strcpy(cfg->subsys, subsys);
438         for (int i = 0; i < strlen(cfg->subsys); i++) {
439             cfg->subsys[i] = tolower(cfg->subsys[i]);
440         }
441     }
442 }