Pseudocode.cs 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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/countop/"
  222. // #CountOp
  223. public enum CountOp {CountOp_CLZ, CountOp_CLS, CountOp_CNT};
  224. #endregion
  225. #region "instrs/extendreg/"
  226. /* #impl-aarch64.DecodeRegExtend.1 */
  227. public static ExtendType DecodeRegExtend(Bits op)
  228. {
  229. switch (op)
  230. {
  231. default:
  232. case Bits bits when bits == "000":
  233. return ExtendType.ExtendType_UXTB;
  234. case Bits bits when bits == "001":
  235. return ExtendType.ExtendType_UXTH;
  236. case Bits bits when bits == "010":
  237. return ExtendType.ExtendType_UXTW;
  238. case Bits bits when bits == "011":
  239. return ExtendType.ExtendType_UXTX;
  240. case Bits bits when bits == "100":
  241. return ExtendType.ExtendType_SXTB;
  242. case Bits bits when bits == "101":
  243. return ExtendType.ExtendType_SXTH;
  244. case Bits bits when bits == "110":
  245. return ExtendType.ExtendType_SXTW;
  246. case Bits bits when bits == "111":
  247. return ExtendType.ExtendType_SXTX;
  248. }
  249. }
  250. /* #impl-aarch64.ExtendReg.3 */
  251. public static Bits ExtendReg(int N, int reg, ExtendType type, int shift)
  252. {
  253. /* assert shift >= 0 && shift <= 4; */
  254. Bits val = X(N, reg);
  255. bool unsigned;
  256. int len;
  257. switch (type)
  258. {
  259. default:
  260. case ExtendType.ExtendType_SXTB:
  261. unsigned = false; len = 8;
  262. break;
  263. case ExtendType.ExtendType_SXTH:
  264. unsigned = false; len = 16;
  265. break;
  266. case ExtendType.ExtendType_SXTW:
  267. unsigned = false; len = 32;
  268. break;
  269. case ExtendType.ExtendType_SXTX:
  270. unsigned = false; len = 64;
  271. break;
  272. case ExtendType.ExtendType_UXTB:
  273. unsigned = true; len = 8;
  274. break;
  275. case ExtendType.ExtendType_UXTH:
  276. unsigned = true; len = 16;
  277. break;
  278. case ExtendType.ExtendType_UXTW:
  279. unsigned = true; len = 32;
  280. break;
  281. case ExtendType.ExtendType_UXTX:
  282. unsigned = true; len = 64;
  283. break;
  284. }
  285. // Note the extended width of the intermediate value and
  286. // that sign extension occurs from bit <len+shift-1>, not
  287. // from bit <len-1>. This is equivalent to the instruction
  288. // [SU]BFIZ Rtmp, Rreg, #shift, #len
  289. // It may also be seen as a sign/zero extend followed by a shift:
  290. // LSL(Extend(val<len-1:0>, N, unsigned), shift);
  291. len = Min(len, N - shift);
  292. return Extend(Bits.Concat(val[len - 1, 0], Zeros(shift)), N, unsigned);
  293. }
  294. // #ExtendType
  295. public enum ExtendType {ExtendType_SXTB, ExtendType_SXTH, ExtendType_SXTW, ExtendType_SXTX,
  296. ExtendType_UXTB, ExtendType_UXTH, ExtendType_UXTW, ExtendType_UXTX};
  297. #endregion
  298. #region "instrs/integer/bitmasks/"
  299. /* #impl-aarch64.DecodeBitMasks.4 */
  300. public static (Bits, Bits) DecodeBitMasks(int M, bool immN, Bits imms, Bits immr, bool immediate)
  301. {
  302. Bits tmask, wmask;
  303. Bits tmask_and, wmask_and;
  304. Bits tmask_or, wmask_or;
  305. Bits levels;
  306. // Compute log2 of element size
  307. // 2^len must be in range [2, M]
  308. int len = HighestSetBit(Bits.Concat(immN, NOT(imms)));
  309. /* if len < 1 then ReservedValue(); */
  310. /* assert M >= (1 << len); */
  311. // Determine S, R and S - R parameters
  312. levels = ZeroExtend(Ones(len), 6);
  313. // For logical immediates an all-ones value of S is reserved
  314. // since it would generate a useless all-ones result (many times)
  315. /* if immediate && (imms AND levels) == levels then ReservedValue(); */
  316. BigInteger S = UInt(AND(imms, levels));
  317. BigInteger R = UInt(AND(immr, levels));
  318. BigInteger diff = S - R; // 6-bit subtract with borrow
  319. // Compute "top mask"
  320. tmask_and = OR(diff.SubBigInteger(5, 0), NOT(levels));
  321. tmask_or = AND(diff.SubBigInteger(5, 0), levels);
  322. tmask = Ones(64);
  323. 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));
  324. 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));
  325. 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));
  326. 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));
  327. 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));
  328. 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));
  329. // Compute "wraparound mask"
  330. wmask_and = OR(immr, NOT(levels));
  331. wmask_or = AND(immr, levels);
  332. wmask = Zeros(64);
  333. 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));
  334. 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));
  335. 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));
  336. 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));
  337. 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));
  338. 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));
  339. if (diff.SubBigInteger(6)) // borrow from S - R
  340. {
  341. wmask = AND(wmask, tmask);
  342. }
  343. else
  344. {
  345. wmask = OR(wmask, tmask);
  346. }
  347. return (wmask[M - 1, 0], tmask[M - 1, 0]);
  348. }
  349. #endregion
  350. #region "instrs/integer/shiftreg/"
  351. /* #impl-aarch64.DecodeShift.1 */
  352. public static ShiftType DecodeShift(Bits op)
  353. {
  354. switch (op)
  355. {
  356. default:
  357. case Bits bits when bits == "00":
  358. return ShiftType.ShiftType_LSL;
  359. case Bits bits when bits == "01":
  360. return ShiftType.ShiftType_LSR;
  361. case Bits bits when bits == "10":
  362. return ShiftType.ShiftType_ASR;
  363. case Bits bits when bits == "11":
  364. return ShiftType.ShiftType_ROR;
  365. }
  366. }
  367. /* #impl-aarch64.ShiftReg.3 */
  368. public static Bits ShiftReg(int N, int reg, ShiftType type, int amount)
  369. {
  370. Bits result = X(N, reg);
  371. switch (type)
  372. {
  373. default:
  374. case ShiftType.ShiftType_LSL:
  375. result = LSL(result, amount);
  376. break;
  377. case ShiftType.ShiftType_LSR:
  378. result = LSR(result, amount);
  379. break;
  380. case ShiftType.ShiftType_ASR:
  381. result = ASR(result, amount);
  382. break;
  383. case ShiftType.ShiftType_ROR:
  384. result = ROR(result, amount);
  385. break;
  386. }
  387. return result;
  388. }
  389. // #ShiftType
  390. public enum ShiftType {ShiftType_LSL, ShiftType_LSR, ShiftType_ASR, ShiftType_ROR};
  391. #endregion
  392. #region "instrs/vector/reduce/reduceop/"
  393. public static Bits Reduce(ReduceOp op, Bits input, int esize)
  394. {
  395. int N = input.Count;
  396. int half;
  397. Bits hi;
  398. Bits lo;
  399. Bits result = new Bits(esize);
  400. if (N == esize)
  401. {
  402. return new Bits(input);
  403. }
  404. half = N / 2;
  405. hi = Reduce(op, input[N - 1, half], esize);
  406. lo = Reduce(op, input[half - 1, 0], esize);
  407. switch (op)
  408. {
  409. case ReduceOp.ReduceOp_FMINNUM:
  410. /* result = FPMinNum(lo, hi, FPCR); */
  411. break;
  412. case ReduceOp.ReduceOp_FMAXNUM:
  413. /* result = FPMaxNum(lo, hi, FPCR); */
  414. break;
  415. case ReduceOp.ReduceOp_FMIN:
  416. /* result = FPMin(lo, hi, FPCR); */
  417. break;
  418. case ReduceOp.ReduceOp_FMAX:
  419. /* result = FPMax(lo, hi, FPCR); */
  420. break;
  421. case ReduceOp.ReduceOp_FADD:
  422. /* result = FPAdd(lo, hi, FPCR); */
  423. break;
  424. default:
  425. case ReduceOp.ReduceOp_ADD:
  426. result = lo + hi;
  427. break;
  428. }
  429. return result;
  430. }
  431. public enum ReduceOp {ReduceOp_FMINNUM, ReduceOp_FMAXNUM,
  432. ReduceOp_FMIN, ReduceOp_FMAX,
  433. ReduceOp_FADD, ReduceOp_ADD};
  434. #endregion
  435. }
  436. internal static class Shared
  437. {
  438. static Shared()
  439. {
  440. _R = new Bits[31];
  441. for (int i = 0; i <= 30; i++)
  442. {
  443. _R[i] = new Bits(64, false);
  444. }
  445. _V = new Bits[32];
  446. for (int i = 0; i <= 31; i++)
  447. {
  448. _V[i] = new Bits(128, false);
  449. }
  450. SP_EL0 = new Bits(64, false);
  451. SP_EL1 = new Bits(64, false);
  452. PSTATE.N = false;
  453. PSTATE.Z = false;
  454. PSTATE.C = false;
  455. PSTATE.V = false;
  456. PSTATE.EL = EL1;
  457. PSTATE.SP = true;
  458. }
  459. #region "functions/common/"
  460. /* */
  461. public static Bits AND(Bits x, Bits y)
  462. {
  463. return x.And(y);
  464. }
  465. // #impl-shared.ASR.2
  466. public static Bits ASR(Bits x, int shift)
  467. {
  468. int N = x.Count;
  469. /* assert shift >= 0; */
  470. Bits result;
  471. if (shift == 0)
  472. {
  473. result = new Bits(x);
  474. }
  475. else
  476. {
  477. (result, _) = ASR_C(x, shift);
  478. }
  479. return result;
  480. }
  481. // #impl-shared.ASR_C.2
  482. public static (Bits, bool) ASR_C(Bits x, int shift)
  483. {
  484. int N = x.Count;
  485. /* assert shift > 0; */
  486. Bits extended_x = SignExtend(x, shift + N);
  487. Bits result = extended_x[shift + N - 1, shift];
  488. bool carry_out = extended_x[shift - 1];
  489. return (result, carry_out);
  490. }
  491. // #impl-shared.Abs.1
  492. public static BigInteger Abs(BigInteger x)
  493. {
  494. return (x >= 0 ? x : -x);
  495. }
  496. // #impl-shared.CountLeadingSignBits.1
  497. public static int CountLeadingSignBits(Bits x)
  498. {
  499. int N = x.Count;
  500. return CountLeadingZeroBits(EOR(x[N - 1, 1], x[N - 2, 0]));
  501. }
  502. // #impl-shared.CountLeadingZeroBits.1
  503. public static int CountLeadingZeroBits(Bits x)
  504. {
  505. int N = x.Count;
  506. return (N - 1 - HighestSetBit(x));
  507. }
  508. // #impl-shared.Elem.read.3
  509. public static Bits Elem(/*in */Bits vector, int e, int size)
  510. {
  511. /* int N = vector.Count; */
  512. /* assert e >= 0 && (e+1)*size <= N; */
  513. return vector[e * size + size - 1, e * size];
  514. }
  515. // #impl-shared.Elem.write.3
  516. public static void Elem(/*out */Bits vector, int e, int size, Bits value)
  517. {
  518. /* int N = vector.Count; */
  519. /* assert e >= 0 && (e+1)*size <= N; */
  520. vector[(e + 1) * size - 1, e * size] = value;
  521. }
  522. /* */
  523. public static Bits EOR(Bits x, Bits y)
  524. {
  525. return x.Xor(y);
  526. }
  527. // #impl-shared.Extend.3
  528. public static Bits Extend(Bits x, int N, bool unsigned)
  529. {
  530. if (unsigned)
  531. {
  532. return ZeroExtend(x, N);
  533. }
  534. else
  535. {
  536. return SignExtend(x, N);
  537. }
  538. }
  539. /* #impl-shared.Extend.2 */
  540. public static Bits Extend(int N, Bits x, bool unsigned)
  541. {
  542. return Extend(x, N, unsigned);
  543. }
  544. // #impl-shared.HighestSetBit.1
  545. public static int HighestSetBit(Bits x)
  546. {
  547. int N = x.Count;
  548. for (int i = N - 1; i >= 0; i--)
  549. {
  550. if (x[i])
  551. {
  552. return i;
  553. }
  554. }
  555. return -1;
  556. }
  557. // #impl-shared.Int.2
  558. public static BigInteger Int(Bits x, bool unsigned)
  559. {
  560. return (unsigned ? UInt(x) : SInt(x));
  561. }
  562. // #impl-shared.IsOnes.1
  563. public static bool IsOnes(Bits x)
  564. {
  565. int N = x.Count;
  566. return (x == Ones(N));
  567. }
  568. // #impl-shared.IsZero.1
  569. public static bool IsZero(Bits x)
  570. {
  571. int N = x.Count;
  572. return (x == Zeros(N));
  573. }
  574. // #impl-shared.IsZeroBit.1
  575. public static bool IsZeroBit(Bits x)
  576. {
  577. return IsZero(x);
  578. }
  579. // #impl-shared.LSL.2
  580. public static Bits LSL(Bits x, int shift)
  581. {
  582. int N = x.Count;
  583. /* assert shift >= 0; */
  584. Bits result;
  585. if (shift == 0)
  586. {
  587. result = new Bits(x);
  588. }
  589. else
  590. {
  591. (result, _) = LSL_C(x, shift);
  592. }
  593. return result;
  594. }
  595. // #impl-shared.LSL_C.2
  596. public static (Bits, bool) LSL_C(Bits x, int shift)
  597. {
  598. int N = x.Count;
  599. /* assert shift > 0; */
  600. Bits extended_x = Bits.Concat(x, Zeros(shift));
  601. Bits result = extended_x[N - 1, 0];
  602. bool carry_out = extended_x[N];
  603. return (result, carry_out);
  604. }
  605. // #impl-shared.LSR.2
  606. public static Bits LSR(Bits x, int shift)
  607. {
  608. int N = x.Count;
  609. /* assert shift >= 0; */
  610. Bits result;
  611. if (shift == 0)
  612. {
  613. result = new Bits(x);
  614. }
  615. else
  616. {
  617. (result, _) = LSR_C(x, shift);
  618. }
  619. return result;
  620. }
  621. // #impl-shared.LSR_C.2
  622. public static (Bits, bool) LSR_C(Bits x, int shift)
  623. {
  624. int N = x.Count;
  625. /* assert shift > 0; */
  626. Bits extended_x = ZeroExtend(x, shift + N);
  627. Bits result = extended_x[shift + N - 1, shift];
  628. bool carry_out = extended_x[shift - 1];
  629. return (result, carry_out);
  630. }
  631. // #impl-shared.Min.2
  632. public static int Min(int a, int b)
  633. {
  634. if (a <= b)
  635. {
  636. return a;
  637. }
  638. else
  639. {
  640. return b;
  641. }
  642. }
  643. /* #impl-shared.NOT.1 */
  644. public static Bits NOT(Bits x)
  645. {
  646. return x.Not();
  647. }
  648. // #impl-shared.Ones.1
  649. public static Bits Ones(int N)
  650. {
  651. return Replicate(true, N);
  652. }
  653. /* */
  654. public static Bits OR(Bits x, Bits y)
  655. {
  656. return x.Or(y);
  657. }
  658. /* */
  659. public static decimal Real(BigInteger value)
  660. {
  661. return (decimal)value;
  662. }
  663. // #impl-shared.ROR.2
  664. public static Bits ROR(Bits x, int shift)
  665. {
  666. /* assert shift >= 0; */
  667. Bits result;
  668. if (shift == 0)
  669. {
  670. result = new Bits(x);
  671. }
  672. else
  673. {
  674. (result, _) = ROR_C(x, shift);
  675. }
  676. return result;
  677. }
  678. // #impl-shared.ROR_C.2
  679. public static (Bits, bool) ROR_C(Bits x, int shift)
  680. {
  681. int N = x.Count;
  682. /* assert shift != 0; */
  683. int m = shift % N;
  684. Bits result = OR(LSR(x, m), LSL(x, N - m));
  685. bool carry_out = result[N - 1];
  686. return (result, carry_out);
  687. }
  688. /* #impl-shared.Replicate.1 */
  689. public static Bits Replicate(int N, Bits x)
  690. {
  691. int M = x.Count;
  692. /* assert N MOD M == 0; */
  693. return Replicate(x, N / M);
  694. }
  695. /* #impl-shared.Replicate.2 */
  696. public static Bits Replicate(Bits x, int N)
  697. {
  698. int M = x.Count;
  699. bool[] dst = new bool[M * N];
  700. for (int i = 0; i < N; i++)
  701. {
  702. x.CopyTo(dst, i * M);
  703. }
  704. return new Bits(dst);
  705. }
  706. /* #impl-shared.RoundDown.1 */
  707. public static BigInteger RoundDown(decimal x)
  708. {
  709. return (BigInteger)Decimal.Floor(x);
  710. }
  711. // #impl-shared.RoundTowardsZero.1
  712. public static BigInteger RoundTowardsZero(decimal x)
  713. {
  714. if (x == 0.0m)
  715. {
  716. return (BigInteger)0m;
  717. }
  718. else if (x >= 0.0m)
  719. {
  720. return RoundDown(x);
  721. }
  722. else
  723. {
  724. return RoundUp(x);
  725. }
  726. }
  727. /* #impl-shared.RoundUp.1 */
  728. public static BigInteger RoundUp(decimal x)
  729. {
  730. return (BigInteger)Decimal.Ceiling(x);
  731. }
  732. // #impl-shared.SInt.1
  733. public static BigInteger SInt(Bits x)
  734. {
  735. int N = x.Count;
  736. BigInteger result = 0;
  737. for (int i = 0; i <= N - 1; i++)
  738. {
  739. if (x[i])
  740. {
  741. result = result + BigInteger.Pow(2, i);
  742. }
  743. }
  744. if (x[N - 1])
  745. {
  746. result = result - BigInteger.Pow(2, N);
  747. }
  748. return result;
  749. }
  750. // #impl-shared.SignExtend.2
  751. public static Bits SignExtend(Bits x, int N)
  752. {
  753. int M = x.Count;
  754. /* assert N >= M; */
  755. return Bits.Concat(Replicate(x[M - 1], N - M), x);
  756. }
  757. /* #impl-shared.SignExtend.1 */
  758. public static Bits SignExtend(int N, Bits x)
  759. {
  760. return SignExtend(x, N);
  761. }
  762. // #impl-shared.UInt.1
  763. public static BigInteger UInt(Bits x)
  764. {
  765. int N = x.Count;
  766. BigInteger result = 0;
  767. for (int i = 0; i <= N - 1; i++)
  768. {
  769. if (x[i])
  770. {
  771. result = result + BigInteger.Pow(2, i);
  772. }
  773. }
  774. return result;
  775. }
  776. // #impl-shared.ZeroExtend.2
  777. public static Bits ZeroExtend(Bits x, int N)
  778. {
  779. int M = x.Count;
  780. /* assert N >= M; */
  781. return Bits.Concat(Zeros(N - M), x);
  782. }
  783. /* #impl-shared.ZeroExtend.1 */
  784. public static Bits ZeroExtend(int N, Bits x)
  785. {
  786. return ZeroExtend(x, N);
  787. }
  788. // #impl-shared.Zeros.1
  789. /* #impl-shared.Zeros.0 */
  790. public static Bits Zeros(int N)
  791. {
  792. return Replicate(false, N);
  793. }
  794. #endregion
  795. #region "functions/crc/"
  796. // #impl-shared.BitReverse.1
  797. public static Bits BitReverse(Bits data)
  798. {
  799. int N = data.Count;
  800. Bits result = new Bits(N);
  801. for (int i = 0; i <= N - 1; i++)
  802. {
  803. result[N - i - 1] = data[i];
  804. }
  805. return result;
  806. }
  807. // #impl-shared.Poly32Mod2.2
  808. public static Bits Poly32Mod2(Bits _data, Bits poly)
  809. {
  810. int N = _data.Count;
  811. /* assert N > 32; */
  812. Bits data = new Bits(_data);
  813. for (int i = N - 1; i >= 32; i--)
  814. {
  815. if (data[i])
  816. {
  817. data[i - 1, 0] = EOR(data[i - 1, 0], Bits.Concat(poly, Zeros(i - 32)));
  818. }
  819. }
  820. return data[31, 0];
  821. }
  822. #endregion
  823. #region "functions/integer/"
  824. /* #impl-shared.AddWithCarry.3 */
  825. public static (Bits, Bits) AddWithCarry(int N, Bits x, Bits y, bool carry_in)
  826. {
  827. BigInteger unsigned_sum = UInt(x) + UInt(y) + UInt(carry_in);
  828. BigInteger signed_sum = SInt(x) + SInt(y) + UInt(carry_in);
  829. Bits result = unsigned_sum.SubBigInteger(N - 1, 0); // same value as signed_sum<N-1:0>
  830. bool n = result[N - 1];
  831. bool z = IsZero(result);
  832. bool c = !(UInt(result) == unsigned_sum);
  833. bool v = !(SInt(result) == signed_sum);
  834. return (result, Bits.Concat(n, z, c, v));
  835. }
  836. #endregion
  837. #region "functions/registers/"
  838. public static readonly Bits[] _R;
  839. public static readonly Bits[] _V;
  840. public static Bits SP_EL0;
  841. public static Bits SP_EL1;
  842. #endregion
  843. #region "functions/system/"
  844. // #impl-shared.ConditionHolds.1
  845. public static bool ConditionHolds(Bits cond)
  846. {
  847. bool result;
  848. // Evaluate base condition.
  849. switch (cond[3, 1])
  850. {
  851. case Bits bits when bits == "000":
  852. result = (PSTATE.Z == true); // EQ or NE
  853. break;
  854. case Bits bits when bits == "001":
  855. result = (PSTATE.C == true); // CS or CC
  856. break;
  857. case Bits bits when bits == "010":
  858. result = (PSTATE.N == true); // MI or PL
  859. break;
  860. case Bits bits when bits == "011":
  861. result = (PSTATE.V == true); // VS or VC
  862. break;
  863. case Bits bits when bits == "100":
  864. result = (PSTATE.C == true && PSTATE.Z == false); // HI or LS
  865. break;
  866. case Bits bits when bits == "101":
  867. result = (PSTATE.N == PSTATE.V); // GE or LT
  868. break;
  869. case Bits bits when bits == "110":
  870. result = (PSTATE.N == PSTATE.V && PSTATE.Z == false); // GT or LE
  871. break;
  872. default:
  873. case Bits bits when bits == "111":
  874. result = true; // AL
  875. break;
  876. }
  877. // Condition flag values in the set '111x' indicate always true
  878. // Otherwise, invert condition if necessary.
  879. if (cond[0] == true && cond != "1111")
  880. {
  881. result = !result;
  882. }
  883. return result;
  884. }
  885. // #EL3
  886. public static readonly Bits EL3 = "11";
  887. // #EL2
  888. public static readonly Bits EL2 = "10";
  889. // #EL1
  890. public static readonly Bits EL1 = "01";
  891. // #EL0
  892. public static readonly Bits EL0 = "00";
  893. /* #impl-shared.HaveEL.1 */
  894. public static bool HaveEL(Bits el)
  895. {
  896. if (el == EL1 || el == EL0)
  897. {
  898. return true; // EL1 and EL0 must exist
  899. }
  900. return false;
  901. }
  902. public static ProcState PSTATE;
  903. /* #ProcState */
  904. internal struct ProcState
  905. {
  906. public void NZCV(Bits nzcv) // ASL: ".<,,,>".
  907. {
  908. N = nzcv[3];
  909. Z = nzcv[2];
  910. C = nzcv[1];
  911. V = nzcv[0];
  912. }
  913. public void NZCV(bool n, bool z, bool c, bool v) // ASL: ".<,,,>".
  914. {
  915. N = n;
  916. Z = z;
  917. C = c;
  918. V = v;
  919. }
  920. public bool N; // Negative condition flag
  921. public bool Z; // Zero condition flag
  922. public bool C; // Carry condition flag
  923. public bool V; // oVerflow condition flag
  924. public Bits EL; // Exception Level
  925. public bool SP; // Stack pointer select: 0=SP0, 1=SPx [AArch64 only]
  926. }
  927. #endregion
  928. }
  929. }