cleaned up some console code

This commit is contained in:
JJ Bliss
2026-05-14 10:43:19 -04:00
parent 1c13f607ee
commit 73d2af846a
4 changed files with 131 additions and 132 deletions
+1 -28
View File
@@ -74,11 +74,6 @@ export fn main() void = {
const ms_interval = perf_freq / 1000; const ms_interval = perf_freq / 1000;
for (run) { for (run) {
const now: u64 = sdl3::GetPerformanceCounter(); const now: u64 = sdl3::GetPerformanceCounter();
if(state.running){
// fmt::printf("Next Step!\n")!;
// uxn::uxn_step(state)!;
yield;
}else{
if(state.console_vector != 0){ if(state.console_vector != 0){
// fmt::printf("Console!\n")!; // fmt::printf("Console!\n")!;
const pollr: uint = match(unix::poll::poll(consolefd, unix::poll::NONBLOCK)){ const pollr: uint = match(unix::poll::poll(consolefd, unix::poll::NONBLOCK)){
@@ -91,31 +86,9 @@ export fn main() void = {
// fmt::println("Input!")!; // fmt::println("Input!")!;
let buf: [MAX_CONSOLE_INPUT]u8 = [0...]; let buf: [MAX_CONSOLE_INPUT]u8 = [0...];
io::read(os::stdin_file, buf)!; io::read(os::stdin_file, buf)!;
for( let b .. buf ){ uxn::console_poll(buf,state);
// fmt::printfln("Input: {}",b: rune)!;
if(b == 0){
// fmt::printfln("Ending stdin argument")!;
match (uxn::console_input('\n',4,state)) {
case done =>
yield done;
case let val: u8 =>
fmt::fatalf("Unhandled Opcode: {:x}", val);
};
// fmt::printfln("Finished stdin argument")!;
break;
};
match (uxn::console_input(b,1,state)) {
case done =>
yield done;
case let val: u8 =>
fmt::fatalf("Unhandled Opcode: {:x}", val);
};
}; };
// fmt::printfln("Finished stdin argument")!;
};
};
}; };
if(now >= next_refresh){ if(now >= next_refresh){
+10 -45
View File
@@ -25,61 +25,26 @@ export fn main() void = {
} ]; } ];
let state: *uxn::uxn = uxn::uxn_init(path)!; let state: *uxn::uxn = uxn::uxn_init(path)!;
uxn::uxn_reset(state); uxn::uxn_reset(state);
uxn::uxn_console_argument(state);
for (run) { for (run) {
if(state.running){
// fmt::printf("Next Step!\n")!;
uxn::uxn_step(state)!;
yield;
}else{
if(state.console_vector != 0){ if(state.console_vector != 0){
// fmt::printf("Console!\n")!; const pollr: uint = match(unix::poll::poll(consolefd, unix::poll::NONBLOCK)){
let args = os::args[2..]; case let i: uint =>
let i: u8 = 0; yield i;
let argcount = len(args): u8; case =>
for (let arg .. args){ yield 0;
// fmt::println("Console input args")!;
for(let char: u8 .. strings::toutf8(arg)){
// fmt::printfln("Console input arg char: {:x}", char)!;
match (uxn::console_input(char,2,state)) {
case done =>
yield done;
case let val: u8 =>
fmt::fatalf("Unhandled Opcode: {:x}", val);
}; };
};
let ctype: u8 = if(i == (argcount - 1)){
yield 4;
} else {
yield 3;
};
match (uxn::console_input('\n',ctype,state)) {
case done =>
yield done;
case let val: u8 =>
fmt::fatalf("Unhandled Opcode: {:x}", val);
};
i+=1; //TODO using i here seems inelegant
};
const pollr = unix::poll::poll(consolefd, unix::poll::NONBLOCK);
if(pollr > 0){ if(pollr > 0){
// fmt::println("Input!")!; // fmt::println("Input!")!;
let buf: [MAX_CONSOLE_INPUT]u8 = [0...]; let buf: [MAX_CONSOLE_INPUT]u8 = [0...];
io::read(os::stdin_file, buf)!; io::read(os::stdin_file, buf)!;
for( let b .. buf ){ uxn::console_poll(buf,state);
// fmt::printfln("Input: {}",buf[0])!;
match (uxn::console_input(b,1,state)) {
case done =>
yield done;
case let val: u8 =>
fmt::fatalf("Unhandled Opcode: {:x}", val);
};
// uxn::console_input(b,1,state);
};
}; };
}; }else{
// nothing left to do...
run = false;
}; };
if(state.dev[0x0f] != 0) run = false; if(state.dev[0x0f] != 0) run = false;
+70 -1
View File
@@ -1,4 +1,8 @@
use fmt; use fmt;
use os;
use bufio;
use strings;
use unix::poll;
export fn console_input(c: u8, ctype: u8, state: *uxn) (done | error) = { export fn console_input(c: u8, ctype: u8, state: *uxn) (done | error) = {
state.dev[0x12] = c; state.dev[0x12] = c;
@@ -19,9 +23,74 @@ export fn console_input(c: u8, ctype: u8, state: *uxn) (done | error) = {
state.dev[0x17] = 0; state.dev[0x17] = 0;
return done; return done;
}; };
}; //TODO implement eval };
state.dev[0x12] = 0; state.dev[0x12] = 0;
state.dev[0x17] = 0; state.dev[0x17] = 0;
return done; return done;
}; };
fn console_output(value: rune, state: *uxn) void = {
fmt::print(value)!;
bufio::flush(os::stdout)!;
};
export fn console_poll(buf: []u8, state: *uxn) void = {
for( let b .. buf ){
// fmt::printfln("Input: {}",b: rune)!;
if(b == 0){
// fmt::printfln("Ending stdin argument")!;
match (console_input('\n',4,state)) {
case done =>
yield done;
case let val: u8 =>
fmt::fatalf("Unhandled Opcode: {:x}", val);
};
// fmt::printfln("Finished stdin argument")!;
break;
};
match (console_input(b,1,state)) {
case done =>
yield done;
case let val: u8 =>
fmt::fatalf("Unhandled Opcode: {:x}", val);
};
};
// fmt::printfln("Finished stdin argument")!;
};
export fn uxn_console_argument(state: *uxn) void = {
// fmt::printf("Evaluating console arguments")!;
let args = os::args[2..];
let i: u8 = 0;
let argcount = len(args): u8;
for (let arg .. args){
// fmt::println("Console input args")!;
for(let char: u8 .. strings::toutf8(arg)){
// fmt::printfln("Console input arg char: {}", char: rune)!;
match (console_input(char,2,state)) {
case done =>
yield done;
case let val: u8 =>
fmt::fatalf("Unhandled Opcode: {:x}", val);
case =>
yield done;
};
};
let ctype: u8 = if(i == (argcount - 1)){
yield 4;
} else {
yield 3;
};
match (console_input('\n',ctype,state)) {
case done =>
yield done;
case let val: u8 =>
fmt::fatalf("Unhandled Opcode: {:x}", val);
case =>
yield done;
};
i+=1; //TODO using i here seems inelegant
};
};
+28 -36
View File
@@ -6,7 +6,6 @@ use bufio;
use bytes; use bytes;
use time; use time;
use time::date; use time::date;
use strings;
export type uxn = struct { export type uxn = struct {
pc: u16, pc: u16,
@@ -14,6 +13,7 @@ export type uxn = struct {
screen_vector: u16, screen_vector: u16,
mouse_vector: u16, mouse_vector: u16,
controller_vector: u16, controller_vector: u16,
audio_vectors: [4] u16,
ram: [BANKS_CAP] u8, ram: [BANKS_CAP] u8,
dev: [0x100] u8, dev: [0x100] u8,
ptr: [2] u8, ptr: [2] u8,
@@ -69,6 +69,7 @@ fn initialize_uxn_mem() *uxn = {
screen_vector = 0, screen_vector = 0,
mouse_vector = 0, mouse_vector = 0,
controller_vector = 0, controller_vector = 0,
audio_vectors = [0...],
ram = [0...], ram = [0...],
dev = [0...], dev = [0...],
ptr = [0...], ptr = [0...],
@@ -178,7 +179,7 @@ export fn emu_deo(port: u8, value: u8, state: *uxn) void = {
const low = value; const low = value;
state.console_vector = short_from_bytes(high,low); state.console_vector = short_from_bytes(high,low);
case 0x18 => case 0x18 =>
fmt::print(value: rune)!; console_output(value: rune,state);
case 0x19 => case 0x19 =>
fmt::error(value)!; fmt::error(value)!;
case 0x21 => case 0x21 =>
@@ -189,6 +190,31 @@ export fn emu_deo(port: u8, value: u8, state: *uxn) void = {
draw_pixel(value,state); draw_pixel(value,state);
case 0x2f => case 0x2f =>
draw_sprite(value,state); draw_sprite(value,state);
case 0x31 =>
const high = state.dev[0x30];
const low = value;
state.audio_vectors[0] = short_from_bytes(high,low);
case 0x3f =>
audio_pitch(0,state);
case 0x41 =>
const high = state.dev[0x40];
const low = value;
state.audio_vectors[1] = short_from_bytes(high,low);
case 0x4f =>
audio_pitch(1,state);
case 0x51 =>
const high = state.dev[0x50];
const low = value;
state.audio_vectors[2] = short_from_bytes(high,low);
case 0x5f =>
audio_pitch(2,state);
case 0x61 =>
const high = state.dev[0x60];
const low = value;
state.audio_vectors[3] = short_from_bytes(high,low);
case 0x6f =>
audio_pitch(3,state);
case 0x81 => case 0x81 =>
const high = state.dev[0x80]; const high = state.dev[0x80];
const low = value; const low = value;
@@ -963,37 +989,3 @@ export fn uxn_reset(state: *uxn) void = {
state.dev[0x17] = 0; state.dev[0x17] = 0;
}; };
export fn uxn_console_argument(state: *uxn) void = {
// fmt::printf("Evaluating console arguments")!;
let args = os::args[2..];
let i: u8 = 0;
let argcount = len(args): u8;
for (let arg .. args){
// fmt::println("Console input args")!;
for(let char: u8 .. strings::toutf8(arg)){
// fmt::printfln("Console input arg char: {}", char: rune)!;
match (console_input(char,2,state)) {
case done =>
yield done;
case let val: u8 =>
fmt::fatalf("Unhandled Opcode: {:x}", val);
case =>
yield done;
};
};
let ctype: u8 = if(i == (argcount - 1)){
yield 4;
} else {
yield 3;
};
match (console_input('\n',ctype,state)) {
case done =>
yield done;
case let val: u8 =>
fmt::fatalf("Unhandled Opcode: {:x}", val);
case =>
yield done;
};
i+=1; //TODO using i here seems inelegant
};
};