-2

实际上这是我的大学项目,如果有人可以提供帮助,我需要如何编写 Verilog 代码?我需要 Verilog 代码.. 谢谢

项目声明:

该项目的目的是添加“完成”信号并删除“按钮”以增加程序计数器。程序计数器将自动递增直到程序结束,当完成信号变为高电平时将停止。查看评论以获取更多详细信息。

程序计数器的 Verilog 代码如下:

module counter_pushbutton(clk, rst, push_button, pc);
input clk, rst, push_button;
output [15:0] pc;

reg [15:0] pc;
reg [15:0] pc_r;


reg push_button_r, pc_en;

always @  (posedge clk or posedge rst)

      begin
          if ( rst )
              push_button_r <= 1'b0;
          else
              push_button_r <= push_button;
      end

always@*

  pc_en <= push_button & ~ push_button;

always@    (posedge pc_en or posedge rst)

  begin
      if (rst)
          pc_r <= 1'b0;
      else 
          pc_r <= pc+1;
  end

assign pc = pc_r;

端模块

点击查看图片

4

1 回答 1

0

这是一个代码,其中 PC 将在到达“pc_stop”(输入端口)时停止,pc_r 和 pc_stop 之间存在相等的比较,只有当“done”为“0”时 pc_r 才会增加:

module counter_pc (/*AUTOARG*/
   // Outputs
   pc, done,
   // Inputs
   clk, rst, pc_stop
   );

  input           clk;
  input           rst;
  input   [15:0]  pc_stop;
  output  [15:0]  pc;
  output          done;

  reg [15:0] pc_r;

  assign done = pc_r == pc_stop;

  always @ (posedge clk or posedge rst) begin
    if(rst)
      pc_r <= 16'd0;
    else begin
      if(~done)
        pc_r <= pc_r + 16'd1;
    end
  end

  assign pc = pc_r;

endmodule // counter_pc

您可以使用以下方法对其进行测试:

module counter_pc_tb ();

  //..local parameters
  localparam PC_STOP = 511;

  //..regs and wires
  reg           clk;
  reg           rst;
  wire  [15:0]  pc;
  wire          done;
  wire          reached_max_pc;
  wire  [15:0]  pc_stop = PC_STOP;

  //..initialization
  initial begin
    clk = 0;
    rst = 1;
    $dumpfile("counter_pc.vcd");
    $dumpvars();
    $display("[+] Starting simulation");
    $monitor("  [+] PC: %d", pc);
  end

  //..clk signal
  always
    #1 clk = ~clk;

  //..rst signal
  always
    #10 rst = 0;

  //..pc timeout
  assign reached_max_pc = &pc;

  //..finish simulation
  wire finished_sim = reached_max_pc | done;

  //..simulation
  always @ (posedge clk) begin
    if(finished_sim) begin
      if(done)
        $display("[!] Reached PC: %d ==> Stop!", pc_stop);
      else
        $display("[!] Reached maximum PC ==> Timeout!");
      $display("[+] Ending simulation");
      $finish;
    end
  end

  //..dut
  counter_pc
    dut (
      .clk          (clk),
      .rst          (rst),
      .pc_stop      (pc_stop),
      .pc           (pc),
      .done         (done)
    );

endmodule // counter_pc_tb
于 2020-06-09T17:58:57.660 回答