re-order instructions in test to avoid deadlock
[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   reg [`DATAWIDTH-1:0] temp;
30   initial temp   = {`DATAWIDTH{1'b1}};
31   reg     full;
32   initial full = 0;
33   wire    op_count;  assign op_count  = inOp_d==12;
34   wire    op_repeat; assign op_repeat = inOp_d[3:2]==0;
35   wire    op_pass;   assign op_pass   = inOp_d[3:2]==1;
36   wire    op_drop;   assign op_drop   = inOp_d[3:2]==2;
37   wire    op_c1;     assign op_c1     = (op_repeat || op_pass || op_drop) && !inOp_d[1];
38   wire    op_c2;     assign op_c2     = (op_repeat || op_pass || op_drop) &&  inOp_d[1];
39   wire    op_v1;     assign op_v1     = (op_repeat || op_pass || op_drop) && !inOp_d[0];
40   wire    op_v2;     assign op_v2     = (op_repeat || op_pass || op_drop) &&  inOp_d[0];
41   assign  out_d_ = op_v1 ? in1_d : op_v2 ? in2_d : temp;
42
43   // FIXME: REPEAT with a count of zero will not work properly
44
45   always @(posedge clk) begin
46     if (!rst) begin
47       `reset
48       full <= 0;
49     end else begin
50       `flush
51       if (!in1_r     &&  in1_a)              in1_a  <= 0;
52       if (!in2_r     &&  in2_a)              in2_a  <= 0;
53       if (!inOp_r    &&  inOp_a)             inOp_a <= 0;
54       if (!inOp_r    && !inOp_a)             full   <= 0;
55       if (out_r      && out_a) begin
56         out_r  <= 0;
57         if (op_count) temp   <= temp - in2_d;
58         else          temp   <= temp - 1;
59         if (op_pass && op_v1) in1_a <= 1;
60         if (op_pass && op_v2) in2_a <= 1;
61       end else if (inOp_r && !inOp_a) begin
62         if (!full) begin
63           if (op_count && in1_r && !in1_a && in2_r && !in2_a) begin
64             temp  <= in1_d[`DATAWIDTH-1:0] - in2_d[`DATAWIDTH-1:0];
65             in1_a <= 1;
66             full  <= 1;
67           end else if (op_c1 && in1_r && !in1_a) begin
68             temp  <= in1_d[`DATAWIDTH-1:0]-1;
69             in1_a <= 1;
70             full  <= 1;
71           end else if (op_c2 && in2_r && !in2_a) begin
72             temp  <= in2_d[`DATAWIDTH-1:0]-1;
73             in2_a <= 1;
74             full  <= 1;
75           end
76         end else if (temp[`DATAWIDTH-1]) begin
77           full <= 0;
78           inOp_a <= 1;
79           if (op_count) begin
80             in2_a <= 1;
81           end else if (op_repeat && op_v1) begin
82             in1_a <= 1;
83           end else if (op_repeat && op_v2) begin
84             in2_a <= 1;
85           end
86         end else if (!out_r && !out_a) begin
87           if (op_count) begin
88             out_r <= 1;
89           end else if (op_v1 && in1_r && !in1_a) begin
90             if (op_drop)    begin in1_a <= 1; temp <= temp-1; end
91             else            out_r <= 1;
92           end else if (op_v2 && in2_r && !in2_a) begin
93             if (op_drop)    begin in2_a <= 1; temp <= temp-1; end
94             else            out_r <= 1;
95           end
96         end
97       end
98     end
99   end
100
101 == Test =================================================================
102
103 #ship counter : Counter
104 #ship debug   : Debug
105
106 #expect 6
107 #expect 3
108 #expect 0
109 #expect 2
110 #expect 1
111 #expect 0
112 #expect 2
113 #expect 2
114 #expect 2
115 #expect 2
116 #expect 9
117 #expect 9
118 #expect 9
119
120 debug.in:
121   set ilc=*;
122   recv, deliver;
123
124 counter.in1:
125   set word=9;
126   deliver;
127   set word=3;
128   deliver;
129   set word=4;
130   deliver;
131   set word=3;
132   deliver;
133
134 counter.in2:
135   set word=3;
136   deliver;
137   set word=1;
138   deliver;
139   set word=2;
140   deliver;
141   set word=9;
142   deliver;
143
144 counter.inOp:
145   set word=12;
146   deliver;
147   deliver;
148   set word=1;
149   deliver;
150   deliver;
151
152 counter.out:
153   set ilc=*;
154   collect, send to debug.in;
155
156
157 == Contributors =========================================================
158 Adam Megacz <megacz@cs.berkeley.edu>