rename Alu2->Alu
[fleet.git] / src / edu / berkeley / fleet / demo / Test3.java
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;
5 import java.util.*;
6
7 public class Test3 {
8
9     private static Instruction literal(Dock dock, int literal) {
10         return new Instruction.Set(dock, false, Predicate.Default,
11                                    SetDest.DataLatch, literal);
12     }
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);
16     }
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);
20     }
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);
24     }
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);
28     }
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);
32     }
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);
36     }
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);
40     }
41     private static Instruction setInnerCounter(Dock dock, int immediate) {
42         return new Instruction.Set(dock, false, Predicate.Default,
43                                    SetDest.InnerLoopCounter, immediate);
44     }
45
46     public static void main(String[] s) {
47
48         Fleet fleet = new Interpreter(new String[] {
49                 "alu2",
50                 "Fifo",
51                 "Debug"
52             },
53             /* logging */ true);
54
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");
59
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")));
65         }
66
67         for(Instruction i : new Instruction[] {
68
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()),
72
73                 // prime the pump with two tokens
74                 setInnerCounter(alu.getDock("in1"), 2),
75                 sendToken(alu.getDock("in1"), fifo.getDock("out").getDataDestination()),
76
77                 // literal to be added to each argument
78                 literal(alu.getDock("in2"), 1),
79
80                 // opcode=ADD
81                 literal(alu.getDock("inOp"), 0),
82
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()),
86
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")),
92
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()),
96
97                 setInnerCounter(debug.getDock("in"), 8),
98                 recvDeliver(debug.getDock("in")),
99             })
100             instructions.add(i);
101
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());
105         fp.terminate();
106     }
107
108 }