1 package net.mograsim.logic.model.am2900;
3 import static org.junit.jupiter.api.Assertions.fail;
5 import java.lang.reflect.Field;
6 import java.util.Objects;
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;
13 public final class TestUtil
21 * Transforms the last four bits of an int to a string that contains the binary ('1' and '0') representation of the 4 bits
23 * @author Christian Femers
25 public static String to4bitBin(int x)
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');
36 * Transforms the given boolean to a string that contains the binary ('1' and '0') representation of the bit
38 * @author Christian Femers
40 public static String to1bitBin(boolean bitIsSet)
42 return bitIsSet ? "1" : "0";
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.
49 * @author Christian Femers
51 public static String to1bitBin(int someInt)
53 return someInt != 0 ? "1" : "0";
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
60 * @author Christian Femers
62 public static int signed4ToSigned32(int signed4bit)
64 if ((signed4bit & 0b1000) > 0)
65 return signed4bit | 0xFF_FF_FF_F0;
66 return signed4bit & 0x00_00_00_0F;
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)
73 * @author Christian Femers
75 public static int signed16ToSigned32(int signed16bit)
77 return (short) signed16bit;
81 * Transforms the last n bits of an int to a string that contains the binary ('1' and '0') representation of the n bits
83 * @author Christian Femers
85 public static String toNbitString(int x, int n)
87 StringBuilder sb = new StringBuilder(n);
88 for (int i = 0; i < n; i++)
90 sb.append((x >> i) & 1);
92 return sb.reverse().toString();
95 public static BitVector of(int value, int length)
97 BitVectorMutator mutator = BitVectorMutator.ofLength(length);
99 for (int i = length - 1; i >= 0; i--)
101 mutator.setMSBit(i, Bit.lastBitOf(val));
104 return mutator.toBitVector();