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