]> git.sesse.net Git - pistorm/blob - platforms/amiga/Gayle.c
Update config_file.h, emulator.c, and amiga-platform.c
[pistorm] / platforms / amiga / Gayle.c
1 //
2 //  Gayle.c
3 //  Omega
4 //
5 //  Created by Matt Parsons on 06/03/2019.
6 //  Copyright © 2019 Matt Parsons. All rights reserved.
7 //
8
9 // Write Byte to Gayle Space 0xda9000 (0x0000c3)
10 // Read Byte From Gayle Space 0xda9000
11 // Read Byte From Gayle Space 0xdaa000
12
13 #include "Gayle.h"
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <time.h>
20 #include <endian.h>
21
22 #include "../shared/rtc.h"
23 #include "../../config_file/config_file.h"
24
25 #include "gayle-ide/ide.h"
26 #include "amiga-registers.h"
27
28 //#define GSTATUS 0xda201c
29 //#define GCLOW   0xda2010
30 //#define GDH   0xda2018
31
32 // Gayle Addresses
33
34 // Gayle IDE Reads
35 #define GERROR 0xda2004   // Error
36 #define GSTATUS 0xda201c  // Status
37 // Gayle IDE Writes
38 #define GFEAT 0xda2004  // Write : Feature
39 #define GCMD 0xda201c   // Write : Command
40 // Gayle IDE RW
41 #define GDATA 0xda2000     // Data
42 #define GSECTCNT 0xda2008  // SectorCount
43 #define GSECTNUM 0xda200c  // SectorNumber
44 #define GCYLLOW 0xda2010   // CylinderLow
45 #define GCYLHIGH 0xda2014  // CylinderHigh
46 #define GDEVHEAD 0xda2018  // Device/Head
47 #define GCTRL 0xda3018     // Control
48 // Gayle Ident
49 #define GIDENT 0xDE1000
50
51 // Gayle IRQ/CC
52 #define GCS 0xDA8000   // Card Control
53 #define GIRQ 0xDA9000  // IRQ
54 #define GINT 0xDAA000  // Int enable
55 #define GCONF 0xDAB000  // Gayle Config
56
57 /* DA8000 */
58 #define GAYLE_CS_IDE 0x80   /* IDE int status */
59 #define GAYLE_CS_CCDET 0x40 /* credit card detect */
60 #define GAYLE_CS_BVD1 0x20  /* battery voltage detect 1 */
61 #define GAYLE_CS_SC 0x20    /* credit card status change */
62 #define GAYLE_CS_BVD2 0x10  /* battery voltage detect 2 */
63 #define GAYLE_CS_DA 0x10    /* digital audio */
64 #define GAYLE_CS_WR 0x08    /* write enable (1 == enabled) */
65 #define GAYLE_CS_BSY 0x04   /* credit card busy */
66 #define GAYLE_CS_IRQ 0x04   /* interrupt request */
67 #define GAYLE_CS_DAEN 0x02  /* enable digital audio */
68 #define GAYLE_CS_DIS 0x01   /* disable PCMCIA slot */
69
70 /* DA9000 */
71 #define GAYLE_IRQ_IDE 0x80
72 #define GAYLE_IRQ_CCDET 0x40 /* credit card detect */
73 #define GAYLE_IRQ_BVD1 0x20  /* battery voltage detect 1 */
74 #define GAYLE_IRQ_SC 0x20    /* credit card status change */
75 #define GAYLE_IRQ_BVD2 0x10  /* battery voltage detect 2 */
76 #define GAYLE_IRQ_DA 0x10    /* digital audio */
77 #define GAYLE_IRQ_WR 0x08    /* write enable (1 == enabled) */
78 #define GAYLE_IRQ_BSY 0x04   /* credit card busy */
79 #define GAYLE_IRQ_IRQ 0x04   /* interrupt request */
80 #define GAYLE_IRQ_RESET 0x02 /* reset machine after CCDET change */
81 #define GAYLE_IRQ_BERR 0x01  /* generate bus error after CCDET change */
82
83 /* DAA000 */
84 #define GAYLE_INT_IDE 0x80     /* IDE interrupt enable */
85 #define GAYLE_INT_CCDET 0x40   /* credit card detect change enable */
86 #define GAYLE_INT_BVD1 0x20    /* battery voltage detect 1 change enable */
87 #define GAYLE_INT_SC 0x20      /* credit card status change enable */
88 #define GAYLE_INT_BVD2 0x10    /* battery voltage detect 2 change enable */
89 #define GAYLE_INT_DA 0x10      /* digital audio change enable */
90 #define GAYLE_INT_WR 0x08      /* write enable change enabled */
91 #define GAYLE_INT_BSY 0x04     /* credit card busy */
92 #define GAYLE_INT_IRQ 0x04     /* credit card interrupt request */
93 #define GAYLE_INT_BVD_LEV 0x02 /* BVD int level, 0=lev2,1=lev6 */
94 #define GAYLE_INT_BSY_LEV 0x01 /* BSY int level, 0=lev2,1=lev6 */
95
96 #define GAYLE_MAX_HARDFILES 8
97
98 int counter;
99 static uint8_t gayle_irq, gayle_int, gayle_cs, gayle_cs_mask, gayle_cfg;
100 static struct ide_controller *ide0;
101 int fd;
102
103 uint8_t rtc_type = RTC_TYPE_RICOH;
104
105 char *hdd_image_file[GAYLE_MAX_HARDFILES];
106
107 uint8_t cdtv_mode = 0;
108 unsigned char cdtv_sram[32 * SIZE_KILO];
109
110 void set_hard_drive_image_file_amiga(uint8_t index, char *filename) {
111   if (hdd_image_file[index] != NULL)
112     free(hdd_image_file[index]);
113   hdd_image_file[index] = calloc(1, strlen(filename) + 1);
114   strcpy(hdd_image_file[index], filename);
115 }
116
117 void InitGayle(void) {
118   if (!hdd_image_file[0]) {
119     hdd_image_file[0] = calloc(1, 64);
120     sprintf(hdd_image_file[0], "hd0.img");
121   }
122
123   ide0 = ide_allocate("cf");
124   fd = open(hdd_image_file[0], O_RDWR);
125   if (fd == -1) {
126     printf("HDD Image %s failed open\n", hdd_image_file[0]);
127   } else {
128     ide_attach(ide0, 0, fd);
129     ide_reset_begin(ide0);
130     printf("HDD Image %s attached\n", hdd_image_file[0]);
131   }
132 }
133
134 uint8_t CheckIrq(void) {
135   uint8_t irq;
136
137   if (gayle_int & (1 << 7)) {
138     irq = ide0->drive->intrq;
139     //  if (irq==0)
140     //  printf("IDE IRQ: %x\n",irq);
141     return irq;
142   };
143   return 0;
144 }
145
146 void writeGayleB(unsigned int address, unsigned int value) {
147   if (address == GFEAT) {
148     ide_write8(ide0, ide_feature_w, value);
149     return;
150   }
151   if (address == GCMD) {
152     ide_write8(ide0, ide_command_w, value);
153     return;
154   }
155   if (address == GSECTCNT) {
156     ide_write8(ide0, ide_sec_count, value);
157     return;
158   }
159   if (address == GSECTNUM) {
160     ide_write8(ide0, ide_sec_num, value);
161     return;
162   }
163   if (address == GCYLLOW) {
164     ide_write8(ide0, ide_cyl_low, value);
165     return;
166   }
167   if (address == GCYLHIGH) {
168     ide_write8(ide0, ide_cyl_hi, value);
169     return;
170   }
171   if (address == GDEVHEAD) {
172     ide_write8(ide0, ide_dev_head, value);
173     return;
174   }
175   if (address == GCTRL) {
176     ide_write8(ide0, ide_devctrl_w, value);
177     return;
178   }
179
180   if (address == GIDENT) {
181     counter = 0;
182     // printf("Write Byte to Gayle Ident 0x%06x (0x%06x)\n",address,value);
183     return;
184   }
185
186   if (address == GIRQ) {
187     //   printf("Write Byte to Gayle GIRQ 0x%06x (0x%06x)\n",address,value);
188     gayle_irq = (gayle_irq & value) | (value & (GAYLE_IRQ_RESET | GAYLE_IRQ_BERR));
189
190     return;
191   }
192
193   if (address == GCS) {
194     printf("Write Byte to Gayle GCS 0x%06x (0x%06x)\n", address, value);
195     gayle_cs_mask = value & ~3;
196     gayle_cs &= ~3;
197     gayle_cs |= value & 3;
198     return;
199   }
200
201   if (address == GINT) {
202     printf("Write Byte to Gayle GINT 0x%06x (0x%06x)\n", address, value);
203     gayle_int = value;
204     return;
205   }
206
207   if (address == GCONF) {
208     printf("Write Byte to Gayle GCONF 0x%06x (0x%06x)\n", address, value);
209     gayle_cfg = value;
210     return;
211   }
212
213   if ((address & GAYLEMASK) == CLOCKBASE) {
214     if ((address & CLOCKMASK) >= 0x8000) {
215       if (cdtv_mode) {
216         cdtv_sram[(address & CLOCKMASK) - 0x8000] = value;
217       }
218       return;
219     }
220     put_rtc_byte(address, value, rtc_type);
221     return;
222   }
223
224   printf("Write Byte to Gayle Space 0x%06x (0x%06x)\n", address, value);
225 }
226
227 void writeGayle(unsigned int address, unsigned int value) {
228   if (address == GDATA) {
229     ide_write16(ide0, ide_data, value);
230     return;
231   }
232
233   if ((address & GAYLEMASK) == CLOCKBASE) {
234     if ((address & CLOCKMASK) >= 0x8000) {
235       if (cdtv_mode) {
236         ((short *) ((size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000)))[0] = htobe16(value);
237       }
238       return;
239     }
240     printf("Word write to RTC.\n");
241     put_rtc_byte(address, (value & 0xFF), rtc_type);
242     put_rtc_byte(address + 1, (value >> 8), rtc_type);
243     return;
244   }
245
246   printf("Write Word to Gayle Space 0x%06x (0x%06x)\n", address, value);
247 }
248
249 void writeGayleL(unsigned int address, unsigned int value) {
250   if ((address & GAYLEMASK) == CLOCKBASE) {
251     if ((address & CLOCKMASK) >= 0x8000) {
252       if (cdtv_mode) {
253         ((int *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0] = htobe32(value);
254       }
255       return;
256     }
257     printf("Longword write to RTC.\n");
258     put_rtc_byte(address, (value & 0xFF), rtc_type);
259     put_rtc_byte(address + 1, ((value & 0x0000FF00) >> 8), rtc_type);
260     put_rtc_byte(address + 2, ((value & 0x00FF0000) >> 16), rtc_type);
261     put_rtc_byte(address + 3, (value >> 24), rtc_type);
262     return;
263   }
264
265   printf("Write Long to Gayle Space 0x%06x (0x%06x)\n", address, value);
266 }
267
268 uint8_t readGayleB(unsigned int address) {
269   if (address == GERROR) {
270     return ide_read8(ide0, ide_error_r);
271   }
272   if (address == GSTATUS) {
273     return ide_read8(ide0, ide_status_r);
274   }
275
276   if (address == GSECTCNT) {
277     return ide_read8(ide0, ide_sec_count);
278   }
279
280   if (address == GSECTNUM) {
281     return ide_read8(ide0, ide_sec_num);
282   }
283
284   if (address == GCYLLOW) {
285     return ide_read8(ide0, ide_cyl_low);
286   }
287
288   if (address == GCYLHIGH) {
289     return ide_read8(ide0, ide_cyl_hi);
290   }
291
292   if (address == GDEVHEAD) {
293     return ide_read8(ide0, ide_dev_head);
294   }
295
296   if (address == GCTRL) {
297     return ide_read8(ide0, ide_altst_r);
298   }
299
300   if ((address & GAYLEMASK) == CLOCKBASE) {
301     if ((address & CLOCKMASK) >= 0x8000) {
302       if (cdtv_mode) {
303         return cdtv_sram[(address & CLOCKMASK) - 0x8000];
304       }
305       return 0;
306     }
307     return get_rtc_byte(address, rtc_type);
308   }
309
310   if (address == GIDENT) {
311     uint8_t val;
312     // printf("Read Byte from Gayle Ident 0x%06x (0x%06x)\n",address,counter);
313     if (counter == 0 || counter == 1 || counter == 3) {
314       val = 0x80;  // 80; to enable gayle
315     } else {
316       val = 0x00;
317     }
318     counter++;
319     return val;
320   }
321
322   if (address == GIRQ) {
323     //  printf("Read Byte From GIRQ Space 0x%06x\n",gayle_irq);
324
325     return 0x80;//gayle_irq;
326 /*
327     uint8_t irq;
328     irq = ide0->drive->intrq;
329
330     if (irq == 1) {
331       // printf("IDE IRQ: %x\n",irq);
332       return 0x80;  // gayle_irq;
333     }
334
335     return 0;
336 */ 
337  }
338
339   if (address == GCS) {
340     printf("Read Byte From GCS Space 0x%06x\n", 0x1234);
341     uint8_t v;
342     v = gayle_cs_mask | gayle_cs;
343     return v;
344   }
345
346   if (address == GINT) {
347     //  printf("Read Byte From GINT Space 0x%06x\n",gayle_int);
348     return gayle_int;
349   }
350
351   if (address == GCONF) {
352     printf("Read Byte From GCONF Space 0x%06x\n", gayle_cfg & 0x0f);
353     return gayle_cfg & 0x0f;
354   }
355
356   printf("Read Byte From Gayle Space 0x%06x\n", address);
357   return 0xFF;
358 }
359
360 uint16_t readGayle(unsigned int address) {
361   if (address == GDATA) {
362     uint16_t value;
363     value = ide_read16(ide0, ide_data);
364     //  value = (value << 8) | (value >> 8);
365     return value;
366   }
367
368   if ((address & GAYLEMASK) == CLOCKBASE) {
369     if ((address & CLOCKMASK) >= 0x8000) {
370       if (cdtv_mode) {
371
372         return be16toh( (( unsigned short *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0]);
373       }
374       return 0;
375     }
376     return ((get_rtc_byte(address, rtc_type) << 8) | (get_rtc_byte(address + 1, rtc_type)));
377   }
378
379   printf("Read Word From Gayle Space 0x%06x\n", address);
380   return 0x8000;
381 }
382
383 uint32_t readGayleL(unsigned int address) {
384   if ((address & GAYLEMASK) == CLOCKBASE) {
385     if ((address & CLOCKMASK) >= 0x8000) {
386       if (cdtv_mode) {
387         return be32toh( (( unsigned short *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0]);
388       }
389       return 0;
390     }
391     return ((get_rtc_byte(address, rtc_type) << 24) | (get_rtc_byte(address + 1, rtc_type) << 16) | (get_rtc_byte(address + 2, rtc_type) << 8) | (get_rtc_byte(address + 3, rtc_type)));
392   }
393
394   printf("Read Long From Gayle Space 0x%06x\n", address);
395   return 0x8000;
396 }