|
| 1 | +//! Direct UART implementation for AArch64 |
| 2 | +//! |
| 3 | +//! This module provides UART functionality that bypasses LLVM's loop |
| 4 | +//! compilation issues by using inline assembly for the critical loop |
| 5 | +//! operations. |
| 6 | +
|
| 7 | +use core::fmt; |
| 8 | + |
| 9 | +/// UART base address for QEMU virt machine |
| 10 | +#[allow(dead_code)] |
| 11 | +const UART0_BASE: usize = 0x0900_0000; |
| 12 | + |
| 13 | +/// Write bytes to UART using pure assembly - avoiding all Rust constructs |
| 14 | +/// |
| 15 | +/// This implementation uses pure inline assembly for the entire operation. |
| 16 | +unsafe fn uart_write_bytes_asm(ptr: *const u8, len: usize) { |
| 17 | + // Use inline assembly to perform the entire operation |
| 18 | + core::arch::asm!( |
| 19 | + "mov {uart}, #0x09000000", // Load UART base address |
| 20 | + "mov {i}, #0", // Initialize counter |
| 21 | + "1:", // Loop start |
| 22 | + "cmp {i}, {len}", // Compare counter with length |
| 23 | + "b.ge 2f", // Branch if counter >= length |
| 24 | + "ldrb {byte:w}, [{ptr}, {i}]", // Load byte from string[i] |
| 25 | + "strb {byte:w}, [{uart}]", // Store byte to UART |
| 26 | + "add {i}, {i}, #1", // Increment counter |
| 27 | + "b 1b", // Branch back to loop |
| 28 | + "2:", // End |
| 29 | + ptr = in(reg) ptr, |
| 30 | + len = in(reg) len, |
| 31 | + uart = out(reg) _, |
| 32 | + i = out(reg) _, |
| 33 | + byte = out(reg) _, |
| 34 | + options(nostack, preserves_flags) |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +/// Print a string directly to UART |
| 39 | +pub fn direct_print_str(s: &str) { |
| 40 | + unsafe { |
| 41 | + uart_write_bytes_asm(s.as_ptr(), s.len()); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Print a single character directly to UART |
| 46 | +pub fn direct_print_char(c: char) { |
| 47 | + let mut buffer = [0u8; 4]; |
| 48 | + let str_slice = c.encode_utf8(&mut buffer); |
| 49 | + direct_print_str(str_slice); |
| 50 | +} |
| 51 | + |
| 52 | +/// Print a newline character |
| 53 | +pub fn direct_print_newline() { |
| 54 | + direct_print_char('\n'); |
| 55 | +} |
| 56 | + |
| 57 | +/// Print a number in decimal format |
| 58 | +pub fn direct_print_num(n: u64) { |
| 59 | + if n == 0 { |
| 60 | + direct_print_char('0'); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Convert number to string manually to avoid heap allocation |
| 65 | + let mut buffer = [0u8; 20]; // Enough for u64::MAX |
| 66 | + let mut pos = buffer.len(); |
| 67 | + let mut num = n; |
| 68 | + |
| 69 | + while num > 0 { |
| 70 | + pos -= 1; |
| 71 | + buffer[pos] = b'0' + (num % 10) as u8; |
| 72 | + num /= 10; |
| 73 | + } |
| 74 | + |
| 75 | + unsafe { |
| 76 | + uart_write_bytes_asm(buffer.as_ptr().add(pos), buffer.len() - pos); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// A writer that implements fmt::Write for use with format macros |
| 81 | +pub struct DirectUartWriter; |
| 82 | + |
| 83 | +impl fmt::Write for DirectUartWriter { |
| 84 | + fn write_str(&mut self, s: &str) -> fmt::Result { |
| 85 | + direct_print_str(s); |
| 86 | + Ok(()) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Create a new UART writer |
| 91 | +pub fn writer() -> DirectUartWriter { |
| 92 | + DirectUartWriter |
| 93 | +} |
| 94 | + |
| 95 | +/// Initialize UART (no-op for QEMU, but kept for compatibility) |
| 96 | +pub fn init() { |
| 97 | + // QEMU's UART doesn't need initialization, but we can add |
| 98 | + // basic setup here if needed for real hardware |
| 99 | +} |
| 100 | + |
| 101 | +#[cfg(test)] |
| 102 | +mod tests { |
| 103 | + use super::*; |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_basic_print() { |
| 107 | + direct_print_str("Test message\n"); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn test_number_print() { |
| 112 | + direct_print_num(12345); |
| 113 | + direct_print_newline(); |
| 114 | + } |
| 115 | +} |
0 commit comments