1 package edu.berkeley.fleet.demo;
2 import edu.berkeley.fleet.api.*;
3 import static edu.berkeley.fleet.api.Instruction.Set.*;
4 import edu.berkeley.fleet.interpreter.Interpreter;
9 private static Instruction literal(Dock dock, int literal) {
10 return new Instruction.Set(dock, false, Predicate.Default,
11 SetDest.DataLatch, literal);
13 private static Instruction deliver(Dock dock) {
14 return new Instruction.Move(dock, false, Predicate.Default,
15 false, null, false, false, false, false, true, false);
17 private static Instruction collectAndSend(Dock dock, Destination dest) {
18 return new Instruction.Move(dock, false, Predicate.Default,
19 false, dock.getPath(dest,null), false, true, true, false, true, false);
21 private static Instruction recvAndDeliver(Dock dock) {
22 return new Instruction.Move(dock, false, Predicate.Default,
23 false, null, false, true, true, false, true, false);
25 private static Instruction collectWaitAndSend(Dock dock, Destination dest) {
26 return new Instruction.Move(dock, false, Predicate.Default,
27 false, dock.getPath(dest,null), true, true, true, false, true, false);
29 private static Instruction recvDeliverAndSendToken(Dock dock, Destination ack) {
30 return new Instruction.Move(dock, false, Predicate.Default,
31 false, dock.getPath(ack,null), false, true, true, false, true, true);
33 private static Instruction recvDeliver(Dock dock) {
34 return new Instruction.Move(dock, false, Predicate.Default,
35 false, null, false, true, true, false, true, false);
37 private static Instruction sendToken(Dock dock, Destination ack) {
38 return new Instruction.Move(dock, false, Predicate.Default,
39 false, dock.getPath(ack,null), false, false, false, false, false, true);
41 private static Instruction setInnerCounter(Dock dock, int immediate) {
42 return new Instruction.Set(dock, false, Predicate.Default,
43 SetDest.InnerLoopCounter, immediate);
46 public static void main(String[] s) {
48 Fleet fleet = new Interpreter(new String[] {
55 Ship alu = fleet.getShip("alu2", 0);
56 Ship debug = fleet.getShip("Debug", 0);
57 Ship fifo = fleet.getShip("Fifo", 0);
58 Dock debugIn = debug.getDock("in");
60 // fill up the fifo with numbers 1..8
61 ArrayList<Instruction> instructions = new ArrayList<Instruction>();
62 for(int i=0; i<8; i++) {
63 instructions.add(literal(fifo.getDock("in"), i));
64 instructions.add(deliver(fifo.getDock("in")));
67 for(Instruction i : new Instruction[] {
69 // repeat 8 times: wait for a token, sent a datum fifo.out->alu.in1
70 setInnerCounter(fifo.getDock("out"), 8),
71 collectWaitAndSend(fifo.getDock("out"), alu.getDock("in1").getDataDestination()),
73 // prime the pump with two tokens
74 setInnerCounter(alu.getDock("in1"), 2),
75 sendToken(alu.getDock("in1"), fifo.getDock("out").getDataDestination()),
77 // literal to be added to each argument
78 literal(alu.getDock("in2"), 1),
81 literal(alu.getDock("inOp"), 0),
83 // 8x: recieve an argument, add it, send acknowledgement token
84 setInnerCounter(alu.getDock("in1"), 8),
85 recvDeliverAndSendToken(alu.getDock("in1"), fifo.getDock("out").getDataDestination()),
87 // 8x: deliver at in2 and inOp
88 setInnerCounter(alu.getDock("in2"), 8),
89 deliver(alu.getDock("in2")),
90 setInnerCounter(alu.getDock("inOp"), 8),
91 deliver(alu.getDock("inOp")),
93 // 8x: take alu output and send it to the debug ship
94 setInnerCounter(alu.getDock("out"), 8),
95 collectAndSend(alu.getDock("out"), debugIn.getDataDestination()),
97 setInnerCounter(debug.getDock("in"), 8),
98 recvDeliver(debug.getDock("in")),
102 FleetProcess fp = fleet.run(instructions.toArray(new Instruction[0]));
103 for(int i=0; i<8; i++)
104 System.out.println(fp.readWord().toLong());