]> git.sesse.net Git - pistorm/blob - platforms/amiga/amiga-autoconf.c
Add not-so-simple config switching from Amiga side
[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/0B, 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 void autoconfig_reset_all() {
107   printf("[AUTOCONF] Resetting all autoconf data.\n");
108   for (int i = 0; i < AC_PIC_LIMIT; i++) {
109     ac_z2_type[i] = ACTYPE_NONE;
110     ac_z3_type[i] = ACTYPE_NONE;
111     ac_z2_index[i] = 0;
112     ac_z3_index[i] = 0;
113   }
114   ac_z3_pic_count = 0;
115   ac_z2_pic_count = 0;
116   ac_z2_current_pic = 0;
117   ac_z3_current_pic = 0;
118 }
119
120 unsigned int autoconfig_read_memory_z3_8(struct emulator_config *cfg, unsigned int address) {
121   int index = ac_z3_index[ac_z3_current_pic];
122   unsigned char val = 0;
123
124   if ((address & 0xFF) >= AC_Z3_REG_RES50 && (address & 0xFF) <= AC_Z3_REG_RES7C) {
125     val = 0;
126   }
127   else {
128     switch(address & 0xFF) {
129       case AC_Z3_REG_ER_TYPE:
130         val |= BOARDTYPE_Z3;
131         if (cfg->map_type[index] == MAPTYPE_RAM)
132           val |= BOARDTYPE_FREEMEM;
133         if (cfg->map_size[index] > 8 * SIZE_MEGA)
134           val |= get_autoconf_size_ext(cfg->map_size[index]);
135         else
136           val |= get_autoconf_size(cfg->map_size[index]);
137         if (ac_z3_current_pic + 1 < ac_z3_pic_count)
138           val |= BOARDTYPE_LINKED;
139         // Pre-invert this value, since it's the only value not physically complemented
140         // for Zorro III.
141         val ^= 0xFF;
142         break;
143       case AC_Z3_REG_ER_PRODUCT:
144         // 1.1... maybe...
145         val = 0x11;
146         break;
147       case AC_Z3_REG_ER_FLAGS:
148         if (cfg->map_type[index] == MAPTYPE_RAM)
149           val |= Z3_FLAGS_MEMORY;
150         if (cfg->map_size[index] > 8 * SIZE_MEGA)
151           val |= Z3_FLAGS_EXTENSION;
152         val |= Z3_FLAGS_RESERVED;
153         // Bottom four bits are zero, useless unles you want really odd RAM sizes.
154         break;
155       // Manufacturer ID low/high bytes.
156       case AC_Z3_REG_MAN_LO:
157         val = PISTORM_MANUF_ID & 0x00FF;
158         break;
159       case AC_Z3_REG_MAN_HI:
160         val = (PISTORM_MANUF_ID >> 8);
161         break;
162       case AC_Z3_REG_SER_BYTE0:
163       case AC_Z3_REG_SER_BYTE1:
164       case AC_Z3_REG_SER_BYTE2:
165       case AC_Z3_REG_SER_BYTE3:
166         // Expansion board serial assigned by manufacturer.
167         val = 0;
168         break;
169       case AC_Z3_REG_INIT_DIAG_VEC_LO:
170       case AC_Z3_REG_INIT_DIAG_VEC_HI:
171         // 16-bit offset to boot ROM in assigned memory range.
172         val = 0;
173         break;
174       // Additional reserved/unused registers.
175       case AC_Z3_REG_ER_RES03:
176       case AC_Z3_REG_ER_RES0D:
177       case AC_Z3_REG_ER_RES0E:
178       case AC_Z3_REG_ER_RES0F:
179       case AC_Z3_REG_ER_Z2_INT:
180         val = 0;
181         break;
182       default:
183         val = 0;
184         break;
185     }
186   }
187   //printf("Read byte %d from Z3 autoconf for PIC %d (%.2X).\n", address, ac_z3_current_pic, val);
188   return (address & 0x100) ? (val << 4) ^ 0xFF : (val & 0xF0) ^ 0xFF;
189 }
190
191 int nib_latch = 0;
192
193 void autoconfig_write_memory_z3_8(struct emulator_config *cfg, unsigned int address, unsigned int value) {
194   int index = ac_z3_index[ac_z3_current_pic];
195   unsigned char val = (unsigned char)value;
196   int done = 0;
197
198   switch(address & 0xFF) {
199     case AC_Z3_REG_WR_ADDR_LO:
200       if (nib_latch) {
201         ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFF0F0000) | ((val & 0xF0) << 16);
202         nib_latch = 0;
203       }
204       else
205         ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFF000000) | (val << 16);
206       break;
207     case AC_Z3_REG_WR_ADDR_HI:
208       if (nib_latch) {
209         ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x0FFF0000) | ((val & 0xF0) << 24);
210         nib_latch = 0;
211       }
212       ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x00FF0000) | (val << 24);
213       done = 1;
214       break;
215     case AC_Z3_REG_WR_ADDR_NIB_LO:
216       ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFFF00000) | ((val & 0xF0) << 12);
217       nib_latch = 1;
218       break;
219     case AC_Z3_REG_WR_ADDR_NIB_HI:
220       ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xF0FF0000) | ((val & 0xF0) << 20);
221       nib_latch = 1;
222       break;
223     case AC_Z3_REG_SHUTUP:
224       //printf("Write to Z3 shutup register for PIC %d.\n", ac_z3_current_pic);
225       done = 1;
226       break;
227     default:
228       break;
229   }
230
231   if (done) {
232     nib_latch = 0;
233     printf("[AUTOCONF] Address of Z3 autoconf RAM assigned to $%.8x [B]\n", ac_base[ac_z3_current_pic]);
234     cfg->map_offset[index] = ac_base[ac_z3_current_pic];
235     cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
236     m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
237     ac_z3_current_pic++;
238     if (ac_z3_current_pic == ac_z3_pic_count) {
239       ac_z3_done = 1;
240       adjust_ranges_amiga(cfg);
241     }
242   }
243
244   return;
245 }
246
247 void autoconfig_write_memory_z3_16(struct emulator_config *cfg, unsigned int address, unsigned int value) {
248   int index = ac_z3_index[ac_z3_current_pic];
249   unsigned short val = (unsigned short)value;
250   int done = 0;
251
252   switch(address & 0xFF) {
253     case AC_Z3_REG_WR_ADDR_HI:
254       // This is, as far as I know, the only register it should write a 16-bit value to.
255       ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x00000000) | (val << 16);
256       done = 1;
257       break;
258     default:
259       printf("Unknown WORD write to Z3 autoconf address $%.2X", address & 0xFF);
260       //stop_cpu_emulation();
261       break;
262   }
263
264   if (done) {
265     printf("[AUTOCONF] Address of Z3 autoconf RAM assigned to $%.8x [W]\n", ac_base[ac_z3_current_pic]);
266     cfg->map_offset[index] = ac_base[ac_z3_current_pic];
267     cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
268     m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
269     ac_z3_current_pic++;
270     if (ac_z3_current_pic == ac_z3_pic_count) {
271       ac_z3_done = 1;
272       adjust_ranges_amiga(cfg);
273     }
274   }
275
276   return;
277 }
278
279 void add_z2_pic(uint8_t type, uint8_t index) {
280   if (ac_z2_pic_count < AC_PIC_LIMIT) {
281     ac_z2_type[ac_z2_pic_count] = type;
282     ac_z2_index[ac_z2_pic_count] = index;
283     ac_z2_pic_count++;
284     return;
285   }
286   printf("[AUTOCONF] Failed to add Z2 PIC of type %d, limit exceeded.\n", type);
287 }
288
289 void remove_z2_pic(uint8_t type, uint8_t index) {
290   uint8_t pic_found = 0;
291   if (index) {}
292
293   for (uint32_t i = 0; i < ac_z2_pic_count; i++) {
294     if (ac_z2_type[i] == type && !pic_found) {
295       pic_found = 1;
296     }
297     if (pic_found && i < AC_PIC_LIMIT - 1) {
298       ac_z2_type[i] = ac_z2_type[i + 1];
299       ac_z2_index[i] = ac_z2_index[ i + 1];
300     }
301   }
302
303   if (pic_found) {
304     ac_z2_type[AC_PIC_LIMIT - 1] = ACTYPE_NONE;
305     ac_z2_index[AC_PIC_LIMIT - 1] = 0;
306     ac_z2_pic_count--;
307   }
308   else {
309     printf("[AUTOCONF] Tried to remove Z2 PIC of type %d, but it wasn't found.\n", type);
310   }
311 }
312
313 unsigned int autoconfig_read_memory_8(struct emulator_config *cfg, unsigned int address) {
314   unsigned char *rom = NULL;
315   unsigned char val = 0;
316
317   switch(ac_z2_type[ac_z2_current_pic]) {
318     case ACTYPE_MAPFAST_Z2:
319       rom = ac_fast_ram_rom;
320       break;
321     case ACTYPE_A314:
322       rom = ac_a314_rom;
323       break;
324     case ACTYPE_PISCSI:
325       rom = ac_piscsi_rom;
326       break;
327     case ACTYPE_PISTORM_DEV:
328       rom = ac_pistorm_rom;
329       break;
330     default:
331       return 0;
332       break;
333   }
334
335
336   if ((address & 1) == 0 && (address / 2) < (int)sizeof(ac_fast_ram_rom)) {
337     if (ac_z2_type[ac_z2_current_pic] == ACTYPE_MAPFAST_Z2 && address / 2 == 1) {
338       val = get_autoconf_size(cfg->map_size[ac_z2_index[ac_z2_current_pic]]);
339       if (ac_z2_current_pic + 1 < ac_z2_pic_count)
340         val |= BOARDTYPE_LINKED;
341     }
342     else
343       val = rom[address / 2];
344     //printf("Read byte %d from Z2 autoconf for PIC %d (%.2X).\n", address/2, ac_z2_current_pic, val);
345   }
346   val <<= 4;
347   if (address != 0 && address != 2 && address != 0x40 && address != 0x42)
348     val ^= 0xff;
349
350   return (unsigned int)val;
351 }
352
353 void autoconfig_write_memory_8(struct emulator_config *cfg, unsigned int address, unsigned int value) {
354   int done = 0;
355   int index = ac_z2_index[ac_z2_current_pic];
356
357   unsigned int *base = NULL;
358
359   switch(ac_z2_type[ac_z2_current_pic]) {
360     case ACTYPE_MAPFAST_Z2:
361       base = &ac_base[ac_z2_current_pic];
362       break;
363     case ACTYPE_A314:
364       //base = &a314_base;
365       break;
366     case ACTYPE_PISCSI:
367       base = &piscsi_base;
368       break;
369     case ACTYPE_PISTORM_DEV:
370       base = &pistorm_dev_base;
371       break;
372     default:
373       break;
374   }
375
376   if (!base) {
377     //printf("Failed to set up the base for autoconfig PIC %d.\n", ac_z2_current_pic);
378     done = 1;
379   }
380   else {
381     if (address == 0x4a) {  // base[19:16]
382       *base = (value & 0xf0) << (16 - 4);
383     } else if (address == 0x48) {  // base[23:20]
384       *base &= 0xff0fffff;
385       *base |= (value & 0xf0) << (20 - 4);
386
387       if (ac_z2_type[ac_z2_current_pic] == ACTYPE_MAPFAST_Z2) { // fast ram
388         //a314_set_mem_base_size(*base, cfg->map_size[ac_index[ac_z2_current_pic]]);
389       }
390       done = 1;
391     } else if (address == 0x4c) {  // shut up
392       //printf("Write to Z2 shutup register for PIC %d.\n", ac_z2_current_pic);
393       done = 1;
394     }
395   }
396
397   if (done) {
398     switch (ac_z2_type[ac_z2_current_pic]) {
399       case ACTYPE_MAPFAST_Z2:
400         cfg->map_offset[index] = ac_base[ac_z2_current_pic];
401         cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
402         printf("[AUTOCONF] Address of Z2 autoconf RAM assigned to $%.8X\n", ac_base[ac_z2_current_pic]);
403         m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
404         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);
405         break;
406       case ACTYPE_PISCSI:
407         printf("[AUTOCONF] PiSCSI Z2 device assigned to $%.8X\n", piscsi_base);
408         //m68k_add_rom_range(piscsi_base + (16 * SIZE_KILO), piscsi_base + (32 * SIZE_KILO), piscsi_rom_ptr);
409         break;
410       case ACTYPE_PISTORM_DEV:
411         printf("[AUTOCONF] PiStorm Interaction Z2 Device assigned to $%.8X\n", pistorm_dev_base);
412         break;
413       default:
414         break;
415     }
416     ac_z2_current_pic++;
417     if (ac_z2_current_pic == ac_z2_pic_count) {
418       ac_z2_done = 1;
419       adjust_ranges_amiga(cfg);
420     }
421   }
422 }