1 package net.mograsim.logic.model.am2900.am2901;
3 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Dest.*;
4 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Func.*;
5 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Src.*;
6 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Register.*;
7 import static net.mograsim.logic.model.am2900.util.TestUtil.*;
8 import static org.junit.jupiter.api.Assertions.*;
10 import java.awt.Point;
11 import java.util.stream.IntStream;
12 import java.util.stream.Stream;
14 import org.junit.jupiter.api.BeforeEach;
15 import org.junit.jupiter.api.DisplayName;
16 import org.junit.jupiter.api.Order;
17 import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
18 import org.junit.jupiter.api.Test;
19 import org.junit.jupiter.api.TestMethodOrder;
20 import org.junit.jupiter.api.extension.RegisterExtension;
21 import org.junit.jupiter.params.ParameterizedTest;
22 import org.junit.jupiter.params.provider.EnumSource;
24 import net.mograsim.logic.model.am2900.am2901.TestableAm2901.Register;
25 import net.mograsim.logic.model.am2900.util.DisplayStateOnFailure;
27 @DisplayName("Am2901 Tests")
28 @TestMethodOrder(OrderAnnotation.class)
29 public class Am2901Test
31 private TestableAm2901 am2901 = new TestableAm2901Impl();
34 DisplayStateOnFailure failureRule = new DisplayStateOnFailure(am2901);
48 void setRegistersToZero()
51 for (Register r : Register.values())
57 void setRegisterToZero(Register r)
64 am2901.assertFullCycleSuccess();
67 void setRegOutput(Register r)
74 am2901.setReg_B(r.toBitString());
79 void setInputsToZero()
81 am2901.setCarryIn("0");
86 am2901.setReg_A("0000");
87 am2901.setReg_B("0000");
92 // am2901.setNotOutEnable("0"); TODO
94 am2901.assertRunSuccess();
97 @ParameterizedTest(name = "{0}")
99 @DisplayName("Direct / high level access")
100 @EnumSource(Register.class)
101 void testDirectAccess(Register r)
103 assertEquals("UUUU", am2901.getDirectly(r));
105 am2901.setDirectly(r, "1011");
107 assertEquals("1011", am2901.getDirectly(r));
110 @ParameterizedTest(name = "{0}")
112 @DisplayName("Setting each register to 0")
113 @EnumSource(Register.class)
114 void testSetToZero(Register r)
116 assertEquals("UUUU", am2901.getDirectly(r));
118 setRegisterToZero(r);
120 assertEquals("0000", am2901.getDirectly(r));
121 assertEquals("0000", am2901.getY());
122 assertEquals("0", am2901.getCarryOut());
123 assertEquals("0", am2901.getOverflow());
124 assertEquals("0", am2901.getSign());
125 assertEquals("1", am2901.getZero());
130 @DisplayName("Setting all registers to 0")
131 void testSetAllToZero()
133 setRegistersToZero();
135 assertEquals("0000", am2901.getY());
136 assertEquals("0", am2901.getCarryOut());
137 assertEquals("0", am2901.getOverflow());
138 assertEquals("0", am2901.getSign());
139 assertEquals("1", am2901.getZero());
140 assertEquals("0", am2901.getQ_0());
141 assertEquals("0", am2901.getQ_3());
142 assertEquals("0", am2901.getRAM_0());
143 assertEquals("0", am2901.getRAM_3());
145 assertAll("register values", Register.stream().map(r -> () ->
147 assertEquals("0000", am2901.getDirectly(r), r.name());
153 @DisplayName("ADD operation")
159 am2901.setReg_A(r0.toBitString());
161 assertAll(getAll4BitPairs().map(xy -> () ->
163 am2901.setDirectly(r0, to4bitBin(xy.x));
164 am2901.setD(to4bitBin(xy.y));
166 am2901.assertFullCycleSuccess();
168 int res32Bit = xy.x + xy.y;
169 int res4Bit = res32Bit & 0b1111;
170 int res32Bit_sgn = signed4ToSigned32(xy.x) + signed4ToSigned32(xy.y);
171 int res4Bit_sgn = signed4ToSigned32(res32Bit_sgn);
173 assertAll("Result of " + xy.x + " + " + xy.y + " = " + res32Bit,
174 () -> assertEquals(to4bitBin(res32Bit), am2901.getY(), " Y"),
175 () -> assertEquals(to1bitBin(res4Bit == 0), am2901.getZero(), " F=0"),
176 () -> assertEquals(to1bitBin(res4Bit & 0b1000), am2901.getSign(), " F3"),
177 () -> assertEquals(to1bitBin(res32Bit > 15), am2901.getCarryOut(), " Cn+4"),
178 () -> assertEquals(to1bitBin(res4Bit_sgn != res32Bit_sgn), am2901.getOverflow(), " OVR"));
184 @DisplayName("AND operation")
190 am2901.setReg_A(r0.toBitString());
192 assertAll(getAll4BitPairs().map(xy -> () ->
194 am2901.setDirectly(r0, to4bitBin(xy.x));
195 am2901.setD(to4bitBin(xy.y));
197 am2901.assertFullCycleSuccess();
199 int res32Bit = xy.x & xy.y;
201 assertAll("Result of " + xy.x + " & " + xy.y + " = " + res32Bit,
202 () -> assertEquals(to4bitBin(res32Bit), am2901.getY(), " Y"),
203 () -> assertEquals(to1bitBin(res32Bit == 0), am2901.getZero(), " F=0"),
204 () -> assertEquals(to1bitBin(res32Bit & 0b1000), am2901.getSign(), " F3")
205 // () -> assertEquals(to1bitBin(res32Bit), am2901.getCarryOut(), " Cn+4"), // TODO
206 // () -> assertEquals(to1bitBin(res32Bit), am2901.getOverflow(), " OVR") // TODO
213 @DisplayName("OR operation")
219 am2901.setReg_A(r0.toBitString());
221 assertAll(getAll4BitPairs().map(xy -> () ->
223 am2901.setDirectly(r0, to4bitBin(xy.x));
224 am2901.setD(to4bitBin(xy.y));
226 am2901.assertFullCycleSuccess();
228 int res32Bit = xy.x | xy.y;
230 assertAll("Result of " + xy.x + " | " + xy.y + " = " + res32Bit,
231 () -> assertEquals(to4bitBin(res32Bit), am2901.getY(), " Y"),
232 () -> assertEquals(to1bitBin(res32Bit == 0), am2901.getZero(), " F=0"),
233 () -> assertEquals(to1bitBin(res32Bit & 0b1000), am2901.getSign(), " F3")
234 // () -> assertEquals(to1bitBin(res32Bit != 0b1111), am2901.getCarryOut(), " Cn+4"), // TODO
235 // () -> assertEquals(to1bitBin(res32Bit != 0b1111), am2901.getOverflow(), " OVR") // TODO
242 @DisplayName("XOR operation")
246 am2901.setFunc(EXOR);
248 am2901.setReg_A(r0.toBitString());
250 assertAll(getAll4BitPairs().map(xy -> () ->
252 am2901.setDirectly(r0, to4bitBin(xy.x));
253 am2901.setD(to4bitBin(xy.y));
255 am2901.assertFullCycleSuccess();
257 int res32Bit = xy.x ^ xy.y;
259 assertAll("Result of " + xy.x + " ^ " + xy.y + " = " + res32Bit,
260 () -> assertEquals(to4bitBin(res32Bit), am2901.getY(), " Y"),
261 () -> assertEquals(to1bitBin(res32Bit == 0), am2901.getZero(), " F=0"),
262 () -> assertEquals(to1bitBin(res32Bit & 0b1000), am2901.getSign(), " F3"));
268 @DisplayName("SUB operation")
272 am2901.setCarryIn("1");
273 am2901.setFunc(SUBR);
275 am2901.setReg_A(r0.toBitString());
277 assertAll(getAll4BitPairs().map(xy -> () ->
279 am2901.setDirectly(r0, to4bitBin(xy.x));
280 am2901.setD(to4bitBin(xy.y));
282 am2901.assertFullCycleSuccess();
284 int res32Bit = xy.x - xy.y;
285 int res4Bit = res32Bit & 0b1111;
286 int res32Bit_sgn = signed4ToSigned32(xy.x) - signed4ToSigned32(xy.y);
287 int res4Bit_sgn = signed4ToSigned32(res32Bit_sgn);
289 assertAll("Result of " + xy.x + " - " + xy.y + " = " + res32Bit,
290 () -> assertEquals(to4bitBin(res32Bit), am2901.getY(), " Y"),
291 () -> assertEquals(to1bitBin(res4Bit == 0), am2901.getZero(), " F=0"),
292 () -> assertEquals(to1bitBin(res4Bit & 0b1000), am2901.getSign(), " F3"),
293 () -> assertEquals(to1bitBin(xy.x >= xy.y), am2901.getCarryOut(), " Cn+4"),
294 () -> assertEquals(to1bitBin(res4Bit_sgn != res32Bit_sgn), am2901.getOverflow(), " OVR"));
298 static Stream<Point> getAll4BitPairs()
300 return IntStream.range(0, 16).boxed().flatMap(x -> IntStream.range(0, 16).mapToObj(y -> new Point(x, y)));