1 package net.mograsim.logic.model.am2900.am2901;
3 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Dest.NOP;
4 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Dest.QREG;
5 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Dest.RAMF;
6 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Func.ADD;
7 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Func.AND;
8 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Func.EXOR;
9 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Func.OR;
10 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Func.SUBR;
11 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Src.AB;
12 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Src.DA;
13 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Am2901_Src.DZ;
14 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Register.Q;
15 import static net.mograsim.logic.model.am2900.am2901.TestableAm2901.Register.r0;
16 import static net.mograsim.logic.model.am2900.util.TestUtil.signed4ToSigned32;
17 import static net.mograsim.logic.model.am2900.util.TestUtil.to1bitBin;
18 import static net.mograsim.logic.model.am2900.util.TestUtil.to4bitBin;
19 import static org.junit.jupiter.api.Assertions.assertAll;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import java.awt.Point;
23 import java.util.stream.IntStream;
24 import java.util.stream.Stream;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.DisplayName;
28 import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
29 import org.junit.jupiter.api.Order;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.TestMethodOrder;
32 import org.junit.jupiter.api.extension.RegisterExtension;
33 import org.junit.jupiter.params.ParameterizedTest;
34 import org.junit.jupiter.params.provider.EnumSource;
36 import net.mograsim.logic.model.am2900.am2901.TestableAm2901.Register;
37 import net.mograsim.logic.model.am2900.util.DisplayStateOnFailure;
39 @DisplayName("Am2901 Tests")
40 @TestMethodOrder(OrderAnnotation.class)
41 public class Am2901Test
43 private TestableAm2901 am2901 = new TestableAm2901Impl();
46 DisplayStateOnFailure failureRule = new DisplayStateOnFailure(am2901);
60 void setRegistersToZero()
63 for (Register r : Register.values())
69 void setRegisterToZero(Register r)
76 am2901.assertFullCycleSuccess();
79 void setRegOutput(Register r)
86 am2901.setReg_B(r.toBitString());
91 void setInputsToZero()
93 am2901.setCarryIn("0");
98 am2901.setReg_A("0000");
99 am2901.setReg_B("0000");
103 am2901.setDest(QREG);
104 // am2901.setNotOutEnable("0"); TODO
105 am2901.clockOn(true);
106 am2901.assertRunSuccess();
109 @ParameterizedTest(name = "{0}")
111 @DisplayName("Direct / high level access")
112 @EnumSource(Register.class)
113 void testDirectAccess(Register r)
115 assertEquals("UUUU", am2901.getDirectly(r));
117 am2901.setDirectly(r, "1011");
119 assertEquals("1011", am2901.getDirectly(r));
122 @ParameterizedTest(name = "{0}")
124 @DisplayName("Setting each register to 0")
125 @EnumSource(Register.class)
126 void testSetToZero(Register r)
128 assertEquals("UUUU", am2901.getDirectly(r));
130 setRegisterToZero(r);
132 assertEquals("0000", am2901.getDirectly(r));
133 assertEquals("0000", am2901.getY());
134 assertEquals("0", am2901.getCarryOut());
135 assertEquals("0", am2901.getOverflow());
136 assertEquals("0", am2901.getSign());
137 assertEquals("1", am2901.getZero());
142 @DisplayName("Setting all registers to 0")
143 void testSetAllToZero()
145 setRegistersToZero();
147 assertEquals("0000", am2901.getY());
148 assertEquals("0", am2901.getCarryOut());
149 assertEquals("0", am2901.getOverflow());
150 assertEquals("0", am2901.getSign());
151 assertEquals("1", am2901.getZero());
152 assertEquals("0", am2901.getQ_0());
153 assertEquals("0", am2901.getQ_3());
154 assertEquals("0", am2901.getRAM_0());
155 assertEquals("0", am2901.getRAM_3());
157 assertAll("register values", Register.stream().map(r -> () ->
159 assertEquals("0000", am2901.getDirectly(r), r.name());
165 @DisplayName("ADD operation")
171 am2901.setReg_A(r0.toBitString());
173 assertAll(getAll4BitPairs().map(xy -> () ->
175 am2901.setDirectly(r0, to4bitBin(xy.x));
176 am2901.setD(to4bitBin(xy.y));
178 am2901.assertFullCycleSuccess();
180 int res32Bit = xy.x + xy.y;
181 int res4Bit = res32Bit & 0b1111;
182 int res32Bit_sgn = signed4ToSigned32(xy.x) + signed4ToSigned32(xy.y);
183 int res4Bit_sgn = signed4ToSigned32(res32Bit_sgn);
185 assertAll("Result of " + xy.x + " + " + xy.y + " = " + res32Bit,
186 () -> assertEquals(to4bitBin(res32Bit), am2901.getY(), " Y"),
187 () -> assertEquals(to1bitBin(res4Bit == 0), am2901.getZero(), " F=0"),
188 () -> assertEquals(to1bitBin(res4Bit & 0b1000), am2901.getSign(), " F3"),
189 () -> assertEquals(to1bitBin(res32Bit > 15), am2901.getCarryOut(), " Cn+4"),
190 () -> assertEquals(to1bitBin(res4Bit_sgn != res32Bit_sgn), am2901.getOverflow(), " OVR"));
196 @DisplayName("AND operation")
202 am2901.setReg_A(r0.toBitString());
204 assertAll(getAll4BitPairs().map(xy -> () ->
206 am2901.setDirectly(r0, to4bitBin(xy.x));
207 am2901.setD(to4bitBin(xy.y));
209 am2901.assertFullCycleSuccess();
211 int res32Bit = xy.x & xy.y;
213 assertAll("Result of " + xy.x + " & " + xy.y + " = " + res32Bit,
214 () -> assertEquals(to4bitBin(res32Bit), am2901.getY(), " Y"),
215 () -> assertEquals(to1bitBin(res32Bit == 0), am2901.getZero(), " F=0"),
216 () -> assertEquals(to1bitBin(res32Bit & 0b1000), am2901.getSign(), " F3")
217 // () -> assertEquals(to1bitBin(res32Bit), am2901.getCarryOut(), " Cn+4"), // TODO
218 // () -> assertEquals(to1bitBin(res32Bit), am2901.getOverflow(), " OVR") // TODO
225 @DisplayName("OR operation")
231 am2901.setReg_A(r0.toBitString());
233 assertAll(getAll4BitPairs().map(xy -> () ->
235 am2901.setDirectly(r0, to4bitBin(xy.x));
236 am2901.setD(to4bitBin(xy.y));
238 am2901.assertFullCycleSuccess();
240 int res32Bit = xy.x | xy.y;
242 assertAll("Result of " + xy.x + " | " + xy.y + " = " + res32Bit,
243 () -> assertEquals(to4bitBin(res32Bit), am2901.getY(), " Y"),
244 () -> assertEquals(to1bitBin(res32Bit == 0), am2901.getZero(), " F=0"),
245 () -> assertEquals(to1bitBin(res32Bit & 0b1000), am2901.getSign(), " F3")
246 // () -> assertEquals(to1bitBin(res32Bit != 0b1111), am2901.getCarryOut(), " Cn+4"), // TODO
247 // () -> assertEquals(to1bitBin(res32Bit != 0b1111), am2901.getOverflow(), " OVR") // TODO
254 @DisplayName("XOR operation")
258 am2901.setFunc(EXOR);
260 am2901.setReg_A(r0.toBitString());
262 assertAll(getAll4BitPairs().map(xy -> () ->
264 am2901.setDirectly(r0, to4bitBin(xy.x));
265 am2901.setD(to4bitBin(xy.y));
267 am2901.assertFullCycleSuccess();
269 int res32Bit = xy.x ^ xy.y;
271 assertAll("Result of " + xy.x + " ^ " + xy.y + " = " + res32Bit,
272 () -> assertEquals(to4bitBin(res32Bit), am2901.getY(), " Y"),
273 () -> assertEquals(to1bitBin(res32Bit == 0), am2901.getZero(), " F=0"),
274 () -> assertEquals(to1bitBin(res32Bit & 0b1000), am2901.getSign(), " F3"));
280 @DisplayName("SUB operation")
284 am2901.setCarryIn("1");
285 am2901.setFunc(SUBR);
287 am2901.setReg_A(r0.toBitString());
289 assertAll(getAll4BitPairs().map(xy -> () ->
291 am2901.setDirectly(r0, to4bitBin(xy.x));
292 am2901.setD(to4bitBin(xy.y));
294 am2901.assertFullCycleSuccess();
296 int res32Bit = xy.x - xy.y;
297 int res4Bit = res32Bit & 0b1111;
298 int res32Bit_sgn = signed4ToSigned32(xy.x) - signed4ToSigned32(xy.y);
299 int res4Bit_sgn = signed4ToSigned32(res32Bit_sgn);
301 assertAll("Result of " + xy.x + " - " + xy.y + " = " + res32Bit,
302 () -> assertEquals(to4bitBin(res32Bit), am2901.getY(), " Y"),
303 () -> assertEquals(to1bitBin(res4Bit == 0), am2901.getZero(), " F=0"),
304 () -> assertEquals(to1bitBin(res4Bit & 0b1000), am2901.getSign(), " F3"),
305 () -> assertEquals(to1bitBin(xy.x >= xy.y), am2901.getCarryOut(), " Cn+4"),
306 () -> assertEquals(to1bitBin(res4Bit_sgn != res32Bit_sgn), am2901.getOverflow(), " OVR"));
310 static Stream<Point> getAll4BitPairs()
312 return IntStream.range(0, 16).boxed().flatMap(x -> IntStream.range(0, 16).mapToObj(y -> new Point(x, y)));