Files
aios/kernel/src/main.rs
2025-07-05 15:00:10 +09:00

208 lines
5.2 KiB
Rust

//! Aios Kernel - Interactive Shell
#![no_std]
#![no_main]
mod keyboard_basic;
use core::panic::PanicInfo;
// Simple VGA buffer writer
struct VgaBuffer {
chars: [[u16; 80]; 25],
}
static mut VGA_BUFFER: *mut VgaBuffer = 0xb8000 as *mut VgaBuffer;
static mut CURSOR_ROW: usize = 12;
static mut CURSOR_COL: usize = 6;
pub fn print_char(c: u8, x: usize, y: usize) {
if x >= 80 || y >= 25 {
return;
}
unsafe {
let vga = &mut *VGA_BUFFER;
vga.chars[y][x] = (0x0f << 8) | (c as u16);
}
}
pub fn print_str(s: &str, x: usize, y: usize) {
for (i, byte) in s.bytes().enumerate() {
if x + i >= 80 {
break;
}
print_char(byte, x + i, y);
}
}
pub fn clear_screen() {
for y in 0..25 {
for x in 0..80 {
print_char(b' ', x, y);
}
}
}
// Shell state
static mut INPUT_BUFFER: [u8; 64] = [0; 64];
static mut INPUT_POS: usize = 0;
fn print_prompt() {
unsafe {
print_str("aios> ", 0, CURSOR_ROW);
CURSOR_COL = 6;
}
}
fn new_line() {
unsafe {
CURSOR_ROW += 1;
CURSOR_COL = 0;
if CURSOR_ROW >= 24 {
CURSOR_ROW = 23;
}
}
}
fn execute_command() {
unsafe {
let cmd = core::str::from_utf8(&INPUT_BUFFER[..INPUT_POS]).unwrap_or("");
new_line();
match cmd {
"help" => {
print_str("Aios Commands:", 0, CURSOR_ROW);
new_line();
print_str(" help - Show this help", 0, CURSOR_ROW);
new_line();
print_str(" version - Show version", 0, CURSOR_ROW);
new_line();
print_str(" echo - Echo test", 0, CURSOR_ROW);
new_line();
print_str(" clear - Clear screen", 0, CURSOR_ROW);
new_line();
}
"version" => {
print_str("Aios 2.0 - AI Operating System", 0, CURSOR_ROW);
new_line();
print_str("Interactive Shell with Keyboard Support", 0, CURSOR_ROW);
new_line();
}
"echo" => {
print_str("Hello from Aios Interactive Shell!", 0, CURSOR_ROW);
new_line();
}
"clear" => {
clear_screen();
CURSOR_ROW = 2;
print_str("Aios - AI Operating System", 25, 0);
print_str("Interactive Shell Ready", 27, 1);
CURSOR_ROW = 3;
}
"" => {
// Empty command
}
_ => {
print_str("Unknown command: ", 0, CURSOR_ROW);
print_str(cmd, 17, CURSOR_ROW);
new_line();
print_str("Type 'help' for available commands", 0, CURSOR_ROW);
new_line();
}
}
// Clear input buffer
INPUT_BUFFER.fill(0);
INPUT_POS = 0;
new_line();
print_prompt();
}
}
fn process_char(ch: char) {
unsafe {
match ch {
'\n' => {
execute_command();
}
'\u{8}' => {
// Backspace
if INPUT_POS > 0 {
INPUT_POS -= 1;
INPUT_BUFFER[INPUT_POS] = 0;
CURSOR_COL -= 1;
print_char(b' ', CURSOR_COL, CURSOR_ROW);
}
}
' '..='~' => {
// Printable characters
if INPUT_POS < INPUT_BUFFER.len() - 1 {
INPUT_BUFFER[INPUT_POS] = ch as u8;
print_char(ch as u8, CURSOR_COL, CURSOR_ROW);
INPUT_POS += 1;
CURSOR_COL += 1;
}
}
_ => {
// Ignore other characters
}
}
}
}
// Entry point
#[no_mangle]
pub extern "C" fn _start() -> ! {
// Clear screen
clear_screen();
// Print welcome message
print_str("Aios - AI Operating System", 25, 0);
print_str("Interactive Shell Ready", 27, 1);
print_str("Type 'help' for commands", 27, 2);
// Initialize shell
unsafe {
CURSOR_ROW = 5;
}
print_prompt();
let mut last_scancode = 0u8;
loop {
if keyboard_basic::has_key() {
let scancode = keyboard_basic::read_scancode();
// Only process key press (not key release)
if scancode != last_scancode && (scancode & 0x80) == 0 {
last_scancode = scancode;
if let Some(ch) = keyboard_basic::scancode_to_char(scancode) {
process_char(ch);
}
}
}
// Small delay to avoid excessive polling
for _ in 0..1000 {
unsafe {
core::arch::asm!("nop");
}
}
}
}
// Panic handler
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
clear_screen();
print_str("!!! KERNEL PANIC !!!", 29, 12);
print_str("System halted.", 33, 13);
loop {
unsafe {
core::arch::asm!("hlt");
}
}
}