Pseudocode.cs 37 KB

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