Compare commits

..

No commits in common. "c100d64fcbfa3ff0ed33e16bbc91eb886ce698cb" and "76789aa8bc29ca38deeafc7fd4950194d6b0de12" have entirely different histories.

7 changed files with 34 additions and 39 deletions

View File

@ -29,9 +29,8 @@ dma: DmaControllers,
tim: Timers,
iwram: Iwram,
ewram: Ewram,
io: Io,
sched: *Scheduler,
io: Io,
pub fn init(alloc: Allocator, sched: *Scheduler, rom_path: []const u8, bios_path: ?[]const u8, save_path: ?[]const u8) !Self {
return Self{
@ -44,7 +43,6 @@ pub fn init(alloc: Allocator, sched: *Scheduler, rom_path: []const u8, bios_path
.dma = DmaControllers.init(),
.tim = Timers.init(sched),
.io = Io.init(),
.sched = sched,
};
}
@ -59,7 +57,6 @@ pub fn deinit(self: Self) void {
pub fn read(self: *const Self, comptime T: type, address: u32) T {
const page = @truncate(u8, address >> 24);
const align_addr = alignAddress(T, address);
self.sched.tick += 1;
return switch (page) {
// General Internal Memory
@ -94,7 +91,6 @@ pub fn read(self: *const Self, comptime T: type, address: u32) T {
pub fn write(self: *Self, comptime T: type, address: u32, value: T) void {
const page = @truncate(u8, address >> 24);
const align_addr = alignAddress(T, address);
self.sched.tick += 1;
switch (page) {
// General Internal Memory

View File

@ -29,11 +29,6 @@ pub fn deinit(self: Self) void {
pub fn read(self: *const Self, comptime T: type, addr: usize) T {
if (self.buf) |buf| {
if (addr > buf.len) {
log.err("Tried to read {} from {X:0>8} (open bus)", .{ T, addr });
return 0;
}
return switch (T) {
u32 => (@as(u32, buf[addr + 3]) << 24) | (@as(u32, buf[addr + 2]) << 16) | (@as(u32, buf[addr + 1]) << 8) | (@as(u32, buf[addr])),
u16 => (@as(u16, buf[addr + 1]) << 8) | @as(u16, buf[addr]),

View File

@ -81,13 +81,13 @@ fn Timer(comptime id: u2) type {
// Reload on Rising edge
self._counter = self._reload;
if (!new.cascade.read()) self.scheduleOverflow(0);
if (!new.cascade.read()) self.scheduleOverflow();
}
self.cnt.raw = halfword;
}
pub fn handleOverflow(self: *Self, cpu: *Arm7tdmi, late: u64) void {
pub fn handleOverflow(self: *Self, cpu: *Arm7tdmi) void {
// Fire IRQ if enabled
const io = &cpu.bus.io;
const tim = &cpu.bus.tim;
@ -107,15 +107,21 @@ fn Timer(comptime id: u2) type {
switch (id) {
0 => if (tim._1.cnt.cascade.read()) {
tim._1._counter +%= 1;
if (tim._1._counter == 0) tim._1.handleOverflow(cpu, late);
if (tim._1._counter == 0)
tim._1.handleOverflow(cpu);
},
1 => if (tim._2.cnt.cascade.read()) {
tim._2._counter +%= 1;
if (tim._2._counter == 0) tim._2.handleOverflow(cpu, late);
if (tim._2._counter == 0)
tim._2.handleOverflow(cpu);
},
2 => if (tim._3.cnt.cascade.read()) {
tim._3._counter +%= 1;
if (tim._3._counter == 0) tim._3.handleOverflow(cpu, late);
if (tim._3._counter == 0)
tim._3.handleOverflow(cpu);
},
3 => {}, // There is no Timer for TIM3 to "cascade" to,
}
@ -123,15 +129,15 @@ fn Timer(comptime id: u2) type {
// Reschedule Timer if we're not cascading
if (!self.cnt.cascade.read()) {
self._counter = self._reload;
self.scheduleOverflow(late);
self.scheduleOverflow();
}
}
fn scheduleOverflow(self: *Self, late: u64) void {
fn scheduleOverflow(self: *Self) void {
const when = (@as(u64, 0x10000) - self._counter) * self.frequency();
self._start_timestamp = self.sched.now();
self.sched.push(.{ .TimerOverflow = id }, self.sched.now() + when - late);
self.sched.push(.{ .TimerOverflow = id }, self.sched.now() + when);
}
fn frequency(self: *const Self) u16 {

View File

@ -245,15 +245,12 @@ pub const Arm7tdmi = struct {
self.cpsr.raw = 0x6000001F;
}
pub fn step(self: *Self) void {
pub fn step(self: *Self) u64 {
// If we're processing a DMA (not Sound or Blanking) the CPU is disabled
if (self.handleDMATransfers()) return;
if (self.handleDMATransfers()) return 1;
// If we're halted, the cpu is disabled
if (self.bus.io.haltcnt == .Halt) {
self.sched.tick += 1;
return;
}
if (self.bus.io.haltcnt == .Halt) return 1;
if (self.cpsr.t.read()) {
const opcode = self.thumbFetch();
@ -268,6 +265,8 @@ pub const Arm7tdmi = struct {
arm_lut[armIdx(opcode)](self, self.bus, opcode);
}
}
return 1;
}
pub fn handleInterrupt(self: *Self) void {

View File

@ -46,7 +46,8 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
const frame_end = sched.tick + cycles_per_frame;
while (sched.tick < frame_end) {
cpu.step();
sched.tick += 1;
_ = cpu.step();
while (sched.tick >= sched.nextTimestamp()) {
sched.handleEvent(cpu, bus);

View File

@ -358,7 +358,7 @@ pub const Ppu = struct {
};
}
pub fn handleHDrawEnd(self: *Self, cpu: *Arm7tdmi, late: u64) void {
pub fn handleHDrawEnd(self: *Self, cpu: *Arm7tdmi) void {
// Transitioning to a Hblank
if (self.dispstat.hblank_irq.read()) {
cpu.bus.io.irq.hblank.set();
@ -369,10 +369,10 @@ pub const Ppu = struct {
pollBlankingDma(cpu.bus, .HBlank);
self.dispstat.hblank.set();
self.sched.push(.HBlank, self.sched.now() + (68 * 4) - late);
self.sched.push(.HBlank, self.sched.now() + (68 * 4));
}
pub fn handleHBlankEnd(self: *Self, cpu: *Arm7tdmi, late: u64) void {
pub fn handleHBlankEnd(self: *Self, cpu: *Arm7tdmi) void {
// The End of a Hblank (During Draw or Vblank)
const old_scanline = self.vcount.scanline.read();
const scanline = (old_scanline + 1) % 228;
@ -391,7 +391,7 @@ pub const Ppu = struct {
if (scanline < 160) {
// Transitioning to another Draw
self.sched.push(.Draw, self.sched.now() + (240 * 4) - late);
self.sched.push(.Draw, self.sched.now() + (240 * 4));
} else {
// Transitioning to a Vblank
if (scanline == 160) {
@ -407,7 +407,7 @@ pub const Ppu = struct {
}
if (scanline == 227) self.dispstat.vblank.unset();
self.sched.push(.VBlank, self.sched.now() + (240 * 4) - late);
self.sched.push(.VBlank, self.sched.now() + (240 * 4));
}
}
};

View File

@ -31,8 +31,6 @@ pub const Scheduler = struct {
pub fn handleEvent(self: *Self, cpu: *Arm7tdmi, bus: *Bus) void {
if (self.queue.removeOrNull()) |event| {
const late = self.tick - event.tick;
switch (event.kind) {
.HeatDeath => {
log.err("A u64 overflowered. This *actually* should never happen.", .{});
@ -41,18 +39,18 @@ pub const Scheduler = struct {
.Draw => {
// The end of a VDraw
bus.ppu.drawScanline();
bus.ppu.handleHDrawEnd(cpu, late);
bus.ppu.handleHDrawEnd(cpu);
},
.TimerOverflow => |id| {
switch (id) {
0 => bus.tim._0.handleOverflow(cpu, late),
1 => bus.tim._1.handleOverflow(cpu, late),
2 => bus.tim._2.handleOverflow(cpu, late),
3 => bus.tim._3.handleOverflow(cpu, late),
0 => bus.tim._0.handleOverflow(cpu),
1 => bus.tim._1.handleOverflow(cpu),
2 => bus.tim._2.handleOverflow(cpu),
3 => bus.tim._3.handleOverflow(cpu),
}
},
.HBlank => bus.ppu.handleHBlankEnd(cpu, late), // The end of a HBlank
.VBlank => bus.ppu.handleHDrawEnd(cpu, late), // The end of a VBlank
.HBlank => bus.ppu.handleHBlankEnd(cpu), // The end of a HBlank
.VBlank => bus.ppu.handleHDrawEnd(cpu), // The end of a VBlank
}
}
}