]> git.sesse.net Git - pistorm/blob - Gayle.c
36c7de2ac3c0929cea0bacaf09892286053cf3bc
[pistorm] / 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 <stdlib.h>
17 #include <unistd.h>
18 #include "ide.h"
19
20 #define CLOCKBASE 0xDC0000
21
22 //#define GSTATUS 0xda201c
23 //#define GCLOW   0xda2010
24 //#define GDH   0xda2018
25
26 // Gayle Addresses
27
28 // Gayle IDE Reads
29 #define GERROR 0xda2004   // Error
30 #define GSTATUS 0xda201c  // Status
31 // Gayle IDE Writes
32 #define GFEAT 0xda2004  // Write : Feature
33 #define GCMD 0xda201c   // Write : Command
34 // Gayle IDE RW
35 #define GDATA 0xda2000     // Data
36 #define GSECTCNT 0xda2008  // SectorCount
37 #define GSECTNUM 0xda200c  // SectorNumber
38 #define GCYLLOW 0xda2010   // CylinderLow
39 #define GCYLHIGH 0xda2014  // CylinderHigh
40 #define GDEVHEAD 0xda2018  // Device/Head
41 #define GCTRL 0xda3018     // Control
42 // Gayle Ident
43 #define GIDENT 0xDE1000
44
45 // Gayle IRQ/CC
46 #define GCS 0xDA8000   // Card Control
47 #define GIRQ 0xDA9000  // IRQ
48 #define GINT 0xDAA000  // Int enable
49 #define GCONF 0xDAB00  // Gayle Config
50
51 /* DA8000 */
52 #define GAYLE_CS_IDE 0x80   /* IDE int status */
53 #define GAYLE_CS_CCDET 0x40 /* credit card detect */
54 #define GAYLE_CS_BVD1 0x20  /* battery voltage detect 1 */
55 #define GAYLE_CS_SC 0x20    /* credit card status change */
56 #define GAYLE_CS_BVD2 0x10  /* battery voltage detect 2 */
57 #define GAYLE_CS_DA 0x10    /* digital audio */
58 #define GAYLE_CS_WR 0x08    /* write enable (1 == enabled) */
59 #define GAYLE_CS_BSY 0x04   /* credit card busy */
60 #define GAYLE_CS_IRQ 0x04   /* interrupt request */
61 #define GAYLE_CS_DAEN 0x02  /* enable digital audio */
62 #define GAYLE_CS_DIS 0x01   /* disable PCMCIA slot */
63
64 /* DA9000 */
65 #define GAYLE_IRQ_IDE 0x80
66 #define GAYLE_IRQ_CCDET 0x40 /* credit card detect */
67 #define GAYLE_IRQ_BVD1 0x20  /* battery voltage detect 1 */
68 #define GAYLE_IRQ_SC 0x20    /* credit card status change */
69 #define GAYLE_IRQ_BVD2 0x10  /* battery voltage detect 2 */
70 #define GAYLE_IRQ_DA 0x10    /* digital audio */
71 #define GAYLE_IRQ_WR 0x08    /* write enable (1 == enabled) */
72 #define GAYLE_IRQ_BSY 0x04   /* credit card busy */
73 #define GAYLE_IRQ_IRQ 0x04   /* interrupt request */
74 #define GAYLE_IRQ_RESET 0x02 /* reset machine after CCDET change */
75 #define GAYLE_IRQ_BERR 0x01  /* generate bus error after CCDET change */
76
77 /* DAA000 */
78 #define GAYLE_INT_IDE 0x80     /* IDE interrupt enable */
79 #define GAYLE_INT_CCDET 0x40   /* credit card detect change enable */
80 #define GAYLE_INT_BVD1 0x20    /* battery voltage detect 1 change enable */
81 #define GAYLE_INT_SC 0x20      /* credit card status change enable */
82 #define GAYLE_INT_BVD2 0x10    /* battery voltage detect 2 change enable */
83 #define GAYLE_INT_DA 0x10      /* digital audio change enable */
84 #define GAYLE_INT_WR 0x08      /* write enable change enabled */
85 #define GAYLE_INT_BSY 0x04     /* credit card busy */
86 #define GAYLE_INT_IRQ 0x04     /* credit card interrupt request */
87 #define GAYLE_INT_BVD_LEV 0x02 /* BVD int level, 0=lev2,1=lev6 */
88 #define GAYLE_INT_BSY_LEV 0x01 /* BSY int level, 0=lev2,1=lev6 */
89
90 int counter;
91 static uint8_t gayle_irq, gayle_int, gayle_cs, gayle_cs_mask, gayle_cfg;
92 static struct ide_controller *ide0;
93 int fd;
94
95 void InitGayle(void) {
96   ide0 = ide_allocate("cf");
97   fd = open("hd0.img", O_RDWR);
98   if (fd == -1) {
99     printf("HDD Image hd0.image failed open\n");
100   } else {
101     ide_attach(ide0, 0, fd);
102     ide_reset_begin(ide0);
103     printf("HDD Image hd0.image attached\n");
104   }
105 }
106
107 uint8_t CheckIrq(void) {
108   uint8_t irq;
109
110   if (gayle_int & (1 << 7)) {
111     irq = ide0->drive->intrq;
112     //  if (irq==0)
113     //  printf("IDE IRQ: %x\n",irq);
114     return irq;
115   };
116   return 0;
117 }
118
119 void writeGayleB(unsigned int address, unsigned int value) {
120   if (address == GFEAT) {
121     ide_write8(ide0, ide_feature_w, value);
122     return;
123   }
124   if (address == GCMD) {
125     ide_write8(ide0, ide_command_w, value);
126     return;
127   }
128   if (address == GSECTCNT) {
129     ide_write8(ide0, ide_sec_count, value);
130     return;
131   }
132   if (address == GSECTNUM) {
133     ide_write8(ide0, ide_sec_num, value);
134     return;
135   }
136   if (address == GCYLLOW) {
137     ide_write8(ide0, ide_cyl_low, value);
138     return;
139   }
140   if (address == GCYLHIGH) {
141     ide_write8(ide0, ide_cyl_hi, value);
142     return;
143   }
144   if (address == GDEVHEAD) {
145     ide_write8(ide0, ide_dev_head, value);
146     return;
147   }
148   if (address == GCTRL) {
149     ide_write8(ide0, ide_devctrl_w, value);
150     return;
151   }
152
153   if (address == GIDENT) {
154     counter = 0;
155     // printf("Write Byte to Gayle Ident 0x%06x (0x%06x)\n",address,value);
156     return;
157   }
158
159   if (address == GIRQ) {
160     //   printf("Write Byte to Gayle GIRQ 0x%06x (0x%06x)\n",address,value);
161     gayle_irq = (gayle_irq & value) | (value & (GAYLE_IRQ_RESET | GAYLE_IRQ_BERR));
162
163     return;
164   }
165
166   if (address == GCS) {
167     printf("Write Byte to Gayle GCS 0x%06x (0x%06x)\n", address, value);
168     gayle_cs_mask = value & ~3;
169     gayle_cs &= ~3;
170     gayle_cs |= value & 3;
171     return;
172   }
173
174   if (address == GINT) {
175     printf("Write Byte to Gayle GINT 0x%06x (0x%06x)\n", address, value);
176     gayle_int = value;
177     return;
178   }
179
180   if (address == GCONF) {
181     printf("Write Byte to Gayle GCONF 0x%06x (0x%06x)\n", address, value);
182     gayle_cfg = value;
183     return;
184   }
185
186   printf("Write Byte to Gayle Space 0x%06x (0x%06x)\n", address, value);
187 }
188
189 void writeGayle(unsigned int address, unsigned int value) {
190   if (address == GDATA) {
191     ide_write16(ide0, ide_data, value);
192     return;
193   }
194
195   printf("Write Word to Gayle Space 0x%06x (0x%06x)\n", address, value);
196 }
197
198 void writeGayleL(unsigned int address, unsigned int value) {
199   printf("Write Long to Gayle Space 0x%06x (0x%06x)\n", address, value);
200 }
201
202 uint8_t readGayleB(unsigned int address) {
203   if (address == GERROR) {
204     return ide_read8(ide0, ide_error_r);
205   }
206   if (address == GSTATUS) {
207     return ide_read8(ide0, ide_status_r);
208   }
209
210   if (address == GSECTCNT) {
211     return ide_read8(ide0, ide_sec_count);
212   }
213
214   if (address == GSECTNUM) {
215     return ide_read8(ide0, ide_sec_num);
216   }
217
218   if (address == GCYLLOW) {
219     return ide_read8(ide0, ide_cyl_low);
220   }
221
222   if (address == GCYLHIGH) {
223     return ide_read8(ide0, ide_cyl_hi);
224   }
225
226   if (address == GDEVHEAD) {
227     return ide_read8(ide0, ide_dev_head);
228   }
229
230   if (address == GCTRL) {
231     return ide_read8(ide0, ide_altst_r);
232   }
233
234   if (address == GIDENT) {
235     uint8_t val;
236     // printf("Read Byte from Gayle Ident 0x%06x (0x%06x)\n",address,counter);
237     if (counter == 0 || counter == 1 || counter == 3) {
238       val = 0x80;  // 80; to enable gayle
239     } else {
240       val = 0x00;
241     }
242     counter++;
243     return val;
244   }
245
246   if (address == GIRQ) {
247     //  printf("Read Byte From GIRQ Space 0x%06x\n",gayle_irq);
248
249     return 0x80;//gayle_irq;
250 /*
251     uint8_t irq;
252     irq = ide0->drive->intrq;
253
254     if (irq == 1) {
255       // printf("IDE IRQ: %x\n",irq);
256       return 0x80;  // gayle_irq;
257     }
258
259     return 0;
260 */ 
261  }
262
263   if (address == GCS) {
264     printf("Read Byte From GCS Space 0x%06x\n", 0x1234);
265     uint8_t v;
266     v = gayle_cs_mask | gayle_cs;
267     return v;
268   }
269
270   if (address == GINT) {
271     //  printf("Read Byte From GINT Space 0x%06x\n",gayle_int);
272     return gayle_int;
273   }
274
275   if (address == GCONF) {
276     printf("Read Byte From GCONF Space 0x%06x\n", gayle_cfg & 0x0f);
277     return gayle_cfg & 0x0f;
278   }
279
280   printf("Read Byte From Gayle Space 0x%06x\n", address);
281   return 0xFF;
282 }
283
284 uint16_t readGayle(unsigned int address) {
285   if (address == GDATA) {
286     uint16_t value;
287     value = ide_read16(ide0, ide_data);
288     //  value = (value << 8) | (value >> 8);
289     return value;
290   }
291
292   printf("Read Word From Gayle Space 0x%06x\n", address);
293   return 0x8000;
294 }
295
296 uint32_t readGayleL(unsigned int address) {
297   printf("Read Long From Gayle Space 0x%06x\n", address);
298   return 0x8000;
299 }