feat: put RTC Sync on Scheduler

TODO: Database to see what games have what GPIO devices
This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:13:00 -03:00
parent d9776e99d3
commit 612f5fe30e
2 changed files with 15 additions and 3 deletions

View File

@ -316,7 +316,7 @@ const Gpio = struct {
}; };
/// GBA Real Time Clock /// GBA Real Time Clock
const Clock = struct { pub const Clock = struct {
const This = @This(); const This = @This();
writer: Writer, writer: Writer,
@ -501,11 +501,14 @@ const Clock = struct {
.cpu = cpu, .cpu = cpu,
.gpio = gpio, // Can't use Arm7tdmi ptr b/c not initialized yet .gpio = gpio, // Can't use Arm7tdmi ptr b/c not initialized yet
}; };
cpu.sched.push(.RealTimeClock, 1 << 24); // Every Second
} }
fn updateRealTime(self: *This) void { pub fn updateTime(self: *This) void {
const now = DateTime.now(); self.cpu.sched.push(.RealTimeClock, 1 << 24); // Reschedule
const now = DateTime.now();
self.year = toBcd(u8, @intCast(u8, now.date.year - 2000)); self.year = toBcd(u8, @intCast(u8, now.date.year - 2000));
self.month = toBcd(u5, now.date.month); self.month = toBcd(u5, now.date.month);
self.day = toBcd(u3, now.date.day); self.day = toBcd(u3, now.date.day);

View File

@ -2,6 +2,7 @@ const std = @import("std");
const Bus = @import("Bus.zig"); const Bus = @import("Bus.zig");
const Arm7tdmi = @import("cpu.zig").Arm7tdmi; const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
const Clock = @import("bus/GamePak.zig").Clock;
const Order = std.math.Order; const Order = std.math.Order;
const PriorityQueue = std.PriorityQueue; const PriorityQueue = std.PriorityQueue;
@ -60,6 +61,13 @@ pub const Scheduler = struct {
3 => cpu.bus.apu.ch4.channelTimerOverflow(late), 3 => cpu.bus.apu.ch4.channelTimerOverflow(late),
} }
}, },
.RealTimeClock => {
const device = &cpu.bus.pak.gpio.device;
if (device.kind != .Rtc or device.ptr == null) return;
const clock = @ptrCast(*Clock, @alignCast(@alignOf(*Clock), device.ptr.?));
clock.updateTime();
},
.FrameSequencer => cpu.bus.apu.tickFrameSequencer(late), .FrameSequencer => cpu.bus.apu.tickFrameSequencer(late),
.SampleAudio => cpu.bus.apu.sampleAudio(late), .SampleAudio => cpu.bus.apu.sampleAudio(late),
.HBlank => cpu.bus.ppu.handleHBlankEnd(cpu, late), // The end of a HBlank .HBlank => cpu.bus.ppu.handleHBlankEnd(cpu, late), // The end of a HBlank
@ -118,4 +126,5 @@ pub const EventKind = union(enum) {
SampleAudio, SampleAudio,
FrameSequencer, FrameSequencer,
ApuChannel: u2, ApuChannel: u2,
RealTimeClock,
}; };