Moved main memory to machine module
[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.Wire;
14 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
15
16 class WordAddressableMemoryTest {
17         
18         private Timeline t = new Timeline(10);
19
20         @Test
21         public void wordAddressableMemoryLargeTest()
22         {
23                 Wire rW = new Wire(t, 1, 2);
24                 Wire data = new Wire(t, 16, 2);
25                 Wire address = new Wire(t, 64, 2);
26                 ReadWriteEnd rWI = rW.createReadWriteEnd();
27                 ReadWriteEnd dataI = data.createReadWriteEnd();
28                 ReadWriteEnd addressI = address.createReadWriteEnd();
29
30                 WordAddressableMemoryComponent memory = new WordAddressableMemoryComponent(t, 4, 4096L, Long.MAX_VALUE, data.createReadWriteEnd(),
31                                 rW.createReadOnlyEnd(), address.createReadOnlyEnd());
32
33                 Random r = new Random();
34                 for (long j = 1; j > 0; j *= 2)
35                 {
36                         for (int i = 0; i < 50; i++)
37                         {
38                                 String sAddress = String.format("%64s", BigInteger.valueOf(4096 + i + j).toString(2)).replace(' ', '0');
39                                 sAddress = new StringBuilder(sAddress).reverse().toString();
40                                 BitVector bAddress = BitVector.parse(sAddress);
41                                 addressI.feedSignals(bAddress);
42                                 t.executeAll();
43                                 String random = BigInteger.valueOf(Math.abs(r.nextInt())).toString(5);
44                                 random = random.substring(Integer.max(0, random.length() - 16));
45                                 random = String.format("%16s", random).replace(' ', '0');
46                                 random = random.replace('2', 'X').replace('3', 'Z').replace('4', 'U');
47                                 BitVector vector = BitVector.parse(random);
48                                 dataI.feedSignals(vector);
49                                 rWI.feedSignals(Bit.ZERO);
50                                 t.executeAll();
51                                 rWI.feedSignals(Bit.ONE);
52                                 t.executeAll();
53                                 dataI.clearSignals();
54                                 t.executeAll();
55
56                                 assertEquals(dataI.getValues(), vector);
57                         }
58                 }
59         }
60
61 }