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

View File

@ -93,7 +93,7 @@ pub fn main() void {
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);
log.info("ROM path: {s}", .{rom_path});