armstate.h 6.4 KB

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