我是Verilog的新手,所以请原谅任何新手的错误。我试图在verilog中实现一个3字节的堆栈。使用R_W读取写入(push / pop)和2D Array来存储堆栈的内容。
`timescale 1ns / 1ps
module one(R_W,PUSH,POP);
input PUSH;
input R_W;
output POP;
wire [31:0] PUSH;
reg [31:0] POP;
wire R_W;
reg [31:0] temp[0:3];
integer tos = 3;
if(R_W == 1 && tos != 3)
begin
POP = temp[tos];
end
if(R_W == 0 && tos != 0)
begin
tos = tos +1;
POP = temp[tos];
end
endmodule
测试台
`include"one.v"
module one_test();
wire pop;
reg [31:0] push;
wire r_w;
initial begin
push = 2'd12;
r_w = 0;
#10
$finish;
end
one one(r_w,push,pop);
endmodule
答案 0 :(得分:1)
感谢代码修正Renato,但我改进了代码并使用了PUSH功能。下面是代码以及Verilog中的Test Bench:
<强> MODULE:强>
`timescale 1ns / 1ps
module some(clk,R_W,PUSH,POP);
input clk;
input [31:0] PUSH;
input R_W;
output [31:0] POP;
wire [31:0] PUSH;
reg [31:0] POP;
wire R_W;
reg [31:0] temp[0:3];
integer tos = 3;
always @ (posedge clk)
if(R_W == 1 && tos != 3)
begin
POP = temp[tos];
tos = tos + 1;
end
else if(R_W == 0 && tos != 0)
begin
temp[tos] = PUSH;
tos = tos - 1;
end
endmodule
TEST BENCH
module some_test();
reg clk;
reg r_w;
integer push;
wire [31:0] pop;
always begin
#1 clk = !clk;
end
initial begin
clk = 0;
#1 r_w = 0;
#1 push = 'd9;
#10
$finish;
end
some some(clk,r_w,push,pop);
endmodule
答案 1 :(得分:0)
两件事:
我不确定 tos 是什么,但是假设它是多少项目留在堆栈中,那么你有两个错误。这是正确的代码
`timescale 1ns / 1ps
module one(R_W,PUSH,POP);
input PUSH;
input R_W;
output POP;
wire [31:0] PUSH;
reg [31:0] POP;
wire R_W;
reg [31:0] temp[0:3];
integer tos = 3;
if(R_W == 1 && tos != 3)
begin
tos = tos + 1;
POP = temp[tos];
end
if(R_W == 0 && tos != 0)
begin
tos = tos - 1;
POP = temp[tos];
end
endmodule
我不确定语法,因为我自己更像是一个VHDL人。但是因为tos逻辑出了问题。我决定回答它。
答案 2 :(得分:0)
确定!这是更正后的代码。它现在应该编译:
`timescale 1ns / 1ps
module one(clk,R_W,PUSH,POP);
input clk;
input [31:0] PUSH;
input R_W;
output [31:0] POP;
//wire [31:0] PUSH;
reg [31:0] POP;
wire R_W;
reg [31:0] temp[0:3];
integer tos = 3;
always @ (posedge clk)
if(R_W == 1 && tos != 3)
begin
POP = temp[tos];
tos = tos + 1;
end
else if(R_W == 0 && tos != 0)
begin
POP = temp[tos];
tos = tos - 1;
end
endmodule
有一个主要区别。我不确定你的设计是否需要与时钟同步。通常在堆栈操作中,您需要这样做。但是如果你想在没有时钟的情况下进行组合练习,可以使用以下代码:
`timescale 1ns / 1ps
module one(R_W,PUSH,POP);
input [31:0] PUSH;
input R_W;
output [31:0] POP;
//wire [31:0] PUSH;
reg [31:0] POP;
wire R_W;
reg [31:0] temp[0:3];
integer tos = 3;
always @ (R_W, tos)
if(R_W == 1 && tos != 3)
begin
POP = temp[tos];
tos = tos + 1;
end
else if(R_W == 0 && tos != 0)
begin
POP = temp[tos];
tos = tos - 1;
end
endmodule