Pseudocode.cs 35 KB

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