X-Git-Url: https://mograsim.net/gitweb/?p=Mograsim.git;a=blobdiff_plain;f=tests%2Fnet.mograsim.machine.tests%2Fsrc%2Fnet%2Fmograsim%2Fmachine%2Fstandard%2Fmemory%2FMainMemoryTest.java;fp=tests%2Fnet.mograsim.machine.tests%2Fsrc%2Fnet%2Fmograsim%2Fmachine%2Fstandard%2Fmemory%2FMainMemoryTest.java;h=220aa9dd1dad6c98e35e446394225e5c7f6b1e1e;hp=0000000000000000000000000000000000000000;hb=b5d55c59d7069171bd928e4a945d9185ee4bc2b0;hpb=f098cd47d06be0cc654532a5fad0e5e89f0ef93c 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 index 00000000..220aa9dd --- /dev/null +++ b/tests/net.mograsim.machine.tests/src/net/mograsim/machine/standard/memory/MainMemoryTest.java @@ -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 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); + } +}