]> git.sesse.net Git - pistorm/blob - platforms/amiga/amiga-platform.c
Update Gayle.c, emulator.c, and amiga-platform.c
[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 extern uint8_t cdtv_mode;
29
30 #define min(a, b) (a < b) ? a : b
31 #define max(a, b) (a > b) ? a : b
32
33 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
34     if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
35         if (ac_z2_pic_count == 0) {
36             ac_z2_done = 1;
37             return -1;
38         }
39
40         if (type == OP_TYPE_BYTE) {
41             *val = autoconfig_read_memory_8(cfg, addr);
42             return 1;
43         }
44     }
45     if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
46         if (ac_z3_pic_count == 0) {
47             ac_z3_done = 1;
48             return -1;
49         }
50
51         if (type == OP_TYPE_BYTE) {
52             *val = autoconfig_read_memory_z3_8(cfg, addr);
53             return 1;
54         }
55         else {
56             printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
57             //stop_emulation();
58         }
59     }
60
61     return -1;
62 }
63
64 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
65     if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
66         if (type == OP_TYPE_BYTE) {
67             if (ac_z2_pic_count == 0) {
68                 ac_z2_done = 1;
69                 return -1;
70             }
71
72             //printf("Write to Z2 autoconf area.\n");
73             autoconfig_write_memory_8(cfg, addr, val);
74             return 1;
75         }
76     }
77
78     if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
79         if (type == OP_TYPE_BYTE) {
80             if (ac_z3_pic_count == 0) {
81                 ac_z3_done = 1;
82                 return -1;
83             }
84
85             //printf("Write to autoconf area.\n");
86             autoconfig_write_memory_z3_8(cfg, addr, val);
87             return 1;
88         }
89         else if (type == OP_TYPE_WORD) {
90             autoconfig_write_memory_z3_16(cfg, addr, val);
91             return 1;
92         }
93         else {
94             printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
95             //stop_emulation();
96         }
97     }
98
99     return -1;
100 }
101
102 void adjust_ranges_amiga(struct emulator_config *cfg) {
103     cfg->mapped_high = 0;
104     cfg->mapped_low = 0;
105     cfg->custom_high = 0;
106     cfg->custom_low = 0;
107
108     // Set up the min/max ranges for mapped reads/writes
109     if (gayle_emulation_enabled) {
110         cfg->mapped_low = GAYLEBASE;
111         cfg->mapped_high = GAYLEBASE + GAYLESIZE;
112     }
113     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
114         if (cfg->map_type[i] != MAPTYPE_NONE) {
115             if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
116                 cfg->mapped_low = cfg->map_offset[i];
117             if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
118                 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
119         }
120     }
121
122     if (ac_z2_pic_count && !ac_z2_done) {
123         if (cfg->custom_low == 0)
124             cfg->custom_low = AC_Z2_BASE;
125         else
126             cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
127         cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
128     }
129     if (ac_z3_pic_count && !ac_z3_done) {
130         if (cfg->custom_low == 0)
131             cfg->custom_low = AC_Z3_BASE;
132         else
133             cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
134         cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
135     }
136
137     printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
138     printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
139 }
140
141 int setup_platform_amiga(struct emulator_config *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     index = get_named_mapped_item(cfg, "cpu_slot_ram");
209     if (index != -1) {
210         m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
211     }
212
213     adjust_ranges_amiga(cfg);
214     
215     return 0;
216 }
217
218 void setvar_amiga(char *var, char *val) {
219     if (!var)
220         return;
221
222     if (strcmp(var, "enable_rtc_emulation") == 0) {
223         int8_t rtc_enabled = 0;
224         if (!val || strlen(val) == 0)
225             rtc_enabled = 1;
226         else {
227             rtc_enabled = get_int(val);
228         }
229         if (rtc_enabled != -1) {
230             configure_rtc_emulation_amiga(rtc_enabled);
231         }
232     }
233     if (strcmp(var, "hdd0") == 0) {
234         if (val && strlen(val) != 0)
235             set_hard_drive_image_file_amiga(0, val);
236     }
237     if (strcmp(var, "cdtv") == 0) {
238         cdtv_mode = 1;
239     }
240 }
241
242 void handle_reset_amiga(struct emulator_config *cfg) {
243     ac_z3_done = 0;
244     ac_z2_done = 0;
245
246     adjust_ranges_amiga(cfg);
247 }
248
249 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
250     cfg->register_read = handle_register_read_amiga;
251     cfg->register_write = handle_register_write_amiga;
252     cfg->custom_read = custom_read_amiga;
253     cfg->custom_write = custom_write_amiga;
254     cfg->platform_initial_setup = setup_platform_amiga;
255     cfg->handle_reset = handle_reset_amiga;
256
257     cfg->setvar = setvar_amiga;
258     cfg->id = PLATFORM_AMIGA;
259
260     if (subsys) {
261         cfg->subsys = malloc(strlen(subsys) + 1);
262         strcpy(cfg->subsys, subsys);
263     }
264 }