Packaging up code using functions
Often, we'll have code that we will be reusing within the same module or that's common to a group of modules. We can package this code up in a function:
function [4:0] func_addr_decode(input [31:0] addr); func_addr_decode = '0; for (int i = 0; i < 32; i++) begin if (addr[i]) begin return(i); end end endfunction
Here, we created a function
called func_addr_decode
that returns a 5-bit value. function
takes a 32-bit input called address. Functions can have multiple outputs, but we will not be using this feature. To return the function's value, you can assign the result to the function name or use the return
statement.
Creating combinational logic
The two main ways of creating logic are via assign
statements and always
blocks. assign
statements are convenient when creating purely combinational logic...