refactor codebag-memory-block creation 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   wire [`DATAWIDTH*2-1:0] double;
37   assign double = { in_d[`DATAWIDTH-1:0], in_d[`DATAWIDTH-1:0] };
38
39   always @(posedge clk) begin
40     if (!rst) begin
41       `reset
42     end else begin
43       if (!in_r        && in_a)       in_a    <= 0;
44       if (!inAmount_r  && inAmount_a) inAmount_a  <= 0;
45       if (out_r        && out_a)      out_r   <= 0;
46       if (in_r && !in_a && inAmount_r && !inAmount_a && !out_r && !out_a) begin
47         in_a <= 1;
48         inAmount_a <= 1;
49         out_r <= 1;
50         out_d <= double >> (`DATAWIDTH - inAmount_d);
51       end
52     end
53   end
54
55 == Test ==============================================================
56
57
58 // expected output
59 #expect 2
60 #expect 44627559471
61
62
63 // ships required in order to run this code
64 #ship debug        : Debug
65 #ship rotator      : Rotator
66
67 rotator.in:         literal 1; deliver; literal 21615257152;  deliver;
68 rotator.inAmount:   literal 1; deliver; literal 20;           deliver;
69 rotator.out:        [*] take, sendto debug.in;
70 debug.in:           [*] take, deliver;
71
72
73 == Contributors =========================================================
74 Amir Kamil <kamil@cs.berkeley.edu>
75 Adam Megacz <megacz@cs.berkeley.edu>