rename Machine -> LogicalHardware
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
pub mod machine;
|
||||
pub mod logical_hardware;
|
||||
pub mod peripherals;
|
||||
pub mod vm;
|
||||
mod model;
|
||||
mod native_interface;
|
||||
mod utilz;
|
||||
|
||||
pub use machine::Machine;
|
||||
pub use logical_hardware::LogicalHardware;
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::sync::Arc;
|
||||
|
||||
/// PROMETEU "hardware lógico" (v0).
|
||||
/// O Host alimenta INPUT SIGNALS e chama `step_frame()` em 60Hz.
|
||||
pub struct Machine {
|
||||
pub struct LogicalHardware {
|
||||
pub gfx: Gfx,
|
||||
pub audio: Audio,
|
||||
pub pad: Pad,
|
||||
@@ -23,13 +23,13 @@ pub struct Machine {
|
||||
|
||||
|
||||
|
||||
impl Machine {
|
||||
impl LogicalHardware {
|
||||
pub const W: usize = 320;
|
||||
pub const H: usize = 180;
|
||||
pub const CYCLES_PER_FRAME: u64 = 100_000; // Exemplo de budget
|
||||
|
||||
pub fn new() -> Self {
|
||||
let mut machine = Self {
|
||||
let mut logical_hardware = Self {
|
||||
gfx: Gfx::new(Self::W, Self::H),
|
||||
audio: Audio::new(),
|
||||
pad: Pad::default(),
|
||||
@@ -44,9 +44,9 @@ impl Machine {
|
||||
};
|
||||
|
||||
// Inicializa samples básicos
|
||||
machine.sample_square = Some(Arc::new(Self::create_square_sample(440.0, 0.1)));
|
||||
logical_hardware.sample_square = Some(Arc::new(Self::create_square_sample(440.0, 0.1)));
|
||||
|
||||
machine
|
||||
logical_hardware
|
||||
}
|
||||
|
||||
pub fn get_color(&self, index: usize) -> Color {
|
||||
@@ -116,7 +116,7 @@ impl Machine {
|
||||
let start = std::time::Instant::now();
|
||||
self.begin_frame(signals);
|
||||
|
||||
// Retira a VM temporariamente para executar run_budget passando self (Machine) como NativeInterface.
|
||||
// Retira a VM temporariamente para executar run_budget passando self (Logical Hardware) como NativeInterface.
|
||||
// Como VirtualMachine implementa Default, usamos std::mem::take.
|
||||
let mut vm = std::mem::take(&mut self.vm);
|
||||
let _result = vm.run_budget(Self::CYCLES_PER_FRAME, self);
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::vm::{Value, VirtualMachine};
|
||||
use crate::Machine;
|
||||
use crate::LogicalHardware;
|
||||
|
||||
pub trait NativeInterface {
|
||||
fn syscall(&mut self, id: u32, vm: &mut VirtualMachine) -> Result<u64, String>;
|
||||
}
|
||||
|
||||
impl NativeInterface for Machine {
|
||||
impl NativeInterface for LogicalHardware {
|
||||
fn syscall(&mut self, id: u32, vm: &mut VirtualMachine) -> Result<u64, String> {
|
||||
match id {
|
||||
// system.has_cart() -> bool
|
||||
|
||||
@@ -726,10 +726,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_system_run_cart() {
|
||||
use crate::Machine;
|
||||
use crate::LogicalHardware;
|
||||
use crate::model::Cartridge;
|
||||
|
||||
let mut machine = Machine::new();
|
||||
let mut logical_hardware = LogicalHardware::new();
|
||||
|
||||
// 1. Verifica que não tem cartucho inicialmente
|
||||
let mut rom = Vec::new();
|
||||
@@ -738,12 +738,12 @@ mod tests {
|
||||
utilz::emit_u32(&mut rom, 0x0001);
|
||||
utilz::emit_op(&mut rom, OpCode::Halt);
|
||||
|
||||
machine.vm = VirtualMachine::new(rom, vec![]);
|
||||
let mut vm = std::mem::take(&mut machine.vm);
|
||||
vm.run_budget(100, &mut machine).unwrap();
|
||||
machine.vm = vm;
|
||||
logical_hardware.vm = VirtualMachine::new(rom, vec![]);
|
||||
let mut vm = std::mem::take(&mut logical_hardware.vm);
|
||||
vm.run_budget(100, &mut logical_hardware).unwrap();
|
||||
logical_hardware.vm = vm;
|
||||
|
||||
assert_eq!(machine.vm.pop().unwrap(), Value::Boolean(false));
|
||||
assert_eq!(logical_hardware.vm.pop().unwrap(), Value::Boolean(false));
|
||||
|
||||
// 2. Adiciona um cartucho e roda
|
||||
let mut cart_rom = Vec::new();
|
||||
@@ -754,7 +754,7 @@ mod tests {
|
||||
let cart_pool = vec![Value::Integer(42)];
|
||||
|
||||
let cart = Cartridge::new(Program::new(cart_rom, cart_pool));
|
||||
machine.load_cartridge(cart);
|
||||
logical_hardware.load_cartridge(cart);
|
||||
|
||||
// Código para rodar o cartucho
|
||||
let mut boot_rom = Vec::new();
|
||||
@@ -762,14 +762,14 @@ mod tests {
|
||||
utilz::emit_op(&mut boot_rom, OpCode::Syscall);
|
||||
utilz::emit_u32(&mut boot_rom, 0x0002);
|
||||
|
||||
machine.vm = VirtualMachine::new(boot_rom, vec![]);
|
||||
let mut vm = std::mem::take(&mut machine.vm);
|
||||
vm.run_budget(1000, &mut machine).unwrap();
|
||||
machine.vm = vm;
|
||||
logical_hardware.vm = VirtualMachine::new(boot_rom, vec![]);
|
||||
let mut vm = std::mem::take(&mut logical_hardware.vm);
|
||||
vm.run_budget(1000, &mut logical_hardware).unwrap();
|
||||
logical_hardware.vm = vm;
|
||||
|
||||
// Após o run_budget, a VM deve ter executado o cartucho
|
||||
assert_eq!(machine.vm.operand_stack.last(), Some(&Value::Integer(42)));
|
||||
assert!(machine.vm.halted);
|
||||
assert_eq!(logical_hardware.vm.operand_stack.last(), Some(&Value::Integer(42)));
|
||||
assert!(logical_hardware.vm.halted);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user