add edu.berkeley.fleet.demo
authoradam <adam@megacz.com>
Thu, 21 Aug 2008 10:28:22 +0000 (11:28 +0100)
committeradam <adam@megacz.com>
Thu, 21 Aug 2008 10:28:22 +0000 (11:28 +0100)
src/edu/berkeley/fleet/demo/Test2.java [new file with mode: 0644]
src/edu/berkeley/fleet/demo/Test3.java [new file with mode: 0644]

diff --git a/src/edu/berkeley/fleet/demo/Test2.java b/src/edu/berkeley/fleet/demo/Test2.java
new file mode 100644 (file)
index 0000000..5a5037d
--- /dev/null
@@ -0,0 +1,54 @@
+package edu.berkeley.fleet.demo;
+import edu.berkeley.fleet.api.*;
+import static edu.berkeley.fleet.api.Instruction.Set.*;
+import edu.berkeley.fleet.interpreter.Interpreter;
+
+public class Test2 {
+
+    private static Instruction literal(Dock dock, int literal) {
+        return new Instruction.Set(dock, false, Predicate.Default,
+                                   SetDest.DataLatch, literal);
+    }
+    private static Instruction deliver(Dock dock) {
+        return new Instruction.Move(dock, false, Predicate.Default,
+                                    false, null, false, false, false, false, true, false);
+    }
+    private static Instruction collectAndSend(Dock dock, Destination dest) {
+        return new Instruction.Move(dock, false, Predicate.Default,
+                                    false, dock.getPath(dest,null), false, true, true, false, true, false);
+    }
+    private static Instruction recvAndDeliver(Dock dock) {
+        return new Instruction.Move(dock, false, Predicate.Default,
+                                    false, null, false, true, true, false, true, false);
+    }
+
+    public static void main(String[] s) {
+
+        Fleet fleet = new Interpreter(new String[] {
+                "Alu2",
+                "Debug"
+            },
+            /* logging */ true);
+
+        Ship alu     = fleet.getShip("Alu2",  0);
+        Ship debug   = fleet.getShip("Debug", 0);
+        Dock debugIn = debug.getDock("in");
+
+        Instruction[] instructions = new Instruction[] {
+            literal(alu.getDock("in1"), 1),
+            literal(alu.getDock("in2"), 2),
+            literal(alu.getDock("inOp"), 0 /* opcode for ADD */),
+            deliver(alu.getDock("in1")),
+            deliver(alu.getDock("in2")),
+            deliver(alu.getDock("inOp")),
+            collectAndSend(alu.getDock("out"), debugIn.getDataDestination()),
+            recvAndDeliver(debugIn),
+        };
+
+        FleetProcess fp = fleet.run(instructions);
+        BitVector bv = fp.readWord();
+        System.out.println(bv.toLong());
+        fp.terminate();
+    }
+
+}
\ No newline at end of file
diff --git a/src/edu/berkeley/fleet/demo/Test3.java b/src/edu/berkeley/fleet/demo/Test3.java
new file mode 100644 (file)
index 0000000..22c212a
--- /dev/null
@@ -0,0 +1,108 @@
+package edu.berkeley.fleet.demo;
+import edu.berkeley.fleet.api.*;
+import static edu.berkeley.fleet.api.Instruction.Set.*;
+import edu.berkeley.fleet.interpreter.Interpreter;
+import java.util.*;
+
+public class Test3 {
+
+    private static Instruction literal(Dock dock, int literal) {
+        return new Instruction.Set(dock, false, Predicate.Default,
+                                   SetDest.DataLatch, literal);
+    }
+    private static Instruction deliver(Dock dock) {
+        return new Instruction.Move(dock, false, Predicate.Default,
+                                    false, null, false, false, false, false, true, false);
+    }
+    private static Instruction collectAndSend(Dock dock, Destination dest) {
+        return new Instruction.Move(dock, false, Predicate.Default,
+                                    false, dock.getPath(dest,null), false, true, true, false, true, false);
+    }
+    private static Instruction recvAndDeliver(Dock dock) {
+        return new Instruction.Move(dock, false, Predicate.Default,
+                                    false, null, false, true, true, false, true, false);
+    }
+    private static Instruction collectWaitAndSend(Dock dock, Destination dest) {
+        return new Instruction.Move(dock, false, Predicate.Default,
+                                    false, dock.getPath(dest,null), true, true, true, false, true, false);
+    }
+    private static Instruction recvDeliverAndSendToken(Dock dock, Destination ack) {
+        return new Instruction.Move(dock, false, Predicate.Default,
+                                    false, dock.getPath(ack,null), false, true, true, false, true, true);
+    }
+    private static Instruction recvDeliver(Dock dock) {
+        return new Instruction.Move(dock, false, Predicate.Default,
+                                    false, null, false, true, true, false, true, false);
+    }
+    private static Instruction sendToken(Dock dock, Destination ack) {
+        return new Instruction.Move(dock, false, Predicate.Default,
+                                    false, dock.getPath(ack,null), false, false, false, false, false, true);
+    }
+    private static Instruction setInnerCounter(Dock dock, int immediate) {
+        return new Instruction.Set(dock, false, Predicate.Default,
+                                   SetDest.InnerLoopCounter, immediate);
+    }
+
+    public static void main(String[] s) {
+
+        Fleet fleet = new Interpreter(new String[] {
+                "Alu2",
+                "Fifo",
+                "Debug"
+            },
+            /* logging */ true);
+
+        Ship alu     = fleet.getShip("Alu2",  0);
+        Ship debug   = fleet.getShip("Debug", 0);
+        Ship fifo    = fleet.getShip("Fifo", 0);
+        Dock debugIn = debug.getDock("in");
+
+        // fill up the fifo with numbers 1..8
+        ArrayList<Instruction> instructions = new ArrayList<Instruction>();
+        for(int i=0; i<8; i++) {
+            instructions.add(literal(fifo.getDock("in"), i));
+            instructions.add(deliver(fifo.getDock("in")));
+        }
+
+        for(Instruction i : new Instruction[] {
+
+                // repeat 8 times: wait for a token, sent a datum fifo.out->alu.in1
+                setInnerCounter(fifo.getDock("out"), 8),
+                collectWaitAndSend(fifo.getDock("out"), alu.getDock("in1").getDataDestination()),
+
+                // prime the pump with two tokens
+                setInnerCounter(alu.getDock("in1"), 2),
+                sendToken(alu.getDock("in1"), fifo.getDock("out").getDataDestination()),
+
+                // literal to be added to each argument
+                literal(alu.getDock("in2"), 1),
+
+                // opcode=ADD
+                literal(alu.getDock("inOp"), 0),
+
+                // 8x: recieve an argument, add it, send acknowledgement token
+                setInnerCounter(alu.getDock("in1"), 8),
+                recvDeliverAndSendToken(alu.getDock("in1"), fifo.getDock("out").getDataDestination()),
+
+                // 8x: deliver at in2 and inOp
+                setInnerCounter(alu.getDock("in2"), 8),
+                deliver(alu.getDock("in2")),
+                setInnerCounter(alu.getDock("inOp"), 8),
+                deliver(alu.getDock("inOp")),
+
+                // 8x: take alu output and send it to the debug ship
+                setInnerCounter(alu.getDock("out"), 8),
+                collectAndSend(alu.getDock("out"), debugIn.getDataDestination()),
+
+                setInnerCounter(debug.getDock("in"), 8),
+                recvDeliver(debug.getDock("in")),
+            })
+            instructions.add(i);
+
+        FleetProcess fp = fleet.run(instructions.toArray(new Instruction[0]));
+        for(int i=0; i<8; i++)
+            System.out.println(fp.readWord().toLong());
+        fp.terminate();
+    }
+
+}
\ No newline at end of file