216 lines
8.1 KiB
Rust
216 lines
8.1 KiB
Rust
//! Nominal ARM7 instruction-cycle summaries, separate from instruction semantics.
|
|
//! Code costs use source-fetch and target-pair addresses; data costs use actual bus accesses.
|
|
//! Successful execution records the next fetch kind in the instruction buffer.
|
|
|
|
use super::{Cpu, CpuError, InstructionSet};
|
|
use crate::{
|
|
memory::Memory,
|
|
timing::{AccessKind, AccessWidth, StepTiming},
|
|
};
|
|
|
|
#[derive(Default)]
|
|
pub(super) struct Summary {
|
|
data: bool,
|
|
pub(super) refill: bool,
|
|
pub(super) internal: u32,
|
|
}
|
|
|
|
impl Summary {
|
|
/// Data accesses and internal cycles break the following code sequence.
|
|
/// A refill ends with a sequential target fetch and starts a new sequence.
|
|
pub(super) fn next_fetch_kind(&self) -> AccessKind {
|
|
if !self.refill && (self.data || self.internal != 0) {
|
|
AccessKind::NonSequential
|
|
} else {
|
|
AccessKind::Sequential
|
|
}
|
|
}
|
|
|
|
fn branch() -> Self {
|
|
Self {
|
|
refill: true,
|
|
..Self::default()
|
|
}
|
|
}
|
|
|
|
fn transfer(load: bool, pc: bool) -> Self {
|
|
Self {
|
|
data: true,
|
|
refill: load && pc,
|
|
internal: u32::from(load),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl InstructionSet {
|
|
pub(super) fn access_width(self) -> AccessWidth {
|
|
match self {
|
|
Self::Arm => AccessWidth::Word,
|
|
Self::Thumb => AccessWidth::Halfword,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Cpu {
|
|
/// Execute one instruction and return its nominal cycle cost. Like Cpu::step,
|
|
/// this does not advance device time or sample interrupts. Machine does that.
|
|
/// Failed instructions discard their timing trace and retain existing diagnostics.
|
|
pub fn step_timed(&mut self, memory: &mut Memory) -> Result<StepTiming, CpuError> {
|
|
let fetched = self.fetch(memory)?;
|
|
let code_kind = memory.cpu_code_kind(self.next_fetch_kind());
|
|
// Source timing precedes data accesses and their WAITCNT writes.
|
|
memory.begin_cpu_timing(fetched.fill.is_some());
|
|
memory.record_code_access(
|
|
fetched.lookahead.address(),
|
|
self.instruction_set.access_width(),
|
|
code_kind,
|
|
);
|
|
let result = self.execute_fetched(fetched, memory);
|
|
let timing = memory.end_cpu_timing(result.is_ok());
|
|
result?;
|
|
Ok(timing)
|
|
}
|
|
|
|
/// Accept IRQ between instructions, discard the old-state fetch, then refill ARM.
|
|
/// A missing discarded fetch cannot prevent entry or become a current-slot error.
|
|
pub(crate) fn take_irq_timed(&mut self, memory: &mut Memory) -> Option<StepTiming> {
|
|
let state = self.instruction_set;
|
|
let cold = !self.has_retained_pipeline();
|
|
let address = self.pc().wrapping_add(2 * state.width());
|
|
let kind = memory.cpu_code_kind(self.next_fetch_kind());
|
|
if !self.take_interrupt(memory.irq_pending(), false) {
|
|
return None;
|
|
}
|
|
memory.begin_cpu_timing(cold);
|
|
let fetch = memory.fetch_instruction(address, state);
|
|
memory.record_code_access(fetch.address(), state.access_width(), kind);
|
|
memory.discarded_cpu_fetch(&fetch);
|
|
self.refill_pipeline(memory);
|
|
Some(memory.end_cpu_timing(true))
|
|
}
|
|
|
|
fn arm_summary(&self, instruction: u32) -> Summary {
|
|
if !self.flags.condition_passed(instruction >> 28) {
|
|
return Summary::default();
|
|
}
|
|
if instruction & 0x0f00_0000 == 0x0f00_0000
|
|
|| instruction & 0x0fff_fff0 == 0x012f_ff10
|
|
|| instruction & 0x0e00_0000 == 0x0a00_0000
|
|
{
|
|
return Summary::branch();
|
|
}
|
|
if instruction & 0x0fbf_0fff == 0x010f_0000
|
|
|| instruction & 0x0fb0_fff0 == 0x0120_f000
|
|
|| instruction & 0x0fb0_f000 == 0x0320_f000
|
|
{
|
|
return Summary::default(); // MRS/MSR overlap the data-processing space.
|
|
}
|
|
if instruction & 0x0e00_0000 == 0x0800_0000 {
|
|
let list = instruction & 0xffff;
|
|
return Summary::transfer(
|
|
instruction & (1 << 20) != 0,
|
|
list == 0 || list & 0x8000 != 0,
|
|
);
|
|
}
|
|
if instruction & 0x0fb0_0ff0 == 0x0100_0090 {
|
|
return Summary {
|
|
data: true,
|
|
internal: 1,
|
|
..Summary::default()
|
|
}; // SWP: two N data accesses
|
|
}
|
|
if instruction & 0x0f80_00f0 == 0x0080_0090 {
|
|
let signed = instruction & (1 << 22) != 0;
|
|
let multiplier = self.registers[((instruction >> 8) & 15) as usize];
|
|
return Summary {
|
|
internal: multiply_cycles(multiplier, signed)
|
|
+ 1
|
|
+ u32::from(instruction & (1 << 21) != 0),
|
|
..Summary::default()
|
|
};
|
|
}
|
|
if instruction & 0x0fc0_00f0 == 0x0000_0090 {
|
|
let multiplier = self.registers[((instruction >> 8) & 15) as usize];
|
|
return Summary {
|
|
internal: multiply_cycles(multiplier, true)
|
|
+ u32::from(instruction & (1 << 21) != 0),
|
|
..Summary::default()
|
|
};
|
|
}
|
|
if instruction & 0x0e00_0090 == 0x0000_0090 {
|
|
return Summary::transfer(instruction & (1 << 20) != 0, false);
|
|
}
|
|
if instruction & 0x0c00_0000 == 0x0400_0000 {
|
|
return Summary::transfer(instruction & (1 << 20) != 0, (instruction >> 12) & 15 == 15);
|
|
}
|
|
let test = (8..=11).contains(&((instruction >> 21) & 15));
|
|
Summary {
|
|
refill: !test && (instruction >> 12) & 15 == 15,
|
|
internal: u32::from(instruction & (1 << 25) == 0 && instruction & (1 << 4) != 0),
|
|
..Summary::default()
|
|
}
|
|
}
|
|
|
|
// Classify before instruction effects in both timed and untimed execution.
|
|
// A PC write can refill even when its target equals the sequential address.
|
|
pub(super) fn instruction_summary(&self, instruction: u32) -> Summary {
|
|
match self.instruction_set {
|
|
InstructionSet::Arm => self.arm_summary(instruction),
|
|
InstructionSet::Thumb => self.thumb_summary(instruction),
|
|
}
|
|
}
|
|
|
|
fn thumb_summary(&self, instruction: u32) -> Summary {
|
|
match instruction {
|
|
0x4000..=0x43ff => {
|
|
let opcode = (instruction >> 6) & 15;
|
|
let internal = match opcode {
|
|
2 | 3 | 4 | 7 => 1,
|
|
// Thumb MUL maps the incoming destination to ARM's Rs.
|
|
13 => multiply_cycles(self.registers[(instruction & 7) as usize], true),
|
|
_ => 0,
|
|
};
|
|
Summary {
|
|
internal,
|
|
..Summary::default()
|
|
}
|
|
}
|
|
0x4400..=0x47ff => {
|
|
let opcode = (instruction >> 8) & 3;
|
|
let destination = (instruction & 7) | ((instruction >> 4) & 8);
|
|
if opcode == 3 || (opcode != 1 && destination == 15) {
|
|
Summary::branch()
|
|
} else {
|
|
Summary::default()
|
|
}
|
|
}
|
|
0x4800..=0x4fff => Summary::transfer(true, false),
|
|
0x5000..=0x5fff => Summary::transfer((instruction >> 9) & 7 >= 3, false),
|
|
0x6000..=0x9fff => Summary::transfer(instruction & (1 << 11) != 0, false),
|
|
0xb400..=0xb5ff | 0xbc00..=0xbdff => Summary::transfer(
|
|
instruction & (1 << 11) != 0,
|
|
instruction & 0x100 != 0 || instruction & 0xff == 0,
|
|
),
|
|
0xc000..=0xcfff => {
|
|
Summary::transfer(instruction & (1 << 11) != 0, instruction & 255 == 0)
|
|
}
|
|
0xd000..=0xddff if self.flags.condition_passed((instruction >> 8) & 15) => {
|
|
Summary::branch()
|
|
}
|
|
0xdf00..=0xdfff | 0xe000..=0xe7ff | 0xf800..=0xffff => Summary::branch(),
|
|
// Thumb BL prefix fetches once; its suffix adds the target N+S pair.
|
|
_ => Summary::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn multiply_cycles(value: u32, signed: bool) -> u32 {
|
|
for bytes in 1..=3 {
|
|
let mask = u32::MAX << (bytes * 8);
|
|
let high = value & mask;
|
|
if high == 0 || signed && high == mask {
|
|
return bytes;
|
|
}
|
|
}
|
|
4
|
|
}
|