PR-09 - VirtualFS

This commit is contained in:
Nilton Constantino
2026-01-16 22:24:24 +00:00
parent a8119d71f0
commit 5e5b703a56
12 changed files with 497 additions and 3 deletions
@@ -2,7 +2,9 @@ use crate::hardware::{HardwareBridge, InputSignals};
use crate::model::{Cartridge, Color, Sample};
use crate::prometeu_os::NativeInterface;
use crate::virtual_machine::{Value, VirtualMachine};
use crate::fs::{VirtualFS, FsBackend, FsState};
use std::sync::Arc;
use std::collections::HashMap;
/// PrometeuOS (POS): O firmware/base do sistema.
/// Autoridade máxima do boot, periféricos, execução da PVM e tratamento de falhas.
@@ -17,6 +19,12 @@ pub struct PrometeuOS {
pub sample_square: Option<Arc<Sample>>,
pub sample_kick: Option<Arc<Sample>>,
pub sample_snare: Option<Arc<Sample>>,
// Filesystem
pub fs: VirtualFS,
pub fs_state: FsState,
pub open_files: HashMap<u32, String>,
pub next_handle: u32,
}
impl PrometeuOS {
@@ -33,6 +41,10 @@ impl PrometeuOS {
sample_square: None,
sample_kick: None,
sample_snare: None,
fs: VirtualFS::new(),
fs_state: FsState::Unmounted,
open_files: HashMap::new(),
next_handle: 1,
};
// Inicializa samples básicos (mesma lógica do LogicalHardware)
@@ -41,6 +53,30 @@ impl PrometeuOS {
os
}
pub fn mount_fs(&mut self, backend: Box<dyn FsBackend>) {
match self.fs.mount(backend) {
Ok(_) => {
self.fs_state = FsState::Mounted;
}
Err(e) => {
self.fs_state = FsState::Error(e);
}
}
}
pub fn unmount_fs(&mut self) {
self.fs.unmount();
self.fs_state = FsState::Unmounted;
}
fn update_fs(&mut self) {
if self.fs_state == FsState::Mounted {
if !self.fs.is_healthy() {
self.unmount_fs();
}
}
}
pub fn reset(&mut self, vm: &mut VirtualMachine) {
*vm = VirtualMachine::default();
self.tick_index = 0;
@@ -60,6 +96,8 @@ impl PrometeuOS {
let start = std::time::Instant::now();
self.tick_index += 1;
self.update_fs();
if !self.logical_frame_active {
self.logical_frame_active = true;
self.logical_frame_remaining_cycles = Self::CYCLES_PER_LOGICAL_FRAME;
@@ -311,6 +349,107 @@ impl NativeInterface for PrometeuOS {
}
Ok(300)
}
// --- Filesystem Syscalls (0x4000) ---
// FS_OPEN(path) -> handle
0x4001 => {
let path = match vm.pop()? {
Value::String(s) => s,
_ => return Err("Expected string path".into()),
};
if self.fs_state != FsState::Mounted {
vm.push(Value::Integer(-1));
return Ok(100);
}
let handle = self.next_handle;
self.open_files.insert(handle, path);
self.next_handle += 1;
vm.push(Value::Integer(handle as i64));
Ok(200)
}
// FS_READ(handle) -> content
0x4002 => {
let handle = vm.pop_integer()? as u32;
let path = self.open_files.get(&handle).ok_or("Invalid handle")?;
match self.fs.read_file(path) {
Ok(data) => {
let s = String::from_utf8_lossy(&data).into_owned();
vm.push(Value::String(s));
Ok(1000)
}
Err(_e) => {
vm.push(Value::Null);
Ok(100)
}
}
}
// FS_WRITE(handle, content)
0x4003 => {
let content = match vm.pop()? {
Value::String(s) => s,
_ => return Err("Expected string content".into()),
};
let handle = vm.pop_integer()? as u32;
let path = self.open_files.get(&handle).ok_or("Invalid handle")?;
match self.fs.write_file(path, content.as_bytes()) {
Ok(_) => {
vm.push(Value::Boolean(true));
Ok(1000)
}
Err(_) => {
vm.push(Value::Boolean(false));
Ok(100)
}
}
}
// FS_CLOSE(handle)
0x4004 => {
let handle = vm.pop_integer()? as u32;
self.open_files.remove(&handle);
Ok(100)
}
// FS_LISTDIR(path)
0x4005 => {
let path = match vm.pop()? {
Value::String(s) => s,
_ => return Err("Expected string path".into()),
};
match self.fs.list_dir(&path) {
Ok(entries) => {
// Por enquanto, retorna uma string separada por ';'
let names: Vec<String> = entries.into_iter().map(|e| e.name).collect();
vm.push(Value::String(names.join(";")));
Ok(500)
}
Err(_) => {
vm.push(Value::Null);
Ok(100)
}
}
}
// FS_EXISTS(path) -> bool
0x4006 => {
let path = match vm.pop()? {
Value::String(s) => s,
_ => return Err("Expected string path".into()),
};
vm.push(Value::Boolean(self.fs.exists(&path)));
Ok(100)
}
// FS_DELETE(path)
0x4007 => {
let path = match vm.pop()? {
Value::String(s) => s,
_ => return Err("Expected string path".into()),
};
match self.fs.delete(&path) {
Ok(_) => vm.push(Value::Boolean(true)),
Err(_) => vm.push(Value::Boolean(false)),
}
Ok(500)
}
_ => Err(format!("Unknown syscall: 0x{:08X}", id)),
}
}