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