Compare commits
No commits in common. "c6123d8a6db65e55b1684f8b4c2a8f4c8ed55c73" and "5037b8f0ccf8bfd3f343cc3322f9ebcb499508c2" have entirely different histories.
c6123d8a6d
...
5037b8f0cc
|
@ -1,6 +1,5 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const Scheduler = @import("scheduler.zig").Scheduler;
|
|
||||||
const Io = @import("bus/io.zig").Io;
|
const Io = @import("bus/io.zig").Io;
|
||||||
const Bios = @import("bus/bios.zig").Bios;
|
const Bios = @import("bus/bios.zig").Bios;
|
||||||
const GamePak = @import("bus/pak.zig").GamePak;
|
const GamePak = @import("bus/pak.zig").GamePak;
|
||||||
|
@ -14,11 +13,11 @@ pub const Bus = struct {
|
||||||
ppu: Ppu,
|
ppu: Ppu,
|
||||||
io: Io,
|
io: Io,
|
||||||
|
|
||||||
pub fn init(alloc: Allocator, sched: *Scheduler, path: []const u8) !@This() {
|
pub fn init(alloc: Allocator, path: []const u8) !@This() {
|
||||||
return @This(){
|
return @This(){
|
||||||
.pak = try GamePak.init(alloc, path),
|
.pak = try GamePak.init(alloc, path),
|
||||||
.bios = try Bios.init(alloc, "./bin/gba_bios.bin"), // TODO: don't hardcode this + bundle open-sorce Boot ROM
|
.bios = try Bios.init(alloc, "./bin/gba_bios.bin"), // TODO: don't hardcode this + bundle open-sorce Boot ROM
|
||||||
.ppu = try Ppu.init(alloc, sched),
|
.ppu = try Ppu.init(alloc),
|
||||||
.io = Io.init(),
|
.io = Io.init(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,15 +5,13 @@ const Bitfield = bitfield.Bitfield;
|
||||||
const Bit = bitfield.Bit;
|
const Bit = bitfield.Bit;
|
||||||
|
|
||||||
pub const Io = struct {
|
pub const Io = struct {
|
||||||
dispcnt: DispCnt,
|
dispcnt: Dispcnt,
|
||||||
dispstat: DispStat,
|
dispstat: Dispstat,
|
||||||
vcount: VCount,
|
|
||||||
|
|
||||||
pub fn init() @This() {
|
pub fn init() @This() {
|
||||||
return .{
|
return .{
|
||||||
.dispcnt = .{ .raw = 0x0000_0000 },
|
.dispcnt = .{ .raw = 0x0000_0000 },
|
||||||
.dispstat = .{ .raw = 0x0000_0000 },
|
.dispstat = .{ .raw = 0x0000_0000 },
|
||||||
.vcount = .{ .raw = 0x0000_0000 },
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +48,7 @@ pub const Io = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const DispCnt = extern union {
|
const Dispcnt = extern union {
|
||||||
bg_mode: Bitfield(u16, 0, 3),
|
bg_mode: Bitfield(u16, 0, 3),
|
||||||
frame_select: Bit(u16, 4),
|
frame_select: Bit(u16, 4),
|
||||||
hblank_interraw_free: Bit(u16, 5),
|
hblank_interraw_free: Bit(u16, 5),
|
||||||
|
@ -63,7 +61,7 @@ const DispCnt = extern union {
|
||||||
raw: u16,
|
raw: u16,
|
||||||
};
|
};
|
||||||
|
|
||||||
const DispStat = extern union {
|
const Dispstat = extern union {
|
||||||
vblank: Bit(u16, 0),
|
vblank: Bit(u16, 0),
|
||||||
hblank: Bit(u16, 1),
|
hblank: Bit(u16, 1),
|
||||||
vcount: Bit(u16, 2),
|
vcount: Bit(u16, 2),
|
||||||
|
@ -73,8 +71,3 @@ const DispStat = extern union {
|
||||||
vcount_setting: Bitfield(u16, 8, 7),
|
vcount_setting: Bitfield(u16, 8, 7),
|
||||||
raw: u16,
|
raw: u16,
|
||||||
};
|
};
|
||||||
|
|
||||||
const VCount = extern union {
|
|
||||||
scanline: Bitfield(u16, 0, 8),
|
|
||||||
raw: u16,
|
|
||||||
};
|
|
||||||
|
|
|
@ -18,14 +18,14 @@ const arm_lut: [0x1000]InstrFn = populate();
|
||||||
|
|
||||||
pub const Arm7tdmi = struct {
|
pub const Arm7tdmi = struct {
|
||||||
r: [16]u32,
|
r: [16]u32,
|
||||||
sched: *Scheduler,
|
sch: *Scheduler,
|
||||||
bus: *Bus,
|
bus: *Bus,
|
||||||
cpsr: CPSR,
|
cpsr: CPSR,
|
||||||
|
|
||||||
pub fn init(sched: *Scheduler, bus: *Bus) @This() {
|
pub fn init(scheduler: *Scheduler, bus: *Bus) @This() {
|
||||||
return .{
|
return .{
|
||||||
.r = [_]u32{0x00} ** 16,
|
.r = [_]u32{0x00} ** 16,
|
||||||
.sched = sched,
|
.sch = scheduler,
|
||||||
.bus = bus,
|
.bus = bus,
|
||||||
.cpsr = .{ .raw = 0x0000_00DF },
|
.cpsr = .{ .raw = 0x0000_00DF },
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,14 +4,14 @@ const Bus = @import("bus.zig").Bus;
|
||||||
|
|
||||||
const cycles_per_frame: u64 = 100; // TODO: How many cycles actually?
|
const cycles_per_frame: u64 = 100; // TODO: How many cycles actually?
|
||||||
|
|
||||||
pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
pub fn runFrame(sch: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||||
var cycles: u64 = 0;
|
var cycles: u64 = 0;
|
||||||
while (cycles < cycles_per_frame) : (cycles += 1) {
|
while (cycles < cycles_per_frame) : (cycles += 1) {
|
||||||
sched.tick += 1;
|
sch.tick += 1;
|
||||||
_ = cpu.step();
|
_ = cpu.step();
|
||||||
|
|
||||||
while (sched.tick >= sched.nextTimestamp()) {
|
while (sch.tick >= sch.nextTimestamp()) {
|
||||||
sched.handleEvent(cpu, bus);
|
sch.handleEvent(cpu, bus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,12 +23,12 @@ pub fn main() anyerror!void {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var bus = try Bus.init(alloc, zba_args[0]);
|
||||||
|
defer bus.deinit();
|
||||||
|
|
||||||
var scheduler = Scheduler.init(alloc);
|
var scheduler = Scheduler.init(alloc);
|
||||||
defer scheduler.deinit();
|
defer scheduler.deinit();
|
||||||
|
|
||||||
var bus = try Bus.init(alloc, &scheduler, zba_args[0]);
|
|
||||||
defer bus.deinit();
|
|
||||||
|
|
||||||
var cpu = Arm7tdmi.init(&scheduler, &bus);
|
var cpu = Arm7tdmi.init(&scheduler, &bus);
|
||||||
|
|
||||||
cpu.skipBios();
|
cpu.skipBios();
|
||||||
|
|
|
@ -1,22 +1,15 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const Scheduler = @import("scheduler.zig").Scheduler;
|
|
||||||
const EventKind = @import("scheduler.zig").EventKind;
|
|
||||||
|
|
||||||
pub const Ppu = struct {
|
pub const Ppu = struct {
|
||||||
vram: Vram,
|
vram: Vram,
|
||||||
palette: Palette,
|
palette: Palette,
|
||||||
sched: *Scheduler,
|
|
||||||
|
|
||||||
pub fn init(alloc: Allocator, sched: *Scheduler) !@This() {
|
|
||||||
// Queue first Hblank
|
|
||||||
sched.push(.{ .kind = .HBlank, .tick = sched.tick + 240 * 4 });
|
|
||||||
|
|
||||||
|
pub fn init(alloc: Allocator) !@This() {
|
||||||
return @This(){
|
return @This(){
|
||||||
.vram = try Vram.init(alloc),
|
.vram = try Vram.init(alloc),
|
||||||
.palette = try Palette.init(alloc),
|
.palette = try Palette.init(alloc),
|
||||||
.sched = sched,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub const Scheduler = struct {
|
||||||
self.queue.deinit();
|
self.queue.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handleEvent(self: *@This(), _: *Arm7tdmi, bus: *Bus) void {
|
pub fn handleEvent(self: *@This(), _: *Arm7tdmi, _: *Bus) void {
|
||||||
const should_handle = if (self.queue.peek()) |e| self.tick >= e.tick else false;
|
const should_handle = if (self.queue.peek()) |e| self.tick >= e.tick else false;
|
||||||
|
|
||||||
if (should_handle) {
|
if (should_handle) {
|
||||||
|
@ -35,56 +35,10 @@ pub const Scheduler = struct {
|
||||||
.HeatDeath => {
|
.HeatDeath => {
|
||||||
std.debug.panic("[Scheduler] Somehow, a u64 overflowed", .{});
|
std.debug.panic("[Scheduler] Somehow, a u64 overflowed", .{});
|
||||||
},
|
},
|
||||||
.HBlank => {
|
|
||||||
std.log.debug("[Scheduler] tick {}: Hblank", .{self.tick});
|
|
||||||
|
|
||||||
// We've reached the end of a scanline
|
|
||||||
const scanline = bus.io.vcount.scanline.read();
|
|
||||||
bus.io.vcount.scanline.write(scanline + 1);
|
|
||||||
|
|
||||||
bus.io.dispstat.hblank.set();
|
|
||||||
|
|
||||||
if (scanline < 160) {
|
|
||||||
self.push(.{ .kind = .Visible, .tick = self.tick + (68 * 4) });
|
|
||||||
} else {
|
|
||||||
self.push(.{ .kind = .VBlank, .tick = self.tick + (68 * 4) });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
.Visible => {
|
|
||||||
std.log.debug("[Scheduler] tick {}: Visible", .{self.tick});
|
|
||||||
|
|
||||||
// Beginning of a Scanline
|
|
||||||
bus.io.dispstat.hblank.unset();
|
|
||||||
bus.io.dispstat.vblank.unset();
|
|
||||||
|
|
||||||
self.push(.{ .kind = .HBlank, .tick = self.tick + (240 * 4) });
|
|
||||||
},
|
|
||||||
.VBlank => {
|
|
||||||
std.log.debug("[Scheduler] tick {}: VBlank", .{self.tick});
|
|
||||||
|
|
||||||
// Beginning of a Scanline, not visible though
|
|
||||||
bus.io.dispstat.hblank.unset();
|
|
||||||
bus.io.dispstat.vblank.set();
|
|
||||||
|
|
||||||
const scanline = bus.io.vcount.scanline.read();
|
|
||||||
bus.io.vcount.scanline.write(scanline + 1);
|
|
||||||
|
|
||||||
if (scanline < 227) {
|
|
||||||
// Another Vblank Scanline
|
|
||||||
self.push(.{ .kind = .VBlank, .tick = self.tick + 68 * (308 * 4) });
|
|
||||||
} else {
|
|
||||||
bus.io.vcount.scanline.write(0); // Reset Scanline
|
|
||||||
self.push(.{ .kind = .Visible, .tick = self.tick + 68 * (308 * 4) });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub inline fn push(self: *@This(), event: Event) void {
|
|
||||||
self.queue.add(event) catch unreachable;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub inline fn nextTimestamp(self: *@This()) u64 {
|
pub inline fn nextTimestamp(self: *@This()) u64 {
|
||||||
if (self.queue.peek()) |e| {
|
if (self.queue.peek()) |e| {
|
||||||
return e.tick;
|
return e.tick;
|
||||||
|
@ -103,7 +57,4 @@ fn lessThan(_: void, a: Event, b: Event) Order {
|
||||||
|
|
||||||
pub const EventKind = enum {
|
pub const EventKind = enum {
|
||||||
HeatDeath,
|
HeatDeath,
|
||||||
HBlank,
|
|
||||||
VBlank,
|
|
||||||
Visible,
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue