change names of modules
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
mod prometeu_os;
|
||||
|
||||
pub use prometeu_os::PrometeuOS;
|
||||
pub use crate::vm::native_interface::NativeInterface;
|
||||
@@ -0,0 +1,229 @@
|
||||
use crate::model::{Cartridge, Color, Sample};
|
||||
use crate::hardware::{HardwareBridge, InputSignals};
|
||||
use crate::vm::{Value, VirtualMachine};
|
||||
use std::sync::Arc;
|
||||
use crate::prometeu_os::NativeInterface;
|
||||
|
||||
/// PrometeuOS (POS): O firmware/base do sistema.
|
||||
/// Autoridade máxima do boot, periféricos, execução da PVM e tratamento de falhas.
|
||||
pub struct PrometeuOS {
|
||||
pub vm: VirtualMachine,
|
||||
pub tick_index: u64,
|
||||
pub logical_frame_index: u64,
|
||||
pub logical_frame_open: bool,
|
||||
pub last_frame_cpu_time_us: u64,
|
||||
|
||||
// Assets de exemplo (mantidos para compatibilidade com syscalls de áudio v0)
|
||||
pub sample_square: Option<Arc<Sample>>,
|
||||
pub sample_kick: Option<Arc<Sample>>,
|
||||
pub sample_snare: Option<Arc<Sample>>,
|
||||
}
|
||||
|
||||
impl PrometeuOS {
|
||||
pub const CYCLES_PER_FRAME: u64 = 100_000;
|
||||
|
||||
pub fn new() -> Self {
|
||||
let mut os = Self {
|
||||
vm: VirtualMachine::default(),
|
||||
tick_index: 0,
|
||||
logical_frame_index: 0,
|
||||
logical_frame_open: false,
|
||||
last_frame_cpu_time_us: 0,
|
||||
sample_square: None,
|
||||
sample_kick: None,
|
||||
sample_snare: None,
|
||||
};
|
||||
|
||||
// Inicializa samples básicos (mesma lógica do LogicalHardware)
|
||||
os.sample_square = Some(Arc::new(Self::create_square_sample(440.0, 0.1)));
|
||||
|
||||
os
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
self.vm = VirtualMachine::default();
|
||||
self.tick_index = 0;
|
||||
self.logical_frame_index = 0;
|
||||
self.logical_frame_open = false;
|
||||
self.last_frame_cpu_time_us = 0;
|
||||
}
|
||||
|
||||
/// Carrega um cartucho na PVM e reseta o estado de execução.
|
||||
pub fn load_cartridge(&mut self, cart: &Cartridge) {
|
||||
// Na spec: "resetar PC/stack/heap ao iniciar app"
|
||||
self.vm.program = cart.program.clone();
|
||||
self.vm.pc = 0;
|
||||
self.vm.operand_stack.clear();
|
||||
self.vm.call_stack.clear();
|
||||
self.vm.globals.clear();
|
||||
self.vm.heap.clear();
|
||||
self.vm.halted = false;
|
||||
}
|
||||
|
||||
/// Executa um tick do host (60Hz).
|
||||
pub fn step_frame(&mut self, signals: &InputSignals, hw: &mut dyn HardwareBridge) -> Option<String> {
|
||||
let start = std::time::Instant::now();
|
||||
self.tick_index += 1;
|
||||
|
||||
if !self.logical_frame_open {
|
||||
self.logical_frame_open = true;
|
||||
self.begin_logical_frame(signals, hw);
|
||||
}
|
||||
|
||||
// Executa budget
|
||||
let mut vm = std::mem::take(&mut self.vm);
|
||||
let run_result = vm.run_budget(Self::CYCLES_PER_FRAME, self, hw);
|
||||
self.vm = vm;
|
||||
|
||||
match run_result {
|
||||
Ok(run) => {
|
||||
if run.reason == crate::vm::LogicalFrameEndingReason::FrameSync {
|
||||
hw.gfx_mut().render_all();
|
||||
self.end_logical_frame(hw);
|
||||
self.logical_frame_index += 1;
|
||||
self.logical_frame_open = false;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Some(format!("PVM Fault: {:?}", e));
|
||||
}
|
||||
}
|
||||
|
||||
self.last_frame_cpu_time_us = start.elapsed().as_micros() as u64;
|
||||
None
|
||||
}
|
||||
|
||||
fn begin_logical_frame(&mut self, signals: &InputSignals, hw: &mut dyn HardwareBridge) {
|
||||
hw.audio_mut().clear_commands();
|
||||
hw.touch_mut().begin_frame(signals);
|
||||
hw.pad_mut().begin_frame(signals);
|
||||
}
|
||||
|
||||
fn end_logical_frame(&mut self, hw: &mut dyn HardwareBridge) {
|
||||
hw.gfx_mut().present();
|
||||
}
|
||||
|
||||
// Helper para syscalls
|
||||
pub fn get_color(&self, index: usize, hw: &mut dyn HardwareBridge) -> Color {
|
||||
if let Some(bank) = hw.gfx().banks[0].as_ref() {
|
||||
bank.palettes[0][index % 16]
|
||||
} else {
|
||||
match index % 16 {
|
||||
0 => Color::BLACK,
|
||||
1 => Color::WHITE,
|
||||
2 => Color::RED,
|
||||
3 => Color::GREEN,
|
||||
4 => Color::BLUE,
|
||||
5 => Color::YELLOW,
|
||||
6 => Color::CYAN,
|
||||
7 => Color::INDIGO,
|
||||
8 => Color::GRAY,
|
||||
_ => Color::BLACK,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper para syscalls
|
||||
pub fn is_button_down(&self, id: u32, hw: &mut dyn HardwareBridge) -> bool {
|
||||
match id {
|
||||
0 => hw.pad().up.down,
|
||||
1 => hw.pad().down.down,
|
||||
2 => hw.pad().left.down,
|
||||
3 => hw.pad().right.down,
|
||||
4 => hw.pad().a.down,
|
||||
5 => hw.pad().b.down,
|
||||
6 => hw.pad().x.down,
|
||||
7 => hw.pad().y.down,
|
||||
8 => hw.pad().l.down,
|
||||
9 => hw.pad().r.down,
|
||||
10 => hw.pad().start.down,
|
||||
11 => hw.pad().select.down,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn create_square_sample(freq: f64, duration: f64) -> Sample {
|
||||
let sample_rate = crate::hardware::OUTPUT_SAMPLE_RATE;
|
||||
let num_samples = (duration * sample_rate as f64) as usize;
|
||||
let mut data = Vec::with_capacity(num_samples);
|
||||
let period = sample_rate as f64 / freq;
|
||||
|
||||
for i in 0..num_samples {
|
||||
let val = if (i as f64 % period) < (period / 2.0) {
|
||||
10000
|
||||
} else {
|
||||
-10000
|
||||
};
|
||||
data.push(val);
|
||||
}
|
||||
|
||||
Sample::new(sample_rate, data)
|
||||
}
|
||||
}
|
||||
|
||||
impl NativeInterface for PrometeuOS {
|
||||
fn syscall(&mut self, id: u32, vm: &mut VirtualMachine, hw: &mut dyn HardwareBridge) -> Result<u64, String> {
|
||||
match id {
|
||||
// system.has_cart() -> bool
|
||||
0x0001 => {
|
||||
// vm.push(Value::Boolean(self.current_cartridge.is_some()));
|
||||
Ok(10)
|
||||
}
|
||||
// system.run_cart()
|
||||
0x0002 => {
|
||||
// if let Some(cart) = self.current_cartridge.as_ref().cloned() {
|
||||
// self.load_cartridge(&cart);
|
||||
// } else {
|
||||
// return Err("No cartridge inserted".into());
|
||||
// }
|
||||
Ok(100)
|
||||
}
|
||||
// gfx.clear(color_index)
|
||||
0x1001 => {
|
||||
let color_idx = vm.pop_integer()? as usize;
|
||||
let color = self.get_color(color_idx, hw);
|
||||
hw.gfx_mut().clear(color);
|
||||
Ok(100)
|
||||
}
|
||||
// gfx.draw_rect(x, y, w, h, color_index)
|
||||
0x1002 => {
|
||||
let color_idx = vm.pop_integer()? as usize;
|
||||
let h = vm.pop_integer()? as i32;
|
||||
let w = vm.pop_integer()? as i32;
|
||||
let y = vm.pop_integer()? as i32;
|
||||
let x = vm.pop_integer()? as i32;
|
||||
let color = self.get_color(color_idx, hw);
|
||||
hw.gfx_mut().fill_rect(x, y, w, h, color);
|
||||
Ok(200)
|
||||
}
|
||||
// input.get_pad(button_id) -> bool
|
||||
0x2001 => {
|
||||
let button_id = vm.pop_integer()? as u32;
|
||||
let is_down = self.is_button_down(button_id, hw);
|
||||
vm.push(Value::Boolean(is_down));
|
||||
Ok(50)
|
||||
}
|
||||
// audio.play_sample(sample_id, voice_id, volume, pan, pitch)
|
||||
0x3001 => {
|
||||
let pitch = vm.pop_number()?;
|
||||
let pan = vm.pop_integer()? as u8;
|
||||
let volume = vm.pop_integer()? as u8;
|
||||
let voice_id = vm.pop_integer()? as usize;
|
||||
let sample_id = vm.pop_integer()? as u32;
|
||||
|
||||
let sample = match sample_id {
|
||||
0 => self.sample_square.clone(),
|
||||
1 => self.sample_kick.clone(),
|
||||
2 => self.sample_snare.clone(),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
if let Some(s) = sample {
|
||||
hw.audio_mut().play(s, voice_id, volume, pan, pitch, 0, crate::hardware::LoopMode::Off);
|
||||
}
|
||||
Ok(300)
|
||||
}
|
||||
_ => Err(format!("Unknown syscall: 0x{:08X}", id)),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user