armstate.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* armdefs.h -- ARMulator common definitions: ARM6 Instruction Emulator.
  2. Copyright (C) 1994 Advanced RISC Machines Ltd.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #pragma once
  15. #include <array>
  16. #include <unordered_map>
  17. #include "common/common_types.h"
  18. #include "core/arm/skyeye_common/arm_regformat.h"
  19. // Signal levels
  20. enum { LOW = 0, HIGH = 1, LOWHIGH = 1, HIGHLOW = 2 };
  21. // Cache types
  22. enum {
  23. NONCACHE = 0,
  24. DATACACHE = 1,
  25. INSTCACHE = 2,
  26. };
  27. // ARM privilege modes
  28. enum PrivilegeMode {
  29. USER32MODE = 16,
  30. FIQ32MODE = 17,
  31. IRQ32MODE = 18,
  32. SVC32MODE = 19,
  33. ABORT32MODE = 23,
  34. UNDEF32MODE = 27,
  35. SYSTEM32MODE = 31
  36. };
  37. // ARM privilege mode register banks
  38. enum {
  39. USERBANK = 0,
  40. FIQBANK = 1,
  41. IRQBANK = 2,
  42. SVCBANK = 3,
  43. ABORTBANK = 4,
  44. UNDEFBANK = 5,
  45. DUMMYBANK = 6,
  46. SYSTEMBANK = 7
  47. };
  48. // Hardware vector addresses
  49. enum {
  50. ARMResetV = 0,
  51. ARMUndefinedInstrV = 4,
  52. ARMSWIV = 8,
  53. ARMPrefetchAbortV = 12,
  54. ARMDataAbortV = 16,
  55. ARMAddrExceptnV = 20,
  56. ARMIRQV = 24,
  57. ARMFIQV = 28,
  58. ARMErrorV = 32, // This is an offset, not an address!
  59. ARMul_ResetV = ARMResetV,
  60. ARMul_UndefinedInstrV = ARMUndefinedInstrV,
  61. ARMul_SWIV = ARMSWIV,
  62. ARMul_PrefetchAbortV = ARMPrefetchAbortV,
  63. ARMul_DataAbortV = ARMDataAbortV,
  64. ARMul_AddrExceptnV = ARMAddrExceptnV,
  65. ARMul_IRQV = ARMIRQV,
  66. ARMul_FIQV = ARMFIQV
  67. };
  68. // Coprocessor status values
  69. enum {
  70. ARMul_FIRST = 0,
  71. ARMul_TRANSFER = 1,
  72. ARMul_BUSY = 2,
  73. ARMul_DATA = 3,
  74. ARMul_INTERRUPT = 4,
  75. ARMul_DONE = 0,
  76. ARMul_CANT = 1,
  77. ARMul_INC = 3
  78. };
  79. // Instruction condition codes
  80. enum ConditionCode {
  81. EQ = 0,
  82. NE = 1,
  83. CS = 2,
  84. CC = 3,
  85. MI = 4,
  86. PL = 5,
  87. VS = 6,
  88. VC = 7,
  89. HI = 8,
  90. LS = 9,
  91. GE = 10,
  92. LT = 11,
  93. GT = 12,
  94. LE = 13,
  95. AL = 14,
  96. NV = 15,
  97. };
  98. // Flags for use with the APSR.
  99. enum : u32 {
  100. NBIT = (1U << 31U),
  101. ZBIT = (1 << 30),
  102. CBIT = (1 << 29),
  103. VBIT = (1 << 28),
  104. QBIT = (1 << 27),
  105. JBIT = (1 << 24),
  106. EBIT = (1 << 9),
  107. ABIT = (1 << 8),
  108. IBIT = (1 << 7),
  109. FBIT = (1 << 6),
  110. TBIT = (1 << 5),
  111. // Masks for groups of bits in the APSR.
  112. MODEBITS = 0x1F,
  113. INTBITS = 0x1C0,
  114. };
  115. // Values for Emulate.
  116. enum {
  117. STOP = 0, // Stop
  118. CHANGEMODE = 1, // Change mode
  119. ONCE = 2, // Execute just one iteration
  120. RUN = 3 // Continuous execution
  121. };
  122. struct ARMul_State final {
  123. public:
  124. explicit ARMul_State(PrivilegeMode initial_mode);
  125. void ChangePrivilegeMode(u32 new_mode);
  126. void Reset();
  127. // Reads/writes data in big/little endian format based on the
  128. // state of the E (endian) bit in the APSR.
  129. u8 ReadMemory8(u32 address) const;
  130. u16 ReadMemory16(u32 address) const;
  131. u32 ReadMemory32(u32 address) const;
  132. u64 ReadMemory64(u32 address) const;
  133. void WriteMemory8(u32 address, u8 data);
  134. void WriteMemory16(u32 address, u16 data);
  135. void WriteMemory32(u32 address, u32 data);
  136. void WriteMemory64(u32 address, u64 data);
  137. u32 ReadCP15Register(u32 crn, u32 opcode_1, u32 crm, u32 opcode_2) const;
  138. void WriteCP15Register(u32 value, u32 crn, u32 opcode_1, u32 crm, u32 opcode_2);
  139. // Exclusive memory access functions
  140. bool IsExclusiveMemoryAccess(u32 address) const {
  141. return exclusive_state && exclusive_tag == (address & RESERVATION_GRANULE_MASK);
  142. }
  143. void SetExclusiveMemoryAddress(u32 address) {
  144. exclusive_tag = address & RESERVATION_GRANULE_MASK;
  145. exclusive_state = true;
  146. }
  147. void UnsetExclusiveMemoryAddress() {
  148. exclusive_tag = 0xFFFFFFFF;
  149. exclusive_state = false;
  150. }
  151. // Whether or not the given CPU is in big endian mode (E bit is set)
  152. bool InBigEndianMode() const {
  153. return (Cpsr & (1 << 9)) != 0;
  154. }
  155. // Whether or not the given CPU is in a mode other than user mode.
  156. bool InAPrivilegedMode() const {
  157. return (Mode != USER32MODE);
  158. }
  159. // Note that for the 3DS, a Thumb instruction will only ever be
  160. // two bytes in size. Thus we don't need to worry about ThumbEE
  161. // or Thumb-2 where instructions can be 4 bytes in length.
  162. u32 GetInstructionSize() const {
  163. return TFlag ? 2 : 4;
  164. }
  165. std::array<u32, 16> Reg{}; // The current register file
  166. std::array<u32, 2> Reg_usr{};
  167. std::array<u32, 2> Reg_svc{}; // R13_SVC R14_SVC
  168. std::array<u32, 2> Reg_abort{}; // R13_ABORT R14_ABORT
  169. std::array<u32, 2> Reg_undef{}; // R13 UNDEF R14 UNDEF
  170. std::array<u32, 2> Reg_irq{}; // R13_IRQ R14_IRQ
  171. std::array<u32, 7> Reg_firq{}; // R8---R14 FIRQ
  172. std::array<u32, 7> Spsr{}; // The exception psr's
  173. std::array<u32, CP15_REGISTER_COUNT> CP15{};
  174. // FPSID, FPSCR, and FPEXC
  175. std::array<u32, VFP_SYSTEM_REGISTER_COUNT> VFP{};
  176. // VFPv2 and VFPv3-D16 has 16 doubleword registers (D0-D16 or S0-S31).
  177. // VFPv3-D32/ASIMD may have up to 32 doubleword registers (D0-D31),
  178. // and only 32 singleword registers are accessible (S0-S31).
  179. std::array<u32, 64> ExtReg{};
  180. u32 Emulate; // To start and stop emulation
  181. u32 Cpsr; // The current PSR
  182. u32 Spsr_copy;
  183. u32 phys_pc;
  184. u32 Mode; // The current mode
  185. u32 Bank; // The current register bank
  186. u32 NFlag, ZFlag, CFlag, VFlag, IFFlags; // Dummy flags for speed
  187. unsigned int shifter_carry_out;
  188. u32 TFlag; // Thumb state
  189. unsigned long long NumInstrs; // The number of instructions executed
  190. unsigned NumInstrsToExecute;
  191. unsigned NresetSig; // Reset the processor
  192. unsigned NfiqSig;
  193. unsigned NirqSig;
  194. unsigned abortSig;
  195. unsigned NtransSig;
  196. unsigned bigendSig;
  197. unsigned syscallSig;
  198. // TODO(bunnei): Move this cache to a better place - it should be per codeset (likely per
  199. // process for our purposes), not per ARMul_State (which tracks CPU core state).
  200. std::unordered_map<u32, int> instruction_cache;
  201. private:
  202. void ResetMPCoreCP15Registers();
  203. // Defines a reservation granule of 2 words, which protects the first 2 words starting at the
  204. // tag. This is the smallest granule allowed by the v7 spec, and is coincidentally just large
  205. // enough to support LDR/STREXD.
  206. static const u32 RESERVATION_GRANULE_MASK = 0xFFFFFFF8;
  207. u32 exclusive_tag; // The address for which the local monitor is in exclusive access mode
  208. bool exclusive_state;
  209. };