Skip to content

Commit

Permalink
Refactor code in cpu.rs and emulator.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Clo91eaf committed Mar 14, 2024
1 parent 6585d83 commit 98a03ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::cmp;
use std::cmp::PartialEq;
use std::collections::BTreeMap;
use std::num::FpCategory;

use crate::{
Expand Down
40 changes: 20 additions & 20 deletions src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ pub struct Emulator {
impl Emulator {
/// Constructor for an emulator.
pub fn new() -> Emulator {
Self {
cpu: Cpu::new(),
}
Self { cpu: Cpu::new() }
}

/// Reset CPU state.
Expand All @@ -37,26 +35,28 @@ impl Emulator {
self.cpu.pc = pc;
}

fn execute(&mut self) -> Trap {
// Run a cycle on peripheral devices.
self.cpu.devices_increment();

// Take an interrupt.
match self.cpu.check_pending_interrupt() {
Some(interrupt) => interrupt.take_trap(&mut self.cpu),
None => {}
}

// Execute an instruction.
match self.cpu.execute() {
// Return a placeholder trap.
Ok(_) => Trap::Requested,
Err(exception) => exception.take_trap(&mut self.cpu),
}
}

/// Start executing the emulator.
pub fn start(&mut self) {
loop {
// Run a cycle on peripheral devices.
self.cpu.devices_increment();

// Take an interrupt.
match self.cpu.check_pending_interrupt() {
Some(interrupt) => interrupt.take_trap(&mut self.cpu),
None => {}
}

// Execute an instruction.
let trap = match self.cpu.execute() {
Ok(_) => {
// Return a placeholder trap.
Trap::Requested
}
Err(exception) => exception.take_trap(&mut self.cpu),
};
let trap = self.execute();

match trap {
Trap::Fatal => {
Expand Down

0 comments on commit 98a03ed

Please sign in to comment.