From d9776e99d3766068bf1bca2a5d08b7af6b68e634 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:13:00 -0300 Subject: [PATCH] chore: import datetime library + default time for RTC --- .gitmodules | 3 +++ build.zig | 4 +++- lib/zig-datetime | 1 + src/core/bus/GamePak.zig | 45 ++++++++++++++++++++++++++++++++-------- 4 files changed, 43 insertions(+), 10 deletions(-) create mode 160000 lib/zig-datetime diff --git a/.gitmodules b/.gitmodules index 9dda538..2c14e46 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "lib/known-folders"] path = lib/known-folders url = https://github.com/ziglibs/known-folders +[submodule "lib/zig-datetime"] + path = lib/zig-datetime + url = https://github.com/frmdstryr/zig-datetime diff --git a/build.zig b/build.zig index 5cdd799..e93d00c 100644 --- a/build.zig +++ b/build.zig @@ -13,10 +13,12 @@ pub fn build(b: *std.build.Builder) void { const mode = b.standardReleaseOptions(); const exe = b.addExecutable("zba", "src/main.zig"); - // Known Folders (%APPDATA%, XDG, etc.) exe.addPackagePath("known_folders", "lib/known-folders/known-folders.zig"); + // DateTime Library + exe.addPackagePath("datetime", "lib/zig-datetime/src/main.zig"); + // Bitfield type from FlorenceOS: https://github.com/FlorenceOS/ // exe.addPackage(.{ .name = "bitfield", .path = .{ .path = "lib/util/bitfield.zig" } }); exe.addPackagePath("bitfield", "lib/util/bitfield.zig"); diff --git a/lib/zig-datetime b/lib/zig-datetime new file mode 160000 index 0000000..5ec1c36 --- /dev/null +++ b/lib/zig-datetime @@ -0,0 +1 @@ +Subproject commit 5ec1c36cf3791b3c6c5b330357bdb6feb93979ba diff --git a/src/core/bus/GamePak.zig b/src/core/bus/GamePak.zig index 031dbf1..ec2f28b 100644 --- a/src/core/bus/GamePak.zig +++ b/src/core/bus/GamePak.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const DateTime = @import("datetime").datetime.Datetime; const Arm7tdmi = @import("../cpu.zig").Arm7tdmi; const Bit = @import("bitfield").Bit; @@ -326,7 +327,7 @@ const Clock = struct { year: u8, month: u5, day: u6, - day_of_week: u3, + weekday: u3, hour: u6, minute: u7, second: u7, @@ -371,7 +372,7 @@ const Clock = struct { 0 => clock.year >> idx, 1 => @as(u8, clock.month) >> idx, 2 => @as(u8, clock.day) >> idx, - 3 => @as(u8, clock.day_of_week) >> idx, + 3 => @as(u8, clock.weekday) >> idx, // Time 4 => @as(u8, clock.hour) >> idx, @@ -490,18 +491,30 @@ const Clock = struct { .reader = .{ .i = 0, .count = 0 }, .state = .Idle, .cnt = .{ .raw = 0 }, - .year = 0, - .month = 0, - .day = 0, - .day_of_week = 0, - .hour = 0, - .minute = 0, - .second = 0, + .year = 0x01, + .month = 0x6, + .day = 0x13, + .weekday = 0x3, + .hour = 0x23, + .minute = 0x59, + .second = 0x59, .cpu = cpu, .gpio = gpio, // Can't use Arm7tdmi ptr b/c not initialized yet }; } + fn updateRealTime(self: *This) void { + const now = DateTime.now(); + + self.year = toBcd(u8, @intCast(u8, now.date.year - 2000)); + self.month = toBcd(u5, now.date.month); + self.day = toBcd(u3, now.date.day); + self.weekday = toBcd(u3, (now.date.weekday() + 1) % 7); // API is Monday = 0, Sunday = 6. We want Sunday = 0, Saturday = 6 + self.hour = toBcd(u6, now.time.hour); + self.minute = toBcd(u7, now.time.minute); + self.second = toBcd(u7, now.time.second); + } + fn step(self: *This, value: Data) u4 { const cache: Data = .{ .raw = self.gpio.data }; @@ -648,3 +661,17 @@ const Clock = struct { } } }; + +fn toBcd(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); +}