f8c516359f553e915c7ee28d4114f28ffa44d58d
[fleet.git] / src / edu / berkeley / fleet / loops / MemoryUtils.java
1 package edu.berkeley.fleet.loops;
2 import edu.berkeley.fleet.loops.*;
3 import java.util.concurrent.Semaphore;
4 import java.util.*;
5 import java.net.*;
6 import edu.berkeley.fleet.two.*;
7 import edu.berkeley.fleet.fpga.*;
8 import edu.berkeley.fleet.api.*;
9 import edu.berkeley.fleet.api.Instruction.*;
10 import edu.berkeley.fleet.api.Instruction.Set;
11 import edu.berkeley.fleet.api.Instruction.Set.*;
12 import edu.berkeley.fleet.api.Instruction.Set.SetDest;
13 import edu.berkeley.fleet.api.Instruction.Set.FlagFunction;
14 import static edu.berkeley.fleet.api.Predicate.*;
15
16 public class MemoryUtils {
17
18     public static void readMem(FleetProcess fp, Ship memory, long offset, BitVector[] vals) throws RuntimeException {
19         doMem(true, fp, memory, offset, vals);
20     }
21     public static void writeMem(FleetProcess fp, Ship memory, long offset, long[] vals) throws RuntimeException {
22         doMem(false, fp, memory, offset, long2bv(fp.getFleet(), vals));
23     }
24     public static void writeMem(FleetProcess fp, Ship memory, long offset, BitVector[] vals) throws RuntimeException {
25         doMem(false, fp, memory, offset, vals);
26     }
27     public static void doMem(final boolean read, final FleetProcess fp, final Ship memory, final long offset, final BitVector[] vals) throws RuntimeException {
28         if (fp.getFleet() != memory.getFleet())
29             throw new RuntimeException("Fleet mismatch");
30
31         final Dock inAddrWrite = memory.getDock("inAddrWrite");
32         final Dock inDataWrite = memory.getDock("inDataWrite");
33         final Dock inAddrRead  = memory.getDock("inAddrRead");
34         final Dock out         = memory.getDock("out");
35         final Dock debugIn     = fp.getDebugInputDock();
36
37         final Semaphore sem = new Semaphore(12 /* FIXME */);
38
39         Context ctx = new Context(fp.getFleet());
40         LoopFactory lf;
41         if (read) {
42             lf = new LoopFactory(ctx, inAddrRead, 0);
43             lf.abortLoopIfTorpedoPresent();
44             lf.recvWord();
45             lf.deliver();
46         } else {
47             lf = new LoopFactory(ctx, inAddrWrite, 0);
48             lf.abortLoopIfTorpedoPresent();
49             lf.recvWord();
50             lf.deliver();
51             lf = new LoopFactory(ctx, inDataWrite, 0);
52             lf.abortLoopIfTorpedoPresent();
53             lf.recvWord();
54             lf.deliver();
55         }
56
57         lf = new LoopFactory(ctx, out, 0);
58         lf.abortLoopIfTorpedoPresent();
59         lf.collectWord();
60         lf.sendWord(debugIn.getDataDestination());
61
62         lf = new LoopFactory(ctx, debugIn, 0);
63         lf.abortLoopIfTorpedoPresent();
64         lf.recvWord();
65         lf.deliver();
66
67         ArrayList<Instruction> ai = new ArrayList<Instruction>();
68         ctx.emit(ai);
69         for(Instruction ins : ai)
70             fp.sendInstruction(ins);
71
72         new Thread() {
73             public void run() { try {
74                     for(int i=0; i<vals.length; i++) {
75                         if (!sem.tryAcquire()) {
76                             fp.flush();
77                             sem.acquire();
78                         }
79                         if (read) {
80                             fp.sendWord(inAddrRead.getDataDestination(), new BitVector(fp.getFleet().getWordWidth()).set(i+offset));
81                         } else {
82                             fp.sendWord(inAddrWrite.getDataDestination(), new BitVector(fp.getFleet().getWordWidth()).set(i+offset));
83                             fp.sendWord(inDataWrite.getDataDestination(), vals[i]);
84                         }
85                     }
86                     fp.flush();
87                 } catch (Exception e) { throw new RuntimeException(e); }
88                 }
89         }.start();
90
91         for(int i=0; i<vals.length; i++) {
92             BitVector outv = fp.recvWord();
93             if (read) vals[i] = outv;
94             if (read) System.out.print("\rread from address: " + i + ", got " + vals[i] + " = " + vals[i].toLong()+"           ");
95             else      System.out.print("\rwrote to address: " + i+"           ");
96             sem.release();
97         }
98
99         if (read) {
100             fp.sendToken(inAddrRead.getInstructionDestination());
101         } else {
102             fp.sendToken(inAddrWrite.getInstructionDestination());
103             fp.sendToken(inDataWrite.getInstructionDestination());
104         }
105         fp.sendToken(out.getInstructionDestination());
106         fp.sendToken(debugIn.getInstructionDestination());
107         System.out.println();
108     }
109
110     private static BitVector[] long2bv(Fleet fleet, long[] initialValues) {
111         BitVector[] bv = new BitVector[initialValues.length];
112         for(int i=0; i<initialValues.length; i++)
113             bv[i] = new BitVector(fleet.getWordWidth()).set(initialValues[i]);
114         return bv;
115     }
116
117     public static void putMemoryShipInDispatchMode(FleetProcess fp, Ship memoryShip) {
118         Context ctx = new Context(fp.getFleet());
119         LoopFactory lf;
120
121         lf = new LoopFactory(ctx, memoryShip.getDock("out"), 0);
122         lf.abortLoopIfTorpedoPresent();
123         lf.collectPacket();
124         lf.sendWord(null);
125
126         lf = new LoopFactory(ctx, memoryShip.getDock("inCBD"), 0);
127         lf.abortLoopIfTorpedoPresent();
128         lf.recvWord();
129         lf.deliver();
130
131         ctx.dispatch(fp);
132     }
133
134     public static void removeMemoryShipFromDispatchMode(FleetProcess fp, Ship memoryShip) {
135         fp.sendToken(memoryShip.getDock("out").getInstructionDestination());
136         fp.sendToken(memoryShip.getDock("inCBD").getInstructionDestination());
137     }
138
139     public static void main(String[] s) throws Exception {
140         Random random = new Random(System.currentTimeMillis());
141         Fleet fleet = new Fpga();
142         FleetProcess fp = fleet.run(new Instruction[0]);
143         Ship memory = fleet.getShip("DDR2",0);
144         //Ship memory = fleet.getShip("Memory",0);
145         BitVector[] vals  = new BitVector[2 * 1024];
146         BitVector[] vals2 = new BitVector[2 * 1024];
147         for(int i=0; i<vals.length; i++)
148             vals[i] = new BitVector(fleet.getWordWidth()).set(random.nextLong());
149         writeMem(fp, memory, 0, vals);
150         readMem(fp, memory, 0, vals2);
151         for(int i=0; i<vals.length; i++)
152             if (!vals[i].equals(vals2[i]))
153                 System.out.println("disagreement!  on index " + i + "\n  "+vals[i]+"\n  "+vals2[i]);
154         System.out.println("done!");
155     }
156 }