You can write a statement inside the combinational block like this:
always_comb
begin
z = 42;
if ( x == 0 )
z = some_value ;
else if ( y == 1 )
z = some_different_value ;
end
This statement before the if statement executes if none of the statements in the if statement are true. It is an alternative method of writing this:
always_comb
begin
if ( x == 0 )
z = some_value ;
else if ( y == 1 )
z = some_different_value ;
else
z = 42;
end
If you write statements in both the else block and before the if condition statement, then the value assigned in the else block will be reflected and the other value will be ignored.
Note: Ensure all possible conditions are covered in the if statement for combinational logic!
If you really need a latch use always_latch
, not always_comb
!
Regarding the concern of avoiding Xs in simulation, the best option is to declare the datatype as bit
instead of logic
. Doing so will initialise it to 0 since bit
is 2-state.
Note: While 2-state simulation is faster, it is not recommended since it can cover up failure to initialise the hardware at start-up.