GBA: Replace RTC bitpacked structs with bitfields
Jeffrey Pfau jeffrey@endrift.com
Sat, 14 Mar 2015 00:56:24 -0700
2 files changed,
30 insertions(+),
38 deletions(-)
M
src/gba/hardware.c
→
src/gba/hardware.c
@@ -81,8 +81,8 @@
hw->rtc.bitsRead = 0; hw->rtc.bits = 0; hw->rtc.commandActive = 0; - hw->rtc.command.packed = 0; - hw->rtc.control.packed = 0x40; + hw->rtc.command = 0; + hw->rtc.control = 0x40; memset(hw->rtc.time, 0, sizeof(hw->rtc.time)); }@@ -146,7 +146,7 @@ } else {
if (hw->p2) { // GPIO direction should always != reading if (hw->dir1) { - if (hw->rtc.command.reading) { + if (RTCCommandDataIsReading(hw->rtc.command)) { GBALog(hw->p, GBA_LOG_GAME_ERROR, "Attempting to write to RTC while in read mode"); } ++hw->rtc.bitsRead;@@ -160,7 +160,7 @@ if (hw->rtc.bitsRead == 8) {
--hw->rtc.bytesRemaining; if (hw->rtc.bytesRemaining <= 0) { hw->rtc.commandActive = 0; - hw->rtc.command.reading = 0; + hw->rtc.command = RTCCommandDataClearReading(hw->rtc.command); } hw->rtc.bitsRead = 0; }@@ -169,7 +169,7 @@ } else {
hw->rtc.bitsRead = 0; hw->rtc.bytesRemaining = 0; hw->rtc.commandActive = 0; - hw->rtc.command.reading = 0; + hw->rtc.command = RTCCommandDataClearReading(hw->rtc.command); hw->rtc.transferStep = 0; } }@@ -180,16 +180,16 @@
void _rtcProcessByte(struct GBACartridgeHardware* hw) { --hw->rtc.bytesRemaining; if (!hw->rtc.commandActive) { - union RTCCommandData command; - command.packed = hw->rtc.bits; - if (command.magic == 0x06) { + RTCCommandData command; + command = hw->rtc.bits; + if (RTCCommandDataGetMagic(command) == 0x06) { hw->rtc.command = command; - hw->rtc.bytesRemaining = RTC_BYTES[hw->rtc.command.command]; + hw->rtc.bytesRemaining = RTC_BYTES[RTCCommandDataGetCommand(command)]; hw->rtc.commandActive = hw->rtc.bytesRemaining > 0; - switch (command.command) { + switch (RTCCommandDataGetCommand(command)) { case RTC_RESET: - hw->rtc.control.packed = 0; + hw->rtc.control = 0; break; case RTC_DATETIME: case RTC_TIME:@@ -203,12 +203,12 @@ } else {
GBALog(hw->p, GBA_LOG_WARN, "Invalid RTC command byte: %02X", hw->rtc.bits); } } else { - switch (hw->rtc.command.command) { + switch (RTCCommandDataGetCommand(hw->rtc.command)) { case RTC_CONTROL: - hw->rtc.control.packed = hw->rtc.bits; + hw->rtc.control = hw->rtc.bits; break; case RTC_FORCE_IRQ: - GBALog(hw->p, GBA_LOG_STUB, "Unimplemented RTC command %u", hw->rtc.command.command); + GBALog(hw->p, GBA_LOG_STUB, "Unimplemented RTC command %u", RTCCommandDataGetCommand(hw->rtc.command)); break; case RTC_RESET: case RTC_DATETIME:@@ -221,15 +221,15 @@ hw->rtc.bits = 0;
hw->rtc.bitsRead = 0; if (!hw->rtc.bytesRemaining) { hw->rtc.commandActive = 0; - hw->rtc.command.reading = 0; + hw->rtc.command = RTCCommandDataClearReading(hw->rtc.command); } } unsigned _rtcOutput(struct GBACartridgeHardware* hw) { uint8_t outputByte = 0; - switch (hw->rtc.command.command) { + switch (RTCCommandDataGetCommand(hw->rtc.command)) { case RTC_CONTROL: - outputByte = hw->rtc.control.packed; + outputByte = hw->rtc.control; break; case RTC_DATETIME: case RTC_TIME:@@ -262,7 +262,7 @@ hw->rtc.time[0] = _rtcBCD(date.tm_year - 100);
hw->rtc.time[1] = _rtcBCD(date.tm_mon + 1); hw->rtc.time[2] = _rtcBCD(date.tm_mday); hw->rtc.time[3] = _rtcBCD(date.tm_wday); - if (hw->rtc.control.hour24) { + if (RTCControlIsHour24(hw->rtc.control)) { hw->rtc.time[4] = _rtcBCD(date.tm_hour); } else { hw->rtc.time[4] = _rtcBCD(date.tm_hour % 12);
M
src/gba/hardware.h
→
src/gba/hardware.h
@@ -8,6 +8,8 @@ #define GBA_HARDWARE_H
#include "util/common.h" +#include "macros.h" + #define IS_GPIO_REGISTER(reg) ((reg) == GPIO_REG_DATA || (reg) == GPIO_REG_DIRECTION || (reg) == GPIO_REG_CONTROL) struct GBARotationSource {@@ -52,16 +54,10 @@ GPIO_WRITE_ONLY = 0,
GPIO_READ_WRITE = 1 }; -union RTCControl { - struct { - unsigned : 3; - unsigned minIRQ : 1; - unsigned : 2; - unsigned hour24 : 1; - unsigned poweroff : 1; - }; - uint8_t packed; -}; +DECL_BITFIELD(RTCControl, uint8_t); +DECL_BIT(RTCControl, MinIRQ, 3); +DECL_BIT(RTCControl, Hour24, 6); +DECL_BIT(RTCControl, Poweroff, 7); enum RTCCommand { RTC_RESET = 0,@@ -71,14 +67,10 @@ RTC_CONTROL = 4,
RTC_TIME = 6 }; -union RTCCommandData { - struct { - unsigned magic : 4; - enum RTCCommand command : 3; - unsigned reading : 1; - }; - uint8_t packed; -}; +DECL_BITFIELD(RTCCommandData, uint8_t); +DECL_BITS(RTCCommandData, Magic, 0, 4); +DECL_BITS(RTCCommandData, Command, 4, 3); +DECL_BIT(RTCCommandData, Reading, 7); struct GBARTC { int bytesRemaining;@@ -86,8 +78,8 @@ int transferStep;
int bitsRead; int bits; int commandActive; - union RTCCommandData command; - union RTCControl control; + RTCCommandData command; + RTCControl control; uint8_t time[7]; } __attribute__((packed));