610 lines
24 KiB
Zig
610 lines
24 KiB
Zig
|
const std = @import("std");
|
||
|
const Uxn = @This();
|
||
|
|
||
|
mem: Memory = .{ .m = undefined },
|
||
|
ws: Stack = .{ .s = undefined },
|
||
|
rs: Stack = .{ .s = undefined },
|
||
|
dev: [0x100]u8 = undefined,
|
||
|
pc: u16,
|
||
|
|
||
|
const Memory = struct {
|
||
|
m: [0x10000]u8,
|
||
|
|
||
|
pub fn peek(self: *Memory, comptime T: type, idx: u16) T {
|
||
|
return switch (T) {
|
||
|
u8 => self.m[idx],
|
||
|
u16 => @as(u16, @intCast(self.m[idx])) << 8 | self.m[idx +% 1],
|
||
|
else => unreachable,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
pub fn poke(self: *Memory, comptime T: type, idx: u16, val: T) void {
|
||
|
switch (T) {
|
||
|
u8 => self.m[idx] = val,
|
||
|
u16 => {
|
||
|
self.m[idx] = @truncate(val >> 8);
|
||
|
self.m[idx +% 1] = @truncate(val & 0xFF);
|
||
|
},
|
||
|
else => unreachable,
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
const Stack = struct {
|
||
|
s: [0x100]u8,
|
||
|
sp: u8 = 0,
|
||
|
|
||
|
pub fn peek(self: *Stack, comptime T: type) T {
|
||
|
return switch (T) {
|
||
|
u8 => self.s[self.sp],
|
||
|
u16 => @as(u16, @intCast(self.s[self.sp])) << 8 | self.s[self.sp +% 1],
|
||
|
else => unreachable,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
pub fn poke(self: *Stack, comptime T: type, v: T) void {
|
||
|
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 => unreachable,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn pop(self: *Stack, comptime T: type) T {
|
||
|
self.sp -%= @intCast(@divExact(@typeInfo(T).Int.bits, 8));
|
||
|
return self.peek(T);
|
||
|
}
|
||
|
|
||
|
pub fn push(self: *Stack, comptime T: type, v: T) void {
|
||
|
self.poke(T, v);
|
||
|
self.sp +%= @intCast(@divExact(@typeInfo(T).Int.bits, 8));
|
||
|
}
|
||
|
};
|
||
|
|
||
|
pub fn eval(self: *Uxn) bool {
|
||
|
switch (self.mem.m[self.pc]) {
|
||
|
0x00 => return true, // 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 => {}, // 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 => {}, // 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.m[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.m[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.m[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.m[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 false;
|
||
|
}
|
||
|
|
||
|
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,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
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);
|
||
|
std.debug.print("DEO: v{X} d{X}\n", .{ v, d });
|
||
|
switch (T) {
|
||
|
u8 => self.dev[d] = v,
|
||
|
u16 => {
|
||
|
self.dev[d] = @truncate(v >> 8);
|
||
|
self.dev[d +% 1] = @truncate(v & 0xFF);
|
||
|
},
|
||
|
else => unreachable,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// TODO move the mem/stack to just below their respective definitions
|
||
|
|
||
|
test "memory peek/poke" {
|
||
|
var mem = Memory{ .m = undefined };
|
||
|
mem.poke(u16, 0, 0xABCD);
|
||
|
mem.poke(u8, 2, 0xAA);
|
||
|
try std.testing.expectEqual(0xABCD, mem.peek(u16, 0));
|
||
|
try std.testing.expectEqual(0xAA, mem.peek(u8, 2));
|
||
|
}
|
||
|
|
||
|
test "stack push/pop" {
|
||
|
var stack = Stack{ .s = undefined };
|
||
|
stack.push(u16, 0xABCD);
|
||
|
stack.push(u8, 0xAA);
|
||
|
try std.testing.expectEqual(0xAA, stack.pop(u8));
|
||
|
try std.testing.expectEqual(0xABCD, stack.pop(u16));
|
||
|
}
|
||
|
|
||
|
// test "arithmetic instructions" {
|
||
|
// var uxn = Uxn{
|
||
|
// .mem = .{ .m = undefined },
|
||
|
// .ws = .{ .s = undefined },
|
||
|
// .rs = .{ .s = undefined },
|
||
|
// .pc = 0,
|
||
|
// };
|
||
|
// uxn.mem.m[0] = 0x18;
|
||
|
// uxn.mem.m[1] = 0x19;
|
||
|
// uxn.mem.m[2] = 0x1A;
|
||
|
// uxn.mem.m[3] = 0x1B;
|
||
|
// uxn.ws.push(u8, 4);
|
||
|
// uxn.ws.push(u8, 20);
|
||
|
// uxn.ws.push(u8, 6);
|
||
|
// uxn.loop();
|
||
|
// uxn.loop();
|
||
|
// try std.testing.expectEqual(18, uxn.ws.pop(u8));
|
||
|
// }
|