Pseudocode.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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.CountLeadingSignBits.1
  505. public static int CountLeadingSignBits(Bits x)
  506. {
  507. int N = x.Count;
  508. return CountLeadingZeroBits(EOR(x[N - 1, 1], x[N - 2, 0]));
  509. }
  510. // shared_pseudocode.html#impl-shared.CountLeadingZeroBits.1
  511. public static int CountLeadingZeroBits(Bits x)
  512. {
  513. int N = x.Count;
  514. return (N - 1 - HighestSetBit(x));
  515. }
  516. // shared_pseudocode.html#impl-shared.Elem.read.3
  517. public static Bits Elem(/*in */Bits vector, int e, int size)
  518. {
  519. /* int N = vector.Count; */
  520. /* assert e >= 0 && (e+1)*size <= N; */
  521. return vector[e * size + size - 1, e * size];
  522. }
  523. // shared_pseudocode.html#impl-shared.Elem.write.3
  524. public static void Elem(/*out */Bits vector, int e, int size, Bits value)
  525. {
  526. /* int N = vector.Count; */
  527. /* assert e >= 0 && (e+1)*size <= N; */
  528. vector[(e + 1) * size - 1, e * size] = value;
  529. }
  530. /* */
  531. public static Bits EOR(Bits x, Bits y)
  532. {
  533. return x.Xor(y);
  534. }
  535. // shared_pseudocode.html#impl-shared.Extend.3
  536. public static Bits Extend(Bits x, int N, bool unsigned)
  537. {
  538. if (unsigned)
  539. {
  540. return ZeroExtend(x, N);
  541. }
  542. else
  543. {
  544. return SignExtend(x, N);
  545. }
  546. }
  547. /* shared_pseudocode.html#impl-shared.Extend.2 */
  548. public static Bits Extend(int N, Bits x, bool unsigned)
  549. {
  550. return Extend(x, N, unsigned);
  551. }
  552. // shared_pseudocode.html#impl-shared.HighestSetBit.1
  553. public static int HighestSetBit(Bits x)
  554. {
  555. int N = x.Count;
  556. for (int i = N - 1; i >= 0; i--)
  557. {
  558. if (x[i])
  559. {
  560. return i;
  561. }
  562. }
  563. return -1;
  564. }
  565. // shared_pseudocode.html#impl-shared.Int.2
  566. public static BigInteger Int(Bits x, bool unsigned)
  567. {
  568. return (unsigned ? UInt(x) : SInt(x));
  569. }
  570. // shared_pseudocode.html#impl-shared.IsOnes.1
  571. public static bool IsOnes(Bits x)
  572. {
  573. int N = x.Count;
  574. return (x == Ones(N));
  575. }
  576. // shared_pseudocode.html#impl-shared.IsZero.1
  577. public static bool IsZero(Bits x)
  578. {
  579. int N = x.Count;
  580. return (x == Zeros(N));
  581. }
  582. // shared_pseudocode.html#impl-shared.IsZeroBit.1
  583. public static bool IsZeroBit(Bits x)
  584. {
  585. return IsZero(x);
  586. }
  587. // shared_pseudocode.html#impl-shared.LSL.2
  588. public static Bits LSL(Bits x, int shift)
  589. {
  590. int N = x.Count;
  591. /* assert shift >= 0; */
  592. Bits result;
  593. if (shift == 0)
  594. {
  595. result = new Bits(x);
  596. }
  597. else
  598. {
  599. (result, _) = LSL_C(x, shift);
  600. }
  601. return result;
  602. }
  603. // shared_pseudocode.html#impl-shared.LSL_C.2
  604. public static (Bits, bool) LSL_C(Bits x, int shift)
  605. {
  606. int N = x.Count;
  607. /* assert shift > 0; */
  608. Bits extended_x = Bits.Concat(x, Zeros(shift));
  609. Bits result = extended_x[N - 1, 0];
  610. bool carry_out = extended_x[N];
  611. return (result, carry_out);
  612. }
  613. // shared_pseudocode.html#impl-shared.LSR.2
  614. public static Bits LSR(Bits x, int shift)
  615. {
  616. int N = x.Count;
  617. /* assert shift >= 0; */
  618. Bits result;
  619. if (shift == 0)
  620. {
  621. result = new Bits(x);
  622. }
  623. else
  624. {
  625. (result, _) = LSR_C(x, shift);
  626. }
  627. return result;
  628. }
  629. // shared_pseudocode.html#impl-shared.LSR_C.2
  630. public static (Bits, bool) LSR_C(Bits x, int shift)
  631. {
  632. int N = x.Count;
  633. /* assert shift > 0; */
  634. Bits extended_x = ZeroExtend(x, shift + N);
  635. Bits result = extended_x[shift + N - 1, shift];
  636. bool carry_out = extended_x[shift - 1];
  637. return (result, carry_out);
  638. }
  639. // shared_pseudocode.html#impl-shared.Min.2
  640. public static int Min(int a, int b)
  641. {
  642. if (a <= b)
  643. {
  644. return a;
  645. }
  646. else
  647. {
  648. return b;
  649. }
  650. }
  651. /* shared_pseudocode.html#impl-shared.NOT.1 */
  652. public static Bits NOT(Bits x)
  653. {
  654. return x.Not();
  655. }
  656. // shared_pseudocode.html#impl-shared.Ones.1
  657. /* shared_pseudocode.html#impl-shared.Ones.0 */
  658. public static Bits Ones(int N)
  659. {
  660. return Replicate(true, N);
  661. }
  662. /* */
  663. public static Bits OR(Bits x, Bits y)
  664. {
  665. return x.Or(y);
  666. }
  667. /* */
  668. public static decimal Real(BigInteger value)
  669. {
  670. return (decimal)value;
  671. }
  672. // shared_pseudocode.html#impl-shared.ROR.2
  673. public static Bits ROR(Bits x, int shift)
  674. {
  675. /* assert shift >= 0; */
  676. Bits result;
  677. if (shift == 0)
  678. {
  679. result = new Bits(x);
  680. }
  681. else
  682. {
  683. (result, _) = ROR_C(x, shift);
  684. }
  685. return result;
  686. }
  687. // shared_pseudocode.html#impl-shared.ROR_C.2
  688. public static (Bits, bool) ROR_C(Bits x, int shift)
  689. {
  690. int N = x.Count;
  691. /* assert shift != 0; */
  692. int m = shift % N;
  693. Bits result = OR(LSR(x, m), LSL(x, N - m));
  694. bool carry_out = result[N - 1];
  695. return (result, carry_out);
  696. }
  697. /* shared_pseudocode.html#impl-shared.Replicate.1 */
  698. public static Bits Replicate(int N, Bits x)
  699. {
  700. int M = x.Count;
  701. /* assert N MOD M == 0; */
  702. return Replicate(x, N / M);
  703. }
  704. /* shared_pseudocode.html#impl-shared.Replicate.2 */
  705. public static Bits Replicate(Bits x, int N)
  706. {
  707. int M = x.Count;
  708. bool[] dst = new bool[M * N];
  709. for (int i = 0; i < N; i++)
  710. {
  711. x.CopyTo(dst, i * M);
  712. }
  713. return new Bits(dst);
  714. }
  715. /* shared_pseudocode.html#impl-shared.RoundDown.1 */
  716. public static BigInteger RoundDown(decimal x)
  717. {
  718. return (BigInteger)Decimal.Floor(x);
  719. }
  720. // shared_pseudocode.html#impl-shared.RoundTowardsZero.1
  721. public static BigInteger RoundTowardsZero(decimal x)
  722. {
  723. if (x == 0.0m)
  724. {
  725. return (BigInteger)0m;
  726. }
  727. else if (x >= 0.0m)
  728. {
  729. return RoundDown(x);
  730. }
  731. else
  732. {
  733. return RoundUp(x);
  734. }
  735. }
  736. /* shared_pseudocode.html#impl-shared.RoundUp.1 */
  737. public static BigInteger RoundUp(decimal x)
  738. {
  739. return (BigInteger)Decimal.Ceiling(x);
  740. }
  741. // shared_pseudocode.html#impl-shared.SInt.1
  742. public static BigInteger SInt(Bits x)
  743. {
  744. int N = x.Count;
  745. BigInteger result = 0;
  746. for (int i = 0; i <= N - 1; i++)
  747. {
  748. if (x[i])
  749. {
  750. result = result + BigInteger.Pow(2, i);
  751. }
  752. }
  753. if (x[N - 1])
  754. {
  755. result = result - BigInteger.Pow(2, N);
  756. }
  757. return result;
  758. }
  759. // shared_pseudocode.html#impl-shared.SignExtend.2
  760. public static Bits SignExtend(Bits x, int N)
  761. {
  762. int M = x.Count;
  763. /* assert N >= M; */
  764. return Bits.Concat(Replicate(x[M - 1], N - M), x);
  765. }
  766. /* shared_pseudocode.html#impl-shared.SignExtend.1 */
  767. public static Bits SignExtend(int N, Bits x)
  768. {
  769. return SignExtend(x, N);
  770. }
  771. // shared_pseudocode.html#impl-shared.UInt.1
  772. public static BigInteger UInt(Bits x)
  773. {
  774. int N = x.Count;
  775. BigInteger result = 0;
  776. for (int i = 0; i <= N - 1; i++)
  777. {
  778. if (x[i])
  779. {
  780. result = result + BigInteger.Pow(2, i);
  781. }
  782. }
  783. return result;
  784. }
  785. // shared_pseudocode.html#impl-shared.ZeroExtend.2
  786. public static Bits ZeroExtend(Bits x, int N)
  787. {
  788. int M = x.Count;
  789. /* assert N >= M; */
  790. return Bits.Concat(Zeros(N - M), x);
  791. }
  792. /* shared_pseudocode.html#impl-shared.ZeroExtend.1 */
  793. public static Bits ZeroExtend(int N, Bits x)
  794. {
  795. return ZeroExtend(x, N);
  796. }
  797. // shared_pseudocode.html#impl-shared.Zeros.1
  798. /* shared_pseudocode.html#impl-shared.Zeros.0 */
  799. public static Bits Zeros(int N)
  800. {
  801. return Replicate(false, N);
  802. }
  803. #endregion
  804. #region "functions/crc/"
  805. // shared_pseudocode.html#impl-shared.BitReverse.1
  806. public static Bits BitReverse(Bits data)
  807. {
  808. int N = data.Count;
  809. Bits result = new Bits(N);
  810. for (int i = 0; i <= N - 1; i++)
  811. {
  812. result[N - i - 1] = data[i];
  813. }
  814. return result;
  815. }
  816. // shared_pseudocode.html#impl-shared.Poly32Mod2.2
  817. public static Bits Poly32Mod2(Bits _data, Bits poly)
  818. {
  819. int N = _data.Count;
  820. /* assert N > 32; */
  821. Bits data = new Bits(_data);
  822. for (int i = N - 1; i >= 32; i--)
  823. {
  824. if (data[i])
  825. {
  826. data[i - 1, 0] = EOR(data[i - 1, 0], Bits.Concat(poly, Zeros(i - 32)));
  827. }
  828. }
  829. return data[31, 0];
  830. }
  831. #endregion
  832. #region "functions/integer/"
  833. /* shared_pseudocode.html#impl-shared.AddWithCarry.3 */
  834. public static (Bits, Bits) AddWithCarry(int N, Bits x, Bits y, bool carry_in)
  835. {
  836. BigInteger unsigned_sum = UInt(x) + UInt(y) + UInt(carry_in);
  837. BigInteger signed_sum = SInt(x) + SInt(y) + UInt(carry_in);
  838. Bits result = unsigned_sum.SubBigInteger(N - 1, 0); // same value as signed_sum<N-1:0>
  839. bool n = result[N - 1];
  840. bool z = IsZero(result);
  841. bool c = !(UInt(result) == unsigned_sum);
  842. bool v = !(SInt(result) == signed_sum);
  843. return (result, Bits.Concat(n, z, c, v));
  844. }
  845. #endregion
  846. #region "functions/registers/"
  847. public static readonly Bits[] _R;
  848. public static readonly Bits[] _V;
  849. public static Bits SP_EL0;
  850. public static Bits SP_EL1;
  851. public static Bits FPSR; // TODO: Add named fields.
  852. #endregion
  853. #region "functions/system/"
  854. // shared_pseudocode.html#impl-shared.ConditionHolds.1
  855. public static bool ConditionHolds(Bits cond)
  856. {
  857. bool result;
  858. // Evaluate base condition.
  859. switch (cond[3, 1])
  860. {
  861. case Bits bits when bits == "000":
  862. result = (PSTATE.Z == true); // EQ or NE
  863. break;
  864. case Bits bits when bits == "001":
  865. result = (PSTATE.C == true); // CS or CC
  866. break;
  867. case Bits bits when bits == "010":
  868. result = (PSTATE.N == true); // MI or PL
  869. break;
  870. case Bits bits when bits == "011":
  871. result = (PSTATE.V == true); // VS or VC
  872. break;
  873. case Bits bits when bits == "100":
  874. result = (PSTATE.C == true && PSTATE.Z == false); // HI or LS
  875. break;
  876. case Bits bits when bits == "101":
  877. result = (PSTATE.N == PSTATE.V); // GE or LT
  878. break;
  879. case Bits bits when bits == "110":
  880. result = (PSTATE.N == PSTATE.V && PSTATE.Z == false); // GT or LE
  881. break;
  882. default:
  883. case Bits bits when bits == "111":
  884. result = true; // AL
  885. break;
  886. }
  887. // Condition flag values in the set '111x' indicate always true
  888. // Otherwise, invert condition if necessary.
  889. if (cond[0] == true && cond != "1111")
  890. {
  891. result = !result;
  892. }
  893. return result;
  894. }
  895. // shared_pseudocode.html#EL3
  896. public static readonly Bits EL3 = "11";
  897. // shared_pseudocode.html#EL2
  898. public static readonly Bits EL2 = "10";
  899. // shared_pseudocode.html#EL1
  900. public static readonly Bits EL1 = "01";
  901. // shared_pseudocode.html#EL0
  902. public static readonly Bits EL0 = "00";
  903. /* shared_pseudocode.html#impl-shared.HaveEL.1 */
  904. public static bool HaveEL(Bits el)
  905. {
  906. if (el == EL1 || el == EL0)
  907. {
  908. return true; // EL1 and EL0 must exist
  909. }
  910. /* return boolean IMPLEMENTATION_DEFINED; */
  911. return false;
  912. }
  913. public static ProcState PSTATE;
  914. /* shared_pseudocode.html#ProcState */
  915. internal struct ProcState
  916. {
  917. public void NZCV(Bits nzcv) // ASL: ".<,,,>".
  918. {
  919. N = nzcv[3];
  920. Z = nzcv[2];
  921. C = nzcv[1];
  922. V = nzcv[0];
  923. }
  924. public void NZCV(bool n, bool z, bool c, bool v) // ASL: ".<,,,>".
  925. {
  926. N = n;
  927. Z = z;
  928. C = c;
  929. V = v;
  930. }
  931. public bool N; // Negative condition flag
  932. public bool Z; // Zero condition flag
  933. public bool C; // Carry condition flag
  934. public bool V; // oVerflow condition flag
  935. public Bits EL; // Exception Level
  936. public bool SP; // Stack pointer select: 0=SP0, 1=SPx [AArch64 only]
  937. }
  938. #endregion
  939. #region "functions/vector/"
  940. // shared_pseudocode.html#impl-shared.SatQ.3
  941. public static (Bits, bool) SatQ(BigInteger i, int N, bool unsigned)
  942. {
  943. (Bits result, bool sat) = (unsigned ? UnsignedSatQ(i, N) : SignedSatQ(i, N));
  944. return (result, sat);
  945. }
  946. // shared_pseudocode.html#impl-shared.SignedSatQ.2
  947. public static (Bits, bool) SignedSatQ(BigInteger i, int N)
  948. {
  949. BigInteger result;
  950. bool saturated;
  951. if (i > BigInteger.Pow(2, N - 1) - 1)
  952. {
  953. result = BigInteger.Pow(2, N - 1) - 1;
  954. saturated = true;
  955. }
  956. else if (i < -(BigInteger.Pow(2, N - 1)))
  957. {
  958. result = -(BigInteger.Pow(2, N - 1));
  959. saturated = true;
  960. }
  961. else
  962. {
  963. result = i;
  964. saturated = false;
  965. }
  966. return (result.SubBigInteger(N - 1, 0), saturated);
  967. }
  968. // shared_pseudocode.html#impl-shared.UnsignedSatQ.2
  969. public static (Bits, bool) UnsignedSatQ(BigInteger i, int N)
  970. {
  971. BigInteger result;
  972. bool saturated;
  973. if (i > BigInteger.Pow(2, N) - 1)
  974. {
  975. result = BigInteger.Pow(2, N) - 1;
  976. saturated = true;
  977. }
  978. else if (i < 0)
  979. {
  980. result = 0;
  981. saturated = true;
  982. }
  983. else
  984. {
  985. result = i;
  986. saturated = false;
  987. }
  988. return (result.SubBigInteger(N - 1, 0), saturated);
  989. }
  990. #endregion
  991. }
  992. }