Apply formatter, optional problems, save actions to machine project
[Mograsim.git] / net.mograsim.machine / test / net / mograsim / machine / standard / memory / WordAddressableMemoryTest.java
1 package net.mograsim.machine.standard.memory;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4
5 import java.math.BigInteger;
6 import java.util.Random;
7
8 import org.junit.jupiter.api.Test;
9
10 import net.mograsim.logic.core.timeline.Timeline;
11 import net.mograsim.logic.core.types.Bit;
12 import net.mograsim.logic.core.types.BitVector;
13 import net.mograsim.logic.core.wires.CoreWire;
14 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
15 import net.mograsim.machine.MainMemoryDefinition;
16
17 class WordAddressableMemoryTest
18 {
19
20         private Timeline t = new Timeline(10);
21
22         @Test
23         public void wordAddressableMemoryLargeTest()
24         {
25                 CoreWire rW = new CoreWire(t, 1, 2);
26                 CoreWire data = new CoreWire(t, 16, 2);
27                 CoreWire address = new CoreWire(t, 64, 2);
28                 ReadWriteEnd rWI = rW.createReadWriteEnd();
29                 ReadWriteEnd dataI = data.createReadWriteEnd();
30                 ReadWriteEnd addressI = address.createReadWriteEnd();
31
32                 @SuppressWarnings("unused")
33                 CoreWordAddressableMemory memory = new CoreWordAddressableMemory(t, 4,
34                                 new WordAddressableMemory(MainMemoryDefinition.create(64, 16, 4096L, Long.MAX_VALUE)), data.createReadWriteEnd(),
35                                 rW.createReadOnlyEnd(), address.createReadOnlyEnd());
36
37                 Random r = new Random();
38                 for (long j = 1; j > 0; j *= 2)
39                 {
40                         for (int i = 0; i < 50; i++)
41                         {
42                                 String sAddress = String.format("%64s", BigInteger.valueOf(4096 + i + j).toString(2)).replace(' ', '0');
43                                 sAddress = new StringBuilder(sAddress).reverse().toString();
44                                 BitVector bAddress = BitVector.parse(sAddress);
45                                 addressI.feedSignals(bAddress);
46                                 t.executeAll();
47                                 String random = BigInteger.valueOf(Math.abs(r.nextInt())).toString(5);
48                                 random = random.substring(Integer.max(0, random.length() - 16));
49                                 random = String.format("%16s", random).replace(' ', '0');
50                                 random = random.replace('2', 'X').replace('3', 'Z').replace('4', 'U');
51                                 BitVector vector = BitVector.parse(random);
52                                 dataI.feedSignals(vector);
53                                 rWI.feedSignals(Bit.ZERO);
54                                 t.executeAll();
55                                 rWI.feedSignals(Bit.ONE);
56                                 t.executeAll();
57                                 dataI.clearSignals();
58                                 t.executeAll();
59
60                                 assertEquals(dataI.getValues(), vector);
61                         }
62                 }
63         }
64
65 }