23 static uint32_t
arm_shift(uint8_t shift, uint32_t rm,
24 uint32_t shift_amount, uint8_t *carry)
26 uint32_t return_value = 0;
30 if (shift_amount > 0 && shift_amount < 32) {
31 return_value = rm << shift_amount;
32 *carry = rm >> (32 - shift_amount);
33 }
else if (shift_amount == 32) {
36 }
else if (shift_amount > 32) {
41 }
else if (shift == 0x1) {
42 if (shift_amount > 0 && shift_amount < 32) {
43 return_value = rm >> shift_amount;
44 *carry = (rm >> (shift_amount - 1)) & 1;
45 }
else if (shift_amount == 32) {
47 *carry = (rm >> 31) & 0x1;
48 }
else if (shift_amount > 32) {
53 }
else if (shift == 0x2) {
54 if (shift_amount > 0 && shift_amount < 32) {
60 return_value = rm >> shift_amount;
62 return_value |= 0xffffffff << (32 - shift_amount);
63 }
else if (shift_amount >= 32) {
64 if (rm & 0x80000000) {
65 return_value = 0xffffffff;
73 }
else if (shift == 0x3) {
74 if (shift_amount == 0)
77 shift_amount = shift_amount % 32;
78 return_value = (rm >> shift_amount) | (rm << (32 - shift_amount));
79 *carry = (return_value >> 31) & 0x1;
81 }
else if (shift == 0x4) {
82 return_value = rm >> 1;
94 uint8_t *shifter_carry_out)
96 uint32_t return_value;
100 instruction_size = 4;
102 instruction_size = 2;
104 *shifter_carry_out = sim->
get_cpsr(sim, 29, 1);
107 return_value = shifter_operand.
immediate.immediate;
108 else if (variant == 1) {
113 rm += 2 * instruction_size;
118 }
else if (variant == 2) {
124 rm += 2 * instruction_size;
127 rm,
rs, shifter_carry_out);
129 LOG_ERROR(
"BUG: shifter_operand.variant not 0, 1 or 2");
130 return_value = 0xffffffff;
138 switch ((opcode & 0xf0000000) >> 28) {
140 if (cpsr & 0x40000000)
145 if (!(cpsr & 0x40000000))
150 if (cpsr & 0x20000000)
155 if (!(cpsr & 0x20000000))
160 if (cpsr & 0x80000000)
165 if (!(cpsr & 0x80000000))
170 if (cpsr & 0x10000000)
175 if (!(cpsr & 0x10000000))
180 if ((cpsr & 0x20000000) && !(cpsr & 0x40000000))
185 if (!(cpsr & 0x20000000) || (cpsr & 0x40000000))
190 if (((cpsr & 0x80000000) && (cpsr & 0x10000000))
191 || (!(cpsr & 0x80000000) && !(cpsr & 0x10000000)))
196 if (((cpsr & 0x80000000) && !(cpsr & 0x10000000))
197 || (!(cpsr & 0x80000000) && (cpsr & 0x10000000)))
202 if (!(cpsr & 0x40000000) &&
203 (((cpsr & 0x80000000) && (cpsr & 0x10000000))
204 || (!(cpsr & 0x80000000) && !(cpsr & 0x10000000))))
209 if ((cpsr & 0x40000000) ||
210 ((cpsr & 0x80000000) && !(cpsr & 0x10000000))
211 || (!(cpsr & 0x80000000) && (cpsr & 0x10000000)))
236 uint32_t current_pc = sim->
get_reg(sim, 15);
274 if ((
opcode & 0xf000) == 0xd000
286 if ((
opcode & 0xf800) == 0xf000) {
302 uint32_t target_address;
314 *dry_run_pc = target_address & ~1;
318 sim->
set_reg(sim, 15, target_address);
320 uint32_t old_pc = sim->
get_reg(sim, 15);
323 sim->
set_reg(sim, 15, target_address);
325 if (target_address & 0x1)
329 sim->
set_reg(sim, 15, target_address & 0xfffffffe);
331 uint32_t old_pc = sim->
get_reg(sim, 15);
335 if (target_address & 0x1)
339 sim->
set_reg(sim, 15, target_address & 0xfffffffe);
348 uint32_t rd, rn, shifter_operand;
349 uint8_t c = sim->
get_cpsr(sim, 29, 1);
369 rd = rn & shifter_operand;
371 rd = rn ^ shifter_operand;
373 rd = rn - shifter_operand;
375 rd = shifter_operand - rn;
377 rd = rn + shifter_operand;
379 rd = rn + shifter_operand + (c & 1);
381 rd = rn - shifter_operand - (c & 1) ? 0 : 1;
383 rd = shifter_operand - rn - (c & 1) ? 0 : 1;
385 rd = rn | shifter_operand;
387 rd = rn & ~(shifter_operand);
389 rd = shifter_operand;
391 rd = ~shifter_operand;
397 *dry_run_pc = rd & ~1;
425 uint32_t load_address = 0, modified_address = 0, load_value = 0;
443 uint8_t carry = sim->
get_cpsr(sim, 29, 1);
448 modified_address = rn +
offset;
450 modified_address = rn -
offset;
452 LOG_ERROR(
"BUG: offset_mode neither 0 (offset) nor 1 (scaled register)");
459 load_address = modified_address;
460 modified_address = rn;
466 load_address = modified_address;
483 *dry_run_pc = load_value & ~1;
509 uint32_t load_values[16];
512 for (i = 0; i < 16; i++) {
525 rn = rn - (bits_set * 4) + 4;
528 rn = rn - (bits_set * 4);
532 for (i = 0; i < 16; i++) {
534 if ((!dry_run_pc) || (i == 15))
542 *dry_run_pc = load_values[15] & ~1;
553 for (i = 0; i < 16; i++) {
556 uint32_t val = load_values[i];
592 for (i = 0; i < 16; i++) {
605 rn = rn - (bits_set * 4) + 4;
608 rn = rn - (bits_set * 4);
612 for (i = 0; i < 16; i++) {
625 }
else if (!dry_run_pc) {
628 LOG_ERROR(
"Unimplemented instruction, could not simulate it.");
Holds the interface to ARM cores.
arm_mode
Represent state of an ARM core.
static struct arm * target_to_arm(const struct target *target)
Convert target handle to generic ARM target state handle.
arm_state
The PSR "T" and "J" bits define the mode of "classic ARM" cores.
int arm_evaluate_opcode(uint32_t opcode, uint32_t address, struct arm_instruction *instruction)
int thumb_evaluate_opcode(uint16_t opcode, uint32_t address, struct arm_instruction *instruction)
static void armv4_5_set_reg(struct arm_sim_interface *sim, int reg, uint32_t value)
static int thumb_pass_branch_condition(uint32_t cpsr, uint16_t opcode)
static void armv4_5_set_reg_mode(struct arm_sim_interface *sim, int reg, uint32_t value)
int arm_simulate_step(struct target *target, uint32_t *dry_run_pc)
static uint32_t arm_shift(uint8_t shift, uint32_t rm, uint32_t shift_amount, uint8_t *carry)
static uint32_t armv4_5_get_reg_mode(struct arm_sim_interface *sim, int reg)
static uint32_t arm_shifter_operand(struct arm_sim_interface *sim, int variant, union arm_shifter_operand shifter_operand, uint8_t *shifter_carry_out)
static uint32_t armv4_5_get_cpsr(struct arm_sim_interface *sim, int pos, int bits)
static void armv4_5_set_state(struct arm_sim_interface *sim, enum arm_state mode)
static int pass_condition(uint32_t cpsr, uint32_t opcode)
static int arm_simulate_step_core(struct target *target, uint32_t *dry_run_pc, struct arm_sim_interface *sim)
static enum arm_mode armv4_5_get_mode(struct arm_sim_interface *sim)
static uint32_t armv4_5_get_reg(struct arm_sim_interface *sim, int reg)
static enum arm_state armv4_5_get_state(struct arm_sim_interface *sim)
#define ARMV4_5_CORE_REG_MODE(cache, mode, num)
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
union arm_shifter_operand shifter_operand
union arm_instruction::@73 info
struct arm_load_store_multiple_instr load_store_multiple
unsigned int instruction_size
enum arm_instruction_type type
struct arm_b_bl_bx_blx_instr b_bl_bx_blx
struct arm_load_store_instr load_store
struct arm_data_proc_instr data_proc
uint32_t(* get_reg_mode)(struct arm_sim_interface *sim, int reg)
enum arm_mode(* get_mode)(struct arm_sim_interface *sim)
void(* set_reg)(struct arm_sim_interface *sim, int reg, uint32_t value)
uint32_t(* get_cpsr)(struct arm_sim_interface *sim, int pos, int bits)
uint32_t(* get_reg)(struct arm_sim_interface *sim, int reg)
void(* set_state)(struct arm_sim_interface *sim, enum arm_state mode)
void(* set_reg_mode)(struct arm_sim_interface *sim, int reg, uint32_t value)
enum arm_state(* get_state)(struct arm_sim_interface *sim)
Represents a generic ARM core, with standard application registers.
enum arm_mode core_mode
Record the current core mode: SVC, USR, or some other mode.
struct reg * cpsr
Handle to the CPSR/xPSR; valid in all core modes.
struct reg_cache * core_cache
enum arm_state core_state
Record the current core state: ARM, Thumb, or otherwise.
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
struct arm_shifter_operand::@70 register_shift
struct arm_shifter_operand::@69 immediate_shift