2019-06-28 23:52:10 +01:00
|
|
|
use std::fmt;
|
|
|
|
|
2019-07-02 14:42:55 +01:00
|
|
|
use num::Num;
|
2020-01-16 18:06:22 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-07-02 14:42:55 +01:00
|
|
|
|
2019-06-23 00:57:14 +01:00
|
|
|
pub mod arm;
|
2019-07-02 14:57:35 +01:00
|
|
|
pub mod thumb;
|
|
|
|
|
2020-02-14 12:01:48 +00:00
|
|
|
use arm::ArmInstruction;
|
|
|
|
use thumb::ThumbInstruction;
|
2019-06-28 23:52:10 +01:00
|
|
|
|
2019-06-25 00:10:09 +01:00
|
|
|
pub mod cpu;
|
2019-07-01 15:45:29 +01:00
|
|
|
pub use cpu::*;
|
2019-07-08 17:56:12 +01:00
|
|
|
pub mod alu;
|
|
|
|
pub use alu::*;
|
2019-07-02 14:42:55 +01:00
|
|
|
pub mod exception;
|
|
|
|
pub mod psr;
|
2020-02-08 12:19:57 +00:00
|
|
|
pub use psr::*;
|
2019-06-27 13:13:38 +01:00
|
|
|
|
2019-06-24 18:20:08 +01:00
|
|
|
pub const REG_PC: usize = 15;
|
2019-06-28 09:46:36 +01:00
|
|
|
pub const REG_LR: usize = 14;
|
|
|
|
pub const REG_SP: usize = 13;
|
2019-06-24 18:20:08 +01:00
|
|
|
|
2020-05-01 16:13:00 +01:00
|
|
|
pub(self) use crate::Addr;
|
2019-06-30 14:59:19 +01:00
|
|
|
|
2020-02-10 23:28:20 +00:00
|
|
|
pub enum CpuAction {
|
|
|
|
AdvancePC,
|
2020-02-10 23:52:18 +00:00
|
|
|
FlushPipeline,
|
2020-02-10 23:28:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 12:47:10 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
|
2019-07-02 14:57:35 +01:00
|
|
|
pub enum DecodedInstruction {
|
|
|
|
Arm(ArmInstruction),
|
|
|
|
Thumb(ThumbInstruction),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DecodedInstruction {
|
|
|
|
pub fn get_pc(&self) -> Addr {
|
|
|
|
match self {
|
|
|
|
DecodedInstruction::Arm(a) => a.pc,
|
|
|
|
DecodedInstruction::Thumb(t) => t.pc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 12:19:57 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "debugger")]
|
2019-07-02 14:57:35 +01:00
|
|
|
impl fmt::Display for DecodedInstruction {
|
2020-02-14 12:01:48 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-07-02 14:57:35 +01:00
|
|
|
match self {
|
|
|
|
DecodedInstruction::Arm(a) => write!(f, "{}", a),
|
2019-07-02 23:26:48 +01:00
|
|
|
DecodedInstruction::Thumb(t) => write!(f, "{}", t),
|
2019-07-02 14:57:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 12:19:57 +00:00
|
|
|
pub trait InstructionDecoder: Sized {
|
2019-07-02 14:57:35 +01:00
|
|
|
type IntType: Num;
|
|
|
|
|
2020-02-14 11:05:14 +00:00
|
|
|
fn decode(n: Self::IntType, addr: Addr) -> Self;
|
2019-07-02 14:57:35 +01:00
|
|
|
/// Helper functions for the Disassembler
|
2020-02-14 11:05:14 +00:00
|
|
|
fn decode_from_bytes(bytes: &[u8], addr: Addr) -> Self;
|
2019-07-02 14:57:35 +01:00
|
|
|
fn get_raw(&self) -> Self::IntType;
|
|
|
|
}
|
|
|
|
|
2020-10-06 07:46:14 +01:00
|
|
|
pub fn reg_string<T: Into<usize>>(reg: T) -> &'static str {
|
2019-06-23 00:57:14 +01:00
|
|
|
let reg_names = &[
|
2019-06-25 00:10:09 +01:00
|
|
|
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "fp", "ip", "sp", "lr",
|
|
|
|
"pc",
|
2019-06-23 00:57:14 +01:00
|
|
|
];
|
2020-10-06 07:46:14 +01:00
|
|
|
reg_names[reg.into()]
|
2019-06-23 00:57:14 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 23:52:10 +01:00
|
|
|
#[derive(Debug, PartialEq, Primitive, Copy, Clone)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum CpuState {
|
|
|
|
ARM = 0,
|
|
|
|
THUMB = 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for CpuState {
|
2020-02-14 12:01:48 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-06-28 23:52:10 +01:00
|
|
|
use CpuState::*;
|
|
|
|
match self {
|
|
|
|
ARM => write!(f, "ARM"),
|
|
|
|
THUMB => write!(f, "THUMB"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Primitive, Copy, Clone, PartialEq)]
|
|
|
|
pub enum CpuMode {
|
|
|
|
User = 0b10000,
|
|
|
|
Fiq = 0b10001,
|
|
|
|
Irq = 0b10010,
|
|
|
|
Supervisor = 0b10011,
|
|
|
|
Abort = 0b10111,
|
|
|
|
Undefined = 0b11011,
|
|
|
|
System = 0b11111,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CpuMode {
|
|
|
|
pub fn spsr_index(&self) -> Option<usize> {
|
|
|
|
match self {
|
|
|
|
CpuMode::Fiq => Some(0),
|
|
|
|
CpuMode::Irq => Some(1),
|
|
|
|
CpuMode::Supervisor => Some(2),
|
|
|
|
CpuMode::Abort => Some(3),
|
|
|
|
CpuMode::Undefined => Some(4),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bank_index(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
CpuMode::User | CpuMode::System => 0,
|
|
|
|
CpuMode::Fiq => 1,
|
|
|
|
CpuMode::Irq => 2,
|
|
|
|
CpuMode::Supervisor => 3,
|
|
|
|
CpuMode::Abort => 4,
|
|
|
|
CpuMode::Undefined => 5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for CpuMode {
|
2020-02-14 12:01:48 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-06-28 23:52:10 +01:00
|
|
|
use CpuMode::*;
|
|
|
|
match self {
|
|
|
|
User => write!(f, "USR"),
|
|
|
|
Fiq => write!(f, "FIQ"),
|
|
|
|
Irq => write!(f, "IRQ"),
|
|
|
|
Supervisor => write!(f, "SVC"),
|
|
|
|
Abort => write!(f, "ABT"),
|
|
|
|
Undefined => write!(f, "UND"),
|
|
|
|
System => write!(f, "SYS"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|