Clean up code

This commit is contained in:
JJ Bliss
2026-05-07 16:41:08 -04:00
parent 1831a1fee5
commit bcdd877bf7
+69 -72
View File
@@ -50,11 +50,9 @@ export type filestate = enum {
}; };
def NUMBANKS = 0x10; def NUMBANKS: u16 = 0x10;
def BANKSIZE = 0x10000; def BANKSIZE: u32 = 0x10000;
def BANKS_CAP = NUMBANKS * BANKSIZE; def BANKS_CAP = NUMBANKS * BANKSIZE;
const banksize: u32 = BANKSIZE;
const numbanks: u16 = NUMBANKS;
const reset_vector: u16 = 0x0100; const reset_vector: u16 = 0x0100;
@@ -246,8 +244,8 @@ fn deo_expansion(addr: u16, state: *uxn) void = {
const dstaddr = short_from_bytes(state.ram[addr+5],state.ram[addr+6]): u32; const dstaddr = short_from_bytes(state.ram[addr+5],state.ram[addr+6]): u32;
const value = state.ram[addr+7]; const value = state.ram[addr+7];
// fmt::printfln("expansion fill: bank: {:x} addr: {:x} value: {:x} | length: {:x}",bank,dstaddr,value,length)!; // fmt::printfln("expansion fill: bank: {:x} addr: {:x} value: {:x} | length: {:x}",bank,dstaddr,value,length)!;
if(bank < numbanks) for(let i: u16 =0; i < length; i+=1){ if(bank < NUMBANKS) for(let i: u16 =0; i < length; i+=1){
state.ram[bank * banksize + dstaddr + i] = value; state.ram[bank * BANKSIZE + dstaddr + i] = value;
}; };
case 0x01 => //cpyl case 0x01 => //cpyl
const srcbank = short_from_bytes(state.ram[addr+3],state.ram[addr+4]): u32; const srcbank = short_from_bytes(state.ram[addr+3],state.ram[addr+4]): u32;
@@ -255,9 +253,9 @@ fn deo_expansion(addr: u16, state: *uxn) void = {
const dstbank = short_from_bytes(state.ram[addr+7],state.ram[addr+8]): u32; const dstbank = short_from_bytes(state.ram[addr+7],state.ram[addr+8]): u32;
const dstaddr = short_from_bytes(state.ram[addr+9],state.ram[addr+10]): u32; const dstaddr = short_from_bytes(state.ram[addr+9],state.ram[addr+10]): u32;
// fmt::printfln("Cpyl: src: {:x} <-> {:x} dst: {:x} <-> {:x} | length: {:x}",srcbank,srcaddr,dstbank,dstaddr,length)!; // fmt::printfln("Cpyl: src: {:x} <-> {:x} dst: {:x} <-> {:x} | length: {:x}",srcbank,srcaddr,dstbank,dstaddr,length)!;
if(srcbank < numbanks && dstbank < numbanks) for(let i: u16 =0; i < length; i+=1){ if(srcbank < NUMBANKS && dstbank < NUMBANKS) for(let i: u16 =0; i < length; i+=1){
const readval = state.ram[srcbank * banksize + srcaddr + i]; const readval = state.ram[srcbank * BANKSIZE + srcaddr + i];
state.ram[dstbank * banksize + dstaddr + i] = readval; state.ram[dstbank * BANKSIZE + dstaddr + i] = readval;
}; };
case 0x02 => //cpr TODO are these actually different?? Either way need to not use for loop case 0x02 => //cpr TODO are these actually different?? Either way need to not use for loop
const srcbank = short_from_bytes(state.ram[addr+3],state.ram[addr+4]): u32; const srcbank = short_from_bytes(state.ram[addr+3],state.ram[addr+4]): u32;
@@ -265,9 +263,9 @@ fn deo_expansion(addr: u16, state: *uxn) void = {
const dstbank = short_from_bytes(state.ram[addr+7],state.ram[addr+8]): u32; const dstbank = short_from_bytes(state.ram[addr+7],state.ram[addr+8]): u32;
const dstaddr = short_from_bytes(state.ram[addr+9],state.ram[addr+10]): u32; const dstaddr = short_from_bytes(state.ram[addr+9],state.ram[addr+10]): u32;
// fmt::printfln("Cpyr: src: {:x} <-> {:x} dst: {:x} <-> {:x} | length: {:x}",srcbank,srcaddr,dstbank,dstaddr,length)!; // fmt::printfln("Cpyr: src: {:x} <-> {:x} dst: {:x} <-> {:x} | length: {:x}",srcbank,srcaddr,dstbank,dstaddr,length)!;
if(srcbank < numbanks && dstbank < numbanks) for(let i: u16 =1; i <= length; i+=1){ if(srcbank < NUMBANKS && dstbank < NUMBANKS) for(let i: u16 =1; i <= length; i+=1){
const readval = state.ram[srcbank * banksize + srcaddr + length - i]; const readval = state.ram[srcbank * BANKSIZE + srcaddr + length - i];
state.ram[dstbank * banksize + dstaddr + length - i] = readval; state.ram[dstbank * BANKSIZE + dstaddr + length - i] = readval;
}; };
case => case =>
fmt::fatalf("Unknown expansion op: 0x{:x}",op); fmt::fatalf("Unknown expansion op: 0x{:x}",op);
@@ -462,8 +460,8 @@ fn read_from_addr(short: bool, addr: u16, state: *uxn) (u8 | u16) = {
fn write_to_addr(value: (u8 | u16), addr: u16, state: *uxn) void = { fn write_to_addr(value: (u8 | u16), addr: u16, state: *uxn) void = {
match(value){ match(value){
case let s: u16 => case let s: u16 =>
let high = (s >> 8 ): u8; const high = (s >> 8 ): u8;
let low = s: u8; const low = s: u8;
state.ram[addr] = high; state.ram[addr] = high;
state.ram[addr+1] = low; state.ram[addr+1] = low;
@@ -485,8 +483,8 @@ fn read_from_zaddr(short: bool, zaddr: u8, state: *uxn) (u8 | u16) = {
fn write_to_zaddr(value: (u8 | u16), zaddr: u8, state: *uxn) void = { fn write_to_zaddr(value: (u8 | u16), zaddr: u8, state: *uxn) void = {
match(value){ match(value){
case let s: u16 => case let s: u16 =>
let high = (s >> 8 ): u8; const high = (s >> 8 ): u8;
let low = s: u8; const low = s: u8;
state.ram[zaddr: u16] = high; state.ram[zaddr: u16] = high;
state.ram[(zaddr+1): u16] = low; state.ram[(zaddr+1): u16] = low;
case let b: u8 => case let b: u8 =>
@@ -495,7 +493,7 @@ fn write_to_zaddr(value: (u8 | u16), zaddr: u8, state: *uxn) void = {
}; };
fn push_to_stack(ret: bool, val: (u8 | u16), state: *uxn) void = { fn push_to_stack(ret: bool, val: (u8 | u16), state: *uxn) void = {
let stack = switch(ret){ const stack = switch(ret){
case true => case true =>
yield 1; yield 1;
case false => case false =>
@@ -518,7 +516,7 @@ fn push_to_stack(ret: bool, val: (u8 | u16), state: *uxn) void = {
// Get value from stack, offset 0 is top of stack. offset depends on short or byte // Get value from stack, offset 0 is top of stack. offset depends on short or byte
fn get_stack_val(ret: bool, short: bool, off: u8, state: *uxn) (u8 | u16) = { fn get_stack_val(ret: bool, short: bool, off: u8, state: *uxn) (u8 | u16) = {
let stack = switch(ret){ const stack = switch(ret){
case true => case true =>
yield 1; yield 1;
case false => case false =>
@@ -537,7 +535,7 @@ fn get_stack_val(ret: bool, short: bool, off: u8, state: *uxn) (u8 | u16) = {
//peek offset is always byte offset //peek offset is always byte offset
fn peek(ret: bool, short: bool, off: u8, state: *uxn) (u8 | u16 ) = { fn peek(ret: bool, short: bool, off: u8, state: *uxn) (u8 | u16 ) = {
let stack = switch(ret){ const stack = switch(ret){
case true => case true =>
yield 1; yield 1;
case false => case false =>
@@ -556,7 +554,7 @@ fn peek(ret: bool, short: bool, off: u8, state: *uxn) (u8 | u16 ) = {
}; };
fn increment_stack(ret: bool, short: bool, state: *uxn) void = { fn increment_stack(ret: bool, short: bool, state: *uxn) void = {
let stack = switch(ret){ const stack = switch(ret){
case true => case true =>
yield 1; yield 1;
case false => case false =>
@@ -569,7 +567,7 @@ fn increment_stack(ret: bool, short: bool, state: *uxn) void = {
}; };
}; };
fn decrement_stack(ret: bool, short: bool, state: *uxn) void = { fn decrement_stack(ret: bool, short: bool, state: *uxn) void = {
let stack = switch(ret){ const stack = switch(ret){
case true => case true =>
yield 1; yield 1;
case false => case false =>
@@ -583,7 +581,7 @@ fn decrement_stack(ret: bool, short: bool, state: *uxn) void = {
}; };
fn pop_from_stack(keep: bool, ret: bool, short: bool, state: *uxn) (u8 | u16) = { fn pop_from_stack(keep: bool, ret: bool, short: bool, state: *uxn) (u8 | u16) = {
let val = get_stack_val(ret, short, 0, state); const val = get_stack_val(ret, short, 0, state);
if(!keep) decrement_stack(ret, short, state); if(!keep) decrement_stack(ret, short, state);
return val; return val;
}; };
@@ -618,8 +616,8 @@ export fn uxn_eval(new_pc: u16, state: *uxn) (done | quit | error ) = {
export fn uxn_step(state: *uxn) (done | error) = { export fn uxn_step(state: *uxn) (done | error) = {
// fmt::printfln("Starting eval with pc: {:x}", pc)!; // fmt::printfln("Starting eval with pc: {:x}", pc)!;
let readval = state.ram[state.pc]; const readval = state.ram[state.pc];
let inst: instruction = get_instruction(readval)?; const inst: instruction = get_instruction(readval)?;
state.pc += 1; //TODO verify all pc changes state.pc += 1; //TODO verify all pc changes
match(inst) { match(inst) {
@@ -694,7 +692,7 @@ export fn uxn_step(state: *uxn) (done | error) = {
const bot = pop_or_get(equ_inst,1,state); const bot = pop_or_get(equ_inst,1,state);
//TODO, I think this works, but it feels unclean with the casting //TODO, I think this works, but it feels unclean with the casting
let val: u8 = if(top: u16 == bot: u16){ const val: u8 = if(top: u16 == bot: u16){
yield 1; yield 1;
} else { } else {
yield 0; yield 0;
@@ -705,7 +703,7 @@ export fn uxn_step(state: *uxn) (done | error) = {
const bot = pop_or_get(neq_inst,1,state); const bot = pop_or_get(neq_inst,1,state);
//TODO, I think this works, but it feels unclean with the casting //TODO, I think this works, but it feels unclean with the casting
let val: u8 = if(top: u16 != bot: u16){ const val: u8 = if(top: u16 != bot: u16){
yield 1; yield 1;
} else { } else {
yield 0; yield 0;
@@ -716,7 +714,7 @@ export fn uxn_step(state: *uxn) (done | error) = {
const bot = pop_or_get(gth_inst,1,state); const bot = pop_or_get(gth_inst,1,state);
//TODO, I think this works, but it feels unclean with the casting //TODO, I think this works, but it feels unclean with the casting
let val: u8 = if(bot: u16 > top: u16){ const val: u8 = if(bot: u16 > top: u16){
yield 1; yield 1;
} else { } else {
yield 0; yield 0;
@@ -727,7 +725,7 @@ export fn uxn_step(state: *uxn) (done | error) = {
const bot = pop_or_get(lth_inst,1,state); const bot = pop_or_get(lth_inst,1,state);
//TODO, I think this works, but it feels unclean with the casting //TODO, I think this works, but it feels unclean with the casting
let val: u8 = if(bot: u16 < top: u16){ const val: u8 = if(bot: u16 < top: u16){
yield 1; yield 1;
} else { } else {
yield 0; yield 0;
@@ -773,34 +771,34 @@ export fn uxn_step(state: *uxn) (done | error) = {
const val = pop_from_stack(sth_inst.keep,sth_inst.ret,sth_inst.short,state); const val = pop_from_stack(sth_inst.keep,sth_inst.ret,sth_inst.short,state);
push_to_stack(!sth_inst.ret, val, state); push_to_stack(!sth_inst.ret, val, state);
case let ldz_inst: LDZ => case let ldz_inst: LDZ =>
let addr: u8 = pop_from_stack(ldz_inst.keep, ldz_inst.ret, false, state): u8; const addr: u8 = pop_from_stack(ldz_inst.keep, ldz_inst.ret, false, state): u8;
let val = read_from_zaddr(ldz_inst.short,addr,state); const val = read_from_zaddr(ldz_inst.short,addr,state);
push_to_stack(ldz_inst.ret, val, state); push_to_stack(ldz_inst.ret, val, state);
case let stz_inst: STZ => case let stz_inst: STZ =>
let addr: u8 = pop_or_peek(normal_op{short = false, keep = stz_inst.keep, ret = stz_inst.ret}, 0, state): u8; const addr: u8 = pop_or_peek(normal_op{short = false, keep = stz_inst.keep, ret = stz_inst.ret}, 0, state): u8;
let val = pop_or_peek(stz_inst,1,state); const val = pop_or_peek(stz_inst,1,state);
write_to_zaddr(val,addr,state); write_to_zaddr(val,addr,state);
case let ldr_inst: LDR => case let ldr_inst: LDR =>
let reladdr = (pop_from_stack(ldr_inst.keep, ldr_inst.ret, false, state): u16): i8; const reladdr = (pop_from_stack(ldr_inst.keep, ldr_inst.ret, false, state): u16): i8;
let addr: u16 = (state.pc: u32: i32 + reladdr): u16; const addr: u16 = (state.pc: u32: i32 + reladdr): u16;
let val = read_from_addr(ldr_inst.short,addr,state); const val = read_from_addr(ldr_inst.short,addr,state);
push_to_stack(ldr_inst.ret, val, state); push_to_stack(ldr_inst.ret, val, state);
case let str_inst: STR => case let str_inst: STR =>
let reladdr = (pop_or_peek(normal_op{short = false, keep = str_inst.keep, ret = str_inst.ret}, 0, state): u16 & 0x00FF): u8: i8; const reladdr = (pop_or_peek(normal_op{short = false, keep = str_inst.keep, ret = str_inst.ret}, 0, state): u16 & 0x00FF): u8: i8;
let addr: u16 = (state.pc: u32: i32 + reladdr): u16; const addr: u16 = (state.pc: u32: i32 + reladdr): u16;
let val = pop_or_peek(str_inst,1,state); const val = pop_or_peek(str_inst,1,state);
write_to_addr(val,addr,state); write_to_addr(val,addr,state);
case let lda_inst: LDA => case let lda_inst: LDA =>
let addr: u16 = pop_from_stack(lda_inst.keep, lda_inst.ret, true, state): u16; const addr: u16 = pop_from_stack(lda_inst.keep, lda_inst.ret, true, state): u16;
let val = read_from_addr(lda_inst.short,addr,state); const val = read_from_addr(lda_inst.short,addr,state);
push_to_stack(lda_inst.ret, val, state); push_to_stack(lda_inst.ret, val, state);
case let sta_inst: STA => case let sta_inst: STA =>
let addr: u16 = pop_or_peek(normal_op{short = true, keep = sta_inst.keep, ret = sta_inst.ret}, 0, state): u16 ; const addr: u16 = pop_or_peek(normal_op{short = true, keep = sta_inst.keep, ret = sta_inst.ret}, 0, state): u16 ;
let val = pop_or_peek(sta_inst,2,state); const val = pop_or_peek(sta_inst,2,state);
write_to_addr(val,addr,state); write_to_addr(val,addr,state);
case let dei_inst: DEI => case let dei_inst: DEI =>
let port: u8 = pop_from_stack(dei_inst.keep, dei_inst.ret, false, state): u8; const port: u8 = pop_from_stack(dei_inst.keep, dei_inst.ret, false, state): u8;
let val = if(dei_inst.short){ const val = if(dei_inst.short){
yield short_from_bytes(emu_dei(port,state),emu_dei(port+1,state)); yield short_from_bytes(emu_dei(port,state),emu_dei(port+1,state));
}else{ }else{
yield emu_dei(port,state); yield emu_dei(port,state);
@@ -819,37 +817,36 @@ export fn uxn_step(state: *uxn) (done | error) = {
emu_deo(port,b,state); emu_deo(port,b,state);
}; };
case let add_inst: ADD => case let add_inst: ADD =>
let top = pop_or_get(add_inst,0,state); const top = pop_or_get(add_inst,0,state);
let bot = pop_or_get(add_inst,1,state); const bot = pop_or_get(add_inst,1,state);
let res = if(!add_inst.short){ const res = if(!add_inst.short){
yield (top: u8 + bot: u8); yield (top: u8 + bot: u8);
} else { } else {
yield (top: u16 + bot: u16); yield (top: u16 + bot: u16);
}; };
push_to_stack(add_inst.ret, res, state); push_to_stack(add_inst.ret, res, state);
case let sub_inst: SUB => case let sub_inst: SUB =>
let top = pop_or_get(sub_inst,0,state); const top = pop_or_get(sub_inst,0,state);
let bot = pop_or_get(sub_inst,1,state); const bot = pop_or_get(sub_inst,1,state);
let res = if(!sub_inst.short){ const res = if(!sub_inst.short){
yield (bot: u8 - top: u8); yield (bot: u8 - top: u8);
} else { } else {
yield (bot: u16 - top: u16); yield (bot: u16 - top: u16);
}; };
push_to_stack(sub_inst.ret, res, state); push_to_stack(sub_inst.ret, res, state);
case let mul_inst: MUL => case let mul_inst: MUL =>
let top = pop_or_get(mul_inst,0,state); const top = pop_or_get(mul_inst,0,state);
let bot = pop_or_get(mul_inst,1,state); const bot = pop_or_get(mul_inst,1,state);
let res = if(!mul_inst.short){ const res = if(!mul_inst.short){
yield (bot: u8 * top: u8); yield (bot: u8 * top: u8);
} else { } else {
yield (bot: u16 * top: u16); yield (bot: u16 * top: u16);
}; };
push_to_stack(mul_inst.ret, res, state); push_to_stack(mul_inst.ret, res, state);
case let div_inst: DIV => case let div_inst: DIV =>
let top = pop_or_get(div_inst,0,state); const top = pop_or_get(div_inst,0,state);
let bot = pop_or_get(div_inst,1,state); const bot = pop_or_get(div_inst,1,state);
const res = if(!div_inst.short){
let res = if(!div_inst.short){
assert(bot is u8); assert(bot is u8);
assert(top is u8); assert(top is u8);
if(top: u8 == 0) yield top; if(top: u8 == 0) yield top;
@@ -859,21 +856,21 @@ export fn uxn_step(state: *uxn) (done | error) = {
assert(top is u16); assert(top is u16);
if(top: u16 == 0) yield top; if(top: u16 == 0) yield top;
yield (bot: u16 / top: u16): u16; yield (bot: u16 / top: u16): u16;
};//TODO handle rounding and edge cases with division };
push_to_stack(div_inst.ret, res, state); push_to_stack(div_inst.ret, res, state);
case let and_inst: AND => case let and_inst: AND =>
let top = pop_or_get(and_inst,0,state); const top = pop_or_get(and_inst,0,state);
let bot = pop_or_get(and_inst,1,state); const bot = pop_or_get(and_inst,1,state);
let res = if(!and_inst.short){ const res = if(!and_inst.short){
yield (bot: u8 & top: u8); yield (bot: u8 & top: u8);
} else { } else {
yield (bot: u16 & top: u16); yield (bot: u16 & top: u16);
}; };
push_to_stack(and_inst.ret, res, state); push_to_stack(and_inst.ret, res, state);
case let ora_inst: ORA => case let ora_inst: ORA =>
let top = pop_or_get(ora_inst,0,state); const top = pop_or_get(ora_inst,0,state);
let bot = pop_or_get(ora_inst,1,state); const bot = pop_or_get(ora_inst,1,state);
let res = if(!ora_inst.short){ const res = if(!ora_inst.short){
yield (bot: u8 | top: u8); yield (bot: u8 | top: u8);
} else { } else {
yield (bot: u16 | top: u16); yield (bot: u16 | top: u16);
@@ -889,11 +886,11 @@ export fn uxn_step(state: *uxn) (done | error) = {
}; };
push_to_stack(eor_inst.ret, res, state); push_to_stack(eor_inst.ret, res, state);
case let sft_inst: SFT => case let sft_inst: SFT =>
let shifts: u8 = pop_or_peek(normal_op{short = false, keep = sft_inst.keep, ret = sft_inst.ret}, 0, state): u8; const shifts: u8 = pop_or_peek(normal_op{short = false, keep = sft_inst.keep, ret = sft_inst.ret}, 0, state): u8;
let rightshifts = shifts & 0b00001111; const rightshifts = shifts & 0b00001111;
let leftshifts = ((shifts & 0b11110000) >> 4) & 0b00001111; const leftshifts = ((shifts & 0b11110000) >> 4) & 0b00001111;
let val = pop_or_peek(sft_inst,1,state); const val = pop_or_peek(sft_inst,1,state);
let res = match(val) { const res = match(val) {
case let s: u16 => case let s: u16 =>
yield (s >> rightshifts ) << leftshifts; yield (s >> rightshifts ) << leftshifts;
case let b: u8 => case let b: u8 =>
@@ -919,15 +916,15 @@ export fn strerror(err: error) str = {
}; };
export fn uxn_init(path: str) ( *uxn | error ) = { export fn uxn_init(path: str) ( *uxn | error ) = {
let romfile = match (os::open(path,fs::flag::RDONLY)) { const romfile = match (os::open(path,fs::flag::RDONLY)) {
case let file: io::file => case let file: io::file =>
yield file; yield file;
case let err: fs::error => case let err: fs::error =>
fmt::fatalf("Error opening {}: {}", path, fs::strerror(err)); fmt::fatalf("Error opening {}: {}", path, fs::strerror(err));
}; };
let state: *uxn = initialize_uxn_mem(); const state: *uxn = initialize_uxn_mem();
//Copy rom into ram //Copy rom into ram
let bytesread = match (io::read(romfile,state.ram[0x100..(BANKS_CAP - 0x100)])){ const bytesread = match (io::read(romfile,state.ram[0x100..(BANKS_CAP - 0x100)])){
case let bytes: size => case let bytes: size =>
yield bytes; yield bytes;
case let eof: io::EOF => case let eof: io::EOF =>