chore: use a more efficient decimal->bcd algorithm

This will not improve perf in any way because this code only gets run
one time a second orz
This commit is contained in:
Rekai Nyangadzayi Musuka 2023-02-21 23:18:37 -06:00
parent e654abfd1d
commit 93e48667cf
2 changed files with 11 additions and 19 deletions

View File

@ -294,12 +294,12 @@ pub const Clock = struct {
const now = DateTime.now(); const now = DateTime.now();
self.year = bcd(u8, @intCast(u8, now.date.year - 2000)); self.year = bcd(u8, @intCast(u8, now.date.year - 2000));
self.month = bcd(u5, now.date.month); self.month = @truncate(u5, bcd(now.date.month));
self.day = bcd(u6, now.date.day); self.day = @truncate(u6, bcd(now.date.day));
self.weekday = bcd(u3, (now.date.weekday() + 1) % 7); // API is Monday = 0, Sunday = 6. We want Sunday = 0, Saturday = 6 self.weekday = @truncate(u3, bcd((now.date.weekday() + 1) % 7)); // API is Monday = 0, Sunday = 6. We want Sunday = 0, Saturday = 6
self.hour = bcd(u6, now.time.hour); self.hour = @truncate(u6, bcd(now.time.hour));
self.minute = bcd(u7, now.time.minute); self.minute = @truncate(u7, bcd(now.time.minute));
self.second = bcd(u7, now.time.second); self.second = @truncate(u7, bcd(now.time.second));
} }
fn step(self: *Self, value: Data) u4 { fn step(self: *Self, value: Data) u4 {
@ -449,16 +449,8 @@ pub const Clock = struct {
} }
}; };
fn bcd(comptime T: type, value: u8) T { /// Converts an 8-bit unsigned integer to its BCD representation.
var input = value; /// Note: Algorithm only works for values between 0 and 99 inclusive.
var ret: u8 = 0; fn bcd(value: u8) u8 {
var shift: u3 = 0; return ((value / 10) << 4) + (value % 10);
while (input > 0) {
ret |= (input % 10) << (shift << 2);
shift += 1;
input /= 10;
}
return @truncate(T, ret);
} }

View File

@ -93,7 +93,7 @@ pub fn main() void {
gui.run(&cpu, &scheduler) catch |e| exitln("failed to run gui thread: {}", .{e}); gui.run(&cpu, &scheduler) catch |e| exitln("failed to run gui thread: {}", .{e});
} }
pub fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, &params, clap.parsers.default)) !FilePaths { fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, &params, clap.parsers.default)) !FilePaths {
const rom_path = romPath(result); const rom_path = romPath(result);
log.info("ROM path: {s}", .{rom_path}); log.info("ROM path: {s}", .{rom_path});