Pseudocode.cs 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. // https://github.com/LDj3SNuD/ARM_v8-A_AArch64_Instructions_Tester/blob/master/Tester/Pseudocode.cs
  2. // https://meriac.github.io/archex/A64_v83A_ISA/shared_pseudocode.xml
  3. // https://alastairreid.github.io/asl-lexical-syntax/
  4. // | ------------------------|----------------------------------- |
  5. // | ASL | C# |
  6. // | ------------------------|----------------------------------- |
  7. // | bit, bits(1); boolean | bool |
  8. // | bits | Bits |
  9. // | integer | BigInteger, int |
  10. // | real | decimal |
  11. // | ------------------------|----------------------------------- |
  12. // | '0'; FALSE | false |
  13. // | '1'; TRUE | true |
  14. // | '010' | "010" |
  15. // | bitsX IN {bitsY, bitsZ} | (bitsX == bitsY || bitsX == bitsZ) |
  16. // | DIV | / |
  17. // | MOD | % |
  18. // | ------------------------|----------------------------------- |
  19. using System;
  20. using System.Numerics;
  21. namespace Ryujinx.Tests.Cpu.Tester
  22. {
  23. using Types;
  24. using static Shared;
  25. internal static class AArch64
  26. {
  27. #region "exceptions/exceptions/"
  28. /* #AArch64.ResetControlRegisters.1 */
  29. public static void ResetControlRegisters(bool cold_reset)
  30. {
  31. PSTATE.N = cold_reset;
  32. PSTATE.Z = cold_reset;
  33. PSTATE.C = cold_reset;
  34. PSTATE.V = cold_reset;
  35. }
  36. /* */
  37. public static void TakeReset(bool cold_reset)
  38. {
  39. /* assert !HighestELUsingAArch32(); */
  40. // Enter the highest implemented Exception level in AArch64 state
  41. if (HaveEL(EL3))
  42. {
  43. PSTATE.EL = EL3;
  44. }
  45. else if (HaveEL(EL2))
  46. {
  47. PSTATE.EL = EL2;
  48. }
  49. else
  50. {
  51. PSTATE.EL = EL1;
  52. }
  53. // Reset the system registers and other system components
  54. AArch64.ResetControlRegisters(cold_reset);
  55. // Reset all other PSTATE fields
  56. PSTATE.SP = true; // Select stack pointer
  57. // All registers, bits and fields not reset by the above pseudocode or by the BranchTo() call
  58. // below are UNKNOWN bitstrings after reset. In particular, the return information registers
  59. // ELR_ELx and SPSR_ELx have UNKNOWN values, so that it
  60. // is impossible to return from a reset in an architecturally defined way.
  61. AArch64.ResetGeneralRegisters();
  62. AArch64.ResetSIMDFPRegisters();
  63. AArch64.ResetSpecialRegisters();
  64. }
  65. #endregion
  66. #region "functions/registers/"
  67. /* #AArch64.ResetGeneralRegisters.0 */
  68. public static void ResetGeneralRegisters()
  69. {
  70. for (int i = 0; i <= 30; i++)
  71. {
  72. /* X[i] = bits(64) UNKNOWN; */
  73. _R[i].SetAll(false);
  74. }
  75. }
  76. /* #AArch64.ResetSIMDFPRegisters.0 */
  77. public static void ResetSIMDFPRegisters()
  78. {
  79. for (int i = 0; i <= 31; i++)
  80. {
  81. /* V[i] = bits(128) UNKNOWN; */
  82. _V[i].SetAll(false);
  83. }
  84. }
  85. /* #AArch64.ResetSpecialRegisters.0 */
  86. public static void ResetSpecialRegisters()
  87. {
  88. // AArch64 special registers
  89. /* SP_EL0 = bits(64) UNKNOWN; */
  90. SP_EL0.SetAll(false);
  91. /* SP_EL1 = bits(64) UNKNOWN; */
  92. SP_EL1.SetAll(false);
  93. }
  94. // #impl-aarch64.SP.write.0
  95. public static void SP(Bits value)
  96. {
  97. /* int width = value.Count; */
  98. /* assert width IN {32,64}; */
  99. if (!PSTATE.SP)
  100. {
  101. SP_EL0 = ZeroExtend(64, value);
  102. }
  103. else
  104. {
  105. switch (PSTATE.EL)
  106. {
  107. case Bits bits when bits == EL0:
  108. SP_EL0 = ZeroExtend(64, value);
  109. break;
  110. default:
  111. case Bits bits when bits == EL1:
  112. SP_EL1 = ZeroExtend(64, value);
  113. break;/*
  114. case Bits bits when bits == EL2:
  115. SP_EL2 = ZeroExtend(64, value);
  116. break;
  117. case Bits bits when bits == EL3:
  118. SP_EL3 = ZeroExtend(64, value);
  119. break;*/
  120. }
  121. }
  122. }
  123. // #impl-aarch64.SP.read.0
  124. public static Bits SP(int width)
  125. {
  126. /* assert width IN {8,16,32,64}; */
  127. if (!PSTATE.SP)
  128. {
  129. return SP_EL0[width - 1, 0];
  130. }
  131. else
  132. {
  133. switch (PSTATE.EL)
  134. {
  135. case Bits bits when bits == EL0:
  136. return SP_EL0[width - 1, 0];
  137. default:
  138. case Bits bits when bits == EL1:
  139. return SP_EL1[width - 1, 0];/*
  140. case Bits bits when bits == EL2:
  141. return SP_EL2[width - 1, 0];
  142. case Bits bits when bits == EL3:
  143. return SP_EL3[width - 1, 0];*/
  144. }
  145. }
  146. }
  147. // #impl-aarch64.V.write.1
  148. public static void V(int n, Bits value)
  149. {
  150. /* int width = value.Count; */
  151. /* assert n >= 0 && n <= 31; */
  152. /* assert width IN {8,16,32,64,128}; */
  153. _V[n] = ZeroExtend(128, value);
  154. }
  155. /* #impl-aarch64.V.read.1 */
  156. public static Bits V(int width, int n)
  157. {
  158. /* assert n >= 0 && n <= 31; */
  159. /* assert width IN {8,16,32,64,128}; */
  160. return _V[n][width - 1, 0];
  161. }
  162. /* #impl-aarch64.Vpart.read.2 */
  163. public static Bits Vpart(int width, int n, int part)
  164. {
  165. /* assert n >= 0 && n <= 31; */
  166. /* assert part IN {0, 1}; */
  167. if (part == 0)
  168. {
  169. /* assert width IN {8,16,32,64}; */
  170. return _V[n][width - 1, 0];
  171. }
  172. else
  173. {
  174. /* assert width == 64; */
  175. return _V[n][(width * 2) - 1, width];
  176. }
  177. }
  178. // #impl-aarch64.Vpart.write.2
  179. public static void Vpart(int n, int part, Bits value)
  180. {
  181. int width = value.Count;
  182. /* assert n >= 0 && n <= 31; */
  183. /* assert part IN {0, 1}; */
  184. if (part == 0)
  185. {
  186. /* assert width IN {8,16,32,64}; */
  187. _V[n] = ZeroExtend(128, value);
  188. }
  189. else
  190. {
  191. /* assert width == 64; */
  192. _V[n][(width * 2) - 1, width] = value[width - 1, 0];
  193. }
  194. }
  195. // #impl-aarch64.X.write.1
  196. public static void X(int n, Bits value)
  197. {
  198. /* int width = value.Count; */
  199. /* assert n >= 0 && n <= 31; */
  200. /* assert width IN {32,64}; */
  201. if (n != 31)
  202. {
  203. _R[n] = ZeroExtend(64, value);
  204. }
  205. }
  206. /* #impl-aarch64.X.read.1 */
  207. public static Bits X(int width, int n)
  208. {
  209. /* assert n >= 0 && n <= 31; */
  210. /* assert width IN {8,16,32,64}; */
  211. if (n != 31)
  212. {
  213. return _R[n][width - 1, 0];
  214. }
  215. else
  216. {
  217. return Zeros(width);
  218. }
  219. }
  220. #endregion
  221. #region "instrs/extendreg/"
  222. /* #impl-aarch64.DecodeRegExtend.1 */
  223. public static ExtendType DecodeRegExtend(Bits op)
  224. {
  225. switch (op)
  226. {
  227. default:
  228. case Bits bits when bits == "000":
  229. return ExtendType.ExtendType_UXTB;
  230. case Bits bits when bits == "001":
  231. return ExtendType.ExtendType_UXTH;
  232. case Bits bits when bits == "010":
  233. return ExtendType.ExtendType_UXTW;
  234. case Bits bits when bits == "011":
  235. return ExtendType.ExtendType_UXTX;
  236. case Bits bits when bits == "100":
  237. return ExtendType.ExtendType_SXTB;
  238. case Bits bits when bits == "101":
  239. return ExtendType.ExtendType_SXTH;
  240. case Bits bits when bits == "110":
  241. return ExtendType.ExtendType_SXTW;
  242. case Bits bits when bits == "111":
  243. return ExtendType.ExtendType_SXTX;
  244. }
  245. }
  246. /* #impl-aarch64.ExtendReg.3 */
  247. public static Bits ExtendReg(int N, int reg, ExtendType type, int shift)
  248. {
  249. /* assert shift >= 0 && shift <= 4; */
  250. Bits val = X(N, reg);
  251. bool unsigned;
  252. int len;
  253. switch (type)
  254. {
  255. default:
  256. case ExtendType.ExtendType_SXTB:
  257. unsigned = false; len = 8;
  258. break;
  259. case ExtendType.ExtendType_SXTH:
  260. unsigned = false; len = 16;
  261. break;
  262. case ExtendType.ExtendType_SXTW:
  263. unsigned = false; len = 32;
  264. break;
  265. case ExtendType.ExtendType_SXTX:
  266. unsigned = false; len = 64;
  267. break;
  268. case ExtendType.ExtendType_UXTB:
  269. unsigned = true; len = 8;
  270. break;
  271. case ExtendType.ExtendType_UXTH:
  272. unsigned = true; len = 16;
  273. break;
  274. case ExtendType.ExtendType_UXTW:
  275. unsigned = true; len = 32;
  276. break;
  277. case ExtendType.ExtendType_UXTX:
  278. unsigned = true; len = 64;
  279. break;
  280. }
  281. // Note the extended width of the intermediate value and
  282. // that sign extension occurs from bit <len+shift-1>, not
  283. // from bit <len-1>. This is equivalent to the instruction
  284. // [SU]BFIZ Rtmp, Rreg, #shift, #len
  285. // It may also be seen as a sign/zero extend followed by a shift:
  286. // LSL(Extend(val<len-1:0>, N, unsigned), shift);
  287. len = Min(len, N - shift);
  288. return Extend(Bits.Concat(val[len - 1, 0], Zeros(shift)), N, unsigned);
  289. }
  290. // #ExtendType
  291. public enum ExtendType {ExtendType_SXTB, ExtendType_SXTH, ExtendType_SXTW, ExtendType_SXTX,
  292. ExtendType_UXTB, ExtendType_UXTH, ExtendType_UXTW, ExtendType_UXTX};
  293. #endregion
  294. #region "instrs/integer/bitmasks/"
  295. /* #impl-aarch64.DecodeBitMasks.4 */
  296. public static (Bits, Bits) DecodeBitMasks(int M, bool immN, Bits imms, Bits immr, bool immediate)
  297. {
  298. Bits tmask, wmask;
  299. Bits tmask_and, wmask_and;
  300. Bits tmask_or, wmask_or;
  301. Bits levels;
  302. // Compute log2 of element size
  303. // 2^len must be in range [2, M]
  304. int len = HighestSetBit(Bits.Concat(immN, NOT(imms)));
  305. /* if len < 1 then ReservedValue(); */
  306. /* assert M >= (1 << len); */
  307. // Determine S, R and S - R parameters
  308. levels = ZeroExtend(Ones(len), 6);
  309. // For logical immediates an all-ones value of S is reserved
  310. // since it would generate a useless all-ones result (many times)
  311. /* if immediate && (imms AND levels) == levels then ReservedValue(); */
  312. BigInteger S = UInt(AND(imms, levels));
  313. BigInteger R = UInt(AND(immr, levels));
  314. BigInteger diff = S - R; // 6-bit subtract with borrow
  315. // Compute "top mask"
  316. tmask_and = OR(diff.SubBigInteger(5, 0), NOT(levels));
  317. tmask_or = AND(diff.SubBigInteger(5, 0), levels);
  318. tmask = Ones(64);
  319. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[0], 1), Ones( 1)), 32)), Replicate(Bits.Concat(Zeros( 1), Replicate(tmask_or[0], 1)), 32));
  320. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[1], 2), Ones( 2)), 16)), Replicate(Bits.Concat(Zeros( 2), Replicate(tmask_or[1], 2)), 16));
  321. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[2], 4), Ones( 4)), 8)), Replicate(Bits.Concat(Zeros( 4), Replicate(tmask_or[2], 4)), 8));
  322. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[3], 8), Ones( 8)), 4)), Replicate(Bits.Concat(Zeros( 8), Replicate(tmask_or[3], 8)), 4));
  323. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[4], 16), Ones(16)), 2)), Replicate(Bits.Concat(Zeros(16), Replicate(tmask_or[4], 16)), 2));
  324. tmask = OR(AND(tmask, Replicate(Bits.Concat(Replicate(tmask_and[5], 32), Ones(32)), 1)), Replicate(Bits.Concat(Zeros(32), Replicate(tmask_or[5], 32)), 1));
  325. // Compute "wraparound mask"
  326. wmask_and = OR(immr, NOT(levels));
  327. wmask_or = AND(immr, levels);
  328. wmask = Zeros(64);
  329. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones( 1), Replicate(wmask_and[0], 1)), 32)), Replicate(Bits.Concat(Replicate(wmask_or[0], 1), Zeros( 1)), 32));
  330. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones( 2), Replicate(wmask_and[1], 2)), 16)), Replicate(Bits.Concat(Replicate(wmask_or[1], 2), Zeros( 2)), 16));
  331. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones( 4), Replicate(wmask_and[2], 4)), 8)), Replicate(Bits.Concat(Replicate(wmask_or[2], 4), Zeros( 4)), 8));
  332. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones( 8), Replicate(wmask_and[3], 8)), 4)), Replicate(Bits.Concat(Replicate(wmask_or[3], 8), Zeros( 8)), 4));
  333. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones(16), Replicate(wmask_and[4], 16)), 2)), Replicate(Bits.Concat(Replicate(wmask_or[4], 16), Zeros(16)), 2));
  334. wmask = OR(AND(wmask, Replicate(Bits.Concat(Ones(32), Replicate(wmask_and[5], 32)), 1)), Replicate(Bits.Concat(Replicate(wmask_or[5], 32), Zeros(32)), 1));
  335. if (diff.SubBigInteger(6)) // borrow from S - R
  336. {
  337. wmask = AND(wmask, tmask);
  338. }
  339. else
  340. {
  341. wmask = OR(wmask, tmask);
  342. }
  343. return (wmask[M - 1, 0], tmask[M - 1, 0]);
  344. }
  345. #endregion
  346. #region "instrs/integer/shiftreg/"
  347. /* #impl-aarch64.DecodeShift.1 */
  348. public static ShiftType DecodeShift(Bits op)
  349. {
  350. switch (op)
  351. {
  352. default:
  353. case Bits bits when bits == "00":
  354. return ShiftType.ShiftType_LSL;
  355. case Bits bits when bits == "01":
  356. return ShiftType.ShiftType_LSR;
  357. case Bits bits when bits == "10":
  358. return ShiftType.ShiftType_ASR;
  359. case Bits bits when bits == "11":
  360. return ShiftType.ShiftType_ROR;
  361. }
  362. }
  363. /* #impl-aarch64.ShiftReg.3 */
  364. public static Bits ShiftReg(int N, int reg, ShiftType type, int amount)
  365. {
  366. Bits result = X(N, reg);
  367. switch (type)
  368. {
  369. default:
  370. case ShiftType.ShiftType_LSL:
  371. result = LSL(result, amount);
  372. break;
  373. case ShiftType.ShiftType_LSR:
  374. result = LSR(result, amount);
  375. break;
  376. case ShiftType.ShiftType_ASR:
  377. result = ASR(result, amount);
  378. break;
  379. case ShiftType.ShiftType_ROR:
  380. result = ROR(result, amount);
  381. break;
  382. }
  383. return result;
  384. }
  385. // #ShiftType
  386. public enum ShiftType {ShiftType_LSL, ShiftType_LSR, ShiftType_ASR, ShiftType_ROR};
  387. #endregion
  388. #region "instrs/vector/reduce/reduceop/"
  389. public static Bits Reduce(ReduceOp op, Bits input, int esize)
  390. {
  391. int N = input.Count;
  392. int half;
  393. Bits hi;
  394. Bits lo;
  395. Bits result = new Bits(esize);
  396. if (N == esize)
  397. {
  398. return new Bits(input);
  399. }
  400. half = N / 2;
  401. hi = Reduce(op, input[N - 1, half], esize);
  402. lo = Reduce(op, input[half - 1, 0], esize);
  403. switch (op)
  404. {
  405. case ReduceOp.ReduceOp_FMINNUM:
  406. /* result = FPMinNum(lo, hi, FPCR); */
  407. break;
  408. case ReduceOp.ReduceOp_FMAXNUM:
  409. /* result = FPMaxNum(lo, hi, FPCR); */
  410. break;
  411. case ReduceOp.ReduceOp_FMIN:
  412. /* result = FPMin(lo, hi, FPCR); */
  413. break;
  414. case ReduceOp.ReduceOp_FMAX:
  415. /* result = FPMax(lo, hi, FPCR); */
  416. break;
  417. case ReduceOp.ReduceOp_FADD:
  418. /* result = FPAdd(lo, hi, FPCR); */
  419. break;
  420. default:
  421. case ReduceOp.ReduceOp_ADD:
  422. result = lo + hi;
  423. break;
  424. }
  425. return result;
  426. }
  427. public enum ReduceOp {ReduceOp_FMINNUM, ReduceOp_FMAXNUM,
  428. ReduceOp_FMIN, ReduceOp_FMAX,
  429. ReduceOp_FADD, ReduceOp_ADD};
  430. #endregion
  431. }
  432. internal static class Shared
  433. {
  434. static Shared()
  435. {
  436. _R = new Bits[31];
  437. for (int i = 0; i <= 30; i++)
  438. {
  439. _R[i] = new Bits(64, false);
  440. }
  441. _V = new Bits[32];
  442. for (int i = 0; i <= 31; i++)
  443. {
  444. _V[i] = new Bits(128, false);
  445. }
  446. SP_EL0 = new Bits(64, false);
  447. SP_EL1 = new Bits(64, false);
  448. PSTATE.N = false;
  449. PSTATE.Z = false;
  450. PSTATE.C = false;
  451. PSTATE.V = false;
  452. PSTATE.EL = EL1;
  453. PSTATE.SP = true;
  454. }
  455. #region "functions/common/"
  456. /* */
  457. public static Bits AND(Bits x, Bits y)
  458. {
  459. return x.And(y);
  460. }
  461. // #impl-shared.ASR.2
  462. public static Bits ASR(Bits x, int shift)
  463. {
  464. int N = x.Count;
  465. /* assert shift >= 0; */
  466. Bits result;
  467. if (shift == 0)
  468. {
  469. result = new Bits(x);
  470. }
  471. else
  472. {
  473. (result, _) = ASR_C(x, shift);
  474. }
  475. return result;
  476. }
  477. // #impl-shared.ASR_C.2
  478. public static (Bits, bool) ASR_C(Bits x, int shift)
  479. {
  480. int N = x.Count;
  481. /* assert shift > 0; */
  482. Bits extended_x = SignExtend(x, shift + N);
  483. Bits result = extended_x[shift + N - 1, shift];
  484. bool carry_out = extended_x[shift - 1];
  485. return (result, carry_out);
  486. }
  487. // #impl-shared.Abs.1
  488. public static BigInteger Abs(BigInteger x)
  489. {
  490. return (x >= 0 ? x : -x);
  491. }
  492. // #impl-shared.CountLeadingSignBits.1
  493. public static int CountLeadingSignBits(Bits x)
  494. {
  495. int N = x.Count;
  496. return CountLeadingZeroBits(EOR(x[N - 1, 1], x[N - 2, 0]));
  497. }
  498. // #impl-shared.CountLeadingZeroBits.1
  499. public static int CountLeadingZeroBits(Bits x)
  500. {
  501. int N = x.Count;
  502. return (N - 1 - HighestSetBit(x));
  503. }
  504. // #impl-shared.Elem.read.3
  505. public static Bits Elem(/*in */Bits vector, int e, int size)
  506. {
  507. /* int N = vector.Count; */
  508. /* assert e >= 0 && (e+1)*size <= N; */
  509. return vector[e * size + size - 1, e * size];
  510. }
  511. // #impl-shared.Elem.write.3
  512. public static void Elem(/*out */Bits vector, int e, int size, Bits value)
  513. {
  514. /* int N = vector.Count; */
  515. /* assert e >= 0 && (e+1)*size <= N; */
  516. vector[(e + 1) * size - 1, e * size] = value;
  517. }
  518. /* */
  519. public static Bits EOR(Bits x, Bits y)
  520. {
  521. return x.Xor(y);
  522. }
  523. // #impl-shared.Extend.3
  524. public static Bits Extend(Bits x, int N, bool unsigned)
  525. {
  526. if (unsigned)
  527. {
  528. return ZeroExtend(x, N);
  529. }
  530. else
  531. {
  532. return SignExtend(x, N);
  533. }
  534. }
  535. /* #impl-shared.Extend.2 */
  536. public static Bits Extend(int N, Bits x, bool unsigned)
  537. {
  538. return Extend(x, N, unsigned);
  539. }
  540. // #impl-shared.HighestSetBit.1
  541. public static int HighestSetBit(Bits x)
  542. {
  543. int N = x.Count;
  544. for (int i = N - 1; i >= 0; i--)
  545. {
  546. if (x[i])
  547. {
  548. return i;
  549. }
  550. }
  551. return -1;
  552. }
  553. // #impl-shared.Int.2
  554. public static BigInteger Int(Bits x, bool unsigned)
  555. {
  556. return (unsigned ? UInt(x) : SInt(x));
  557. }
  558. // #impl-shared.IsOnes.1
  559. public static bool IsOnes(Bits x)
  560. {
  561. int N = x.Count;
  562. return (x == Ones(N));
  563. }
  564. // #impl-shared.IsZero.1
  565. public static bool IsZero(Bits x)
  566. {
  567. int N = x.Count;
  568. return (x == Zeros(N));
  569. }
  570. // #impl-shared.IsZeroBit.1
  571. public static bool IsZeroBit(Bits x)
  572. {
  573. return IsZero(x);
  574. }
  575. // #impl-shared.LSL.2
  576. public static Bits LSL(Bits x, int shift)
  577. {
  578. int N = x.Count;
  579. /* assert shift >= 0; */
  580. Bits result;
  581. if (shift == 0)
  582. {
  583. result = new Bits(x);
  584. }
  585. else
  586. {
  587. (result, _) = LSL_C(x, shift);
  588. }
  589. return result;
  590. }
  591. // #impl-shared.LSL_C.2
  592. public static (Bits, bool) LSL_C(Bits x, int shift)
  593. {
  594. int N = x.Count;
  595. /* assert shift > 0; */
  596. Bits extended_x = Bits.Concat(x, Zeros(shift));
  597. Bits result = extended_x[N - 1, 0];
  598. bool carry_out = extended_x[N];
  599. return (result, carry_out);
  600. }
  601. // #impl-shared.LSR.2
  602. public static Bits LSR(Bits x, int shift)
  603. {
  604. int N = x.Count;
  605. /* assert shift >= 0; */
  606. Bits result;
  607. if (shift == 0)
  608. {
  609. result = new Bits(x);
  610. }
  611. else
  612. {
  613. (result, _) = LSR_C(x, shift);
  614. }
  615. return result;
  616. }
  617. // #impl-shared.LSR_C.2
  618. public static (Bits, bool) LSR_C(Bits x, int shift)
  619. {
  620. int N = x.Count;
  621. /* assert shift > 0; */
  622. Bits extended_x = ZeroExtend(x, shift + N);
  623. Bits result = extended_x[shift + N - 1, shift];
  624. bool carry_out = extended_x[shift - 1];
  625. return (result, carry_out);
  626. }
  627. // #impl-shared.Min.2
  628. public static int Min(int a, int b)
  629. {
  630. if (a <= b)
  631. {
  632. return a;
  633. }
  634. else
  635. {
  636. return b;
  637. }
  638. }
  639. /* #impl-shared.NOT.1 */
  640. public static Bits NOT(Bits x)
  641. {
  642. return x.Not();
  643. }
  644. // #impl-shared.Ones.1
  645. public static Bits Ones(int N)
  646. {
  647. return Replicate(true, N);
  648. }
  649. /* */
  650. public static Bits OR(Bits x, Bits y)
  651. {
  652. return x.Or(y);
  653. }
  654. /* */
  655. public static decimal Real(BigInteger value)
  656. {
  657. return (decimal)value;
  658. }
  659. // #impl-shared.ROR.2
  660. public static Bits ROR(Bits x, int shift)
  661. {
  662. /* assert shift >= 0; */
  663. Bits result;
  664. if (shift == 0)
  665. {
  666. result = new Bits(x);
  667. }
  668. else
  669. {
  670. (result, _) = ROR_C(x, shift);
  671. }
  672. return result;
  673. }
  674. // #impl-shared.ROR_C.2
  675. public static (Bits, bool) ROR_C(Bits x, int shift)
  676. {
  677. int N = x.Count;
  678. /* assert shift != 0; */
  679. int m = shift % N;
  680. Bits result = OR(LSR(x, m), LSL(x, N - m));
  681. bool carry_out = result[N - 1];
  682. return (result, carry_out);
  683. }
  684. /* #impl-shared.Replicate.1 */
  685. public static Bits Replicate(int N, Bits x)
  686. {
  687. int M = x.Count;
  688. /* assert N MOD M == 0; */
  689. return Replicate(x, N / M);
  690. }
  691. /* #impl-shared.Replicate.2 */
  692. public static Bits Replicate(Bits x, int N)
  693. {
  694. int M = x.Count;
  695. bool[] dst = new bool[M * N];
  696. for (int i = 0; i < N; i++)
  697. {
  698. x.CopyTo(dst, i * M);
  699. }
  700. return new Bits(dst);
  701. }
  702. /* #impl-shared.RoundDown.1 */
  703. public static BigInteger RoundDown(decimal x)
  704. {
  705. return (BigInteger)Decimal.Floor(x);
  706. }
  707. // #impl-shared.RoundTowardsZero.1
  708. public static BigInteger RoundTowardsZero(decimal x)
  709. {
  710. if (x == 0.0m)
  711. {
  712. return (BigInteger)0m;
  713. }
  714. else if (x >= 0.0m)
  715. {
  716. return RoundDown(x);
  717. }
  718. else
  719. {
  720. return RoundUp(x);
  721. }
  722. }
  723. /* #impl-shared.RoundUp.1 */
  724. public static BigInteger RoundUp(decimal x)
  725. {
  726. return (BigInteger)Decimal.Ceiling(x);
  727. }
  728. // #impl-shared.SInt.1
  729. public static BigInteger SInt(Bits x)
  730. {
  731. int N = x.Count;
  732. BigInteger result = 0;
  733. for (int i = 0; i <= N - 1; i++)
  734. {
  735. if (x[i])
  736. {
  737. result = result + BigInteger.Pow(2, i);
  738. }
  739. }
  740. if (x[N - 1])
  741. {
  742. result = result - BigInteger.Pow(2, N);
  743. }
  744. return result;
  745. }
  746. // #impl-shared.SignExtend.2
  747. public static Bits SignExtend(Bits x, int N)
  748. {
  749. int M = x.Count;
  750. /* assert N >= M; */
  751. return Bits.Concat(Replicate(x[M - 1], N - M), x);
  752. }
  753. /* #impl-shared.SignExtend.1 */
  754. public static Bits SignExtend(int N, Bits x)
  755. {
  756. return SignExtend(x, N);
  757. }
  758. // #impl-shared.UInt.1
  759. public static BigInteger UInt(Bits x)
  760. {
  761. int N = x.Count;
  762. BigInteger result = 0;
  763. for (int i = 0; i <= N - 1; i++)
  764. {
  765. if (x[i])
  766. {
  767. result = result + BigInteger.Pow(2, i);
  768. }
  769. }
  770. return result;
  771. }
  772. // #impl-shared.ZeroExtend.2
  773. public static Bits ZeroExtend(Bits x, int N)
  774. {
  775. int M = x.Count;
  776. /* assert N >= M; */
  777. return Bits.Concat(Zeros(N - M), x);
  778. }
  779. /* #impl-shared.ZeroExtend.1 */
  780. public static Bits ZeroExtend(int N, Bits x)
  781. {
  782. return ZeroExtend(x, N);
  783. }
  784. // #impl-shared.Zeros.1
  785. /* #impl-shared.Zeros.0 */
  786. public static Bits Zeros(int N)
  787. {
  788. return Replicate(false, N);
  789. }
  790. #endregion
  791. #region "functions/crc/"
  792. // #impl-shared.BitReverse.1
  793. public static Bits BitReverse(Bits data)
  794. {
  795. int N = data.Count;
  796. Bits result = new Bits(N);
  797. for (int i = 0; i <= N - 1; i++)
  798. {
  799. result[N - i - 1] = data[i];
  800. }
  801. return result;
  802. }
  803. // #impl-shared.Poly32Mod2.2
  804. public static Bits Poly32Mod2(Bits _data, Bits poly)
  805. {
  806. int N = _data.Count;
  807. /* assert N > 32; */
  808. Bits data = new Bits(_data);
  809. for (int i = N - 1; i >= 32; i--)
  810. {
  811. if (data[i])
  812. {
  813. data[i - 1, 0] = EOR(data[i - 1, 0], Bits.Concat(poly, Zeros(i - 32)));
  814. }
  815. }
  816. return data[31, 0];
  817. }
  818. #endregion
  819. #region "functions/integer/"
  820. /* #impl-shared.AddWithCarry.3 */
  821. public static (Bits, Bits) AddWithCarry(int N, Bits x, Bits y, bool carry_in)
  822. {
  823. BigInteger unsigned_sum = UInt(x) + UInt(y) + UInt(carry_in);
  824. BigInteger signed_sum = SInt(x) + SInt(y) + UInt(carry_in);
  825. Bits result = unsigned_sum.SubBigInteger(N - 1, 0); // same value as signed_sum<N-1:0>
  826. bool n = result[N - 1];
  827. bool z = IsZero(result);
  828. bool c = !(UInt(result) == unsigned_sum);
  829. bool v = !(SInt(result) == signed_sum);
  830. return (result, Bits.Concat(n, z, c, v));
  831. }
  832. #endregion
  833. #region "functions/registers/"
  834. public static readonly Bits[] _R;
  835. public static readonly Bits[] _V;
  836. public static Bits SP_EL0;
  837. public static Bits SP_EL1;
  838. #endregion
  839. #region "functions/system/"
  840. // #impl-shared.ConditionHolds.1
  841. public static bool ConditionHolds(Bits cond)
  842. {
  843. bool result;
  844. // Evaluate base condition.
  845. switch (cond[3, 1])
  846. {
  847. case Bits bits when bits == "000":
  848. result = (PSTATE.Z == true); // EQ or NE
  849. break;
  850. case Bits bits when bits == "001":
  851. result = (PSTATE.C == true); // CS or CC
  852. break;
  853. case Bits bits when bits == "010":
  854. result = (PSTATE.N == true); // MI or PL
  855. break;
  856. case Bits bits when bits == "011":
  857. result = (PSTATE.V == true); // VS or VC
  858. break;
  859. case Bits bits when bits == "100":
  860. result = (PSTATE.C == true && PSTATE.Z == false); // HI or LS
  861. break;
  862. case Bits bits when bits == "101":
  863. result = (PSTATE.N == PSTATE.V); // GE or LT
  864. break;
  865. case Bits bits when bits == "110":
  866. result = (PSTATE.N == PSTATE.V && PSTATE.Z == false); // GT or LE
  867. break;
  868. default:
  869. case Bits bits when bits == "111":
  870. result = true; // AL
  871. break;
  872. }
  873. // Condition flag values in the set '111x' indicate always true
  874. // Otherwise, invert condition if necessary.
  875. if (cond[0] == true && cond != "1111")
  876. {
  877. result = !result;
  878. }
  879. return result;
  880. }
  881. // #EL3
  882. public static readonly Bits EL3 = "11";
  883. // #EL2
  884. public static readonly Bits EL2 = "10";
  885. // #EL1
  886. public static readonly Bits EL1 = "01";
  887. // #EL0
  888. public static readonly Bits EL0 = "00";
  889. /* #impl-shared.HaveEL.1 */
  890. public static bool HaveEL(Bits el)
  891. {
  892. if (el == EL1 || el == EL0)
  893. {
  894. return true; // EL1 and EL0 must exist
  895. }
  896. return false;
  897. }
  898. public static ProcState PSTATE;
  899. /* #ProcState */
  900. internal struct ProcState
  901. {
  902. public void NZCV(Bits nzcv) // ASL: ".<,,,>".
  903. {
  904. N = nzcv[3];
  905. Z = nzcv[2];
  906. C = nzcv[1];
  907. V = nzcv[0];
  908. }
  909. public void NZCV(bool n, bool z, bool c, bool v) // ASL: ".<,,,>".
  910. {
  911. N = n;
  912. Z = z;
  913. C = c;
  914. V = v;
  915. }
  916. public bool N; // Negative condition flag
  917. public bool Z; // Zero condition flag
  918. public bool C; // Carry condition flag
  919. public bool V; // oVerflow condition flag
  920. public Bits EL; // Exception Level
  921. public bool SP; // Stack pointer select: 0=SP0, 1=SPx [AArch64 only]
  922. }
  923. #endregion
  924. }
  925. }