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 BANKSIZE = 0x10000;
def NUMBANKS: u16 = 0x10;
def BANKSIZE: u32 = 0x10000;
def BANKS_CAP = NUMBANKS * BANKSIZE;
const banksize: u32 = BANKSIZE;
const numbanks: u16 = NUMBANKS;
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 value = state.ram[addr+7];
// 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){
state.ram[bank * banksize + dstaddr + i] = value;
if(bank < NUMBANKS) for(let i: u16 =0; i < length; i+=1){
state.ram[bank * BANKSIZE + dstaddr + i] = value;
};
case 0x01 => //cpyl
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 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)!;
if(srcbank < numbanks && dstbank < numbanks) for(let i: u16 =0; i < length; i+=1){
const readval = state.ram[srcbank * banksize + srcaddr + i];
state.ram[dstbank * banksize + dstaddr + i] = readval;
if(srcbank < NUMBANKS && dstbank < NUMBANKS) for(let i: u16 =0; i < length; i+=1){
const readval = state.ram[srcbank * BANKSIZE + srcaddr + i];
state.ram[dstbank * BANKSIZE + dstaddr + i] = readval;
};
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;
@@ -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 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)!;
if(srcbank < numbanks && dstbank < numbanks) for(let i: u16 =1; i <= length; i+=1){
const readval = state.ram[srcbank * banksize + srcaddr + length - i];
state.ram[dstbank * banksize + dstaddr + length - i] = readval;
if(srcbank < NUMBANKS && dstbank < NUMBANKS) for(let i: u16 =1; i <= length; i+=1){
const readval = state.ram[srcbank * BANKSIZE + srcaddr + length - i];
state.ram[dstbank * BANKSIZE + dstaddr + length - i] = readval;
};
case =>
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 = {
match(value){
case let s: u16 =>
let high = (s >> 8 ): u8;
let low = s: u8;
const high = (s >> 8 ): u8;
const low = s: u8;
state.ram[addr] = high;
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 = {
match(value){
case let s: u16 =>
let high = (s >> 8 ): u8;
let low = s: u8;
const high = (s >> 8 ): u8;
const low = s: u8;
state.ram[zaddr: u16] = high;
state.ram[(zaddr+1): u16] = low;
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 = {
let stack = switch(ret){
const stack = switch(ret){
case true =>
yield 1;
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
fn get_stack_val(ret: bool, short: bool, off: u8, state: *uxn) (u8 | u16) = {
let stack = switch(ret){
const stack = switch(ret){
case true =>
yield 1;
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
fn peek(ret: bool, short: bool, off: u8, state: *uxn) (u8 | u16 ) = {
let stack = switch(ret){
const stack = switch(ret){
case true =>
yield 1;
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 = {
let stack = switch(ret){
const stack = switch(ret){
case true =>
yield 1;
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 = {
let stack = switch(ret){
const stack = switch(ret){
case true =>
yield 1;
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) = {
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);
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) = {
// fmt::printfln("Starting eval with pc: {:x}", pc)!;
let readval = state.ram[state.pc];
let inst: instruction = get_instruction(readval)?;
const readval = state.ram[state.pc];
const inst: instruction = get_instruction(readval)?;
state.pc += 1; //TODO verify all pc changes
match(inst) {
@@ -694,7 +692,7 @@ export fn uxn_step(state: *uxn) (done | error) = {
const bot = pop_or_get(equ_inst,1,state);
//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;
} else {
yield 0;
@@ -705,7 +703,7 @@ export fn uxn_step(state: *uxn) (done | error) = {
const bot = pop_or_get(neq_inst,1,state);
//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;
} else {
yield 0;
@@ -716,7 +714,7 @@ export fn uxn_step(state: *uxn) (done | error) = {
const bot = pop_or_get(gth_inst,1,state);
//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;
} else {
yield 0;
@@ -727,7 +725,7 @@ export fn uxn_step(state: *uxn) (done | error) = {
const bot = pop_or_get(lth_inst,1,state);
//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;
} else {
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);
push_to_stack(!sth_inst.ret, val, state);
case let ldz_inst: LDZ =>
let 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 addr: u8 = pop_from_stack(ldz_inst.keep, ldz_inst.ret, false, state): u8;
const val = read_from_zaddr(ldz_inst.short,addr,state);
push_to_stack(ldz_inst.ret, val, state);
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;
let val = pop_or_peek(stz_inst,1,state);
const addr: u8 = pop_or_peek(normal_op{short = false, keep = stz_inst.keep, ret = stz_inst.ret}, 0, state): u8;
const val = pop_or_peek(stz_inst,1,state);
write_to_zaddr(val,addr,state);
case let ldr_inst: LDR =>
let reladdr = (pop_from_stack(ldr_inst.keep, ldr_inst.ret, false, state): u16): i8;
let addr: u16 = (state.pc: u32: i32 + reladdr): u16;
let val = read_from_addr(ldr_inst.short,addr,state);
const reladdr = (pop_from_stack(ldr_inst.keep, ldr_inst.ret, false, state): u16): i8;
const addr: u16 = (state.pc: u32: i32 + reladdr): u16;
const val = read_from_addr(ldr_inst.short,addr,state);
push_to_stack(ldr_inst.ret, val, state);
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;
let addr: u16 = (state.pc: u32: i32 + reladdr): u16;
let val = pop_or_peek(str_inst,1,state);
const reladdr = (pop_or_peek(normal_op{short = false, keep = str_inst.keep, ret = str_inst.ret}, 0, state): u16 & 0x00FF): u8: i8;
const addr: u16 = (state.pc: u32: i32 + reladdr): u16;
const val = pop_or_peek(str_inst,1,state);
write_to_addr(val,addr,state);
case let lda_inst: LDA =>
let 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 addr: u16 = pop_from_stack(lda_inst.keep, lda_inst.ret, true, state): u16;
const val = read_from_addr(lda_inst.short,addr,state);
push_to_stack(lda_inst.ret, val, state);
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 ;
let val = pop_or_peek(sta_inst,2,state);
const addr: u16 = pop_or_peek(normal_op{short = true, keep = sta_inst.keep, ret = sta_inst.ret}, 0, state): u16 ;
const val = pop_or_peek(sta_inst,2,state);
write_to_addr(val,addr,state);
case let dei_inst: DEI =>
let port: u8 = pop_from_stack(dei_inst.keep, dei_inst.ret, false, state): u8;
let val = if(dei_inst.short){
const port: u8 = pop_from_stack(dei_inst.keep, dei_inst.ret, false, state): u8;
const val = if(dei_inst.short){
yield short_from_bytes(emu_dei(port,state),emu_dei(port+1,state));
}else{
yield emu_dei(port,state);
@@ -819,37 +817,36 @@ export fn uxn_step(state: *uxn) (done | error) = {
emu_deo(port,b,state);
};
case let add_inst: ADD =>
let top = pop_or_get(add_inst,0,state);
let bot = pop_or_get(add_inst,1,state);
let res = if(!add_inst.short){
const top = pop_or_get(add_inst,0,state);
const bot = pop_or_get(add_inst,1,state);
const res = if(!add_inst.short){
yield (top: u8 + bot: u8);
} else {
yield (top: u16 + bot: u16);
};
push_to_stack(add_inst.ret, res, state);
case let sub_inst: SUB =>
let top = pop_or_get(sub_inst,0,state);
let bot = pop_or_get(sub_inst,1,state);
let res = if(!sub_inst.short){
const top = pop_or_get(sub_inst,0,state);
const bot = pop_or_get(sub_inst,1,state);
const res = if(!sub_inst.short){
yield (bot: u8 - top: u8);
} else {
yield (bot: u16 - top: u16);
};
push_to_stack(sub_inst.ret, res, state);
case let mul_inst: MUL =>
let top = pop_or_get(mul_inst,0,state);
let bot = pop_or_get(mul_inst,1,state);
let res = if(!mul_inst.short){
const top = pop_or_get(mul_inst,0,state);
const bot = pop_or_get(mul_inst,1,state);
const res = if(!mul_inst.short){
yield (bot: u8 * top: u8);
} else {
yield (bot: u16 * top: u16);
};
push_to_stack(mul_inst.ret, res, state);
case let div_inst: DIV =>
let top = pop_or_get(div_inst,0,state);
let bot = pop_or_get(div_inst,1,state);
let res = if(!div_inst.short){
const top = pop_or_get(div_inst,0,state);
const bot = pop_or_get(div_inst,1,state);
const res = if(!div_inst.short){
assert(bot is u8);
assert(top is u8);
if(top: u8 == 0) yield top;
@@ -859,21 +856,21 @@ export fn uxn_step(state: *uxn) (done | error) = {
assert(top is u16);
if(top: u16 == 0) yield top;
yield (bot: u16 / top: u16): u16;
};//TODO handle rounding and edge cases with division
};
push_to_stack(div_inst.ret, res, state);
case let and_inst: AND =>
let top = pop_or_get(and_inst,0,state);
let bot = pop_or_get(and_inst,1,state);
let res = if(!and_inst.short){
const top = pop_or_get(and_inst,0,state);
const bot = pop_or_get(and_inst,1,state);
const res = if(!and_inst.short){
yield (bot: u8 & top: u8);
} else {
yield (bot: u16 & top: u16);
};
push_to_stack(and_inst.ret, res, state);
case let ora_inst: ORA =>
let top = pop_or_get(ora_inst,0,state);
let bot = pop_or_get(ora_inst,1,state);
let res = if(!ora_inst.short){
const top = pop_or_get(ora_inst,0,state);
const bot = pop_or_get(ora_inst,1,state);
const res = if(!ora_inst.short){
yield (bot: u8 | top: u8);
} else {
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);
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;
let rightshifts = shifts & 0b00001111;
let leftshifts = ((shifts & 0b11110000) >> 4) & 0b00001111;
let val = pop_or_peek(sft_inst,1,state);
let res = match(val) {
const shifts: u8 = pop_or_peek(normal_op{short = false, keep = sft_inst.keep, ret = sft_inst.ret}, 0, state): u8;
const rightshifts = shifts & 0b00001111;
const leftshifts = ((shifts & 0b11110000) >> 4) & 0b00001111;
const val = pop_or_peek(sft_inst,1,state);
const res = match(val) {
case let s: u16 =>
yield (s >> rightshifts ) << leftshifts;
case let b: u8 =>
@@ -919,15 +916,15 @@ export fn strerror(err: error) str = {
};
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 =>
yield file;
case let err: fs::error =>
fmt::fatalf("Error opening {}: {}", path, fs::strerror(err));
};
let state: *uxn = initialize_uxn_mem();
const state: *uxn = initialize_uxn_mem();
//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 =>
yield bytes;
case let eof: io::EOF =>