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