Verilog计时和时钟-输入和输出问题

问题描述 投票:0回答:1

我已经用verilog编写了此代码,但是我的代码有问题!我想做的是输入数字0-127,以便可以将它们写入mem中,然后读取它们并查看输出。根据结果​​,我将时钟设置为每5个从0更改为1。但是这些数字在内存中多次被欺骗。我如何解决此问题,以便数字只能写入一次?可能是计时问题吗?

开始时输出数据为0,因为我需要先写入数据然后读取它们。

另外,我想知道我需要更改什么,以便当存储器的最后一个位置填充数字127时,我的程序将了解到,存储器的其他位置都不会留空!!我正在EDA Playground中运行代码。

module process_data(
  input wire [63:0] DATA_IN ,  //input data 
  input QUE_Mode,              //fifo or filo 
  //input wire [6:0] AdrR ,    //pointers for read, write
  //input wire [6:0] AdrW,  
  input R_W ,
  input Cen,  
  input clk,
  input reset, 
  output reg [63:0]Reg_Data_Out,  //output data
  output reg Que_Full,            //state of the queue
  output reg Que_Last, 
  output reg Que_Empty);

  integer i;
  reg [63:0] Memory [0:127];      //array 
  reg [6:0] AdrW;
  reg [6:0] AdrR;

  initial begin        //initialization 
   Que_Full= 1'b0;     //when  Que_Full = 1'b0, the queue is not full 
   Que_Last = 1'b0;    //when  Que_Last = 1'b0, this is not the last item of the list that can be added to 
   Que_Empty = 1'b0;   //when Que_Empty = 1'b0, the queue is empty.
   AdrR=7'b0000_000;
   AdrW=7'b0000_000;

   i=0;   
    repeat (128)        //initialization of memory 
    begin
        Memory[i]=64'd1;    //64 bits/ position , 127 positions 
        i=i+1;
    end
   end 

  always @* begin
    $display("AdrR=%d",AdrR);
    $display("AdrW=%d",AdrW);

  end 

  always @(negedge(clk))
  begin
    if(Cen == 1'b1) begin                        // cen = chip enabled , when 1=> chip is enabled => circuit works
    case (R_W)   
      1'b1:
         begin
           if(Que_Empty == 1'b1 )begin           //check if queue not empty
                                                 // Return the value from the FIFO  foun at the read address
              Reg_Data_Out <= Memory[AdrR];      // (read)  out put memory context          
              AdrR<=AdrR+7'b0000_001;            //assign  AdrR [6:0] <= AdrR [6:0] +7'b0000_001;   
           end 
         end 

      1'b0:
        begin 
          if( Que_Full == 1'b0  )begin               //check if queue not full 
            if(AdrW >= 7'b0000_000) begin
              Memory[AdrW] <= DATA_IN ;              // write input to memory , On any write -> update the memory
              AdrW <= AdrW+7'b0000_001;              //non blocking statements to avoid race conditions 
              Que_Empty = 1'b1;                      //when Que_Empty = 1'b1, the queue is NOT empty

              if(AdrW==7'b1111_111)begin  
                                                     //AdrW <= 7'b0000_000;
                 Que_Full= 1'b1;                     //when  Que_Full = 1'b1, the queue IS full 
                 Que_Last = 1'b1;                    //when  Que_Last = 1'b0, this IS the last item of the list that can be added to 
              end 
            end
          end 
        end 

      default:
        Reg_Data_Out = 64'bxxxxxxxx;  
    endcase

    end
  end 
endmodule 


module clock(output reg clk);
  initial
  clk=1'b0;
  always
  #5 clk=~clk;
endmodule


module TOP();
  parameter ENDTIME=40000;  

  reg [63:0] inputdata1;  // is an 63-bit "register", or variable  
  wire [6:0] AddressR,AddressW;

  reg cen, R_W, reset;  //clk=1'b0  , 
  reg QUE_Mode;

  wire [63:0] Data_Out;

  integer count;
  integer i;

  wire Que_Full, Que_Last, Que_Empty;

  wire clk;

  //call module for data I/O 
  process_data process_data(
    inputdata1,
    QUE_Mode,
    //AddressR,
    //AddressW, 
    R_W ,
    cen, 
    clk, 
    reset,
    Data_Out, 
    Que_Full,
    Que_Last,
    Que_Empty);  //reset, Que_Full, Que_Last, Que_Empty do not do anything yet

  clock MyClock(clk);
  initial
  begin
    $dumpfile("ALU.vcd");
    $dumpvars(0);

    $display("\t\t\t\t\t\t\t\t\tSIMULATION RESULT ");  
    $display("\t\ttime\tclk\t\t\tinputdata1\t\tData_Out\tQUE_Mode\t\tAddressR\t\tAddressW\t\tQue_Full\t\Que_Last\t\Que_Empty");
    $monitor($time, "\t%d\t%d\t%d\t%d",clk,inputdata1,Data_Out,QUE_Mode,AddressR,AddressW,Que_Full,Que_Last,Que_Empty);


    cen=1'b1;   //chip enabled 
    count = 0;
    R_W = 1'b0;   //write
    QUE_Mode = 1'b0; // QUE_Mode = 1'b0 => FIFO MODE 
    i=0;

    //input    
    for (i = 0; i < 128; i = i + 1) begin   //input   
                R_W=1'b0;   //write
                inputdata1 = i;
                #20;
           end  

    #10 
    for (i = 0; i < 128; i = i + 1) begin //output   
                #20; 
                R_W=1'b1;   //read  => output
                #20;
           end  
    $display("-------------- THE SIMULATION FINISHED ------------");  
    $finish; 
   end

endmodule


结果:

                            SIMULATION RESULT 
        time    clk         inputdata1      Data_Out    QUE_Mode        AddressR        AddressW         Que_Full   Que_Last    Que_Empty
AdrR=  0
AdrW=  0
AdrR=  0
AdrW=  1
                   0    0                      0                       x    0  z  z001
                   5    1                      0                       x    0  z  z001
AdrR=  0
AdrW=  2
                  10    0                      0                       x    0  z  z001
                  15    1                      0                       x    0  z  z001
AdrR=  0
AdrW=  3
                  20    0                      1                       x    0  z  z001
                  25    1                      1                       x    0  z  z001
AdrR=  0
AdrW=  4
                  30    0                      1                       x    0  z  z001
                  35    1                      1                       x    0  z  z001
AdrR=  0
AdrW=  5
                  40    0                      2                       x    0  z  z001
                  45    1                      2                       x    0  z  z001
AdrR=  0
AdrW=  6
                  50    0                      2                       x    0  z  z001
                  55    1                      2                       x    0  z  z001
AdrR=  0
AdrW=  7
                  60    0                      3                       x    0  z  z001
                  65    1                      3                       x    0  z  z001
AdrR=  0
AdrW=  8
                  70    0                      3                       x    0  z  z001
                  75    1                      3                       x    0  z  z001
AdrR=  0
AdrW=  9
                  80    0                      4                       x    0  z  z001
                  85    1                      4                       x    0  z  z001
AdrR=  0
AdrW= 10
                  90    0                      4                       x    0  z  z001
                  95    1                      4                       x    0  z  z001
AdrR=  0
AdrW= 11
                 100    0                      5                       x    0  z  z001
                 105    1                      5                       x    0  z  z001
AdrR=  0
AdrW= 12
                 110    0                      5                       x    0  z  z001
                 115    1                      5                       x    0  z  z001
AdrR=  0
AdrW= 13
                 120    0                      6                       x    0  z  z001
                 125    1                      6                       x    0  z  z001
AdrR=  0
AdrW= 14
                 130    0                      6                       x    0  z  z001
                 135    1                      6                       x    0  z  z001
......................


AdrR=121
AdrW=  0
                7630    0                    127                      60    0  z  z111
                7635    1                    127                      60    0  z  z111
AdrR=122
AdrW=  0
                7640    0                    127                      60    0  z  z111
                7645    1                    127                      60    0  z  z111
AdrR=123
AdrW=  0
                7650    0                    127                      61    0  z  z111
                7655    1                    127                      61    0  z  z111
AdrR=124
AdrW=  0
                7660    0                    127                      61    0  z  z111
                7665    1                    127                      61    0  z  z111
AdrR=125
AdrW=  0
                7670    0                    127                      62    0  z  z111
                7675    1                    127                      62    0  z  z111
AdrR=126
AdrW=  0
                7680    0                    127                      62    0  z  z111
                7685    1                    127                      62    0  z  z111
-------------- THE SIMULATION FINISHED ------------
AdrR=127
                7690    0                    127                      63    0  z  z111
Done


input verilog simulation clock
1个回答
1
投票

您需要快速两次更改输入数据。将#20更改为#10

    //input    
    for (i = 0; i < 128; i = i + 1) begin   //input   
                R_W=1'b0;   //write
                inputdata1 = i;
//                #20;
                #10;
           end  

这会将0写入地址0,将1写入地址1,...将127写入地址127。


日志文件在clk的每条边上显示输出,包括定格和忽略,因为clk语句中有$monitor。但是,每个时钟周期数据仅更改一次(根据需要)。

© www.soinside.com 2019 - 2024. All rights reserved.