]> git.sesse.net Git - pistorm/blob - platforms/amiga/amiga-autoconf.c
[WIP] Pile of stuff
[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 #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 address = address_ - AC_Z3_BASE;
91   int index = ac_z3_index[ac_z3_current_pic];
92   unsigned char val = 0;
93
94   if ((address & 0xFF) >= AC_Z3_REG_RES50 && (address & 0xFF) <= AC_Z3_REG_RES7C)
95     val = 0;
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       default:
150         val = 0;
151         break;
152     }
153   }
154   //printf("Read byte %d from Z3 autoconf for PIC %d (%.2X).\n", address, ac_z3_current_pic, val);
155   return (address & 0x100) ? (val << 4) ^ 0xFF : (val & 0xF0) ^ 0xFF;
156 }
157
158 int nib_latch = 0;
159
160 void autoconfig_write_memory_z3_8(struct emulator_config *cfg, unsigned int address_, unsigned int value) {
161   int address = address_ - AC_Z3_BASE;
162   int index = ac_z3_index[ac_z3_current_pic];
163   unsigned char val = (unsigned char)value;
164   int done = 0;
165
166   switch(address & 0xFF) {
167     case AC_Z3_REG_WR_ADDR_LO:
168       if (nib_latch) {
169         ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFF0F0000) | ((val & 0xF0) << 16);
170         nib_latch = 0;
171       }
172       else
173         ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFF000000) | (val << 16);
174       break;
175     case AC_Z3_REG_WR_ADDR_HI:
176       if (nib_latch) {
177         ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x0FFF0000) | ((val & 0xF0) << 24);
178         nib_latch = 0;
179       }
180       ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0x00FF0000) | (val << 24);
181       done = 1;
182       break;
183     case AC_Z3_REG_WR_ADDR_NIB_LO:
184       ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xFFF00000) | ((val & 0xF0) << 12);
185       nib_latch = 1;
186       break;
187     case AC_Z3_REG_WR_ADDR_NIB_HI:
188       ac_base[ac_z3_current_pic] = (ac_base[ac_z3_current_pic] & 0xF0FF0000) | ((val & 0xF0) << 20);
189       nib_latch = 1;
190       break;
191     case AC_Z3_REG_SHUTUP:
192       //printf("Write to Z3 shutup register for PIC %d.\n", ac_z3_current_pic);
193       done = 1;
194       break;
195     default:
196       break;
197   }
198
199   if (done) {
200     nib_latch = 0;
201     printf("Address of Z3 autoconf RAM assigned to $%.8x [B]\n", ac_base[ac_z3_current_pic]);
202     cfg->map_offset[index] = ac_base[ac_z3_current_pic];
203     cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
204     m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
205     ac_z3_current_pic++;
206     if (ac_z3_current_pic == ac_z3_pic_count) {
207       ac_z3_done = 1;
208       adjust_ranges_amiga(cfg);
209     }
210   }
211
212   return;
213 }
214
215 void autoconfig_write_memory_z3_16(struct emulator_config *cfg, unsigned int address_, unsigned int value) {
216   int address = address_ - AC_Z3_BASE;
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   int address = address_ - AC_Z2_BASE;
249   unsigned char val = 0;
250
251   switch(ac_z2_type[ac_z2_current_pic]) {
252     case ACTYPE_MAPFAST_Z2:
253       rom = ac_fast_ram_rom;
254       break;
255     case ACTYPE_A314:
256       rom = ac_a314_rom;
257       break;
258     case ACTYPE_PISCSI:
259       rom = ac_piscsi_rom;
260       break;
261     default:
262       return 0;
263       break;
264   }
265
266   
267   if ((address & 1) == 0 && (address / 2) < (int)sizeof(ac_fast_ram_rom)) {
268     if (ac_z2_type[ac_z2_current_pic] == ACTYPE_MAPFAST_Z2 && address / 2 == 1) {
269       val = get_autoconf_size(cfg->map_size[ac_z2_index[ac_z2_current_pic]]);
270       if (ac_z2_current_pic + 1 < ac_z2_pic_count)
271         val |= BOARDTYPE_LINKED;
272     }
273     else
274       val = rom[address / 2];
275     //printf("Read byte %d from Z2 autoconf for PIC %d (%.2X).\n", address/2, ac_z2_current_pic, val);
276   }
277   val <<= 4;
278   if (address != 0 && address != 2 && address != 0x40 && address != 0x42)
279     val ^= 0xff;
280   
281   return (unsigned int)val;
282 }
283
284 void autoconfig_write_memory_8(struct emulator_config *cfg, unsigned int address_, unsigned int value) {
285   int address = address_ - AC_Z2_BASE;
286   int done = 0;
287   int index = ac_z2_index[ac_z2_current_pic];
288
289   unsigned int *base = NULL;
290
291   switch(ac_z2_type[ac_z2_current_pic]) {
292     case ACTYPE_MAPFAST_Z2:
293       base = &ac_base[ac_z2_current_pic];
294       break;
295     case ACTYPE_A314:
296       //base = &a314_base;
297       break;
298     case ACTYPE_PISCSI:
299       base = &piscsi_base;
300       break;
301     default:
302       break;
303   }
304
305   if (!base) {
306     //printf("Failed to set up the base for autoconfig PIC %d.\n", ac_z2_current_pic);
307     done = 1;
308   }
309   else {
310     if (address == 0x4a) {  // base[19:16]
311       *base = (value & 0xf0) << (16 - 4);
312     } else if (address == 0x48) {  // base[23:20]
313       *base &= 0xff0fffff;
314       *base |= (value & 0xf0) << (20 - 4);
315
316       if (ac_z2_type[ac_z2_current_pic] == ACTYPE_MAPFAST_Z2) { // fast ram
317         //a314_set_mem_base_size(*base, cfg->map_size[ac_index[ac_z2_current_pic]]);
318       }
319       done = 1;
320     } else if (address == 0x4c) {  // shut up
321       //printf("Write to Z2 shutup register for PIC %d.\n", ac_z2_current_pic);
322       done = 1;
323     }
324   }
325
326   if (done) {
327     switch (ac_z2_type[ac_z2_current_pic]) {
328       case ACTYPE_MAPFAST_Z2:
329         cfg->map_offset[index] = ac_base[ac_z2_current_pic];
330         cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index];
331         printf("Address of Z2 autoconf RAM assigned to $%.8x\n", ac_base[ac_z2_current_pic]);
332         m68k_add_ram_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
333         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);
334         break;
335       case ACTYPE_PISCSI:
336         printf("PiSCSI Z2 device assigned to $%.8x\n", piscsi_base);
337         //m68k_add_rom_range(piscsi_base + (16 * SIZE_KILO), piscsi_base + (32 * SIZE_KILO), piscsi_rom_ptr);
338         break;
339       default:
340         break;
341     }
342     ac_z2_current_pic++;
343     if (ac_z2_current_pic == ac_z2_pic_count) {
344       ac_z2_done = 1;
345       adjust_ranges_amiga(cfg);
346     }
347   }
348 }