From 73d2af846a961b6cbf7fb12c5330e5d94a58e9fb Mon Sep 17 00:00:00 2001 From: JJ Bliss Date: Thu, 14 May 2026 10:43:19 -0400 Subject: [PATCH] cleaned up some console code --- cmd/meadow/main.ha | 57 +++++++++------------------------ cmd/meadowcli/uxnmin.ha | 71 +++++++++++------------------------------ uxn/console.ha | 71 ++++++++++++++++++++++++++++++++++++++++- uxn/uxn.ha | 64 ++++++++++++++++--------------------- 4 files changed, 131 insertions(+), 132 deletions(-) diff --git a/cmd/meadow/main.ha b/cmd/meadow/main.ha index 888a259..feb911e 100644 --- a/cmd/meadow/main.ha +++ b/cmd/meadow/main.ha @@ -74,48 +74,21 @@ export fn main() void = { const ms_interval = perf_freq / 1000; for (run) { const now: u64 = sdl3::GetPerformanceCounter(); - if(state.running){ - // fmt::printf("Next Step!\n")!; - // uxn::uxn_step(state)!; - yield; - }else{ - if(state.console_vector != 0){ - // fmt::printf("Console!\n")!; - const pollr: uint = match(unix::poll::poll(consolefd, unix::poll::NONBLOCK)){ - case let i: uint => - yield i; - case => - yield 0; - }; - if(pollr > 0){ - // fmt::println("Input!")!; - let buf: [MAX_CONSOLE_INPUT]u8 = [0...]; - io::read(os::stdin_file, buf)!; - for( let b .. buf ){ - // 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(state.console_vector != 0){ + // fmt::printf("Console!\n")!; + const pollr: uint = match(unix::poll::poll(consolefd, unix::poll::NONBLOCK)){ + case let i: uint => + yield i; + case => + yield 0; + }; + if(pollr > 0){ + // fmt::println("Input!")!; + let buf: [MAX_CONSOLE_INPUT]u8 = [0...]; + io::read(os::stdin_file, buf)!; + uxn::console_poll(buf,state); + }; + }; if(now >= next_refresh){ diff --git a/cmd/meadowcli/uxnmin.ha b/cmd/meadowcli/uxnmin.ha index 97446a9..ed98a32 100644 --- a/cmd/meadowcli/uxnmin.ha +++ b/cmd/meadowcli/uxnmin.ha @@ -25,61 +25,26 @@ export fn main() void = { } ]; let state: *uxn::uxn = uxn::uxn_init(path)!; uxn::uxn_reset(state); - + uxn::uxn_console_argument(state); for (run) { - if(state.running){ - // fmt::printf("Next Step!\n")!; - uxn::uxn_step(state)!; - yield; - }else{ - if(state.console_vector != 0){ - // fmt::printf("Console!\n")!; - 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: {: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){ - // fmt::println("Input!")!; - let buf: [MAX_CONSOLE_INPUT]u8 = [0...]; - io::read(os::stdin_file, buf)!; - for( let b .. buf ){ - // 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); - }; - }; - - }; + if(state.console_vector != 0){ + const pollr: uint = match(unix::poll::poll(consolefd, unix::poll::NONBLOCK)){ + case let i: uint => + yield i; + case => + yield 0; + }; + if(pollr > 0){ + // fmt::println("Input!")!; + let buf: [MAX_CONSOLE_INPUT]u8 = [0...]; + io::read(os::stdin_file, buf)!; + uxn::console_poll(buf,state); + }; + + }else{ + // nothing left to do... + run = false; }; if(state.dev[0x0f] != 0) run = false; diff --git a/uxn/console.ha b/uxn/console.ha index 670478e..dfa6bf7 100644 --- a/uxn/console.ha +++ b/uxn/console.ha @@ -1,4 +1,8 @@ use fmt; +use os; +use bufio; +use strings; +use unix::poll; export fn console_input(c: u8, ctype: u8, state: *uxn) (done | error) = { state.dev[0x12] = c; @@ -19,9 +23,74 @@ export fn console_input(c: u8, ctype: u8, state: *uxn) (done | error) = { state.dev[0x17] = 0; return done; }; - }; //TODO implement eval + }; state.dev[0x12] = 0; state.dev[0x17] = 0; 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 + }; +}; diff --git a/uxn/uxn.ha b/uxn/uxn.ha index 92c80c9..d106e46 100644 --- a/uxn/uxn.ha +++ b/uxn/uxn.ha @@ -6,7 +6,6 @@ use bufio; use bytes; use time; use time::date; -use strings; export type uxn = struct { pc: u16, @@ -14,6 +13,7 @@ export type uxn = struct { screen_vector: u16, mouse_vector: u16, controller_vector: u16, + audio_vectors: [4] u16, ram: [BANKS_CAP] u8, dev: [0x100] u8, ptr: [2] u8, @@ -69,6 +69,7 @@ fn initialize_uxn_mem() *uxn = { screen_vector = 0, mouse_vector = 0, controller_vector = 0, + audio_vectors = [0...], ram = [0...], dev = [0...], ptr = [0...], @@ -178,7 +179,7 @@ export fn emu_deo(port: u8, value: u8, state: *uxn) void = { const low = value; state.console_vector = short_from_bytes(high,low); case 0x18 => - fmt::print(value: rune)!; + console_output(value: rune,state); case 0x19 => fmt::error(value)!; case 0x21 => @@ -189,6 +190,31 @@ export fn emu_deo(port: u8, value: u8, state: *uxn) void = { draw_pixel(value,state); case 0x2f => 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 => const high = state.dev[0x80]; const low = value; @@ -963,37 +989,3 @@ export fn uxn_reset(state: *uxn) void = { 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 - }; -};