2019-06-28 09:46:36 +01:00
|
|
|
use crate::bit::BitIndex;
|
|
|
|
|
2019-07-08 17:56:12 +01:00
|
|
|
use crate::arm7tdmi::alu::*;
|
2019-07-05 13:34:52 +01:00
|
|
|
use crate::arm7tdmi::bus::Bus;
|
2019-06-30 14:59:19 +01:00
|
|
|
use crate::arm7tdmi::cpu::{Core, CpuExecResult, CpuPipelineAction};
|
|
|
|
use crate::arm7tdmi::exception::Exception;
|
2019-07-02 14:42:55 +01:00
|
|
|
use crate::arm7tdmi::psr::RegPSR;
|
2019-07-02 14:57:35 +01:00
|
|
|
use crate::arm7tdmi::{Addr, CpuError, CpuResult, CpuState, DecodedInstruction, REG_PC};
|
2019-06-30 14:59:19 +01:00
|
|
|
|
2019-07-07 21:10:17 +01:00
|
|
|
use super::*;
|
2019-06-25 03:35:52 +01:00
|
|
|
|
|
|
|
impl Core {
|
2019-07-05 11:07:07 +01:00
|
|
|
pub fn exec_arm(&mut self, bus: &mut Bus, insn: ArmInstruction) -> CpuExecResult {
|
2019-06-28 23:52:10 +01:00
|
|
|
if !self.check_arm_cond(insn.cond) {
|
|
|
|
return Ok(CpuPipelineAction::IncPC);
|
|
|
|
}
|
|
|
|
match insn.fmt {
|
2019-07-05 11:07:07 +01:00
|
|
|
ArmFormat::BX => self.exec_bx(bus, insn),
|
|
|
|
ArmFormat::B_BL => self.exec_b_bl(bus, insn),
|
|
|
|
ArmFormat::DP => self.exec_data_processing(bus, insn),
|
|
|
|
ArmFormat::SWI => self.exec_swi(bus, insn),
|
|
|
|
ArmFormat::LDR_STR => self.exec_ldr_str(bus, insn),
|
2019-07-07 21:10:17 +01:00
|
|
|
ArmFormat::LDR_STR_HS_IMM => self.exec_ldr_str_hs(bus, insn),
|
|
|
|
ArmFormat::LDR_STR_HS_REG => self.exec_ldr_str_hs(bus, insn),
|
2019-07-05 16:38:20 +01:00
|
|
|
ArmFormat::LDM_STM => self.exec_ldm_stm(bus, insn),
|
2019-07-05 11:07:07 +01:00
|
|
|
ArmFormat::MSR_REG => self.exec_msr_reg(bus, insn),
|
2019-07-10 19:44:00 +01:00
|
|
|
ArmFormat::MUL_MLA => self.exec_mul_mla(bus, insn),
|
2019-07-02 14:57:35 +01:00
|
|
|
_ => Err(CpuError::UnimplementedCpuInstruction(
|
|
|
|
insn.pc,
|
|
|
|
insn.raw,
|
|
|
|
DecodedInstruction::Arm(insn),
|
|
|
|
)),
|
2019-06-28 23:52:10 +01:00
|
|
|
}
|
2019-06-26 22:48:43 +01:00
|
|
|
}
|
|
|
|
|
2019-06-30 14:59:19 +01:00
|
|
|
/// Cycles 2S+1N
|
2019-07-05 13:34:52 +01:00
|
|
|
fn exec_b_bl(&mut self, _bus: &mut Bus, insn: ArmInstruction) -> CpuResult<CpuPipelineAction> {
|
2019-06-25 11:28:02 +01:00
|
|
|
if insn.link_flag() {
|
2019-07-01 17:24:52 +01:00
|
|
|
self.set_reg(14, (insn.pc + (self.word_size() as u32)) & !0b1);
|
2019-06-25 11:28:02 +01:00
|
|
|
}
|
2019-07-01 15:45:29 +01:00
|
|
|
|
2019-07-02 14:42:55 +01:00
|
|
|
self.pc = (self.pc as i32).wrapping_add(insn.branch_offset()) as u32 & !1;
|
2019-06-30 14:59:19 +01:00
|
|
|
|
2019-06-28 23:52:10 +01:00
|
|
|
Ok(CpuPipelineAction::Flush)
|
2019-06-25 11:28:02 +01:00
|
|
|
}
|
|
|
|
|
2019-06-30 14:59:19 +01:00
|
|
|
/// Cycles 2S+1N
|
2019-07-05 13:34:52 +01:00
|
|
|
fn exec_bx(&mut self, _bus: &mut Bus, insn: ArmInstruction) -> CpuResult<CpuPipelineAction> {
|
2019-06-25 11:28:02 +01:00
|
|
|
let rn = self.get_reg(insn.rn());
|
|
|
|
if rn.bit(0) {
|
2019-06-26 22:48:43 +01:00
|
|
|
self.cpsr.set_state(CpuState::THUMB);
|
2019-06-25 11:28:02 +01:00
|
|
|
} else {
|
2019-06-26 22:48:43 +01:00
|
|
|
self.cpsr.set_state(CpuState::ARM);
|
2019-06-25 11:28:02 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 23:52:10 +01:00
|
|
|
self.pc = rn & !1;
|
2019-06-30 14:59:19 +01:00
|
|
|
|
2019-06-28 23:52:10 +01:00
|
|
|
Ok(CpuPipelineAction::Flush)
|
2019-06-25 11:28:02 +01:00
|
|
|
}
|
|
|
|
|
2019-07-05 11:07:07 +01:00
|
|
|
fn exec_swi(&mut self, _bus: &mut Bus, _insn: ArmInstruction) -> CpuResult<CpuPipelineAction> {
|
2019-06-28 09:46:36 +01:00
|
|
|
self.exception(Exception::SoftwareInterrupt);
|
2019-06-28 23:52:10 +01:00
|
|
|
Ok(CpuPipelineAction::Flush)
|
2019-06-28 09:46:36 +01:00
|
|
|
}
|
|
|
|
|
2019-07-02 14:42:55 +01:00
|
|
|
fn exec_msr_reg(
|
|
|
|
&mut self,
|
2019-07-05 13:34:52 +01:00
|
|
|
_bus: &mut Bus,
|
2019-07-02 14:42:55 +01:00
|
|
|
insn: ArmInstruction,
|
|
|
|
) -> CpuResult<CpuPipelineAction> {
|
|
|
|
let new_psr = RegPSR::new(self.get_reg(insn.rm()));
|
|
|
|
let old_mode = self.cpsr.mode();
|
|
|
|
if insn.spsr_flag() {
|
|
|
|
if let Some(index) = old_mode.spsr_index() {
|
|
|
|
self.spsr[index] = new_psr;
|
|
|
|
} else {
|
|
|
|
panic!("tried to change spsr from invalid mode {}", old_mode)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if old_mode != new_psr.mode() {
|
|
|
|
self.change_mode(new_psr.mode());
|
|
|
|
}
|
|
|
|
self.cpsr = new_psr;
|
|
|
|
}
|
|
|
|
Ok(CpuPipelineAction::IncPC)
|
|
|
|
}
|
|
|
|
|
2019-06-30 14:59:19 +01:00
|
|
|
/// Logical/Arithmetic ALU operations
|
2019-07-01 15:45:29 +01:00
|
|
|
///
|
2019-06-30 14:59:19 +01:00
|
|
|
/// Cycles: 1S+x+y (from GBATEK)
|
|
|
|
/// Add x=1I cycles if Op2 shifted-by-register. Add y=1S+1N cycles if Rd=R15.
|
2019-06-26 22:48:43 +01:00
|
|
|
fn exec_data_processing(
|
|
|
|
&mut self,
|
2019-07-05 13:34:52 +01:00
|
|
|
_bus: &mut Bus,
|
2019-06-26 22:48:43 +01:00
|
|
|
insn: ArmInstruction,
|
|
|
|
) -> CpuResult<CpuPipelineAction> {
|
|
|
|
// TODO handle carry flag
|
|
|
|
|
2019-06-30 14:59:19 +01:00
|
|
|
let mut pipeline_action = CpuPipelineAction::IncPC;
|
|
|
|
|
2019-07-02 14:42:55 +01:00
|
|
|
let op1 = if insn.rn() == REG_PC {
|
|
|
|
self.pc as i32 // prefething
|
|
|
|
} else {
|
|
|
|
self.get_reg(insn.rn()) as i32
|
|
|
|
};
|
2019-06-26 22:48:43 +01:00
|
|
|
let op2 = insn.operand2()?;
|
|
|
|
|
2019-06-30 14:59:19 +01:00
|
|
|
let rd = insn.rd();
|
|
|
|
|
2019-06-26 22:48:43 +01:00
|
|
|
let op2: i32 = match op2 {
|
2019-07-08 17:56:12 +01:00
|
|
|
BarrelShifterValue::RotatedImmediate(immediate, rotate) => {
|
|
|
|
immediate.rotate_right(rotate) as i32
|
2019-06-26 22:48:43 +01:00
|
|
|
}
|
2019-07-08 17:56:12 +01:00
|
|
|
BarrelShifterValue::ShiftedRegister {
|
2019-06-26 22:48:43 +01:00
|
|
|
reg,
|
|
|
|
shift,
|
|
|
|
added: _,
|
2019-06-30 14:59:19 +01:00
|
|
|
} => {
|
|
|
|
// +1I
|
|
|
|
self.add_cycle();
|
2019-07-08 17:56:12 +01:00
|
|
|
let result = self.register_shift(reg, shift)?;
|
|
|
|
result
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
2019-06-26 22:48:43 +01:00
|
|
|
_ => unreachable!(),
|
2019-07-08 17:56:12 +01:00
|
|
|
};
|
2019-06-26 22:48:43 +01:00
|
|
|
|
|
|
|
let opcode = insn.opcode().unwrap();
|
2019-06-28 09:46:36 +01:00
|
|
|
let set_flags = opcode.is_setting_flags() || insn.set_cond_flag();
|
|
|
|
if let Some(result) = self.alu(opcode, op1, op2, set_flags) {
|
2019-06-30 14:59:19 +01:00
|
|
|
self.set_reg(rd, result as u32);
|
2019-07-02 14:42:55 +01:00
|
|
|
if rd == REG_PC {
|
2019-06-30 14:59:19 +01:00
|
|
|
pipeline_action = CpuPipelineAction::Flush;
|
|
|
|
}
|
2019-06-26 22:48:43 +01:00
|
|
|
}
|
|
|
|
|
2019-06-30 14:59:19 +01:00
|
|
|
Ok(pipeline_action)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Memory Load/Store
|
|
|
|
/// Instruction | Cycles | Flags | Expl.
|
|
|
|
/// ------------------------------------------------------------------------------
|
|
|
|
/// LDR{cond}{B}{T} Rd,<Address> | 1S+1N+1I+y | ---- | Rd=[Rn+/-<offset>]
|
|
|
|
/// STR{cond}{B}{T} Rd,<Address> | 2N | ---- | [Rn+/-<offset>]=Rd
|
|
|
|
/// ------------------------------------------------------------------------------
|
|
|
|
/// For LDR, add y=1S+1N if Rd=R15.
|
2019-06-28 23:52:10 +01:00
|
|
|
fn exec_ldr_str(
|
|
|
|
&mut self,
|
2019-07-05 11:07:07 +01:00
|
|
|
bus: &mut Bus,
|
2019-06-28 23:52:10 +01:00
|
|
|
insn: ArmInstruction,
|
|
|
|
) -> CpuResult<CpuPipelineAction> {
|
2019-06-30 14:59:19 +01:00
|
|
|
if insn.write_back_flag() && insn.rd() == insn.rn() {
|
|
|
|
return Err(CpuError::IllegalInstruction);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut pipeline_action = CpuPipelineAction::IncPC;
|
|
|
|
|
|
|
|
let mut addr = self.get_reg(insn.rn());
|
|
|
|
if insn.rn() == REG_PC {
|
2019-07-02 14:42:55 +01:00
|
|
|
addr = insn.pc + 8; // prefetching
|
2019-06-30 14:59:19 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 23:30:24 +01:00
|
|
|
let offset = self.get_barrel_shifted_value(insn.ldr_str_offset());
|
2019-06-30 14:59:19 +01:00
|
|
|
|
|
|
|
let effective_addr = (addr as i32).wrapping_add(offset) as Addr;
|
|
|
|
addr = if insn.pre_index_flag() {
|
|
|
|
effective_addr
|
2019-07-01 15:45:29 +01:00
|
|
|
} else {
|
2019-06-30 14:59:19 +01:00
|
|
|
addr
|
|
|
|
};
|
|
|
|
|
|
|
|
if insn.load_flag() {
|
|
|
|
let data = if insn.transfer_size() == 1 {
|
2019-07-05 11:07:07 +01:00
|
|
|
self.load_8(addr, bus) as u32
|
2019-06-30 14:59:19 +01:00
|
|
|
} else {
|
2019-07-05 11:07:07 +01:00
|
|
|
self.load_32(addr, bus)
|
2019-06-30 14:59:19 +01:00
|
|
|
};
|
2019-07-01 15:45:29 +01:00
|
|
|
|
2019-06-30 14:59:19 +01:00
|
|
|
self.set_reg(insn.rd(), data);
|
|
|
|
|
|
|
|
// +1I
|
|
|
|
self.add_cycle();
|
2019-07-05 12:09:04 +01:00
|
|
|
|
2019-06-30 14:59:19 +01:00
|
|
|
if insn.rd() == REG_PC {
|
|
|
|
pipeline_action = CpuPipelineAction::Flush;
|
|
|
|
}
|
|
|
|
} else {
|
2019-07-07 20:57:21 +01:00
|
|
|
let value = if insn.rd() == REG_PC {
|
|
|
|
insn.pc + 12
|
|
|
|
} else {
|
|
|
|
self.get_reg(insn.rd())
|
|
|
|
};
|
2019-06-30 14:59:19 +01:00
|
|
|
if insn.transfer_size() == 1 {
|
2019-07-05 11:07:07 +01:00
|
|
|
self.store_8(addr, value as u8, bus);
|
2019-06-30 14:59:19 +01:00
|
|
|
} else {
|
2019-07-05 11:07:07 +01:00
|
|
|
self.store_32(addr, value, bus);
|
2019-06-30 14:59:19 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-07 21:10:17 +01:00
|
|
|
if insn.write_back_flag() {
|
|
|
|
self.set_reg(insn.rn(), effective_addr as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(pipeline_action)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exec_ldr_str_hs(
|
|
|
|
&mut self,
|
|
|
|
bus: &mut Bus,
|
|
|
|
insn: ArmInstruction,
|
|
|
|
) -> CpuResult<CpuPipelineAction> {
|
|
|
|
if insn.write_back_flag() && insn.rd() == insn.rn() {
|
|
|
|
return Err(CpuError::IllegalInstruction);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut pipeline_action = CpuPipelineAction::IncPC;
|
|
|
|
|
|
|
|
let mut addr = self.get_reg(insn.rn());
|
|
|
|
if insn.rn() == REG_PC {
|
|
|
|
addr = insn.pc + 8; // prefetching
|
|
|
|
}
|
|
|
|
|
2019-07-08 23:30:24 +01:00
|
|
|
let offset = self.get_barrel_shifted_value(insn.ldr_str_hs_offset().unwrap());
|
2019-07-07 21:10:17 +01:00
|
|
|
|
|
|
|
let effective_addr = (addr as i32).wrapping_add(offset) as Addr;
|
|
|
|
addr = if insn.pre_index_flag() {
|
|
|
|
effective_addr
|
|
|
|
} else {
|
|
|
|
addr
|
|
|
|
};
|
|
|
|
|
|
|
|
if insn.load_flag() {
|
|
|
|
let data = match insn.halfword_data_transfer_type().unwrap() {
|
|
|
|
ArmHalfwordTransferType::SignedByte => self.load_8(addr, bus) as u8 as i8 as u32,
|
|
|
|
ArmHalfwordTransferType::SignedHalfwords => {
|
|
|
|
self.load_16(addr, bus) as u16 as i16 as u32
|
|
|
|
}
|
|
|
|
ArmHalfwordTransferType::UnsignedHalfwords => self.load_16(addr, bus) as u16 as u32,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.set_reg(insn.rd(), data);
|
|
|
|
|
|
|
|
// +1I
|
|
|
|
self.add_cycle();
|
|
|
|
|
|
|
|
if insn.rd() == REG_PC {
|
|
|
|
pipeline_action = CpuPipelineAction::Flush;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let value = if insn.rd() == REG_PC {
|
|
|
|
insn.pc + 12
|
|
|
|
} else {
|
|
|
|
self.get_reg(insn.rd())
|
|
|
|
};
|
|
|
|
|
|
|
|
match insn.halfword_data_transfer_type().unwrap() {
|
|
|
|
ArmHalfwordTransferType::UnsignedHalfwords => {
|
|
|
|
self.store_16(addr, value as u16, bus)
|
|
|
|
}
|
2019-07-07 21:33:47 +01:00
|
|
|
_ => panic!("invalid HS flags for L=0"),
|
2019-07-07 21:10:17 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-30 14:59:19 +01:00
|
|
|
if insn.write_back_flag() {
|
|
|
|
self.set_reg(insn.rn(), effective_addr as u32)
|
|
|
|
}
|
|
|
|
|
2019-07-01 15:45:29 +01:00
|
|
|
Ok(pipeline_action)
|
2019-06-25 03:35:52 +01:00
|
|
|
}
|
2019-07-05 16:38:20 +01:00
|
|
|
|
|
|
|
fn exec_ldm_stm(&mut self, bus: &mut Bus, insn: ArmInstruction) -> CpuExecResult {
|
|
|
|
let full = insn.pre_index_flag();
|
|
|
|
let ascending = insn.add_offset_flag();
|
|
|
|
let psr_user = insn.psr_and_force_user_flag();
|
|
|
|
let is_load = insn.load_flag();
|
|
|
|
let mut writeback = insn.write_back_flag();
|
|
|
|
let mut pipeline_action = CpuPipelineAction::IncPC;
|
|
|
|
let rn = insn.rn();
|
|
|
|
let mut addr = self.gpr[rn] as i32;
|
|
|
|
|
|
|
|
let step: i32 = if ascending { 4 } else { -4 };
|
|
|
|
let rlist = if ascending {
|
|
|
|
insn.register_list()
|
|
|
|
} else {
|
|
|
|
let mut rlist = insn.register_list();
|
|
|
|
rlist.reverse();
|
|
|
|
rlist
|
|
|
|
};
|
|
|
|
|
|
|
|
if psr_user {
|
|
|
|
unimplemented!("Too tired to implement the mode enforcement");
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_load {
|
|
|
|
if rlist.contains(&rn) {
|
|
|
|
writeback = false;
|
|
|
|
}
|
|
|
|
for r in rlist {
|
|
|
|
if full {
|
|
|
|
addr = addr.wrapping_add(step);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.add_cycle();
|
|
|
|
let val = self.load_32(addr as Addr, bus);
|
|
|
|
self.set_reg(r, val);
|
|
|
|
|
|
|
|
if r == REG_PC {
|
|
|
|
pipeline_action = CpuPipelineAction::Flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !full {
|
|
|
|
addr = addr.wrapping_add(step);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for r in rlist {
|
|
|
|
if full {
|
|
|
|
addr = addr.wrapping_add(step);
|
|
|
|
}
|
|
|
|
|
2019-07-06 21:38:08 +01:00
|
|
|
let val = if r == REG_PC {
|
|
|
|
insn.pc + 12
|
|
|
|
} else {
|
|
|
|
self.get_reg(r)
|
|
|
|
};
|
|
|
|
self.store_32(addr as Addr, val, bus);
|
2019-07-05 16:38:20 +01:00
|
|
|
|
|
|
|
if !full {
|
|
|
|
addr = addr.wrapping_add(step);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if writeback {
|
|
|
|
self.set_reg(rn, addr as u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(pipeline_action)
|
|
|
|
}
|
2019-07-10 19:44:00 +01:00
|
|
|
|
|
|
|
fn exec_mul_mla(
|
|
|
|
&mut self,
|
|
|
|
bus: &mut Bus,
|
|
|
|
insn: ArmInstruction,
|
|
|
|
) -> CpuResult<CpuPipelineAction> {
|
|
|
|
let rd = insn.rd();
|
|
|
|
let rn = insn.rn();
|
|
|
|
let rs = insn.rs();
|
|
|
|
let rm = insn.rm();
|
|
|
|
|
|
|
|
// check validity
|
|
|
|
if REG_PC == rd || REG_PC == rn || REG_PC == rs || REG_PC == rm {
|
|
|
|
return Err(CpuError::IllegalInstruction);
|
|
|
|
}
|
|
|
|
if rd == rm {
|
|
|
|
return Err(CpuError::IllegalInstruction);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !insn.accumulate_flag() {
|
|
|
|
self.set_reg(insn.rn(), 0);
|
|
|
|
} else {
|
|
|
|
panic!("accumelate not implemented yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
let op1 = self.get_reg(rm) as i32;
|
|
|
|
let op2 = self.get_reg(rs) as i32;
|
|
|
|
let result = (op1 * op2) as u32;
|
|
|
|
self.set_reg(rd, result);
|
|
|
|
|
|
|
|
let m = self.get_required_multipiler_array_cycles(op2);
|
|
|
|
for _ in 0..m {
|
|
|
|
self.add_cycle();
|
|
|
|
}
|
|
|
|
|
|
|
|
if insn.set_cond_flag() {
|
|
|
|
self.cpsr.set_N(result.bit(31));
|
|
|
|
self.cpsr.set_Z(result == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(CpuPipelineAction::IncPC)
|
|
|
|
}
|
2019-06-26 22:48:43 +01:00
|
|
|
}
|