📄 thumbemu.c
字号:
}; /* The offset range defends on whether we are transferring a byte or word value: */ *ainstr = subset[(tinstr & 0x1800) >> 11] /* base */ | ((tinstr & 0x0007) << (12 - 0)) /* Rd */ | ((tinstr & 0x0038) << (16 - 3)) /* Rb */ | ((tinstr & 0x07C0) >> (6 - ((tinstr & (1 << 12)) ? 0 : 2))); /* off5 */ } break; case 16: /* STRH Rd,[Rb,#imm5] */ case 17: /* LDRH Rd,[Rb,#imm5] */ /* Format 10 */ *ainstr = ((tinstr & (1 << 11)) /* base */ ? 0xE1D000B0 /* LDRH */ : 0xE1C000B0) /* STRH */ | ((tinstr & 0x0007) << (12 - 0)) /* Rd */ | ((tinstr & 0x0038) << (16 - 3)) /* Rb */ | ((tinstr & 0x01C0) >> (6 - 1)) /* off5, low nibble */ | ((tinstr & 0x0600) >> (9 - 8)); /* off5, high nibble */ break; case 18: /* STR Rd,[SP,#imm8] */ case 19: /* LDR Rd,[SP,#imm8] */ /* Format 11 */ *ainstr = ((tinstr & (1 << 11)) /* base */ ? 0xE59D0000 /* LDR */ : 0xE58D0000) /* STR */ | ((tinstr & 0x0700) << (12 - 8)) /* Rd */ | ((tinstr & 0x00FF) << 2); /* off8 */ break; case 20: /* ADD Rd,PC,#imm8 */ case 21: /* ADD Rd,SP,#imm8 */ /* Format 12 */ if ((tinstr & (1 << 11)) == 0) { /* NOTE: The PC value used here should by word aligned */ /* We encode shift-left-by-2 in the rotate immediate field, so no shift of off8 is needed. */ *ainstr = 0xE28F0F00 /* base */ | ((tinstr & 0x0700) << (12 - 8)) /* Rd */ | (tinstr & 0x00FF); /* off8 */ } else { /* We encode shift-left-by-2 in the rotate immediate field, so no shift of off8 is needed. */ *ainstr = 0xE28D0F00 /* base */ | ((tinstr & 0x0700) << (12 - 8)) /* Rd */ | (tinstr & 0x00FF); /* off8 */ } break; case 22: case 23: switch (tinstr & 0x0F00) { case 0x0000: /* Format 13 */ /* NOTE: The instruction contains a shift left of 2 equivalent (implemented as ROR #30): */ *ainstr = ((tinstr & (1 << 7)) /* base */ ? 0xE24DDF00 /* SUB */ : 0xE28DDF00) /* ADD */ | (tinstr & 0x007F); /* off7 */ break; case 0x0400: /* Format 14 - Push */ * ainstr = 0xE92D0000 | (tinstr & 0x00FF); break; case 0x0500: /* Format 14 - Push + LR */ * ainstr = 0xE92D4000 | (tinstr & 0x00FF); break; case 0x0c00: /* Format 14 - Pop */ * ainstr = 0xE8BD0000 | (tinstr & 0x00FF); break; case 0x0d00: /* Format 14 - Pop + PC */ * ainstr = 0xE8BD8000 | (tinstr & 0x00FF); break; case 0x0e00: if (state->is_v5) { /* This is normally an undefined instruction. The v5t architecture defines this particular pattern as a BKPT instruction, for hardware assisted debugging. We map onto the arm BKPT instruction. */ * ainstr = 0xE1200070 | ((tinstr & 0xf0) << 4) | (tinstr & 0xf); break; } /* Drop through. */ default: /* Everything else is an undefined instruction. */ valid = t_undefined; break; } break; case 24: /* STMIA */ case 25: /* LDMIA */ /* Format 15 */ *ainstr = ((tinstr & (1 << 11)) /* base */ ? 0xE8B00000 /* LDMIA */ : 0xE8A00000) /* STMIA */ | ((tinstr & 0x0700) << (16 - 8)) /* Rb */ | (tinstr & 0x00FF); /* mask8 */ break; case 26: /* Bcc */ case 27: /* Bcc/SWI */ if ((tinstr & 0x0F00) == 0x0F00) { /* Format 17 : SWI */ *ainstr = 0xEF000000; /* Breakpoint must be handled specially. */ if ((tinstr & 0x00FF) == 0x18) *ainstr |= ((tinstr & 0x00FF) << 16); /* New breakpoint value. See gdb/arm-tdep.c */ else if ((tinstr & 0x00FF) == 0xFE) *ainstr |= SWI_Breakpoint; else *ainstr |= (tinstr & 0x00FF); } else if ((tinstr & 0x0F00) != 0x0E00) { /* Format 16 */ int doit = FALSE; /* TODO: Since we are doing a switch here, we could just add the SWI and undefined instruction checks into this switch to same on a couple of conditionals: */ switch ((tinstr & 0x0F00) >> 8) { case EQ: doit = ZFLAG; break; case NE: doit = !ZFLAG; break; case VS: doit = VFLAG; break; case VC: doit = !VFLAG; break; case MI: doit = NFLAG; break; case PL: doit = !NFLAG; break; case CS: doit = CFLAG; break; case CC: doit = !CFLAG; break; case HI: doit = (CFLAG && !ZFLAG); break; case LS: doit = (!CFLAG || ZFLAG); break; case GE: doit = ((!NFLAG && !VFLAG) || (NFLAG && VFLAG)); break; case LT: doit = ((NFLAG && !VFLAG) || (!NFLAG && VFLAG)); break; case GT: doit = ((!NFLAG && !VFLAG && !ZFLAG) || (NFLAG && VFLAG && !ZFLAG)); break; case LE: doit = ((NFLAG && !VFLAG) || (!NFLAG && VFLAG)) || ZFLAG; break; } if (doit) { state->Reg[15] = (pc + 4 + (((tinstr & 0x7F) << 1) | ((tinstr & (1 << 7)) ? 0xFFFFFF00 : 0))); FLUSHPIPE; } valid = t_branch; } else /* UNDEFINED : cc=1110(AL) uses different format */ valid = t_undefined; break; case 28: /* B */ /* Format 18 */ state->Reg[15] = (pc + 4 + (((tinstr & 0x3FF) << 1) | ((tinstr & (1 << 10)) ? 0xFFFFF800 : 0))); FLUSHPIPE; valid = t_branch; break; case 29: /* UNDEFINED */ if (state->is_v5) { if (tinstr & 1) { valid = t_undefined; break; } /* Drop through. */ /* Format 19 */ /* There is no single ARM instruction equivalent for this instruction. Also, it should only ever be matched with the fmt19 "BL/BLX instruction 1" instruction. However, we do allow the simulation of it on its own, with undefined results if r14 is not suitably initialised. */ { ARMword tmp = (pc + 2); state->Reg[15] = ((state->Reg[14] + ((tinstr & 0x07FF) << 1)) & 0xFFFFFFFC); CLEART; state->Reg[14] = (tmp | 1); valid = t_branch; FLUSHPIPE; break; } } valid = t_undefined; break; case 30: /* BL instruction 1 */ /* Format 19 */ /* There is no single ARM instruction equivalent for this Thumb instruction. To keep the simulation simple (from the user perspective) we check if the following instruction is the second half of this BL, and if it is we simulate it immediately. */ state->Reg[14] = state->Reg[15] \ + (((tinstr & 0x07FF) << 12) \ | ((tinstr & (1 << 10)) ? 0xFF800000 : 0)); valid = t_branch; /* in-case we don't have the 2nd half */ tinstr = next_instr; /* move the instruction down */ pc += 2; /* point the pc at the 2nd half */ if (((tinstr & 0xF800) >> 11) != 31) { if (((tinstr & 0xF800) >> 11) == 29) { ARMword tmp = (pc + 2); state->Reg[15] = ((state->Reg[14] + ((tinstr & 0x07FE) << 1)) & 0xFFFFFFFC); CLEART; state->Reg[14] = (tmp | 1); valid = t_branch; FLUSHPIPE; } else /* Exit, since not correct instruction. */ pc -= 2; break; } /* else we fall through to process the second half of the BL */ pc += 2; /* point the pc at the 2nd half */ case 31: /* BL instruction 2 */ /* Format 19 */ /* There is no single ARM instruction equivalent for this instruction. Also, it should only ever be matched with the fmt19 "BL instruction 1" instruction. However, we do allow the simulation of it on its own, with undefined results if r14 is not suitably initialised. */ { ARMword tmp = pc; state->Reg[15] = (state->Reg[14] + ((tinstr & 0x07FF) << 1)); state->Reg[14] = (tmp | 1); valid = t_branch; FLUSHPIPE; } break; } return valid;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -