]> git.sesse.net Git - pistorm/blob - platforms/amiga/amiga-autoconf.c
Plug some read/write mappings directly into Musashi
[pistorm] / platforms / amiga / amiga-autoconf.c
1 #include "../platforms.h"
2 #include "amiga-autoconf.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 static unsigned char ac_fast_ram_rom[] = {
8     0xe, AC_MEM_SIZE_8MB,                   // 00/02, link into memory free list, 8 MB
9     0x6, 0x9,                               // 04/06, product id
10     0x8, 0x0,                               // 08/0a, preference to 8 MB space
11     0x0, 0x0,                               // 0c/0e, reserved
12     0x0, 0x7, 0xd, 0xb,                     // 10/12/14/16, mfg id
13     0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x2, 0x0  // 18/.../26, serial
14 };
15
16 static unsigned char ac_a314_rom[] = {
17     0xc, AC_MEM_SIZE_64KB,                  // 00/02, 64 kB
18     0xa, 0x3,                               // 04/06, product id
19     0x0, 0x0,                               // 08/0a, any space okay
20     0x0, 0x0,                               // 0c/0e, reserved
21     0x0, 0x7, 0xd, 0xb,                     // 10/12/14/16, mfg id
22     0xa, 0x3, 0x1, 0x4, 0x0, 0x0, 0x0, 0x0  // 18/.../26, serial
23 };
24
25 int ac_z2_current_pic = 0;
26 int ac_z2_pic_count = 0;
27 int ac_z2_done = 0;
28 int ac_z2_type[AC_PIC_LIMIT];
29 int ac_z2_index[AC_PIC_LIMIT];
30 unsigned int ac_base[AC_PIC_LIMIT];
31
32 int ac_z3_current_pic = 0;
33 int ac_z3_pic_count = 0;
34 int ac_z3_done = 0;
35 int ac_z3_type[AC_PIC_LIMIT];
36 int ac_z3_index[AC_PIC_LIMIT];
37
38
39 unsigned char get_autoconf_size(int size) {
40   if (size == 8 * SIZE_MEGA)
41     return AC_MEM_SIZE_8MB;
42   if (size == 4 * SIZE_MEGA)
43     return AC_MEM_SIZE_4MB;
44   if (size == 2 * SIZE_MEGA)
45     return AC_MEM_SIZE_2MB;
46   else
47     return AC_MEM_SIZE_64KB;
48 }
49
50 unsigned char get_autoconf_size_ext(int size) {
51   if (size == 16 * SIZE_MEGA)
52     return AC_MEM_SIZE_EXT_16MB;
53   if (size == 32 * SIZE_MEGA)
54     return AC_MEM_SIZE_EXT_32MB;
55   if (size == 64 * SIZE_MEGA)
56     return AC_MEM_SIZE_EXT_64MB;
57   if (size == 128 * SIZE_MEGA)
58     return AC_MEM_SIZE_EXT_128MB;
59   if (size == 256 * SIZE_MEGA)
60     return AC_MEM_SIZE_EXT_256MB;
61   if (size == 512 * SIZE_MEGA)
62     return AC_MEM_SIZE_EXT_512MB;
63   if (size == 1024 * SIZE_MEGA)
64     return AC_MEM_SIZE_EXT_1024MB;
65   else
66     return AC_MEM_SIZE_EXT_64MB;
67 }
68
69 extern void adjust_ranges_amiga(struct emulator_config *cfg);
70
71 unsigned int autoconfig_read_memory_z3_8(struct emulator_config *cfg, unsigned int address_) {
72   int address = address_ - AC_Z3_BASE;
73   int index = ac_z3_index[ac_z3_current_pic];
74   unsigned char val = 0;
75
76   if ((address & 0xFF) >= AC_Z3_REG_RES50 && (address & 0xFF) <= AC_Z3_REG_RES7C)
77     val = 0;
78   else {
79     switch(address & 0xFF) {
80       case AC_Z3_REG_ER_TYPE:
81         val |= BOARDTYPE_Z3;
82         if (cfg->map_type[index] == MAPTYPE_RAM)
83           val |= BOARDTYPE_FREEMEM;
84         if (cfg->map_size[index] > 8 * SIZE_MEGA)
85           val |= get_autoconf_size_ext(cfg->map_size[index]);
86         else
87           val |= get_autoconf_size(cfg->map_size[index]);
88         if (ac_z3_current_pic + 1 < ac_z3_pic_count)
89           val |= BOARDTYPE_LINKED;
90         // Pre-invert this value, since it's the only value not physically complemented
91         // for Zorro III.
92         val ^= 0xFF;
93         break;
94       case AC_Z3_REG_ER_PRODUCT:
95         // 1.1... maybe...
96         val = 0x11;
97         break;
98       case AC_Z3_REG_ER_FLAGS:
99         if (cfg->map_type[index] == MAPTYPE_RAM)
100           val |= Z3_FLAGS_MEMORY;
101         if (cfg->map_size[index] > 8 * SIZE_MEGA)
102           val |= Z3_FLAGS_EXTENSION;
103         val |= Z3_FLAGS_RESERVED;
104         // Bottom four bits are zero, useless unles you want really odd RAM sizes.
105         break;
106       // Manufacturer ID low/high bytes.
107       case AC_Z3_REG_MAN_LO:
108         val = PISTORM_MANUF_ID & 0x00FF;
109         break;
110       case AC_Z3_REG_MAN_HI:
111         val = (PISTORM_MANUF_ID >> 8);
112         break;
113       case AC_Z3_REG_SER_BYTE0:
114       case AC_Z3_REG_SER_BYTE1:
115       case AC_Z3_REG_SER_BYTE2:
116       case AC_Z3_REG_SER_BYTE3:
117         // Expansion board serial assigned by manufacturer.
118         val = 0;
119         break;
120       case AC_Z3_REG_INIT_DIAG_VEC_LO:
121       case AC_Z3_REG_INIT_DIAG_VEC_HI:
122         // 16-bit offset to boot ROM in assigned memory range.
123         val = 0;
124         break;
125       // Additional reserved/unused registers.
126       case AC_Z3_REG_ER_RES03:
127       case AC_Z3_REG_ER_RES0D:
128       case AC_Z3_REG_ER_RES0E:
129       case AC_Z3_REG_ER_RES0F:
130       case AC_Z3_REG_ER_Z2_INT:
131       default:
132         val = 0;
133         break;
134     }
135   }
136   //printf("Read byte %d from Z3 autoconf for PIC %d (%.2X).\n", address, ac_z3_current_pic, val);
137   return (address & 0x100) ? (val << 4) ^ 0xFF : (val & 0xF0) ^ 0xFF;
138 }
139
140 int nib_latch = 0;
141
142 void autoconfig_write_memory_z3_8(struct emulator_config *cfg, unsigned int address_, unsigned int value) {
143   int address = address_ - AC_Z3_BASE;
144   int index = ac_z3_index[ac_z3_current_pic];
145   unsigned char val = (unsigned char)value;
146   int done = 0;
147
148   switch(address & 0xFF) {
149     case AC_Z3_REG_WR_ADDR_LO:
150       if (nib_latch) {
151         ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFF0F0000) | ((val & 0xF0) << 16);
152         nib_latch = 0;
153       }
154       else
155         ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFF000000) | (val << 16);
156       break;
157     case AC_Z3_REG_WR_ADDR_HI:
158       if (nib_latch) {
159         ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x0FFF0000) | ((val & 0xF0) << 24);
160         nib_latch = 0;
161       }
162       ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x00FF0000) | (val << 24);
163       done = 1;
164       break;
165     case AC_Z3_REG_WR_ADDR_NIB_LO:
166       ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFFF00000) | ((val & 0xF0) << 12);
167       nib_latch = 1;
168       break;
169     case AC_Z3_REG_WR_ADDR_NIB_HI:
170       ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xF0FF0000) | ((val & 0xF0) << 20);
171       nib_latch = 1;
172       break;
173     case AC_Z3_REG_SHUTUP:
174       //printf("Write to Z3 shutup register for PIC %d.\n", ac_z3_current_pic);
175       done = 1;
176       break;
177     default:
178       break;
179   }
180
181   if (done) {
182     nib_latch = 0;
183     printf("Address of Z3 autoconf RAM assigned to $%.8x [B]\n", ac_base[ac_z3_current_pic]);
184     cfg->map_offset[index] = ac_base[ac_z3_current_pic];
185     cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
186     m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
187     ac_z3_current_pic++;
188     if (ac_z3_current_pic == ac_z3_pic_count) {
189       ac_z3_done = 1;
190       adjust_ranges_amiga(cfg);
191     }
192   }
193
194   return;
195 }
196
197 void autoconfig_write_memory_z3_16(struct emulator_config *cfg, unsigned int address_, unsigned int value) {
198   int address = address_ - AC_Z3_BASE;
199   int index = ac_z3_index[ac_z3_current_pic];
200   unsigned short val = (unsigned short)value;
201   int done = 0;
202
203   switch(address & 0xFF) {
204     case AC_Z3_REG_WR_ADDR_HI:
205       // This is, as far as I know, the only register it should write a 16-bit value to.
206       ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x00000000) | (val << 16);
207       done = 1;
208       break;
209     default:
210       printf("Unknown WORD write to Z3 autoconf address $%.2X", address & 0xFF);
211       //stop_cpu_emulation();
212       break;
213   }
214
215   if (done) {
216     printf("Address of Z3 autoconf RAM assigned to $%.8x [W]\n", ac_base[ac_z3_current_pic]);
217     cfg->map_offset[index] = ac_base[ac_z3_current_pic];
218     cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
219     m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
220     ac_z3_current_pic++;
221     if (ac_z3_current_pic == ac_z3_pic_count)
222       ac_z3_done = 1;
223   }
224
225   return;
226 }
227
228 unsigned int autoconfig_read_memory_8(struct emulator_config *cfg, unsigned int address_) {
229   unsigned char *rom = NULL;
230   int address = address_ - AC_Z2_BASE;
231   unsigned char val = 0;
232
233   switch(ac_z2_type[ac_z2_current_pic]) {
234     case ACTYPE_MAPFAST_Z2:
235       rom = ac_fast_ram_rom;
236       break;
237     case ACTYPE_A314:
238       rom = ac_a314_rom;
239       break;
240     default:
241       return 0;
242       break;
243   }
244
245   
246   if ((address & 1) == 0 && (address / 2) < (int)sizeof(ac_fast_ram_rom)) {
247     if (ac_z2_type[ac_z2_current_pic] == ACTYPE_MAPFAST_Z2 && address / 2 == 1) {
248       val = get_autoconf_size(cfg->map_size[ac_z2_index[ac_z2_current_pic]]);
249       if (ac_z2_current_pic + 1 < ac_z2_pic_count)
250         val |= BOARDTYPE_LINKED;
251     }
252     else
253       val = rom[address / 2];
254     //printf("Read byte %d from Z2 autoconf for PIC %d (%.2X).\n", address/2, ac_z2_current_pic, val);
255   }
256   val <<= 4;
257   if (address != 0 && address != 2 && address != 40 && address != 42)
258     val ^= 0xff;
259   
260   return (unsigned int)val;
261 }
262
263 void autoconfig_write_memory_8(struct emulator_config *cfg, unsigned int address_, unsigned int value) {
264   int address = address_ - AC_Z2_BASE;
265   int done = 0;
266   int index = ac_z2_index[ac_z2_current_pic];
267
268   unsigned int *base = NULL;
269
270   switch(ac_z2_type[ac_z2_current_pic]) {
271     case ACTYPE_MAPFAST_Z2:
272       base = &ac_base[ac_z2_current_pic];
273       break;
274     case ACTYPE_A314:
275       //base = &a314_base;
276       break;
277     default:
278       break;
279   }
280
281   if (!base) {
282     //printf("Failed to set up the base for autoconfig PIC %d.\n", ac_z2_current_pic);
283     done = 1;
284   }
285   else {
286     if (address == 0x4a) {  // base[19:16]
287       *base = (value & 0xf0) << (16 - 4);
288     } else if (address == 0x48) {  // base[23:20]
289       *base &= 0xff0fffff;
290       *base |= (value & 0xf0) << (20 - 4);
291
292       if (ac_z2_type[ac_z2_current_pic] == ACTYPE_MAPFAST_Z2) { // fast ram
293         //a314_set_mem_base_size(*base, cfg->map_size[ac_index[ac_z2_current_pic]]);
294       }
295       done = 1;
296     } else if (address == 0x4c) {  // shut up
297       //printf("Write to Z2 shutup register for PIC %d.\n", ac_z2_current_pic);
298       done = 1;
299     }
300   }
301
302   if (done) {
303     printf("Address of Z2 autoconf RAM assigned to $%.8x\n", ac_base[ac_z2_current_pic]);
304     cfg->map_offset[index] = ac_base[ac_z2_current_pic];
305     cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
306     m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
307     printf("Z2 PIC %d at $%.8lX-%.8lX, Size: %d MB\n", ac_z2_current_pic, cfg->map_offset[index], cfg->map_high[index], cfg->map_size[index] / SIZE_MEGA);
308     ac_z2_current_pic++;
309     if (ac_z2_current_pic == ac_z2_pic_count) {
310       ac_z2_done = 1;
311       adjust_ranges_amiga(cfg);
312     }
313   }
314 }