Skip to content

Commit

Permalink
fix most of u16 arithmethics
Browse files Browse the repository at this point in the history
  • Loading branch information
Maya Karabula-Stysiak committed Apr 30, 2023
1 parent ea631e0 commit 314a50e
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 44 deletions.
4 changes: 2 additions & 2 deletions devices/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function system_deo(u: Uxn, d: number[], port: number)
system_cmd(u.ram, PEEK2(d.slice(2)));
break;
case 0xe:
console.log('DEO')
// console.log('DEO')
system_inspect(u);
break;
}
Expand All @@ -80,7 +80,7 @@ export function uxn_halt(u: Uxn, instr: number, err: number, addr: number)
const d = u.dev.slice(0x00);
const handler = PEEK2(d);

console.log('HALT', d, handler)
// console.log('HALT', d, handler)

if(handler) {
u.wst.ptr = 4;
Expand Down
2 changes: 1 addition & 1 deletion examples/pebble.rom
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
�p��e��b��b��l��e��<��3��U��X��N��
�p��e��b��b��l��e����<��3��U��X��N��
�
1 change: 1 addition & 0 deletions examples/pebble.tal
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
LIT "b EMIT
LIT "l EMIT
LIT "e EMIT
LIT 10 EMIT
LIT "< EMIT
LIT "3 EMIT
LIT "U EMIT
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@
}
</style>
</body>
</html>
</html>
4 changes: 4 additions & 0 deletions out.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ let error = ''
export const out = (c: string) => {
output += c;

// console.log({output})

console.log(c)

const element = document.getElementById('stdout');
if (element) {
element.textContent = output;
Expand Down
4 changes: 2 additions & 2 deletions pebble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ let error = ''
const out = (c: string) => {
output += c;

simply.text({ title: output, subtitle: '' });
// simply.text({ title: output, subtitle: '' });
}

const outError = (c: string) => {
error += c;

simply.text({ title: error, subtitle: '' });
// simply.text({ title: error, subtitle: '' });
}

// Short reports whether the opcode has the short flag set.
Expand Down
131 changes: 95 additions & 36 deletions uxn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const u8 = (a: number) => {

export function POKE2(d: number[], addr: number, v: number) {
d[addr] = u16(v >> 8);
d[addr + 1] = u16(v);
d[addr + 1] = u8(v);
}

export function PEEK2(d: number[]): number {
Expand Down Expand Up @@ -67,51 +67,49 @@ export function uxn_eval(u: Uxn, pc: number): number {
const T = () => u16(s.dat[s.ptr - 1]);
const N = () => u16(s.dat[s.ptr - 2]);
const L = () => u16(s.dat[s.ptr - 3])
const H2 = () => u16(u16(s.dat[s.ptr - 3] << 8) | u16(s.dat[s.ptr - 2]))
const T2 = () => u16(u16(s.dat[s.ptr - 2] << 8) | u16(s.dat[s.ptr - 1]))
const N2 = () => u16(u16(s.dat[s.ptr - 4] << 8) | u16(s.dat[s.ptr - 3]))
const L2 = () => u16(u16(s.dat[s.ptr - 6] << 8) | u16(s.dat[s.ptr - 5]))
const H2 = () => PEEK2(s.dat.slice(s.ptr - 3))
const T2 = () => PEEK2(s.dat.slice(s.ptr - 2))
const N2 = () => PEEK2(s.dat.slice(s.ptr - 4))
const L2 = () => PEEK2(s.dat.slice(s.ptr - 6))

const HALT = (c: number): number => uxn_halt(u, ins, c, (pc - 1));

const SET = (mul: number, add: number): void => {
if (mul > s.ptr) HALT(1);
tmp = (u16(s.ptr + u16(u16(k * mul) + add)))
tmp = (u16(s.ptr + ((k * mul) + add)))
if (tmp > 254) HALT(2);
s.ptr = (tmp);
};

const PUT = (offset: number, value: number) => {
s.dat[s.ptr - 1 - offset] = (value);
s.dat[s.ptr - 1 - offset] = u16(value);
};

const PUT2 = (offset: number, value: number) => {
const tmp = value;
s.dat[s.ptr - offset - 2] = (tmp >> 8);
s.dat[s.ptr - offset - 1] = (tmp);
s.dat[s.ptr - offset - 2] = u16(tmp >> 8);
s.dat[s.ptr - offset - 1] = u8(tmp);
}

const PUSH = (x: Stack, value: number) => {
console.log('PUSH', value)
z = x;
if (z.ptr > 254) HALT(2);
z.dat[z.ptr] = u16(value);
z.ptr = u16(z.ptr + 1)
}

const PUSH2 = (x: Stack, value: number) => {
console.log('PUSH2', value)
z = x;
if(s.ptr > 253) HALT(2);

if(z.ptr > 253) HALT(2);
tmp = value;

z.dat[z.ptr] = u16(tmp >> 8);
z.dat[z.ptr + 1] = u16(tmp);
z.dat[z.ptr + 1] = u8(tmp);
z.ptr = u16(z.ptr + 2);
}

const DEO = (address: number, value: number) => {
console.log({ address, value, char: String.fromCharCode(value) })

u.dev[address] = (value);
if ((deo_mask[address >> 4] >> (address & 0xf)) & 0x1) uxn_deo(u, address);
}
Expand All @@ -125,37 +123,61 @@ export function uxn_eval(u: Uxn, pc: number): number {
k = keepFlag(ins)
s = returnFlag(ins) ? u.rst : u.wst

console.log({ pc, ins, opCodes, op: opCodes[ins], base: base(ins), baseOp: opCodes[base(ins) || 0] })
// console.log(opCodes[ins])
// console.log("( ", s.dat.slice(0, s.ptr).map(i => i.toString(16).padStart(2, "0")).join(' '), " )")

switch (ins) {
case opCodes.BRK: return 1
case opCodes.JCI:
if (s.dat[s.ptr] > 0) {
pc = u16(PEEK2(u.ram.slice(pc)) + pc)
}
pc = u16(2 + pc)

pc = u16(pc + ((s.dat[s.ptr - 1] > 0 ? 1 : 0) * PEEK2(u.ram.slice(pc))) + 2 )
s.ptr--

break;
case opCodes.JMI: pc = u16(pc + u16(PEEK2(u.ram.slice(pc)) + 2)); break;
case opCodes.JSI: PUSH2(u.rst, u16(pc + 2)); pc = u16(pc + u16(PEEK2(u.ram.slice(pc)) + 2)); break;
case opCodes.JSI:
PUSH2(u.rst, u16(pc + 2));
pc = u16(pc + u16(PEEK2(u.ram.slice(pc)) + 2));

break;
}

const short = shortFlag(ins)

if (short) {
switch (base(ins)) {
case opCodes.LIT: PUSH(s, u.ram[pc]); console.log('ram', u.ram[pc]); pc = u16(pc + 1); break;
case opCodes.INC: t = T(); SET(1, 0); PUT(0, u16(t + 1)); break;
case opCodes.LIT:
PUSH(s, u.ram[pc]);
pc = u16(pc + 1);
break;
case opCodes.INC:
t = T(); SET(1, 0);
PUT(0, u16(t + 1));
break;
case opCodes.POP: SET(1, -1); break;
case opCodes.NIP: t = T(); SET(2, -1); PUT(0, t); break;
case opCodes.SWP: t = T(); n = N(); SET(2, 0); PUT(0, n); PUT(1, t); break;
case opCodes.SWP:
t = T(); n = N(); SET(2, 0);
PUT(0, n);
PUT(1, t); break;
case opCodes.ROT: t = T(); n = N(); l = L(); SET(3, 0); PUT(0, l); PUT(1, t); PUT(2, n); break;
case opCodes.DUP: t = T(); SET(1, 1); PUT(0, t); PUT(1, t); break;
case opCodes.DUP:
t = T(); SET(1, 1);
PUT(0, t);
PUT(1, t);
break;
case opCodes.OVR: t = T(); n = N(); SET(2, 1); PUT(0, n); PUT(1, t); PUT(2, n); break;
case opCodes.EQU: t = T(); n = N(); SET(2, -1); PUT(0, n == t ? 1 : 0); break;
case opCodes.NEQ: t = T(); n = N(); SET(2, -1); PUT(0, n != t ? 0 : 1); break;
case opCodes.GTH: t = T(); n = N(); SET(2, -1); PUT(0, n > t ? 1 : 0); break;
case opCodes.GTH:
t = T(); n = N(); SET(2, -1);
PUT(0, n > t ? 1 : 0);
break;
case opCodes.LTH: t = T(); n = N(); SET(2, -1); PUT(0, n < t ? 1 : 0); break;
case opCodes.JMP: t = T(); SET(1, -1); pc = u16(pc + t); break;
case opCodes.JMP:
t = T(); SET(1, -1);
pc = u16(pc + t);
break;
case opCodes.JCN: t = T(); n = N(); SET(2, -2); pc = u16(pc + u16(n * t)); break;
case opCodes.JSR: t = T(); SET(1, -1); PUSH2(u.rst, pc); pc = u16(pc + t); break;
case opCodes.STH: t = T(); SET(1, -1); PUSH((ins & 0x40 ? u.wst : u.rst), t); break;
Expand All @@ -167,33 +189,66 @@ export function uxn_eval(u: Uxn, pc: number): number {
case opCodes.STA: t = T2(); n = L(); SET(3,-3); u.ram[t] = (n); break;
case opCodes.DEI: t = T(); SET(1, 0); DEI(0, t); break;
case opCodes.DEO: t = T(); n = N(); SET(2,-2); DEO(t, n); break;
case opCodes.ADD: t = T(); n = N(); SET(2,-1); PUT(0, u16(n + t)); break;
case opCodes.ADD:
t = T(); n = N();
SET(2, -1);
PUT(0, u16(n + t));
break;
case opCodes.SUB: t = T(); n = N(); SET(2,-1); PUT(0, u16(n - t)); break;
case opCodes.MUL: t = T(); n = N(); SET(2,-1); PUT(0, u16(n * t)); break;
case opCodes.MUL:
t = T(); n = N();
const v = s.dat.slice(0, s.ptr)

SET(2, -1);
PUT(0, u16(n * t));
break;
case opCodes.DIV: t = T(); n = N(); SET(2,-1); if(!t) HALT(3); PUT(0, u16(n / t)); break;
case opCodes.AND: t = T(); n = N(); SET(2,-1); PUT(0, u16(n & t)); break;
case opCodes.AND:
t = T(); n = N();
SET(2,-1);
PUT(0, u16(n & t));
break;
case opCodes.ORA: t = T(); n = N(); SET(2,-1); PUT(0, u16(n | t)); break;
case opCodes.EOR: t = T(); n = N(); SET(2,-1); PUT(0, u16(n ^ t)); break;
case opCodes.SFT: t = T(); n = N(); SET(2,-1); PUT2(0, u16(n >> u16(t & 0xf) << u16(t >> 4))); break;
case opCodes.SFT:
t = T(); n = N();
SET(2, -1);
PUT(0, u16(n >> u16(t & 0xf) << u16(t >> 4)));

break;
}
} else {
switch (base(ins)) {
case opCodes.LIT:
PUSH(s, u.ram[pc]); pc = u16(pc + 1);
PUSH(s, u.ram[pc]); pc = u16(pc + 1);
break;
case opCodes.INC: t = T2(); SET(2, 0); PUT2(0, t + 1); break;
case opCodes.INC: t = T2(); SET(2, 0);
PUT2(0, u16(t + 1));
break;
case opCodes.POP: SET(2, -2); break;
case opCodes.NIP: t = T2(); SET(4, -2); PUT2(0, t); break;
case opCodes.SWP: t = T2(); n = N2(); SET(4, 0); PUT2(0, n); PUT2(2, t); break;
case opCodes.ROT: t = T2(); n = N2(); l = L2(); SET(6, 0); PUT2(0, l); PUT2(2, t); PUT2(4, n); break;
case opCodes.DUP: t = T2(); SET(2, 2); PUT2(0, t); PUT2(2, t); break;
case opCodes.DUP:
t = T2(); SET(2, 2);
PUT2(0, t);
PUT2(2, t);
break;
case opCodes.OVR: t = T2(); n = N2(); SET(4, 2); PUT2(0, n); PUT2(2, t); PUT2(4, n); break;
case opCodes.EQU: t = T2(); n = N2(); SET(4, -3); PUT(0, n == t ? 1 : 0); break;
case opCodes.NEQ: t = T2(); n = N2(); SET(4, -3); PUT(0, n != t ? 1 : 0); break;
case opCodes.GTH: t = T2(); n = N2(); SET(4, -3); PUT(0, n > t ? 1 : 0); break;
case opCodes.LTH: t = T2(); n = N2(); SET(4, -3); PUT(0, n < t ? 1 : 0); break;
case opCodes.JMP: t = T2(); SET(2, -2); pc = t; break;
case opCodes.LTH:
t = T2(); n = N2();
SET(4, -3);
PUT(0, n < t ? 1 : 0);
break;
case opCodes.JMP:
t = T2();
SET(2, -2);
pc = t;
break;
case opCodes.JCN: t = T2(); n = L(); SET(3, -3); if(n) pc = t; break;
case opCodes.JSR: t = T2(); SET(2, -2); PUSH2(u.rst, pc); pc = t; break;
case opCodes.STH: t = T2(); SET(2, -2); PUSH2((ins & 0x40 ? u.wst : u.rst), t); break;
Expand All @@ -205,7 +260,11 @@ export function uxn_eval(u: Uxn, pc: number): number {
case opCodes.STA: t = T2(); n = N2(); SET(4,-4); POKE2(u.ram, (t), n); break;
case opCodes.DEI: t = T(); SET(1, 1); DEI(1, t); DEI(0, t + 1); break;
case opCodes.DEO: t = T(); n = N(); l = L(); SET(3,-3); DEO(t, l); DEO(t + 1, n); break;
case opCodes.ADD: t = T2(); n = N2(); SET(4,-2); PUT(0, u16(n + t)); break;
case opCodes.ADD:
t = T2(); n = N2();
SET(4,-2);
PUT2(0, u16(n + t));
break;
case opCodes.SUB: t = T2(); n = N2(); SET(4,-2); PUT2(0, u16(n - t)); break;
case opCodes.MUL: t = T2(); n = N2(); SET(4,-2); PUT2(0, u16(n * t)); break;
case opCodes.DIV: t = T2(); n = N2(); SET(4,-2); if(!t) HALT(3); PUT2(0, u16(n / t)); break;
Expand Down
4 changes: 2 additions & 2 deletions uxnemu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function emu_error(msg: string, err: string): number {
}

function console_deo(d: number[], port: number): void {
console.log('!!', d[port], d, port, '>>', String.fromCharCode(d[port]), '<<')
// console.log('!!', d[port], d, port, '>>', String.fromCharCode(d[port]), '<<')

switch (port) {
case 0x8:
Expand Down Expand Up @@ -50,7 +50,7 @@ export function uxn_dei(u: Uxn, addr: number): number {
export function uxn_deo(u: Uxn, addr: number): void {
const p = u16(addr & 0x0f), d = u16(addr & 0xf0);

console.log({ p, d })
// console.log({ p, d })

switch(d) {
case 0x00:
Expand Down

0 comments on commit 314a50e

Please sign in to comment.