overhaul of interpreter, update ships to match; "make test" works now
[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 least 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     long res = ((data >> amount) | (data << (getInterpreter().getWordWidth()-amount))) & mask;
30     box_out.addDataFromShip(res, (res & (1L << (getInterpreter().getWordWidth()-1)))!=0);
31   }
32 }
33
34
35 == FPGA ==============================================================
36
37   reg [(`WORDWIDTH):0] out_d;
38   assign out_d_ = out_d;
39
40   reg full;
41   initial full = 0;
42
43   reg [5:0] shamt;
44   initial shamt = 0;
45
46   wire shamt_eq;
47   assign shamt_eq = (shamt[5:0] == (inAmount_d[5:0]));
48
49   always @(posedge clk) begin
50     if (!rst) begin
51       `reset
52       full <= 0;
53     end else begin
54       `flush
55       `cleanup
56       if (`in_full && `inAmount_full && `out_empty) begin
57         if (!full) begin
58           out_d <= { 1'b0, in_d };
59           shamt <= 0;
60           full  <= 1;
61         end else if (!shamt_eq) begin
62           out_d <= { out_d[0], out_d[0], out_d[`WORDWIDTH-1:1] };
63           shamt <= shamt+1;
64         end else begin
65           `drain_in
66           `drain_inAmount
67           `fill_out
68           full <= 0;
69         end
70       end
71     end
72   end
73
74 == Test ==============================================================
75
76
77 // expected output
78 #expect 2
79 #expect 0
80 #expect -0x1000000000
81 #expect 1
82 #expect 44627559471
83 #expect 0
84
85
86 // ships required in order to run this code
87 #ship debug        : Debug
88 #ship rotator      : Rotator
89
90 debug.in: set ilc=*;  recv, deliver;
91
92 rotator.in:
93    set word=1;
94    deliver;
95    set word=1;
96    deliver;
97    set word=21615257152;
98    deliver;
99 rotator.inAmount:
100    set word=36;
101    deliver;
102    set word=1;
103    deliver;
104    set word=17;
105    deliver;
106 rotator.out:
107    set olc=3;
108    head;
109    [Rq] collect, send to debug.in;
110    [Rq] set flags a=c, b=b;
111    [Rq] [!a] set word=0;
112    [Rq] [a]  set word=1;
113    [Rq] send to debug.in;
114    tail;
115
116
117
118 == Contributors =========================================================
119 Amir Kamil <kamil@cs.berkeley.edu>
120 Adam Megacz <megacz@cs.berkeley.edu>