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