arm_dyncom.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "core/arm/skyeye_common/armemu.h"
  5. #include "core/arm/skyeye_common/vfp/vfp.h"
  6. #include "core/arm/dyncom/arm_dyncom.h"
  7. #include "core/arm/dyncom/arm_dyncom_interpreter.h"
  8. #include "core/arm/dyncom/arm_dyncom_run.h"
  9. #include "core/core.h"
  10. #include "core/core_timing.h"
  11. const static cpu_config_t s_arm11_cpu_info = {
  12. "armv6", "arm11", 0x0007b000, 0x0007f000, NONCACHE
  13. };
  14. ARM_DynCom::ARM_DynCom(PrivilegeMode initial_mode) {
  15. state = std::unique_ptr<ARMul_State>(new ARMul_State);
  16. ARMul_NewState(state.get());
  17. ARMul_SelectProcessor(state.get(), ARM_v6_Prop | ARM_v5_Prop | ARM_v5e_Prop);
  18. state->abort_model = ABORT_BASE_RESTORED;
  19. state->cpu = (cpu_config_t*)&s_arm11_cpu_info;
  20. state->bigendSig = LOW;
  21. state->lateabtSig = LOW;
  22. state->NirqSig = HIGH;
  23. // Reset the core to initial state
  24. ARMul_Reset(state.get());
  25. state->NextInstr = RESUME; // NOTE: This will be overwritten by LoadContext
  26. state->Emulate = RUN;
  27. // Switch to the desired privilege mode.
  28. switch_mode(state.get(), initial_mode);
  29. state->Reg[13] = 0x10000000; // Set stack pointer to the top of the stack
  30. state->Reg[15] = 0x00000000;
  31. }
  32. ARM_DynCom::~ARM_DynCom() {
  33. }
  34. void ARM_DynCom::SetPC(u32 pc) {
  35. state->Reg[15] = pc;
  36. }
  37. u32 ARM_DynCom::GetPC() const {
  38. return state->Reg[15];
  39. }
  40. u32 ARM_DynCom::GetReg(int index) const {
  41. return state->Reg[index];
  42. }
  43. void ARM_DynCom::SetReg(int index, u32 value) {
  44. state->Reg[index] = value;
  45. }
  46. u32 ARM_DynCom::GetCPSR() const {
  47. return state->Cpsr;
  48. }
  49. void ARM_DynCom::SetCPSR(u32 cpsr) {
  50. state->Cpsr = cpsr;
  51. }
  52. u64 ARM_DynCom::GetTicks() const {
  53. // TODO(Subv): Remove ARM_DynCom::GetTicks() and use CoreTiming::GetTicks() directly once ARMemu is gone
  54. return CoreTiming::GetTicks();
  55. }
  56. void ARM_DynCom::AddTicks(u64 ticks) {
  57. down_count -= ticks;
  58. if (down_count < 0)
  59. CoreTiming::Advance();
  60. }
  61. void ARM_DynCom::ExecuteInstructions(int num_instructions) {
  62. state->NumInstrsToExecute = num_instructions;
  63. // Dyncom only breaks on instruction dispatch. This only happens on every instruction when
  64. // executing one instruction at a time. Otherwise, if a block is being executed, more
  65. // instructions may actually be executed than specified.
  66. unsigned ticks_executed = InterpreterMainLoop(state.get());
  67. AddTicks(ticks_executed);
  68. }
  69. void ARM_DynCom::ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg) {
  70. memset(&context, 0, sizeof(Core::ThreadContext));
  71. context.cpu_registers[0] = arg;
  72. context.pc = entry_point;
  73. context.sp = stack_top;
  74. context.cpsr = 0x1F; // Usermode
  75. context.mode = 8; // Instructs dyncom CPU core to start execution as if it's "resuming" a thread.
  76. }
  77. void ARM_DynCom::SaveContext(Core::ThreadContext& ctx) {
  78. memcpy(ctx.cpu_registers, state->Reg, sizeof(ctx.cpu_registers));
  79. memcpy(ctx.fpu_registers, state->ExtReg, sizeof(ctx.fpu_registers));
  80. ctx.sp = state->Reg[13];
  81. ctx.lr = state->Reg[14];
  82. ctx.pc = state->Reg[15];
  83. ctx.cpsr = state->Cpsr;
  84. ctx.fpscr = state->VFP[1];
  85. ctx.fpexc = state->VFP[2];
  86. ctx.mode = state->NextInstr;
  87. }
  88. void ARM_DynCom::LoadContext(const Core::ThreadContext& ctx) {
  89. memcpy(state->Reg, ctx.cpu_registers, sizeof(ctx.cpu_registers));
  90. memcpy(state->ExtReg, ctx.fpu_registers, sizeof(ctx.fpu_registers));
  91. state->Reg[13] = ctx.sp;
  92. state->Reg[14] = ctx.lr;
  93. state->Reg[15] = ctx.pc;
  94. state->Cpsr = ctx.cpsr;
  95. state->VFP[1] = ctx.fpscr;
  96. state->VFP[2] = ctx.fpexc;
  97. state->NextInstr = ctx.mode;
  98. }
  99. void ARM_DynCom::PrepareReschedule() {
  100. state->NumInstrsToExecute = 0;
  101. }