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