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