convert many more test cases
[fleet.git] / ships / Stack.ship
1 ship: Stack
2
3 == Ports ===========================================================
4 data  in:   push
5 data  out:  pop
6
7 == Constants ========================================================
8
9 == TeX ==============================================================
10 A stack ship with capacity for at least 32 elements.
11
12 Push operations are executed as soon as an inbound datum is {\tt
13 deliver}ed on the {\tt push} port.  Completion of a push can be
14 confirmed by sending a {\tt notify} token from the {\tt push} port.
15
16 Pop operations are executed no earlier than the time at which the {\tt
17 pop} port attempts to {\tt take} data from the ship.
18
19 When the stack becomes full, it will simply not process any new {\tt
20 push} operations.  When the stack becomes empty, it will simply not
21 process any new {\tt pop} operations.
22
23 == Fleeterpreter ====================================================
24     private ArrayList<Long> stack = new ArrayList<Long>();
25     public void service() {
26         if (box_push.dataReadyForShip() && stack.size()<32) {
27             stack.add(box_push.removeDataForShip());
28         }
29         if (box_pop.readyForDataFromShip() && stack.size() > 0) {
30             box_pop.addDataFromShip(stack.get(stack.size()-1));
31             stack.remove(stack.size()-1);
32         }
33     }
34
35 == FleetSim ==============================================================
36 == FPGA ==============================================================
37 /* FIXME: inefficient */
38   reg    [(`DATAWIDTH-1):0] mem [4:0];
39   reg    [5:0]              depth;
40   reg    skip;
41   initial depth = 0;
42
43   always @(posedge clk) begin
44     skip = 0;
45     if (depth > 0) begin
46       `onwrite(pop_r, pop_a)
47         if (depth > 1) begin
48           pop_d <= mem[depth-2];
49         end
50         depth <= depth - 1;
51         skip = 1;
52       end
53     end
54     if (!skip && depth < 32) begin
55       `onread(push_r, push_a)
56         pop_d      <= push_d;
57         mem[depth] <= push_d;
58         depth      <= depth + 1;
59       end
60     end
61   end
62
63
64 == Test ====================================================
65 #ship stack : Stack
66 #ship debug : Debug
67
68 #expect 4
69 #expect 3
70 #expect 2
71 #expect 1
72 #expect 0
73
74 debug.in:  [*] take, deliver;
75
76 stack.pop: wait; [*] take, sendto debug.in;
77
78 stack.push:
79   literal 0; deliver;
80   literal 1; deliver;
81   literal 2; deliver;
82   literal 3; deliver;
83   literal 4; deliver;
84   notify stack.pop;
85
86
87
88
89 == Contributors =========================================================
90 Adam Megacz <megacz@cs.berkeley.edu>