add stack

This commit is contained in:
Jose Luis Montañes Ojados
2026-03-01 04:28:56 +01:00
parent 42811f868b
commit 6df7024707
3 changed files with 97 additions and 1 deletions

View File

@@ -353,7 +353,7 @@ gtkwave modulo.vcd
- [x] Fase 2.4 — Register File
- [x] Fase 3.1 — RAM sincrona
- [x] Fase 3.2 — ROM
- [ ] Fase 3.3 — Stack
- [x] Fase 3.3 — Stack
- [ ] Fase 4.1 — FSM Semaforo
- [ ] Fase 4.2 — UART TX
- [ ] Fase 5.1 — Fetch Unit

33
stack/stack.v Normal file
View File

@@ -0,0 +1,33 @@
/*
Stack
*/
module stack #(
parameter DEPTH = 256,
parameter W = 16
) (
input clk,
input rst,
input push,
input pop,
input [W-1:0] value,
output reg [W-1:0] out
);
reg [W-1:0] mem [0:DEPTH-1];
reg [$clog2(DEPTH):0] sp; // bit extra para detectar overflow
wire full = (sp == DEPTH);
wire empty = (sp == 0);
always @(posedge clk or posedge rst) begin
if (rst) begin
sp <= 0;
end else if (push && !full) begin
mem[sp] <= value;
sp <= sp + 1;
end else if (pop && !empty) begin
out <= mem[sp - 1];
sp <= sp - 1;
end
end
endmodule

63
stack/stack_tb.v Normal file
View File

@@ -0,0 +1,63 @@
/*
Stack testbench
*/
`include "stack/stack.v"
module stack_tb;
reg clk, rst, push, pop;
reg[15:0] in;
wire [15:0] out;
stack stack1(
.clk(clk),
.rst(rst),
.push(push),
.pop(pop),
.value(in),
.out(out)
);
initial clk = 0;
always #5 clk = ~clk;
initial begin
$dumpfile("stack/stack.vcd");
$dumpvars(0, stack_tb);
rst = 1; push = 0; pop = 0; in = 0;
@(posedge clk); #1
rst = 0;
$display("push=%b, pop=%b, value=%x, out=%x", push, pop, in, out);
push = 1; pop = 0; in = 69;
@(posedge clk); #1
$display("push=%b, pop=%b, value=%x, out=%x", push, pop, in, out);
push = 1; pop = 0; in = 40;
@(posedge clk); #1
$display("push=%b, pop=%b, value=%x, out=%x", push, pop, in, out);
push = 0; pop = 0; in = 0;
@(posedge clk); #1
$display("push=%b, pop=%b, value=%x, out=%x", push, pop, in, out);
push = 0; pop = 1; in = 0;
@(posedge clk); #1
$display("push=%b, pop=%b, value=%x, out=%x", push, pop, in, out);
push = 0; pop = 0; in = 0;
@(posedge clk); #1
$display("push=%b, pop=%b, value=%x, out=%x", push, pop, in, out);
push = 0; pop = 1; in = 0;
@(posedge clk); #1
$display("push=%b, pop=%b, value=%x, out=%x", push, pop, in, out);
push = 0; pop = 0; in = 0;
@(posedge clk); #1
$display("push=%b, pop=%b, value=%x, out=%x", push, pop, in, out);
$finish;
end
endmodule