Maybe needs review: fixed bug in ManualSwitch concerning the input
[Mograsim.git] / net.mograsim.logic.model.am2900 / test / net / mograsim / logic / model / am2900 / TestUtil.java
1 package net.mograsim.logic.model.am2900;
2
3 import static org.junit.jupiter.api.Assertions.fail;
4
5 import java.lang.reflect.Field;
6 import java.util.Objects;
7
8 import net.mograsim.logic.core.types.Bit;
9 import net.mograsim.logic.core.types.BitVector;
10 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
11 import net.mograsim.logic.model.am2900.am2901.TestableAm2901Impl;
12
13 public final class TestUtil
14 {
15         private TestUtil()
16         {
17
18         }
19
20         /**
21          * Transforms the last four bits of an int to a string that contains the binary ('1' and '0') representation of the 4 bits
22          * 
23          * @author Christian Femers
24          */
25         public static String to4bitBin(int x)
26         {
27                 StringBuilder sb = new StringBuilder(4);
28                 sb.append((x & 0b1000) == 0 ? '0' : '1');
29                 sb.append((x & 0b0100) == 0 ? '0' : '1');
30                 sb.append((x & 0b0010) == 0 ? '0' : '1');
31                 sb.append((x & 0b0001) == 0 ? '0' : '1');
32                 return sb.toString();
33         }
34
35         /**
36          * Transforms the given boolean to a string that contains the binary ('1' and '0') representation of the bit
37          * 
38          * @author Christian Femers
39          */
40         public static String to1bitBin(boolean bitIsSet)
41         {
42                 return bitIsSet ? "1" : "0";
43         }
44
45         /**
46          * Transforms the given int to a string that contains the binary ('1' and '0') representation of the int. "0" is only returned when the
47          * int is equal to zero.
48          * 
49          * @author Christian Femers
50          */
51         public static String to1bitBin(int someInt)
52         {
53                 return someInt != 0 ? "1" : "0";
54         }
55
56         /**
57          * Transforms a 4 bit signed integer (-8 to 7) to a int representing the same number. (Adding leading 1-bits if the 4 bit int is
58          * negative)
59          * 
60          * @author Christian Femers
61          */
62         public static int signed4ToSigned32(int signed4bit)
63         {
64                 if ((signed4bit & 0b1000) > 0)
65                         return signed4bit | 0xFF_FF_FF_F0;
66                 return signed4bit & 0x00_00_00_0F;
67         }
68
69         /**
70          * Transforms a 16 bit signed integer (-32768 to 32767 - a short) to a int representing the same number. (Adding leading 1-bits if the
71          * 16 bit int is negative)
72          * 
73          * @author Christian Femers
74          */
75         public static int signed16ToSigned32(int signed16bit)
76         {
77                 return (short) signed16bit;
78         }
79
80         /**
81          * Transforms the last n bits of an int to a string that contains the binary ('1' and '0') representation of the n bits
82          * 
83          * @author Christian Femers
84          */
85         public static String toNbitString(int x, int n)
86         {
87                 StringBuilder sb = new StringBuilder(n);
88                 for (int i = 0; i < n; i++)
89                 {
90                         sb.append((x >> i) & 1);
91                 }
92                 return sb.reverse().toString();
93         }
94
95         public static BitVector of(int value, int length)
96         {
97                 BitVectorMutator mutator = BitVectorMutator.ofLength(length);
98                 int val = value;
99                 for (int i = length - 1; i >= 0; i--)
100                 {
101                         mutator.setMSBit(i, Bit.lastBitOf(val));
102                         val >>>= 1;
103                 }
104                 return mutator.toBitVector();
105         }
106 }