SparkyNZ
SparkyNZ

Reputation: 6676

Verilog function returning wrong value

Can somebody please help me understand why my function is not working as I'd like it to?

module TestBench2();
 
localparam SELECT_NONE   = 0;
localparam SELECT_RAM    = 1;
localparam SELECT_VGA    = 2;
localparam SELECT_UNKNOWN = 8;
 
logic iClk;  
logic reset;
 
logic [ 4 : 0 ] chipSelect;
logic [ 4 : 0 ] requestChipSelect;
logic [ 4 : 0 ] chipSelected;
 
logic oVGAInitReadRequest;
logic oVGALineComplete;
 
initial
begin
  reset = 1;
  oVGAInitReadRequest = 0;
  oVGALineComplete = 0;  
  chipSelected = SELECT_UNKNOWN;
end
 
function automatic logic [ 4 : 0 ] chipSelectFunc;
input requestChipSelect, oVGAInitReadRequest, oVGALineComplete;
 
  if( requestChipSelect == SELECT_VGA )
  begin
    $display( "  requestChipSelect == SELECT_VGA?  -> YES");
    chipSelectFunc = SELECT_VGA;
    chipSelected   = SELECT_VGA;
  end
  else
  if( oVGAInitReadRequest )
  begin
    $display( "  oVGAInitReadRequest?  -> YES");
    chipSelectFunc = SELECT_VGA;
    chipSelected   = SELECT_VGA;
  end
  else
  if( oVGALineComplete )
  begin
    $display( "  oVGALineComplete?  -> YES");
    chipSelectFunc = SELECT_NONE;
    chipSelected   = SELECT_NONE;
  end
  else
  if( requestChipSelect == SELECT_RAM )
  begin
    $display( "  requestChipSelect == SELECT_RAM?  -> YES");
    chipSelectFunc = SELECT_RAM;
    chipSelected   = SELECT_RAM;
  end
  else
  if( requestChipSelect == SELECT_NONE )
  begin
    $display( "  requestChipSelect == SELECT_NONE?  -> YES");
    chipSelectFunc = SELECT_NONE;
    chipSelected   = SELECT_NONE;
  end
  else
  begin
    $display( "requestChipSelect -> STAY SAME");
    // Leave it as-is
    chipSelectFunc = chipSelected;
  end
 
endfunction
 
assign chipSelect = chipSelectFunc( requestChipSelect, oVGAInitReadRequest, oVGALineComplete );
 
integer count = 0;
 
always @( posedge iClk )
begin
  reset = 0;
 
  case( count )
     1: begin
          $strobe ("%2d:requestChipSelect <= SELECT_VGA", count);  
          requestChipSelect <= SELECT_VGA;
        end
 
     2: begin
          $strobe ("%2d:requestChipSelect <= SELECT_NONE", count);
          requestChipSelect <= SELECT_NONE;
        end      
 
     default: begin
          $strobe ("%2d:Default case", count);
        end
  endcase
 
  $strobe ("%2d:chipSelect: %d ", count, chipSelect);  
  count ++;
end
 
endmodule

When I run the above code, I get this as the output:

# requestChipSelect -> STAY SAME
#  1:Default case
#  1:chipSelect:  8 
#   requestChipSelect == SELECT_NONE?  -> YES
#  2:requestChipSelect <= SELECT_VGA
#  2:chipSelect:  0 
#   requestChipSelect == SELECT_NONE?  -> YES

Why is it that when requestChipSelect is set to SELECT_VGA (2), my function always sees/treats this as 0?

Upvotes: 0

Views: 286

Answers (1)

Matthew
Matthew

Reputation: 13967

Because you have declared requestChipSelect to be 1 bit wide:

function automatic logic [ 4 : 0 ] chipSelectFunc;
input requestChipSelect, oVGAInitReadRequest, oVGALineComplete;

instead of (eg) 5, like your return value:

function automatic logic [ 4 : 0 ] chipSelectFunc;
input [ 4 : 0 ] requestChipSelect;       
input oVGAInitReadRequest, oVGALineComplete;

Upvotes: 3

Related Questions