const std = @import("std"); pub fn main() !void { // var gpa = std.heap.GeneralPurposeAllocator(.{}){}; // defer _ = gpa.deinit(); // const allocator = gpa.allocator(); const mem: [0xFFFF]u8 = undefined; const ws: [0xFF]u8 = undefined; const rs: [0xFF]u8 = undefined; var uxn = Uxn{ .mem = .{ .m = mem }, .ws = .{ .s = ws }, .rs = .{ .s = rs }, .pc = 0x100, }; while (true) uxn.loop(); } pub const Uxn = struct { mem: Memory, ws: Stack, rs: Stack, pc: u16, const Memory = struct { m: [0xFFFF]u8, pub fn peek(self: *Memory, comptime T: type, idx: u16) T { return switch (T) { u8 => self.m[idx], u16 => self.m[idx], else => unreachable, }; } pub fn poke(self: *Memory, comptime T: type, idx: u16, val: T) void { switch (T) { u8 => self.m[idx] = val, u16 => {}, else => unreachable, } } }; const Stack = struct { s: [0xFF]u8, sp: u8 = 0, pub fn peek(self: *Stack, comptime T: type) T { return switch (T) { u8 => self.s[self.sp], u16 => @intCast(self.s[self.sp]), // but actually do the conversion else => unreachable, }; } pub fn poke(self: *Stack, comptime T: type, v: T) void { switch (T) { u8 => self.s[self.sp] = v, u16 => {}, else => unreachable, } } pub fn pop(self: *Stack, comptime T: type) T { switch (T) { u8 => { self.sp -%= 1; return self.s[self.sp +% 1]; }, u16 => { self.sp -%= 2; return self.s[self.sp +% 1]; }, else => unreachable, } } pub fn push(self: *Stack, comptime T: type, v: T) void { switch (T) { u8 => { self.sp +%= 1; self.s[self.sp] = v; }, u16 => { self.sp +%= 2; self.s[self.sp] = @truncate(v); }, else => unreachable, } } }; pub fn loop(self: *Uxn) void { switch (self.mem.m[self.pc]) { 0x00 => {}, // 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 => dei(&self.ws, u8, false), // DEI 0x17 => 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 => dei(&self.ws, u16, false), // DEI2 0x37 => 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 => dei(&self.rs, u8, false), // DEIr 0x57 => 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 => {}, // 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 => dei(&self.rs, u16, false), // DEI2r 0x77 => 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 => {}, // 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 => dei(&self.ws, u8, true), // DEIk 0x97 => 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 => {}, // 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 => dei(&self.ws, u16, true), // DEI2k 0xB7 => 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 => {}, // 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 => dei(&self.rs, u8, true), // DEIkr 0xD7 => 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 => {}, // 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 => dei(&self.rs, u16, true), // DEI2kr 0xF7 => 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 } } 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)); // switch (T) { // u8 => stack.push(T, self.mem[addr]), // u16 => {}, // else => unreachable, // } } 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(stack: *Stack, comptime T: type, comptime keep: bool) void { _ = stack; _ = T; _ = keep; } fn deo(stack: *Stack, comptime T: type, comptime keep: bool) void { _ = stack; _ = T; _ = keep; } 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 "uxnasm" { const rom = [_]u8{ 0xa0, 0x01, 0x07, 0x80, 0x10, 0x37, 0x00, 0x80, 0x12, 0x16, 0x06, 0x20, 0x00, 0x05, 0x02, 0x60, 0x00, 0x65, 0x00, 0x60, 0x00, 0x01, 0x00, 0x80, 0x20, 0x8a, 0x0c, 0x04, 0x02, 0x80, 0x20, 0x07, 0x80, 0xfb, 0x13, 0x26, 0xa0, 0x20, 0x20, 0x28, 0x20, 0x00, 0x42, 0x80, 0x20, 0x09, 0x20, 0x00, 0x1c, 0x06, 0x80, 0x5b, 0x08, 0x20, 0x00, 0x1a, 0x06, 0x80, 0x5d, 0x08, 0x20, 0x00, 0x13, 0x06, 0x80, 0x28, 0x08, 0x20, 0x00, 0x29, 0x06, 0x80, 0x29, 0x08, 0x20, 0x00, 0x22, 0x80, 0x01, 0x20, 0x00, 0x02, 0x02, 0x6c, 0x80, 0x20, 0x07, 0x80, 0xfb, 0x13, 0x26, 0xa0, 0x20, 0x20, 0x28, 0x20, 0x00, 0x0b, 0x02, 0x80, 0x00, 0xa0, 0x08, 0x6b, 0xb5, 0x21, 0x80, 0xf9, 0x33, 0x22, 0x6c, 0x80, 0x28, 0x19, 0xa0, 0x01, 0x4e, 0x15, 0x6c, 0xa0, 0x01, 0x66, 0x34, 0x21, 0x26, 0xa0, 0x04, 0x1a, 0x35, 0xa0, 0x04, 0x51, 0x35, 0xa0, 0x08, 0x6b, 0x60, 0x04, 0x6d, 0x80, 0x01, 0x60, 0x00, 0x91, 0x20, 0x00, 0x85, 0x80, 0x00, 0x60, 0x00, 0x89, 0x20, 0x00, 0x7d, 0xa0, 0x80, 0x00, 0x80, 0x62, 0x30, 0x21, 0x38, 0xa0, 0x81, 0x00, 0x94, 0x80, 0x18, 0x17, 0x21, 0xa9, 0x20, 0xff, 0xf7, 0x22, 0x22, 0xa0, 0x04, 0x1a, 0x34, 0xa0, 0x04, 0x51, 0x34, 0xa1, 0x21, 0x14, 0x20, 0x00, 0x26, 0xa0, 0x00, 0x03, 0x38, 0x94, 0x80, 0x41, 0x19, 0x80, 0x1a, 0x0b, 0x20, 0x00, 0x18, 0x26, 0xa0, 0x06, 0x5e, 0x60, 0x03, 0xd1, 0x20, 0x00, 0x0e, 0xa0, 0x06, 0x52, 0x60, 0x04, 0x3d, 0x26, 0x60, 0x04, 0x3e, 0xa0, 0x0a, 0x19, 0x17, 0x60, 0x03, 0xb1, 0x21, 0xaa, 0x20, 0xff, 0xcc, 0x22, 0x22, 0xa0, 0x06, 0x64, 0x60, 0x04, 0x25, 0x80, 0x62, 0x30, 0xa0, 0x00, 0xff, 0x39, 0x60, 0x04, 0x26, 0xa0, 0x06, 0x74, 0x60, 0x04, 0x15, 0xa0, 0x04, 0x34, 0x34, 0x60, 0x04, 0x19, 0xa0, 0x06, 0x80, 0x60, 0x04, 0x08, 0xa0, 0x06, 0x7c, 0x60, 0x04, 0x02, 0xa0, 0x80, 0x66, 0x10, 0x1d, 0x80, 0x0f, 0x17, 0x6c, 0x80, 0x67, 0x11, 0xa0, 0x01, 0x00, 0x60, 0x01, 0x24, 0x80, 0x00, 0xa0, 0x05, 0x6a, 0x15, 0xa0, 0x06, 0x5e, 0x60, 0x01, 0x94, 0xa0, 0x08, 0x6b, 0x26, 0x80, 0x64, 0x31, 0x26, 0x60, 0x00, 0x13, 0x80, 0x66, 0x10, 0x20, 0x00, 0x08, 0x60, 0x03, 0x4f, 0x21, 0x94, 0x20, 0xff, 0xea, 0x22, 0x80, 0x66, 0x10, 0x6c, 0x94, 0x80, 0x08, 0x13, 0xa0, 0x07, 0x6b, 0xa0, 0x07, 0x3b, 0x94, 0x80, 0x00, 0x09, 0x20, 0x00, 0x04, 0x23, 0x21, 0x34, 0x2c, 0xa0, 0x00, 0x03, 0x38, 0xaa, 0x20, 0xff, 0xed, 0x22, 0x22, 0x60, 0x02, 0x71, 0x20, 0x00, 0x8f, 0x60, 0x02, 0x81, 0x20, 0x00, 0x6e, 0x40, 0x00, 0x4e, 0x21, 0x60, 0x01, 0xf3, 0x40, 0x00, 0xc5, 0x21, 0x60, 0x01, 0xec, 0x80, 0x60, 0x30, 0x38, 0x40, 0x00, 0xba, 0x21, 0x40, 0x01, 0x32, 0x21, 0x60, 0x01, 0x0c, 0x40, 0x01, 0x5a, 0x80, 0x80, 0x60, 0x00, 0x9b, 0x21, 0x60, 0x02, 0x1b, 0x40, 0x00, 0x94, 0x80, 0x80, 0x60, 0x00, 0x8f, 0x21, 0x60, 0x01, 0xce, 0x03, 0x40, 0x00, 0x87, 0x80, 0xa0, 0x60, 0x00, 0x82, 0x21, 0x60, 0x01, 0xc1, 0x40, 0x00, 0x77, 0x21, 0x80, 0x20, 0x40, 0x00, 0x65, 0x21, 0x80, 0x40, 0x40, 0x00, 0x5f, 0x80, 0x60, 0x40, 0x00, 0x5a, 0x21, 0x40, 0x00, 0x1a, 0x21, 0x40, 0x00, 0x0a, 0x22, 0x40, 0x02, 0xa1, 0x6c, 0x94, 0x60, 0x00, 0x59, 0x21, 0x94, 0x20, 0xff, 0xf7, 0x22, 0x6c, 0x60, 0x02, 0x1e, 0x40, 0x00, 0x4c, 0x26, 0x60, 0x02, 0xa7, 0x03, 0x06, 0x80, 0x04, 0x08, 0x80, 0x50, 0x1f, 0x80, 0x80, 0x1d, 0x60, 0x00, 0x3a, 0x40, 0x00, 0x05, 0x26, 0x60, 0x02, 0x92, 0x03, 0x06, 0x80, 0x02, 0x09, 0x20, 0x00, 0x08, 0x02, 0x60, 0x02, 0xca, 0x03, 0x40, 0x00, 0x23, 0x80, 0x04, 0x09, 0x20, 0x00, 0x06, 0x60, 0x02, 0xbd, 0x40, 0x00, 0x13, 0x22, 0xa0, 0x06, 0x9d, 0x40, 0x00, 0x23, 0x60, 0x00, 0x0d, 0x60, 0x01, 0x4d, 0x80, 0x60, 0x30, 0x21, 0x21, 0x39, 0x04, 0x60, 0x00, 0x00, 0x06, 0x80, 0x60, 0x30, 0xa1, 0x2f, 0xa0, 0x80, 0x00, 0x38, 0x15, 0x60, 0x00, 0x31, 0x6f, 0x80, 0x60, 0x31, 0x6c, 0xa0, 0x06, 0x88, 0x60, 0x02, 0xc2, 0x60, 0x02, 0xbf, 0xa0, 0x20, 0x19, 0x17, 0x80, 0x64, 0x30, 0x60, 0x02, 0xba, 0xa0, 0x06, 0x6f, 0x60, 0x02, 0xaf, 0x80, 0x00, 0x30, 0x60, 0x02, 0xae, 0xa0, 0x2e, 0x19, 0x17, 0xa0, 0x0a, 0x19, 0x17, 0xa0, 0x01, 0x66, 0x11, 0x6c, 0x20, 0x00, 0x01, 0x6c, 0x80, 0x67, 0x10, 0x20, 0x00, 0x01, 0x6c, 0x80, 0x60, 0x30, 0x26, 0x07, 0x20, 0x00, 0x06, 0xa0, 0x06, 0xce, 0x60, 0xff, 0xbb, 0xa0, 0x80, 0x00, 0x2b, 0x20, 0x00, 0x06, 0xa0, 0x06, 0xa4, 0x60, 0xff, 0xae, 0x80, 0x62, 0x31, 0x6c, 0x26, 0x60, 0x01, 0xf2, 0xa0, 0x00, 0x30, 0x60, 0x01, 0xec, 0x38, 0xa0, 0x00, 0x30, 0x2b, 0x20, 0x00, 0x06, 0xa0, 0x06, 0xb5, 0x40, 0xff, 0x92, 0xa0, 0x00, 0x00, 0x60, 0x02, 0x45, 0xa0, 0x00, 0x30, 0x6c, 0x26, 0xc0, 0x00, 0x94, 0x80, 0x2f, 0x08, 0x20, 0x00, 0x09, 0x94, 0xcf, 0x11, 0x41, 0x21, 0x94, 0x20, 0xff, 0xf0, 0x22, 0x80, 0x00, 0x4f, 0x11, 0xa0, 0x00, 0x00, 0xa0, 0x00, 0x30, 0x60, 0x02, 0x20, 0xa0, 0x2f, 0x00, 0xa0, 0x00, 0x30, 0x60, 0x01, 0xa5, 0xa1, 0x80, 0xc9, 0x33, 0x35, 0x80, 0x67, 0x10, 0x20, 0x00, 0x02, 0x22, 0x6c, 0x60, 0x00, 0xe3, 0x20, 0x00, 0x34, 0x60, 0x00, 0xf3, 0x20, 0x00, 0x2e, 0x26, 0x60, 0x00, 0x38, 0x21, 0x1d, 0x20, 0x00, 0x2c, 0x80, 0x60, 0x30, 0xa0, 0x00, 0x00, 0xaf, 0x61, 0x61, 0x35, 0x80, 0x00, 0xef, 0x61, 0x15, 0x26, 0xef, 0x60, 0x01, 0xe1, 0x60, 0x01, 0x71, 0x6f, 0x38, 0x21, 0x80, 0xe7, 0x33, 0xa0, 0x00, 0x00, 0x21, 0x80, 0xfa, 0x33, 0x6c, 0x22, 0xa0, 0x06, 0xc7, 0x40, 0xff, 0x10, 0x22, 0xa0, 0x06, 0x93, 0x40, 0xff, 0x09, 0x80, 0x11, 0x33, 0xa0, 0x04, 0x1a, 0x34, 0xa0, 0x00, 0x00, 0xa8, 0x20, 0x00, 0x16, 0xa0, 0x00, 0x03, 0x38, 0x26, 0xa0, 0x00, 0x00, 0x60, 0x01, 0x43, 0x20, 0x00, 0x0e, 0x60, 0x01, 0x31, 0x21, 0xaa, 0x20, 0xff, 0xe6, 0x22, 0x22, 0xa0, 0xff, 0xff, 0x6c, 0xa0, 0x00, 0x03, 0x39, 0x23, 0x6c, 0x60, 0x00, 0x6b, 0x20, 0x01, 0x63, 0x40, 0x00, 0x11, 0x94, 0x80, 0x7b, 0x09, 0x20, 0x00, 0x04, 0x22, 0x60, 0x00, 0xdc, 0x80, 0x67, 0x10, 0x20, 0x00, 0x29, 0x94, 0x80, 0x26, 0x09, 0x20, 0x00, 0x04, 0x21, 0x60, 0xff, 0x0a, 0x94, 0x80, 0x2f, 0x09, 0x20, 0x00, 0x04, 0x21, 0x60, 0xfe, 0xff, 0x60, 0xff, 0x9d, 0xa1, 0xa0, 0x00, 0x00, 0x28, 0x20, 0x00, 0x09, 0xa1, 0x21, 0x94, 0x01, 0x05, 0x05, 0x15, 0x34, 0x6c, 0xa0, 0x06, 0xab, 0x40, 0xfe, 0x8f, 0x60, 0xff, 0xbc, 0x80, 0x60, 0x30, 0x21, 0x21, 0x39, 0x26, 0xa0, 0x00, 0x80, 0x38, 0x02, 0x20, 0x00, 0x02, 0x03, 0x6c, 0x80, 0x67, 0x10, 0x20, 0x00, 0x06, 0xa0, 0x06, 0xbe, 0x60, 0xfe, 0x6f, 0x22, 0x80, 0xff, 0x6c, 0x26, 0x94, 0x60, 0x00, 0xde, 0x01, 0x20, 0x00, 0x04, 0x22, 0x80, 0x00, 0x6c, 0x21, 0x94, 0x20, 0xff, 0xef, 0x22, 0x80, 0x01, 0x6c, 0x26, 0x60, 0x00, 0x0d, 0x20, 0x00, 0x07, 0x26, 0xa0, 0x07, 0x38, 0x40, 0x00, 0xb0, 0x80, 0x01, 0x6c, 0x26, 0x80, 0x0e, 0x33, 0xa0, 0x20, 0x00, 0x80, 0x00, 0x07, 0x80, 0x03, 0x1a, 0xa0, 0x06, 0xd8, 0x38, 0xa0, 0x00, 0x00, 0x60, 0x00, 0x96, 0x20, 0x00, 0x0a, 0x01, 0x8a, 0x20, 0xff, 0xe8, 0x22, 0x22, 0x80, 0x00, 0x6c, 0x03, 0x06, 0x80, 0x00, 0x08, 0x80, 0x70, 0x1f, 0x1d, 0x0f, 0xa0, 0x00, 0x03, 0x38, 0x94, 0x20, 0x00, 0x03, 0x22, 0x4f, 0x6c, 0x94, 0x80, 0x32, 0x09, 0x20, 0x00, 0x03, 0xc0, 0x20, 0x5d, 0x94, 0x80, 0x6b, 0x09, 0x20, 0x00, 0x03, 0xc0, 0x80, 0x5d, 0x94, 0x80, 0x72, 0x09, 0x20, 0x00, 0x03, 0xc0, 0x40, 0x5d, 0x21, 0x40, 0xff, 0xd7, 0x80, 0x00, 0x81, 0x80, 0xfb, 0x13, 0x06, 0xa0, 0x07, 0x6b, 0xa1, 0x80, 0xfa, 0x33, 0x15, 0x80, 0x21, 0x18, 0x80, 0x18, 0x13, 0xa0, 0x05, 0x94, 0x6c, 0x80, 0xec, 0x32, 0xa0, 0x00, 0x01, 0x39, 0x94, 0x60, 0xff, 0xeb, 0x60, 0xfe, 0x69, 0x80, 0xde, 0x33, 0x6c, 0xce, 0xbb, 0x00, 0x00, 0x21, 0x94, 0x20, 0xff, 0xfb, 0x6c, 0x26, 0x60, 0xff, 0xf7, 0x24, 0x39, 0x6c, 0x2f, 0x94, 0x20, 0x00, 0x05, 0x14, 0x54, 0x4f, 0x08, 0x6c, 0x94, 0xd4, 0x4f, 0x09, 0x20, 0xff, 0xf4, 0x21, 0x61, 0x40, 0xff, 0xeb, 0x2f, 0xd4, 0x94, 0x4f, 0x09, 0x20, 0x00, 0x02, 0x61, 0x21, 0x74, 0x34, 0x6f, 0x28, 0x6c, 0x80, 0x30, 0x19, 0x06, 0x80, 0x0a, 0x0b, 0x20, 0x00, 0x0d, 0x80, 0x27, 0x19, 0x06, 0x80, 0x10, 0x0b, 0x20, 0x00, 0x03, 0x02, 0x80, 0xff, 0x6c, 0xe0, 0x00, 0x00, 0xc0, 0x40, 0x7f, 0x94, 0x60, 0xff, 0xde, 0xc0, 0x00, 0x0f, 0x78, 0x21, 0x94, 0x20, 0xff, 0xf0, 0x22, 0x6f, 0x6c, 0x80, 0x00, 0x05, 0x05, 0x94, 0x80, 0x20, 0x0a, 0x20, 0x00, 0x01, 0x95, 0x21, 0x94, 0x20, 0xff, 0xf3, 0x15, 0x6c, 0x2f, 0x94, 0x80, 0x00, 0xef, 0x35, 0x61, 0x21, 0x94, 0x20, 0xff, 0xf5, 0x22, 0x62, 0x6c, 0x94, 0x80, 0x19, 0x17, 0x21, 0x94, 0x20, 0xff, 0xf7, 0x22, 0x6c, 0xa0, 0x27, 0x10, 0xe0, 0x00, 0xfb, 0xbb, 0xa0, 0x00, 0x0a, 0xbb, 0x3a, 0x39, 0x44, 0x88, 0x07, 0xcf, 0x08, 0x1c, 0x20, 0x00, 0x08, 0x06, 0x80, 0x30, 0x18, 0x80, 0x19, 0x17, 0x41, 0x22, 0xa0, 0x00, 0x0a, 0x3b, 0x44, 0x41, 0xcf, 0x20, 0xff, 0xdd, 0x62, 0x22, 0x22, 0x6c, 0x2d, 0x2d, 0x20, 0x55, 0x6e, 0x75, 0x73, 0x65, 0x64, 0x3a, 0x20, 0x00, 0x52, 0x45, 0x53, 0x45, 0x54, 0x00, 0x41, 0x73, 0x73, 0x65, 0x6d, 0x62, 0x6c, 0x65, 0x64, 0x20, 0x00, 0x20, 0x69, 0x6e, 0x20, 0x00, 0x20, 0x62, 0x79, 0x74, 0x65, 0x73, 0x28, 0x00, 0x29, 0x2e, 0x0a, 0x00, 0x20, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x73, 0x00, 0x21, 0x21, 0x20, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x3a, 0x20, 0x00, 0x44, 0x75, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x65, 0x00, 0x4e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x00, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x00, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65, 0x00, 0x53, 0x75, 0x62, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x00, 0x44, 0x69, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x00, 0x53, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x00, 0x5a, 0x65, 0x72, 0x6f, 0x2d, 0x70, 0x61, 0x67, 0x65, 0x00, 0x4c, 0x49, 0x54, 0x49, 0x4e, 0x43, 0x50, 0x4f, 0x50, 0x4e, 0x49, 0x50, 0x53, 0x57, 0x50, 0x52, 0x4f, 0x54, 0x44, 0x55, 0x50, 0x4f, 0x56, 0x52, 0x45, 0x51, 0x55, 0x4e, 0x45, 0x51, 0x47, 0x54, 0x48, 0x4c, 0x54, 0x48, 0x4a, 0x4d, 0x50, 0x4a, 0x43, 0x4e, 0x4a, 0x53, 0x52, 0x53, 0x54, 0x48, 0x4c, 0x44, 0x5a, 0x53, 0x54, 0x5a, 0x4c, 0x44, 0x52, 0x53, 0x54, 0x52, 0x4c, 0x44, 0x41, 0x53, 0x54, 0x41, 0x44, 0x45, 0x49, 0x44, 0x45, 0x4f, 0x41, 0x44, 0x44, 0x53, 0x55, 0x42, 0x4d, 0x55, 0x4c, 0x44, 0x49, 0x56, 0x41, 0x4e, 0x44, 0x4f, 0x52, 0x41, 0x45, 0x4f, 0x52, 0x53, 0x46, 0x54, 0x42, 0x52, 0x4b, 0x7c, 0x02, 0x82, 0x24, 0x02, 0x89, 0x40, 0x02, 0x94, 0x26, 0x02, 0x98, 0x23, 0x02, 0xd5, 0x7d, 0x02, 0xdd, 0x2c, 0x02, 0x9f, 0x5f, 0x02, 0xa4, 0x2e, 0x02, 0xab, 0x2d, 0x02, 0xb0, 0x3b, 0x02, 0xb8, 0x3d, 0x02, 0xbd, 0x3f, 0x02, 0xc4, 0x21, 0x02, 0xca, 0x22, 0x02, 0xd9, 0x7e, 0x02, 0xe1, }; const mem: [0xFFFF]u8 = undefined; const ws: [0xFF]u8 = undefined; const rs: [0xFF]u8 = undefined; var uxn = Uxn{ .mem = .{ .m = mem }, .ws = .{ .s = ws, .sp = 0 }, .rs = .{ .s = rs, .sp = 0 }, .pc = 0x100, }; @memcpy(uxn.mem.m[0x100 .. 0x100 + rom.len], &rom); while (true) uxn.loop(); }