Better structure

This commit is contained in:
2025-10-28 20:51:19 +01:00
parent b42aeaacab
commit b1b0a78aad
4 changed files with 33 additions and 26 deletions
+22
View File
@@ -0,0 +1,22 @@
use std::fmt::Display;
#[derive(Debug, Clone)]
pub enum Instruction {
HLT,
REG(u8, usize),
ADD(u8, u8, u8),
}
impl Display for Instruction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Instruction::HLT => format!("HLT"),
Instruction::REG(a, b) => format!("REG {a}, {b}"),
Instruction::ADD(a, b, c) => format!("ADD {a}, {b}, {c}"),
}
)
}
}
+5 -24
View File
@@ -1,16 +1,11 @@
pub mod instruction;
pub mod registry;
use std::fmt::Display;
use osui::state::{DependencyHandler, State};
#[derive(Debug, Clone)]
pub struct Registry(pub State<[usize; 8]>);
#[derive(Debug, Clone)]
pub enum Instruction {
HLT,
REG(u8, usize),
ADD(u8, u8, u8),
}
use crate::emulator::{instruction::Instruction, registry::Registry};
pub struct Emulator {
pub pc: State<usize>,
@@ -22,7 +17,7 @@ pub struct Emulator {
impl Emulator {
pub fn run(&self) {
loop {
std::thread::sleep(std::time::Duration::from_millis(300));
std::thread::sleep(std::time::Duration::from_millis(100));
let mut pc = self.pc.get();
if let Some(inst) = self.instructions.get(**pc) {
**self.inst.get() = inst.clone();
@@ -85,17 +80,3 @@ impl DependencyHandler for Registry {
self.0.check()
}
}
impl Display for Instruction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Instruction::HLT => format!("HLT"),
Instruction::REG(a, b) => format!("REG {a}, {b}"),
Instruction::ADD(a, b, c) => format!("ADD {a}, {b}, {c}"),
}
)
}
}
+4
View File
@@ -0,0 +1,4 @@
use osui::state::State;
#[derive(Debug, Clone)]
pub struct Registry(pub State<[usize; 8]>);
+2 -2
View File
@@ -6,7 +6,7 @@ use std::sync::Arc;
use osui::prelude::*;
use crate::emulator::{Instruction, Registry};
use crate::emulator::{instruction::Instruction, registry::Registry};
fn main() {
let screen = Screen::new();
@@ -21,7 +21,7 @@ fn main() {
screen.run().unwrap();
}
fn app(_screen: &Arc<Screen>, instructions: Vec<emulator::Instruction>) -> Rsx {
fn app(_screen: &Arc<Screen>, instructions: Vec<Instruction>) -> Rsx {
let pc = use_state::<usize>(0);
let inst = use_state::<Instruction>(Instruction::HLT);
let registers = Registry(use_state([0; 8]));