3 // Originally based on Omega's Gayle emulation,
4 // created by Matt Parsons on 06/03/2019.
5 // Copyright © 2019 Matt Parsons. All rights reserved.
17 #include "platforms/shared/rtc.h"
18 #include "config_file/config_file.h"
20 #include "amiga-registers.h"
34 uint8_t ide_feature_w = 0, ide_command_w = 0, ide_sec_count = 0, ide_sec_num = 0, idewrite8 = 0, ide_cyl_hi = 0, ide_dev_head = 0;
35 uint8_t ide_devctrl_w = 0, ide_cyl_low = 0, ide_error_r = 0, ide_status_r = 0, ide_altst_r = 0, ide_data = 0;
37 uint8_t ide_read8(uint8_t *dummy, uint8_t ide_action) { if (dummy || ide_action) {}; return 0; }
38 uint16_t ide_read16(uint8_t *dummy, uint8_t ide_action) { if (dummy || ide_action) {}; return 0; }
40 void ide_write8(uint8_t *dummy, uint8_t ide_action, uint8_t value) { if (dummy || ide_action || value) {}; }
41 void ide_write16(uint8_t *dummy, uint8_t ide_action, uint16_t value) { if (dummy || ide_action || value) {}; }
42 void ide_reset_begin(uint8_t *dummy) { if (dummy) {}; }
44 uint8_t *ide_allocate(const char *name) { if (name) {}; return NULL; }
46 void ide_attach_hdf(uint8_t *dummy, uint32_t idx, uint32_t fd) {
47 if (dummy || idx || fd) {};
48 printf("[!!!IDE] No IDE emulation layer available, HDF image not attached.\n");
52 void ide_attach(uint8_t *dummy, uint32_t idx, uint32_t fd) {
53 if (dummy || idx || fd) {};
54 printf("[!!!IDE] No IDE emulation layer available, image not mounted.\n");
58 static struct ide_controller *ide0 = NULL;
63 uint8_t ramsey_cfg = 0x08;
64 static uint8_t ramsey_id = RAMSEY_REV7;
67 static uint8_t gayle_irq, gayle_cs, gayle_cs_mask, gayle_cfg;
70 uint8_t rtc_type = RTC_TYPE_RICOH;
72 char *hdd_image_file[GAYLE_MAX_HARDFILES];
74 uint8_t cdtv_mode = 0;
75 unsigned char cdtv_sram[32 * SIZE_KILO];
77 uint8_t gayle_a4k = 0xA0;
78 uint16_t gayle_a4k_irq = 0;
79 uint8_t gayle_a4k_int = 0;
80 uint8_t gayle_int = 0;
82 uint32_t gayle_ide_mask = ~GDATA;
83 uint32_t gayle_ide_base = GDATA;
84 uint8_t gayle_ide_enabled = 1;
85 uint8_t gayle_emulation_enabled = 1;
86 uint8_t gayle_ide_adj = 0;
88 void adjust_gayle_4000() {
89 gayle_ide_base = GAYLE_IDE_BASE_A4000;
94 void adjust_gayle_1200() {
97 void set_hard_drive_image_file_amiga(uint8_t index, char *filename) {
98 if (hdd_image_file[index] != NULL)
99 free(hdd_image_file[index]);
100 hdd_image_file[index] = calloc(1, strlen(filename) + 1);
101 strcpy(hdd_image_file[index], filename);
104 void InitGayle(void) {
105 uint8_t num_ide_drives = 0;
107 for (int i = 0; i < GAYLE_MAX_HARDFILES; i++) {
108 if (hdd_image_file[i]) {
109 fd = open(hdd_image_file[i], O_RDWR);
112 ide0 = ide_allocate("cf");
116 printf("[HDD%d] HDD Image %s failed open\n", i, hdd_image_file[i]);
118 printf("[HDD%d] Attaching HDD image %s.\n", i, hdd_image_file[i]);
119 if (strcmp(hdd_image_file[i] + (strlen(hdd_image_file[i]) - 3), "img") != 0) {
120 printf("No header present on HDD image %s.\n", hdd_image_file[i]);
121 ide_attach_hdf(ide0, i, fd);
125 printf("Attaching HDD image with header.\n");
126 ide_attach(ide0, i, fd);
129 printf("[HDD%d] HDD Image %s attached\n", i, hdd_image_file[i]);
134 ide_reset_begin(ide0);
136 if (num_ide_drives == 0) {
137 // No IDE drives mounted, disable IDE component of Gayle
138 printf("No IDE drives mounted, disabling Gayle IDE component.\n");
139 gayle_ide_enabled = 0;
143 static uint8_t ide_action = 0;
145 void writeGayleB(unsigned int address, unsigned int value) {
147 if (address >= gayle_ide_base) {
148 switch ((address - gayle_ide_base) - gayle_ide_adj) {
150 //printf("Write to GFEAT: %.2X.\n", value);
151 ide_action = ide_feature_w;
154 //printf("Write to GCMD: %.2X.\n", value);
155 ide_action = ide_command_w;
157 case GSECTCOUNT_OFFSET:
158 ide_action = ide_sec_count;
160 case GSECTNUM_OFFSET:
161 ide_action = ide_sec_num;
164 ide_action = ide_cyl_low;
166 case GCYLHIGH_OFFSET:
167 ide_action = ide_cyl_hi;
169 case GDEVHEAD_OFFSET:
170 //printf("Write to GDEVHEAD: %.2X.\n", value);
171 ide_action = ide_dev_head;
174 //printf("Write to GCTRL: %.2X.\n", value);
175 ide_action = ide_devctrl_w;
177 case GIRQ_4000_OFFSET:
178 gayle_a4k_irq = value;
181 gayle_irq = (gayle_irq & value) | (value & (GAYLE_IRQ_RESET | GAYLE_IRQ_BERR));
186 ide_write8(ide0, ide_action, value);
194 printf("Write bye to A4000 Gayle: %.2X\n", value);
198 //printf("Write to GIDENT: %d\n", value);
202 //printf("Write to GCONF: %d\n", gayle_cfg);
206 ramsey_cfg = value & 0x0F;
212 gayle_cs_mask = value & ~3;
214 gayle_cs |= value & 3;
215 printf("Write to GCS: %d\n", gayle_cs);
216 //ide0->selected = gayle_cs;
220 if ((address & GAYLEMASK) == CLOCKBASE) {
221 if ((address & CLOCKMASK) >= 0x8000) {
223 //printf("[CDTV] BYTE write to SRAM @%.8X (%.8X): %.2X\n", (address & CLOCKMASK) - 0x8000, address, value);
224 cdtv_sram[(address & CLOCKMASK) - 0x8000] = value;
228 //printf("Byte write to RTC.\n");
229 put_rtc_byte(address, value, rtc_type);
233 DEBUG("Write Byte to Gayle Space 0x%06x (0x%06x)\n", address, value);
236 void writeGayle(unsigned int address, unsigned int value) {
238 if (address - gayle_ide_base == GDATA_OFFSET) {
239 ide_write16(ide0, ide_data, value);
243 if (address == GIRQ_A4000) {
244 gayle_a4k_irq = value;
249 if ((address & GAYLEMASK) == CLOCKBASE) {
250 if ((address & CLOCKMASK) >= 0x8000) {
252 //printf("[CDTV] WORD write to SRAM @%.8X (%.8X): %.4X\n", (address & CLOCKMASK) - 0x8000, address, htobe16(value));
253 ((short *) ((size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000)))[0] = htobe16(value);
257 //printf("Word write to RTC.\n");
258 put_rtc_byte(address + 1, (value & 0xFF), rtc_type);
259 put_rtc_byte(address, (value >> 8), rtc_type);
263 DEBUG("Write Word to Gayle Space 0x%06x (0x%06x)\n", address, value);
266 void writeGayleL(unsigned int address, unsigned int value) {
267 if ((address & GAYLEMASK) == CLOCKBASE) {
268 if ((address & CLOCKMASK) >= 0x8000) {
270 //printf("[CDTV] LONGWORD write to SRAM @%.8X (%.8X): %.8X\n", (address & CLOCKMASK) - 0x8000, address, htobe32(value));
271 ((int *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0] = htobe32(value);
275 //printf("Longword write to RTC.\n");
276 put_rtc_byte(address + 3, (value & 0xFF), rtc_type);
277 put_rtc_byte(address + 2, ((value & 0x0000FF00) >> 8), rtc_type);
278 put_rtc_byte(address + 1, ((value & 0x00FF0000) >> 16), rtc_type);
279 put_rtc_byte(address, (value >> 24), rtc_type);
283 DEBUG("Write Long to Gayle Space 0x%06x (0x%06x)\n", address, value);
286 uint8_t readGayleB(unsigned int address) {
288 uint8_t ide_action = 0, ide_val = 0;
290 if (address >= gayle_ide_base) {
291 switch ((address - gayle_ide_base) - gayle_ide_adj) {
293 ide_action = ide_error_r;
296 ide_action = ide_status_r;
298 case GSECTCOUNT_OFFSET:
299 ide_action = ide_sec_count;
301 case GSECTNUM_OFFSET:
302 ide_action = ide_sec_num;
305 ide_action = ide_cyl_low;
307 case GCYLHIGH_OFFSET:
308 ide_action = ide_cyl_hi;
310 case GDEVHEAD_OFFSET:
311 ide_action = ide_dev_head;
314 ide_action = ide_altst_r;
316 case GIRQ_4000_OFFSET:
319 //gayle_irq = (gayle_irq & value) | (value & (GAYLE_IRQ_RESET | GAYLE_IRQ_BERR));
323 ide_val = ide_read8(ide0, ide_action);
331 if (counter == 0 || counter == 1 || counter == 3) {
332 val = 0x80; // 80; to enable gayle
337 //printf("Read from GIDENT: %.2X.\n", val);
343 //printf("Read from GCONF: %d\n", gayle_cfg & 0x0F);
344 return gayle_cfg & 0x0f;
347 v = gayle_cs_mask | gayle_cs;
348 printf("Read from GCS: %d\n", v);
351 // This seems incorrect, GARY_REG3 is the same as GIDENT, and the A4000
352 // service manual says that Gary is accessible in the address range $DFC000 to $DFFFFF.
356 return gary_cfg[address - GARY_REG0];
361 return gary_cfg[address - GARY_REG4];
366 case GARY_REG5: { // This makes no sense.
368 if (counter == 0 || counter == 1 || counter == 3) {
369 val = 0x80; // 80; to enable GARY
377 // This can't be correct, as this is the same address as GDEVHEAD on the A4000 Gayle.
378 //printf("Read Byte from Gayle A4k: %.2X\n", gayle_a4k);
383 if ((address & GAYLEMASK) == CLOCKBASE) {
384 if ((address & CLOCKMASK) >= 0x8000) {
386 //printf("[CDTV] BYTE read from SRAM @%.8X (%.8X): %.2X\n", (address & CLOCKMASK) - 0x8000, address, cdtv_sram[(address & CLOCKMASK) - 0x8000]);
387 return cdtv_sram[(address & CLOCKMASK) - 0x8000];
391 //printf("Byte read from RTC.\n");
392 return get_rtc_byte(address, rtc_type);
395 DEBUG("Read Byte From Gayle Space 0x%06x\n", address);
399 uint16_t readGayle(unsigned int address) {
401 if (address - gayle_ide_base == GDATA_OFFSET) {
403 value = ide_read16(ide0, ide_data);
404 // value = (value << 8) | (value >> 8);
408 if (address == GIRQ_A4000) {
409 gayle_a4k_irq = 0x8000;
414 if ((address & GAYLEMASK) == CLOCKBASE) {
415 if ((address & CLOCKMASK) >= 0x8000) {
417 //printf("[CDTV] WORD read from SRAM @%.8X (%.8X): %.4X\n", (address & CLOCKMASK) - 0x8000, address, be16toh( (( unsigned short *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0]));
418 return be16toh( (( unsigned short *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0]);
422 //printf("Word read from RTC.\n");
423 return ((get_rtc_byte(address, rtc_type) << 8) | (get_rtc_byte(address + 1, rtc_type)));
426 DEBUG("Read Word From Gayle Space 0x%06x\n", address);
430 uint32_t readGayleL(unsigned int address) {
431 if ((address & GAYLEMASK) == CLOCKBASE) {
432 if ((address & CLOCKMASK) >= 0x8000) {
434 //printf("[CDTV] LONGWORD read from SRAM @%.8X (%.8X): %.8X\n", (address & CLOCKMASK) - 0x8000, address, be32toh( (( unsigned short *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0]));
435 return be32toh( (( unsigned int *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0]);
439 //printf("Longword read from RTC.\n");
440 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)));
443 DEBUG("Read Long From Gayle Space 0x%06x\n", address);