正在尝试在Verilog中实现堆栈。什么';代码怎么了?

正在尝试在Verilog中实现堆栈。什么';代码怎么了?,verilog,fpga,xilinx,hdl,Verilog,Fpga,Xilinx,Hdl,我是Verilog新手,所以请原谅任何新手的错误。我试图在verilog中实现一个3字节的堆栈。使用R_W读写(push/pop)和2D数组存储堆栈内容 `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

我是Verilog新手,所以请原谅任何新手的错误。我试图在verilog中实现一个3字节的堆栈。使用R_W读写(push/pop)和2D数组存储堆栈内容

`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
两件事:

  • 您正在实现一个3字堆栈,而不是3字节堆栈
  • 我不确定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的逻辑有问题。我决定回答它。

    好的!这是正确的代码。它现在应该编译:

    `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
    

    感谢Renato的代码更正,但我已经改进了代码并使用了推送功能。以下是Verilog中的代码和测试台:

    模块:

    `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
    
    试验台

    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
    

    你的问题是什么?请签出。您缺少一个
    始终
    块。您可能还需要时钟和/或启用信号。是的。很抱歉我的意思是3个单词。tos代表栈顶。问题主要在于语法。我不熟悉这些数据类型以及Verilog的工作方式。很抱歉这篇需要的帖子,谢谢你的回答。那么如果是语法问题,编译器会给你什么错误消息呢?原始代码中的tos仍然存在问题。第18行:靠近“=”的语法错误--------------------第24行:靠近“=”的语法错误.------------------------------第16行:R_W不是常量-----------------第18行:tos是未知类型-----------------第19行:POP是未知类型第22行:R_W不是常量------第24行:tos是未知类型-----------------第25行:POP是未知类型-----------------第3行:模块被忽略由于以前的错误------------------。由于错误而忽略了Verilog文件------------------此外,Verilog不允许从阵列中选择切片,正如我在上面代码中所做的那样,这是仅在systemVerilog中可用的功能。我只希望有关于Verilog的系统指导教程。我正在寻找在virtex 5上执行的项目当堆栈为空并且想要读取其内容时会发生什么?堆栈溢出和下溢错误会发生什么情况?例如,不应该有
    错误
    行?