Finished MPROM support. Fixes #10
[Mograsim.git] / tests / net.mograsim.machine.tests / src / net / mograsim / machine / standard / memory / MainMemoryTest.java
diff --git a/tests/net.mograsim.machine.tests/src/net/mograsim/machine/standard/memory/MainMemoryTest.java b/tests/net.mograsim.machine.tests/src/net/mograsim/machine/standard/memory/MainMemoryTest.java
new file mode 100644 (file)
index 0000000..220aa9d
--- /dev/null
@@ -0,0 +1,69 @@
+package net.mograsim.machine.standard.memory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.math.BigInteger;
+import java.util.Random;
+import java.util.stream.LongStream;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import net.mograsim.logic.core.timeline.Timeline;
+import net.mograsim.logic.core.types.Bit;
+import net.mograsim.logic.core.types.BitVector;
+import net.mograsim.logic.core.wires.CoreWire;
+import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
+import net.mograsim.machine.MainMemory;
+import net.mograsim.machine.MainMemoryDefinition;
+import net.mograsim.machine.StandardMainMemory;
+
+class MainMemoryTest
+{
+
+       private Timeline t = new Timeline(10);
+
+       @ParameterizedTest(name = "seed = {0}")
+       @MethodSource("seedsForRandom")
+       public void wordAddressableMemoryLargeTest(long seed)
+       {
+               CoreWire rW = new CoreWire(t, 1, 2);
+               CoreWire data = new CoreWire(t, 16, 2);
+               CoreWire address = new CoreWire(t, 64, 2);
+               ReadWriteEnd rWI = rW.createReadWriteEnd();
+               ReadWriteEnd dataI = data.createReadWriteEnd();
+               ReadWriteEnd addressI = address.createReadWriteEnd();
+
+               MainMemoryDefinition definition = MainMemoryDefinition.create(64, 16, 4096L, Long.MAX_VALUE);
+               CoreBitVectorMemory<MainMemory> memory = new CoreBitVectorMemory<>(t, 4, definition, data.createReadWriteEnd(),
+                               rW.createReadOnlyEnd(), address.createReadOnlyEnd(), false);
+               memory.setMemory(new StandardMainMemory(definition));
+
+               Random r = new Random(seed);
+               for (long j = 1; j > 0; j *= 2)
+               {
+                       for (int i = 0; i < 100; i++)
+                       {
+                               BitVector bAddress = BitVector.from(4096 + i + j, 64);
+                               addressI.feedSignals(bAddress);
+                               t.executeAll();
+                               BigInteger random = BigInteger.valueOf(Math.abs(r.nextInt()));
+                               BitVector vector = BitVector.from(random, 16);
+                               dataI.feedSignals(vector);
+                               rWI.feedSignals(Bit.ZERO);
+                               t.executeAll();
+                               rWI.feedSignals(Bit.ONE);
+                               t.executeAll();
+                               dataI.clearSignals();
+                               t.executeAll();
+
+                               assertEquals(vector, dataI.getValues(), "seed=" + seed + ", j=" + j + ", i=" + i);
+                       }
+               }
+       }
+
+       public static LongStream seedsForRandom()
+       {
+               return LongStream.range(0, 20);
+       }
+}