]> git.sesse.net Git - pistorm/blob - platforms/amiga/amiga-platform.c
c2361e30b91d6f600e7d8049a5b7fb9f66039948
[pistorm] / platforms / amiga / amiga-platform.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "../platforms.h"
5 #include "amiga-autoconf.h"
6 #include "amiga-registers.h"
7
8 int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val);
9 int handle_register_write_amiga(unsigned int addr, unsigned int value, unsigned char type);
10
11 extern int ac_z2_done;
12 extern int ac_z2_pic_count;
13 extern int ac_z2_type[AC_PIC_LIMIT];
14 extern int ac_z2_index[AC_PIC_LIMIT];
15
16 extern int ac_z3_pic_count;
17 extern int ac_z3_done;
18 extern int ac_z3_type[AC_PIC_LIMIT];
19 extern int ac_z3_index[AC_PIC_LIMIT];
20 extern int gayle_emulation_enabled;
21
22 char *z2_autoconf_id = "z2_autoconf_fast";
23 char *z2_autoconf_zap_id = "^2_autoconf_fast";
24 char *z3_autoconf_id = "z3_autoconf_fast";
25 char *z3_autoconf_zap_id = "^3_autoconf_fast";
26
27 extern const char *op_type_names[OP_TYPE_NUM];
28
29 #define min(a, b) (a < b) ? a : b
30 #define max(a, b) (a > b) ? a : b
31
32 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
33     if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
34         if (ac_z2_pic_count == 0) {
35             ac_z2_done = 1;
36             return -1;
37         }
38
39         if (type == OP_TYPE_BYTE) {
40             *val = autoconfig_read_memory_8(cfg, addr);
41             return 1;
42         }
43     }
44     if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
45         if (ac_z3_pic_count == 0) {
46             ac_z3_done = 1;
47             return -1;
48         }
49
50         if (type == OP_TYPE_BYTE) {
51             *val = autoconfig_read_memory_z3_8(cfg, addr);
52             return 1;
53         }
54         else {
55             printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
56             //stop_emulation();
57         }
58     }
59
60     return -1;
61 }
62
63 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
64     if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
65         if (type == OP_TYPE_BYTE) {
66             if (ac_z2_pic_count == 0) {
67                 ac_z2_done = 1;
68                 return -1;
69             }
70
71             //printf("Write to Z2 autoconf area.\n");
72             autoconfig_write_memory_8(cfg, addr, val);
73             return 1;
74         }
75     }
76
77     if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
78         if (type == OP_TYPE_BYTE) {
79             if (ac_z3_pic_count == 0) {
80                 ac_z3_done = 1;
81                 return -1;
82             }
83
84             //printf("Write to autoconf area.\n");
85             autoconfig_write_memory_z3_8(cfg, addr, val);
86             return 1;
87         }
88         else if (type == OP_TYPE_WORD) {
89             autoconfig_write_memory_z3_16(cfg, addr, val);
90             return 1;
91         }
92         else {
93             printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
94             //stop_emulation();
95         }
96     }
97
98     return -1;
99 }
100
101 void adjust_ranges_amiga(struct emulator_config *cfg) {
102     cfg->mapped_high = 0;
103     cfg->mapped_low = 0;
104     cfg->custom_high = 0;
105     cfg->custom_low = 0;
106
107     // Set up the min/max ranges for mapped reads/writes
108     if (gayle_emulation_enabled) {
109         cfg->mapped_low = GAYLEBASE;
110         cfg->mapped_high = GAYLEBASE + GAYLESIZE;
111     }
112     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
113         if (cfg->map_type[i] != MAPTYPE_NONE) {
114             if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
115                 cfg->mapped_low = cfg->map_offset[i];
116             if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
117                 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
118         }
119     }
120
121     if (ac_z2_pic_count && !ac_z2_done) {
122         if (cfg->custom_low == 0)
123             cfg->custom_low = AC_Z2_BASE;
124         else
125             cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
126         cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
127     }
128     if (ac_z3_pic_count && !ac_z3_done) {
129         if (cfg->custom_low == 0)
130             cfg->custom_low = AC_Z3_BASE;
131         else
132             cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
133         cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
134     }
135
136     printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
137     printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
138 }
139
140 int setup_platform_amiga(struct emulator_config *cfg) {
141     if (cfg) {}
142     printf("Performing setup for Amiga platform.\n");
143     // Look for Z2 autoconf Fast RAM by id
144     int index = get_named_mapped_item(cfg, z2_autoconf_id);
145     more_z2_fast:;
146     if (index != -1) {
147         // "Zap" config items as they are processed.
148         cfg->map_id[index][0] = '^';
149         int resize_data = 0;
150         if (cfg->map_size[index] > 8 * SIZE_MEGA) {
151             printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
152             resize_data = 8 * SIZE_MEGA;
153         }
154         else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
155             printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
156             if (cfg->map_size[index] > 8 * SIZE_MEGA)
157                 resize_data = 8 * SIZE_MEGA;
158             else if (cfg->map_size[index] > 4 * SIZE_MEGA)
159                 resize_data = 4 * SIZE_MEGA;
160             else
161                 resize_data = 2 * SIZE_MEGA;
162             printf("%dMB.\n", resize_data / SIZE_MEGA);
163         }
164         if (resize_data) {
165             free(cfg->map_data[index]);
166             cfg->map_size[index] = resize_data;
167             cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
168         }
169         printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
170         ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
171         ac_z2_index[ac_z2_pic_count] = index;
172         ac_z2_pic_count++;
173     }
174     else
175         printf("No Z2 Fast RAM configured.\n");
176
177     index = get_named_mapped_item(cfg, z2_autoconf_id);
178     if (index != -1)
179         goto more_z2_fast;
180     
181     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
182         // Restore any "zapped" autoconf items so they can be reinitialized if needed.
183         if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
184             cfg->map_id[i][0] = z2_autoconf_id[0];
185         }
186     }
187
188     index = get_named_mapped_item(cfg, z3_autoconf_id);
189     more_z3_fast:;
190     if (index != -1) {
191         cfg->map_id[index][0] = '^';
192         printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
193         ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
194         ac_z3_index[ac_z3_pic_count] = index;
195         ac_z3_pic_count++;
196     }
197     else
198         printf("No Z3 Fast RAM configured.\n");
199     index = get_named_mapped_item(cfg, z3_autoconf_id);
200     if (index != -1)
201         goto more_z3_fast;
202     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
203         if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
204             cfg->map_id[i][0] = z3_autoconf_id[0];
205         }
206     }
207
208     adjust_ranges_amiga(cfg);
209     
210     return 0;
211 }
212
213 void setvar_amiga(char *var, char *val) {
214     if (!var)
215         return;
216
217     if (strcmp(var, "enable_rtc_emulation") == 0) {
218         int8_t rtc_enabled = 0;
219         if (!val || strlen(val) == 0)
220             rtc_enabled = 1;
221         else {
222             rtc_enabled = get_int(val);
223         }
224         if (rtc_enabled != -1) {
225             configure_rtc_emulation_amiga(rtc_enabled);
226         }
227     }
228     if (strcmp(var, "hdd0") == 0) {
229         if (val && strlen(val) != 0)
230             set_hard_drive_image_file_amiga(0, val);
231     }
232 }
233
234 void handle_reset_amiga(struct emulator_config *cfg) {
235     ac_z3_done = 0;
236     ac_z2_done = 0;
237
238     adjust_ranges_amiga(cfg);
239 }
240
241 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
242     cfg->register_read = handle_register_read_amiga;
243     cfg->register_write = handle_register_write_amiga;
244     cfg->custom_read = custom_read_amiga;
245     cfg->custom_write = custom_write_amiga;
246     cfg->platform_initial_setup = setup_platform_amiga;
247     cfg->handle_reset = handle_reset_amiga;
248
249     cfg->setvar = setvar_amiga;
250     cfg->id = PLATFORM_AMIGA;
251
252     if (subsys) {
253         cfg->subsys = malloc(strlen(subsys) + 1);
254         strcpy(cfg->subsys, subsys);
255     }
256 }