'Sync RAM related

I am trying to simulate the following code for an synchronous ram in Verilog.

When I am trying to write in a specific address, dataOut is not coming the way I was expecting. It is skipping 1 address while reading data in the specified address.

enter image description here

Can anyone tell me the problem?

Here is GtkWave simulation result:

module ram 
  # (parameter ADDR_WIDTH = 4,
     parameter DATA_WIDTH = 10,
     parameter DEPTH = 1 <<  ADDR_WIDTH
    )
  
  (     input                   clk,
        input [ADDR_WIDTH-1:0]  addr,
        input [DATA_WIDTH-1:0]  dataIn,
        input                   enable,
        input                   write,
        input                   read,
      input           resetRam, 
      output[DATA_WIDTH-1:0]  dataOut  
  );
  
  reg [DATA_WIDTH-1:0]  tmp_data;
  reg [DATA_WIDTH-1:0]  mem [DEPTH-1:0];

  //reset 
  always @(posedge clk) begin 
    if(enable & resetRam) begin
      for (integer i = 0; i < 2**ADDR_WIDTH; i = i + 1)
      begin
        mem[i] = {DATA_WIDTH{1'b0}};
      end
    end
  end
  
  //write
  always @ (posedge clk) begin
    if (enable & write & !read)
      mem[addr] <= dataIn;
  end
  
  //read
  always @ (posedge clk) begin
    if (enable & !write & read)
        tmp_data <= mem[addr];
  end
  
  assign dataOut = enable & read & !write ? tmp_data : 'hz || enable & resetRam ? 10'b0 : 'hz;
  
endmodule

module ram_tb;
  parameter ADDR_WIDTH = 4;
  parameter DATA_WIDTH = 10;
  parameter DEPTH = 1 << ADDR_WIDTH;
  
  reg clk;
  reg enable;
  reg write;
  reg read;
  reg resetRam;
  reg [ADDR_WIDTH-1:0] addr;
  wire [DATA_WIDTH-1:0] dataIn;
  wire [DATA_WIDTH-1:0] dataOut;
  reg [DATA_WIDTH-1:0] tb_data;
  
  ram #(.DATA_WIDTH(DATA_WIDTH)) iram(
                          .clk(clk),
                            .addr(addr),
                            .dataIn(dataIn),
                            .enable(enable),
                                      .write(write),
                                      .read(read),
                          .resetRam(resetRam),
                          .dataOut(dataOut)
                         );
  
  
  always #10 clk = ~clk;
  assign dataIn = !read ? tb_data : 'hz ;
  
  
  initial begin
    $monitor("addrs = %b resetRam=%b Write = %b  read=%b dataIn = %b dataOut = %b", addr,resetRam,write,read,dataIn,dataOut);
    $dumpfile("ram_tb.vcd");
    $dumpvars(0,ram_tb);

    {clk, enable, write, addr, tb_data, read,resetRam} <= 0;
    


    // //writing all the adrress with random value
    // for (integer i = 0; i < DEPTH; i= i+1) begin
    //  @(posedge clk) addr <= i; write = 1; enable =1; read = 0; tb_data <= $random;
    // end


    // //reading them
    // for (integer i = 0; i < DEPTH; i= i+1) begin
    //  @(posedge clk) addr = i; write = 0; enable = 1; read = 1;
    // end


     //Writing at specific address
    @(posedge clk) addr = 4'b1000; write = 1; enable =1; read = 0; tb_data <= $random;
    @(posedge clk) addr = 4'b1111; write = 1; enable =1; read = 0; tb_data <= $random;
    @(posedge clk) addr = 4'b1001; write = 1; enable =1; read = 0; tb_data <= $random;
    @(posedge clk) addr = 4'b0001; write = 1; enable =1; read = 0; tb_data <= $random;  

    

    //reading them
    @(posedge clk) addr = 4'b1000;write = 0; enable =1; read = 1;
    @(posedge clk) addr = 4'b1111;write = 0; enable =1; read = 1;
    @(posedge clk) addr = 4'b1001;write = 0; enable =1; read = 1;
    @(posedge clk) addr = 4'b0001;write = 0; enable =1; read = 1;
    

    

    // //reset memory
    // for (integer i = 0; i < DEPTH; i= i+1) begin
    //  repeat (1) @(posedge clk) addr <= i; enable <= 1; resetRam <=1;
    // end


    
    #200 $finish;
  end
endmodule

//iverilog -o ram_tb.vpp ram.v
//vvp ram_tb.vpp


Solution 1:[1]

If whitelisting addresses doesn't do the trick, I believe you can do the following to the oracle contract:


    /**
     * @notice Creates the Chainlink request. This is a backwards compatible API
     * with the Oracle.sol contract, but the behavior changes because
     * callbackAddress is assumed to be the same as the request sender.
     * @param callbackAddress The consumer of the request
     * @param payment The amount of payment given (specified in wei)
     * @param specId The Job Specification ID
     * @param callbackAddress The address the oracle data will be sent to
     * @param callbackFunctionId The callback function ID for the response
     * @param nonce The nonce sent by the requester
     * @param dataVersion The specified data version
     * @param data The extra request parameters
     */
    function oracleRequest(
        address sender,
        uint256 payment,
        bytes32 specId,
        address callbackAddress,
        bytes4 callbackFunctionId,
        uint256 nonce,
        uint256 dataVersion,
        bytes calldata data
    ) external override validateFromLINK validateIsAuthorizedConsumer(sender) {
        revert("use the operatorRequest only");
    }

    /**
     * @notice Creates the Chainlink request
     * @dev Stores the hash of the params as the on-chain commitment for the request.
     * Emits OracleRequest event for the Chainlink node to detect.
     * @param sender The sender of the request
     * @param payment The amount of payment given (specified in wei)
     * @param specId The Job Specification ID
     * @param callbackFunctionId The callback function ID for the response
     * @param nonce The nonce sent by the requester
     * @param dataVersion The specified data version
     * @param data The extra request parameters
     */
    function operatorRequest(
        address sender,
        uint256 payment,
        bytes32 specId,
        bytes4 callbackFunctionId,
        uint256 nonce,
        uint256 dataVersion,
        bytes calldata data
    ) external override validateIsAuthorizedConsumer(sender) validateFromLINK {
        (
            bytes32 requestId,
            uint256 expiration
        ) = _verifyAndProcessOracleRequest(
                sender,
                payment,
                sender,
                callbackFunctionId,
                nonce,
                dataVersion
            );
        emit OracleRequest(
            specId,
            sender,
            requestId,
            payment,
            sender,
            callbackFunctionId,
            expiration,
            dataVersion,
            data
        );
    }

I removed the oracleRequest() function because it exceeds stack size by adding more modifiers to it and since I can use operatorRequest() to fulfill both multi-word and single-word requests I will make that method deprecated by reverting everytime it's called.

The modifier that sets the authorized consumer is basically the following:

    /**
     * @dev function used to change the authorized consumer. Can only be set once
     */
    function setAuthorizedConsumer(address _consumer) public onlyOwner {
        require(
            authorizedConsumer == address(0),
            "authorized consumer is already set"
        );
        authorizedConsumer = _consumer;
    }

    /**
     * @notice validates the consumer is an authorized consumer
     */
    function _validateIsAuthorizedConsumer(address _consumer) internal view {
        require(_consumer == authorizedConsumer, "Not authorized sender");
    }

    /**
     * @notice prevents non-authorized addresses from calling this method
     */
    modifier validateIsAuthorizedConsumer(address _consumer) {
        _validateIsAuthorizedConsumer(_consumer);
        _;
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pedro Henrique Bufulin