f82bc14300b510bd1db1d1f42f95a7fc668c92f4
[fleet.git] / ships / Counter.ship
1 ship: Counter
2
3 == Ports ===========================================================
4 data  in:   in1
5 data  in:   in2
6 data  in:   inOp
7   constant REPEAT_C1_V1: 0
8   constant REPEAT_C1_V2: 1
9   constant REPEAT_C2_V1: 2
10   constant REPEAT_C2_V2: 3
11   constant PASS_C1_V1:   4
12   constant PASS_C1_V2:   5
13   constant PASS_C2_V1:   6
14   constant PASS_C2_V2:   7
15   constant DROP_C1_V1:   8
16   constant DROP_C1_V2:   9
17   constant DROP_C2_V1:   10
18   constant DROP_C2_V2:   11
19   constant COUNT:        12
20 data  out:  out
21
22 == Fleeterpreter ====================================================
23 public void service() { }
24
25 == FleetSim ==============================================================
26
27 == FPGA ==============================================================
28
29   wire [3:0]              inOp_d_trunc;
30   assign                  inOp_d_trunc = inOp_d[3:0];
31
32   reg [`WORDWIDTH-1:0] temp;
33   initial temp   = {`WORDWIDTH{1'b1}};
34   reg     out_draining;
35   reg     full;
36   initial full = 0;
37   wire    op_count;  assign op_count  = inOp_d_trunc==12;
38   wire    op_repeat; assign op_repeat = inOp_d[3:2]==0;
39   wire    op_pass;   assign op_pass   = inOp_d[3:2]==1;
40   wire    op_drop;   assign op_drop   = inOp_d[3:2]==2;
41   wire    op_c1;     assign op_c1     = (op_repeat || op_pass || op_drop) && !inOp_d[1];
42   wire    op_c2;     assign op_c2     = (op_repeat || op_pass || op_drop) &&  inOp_d[1];
43   wire    op_v1;     assign op_v1     = (op_repeat || op_pass || op_drop) && !inOp_d[0];
44   wire    op_v2;     assign op_v2     = (op_repeat || op_pass || op_drop) &&  inOp_d[0];
45   assign  out_d_ = op_v1 ? in1_d : op_v2 ? in2_d : temp;
46
47   // FIXME: REPEAT with a count of zero will not work properly
48
49   always @(posedge clk) begin
50     if (!rst) begin
51       `reset
52       full <= 0;
53       out_draining <= 0;
54     end else begin
55       `flush
56       `cleanup
57       if (`inOp_empty)         full   <= 0;
58       if (out_draining && `out_empty) begin
59         if (op_count) temp   <= temp - in2_d;
60         else          temp   <= temp - 1;
61         if (op_pass && op_v1) `drain_in1
62         if (op_pass && op_v2) `drain_in2
63         out_draining <= 0;
64       end else if (`inOp_full) begin
65         if (!full) begin
66           if (op_count && `in1_full && `in2_full) begin
67             temp  <= in1_d[`WORDWIDTH-1:0] - in2_d[`WORDWIDTH-1:0];
68             `drain_in1
69             full  <= 1;
70           end else if (op_c1 && `in1_full) begin
71             temp  <= in1_d[`WORDWIDTH-1:0]-1;
72             `drain_in1
73             full  <= 1;
74           end else if (op_c2 && `in2_full) begin
75             temp  <= in2_d[`WORDWIDTH-1:0]-1;
76             `drain_in2
77             full  <= 1;
78           end
79         end else if (temp[`WORDWIDTH-1]) begin
80           full <= 0;
81           `drain_inOp
82           if (op_count) begin
83             `drain_in2
84           end else if (op_repeat && op_v1) begin
85             `drain_in1
86           end else if (op_repeat && op_v2) begin
87             `drain_in2
88           end
89         end else if (`out_empty) begin
90           if (op_count) begin
91             `fill_out
92             out_draining <= 1;
93           end else if (op_v1 && `in1_full) begin
94             if (op_drop)    begin `drain_in1 temp <= temp-1; end
95             else            begin `fill_out out_draining <= 1; end
96           end else if (op_v2 && `in2_full) begin
97             if (op_drop)    begin `drain_in2 temp <= temp-1; end
98             else            begin `fill_out out_draining <= 1; end
99           end
100         end
101       end
102     end
103   end
104
105 == Test =================================================================
106
107 #ship counter : Counter
108 #ship debug   : Debug
109
110 #expect 6
111 #expect 3
112 #expect 0
113 #expect 2
114 #expect 1
115 #expect 0
116 #expect 2
117 #expect 2
118 #expect 2
119 #expect 2
120 #expect 9
121 #expect 9
122 #expect 9
123
124 debug.in:
125   set ilc=*;
126   recv, deliver;
127
128 counter.in1:
129   set word=9;
130   deliver;
131   set word=3;
132   deliver;
133   set word=4;
134   deliver;
135   set word=3;
136   deliver;
137
138 counter.in2:
139   set word=3;
140   deliver;
141   set word=1;
142   deliver;
143   set word=2;
144   deliver;
145   set word=9;
146   deliver;
147
148 counter.inOp:
149   set word=12;
150   deliver;
151   deliver;
152   set word=1;
153   deliver;
154   deliver;
155
156 counter.out:
157   set ilc=*;
158   collect, send to debug.in;
159
160
161 == Contributors =========================================================
162 Adam Megacz <megacz@cs.berkeley.edu>