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