Skip to content

Commit

Permalink
sapemu: minor performance work
Browse files Browse the repository at this point in the history
  • Loading branch information
kleinesfilmroellchen committed Oct 14, 2024
1 parent 16138a4 commit d75a5bf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ lto = "thin"
opt-level = 3
incremental = true

# development profile for performance testing, similar to "bench"
[profile.perfcheck]
inherits = "spcasm-fastrelease"
debug = 2
debug-assertions = true

[profile.spcasm-release]
inherits = "spcasm-fastrelease"
lto = "fat"
Expand Down
40 changes: 31 additions & 9 deletions sapemu/src/smp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::marker::ConstParamTy;

use bitflags::bitflags;
use log::{debug, error};
use ops::InstructionImpl;
use spcasm::sema::Register;

use self::ops::InstructionInternalState;
Expand All @@ -17,7 +18,7 @@ use crate::smp::ops::OPCODE_TABLE;
use crate::trace;

/// State of the microprocessor.
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug)]
pub struct Smp {
/// TEST register.
pub test: TestRegister,
Expand Down Expand Up @@ -51,6 +52,32 @@ pub struct Smp {

/// CPU execution state.
pub(crate) run_state: RunState,

/// Cached function that executes the current instruction.
pub(crate) instruction_function: InstructionImpl,
}

impl Default for Smp {
fn default() -> Self {
Self {
test: TestRegister::default(),
control: ControlRegister::default(),
a: Default::default(),
x: Default::default(),
y: Default::default(),
pc: Default::default(),
sp: Default::default(),
psw: ProgramStatusWord::default(),
ports: CpuIOPorts::default(),
timers: Timers::default(),
cycle_counter: Default::default(),
instruction_cycle: Default::default(),
current_opcode: Default::default(),
last_instruction_state: InstructionInternalState::default(),
run_state: RunState::default(),
instruction_function: ops::nop,
}
}
}

/// CPU execution state.
Expand Down Expand Up @@ -204,7 +231,6 @@ impl Timers {
}
self.timer_tick_remaining[finalized_timer] = Self::TIMER_CLOCKS_PER_STEP[finalized_timer] * divisor;
self.timer_out[finalized_timer] = (self.timer_out[finalized_timer] + 1) % 0xf;
#[cfg(debug_assertions)]
trace!(
"Timer {} step to {} ({}Hz / {})",
finalized_timer,
Expand Down Expand Up @@ -370,14 +396,14 @@ impl Smp {
if self.run_state != RunState::Running {
return;
}
#[cfg(debug_assertions)]
trace!("SMP tick {}", self.cycle_counter);

self.timers.tick(self.control);

// Fetch next instruction
if self.instruction_cycle == 0 {
self.current_opcode = self.read_next_pc(memory);
self.instruction_function = OPCODE_TABLE[self.current_opcode as usize];
trace!(
"(@{}) fetch instruction [{:04x}] = {:02x}",
self.cycle_counter,
Expand All @@ -387,12 +413,8 @@ impl Smp {
}

// Execute tick
let instruction_result = OPCODE_TABLE[self.current_opcode as usize](
self,
memory,
self.instruction_cycle,
self.last_instruction_state,
);
let instruction_result =
(self.instruction_function)(self, memory, self.instruction_cycle, self.last_instruction_state);
// Decide whether to advance to next instruction or not
match instruction_result {
ops::MicroArchAction::Continue(new_state) => {
Expand Down
7 changes: 6 additions & 1 deletion sapemu/src/smp/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,12 @@ macro_rules! debug_instruction {
};
}

fn nop(cpu: &mut Smp, memory: &mut Memory, cycle: usize, state: InstructionInternalState) -> MicroArchAction {
pub(super) fn nop(
cpu: &mut Smp,
memory: &mut Memory,
cycle: usize,
state: InstructionInternalState,
) -> MicroArchAction {
debug_instruction!("nop", cycle, cpu);

match cycle {
Expand Down

0 comments on commit d75a5bf

Please sign in to comment.