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