2020-06-04 17:03:46 +00:00
|
|
|
module LIFOstack (Din, clk, en, rst, rw, Dout, empty, full);
|
|
|
|
|
|
|
|
input [15:0] Din; // Data being fed to stack
|
|
|
|
input clk; // clock signal input
|
|
|
|
input en; // disable stack when not in use
|
2020-06-07 14:08:34 +00:00
|
|
|
input rst; // reset pin to clear and reinitialise stack (active high)
|
2020-06-04 17:03:46 +00:00
|
|
|
input rw; // 0: read, 1: write
|
|
|
|
|
|
|
|
output reg [15:0] Dout; // Data being pulled from stack
|
|
|
|
output reg empty; // goes high to indicate SP is at 0
|
|
|
|
output reg full; // goes high to indicate SP is at (slots)
|
|
|
|
|
2020-06-07 19:05:46 +00:00
|
|
|
reg [5:0] SP; // Points to slot to save next value to
|
2020-06-04 17:03:46 +00:00
|
|
|
integer i;
|
2020-06-07 19:05:46 +00:00
|
|
|
reg [15:0] mem [31:0];
|
2020-06-04 17:03:46 +00:00
|
|
|
|
|
|
|
always @ (posedge clk) begin
|
|
|
|
if (!en); // if not enabled, ignore this cycle
|
|
|
|
else begin
|
|
|
|
if (rst) begin // if rst is high, clear memory and reset pointers/outputs
|
2020-06-07 14:08:34 +00:00
|
|
|
Dout = 16'h0000;
|
2020-06-07 19:05:46 +00:00
|
|
|
SP = 6'b000000;
|
2020-06-04 17:03:46 +00:00
|
|
|
empty = 1'b1;
|
2020-06-07 19:05:46 +00:00
|
|
|
for (i = 0; i < 32; i = i + 1) begin
|
2020-06-07 14:08:34 +00:00
|
|
|
mem[i] = 16'h0000;
|
2020-06-04 17:03:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else begin
|
|
|
|
if (!full && rw) begin // Write when NOT full & Writing
|
2020-06-07 14:08:34 +00:00
|
|
|
mem[SP] = Din; // Store data into current slot
|
2020-06-04 17:03:46 +00:00
|
|
|
SP = SP + 1'b1; // Increment stack pointer to next empty slot
|
2020-06-07 19:05:46 +00:00
|
|
|
full = (SP == 6'b100000) ? 1 : 0; // Stack is full if SP is (slots)
|
2020-06-04 17:03:46 +00:00
|
|
|
empty = 1'b0; // Stack is never empty after a push
|
|
|
|
end
|
|
|
|
else if (!empty && !rw) begin // Read when NOT empty & reading
|
|
|
|
SP = SP - 1'b1; // Decrement stack pointer to last filled slot
|
2020-06-07 14:08:34 +00:00
|
|
|
Dout = mem[SP]; // Output data from last filled slot
|
|
|
|
mem[SP] = 16'h0000; // Clear slot after setting output
|
2020-06-04 17:03:46 +00:00
|
|
|
full = 1'b0; // Stack is never full after a pop
|
2020-06-07 19:05:46 +00:00
|
|
|
empty = (SP == 6'b000000) ? 1 : 0; // Stack is empty if SP is 0
|
2020-06-04 17:03:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
endmodule
|