]> git.sesse.net Git - pistorm/blob - platforms/amiga/amiga-platform.c
71fd2b5d1c4c1c66bf6584171950c6627adfd8a3
[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     printf("Performing setup for Amiga platform.\n");
142     // Look for Z2 autoconf Fast RAM by id
143     int index = get_named_mapped_item(cfg, z2_autoconf_id);
144     more_z2_fast:;
145     if (index != -1) {
146         // "Zap" config items as they are processed.
147         cfg->map_id[index][0] = '^';
148         int resize_data = 0;
149         if (cfg->map_size[index] > 8 * SIZE_MEGA) {
150             printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
151             resize_data = 8 * SIZE_MEGA;
152         }
153         else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
154             printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
155             if (cfg->map_size[index] > 8 * SIZE_MEGA)
156                 resize_data = 8 * SIZE_MEGA;
157             else if (cfg->map_size[index] > 4 * SIZE_MEGA)
158                 resize_data = 4 * SIZE_MEGA;
159             else
160                 resize_data = 2 * SIZE_MEGA;
161             printf("%dMB.\n", resize_data / SIZE_MEGA);
162         }
163         if (resize_data) {
164             free(cfg->map_data[index]);
165             cfg->map_size[index] = resize_data;
166             cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
167         }
168         printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
169         ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
170         ac_z2_index[ac_z2_pic_count] = index;
171         ac_z2_pic_count++;
172     }
173     else
174         printf("No Z2 Fast RAM configured.\n");
175
176     index = get_named_mapped_item(cfg, z2_autoconf_id);
177     if (index != -1)
178         goto more_z2_fast;
179     
180     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
181         // Restore any "zapped" autoconf items so they can be reinitialized if needed.
182         if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
183             cfg->map_id[i][0] = z2_autoconf_id[0];
184         }
185     }
186
187     index = get_named_mapped_item(cfg, z3_autoconf_id);
188     more_z3_fast:;
189     if (index != -1) {
190         cfg->map_id[index][0] = '^';
191         printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
192         ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
193         ac_z3_index[ac_z3_pic_count] = index;
194         ac_z3_pic_count++;
195     }
196     else
197         printf("No Z3 Fast RAM configured.\n");
198     index = get_named_mapped_item(cfg, z3_autoconf_id);
199     if (index != -1)
200         goto more_z3_fast;
201     for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
202         if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
203             cfg->map_id[i][0] = z3_autoconf_id[0];
204         }
205     }
206
207     index = get_named_mapped_item(cfg, "cpu_slot_ram");
208     if (index != -1) {
209         m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
210     }
211
212     adjust_ranges_amiga(cfg);
213     
214     return 0;
215 }
216
217 void setvar_amiga(char *var, char *val) {
218     if (!var)
219         return;
220
221     if (strcmp(var, "enable_rtc_emulation") == 0) {
222         int8_t rtc_enabled = 0;
223         if (!val || strlen(val) == 0)
224             rtc_enabled = 1;
225         else {
226             rtc_enabled = get_int(val);
227         }
228         if (rtc_enabled != -1) {
229             configure_rtc_emulation_amiga(rtc_enabled);
230         }
231     }
232     if (strcmp(var, "hdd0") == 0) {
233         if (val && strlen(val) != 0)
234             set_hard_drive_image_file_amiga(0, val);
235     }
236 }
237
238 void handle_reset_amiga(struct emulator_config *cfg) {
239     ac_z3_done = 0;
240     ac_z2_done = 0;
241
242     adjust_ranges_amiga(cfg);
243 }
244
245 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
246     cfg->register_read = handle_register_read_amiga;
247     cfg->register_write = handle_register_write_amiga;
248     cfg->custom_read = custom_read_amiga;
249     cfg->custom_write = custom_write_amiga;
250     cfg->platform_initial_setup = setup_platform_amiga;
251     cfg->handle_reset = handle_reset_amiga;
252
253     cfg->setvar = setvar_amiga;
254     cfg->id = PLATFORM_AMIGA;
255
256     if (subsys) {
257         cfg->subsys = malloc(strlen(subsys) + 1);
258         strcpy(cfg->subsys, subsys);
259     }
260 }