Functions with assign statements

I’m trying to use a function to do the same as this line (which is commented out in the below testbench):

assign chipSelect = ( reset ) ? SELECT_NONE : SELECT_VGA;

The expected output for 3 clock cycles is:

# TestBench2 : time                    0 chipSelect: 0 
# TestBench2 : time                20000 chipSelect: 2 
# TestBench2 : time                40000 chipSelect: 2

But when I run the below testbench using a function, I get this:

# TestBench2 : time                    0 chipSelect: 0 
# TestBench2 : time                20000 chipSelect: 0 
# TestBench2 : time                40000 chipSelect: 0

Why won’t my function behave as expected? There must be something fundamental I am missing. I realise there is no point in using a function for this trivial example but I’d like to know why it won’t work anyway - I was hoping to use a function in a more complicated use case.

module TestBench2();

logic iClk;  

localparam SELECT_NONE   = 0;
localparam SELECT_VGA    = 2;

logic [ 2 : 0 ] chipSelect;
logic reset;

initial
begin
  reset = 1;
end
 
function automatic logic [ 2 : 0 ] chipSelectFunc();
  if( reset )
  begin
    chipSelectFunc = SELECT_NONE;
  end
  else
  begin
    chipSelectFunc = SELECT_VGA; 
  end
endfunction 
 
assign chipSelect = chipSelectFunc();
//assign chipSelect = ( reset ) ? SELECT_NONE : SELECT_VGA;  // This works as expected

always @( posedge iClk )
begin

  $display ("%m : time %t chipSelect: %d ", $time, chipSelect);
  reset = 0;
end

endmodule

In reply to SparkyNZ:

Functions are declared within a module, and can be called from continuous assignments, always blocks or other functions. In a continuous assignment, they are evaluated when any of its declared inputs change__. In a procedure, they are evaluated when invoked.

OK, I guess this is where I went wrong.

In reply to SparkyNZ:

Note that the always_comb feature of SystemVerilog addresses this issue by expanding (or inlining) the function call to include the sensitivity from statements inside the function.

always_comb chipSelect = chipSelectFunc();

This gives you the behavior you are expecting.

In reply to dave_59:

Thanks Dave