4 #include "../platforms.h"
5 #include "amiga-autoconf.h"
6 #include "amiga-registers.h"
7 #include "../shared/rtc.h"
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);
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];
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;
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";
28 extern const char *op_type_names[OP_TYPE_NUM];
29 extern uint8_t cdtv_mode;
30 extern uint8_t rtc_type;
31 extern unsigned char cdtv_sram[32 * SIZE_KILO];
33 #define min(a, b) (a < b) ? a : b
34 #define max(a, b) (a > b) ? a : b
36 inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) {
37 if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
38 if (ac_z2_pic_count == 0) {
43 if (type == OP_TYPE_BYTE) {
44 *val = autoconfig_read_memory_8(cfg, addr);
48 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
49 if (ac_z3_pic_count == 0) {
54 if (type == OP_TYPE_BYTE) {
55 *val = autoconfig_read_memory_z3_8(cfg, addr);
59 printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
67 inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) {
68 if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
69 if (type == OP_TYPE_BYTE) {
70 if (ac_z2_pic_count == 0) {
75 //printf("Write to Z2 autoconf area.\n");
76 autoconfig_write_memory_8(cfg, addr, val);
81 if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
82 if (type == OP_TYPE_BYTE) {
83 if (ac_z3_pic_count == 0) {
88 //printf("Write to autoconf area.\n");
89 autoconfig_write_memory_z3_8(cfg, addr, val);
92 else if (type == OP_TYPE_WORD) {
93 autoconfig_write_memory_z3_16(cfg, addr, val);
97 printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
105 void adjust_ranges_amiga(struct emulator_config *cfg) {
106 cfg->mapped_high = 0;
108 cfg->custom_high = 0;
111 // Set up the min/max ranges for mapped reads/writes
112 if (gayle_emulation_enabled) {
113 cfg->mapped_low = GAYLEBASE;
114 cfg->mapped_high = GAYLEBASE + GAYLESIZE;
116 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
117 if (cfg->map_type[i] != MAPTYPE_NONE) {
118 if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->mapped_low) || cfg->mapped_low == 0)
119 cfg->mapped_low = cfg->map_offset[i];
120 if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high)
121 cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i];
125 if (ac_z2_pic_count && !ac_z2_done) {
126 if (cfg->custom_low == 0)
127 cfg->custom_low = AC_Z2_BASE;
129 cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE);
130 cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE);
132 if (ac_z3_pic_count && !ac_z3_done) {
133 if (cfg->custom_low == 0)
134 cfg->custom_low = AC_Z3_BASE;
136 cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE);
137 cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE);
140 printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high);
141 printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high);
144 int setup_platform_amiga(struct emulator_config *cfg) {
145 printf("Performing setup for Amiga platform.\n");
146 // Look for Z2 autoconf Fast RAM by id
147 int index = get_named_mapped_item(cfg, z2_autoconf_id);
150 // "Zap" config items as they are processed.
151 cfg->map_id[index][0] = '^';
153 if (cfg->map_size[index] > 8 * SIZE_MEGA) {
154 printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
155 resize_data = 8 * SIZE_MEGA;
157 else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
158 printf("Z2 Fast RAM may only provision 2, 4 or 8MB of memory, resizing to ");
159 if (cfg->map_size[index] > 8 * SIZE_MEGA)
160 resize_data = 8 * SIZE_MEGA;
161 else if (cfg->map_size[index] > 4 * SIZE_MEGA)
162 resize_data = 4 * SIZE_MEGA;
164 resize_data = 2 * SIZE_MEGA;
165 printf("%dMB.\n", resize_data / SIZE_MEGA);
168 free(cfg->map_data[index]);
169 cfg->map_size[index] = resize_data;
170 cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
172 printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
173 ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
174 ac_z2_index[ac_z2_pic_count] = index;
178 printf("No Z2 Fast RAM configured.\n");
180 index = get_named_mapped_item(cfg, z2_autoconf_id);
184 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) {
185 // Restore any "zapped" autoconf items so they can be reinitialized if needed.
186 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z2_autoconf_zap_id) == 0) {
187 cfg->map_id[i][0] = z2_autoconf_id[0];
191 index = get_named_mapped_item(cfg, z3_autoconf_id);
194 cfg->map_id[index][0] = '^';
195 printf("%dMB of Z3 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
196 ac_z3_type[ac_z3_pic_count] = ACTYPE_MAPFAST_Z3;
197 ac_z3_index[ac_z3_pic_count] = index;
201 printf("No Z3 Fast RAM configured.\n");
202 index = get_named_mapped_item(cfg, z3_autoconf_id);
205 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
206 if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) {
207 cfg->map_id[i][0] = z3_autoconf_id[0];
211 index = get_named_mapped_item(cfg, "cpu_slot_ram");
213 m68k_add_ram_range((uint32_t)cfg->map_offset[index], (uint32_t)cfg->map_high[index], cfg->map_data[index]);
216 adjust_ranges_amiga(cfg);
219 FILE *in = fopen("data/cdtv.sram", "rb");
221 printf("Loaded CDTV SRAM.\n");
222 fread(cdtv_sram, 32 * SIZE_KILO, 1, in);
230 void setvar_amiga(char *var, char *val) {
234 if (strcmp(var, "enable_rtc_emulation") == 0) {
235 int8_t rtc_enabled = 0;
236 if (!val || strlen(val) == 0)
239 rtc_enabled = get_int(val);
241 if (rtc_enabled != -1) {
242 configure_rtc_emulation_amiga(rtc_enabled);
245 if (strcmp(var, "hdd0") == 0) {
246 if (val && strlen(val) != 0)
247 set_hard_drive_image_file_amiga(0, val);
249 if (strcmp(var, "cdtv") == 0) {
250 printf("[AMIGA] CDTV mode enabled.\n");
253 if (strcmp(var, "rtc_type") == 0) {
254 if (val && strlen(val) != 0) {
255 if (strcmp(val, "msm") == 0) {
256 printf("[AMIGA] RTC type set to MSM.\n");
257 rtc_type = RTC_TYPE_MSM;
260 printf("[AMIGA] RTC type set to Ricoh.\n");
261 rtc_type = RTC_TYPE_RICOH;
267 void handle_reset_amiga(struct emulator_config *cfg) {
271 adjust_ranges_amiga(cfg);
274 void shutdown_platform_amiga(struct emulator_config *cfg) {
277 FILE *out = fopen("data/cdtv.sram", "wb+");
279 printf("Saving CDTV SRAM.\n");
280 fwrite(cdtv_sram, 32 * SIZE_KILO, 1, out);
284 printf("Failed to write CDTV SRAM to disk.\n");
289 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
290 cfg->register_read = handle_register_read_amiga;
291 cfg->register_write = handle_register_write_amiga;
292 cfg->custom_read = custom_read_amiga;
293 cfg->custom_write = custom_write_amiga;
294 cfg->platform_initial_setup = setup_platform_amiga;
295 cfg->handle_reset = handle_reset_amiga;
296 cfg->shutdown = shutdown_platform_amiga;
298 cfg->setvar = setvar_amiga;
299 cfg->id = PLATFORM_AMIGA;
302 cfg->subsys = malloc(strlen(subsys) + 1);
303 strcpy(cfg->subsys, subsys);