From ea31f54a796680df704067d4da7cb38015cc2d7d Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 28 Jan 2022 04:21:40 +0800 Subject: [PATCH] console escape codes working better just couldn't let it go. figured out the issue is a race condition in how the Litex UARTs are constructed...I don't know how Litex normally handles this kind of situation but they have this funky construction where reading the fifo doesn't advance it, you have to...clear the pending bit. I think it was meant to be a "clever" thing that saves an instruction or something like that since you always have to clear the pending bit. Anyways, the previous incarnation of this loop checked to see if the pending bit was set again and if it was it'd run back and fetch another character. This turns out to be a bad idea because the pending bit goes through a multireg and other stuff before it's readable which means it has a lot of delay on it. This leads to a race condition. This version actually checks if the fifo is empty and if it isn't, it reads the character *and* pumps the pending bit (because the FIFO doesn't auto-advance when you read it, you have to clear the pending bit). This works a lot better, but it's not at all well documented how this works and it's very hard to tell from reading the source code because it's written using really fancy migen abstractions that are also poorly documented and have a lot of cursed constructions. Anyways. At least the console doesn't die now when you type a control character, *but* you do have the problem that the escape codes get mis-interpreted sometimes. If it really really becomes important later on to fix that it would involve tracing out the sequence of keys that arrive at InjectKey and figuring out if things are coming out of order or what is triggering the bad escape codes. --- services/keyboard/src/main.rs | 4 ++-- services/log-server/src/main.rs | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/services/keyboard/src/main.rs b/services/keyboard/src/main.rs index b9b905766..4dedcedd5 100644 --- a/services/keyboard/src/main.rs +++ b/services/keyboard/src/main.rs @@ -906,7 +906,7 @@ fn xmain() -> ! { }), Some(Opcode::InjectKey) => msg_scalar_unpack!(msg, k, _, _, _, { // key substitutions to help things work better - // 5b31 = home + // 5b31 = home (5b7e now that we have things working better?? maybe this is a three-byte sequence and I'm missing a third?) // 5b44 = left // 5b43 = right // 5b41 = up @@ -923,7 +923,7 @@ fn xmain() -> ! { } else { inject_esc = false; match k { - 0x31 => '∴' as u32, + 0x7e => '∴' as u32, 0x44 => '←' as u32, 0x43 => '→' as u32, 0x41 => '↑' as u32, diff --git a/services/log-server/src/main.rs b/services/log-server/src/main.rs index 4a5921b5f..918df479a 100644 --- a/services/log-server/src/main.rs +++ b/services/log-server/src/main.rs @@ -171,8 +171,17 @@ mod implementation { if cfg!(feature = "logging") { let mut inject_csr = CSR::new(arg as *mut u32); let mut uart_csr = CSR::new(unsafe { crate::debug::DEFAULT_UART_ADDR as *mut u32 }); + // println!("rxe {}", uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY)); + while uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY) == 0 { + // I really rather think this is more readable, than the "Rusty" version below. + inject_csr.wfo(utra::keyinject::UART_CHAR_CHAR, + uart_csr.rf(utra::uart::RXTX_RXTX)); + uart_csr.wfo(utra::uart::EV_PENDING_RX, 1); - loop { + // I guess this is how you would do it if you were "really doing Rust" + // (except this is checking pending not fifo status for loop termination) + // (which was really hard to figure out just looking at this loop) + /* let maybe_c = match uart_csr.rf(utra::uart::EV_PENDING_RX) { 0 => None, ack => { @@ -182,14 +191,13 @@ mod implementation { } }; if let Some(c) = maybe_c { - // println!("Inject {}", c); inject_csr.wfo(utra::keyinject::UART_CHAR_CHAR, (c & 0xff) as u32); } else { break; - } + }*/ } - // make sure this doesn't get disabled - uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1); + // println!("rxe {}", uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY)); + // println!("pnd {}", uart_csr.rf(utra::uart::EV_PENDING_RX)); } } @@ -204,7 +212,10 @@ mod implementation { while uart_csr.r(utra::uart::TXFULL) != 0 {} uart_csr.wo(utra::uart::RXTX, c as u32); - uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1); + // there's a race condition in the handler, if a new character comes in while handling the interrupt, + // the pending bit never clears. If the console seems to freeze, uncomment this line. + // This kind of works around that, at the expense of maybe losing some Rx characters. + // uart_csr.wfo(utra::uart::EV_PENDING_RX, 1); } }