clean up Rotator fpga code
[fleet.git] / ships / Rotator.ship
1 ship: Rotator
2
3 == Ports ===========================================================
4 in:   in
5 in:   inAmount
6
7 out:  out
8
9   
10 == TeX ==============================================================
11
12 The Rotator performs bitwise rotations of words.  When a value is
13 present at both {\tt in} and {\tt inAmount}, both values are consumed,
14 the value {\tt in} is rotated {\it towards the most significant bit}
15 by {\tt inAmount} bits and emitted at {\tt out}.
16
17 The output is undefined if {\tt inAmount} is greater than or equal to
18 the number of bits in a word.
19
20
21 == Fleeterpreter ====================================================
22
23 public void service() {
24   if (box_inAmount.dataReadyForShip() && box_in.dataReadyForShip() && box_out.readyForDataFromShip()) {
25     long amount = box_inAmount.removeDataForShip();
26     long data   = box_in.removeDataForShip();
27     long mask = ~((-1L) << getInterpreter().getWordWidth());
28     data = data & mask;
29     box_out.addDataFromShip(((data << amount) | (data >> (getInterpreter().getWordWidth()-amount))) & mask);
30   }
31 }
32
33
34 == FPGA ==============================================================
35
36   reg [(`DATAWIDTH-1):0] out_d;
37   assign out_d_ = out_d;
38
39   reg [5:0] shamt;
40   initial shamt = 0;
41
42   wire shamt_eq;
43   assign shamt_eq = (shamt[5:0] == (inAmount_d[5:0]));
44
45   always @(posedge clk) begin
46     if (!rst) begin
47       `reset
48     end else begin
49       if (!in_r        && in_a && !inAmount_r)       in_a        <= 0;
50       if (!inAmount_r  && inAmount_a) inAmount_a  <= 0;
51       if (out_r        && out_a)      out_r       <= 0;
52       if (in_r && !in_a && inAmount_r && !inAmount_a && !out_r && !out_a) begin
53         in_a  <= 1;
54         out_d <= in_d;
55         shamt <= 0;
56       end else if (in_a && inAmount_r && !inAmount_a && !out_r && !out_a) begin
57         if (!shamt_eq) begin
58            out_d <= { out_d[`DATAWIDTH-2:0], out_d[`DATAWIDTH-1] };
59            shamt <= shamt+1;
60         end else begin
61            inAmount_a <= 1;
62            out_r <= 1;
63         end
64       end
65     end
66   end
67
68 == Test ==============================================================
69
70
71 // expected output
72 #expect 2
73 #expect 44627559471
74
75
76 // ships required in order to run this code
77 #ship debug        : Debug
78 #ship rotator      : Rotator
79
80 rotator.in:         literal 1; deliver; literal 21615257152;  deliver;
81 rotator.inAmount:   literal 1; deliver; literal 20;           deliver;
82 rotator.out:        [*] take, sendto debug.in;
83 debug.in:           [*] take, deliver;
84
85
86 == Contributors =========================================================
87 Amir Kamil <kamil@cs.berkeley.edu>
88 Adam Megacz <megacz@cs.berkeley.edu>