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