From ef9d05f9de811eb674657e8832bb6070f1f7e1ee Mon Sep 17 00:00:00 2001 From: beeanyew Date: Sat, 2 Jan 2021 02:50:28 +0100 Subject: [PATCH] Fix some RTC and CDTV NVRAM stuff --- platforms/amiga/Gayle.c | 29 +++++++++++++++++++---------- platforms/amiga/amiga-platform.c | 9 +++++++-- platforms/shared/rtc.c | 15 ++++++++++++++- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/platforms/amiga/Gayle.c b/platforms/amiga/Gayle.c index a8d4657..1bbba36 100644 --- a/platforms/amiga/Gayle.c +++ b/platforms/amiga/Gayle.c @@ -218,10 +218,12 @@ void writeGayleB(unsigned int address, unsigned int value) { if ((address & GAYLEMASK) == CLOCKBASE) { if ((address & CLOCKMASK) >= 0x8000) { if (cdtv_mode) { + //printf("[CDTV] BYTE write to SRAM @%.8X (%.8X): %.2X\n", (address & CLOCKMASK) - 0x8000, address, value); cdtv_sram[(address & CLOCKMASK) - 0x8000] = value; } return; } + //printf("Byte write to RTC.\n"); put_rtc_byte(address, value, rtc_type); return; } @@ -238,13 +240,14 @@ void writeGayle(unsigned int address, unsigned int value) { if ((address & GAYLEMASK) == CLOCKBASE) { if ((address & CLOCKMASK) >= 0x8000) { if (cdtv_mode) { + //printf("[CDTV] WORD write to SRAM @%.8X (%.8X): %.4X\n", (address & CLOCKMASK) - 0x8000, address, htobe16(value)); ((short *) ((size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000)))[0] = htobe16(value); } return; } - printf("Word write to RTC.\n"); - put_rtc_byte(address, (value & 0xFF), rtc_type); - put_rtc_byte(address + 1, (value >> 8), rtc_type); + //printf("Word write to RTC.\n"); + put_rtc_byte(address + 1, (value & 0xFF), rtc_type); + put_rtc_byte(address, (value >> 8), rtc_type); return; } @@ -255,15 +258,16 @@ void writeGayleL(unsigned int address, unsigned int value) { if ((address & GAYLEMASK) == CLOCKBASE) { if ((address & CLOCKMASK) >= 0x8000) { if (cdtv_mode) { + //printf("[CDTV] LONGWORD write to SRAM @%.8X (%.8X): %.8X\n", (address & CLOCKMASK) - 0x8000, address, htobe32(value)); ((int *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0] = htobe32(value); } return; } - printf("Longword write to RTC.\n"); - put_rtc_byte(address, (value & 0xFF), rtc_type); - put_rtc_byte(address + 1, ((value & 0x0000FF00) >> 8), rtc_type); - put_rtc_byte(address + 2, ((value & 0x00FF0000) >> 16), rtc_type); - put_rtc_byte(address + 3, (value >> 24), rtc_type); + //printf("Longword write to RTC.\n"); + put_rtc_byte(address + 3, (value & 0xFF), rtc_type); + put_rtc_byte(address + 2, ((value & 0x0000FF00) >> 8), rtc_type); + put_rtc_byte(address + 1, ((value & 0x00FF0000) >> 16), rtc_type); + put_rtc_byte(address, (value >> 24), rtc_type); return; } @@ -305,10 +309,12 @@ uint8_t readGayleB(unsigned int address) { if ((address & GAYLEMASK) == CLOCKBASE) { if ((address & CLOCKMASK) >= 0x8000) { if (cdtv_mode) { + //printf("[CDTV] BYTE read from SRAM @%.8X (%.8X): %.2X\n", (address & CLOCKMASK) - 0x8000, address, cdtv_sram[(address & CLOCKMASK) - 0x8000]); return cdtv_sram[(address & CLOCKMASK) - 0x8000]; } return 0; } + //printf("Byte read from RTC.\n"); return get_rtc_byte(address, rtc_type); } @@ -373,11 +379,12 @@ uint16_t readGayle(unsigned int address) { if ((address & GAYLEMASK) == CLOCKBASE) { if ((address & CLOCKMASK) >= 0x8000) { if (cdtv_mode) { - + //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])); return be16toh( (( unsigned short *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0]); } return 0; } + //printf("Word read from RTC.\n"); return ((get_rtc_byte(address, rtc_type) << 8) | (get_rtc_byte(address + 1, rtc_type))); } @@ -389,10 +396,12 @@ uint32_t readGayleL(unsigned int address) { if ((address & GAYLEMASK) == CLOCKBASE) { if ((address & CLOCKMASK) >= 0x8000) { if (cdtv_mode) { - return be32toh( (( unsigned short *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0]); + //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])); + return be32toh( (( unsigned int *) (size_t)(cdtv_sram + (address & CLOCKMASK) - 0x8000))[0]); } return 0; } + //printf("Longword read from RTC.\n"); 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))); } diff --git a/platforms/amiga/amiga-platform.c b/platforms/amiga/amiga-platform.c index 12d80a4..5eac5d7 100644 --- a/platforms/amiga/amiga-platform.c +++ b/platforms/amiga/amiga-platform.c @@ -247,14 +247,19 @@ void setvar_amiga(char *var, char *val) { set_hard_drive_image_file_amiga(0, val); } if (strcmp(var, "cdtv") == 0) { + printf("[AMIGA] CDTV mode enabled.\n"); cdtv_mode = 1; } - if (strcmp(var, "rtctype") == 0) { + if (strcmp(var, "rtc_type") == 0) { if (val && strlen(val) != 0) { if (strcmp(val, "msm") == 0) { + printf("[AMIGA] RTC type set to MSM.\n"); rtc_type = RTC_TYPE_MSM; } - rtc_type = RTC_TYPE_RICOH; + else { + printf("[AMIGA] RTC type set to Ricoh.\n"); + rtc_type = RTC_TYPE_RICOH; + } } } } diff --git a/platforms/shared/rtc.c b/platforms/shared/rtc.c index dd5edad..2e07493 100644 --- a/platforms/shared/rtc.c +++ b/platforms/shared/rtc.c @@ -7,9 +7,14 @@ static unsigned char rtc_mystery_reg[3]; unsigned char ricoh_memory[0x0F]; unsigned char ricoh_alarm[0x0F]; -void put_rtc_byte(uint32_t address_, uint8_t value, uint8_t rtc_type) { +void put_rtc_byte(uint32_t address_, uint8_t value_, uint8_t rtc_type) { uint32_t address = address_ & 0x3F; + uint8_t value = (value_ & 0x0F); + address >>= 2; + + //printf("Wrote byte %.2X.\n", address); + if (rtc_type == RTC_TYPE_MSM) { switch(address) { case 0x0D: @@ -75,6 +80,12 @@ void put_rtc_byte(uint32_t address_, uint8_t value, uint8_t rtc_type) { uint8_t get_rtc_byte(uint32_t address_, uint8_t rtc_type) { uint32_t address = address_ & 0x3F; + + if ((address & 3) == 2 || (address & 3) == 0) { + //printf("Garbage byte read.\n"); + return 0; + } + address >>= 2; time_t t; time(&t); @@ -92,6 +103,8 @@ uint8_t get_rtc_byte(uint32_t address_, uint8_t rtc_type) { } } + //printf("Read byte %.2X.\n", address); + switch (address) { case 0x00: // Seconds low? return rtc_time->tm_sec % 10;