uzn/src/uxn.zig

777 lines
31 KiB
Zig

const std = @import("std");
const Uxn = @This();
var debug = true;
pub fn setDebug(v: bool) void {
debug = v;
}
mem: Memory = .{ .m = undefined },
ws: Stack = .{ .s = undefined },
rs: Stack = .{ .s = undefined },
dev: [0x100]u8 = undefined,
pc: u16 = 0x100,
deo8: *const fn (*Uxn, *Stack, u8, u8) void = deo8Stub,
deo16: *const fn (*Uxn, *Stack, u8, u16) void = deo16Stub,
dei8: *const fn (*Uxn, *Stack, u8) void = dei8Stub,
dei16: *const fn (*Uxn, *Stack, u8) void = dei16Stub,
fn deo8Stub(_: *Uxn, _: *Stack, _: u8, _: u8) void {}
fn deo16Stub(_: *Uxn, _: *Stack, _: u8, _: u16) void {}
fn dei8Stub(_: *Uxn, _: *Stack, _: u8) void {}
fn dei16Stub(_: *Uxn, _: *Stack, _: u8) void {}
pub fn init(allocator: std.mem.Allocator) !Uxn {
const mem = try allocator.alloc(u8, 0x10000);
errdefer allocator.free(mem);
for (mem) |*b| b.* = 0;
return .{
.mem = .{ .m = mem },
.ws = .{ .s = [_]u8{0} ** 0x100 },
.rs = .{ .s = [_]u8{0} ** 0x100 },
.dev = [_]u8{0} ** 0x100,
};
}
pub fn deinit(self: *Uxn, allocator: std.mem.Allocator) void {
allocator.free(self.mem.m);
}
const Memory = struct {
m: []u8,
pub fn peek(self: *Memory, comptime T: type, idx: u16) T {
switch (T) {
u8 => {
const val = self.m[idx];
if (debug) std.debug.print("\x1b[33mmem peek:\x1b[0m\tidx {d}, type {s}, val {d}\n", .{ idx, @typeName(T), val });
return val;
},
u16 => {
const val = @as(u16, @intCast(self.m[idx])) << 8 | self.m[idx +% 1];
if (debug) std.debug.print("\x1b[33mmem peek:\x1b[0m\tidx {d}, type {s}, val {d}\n", .{ idx, @typeName(T), val });
return val;
},
else => @compileError("expected u8 or u16, got " + @typeName(T)),
}
}
pub fn poke(self: *Memory, comptime T: type, idx: u16, val: T) void {
if (debug) std.debug.print("\x1b[33mmem poke:\x1b[0m\tidx {d}, type {s}, val {d}\n", .{ idx, @typeName(T), val });
switch (T) {
u8 => self.m[idx] = val,
u16 => {
self.m[idx] = @truncate(val >> 8);
self.m[idx +% 1] = @truncate(val & 0xFF);
},
else => @compileError("expected u8 or u16, got " + @typeName(T)),
}
}
};
test "memory peek/poke" {
const m = try std.testing.allocator.alloc(u8, 0x10000);
defer std.testing.allocator.free(m);
var mem = Memory{ .m = m };
mem.poke(u16, 0, 0xABCD);
mem.poke(u8, 2, 0x69);
try std.testing.expectEqual(0xABCD, mem.peek(u16, 0));
try std.testing.expectEqual(0x69, mem.peek(u8, 2));
}
pub const Stack = struct {
s: [0x100]u8,
sp: u8 = 0,
pub fn peek(self: *Stack, comptime T: type) T {
switch (T) {
u8 => {
const val = self.s[self.sp -% 1];
if (debug) std.debug.print("\x1b[32mstack peek:\x1b[0m\tsp {d}, type {s}, val {d}\n", .{ self.sp, @typeName(T), val });
return val;
},
u16 => {
const val = @as(u16, @intCast(self.s[self.sp -% 2])) << 8 | self.s[self.sp -% 1];
if (debug) std.debug.print("\x1b[32mstack peek:\x1b[0m\tsp {d}, type {s}, val {d}\n", .{ self.sp, @typeName(T), val });
return val;
},
else => @compileError("expected u8 or u16, got " + @typeName(T)),
}
}
pub fn poke(self: *Stack, comptime T: type, v: T) void {
if (debug) std.debug.print("\x1b[32mstack poke:\x1b[0m\tsp {d}, type {s}, val {d}\n", .{ self.sp, @typeName(T), v });
switch (T) {
u8 => self.s[self.sp -% 1] = v,
u16 => {
self.s[self.sp -% 2] = @truncate(v >> 8);
self.s[self.sp -% 1] = @truncate(v & 0xFF);
},
else => @compileError("expected u8 or u16, got " + @typeName(T)),
}
}
pub fn pop(self: *Stack, comptime T: type) T {
self.sp -%= @intCast(@divExact(@typeInfo(T).int.bits, 8));
return switch (T) {
u8 => {
const val = self.s[self.sp];
if (debug) std.debug.print("\x1b[32mstack pop:\x1b[0m\tsp {d}, type {s}, val {d}\n", .{ self.sp, @typeName(T), val });
return val;
},
u16 => {
const val = @as(u16, @intCast(self.s[self.sp])) << 8 | self.s[self.sp +% 1];
if (debug) std.debug.print("\x1b[32mstack pop:\x1b[0m\tsp {d}, type {s}, val {d}\n", .{ self.sp, @typeName(T), val });
return val;
},
else => @compileError("expected u8 or u16, got " + @typeName(T)),
};
}
pub fn push(self: *Stack, comptime T: type, v: T) void {
if (debug) std.debug.print("\x1b[32mstack push:\x1b[0m\tsp {d}, type {s}, value {d}\n", .{ self.sp, @typeName(T), v });
switch (T) {
u8 => self.s[self.sp] = v,
u16 => {
self.s[self.sp] = @truncate(v >> 8);
self.s[self.sp +% 1] = @truncate(v & 0xFF);
},
else => @compileError("expected u8 or u16, got " + @typeName(T)),
}
self.sp +%= @intCast(@divExact(@typeInfo(T).int.bits, 8));
}
};
test "stack poke/peek/push/pop" {
var stack = Stack{ .s = undefined };
stack.push(u16, 0xABCD);
stack.push(u8, 0x69);
try std.testing.expectEqual(0x69, stack.peek(u8));
try std.testing.expectEqual(0x69, stack.pop(u8));
try std.testing.expectEqual(0xABCD, stack.peek(u16));
try std.testing.expectEqual(0xABCD, stack.pop(u16));
stack.poke(u8, 0xBF);
try std.testing.expectEqual(0xBF, stack.peek(u8));
stack.poke(u16, 0xBEEF);
try std.testing.expectEqual(0xBEEF, stack.peek(u16));
}
pub fn eval(self: *Uxn) bool {
switch (self.mem.m[self.pc]) {
0x00 => return false, // BRK
0x01 => inc(&self.ws, u8, false), // INC
0x02 => pop(&self.ws, u8, false), // POP
0x03 => nip(&self.ws, u8, false), // NIP
0x04 => swp(&self.ws, u8, false), // SWP
0x05 => rot(&self.ws, u8, false), // ROT
0x06 => dup(&self.ws, u8, false), // DUP
0x07 => ovr(&self.ws, u8, false), // OVR
0x08 => equ(&self.ws, u8, false), // EQU
0x09 => neq(&self.ws, u8, false), // NEQ
0x0A => gth(&self.ws, u8, false), // GTH
0x0B => lth(&self.ws, u8, false), // LTH
0x0C => self.jmp(&self.ws, u8, false), // JMP
0x0D => self.jcn(&self.ws, u8, false), // JCN
0x0E => self.jsr(&self.ws, u8, false), // JSR
0x0F => self.sth(false, u8, false), // STH
0x10 => self.ldz(&self.ws, u8, false), // LDZ
0x11 => self.stz(&self.ws, u8, false), // STZ
0x12 => self.ldr(&self.ws, u8, false), // LDR
0x13 => self.str(&self.ws, u8, false), // STR
0x14 => self.lda(&self.ws, u8, false), // LDA
0x15 => self.sta(&self.ws, u8, false), // STA
0x16 => self.dei(&self.ws, u8, false), // DEI
0x17 => self.deo(&self.ws, u8, false), // DEO
0x18 => add(&self.ws, u8, false), // ADD
0x19 => sub(&self.ws, u8, false), // SUB
0x1A => mul(&self.ws, u8, false), // MUL
0x1B => div(&self.ws, u8, false), // DIV
0x1C => @"and"(&self.ws, u8, false), // AND
0x1D => ora(&self.ws, u8, false), // ORA
0x1E => eor(&self.ws, u8, false), // EOR
0x1F => sft(&self.ws, u8, false), // SFT
0x20 => {
if (self.ws.pop(u8) != 0)
self.pc +%= 2 + self.mem.peek(u16, self.pc +% 1)
else
self.pc +%= 2;
}, // JCI
0x21 => inc(&self.ws, u16, false), // INC2
0x22 => pop(&self.ws, u16, false), // POP2
0x23 => nip(&self.ws, u16, false), // NIP2
0x24 => swp(&self.ws, u16, false), // SWP2
0x25 => rot(&self.ws, u16, false), // ROT2
0x26 => dup(&self.ws, u16, false), // DUP2
0x27 => ovr(&self.ws, u16, false), // OVR2
0x28 => equ(&self.ws, u16, false), // EQU2
0x29 => neq(&self.ws, u16, false), // NEQ2
0x2A => gth(&self.ws, u16, false), // GTH2
0x2B => lth(&self.ws, u16, false), // LTH2
0x2C => self.jmp(&self.ws, u16, false), // JMP2
0x2D => self.jcn(&self.ws, u16, false), // JCN2
0x2E => self.jsr(&self.ws, u16, false), // JSR2
0x2F => self.sth(false, u16, false), // STH2
0x30 => self.ldz(&self.ws, u16, false), // LDZ2
0x31 => self.stz(&self.ws, u16, false), // STZ2
0x32 => self.ldr(&self.ws, u16, false), // LDR2
0x33 => self.str(&self.ws, u16, false), // STR2
0x34 => self.lda(&self.ws, u16, false), // LDA2
0x35 => self.sta(&self.ws, u16, false), // STA2
0x36 => self.dei(&self.ws, u16, false), // DEI2
0x37 => self.deo(&self.ws, u16, false), // DEO2
0x38 => add(&self.ws, u16, false), // ADD2
0x39 => sub(&self.ws, u16, false), // SUB2
0x3A => mul(&self.ws, u16, false), // MUL2
0x3B => div(&self.ws, u16, false), // DIV2
0x3C => @"and"(&self.ws, u16, false), // AND2
0x3D => ora(&self.ws, u16, false), // ORA2
0x3E => eor(&self.ws, u16, false), // EOR2
0x3F => sft(&self.ws, u16, false), // SFT2
0x40 => {
self.pc +%= 2 + self.mem.peek(u16, self.pc +% 1);
}, // JMI
0x41 => inc(&self.rs, u8, false), // INCr
0x42 => pop(&self.rs, u8, false), // POPr
0x43 => nip(&self.rs, u8, false), // NIPr
0x44 => swp(&self.rs, u8, false), // SWPr
0x45 => rot(&self.rs, u8, false), // ROTr
0x46 => dup(&self.rs, u8, false), // DUPr
0x47 => ovr(&self.rs, u8, false), // OVRr
0x48 => equ(&self.rs, u8, false), // EQUr
0x49 => neq(&self.rs, u8, false), // NEQr
0x4A => gth(&self.rs, u8, false), // GTHr
0x4B => lth(&self.rs, u8, false), // LTHr
0x4C => self.jmp(&self.rs, u8, false), // JMPr
0x4D => self.jcn(&self.rs, u8, false), // JCNr
0x4E => self.jsr(&self.rs, u8, false), // JSRr
0x4F => self.sth(true, u8, false), // STHr
0x50 => self.ldz(&self.rs, u8, false), // LDZr
0x51 => self.stz(&self.rs, u8, false), // STZr
0x52 => self.ldr(&self.rs, u8, false), // LDRr
0x53 => self.str(&self.rs, u8, false), // STRr
0x54 => self.lda(&self.rs, u8, false), // LDAr
0x55 => self.sta(&self.rs, u8, false), // STAr
0x56 => self.dei(&self.rs, u8, false), // DEIr
0x57 => self.deo(&self.rs, u8, false), // DEOr
0x58 => add(&self.rs, u8, false), // ADDr
0x59 => sub(&self.rs, u8, false), // SUBr
0x5A => mul(&self.rs, u8, false), // MULr
0x5B => div(&self.rs, u8, false), // DIVr
0x5C => @"and"(&self.rs, u8, false), // ANDr
0x5D => ora(&self.rs, u8, false), // ORAr
0x5E => eor(&self.rs, u8, false), // EORr
0x5F => sft(&self.rs, u8, false), // SFTr
0x60 => {
self.rs.push(u16, self.pc +% 2);
self.pc +%= 2 + self.mem.peek(u16, self.pc +% 1);
}, // JSI
0x61 => inc(&self.rs, u16, false), // INC2r
0x62 => pop(&self.rs, u16, false), // POP2r
0x63 => nip(&self.rs, u16, false), // NIP2r
0x64 => swp(&self.rs, u16, false), // SWP2r
0x65 => rot(&self.rs, u16, false), // ROT2r
0x66 => dup(&self.rs, u16, false), // DUP2r
0x67 => ovr(&self.rs, u16, false), // OVR2r
0x68 => equ(&self.rs, u16, false), // EQU2r
0x69 => neq(&self.rs, u16, false), // NEQ2r
0x6A => gth(&self.rs, u16, false), // GTH2r
0x6B => lth(&self.rs, u16, false), // LTH2r
0x6C => self.jmp(&self.rs, u16, false), // JMP2r
0x6D => self.jcn(&self.rs, u16, false), // JCN2r
0x6E => self.jsr(&self.rs, u16, false), // JSR2r
0x6F => self.sth(true, u16, false), // STH2r
0x70 => self.ldz(&self.rs, u16, false), // LDZ2r
0x71 => self.stz(&self.rs, u16, false), // STZ2r
0x72 => self.ldr(&self.rs, u16, false), // LDR2r
0x73 => self.str(&self.rs, u16, false), // STR2r
0x74 => self.lda(&self.rs, u16, false), // LDA2r
0x75 => self.sta(&self.rs, u16, false), // STA2r
0x76 => self.dei(&self.rs, u16, false), // DEI2r
0x77 => self.deo(&self.rs, u16, false), // DEO2r
0x78 => add(&self.rs, u16, false), // ADD2r
0x79 => sub(&self.rs, u16, false), // SUB2r
0x7A => mul(&self.rs, u16, false), // MUL2r
0x7B => div(&self.rs, u16, false), // DIV2r
0x7C => @"and"(&self.rs, u16, false), // AND2r
0x7D => ora(&self.rs, u16, false), // ORA2r
0x7E => eor(&self.rs, u16, false), // EOR2r
0x7F => sft(&self.rs, u16, false), // SFT2r
0x80 => {
self.ws.push(u8, self.mem.peek(u8, self.pc +% 1));
self.pc +%= 1;
}, // LIT
0x81 => inc(&self.ws, u8, true), // INCk
0x82 => pop(&self.ws, u8, true), // POPk
0x83 => nip(&self.ws, u8, true), // NIPk
0x84 => swp(&self.ws, u8, true), // SWPk
0x85 => rot(&self.ws, u8, true), // ROTk
0x86 => dup(&self.ws, u8, true), // DUPk
0x87 => ovr(&self.ws, u8, true), // OVRk
0x88 => equ(&self.ws, u8, true), // EQUk
0x89 => neq(&self.ws, u8, true), // NEQk
0x8A => gth(&self.ws, u8, true), // GTHk
0x8B => lth(&self.ws, u8, true), // LTHk
0x8C => self.jmp(&self.ws, u8, true), // JMPk
0x8D => self.jcn(&self.ws, u8, true), // JCNk
0x8E => self.jsr(&self.ws, u8, true), // JSRk
0x8F => self.sth(false, u8, true), // STHk
0x90 => self.ldz(&self.ws, u8, true), // LDZk
0x91 => self.stz(&self.ws, u8, true), // STZk
0x92 => self.ldr(&self.ws, u8, true), // LDRk
0x93 => self.str(&self.ws, u8, true), // STRk
0x94 => self.lda(&self.ws, u8, true), // LDAk
0x95 => self.sta(&self.ws, u8, true), // STAk
0x96 => self.dei(&self.ws, u8, true), // DEIk
0x97 => self.deo(&self.ws, u8, true), // DEOk
0x98 => add(&self.ws, u8, true), // ADDk
0x99 => sub(&self.ws, u8, true), // SUBk
0x9A => mul(&self.ws, u8, true), // MULk
0x9B => div(&self.ws, u8, true), // DIVk
0x9C => @"and"(&self.ws, u8, true), // ANDk
0x9D => ora(&self.ws, u8, true), // ORAk
0x9E => eor(&self.ws, u8, true), // EORk
0x9F => sft(&self.ws, u8, true), // SFTk
0xA0 => {
self.ws.push(u16, self.mem.peek(u16, self.pc +% 1));
self.pc +%= 2;
}, // LIT2
0xA1 => inc(&self.ws, u16, true), // INC2k
0xA2 => pop(&self.ws, u16, true), // POP2k
0xA3 => nip(&self.ws, u16, true), // NIP2k
0xA4 => swp(&self.ws, u16, true), // SWP2k
0xA5 => rot(&self.ws, u16, true), // ROT2k
0xA6 => dup(&self.ws, u16, true), // DUP2k
0xA7 => ovr(&self.ws, u16, true), // OVR2k
0xA8 => equ(&self.ws, u16, true), // EQU2k
0xA9 => neq(&self.ws, u16, true), // NEQ2k
0xAA => gth(&self.ws, u16, true), // GTH2k
0xAB => lth(&self.ws, u16, true), // LTH2k
0xAC => self.jmp(&self.ws, u16, true), // JMP2k
0xAD => self.jcn(&self.ws, u16, true), // JCN2k
0xAE => self.jsr(&self.ws, u16, true), // JSR2k
0xAF => self.sth(false, u16, true), // STH2k
0xB0 => self.ldz(&self.ws, u16, true), // LDZ2k
0xB1 => self.stz(&self.ws, u16, true), // STZ2k
0xB2 => self.ldr(&self.ws, u16, true), // LDR2k
0xB3 => self.str(&self.ws, u16, true), // STR2k
0xB4 => self.lda(&self.ws, u16, true), // LDA2k
0xB5 => self.sta(&self.ws, u16, true), // STA2k
0xB6 => self.dei(&self.ws, u16, true), // DEI2k
0xB7 => self.deo(&self.ws, u16, true), // DEO2k
0xB8 => add(&self.ws, u16, true), // ADD2k
0xB9 => sub(&self.ws, u16, true), // SUB2k
0xBA => mul(&self.ws, u16, true), // MUL2k
0xBB => div(&self.ws, u16, true), // DIV2k
0xBC => @"and"(&self.ws, u16, true), // AND2k
0xBD => ora(&self.ws, u16, true), // ORA2k
0xBE => eor(&self.ws, u16, true), // EOR2k
0xBF => sft(&self.ws, u16, true), // SFT2k
0xC0 => {
self.rs.push(u8, self.mem.peek(u8, self.pc +% 1));
self.pc +%= 1;
}, // LITr
0xC1 => inc(&self.rs, u8, true), // INCkr
0xC2 => pop(&self.rs, u8, true), // POPkr
0xC3 => nip(&self.rs, u8, true), // NIPkr
0xC4 => swp(&self.rs, u8, true), // SWPkr
0xC5 => rot(&self.rs, u8, true), // ROTkr
0xC6 => dup(&self.rs, u8, true), // DUPkr
0xC7 => ovr(&self.rs, u8, true), // OVRkr
0xC8 => equ(&self.rs, u8, true), // EQUkr
0xC9 => neq(&self.rs, u8, true), // NEQkr
0xCA => gth(&self.rs, u8, true), // GTHkr
0xCB => lth(&self.rs, u8, true), // LTHkr
0xCC => self.jmp(&self.rs, u8, true), // JMPkr
0xCD => self.jcn(&self.rs, u8, true), // JCNkr
0xCE => self.jsr(&self.rs, u8, true), // JSRkr
0xCF => self.sth(true, u8, true), // STHkr
0xD0 => self.ldz(&self.rs, u8, true), // LDZkr
0xD1 => self.stz(&self.rs, u8, true), // STZkr
0xD2 => self.ldr(&self.rs, u8, true), // LDRkr
0xD3 => self.str(&self.rs, u8, true), // STRkr
0xD4 => self.lda(&self.rs, u8, true), // LDAkr
0xD5 => self.sta(&self.rs, u8, true), // STAkr
0xD6 => self.dei(&self.rs, u8, true), // DEIkr
0xD7 => self.deo(&self.rs, u8, true), // DEOkr
0xD8 => add(&self.rs, u8, true), // ADDkr
0xD9 => sub(&self.rs, u8, true), // SUBkr
0xDA => mul(&self.rs, u8, true), // MULkr
0xDB => div(&self.rs, u8, true), // DIVkr
0xDC => @"and"(&self.rs, u8, true), // ANDkr
0xDD => ora(&self.rs, u8, true), // ORAkr
0xDE => eor(&self.rs, u8, true), // EORkr
0xDF => sft(&self.rs, u8, true), // SFTkr
0xE0 => {
self.rs.push(u16, self.mem.peek(u16, self.pc +% 1));
self.pc +%= 2;
}, // LIT2r
0xE1 => inc(&self.rs, u16, true), // INC2kr
0xE2 => pop(&self.rs, u16, true), // POP2kr
0xE3 => nip(&self.rs, u16, true), // NIP2kr
0xE4 => swp(&self.rs, u16, true), // SWP2kr
0xE5 => rot(&self.rs, u16, true), // ROT2kr
0xE6 => dup(&self.rs, u16, true), // DUP2kr
0xE7 => ovr(&self.rs, u16, true), // OVR2kr
0xE8 => equ(&self.rs, u16, true), // EQU2kr
0xE9 => neq(&self.rs, u16, true), // NEQ2kr
0xEA => gth(&self.rs, u16, true), // GTH2kr
0xEB => lth(&self.rs, u16, true), // LTH2kr
0xEC => self.jmp(&self.rs, u16, true), // JMP2kr
0xED => self.jcn(&self.rs, u16, true), // JCN2kr
0xEE => self.jsr(&self.rs, u16, true), // JSR2kr
0xEF => self.sth(true, u16, true), // STH2kr
0xF0 => self.ldz(&self.rs, u16, true), // LDZ2kr
0xF1 => self.stz(&self.rs, u16, true), // STZ2kr
0xF2 => self.ldr(&self.rs, u16, true), // LDR2kr
0xF3 => self.str(&self.rs, u16, true), // STR2kr
0xF4 => self.lda(&self.rs, u16, true), // LDA2kr
0xF5 => self.sta(&self.rs, u16, true), // STA2kr
0xF6 => self.dei(&self.rs, u16, true), // DEI2kr
0xF7 => self.deo(&self.rs, u16, true), // DEO2kr
0xF8 => add(&self.rs, u16, true), // ADD2kr
0xF9 => sub(&self.rs, u16, true), // SUB2kr
0xFA => mul(&self.rs, u16, true), // MUL2kr
0xFB => div(&self.rs, u16, true), // DIV2kr
0xFC => @"and"(&self.rs, u16, true), // AND2kr
0xFD => ora(&self.rs, u16, true), // ORA2kr
0xFE => eor(&self.rs, u16, true), // EOR2kr
0xFF => sft(&self.rs, u16, true), // SFT2kr
}
self.pc +%= 1;
return true;
}
fn brk() void {}
fn inc(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, a +% 1);
}
fn pop(stack: *Stack, comptime T: type, comptime keep: bool) void {
if (keep) return;
_ = stack.pop(T);
}
fn nip(stack: *Stack, comptime T: type, comptime keep: bool) void {
_ = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, b);
}
fn swp(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, b);
stack.push(T, a);
}
fn rot(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
const c = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, b);
stack.push(T, c);
stack.push(T, a);
}
fn dup(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, a);
}
fn ovr(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, a);
stack.push(T, b);
stack.push(T, a);
}
fn equ(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
if (a == b) stack.push(T, 1) else stack.push(T, 0);
}
fn neq(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
if (a != b) stack.push(T, 1) else stack.push(T, 0);
}
fn gth(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
if (a > b) stack.push(T, 1) else stack.push(T, 0);
}
fn lth(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
if (a < b) stack.push(T, 1) else stack.push(T, 0);
}
fn jmp(self: *Uxn, stack: *Stack, comptime T: type, comptime keep: bool) void {
const addr = if (keep) stack.peek(T) else stack.pop(T);
switch (T) {
u8 => self.pc = @intCast(@as(i32, @intCast(self.pc)) + @as(i8, @bitCast(addr))),
u16 => self.pc = addr,
else => unreachable,
}
}
fn jcn(self: *Uxn, stack: *Stack, comptime T: type, comptime keep: bool) void {
const cond = if (keep) stack.peek(T) else stack.pop(T);
const addr = if (keep) stack.peek(T) else stack.pop(T);
if (cond == 0) return;
switch (T) {
u8 => self.pc = @intCast(@as(i32, @intCast(self.pc)) + @as(i8, @bitCast(addr))),
u16 => self.pc = addr,
else => unreachable,
}
}
fn jsr(self: *Uxn, stack: *Stack, comptime T: type, comptime keep: bool) void {
self.rs.push(u16, self.pc);
const addr = if (keep) stack.peek(T) else stack.pop(T);
switch (T) {
u8 => self.pc = @intCast(@as(i32, @intCast(self.pc)) + @as(i8, @bitCast(addr))),
u16 => self.pc = addr,
else => unreachable,
}
}
// this one uses a bool instead of a *Stack, because it needs access to both stacks
// TODO now that I think about it, just use a bool everywhere
fn sth(self: *Uxn, comptime swap: bool, comptime T: type, comptime keep: bool) void {
var src = if (swap) self.rs else self.ws;
var dst = if (swap) self.ws else self.rs;
const a = if (keep) src.peek(T) else src.pop(T);
dst.push(T, a);
}
fn ldz(self: *Uxn, stack: *Stack, comptime T: type, comptime keep: bool) void {
const addr = if (keep) stack.peek(u8) else stack.pop(u8);
stack.push(T, self.mem.peek(T, addr));
}
fn stz(self: *Uxn, stack: *Stack, comptime T: type, comptime keep: bool) void {
const addr = if (keep) stack.peek(u8) else stack.pop(u8);
const val = if (keep) stack.peek(T) else stack.pop(T);
self.mem.poke(T, addr, val);
}
fn ldr(self: *Uxn, stack: *Stack, comptime T: type, comptime keep: bool) void {
const addr = if (keep) stack.peek(u8) else stack.pop(u8);
const val = self.mem.peek(T, @intCast(@as(i32, @intCast(self.pc)) + @as(i8, @intCast(addr))));
stack.push(T, val);
}
fn str(self: *Uxn, stack: *Stack, comptime T: type, comptime keep: bool) void {
const addr = if (keep) stack.peek(u8) else stack.pop(u8);
const val = if (keep) stack.peek(T) else stack.pop(T);
self.mem.poke(T, @intCast(@as(i32, @intCast(self.pc)) + @as(i8, @bitCast(addr))), val);
}
fn lda(self: *Uxn, stack: *Stack, comptime T: type, comptime keep: bool) void {
const addr = if (keep) stack.peek(u16) else stack.pop(u16);
stack.push(T, self.mem.peek(T, addr));
}
fn sta(self: *Uxn, stack: *Stack, comptime T: type, comptime keep: bool) void {
const addr = if (keep) stack.peek(u16) else stack.pop(u16);
const val = if (keep) stack.peek(T) else stack.pop(T);
self.mem.poke(T, addr, val);
}
fn dei(self: *Uxn, stack: *Stack, comptime T: type, comptime keep: bool) void {
const d = if (keep) stack.peek(u8) else stack.pop(u8);
switch (T) {
u8 => self.dei8(self, stack, d),
u16 => self.dei16(self, stack, d),
else => @compileError("expected u8 or u16, got " + @typeName(T)),
}
stack.push(T, self.dev[d]);
}
fn deo(self: *Uxn, stack: *Stack, comptime T: type, comptime keep: bool) void {
const d = if (keep) stack.peek(u8) else stack.pop(u8);
const v = if (keep) stack.peek(T) else stack.pop(T);
if (debug) std.debug.print("DEO: dev {X}, val {X}\n", .{ d, v });
switch (T) {
u8 => {
self.dev[d] = v;
self.deo8(self, stack, d, v);
},
u16 => {
self.dev[d] = @truncate(v >> 8);
self.dev[d +% 1] = @truncate(v & 0xFF);
self.deo16(self, stack, d, v);
},
else => @compileError("expected u8 or u16, got " + @typeName(T)),
}
}
fn add(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, a +% b);
}
fn sub(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, a -% b);
}
fn mul(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, a *% b);
}
fn div(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, a / b);
}
fn @"and"(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, a & b);
}
fn ora(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, a | b);
}
fn eor(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
stack.push(T, a ^ b);
}
fn sft(stack: *Stack, comptime T: type, comptime keep: bool) void {
const a = if (keep) stack.peek(T) else stack.pop(T);
const b = if (keep) stack.peek(T) else stack.pop(T);
switch (T) {
u8 => {
const right: u3 = @truncate(b & 0b00001111);
const left: u3 = @truncate((b & 0b11110000) >> 4);
stack.push(T, (a >> right) << left);
},
u16 => {
const right: u4 = @truncate(b & 0b0000000011111111);
const left: u4 = @truncate((b & 0b1111111100000000) >> 8);
stack.push(T, (a >> right) << left);
},
else => unreachable,
}
}
test "op" {
var uxn = try Uxn.init(std.testing.allocator);
defer uxn.deinit(std.testing.allocator);
const rom = [_]u8{
0x01, 0x01, 0x01, 0x01,
0x00,
};
@memcpy(uxn.mem.m[0x100 .. rom.len + 0x100], &rom);
std.debug.print("op.inc\n", .{});
uxn.ws.push(u8, 0x01);
uxn.ws.push(u8, 0xff);
uxn.ws.push(u8, 0xfe);
uxn.ws.push(u8, 0x00);
_ = uxn.eval();
try std.testing.expectEqual(0x01, uxn.ws.pop(u8));
_ = uxn.eval();
try std.testing.expectEqual(0xff, uxn.ws.pop(u8));
_ = uxn.eval();
try std.testing.expectEqual(0x00, uxn.ws.pop(u8));
_ = uxn.eval();
try std.testing.expectEqual(0x02, uxn.ws.pop(u8));
}
pub fn formatInstruction(
bytes: []const u8,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = fmt;
_ = options;
if (!debug) return;
std.debug.assert(bytes.len == 1);
try writer.writeAll("\x1b[1;31m");
instr: {
switch (bytes[0] & 0x1F) {
0x00 => break :instr switch (bytes[0]) {
0x00 => try writer.writeAll("BRK"),
0x20 => try writer.writeAll("JCI"),
0x40 => try writer.writeAll("JMI"),
0x60 => try writer.writeAll("JSI"),
0x80 => try writer.writeAll("LIT"),
0xA0 => try writer.writeAll("LIT2"),
0xC0 => try writer.writeAll("LITr"),
0xE0 => try writer.writeAll("LIT2r"),
else => unreachable,
},
0x01 => try writer.writeAll("INC"),
0x02 => try writer.writeAll("POP"),
0x03 => try writer.writeAll("NIP"),
0x04 => try writer.writeAll("SWP"),
0x05 => try writer.writeAll("ROT"),
0x06 => try writer.writeAll("DUP"),
0x07 => try writer.writeAll("OVR"),
0x08 => try writer.writeAll("EQU"),
0x09 => try writer.writeAll("NEQ"),
0x0A => try writer.writeAll("GTH"),
0x0B => try writer.writeAll("LTH"),
0x0C => try writer.writeAll("JMP"),
0x0D => try writer.writeAll("JCN"),
0x0E => try writer.writeAll("JSR"),
0x0F => try writer.writeAll("STH"),
0x10 => try writer.writeAll("LDZ"),
0x11 => try writer.writeAll("STZ"),
0x12 => try writer.writeAll("LDR"),
0x13 => try writer.writeAll("STR"),
0x14 => try writer.writeAll("LDA"),
0x15 => try writer.writeAll("STA"),
0x16 => try writer.writeAll("DEI"),
0x17 => try writer.writeAll("DEO"),
0x18 => try writer.writeAll("ADD"),
0x19 => try writer.writeAll("SUB"),
0x1A => try writer.writeAll("MUL"),
0x1B => try writer.writeAll("DIV"),
0x1C => try writer.writeAll("AND"),
0x1D => try writer.writeAll("ORA"),
0x1E => try writer.writeAll("EOR"),
0x1F => try writer.writeAll("SFT"),
else => unreachable,
}
if (bytes[0] & 0x20 == 0x20) try writer.writeByte('2');
if (bytes[0] & 0x40 == 0x40) try writer.writeByte('k');
if (bytes[0] & 0x80 == 0x80) try writer.writeByte('r');
}
try writer.writeAll("\x1b[0m");
}
pub fn fmtInstrs(bytes: []const u8) std.fmt.Formatter(formatInstruction) {
return .{ .data = bytes };
}