init
This commit is contained in:
commit
ca67490e6f
7 changed files with 408 additions and 0 deletions
139
src/main.zig
Normal file
139
src/main.zig
Normal file
|
@ -0,0 +1,139 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
var uxn = Uxn{
|
||||
.mem = try allocator.alloc(u8, 0xFFFF),
|
||||
.ws = try allocator.alloc(u8, 0xFF),
|
||||
.rs = try allocator.alloc(u8, 0xFF),
|
||||
.wsp = 0,
|
||||
.rsp = 0,
|
||||
.pc = 0x0100,
|
||||
};
|
||||
defer allocator.free(uxn.mem);
|
||||
defer allocator.free(uxn.ws);
|
||||
defer allocator.free(uxn.rs);
|
||||
|
||||
while (true) uxn.loop();
|
||||
}
|
||||
|
||||
pub const Uxn = struct {
|
||||
mem: [0xFFFF]u8,
|
||||
// ws: [0xFF]u8,
|
||||
// rs: [0xFF]u8,
|
||||
// wsp: u8,
|
||||
// rsp: u8,
|
||||
ws: Stack,
|
||||
rs: Stack,
|
||||
pc: u16,
|
||||
|
||||
const Stack = struct {
|
||||
s: *[0xFF]u8,
|
||||
sp: *u8,
|
||||
};
|
||||
|
||||
pub fn loop(self: *Uxn) void {
|
||||
switch (self.mem[self.pc]) {
|
||||
0x00 => {}, // BRK
|
||||
0x01 => inc(&self.ws, false, false), // INC
|
||||
0x02 => pop(&self.ws, false, false), // POP
|
||||
0x03 => nip(&self.ws, false, false), // NIP
|
||||
0x04 => swp(&self.ws, false, false), // SWP
|
||||
0x05 => {}, // ROT
|
||||
0x06 => {}, // DUP
|
||||
0x07 => {}, // OVR
|
||||
0x08 => {}, // EQU
|
||||
0x09 => {}, // NEQ
|
||||
0x0A => {}, // GTH
|
||||
0x0B => {}, // LTH
|
||||
0x0C => {}, // JMP
|
||||
0x0D => {}, // JCN
|
||||
0x0E => {}, // JSR
|
||||
0x0F => {}, // STH
|
||||
0x10 => {}, // LDZ
|
||||
0x11 => {}, // STZ
|
||||
0x12 => {}, // LDR
|
||||
0x13 => {}, // STR
|
||||
0x14 => {}, // LDA
|
||||
0x15 => {}, // STA
|
||||
0x16 => {}, // DEI
|
||||
0x17 => {}, // DEO
|
||||
0x18 => {}, // ADD
|
||||
0x19 => {}, // SUB
|
||||
0x1A => {}, // MUL
|
||||
0x1B => {}, // DIV
|
||||
0x1C => {}, // AND
|
||||
0x1D => {}, // ORA
|
||||
0x1E => {}, // EOR
|
||||
0x1F => {}, // SFT
|
||||
}
|
||||
}
|
||||
|
||||
fn brk() void {}
|
||||
|
||||
fn inc(stack: *Stack, comptime short: bool, comptime keep: bool) void {
|
||||
_ = short;
|
||||
if (keep) {
|
||||
stack.s[stack.sp +% 1] = stack.s[stack.sp] +% 1;
|
||||
stack.sp +%= 1;
|
||||
} else {
|
||||
stack.s[stack.sp] +%= 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn pop(stack: *Stack, comptime short: bool, comptime keep: bool) void {
|
||||
if (!keep) {
|
||||
if (short) stack.sp -%= 2 else stack.sp -%= 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn nip(stack: *Stack, comptime short: bool, comptime keep: bool) void {
|
||||
_ = short;
|
||||
if (!keep) {
|
||||
stack.sp -%= 1;
|
||||
stack.s[stack.sp] = stack.s[stack.sp +% 1];
|
||||
}
|
||||
}
|
||||
|
||||
fn swp(stack: *Stack, comptime short: bool, comptime keep: bool) void {
|
||||
_ = short;
|
||||
if (keep) {
|
||||
stack.s[stack.sp +% 1] = stack.s[stack.sp];
|
||||
stack.s[stack.sp +% 2] = stack.s[stack.sp -% 1];
|
||||
} else {
|
||||
const a = stack.s[stack.sp -% 1];
|
||||
stack.s[stack.sp -% 1] = stack.s[stack.sp];
|
||||
stack.sp = a;
|
||||
}
|
||||
}
|
||||
|
||||
fn dup() void {}
|
||||
fn ovr() void {}
|
||||
fn equ() void {}
|
||||
fn neq() void {}
|
||||
fn gth() void {}
|
||||
fn lth() void {}
|
||||
fn jmp() void {}
|
||||
fn jcn() void {}
|
||||
fn jsr() void {}
|
||||
fn sth() void {}
|
||||
fn ldz() void {}
|
||||
fn stz() void {}
|
||||
fn ldr() void {}
|
||||
fn str() void {}
|
||||
fn lda() void {}
|
||||
fn sta() void {}
|
||||
fn dei() void {}
|
||||
fn deo() void {}
|
||||
fn add() void {}
|
||||
fn sub() void {}
|
||||
fn mul() void {}
|
||||
fn div() void {}
|
||||
fn @"and"() void {}
|
||||
fn ora() void {}
|
||||
fn eor() void {}
|
||||
fn sft() void {}
|
||||
};
|
10
src/root.zig
Normal file
10
src/root.zig
Normal file
|
@ -0,0 +1,10 @@
|
|||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
export fn add(a: i32, b: i32) i32 {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
test "basic add functionality" {
|
||||
try testing.expect(add(3, 7) == 10);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue