When writing a function in SCode, you can either pass an output logic signal as a parameter or create the logic within the function and return it.
func(clk, reset, inlogics, out_logic)out_logic <= func(clk, reset, inlogics)
SCode supports both methods, but the second approach—returning the logic—is highly recommended.
In fact, all library functions provided by SCode are implemented using this second method. It offers better readability, eliminates the need to declare the output logic beforehand, and allows you to take advantage of SCode’s automatic declaration feature when assigning the returned value.
For example, consider a function count_nbit that counts from 0 up to a specified number of bits:
def count_nbit(clk, reset, nbit):
cnt = logic_unique("cnt[%s]" % nbit)
with sequence(clk):
cnt <= (0, reset, cnt + 1)
return cnt
To create 4-bit and 5-bit counters using this function:
cnt1 <= count_nbit(clk, reset, 4)
cnt2 <= count_nbit(clk, reset, 5)
There is no need to declare cnt1 or cnt2 separately. Because they are defined and returned within count_nbit, they are automatically declared as 4-bit and 5-bit logic signals, respectively.
To return multiple values from a function, use a tuple:
def tb_pattern(clk, values, clock_index):
...
return data, valid
(d, v) <= tb_pattern(clk, [10, 20, 30], 10)
As with single returns, d and v are automatically declared, so you don’t need to define them separately.