Skip to content

Commit

Permalink
faster
Browse files Browse the repository at this point in the history
  • Loading branch information
190n committed May 17, 2024
1 parent 5fa2502 commit 5490ef3
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 79 deletions.
18 changes: 9 additions & 9 deletions src/tail.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub const Cpu = struct {

pub fn run(self: *Cpu, instructions: u16) void {
self.instructions = instructions;
self.code[self.pc].func(self, self.code[self.pc].decoded.toInt());
self.code[self.pc].func(self, self.code[self.pc].decoded.toInt(), self.pc);
}

pub fn timerTick(self: *Cpu) void {
Expand All @@ -47,7 +47,7 @@ pub const Cpu = struct {
}
};

const GadgetFunc = *const fn (*Cpu, u32) void;
const GadgetFunc = *const fn (*Cpu, u32, u12) void;

pub const Decoded = union {
xy: [2]u4,
Expand Down Expand Up @@ -97,8 +97,8 @@ const Inst = struct {
decoded: Decoded,
};

pub fn decode(cpu: *Cpu, _: Decoded.Int) void {
const opcode = std.mem.readInt(u16, cpu.mem[cpu.pc..][0..2], .big);
pub fn decode(cpu: *Cpu, _: Decoded.Int, pc: u12) void {
const opcode = std.mem.readInt(u16, cpu.mem[pc..][0..2], .big);
const inst: Inst = switch (@as(u4, @truncate(opcode >> 12))) {
0x0 => switch (opcode & 0xff) {
0xE0 => .{ .func = &tailfuncs.clear, .decoded = .{ .none = {} } },
Expand Down Expand Up @@ -161,16 +161,16 @@ pub fn decode(cpu: *Cpu, _: Decoded.Int) void {
.decoded = Decoded.decode(opcode, .xnn),
},
};
cpu.code[cpu.pc] = inst;
@call(.always_tail, inst.func, .{ cpu, inst.decoded.toInt() });
cpu.code[pc] = inst;
@call(.always_tail, inst.func, .{ cpu, inst.decoded.toInt(), pc });
}

fn invalid(cpu: *Cpu, _: Decoded.Int) void {
fn invalid(cpu: *Cpu, _: Decoded.Int, pc: u12) void {
std.debug.panic(
"invalid instruction: {X:0>4} at 0x{X:0>3}",
.{
std.mem.readInt(u16, cpu.mem[cpu.pc..][0..2], .big),
cpu.pc,
std.mem.readInt(u16, cpu.mem[pc..][0..2], .big),
pc,
},
);
}
Loading

0 comments on commit 5490ef3

Please sign in to comment.