mirror of
https://github.com/myhdl/myhdl.git
synced 2025-01-24 21:52:56 +08:00
Merge pull request #200 from hgomersall/initial_value_support
[FIX] fixed initial value support for bool lists and list of wires
This commit is contained in:
commit
29069ae477
@ -468,9 +468,14 @@ def _writeSigDecls(f, intf, siglist, memlist):
|
||||
sig_vhdl_objs = [inferVhdlObj(each) for each in m.mem]
|
||||
|
||||
if all([each._init == m.mem[0]._init for each in m.mem]):
|
||||
val_str = (
|
||||
' := (others => %dX"%s")' %
|
||||
(sig_vhdl_objs[0].size, str(m.mem[0]._init)))
|
||||
if isinstance(m.mem[0]._init, bool):
|
||||
val_str = (
|
||||
' := (others => \'%s\')' % str(int(m.mem[0]._init)))
|
||||
|
||||
else:
|
||||
val_str = (
|
||||
' := (others => %dX"%s")' %
|
||||
(sig_vhdl_objs[0].size, str(m.mem[0]._init)))
|
||||
else:
|
||||
_val_str = ',\n '.join(
|
||||
['%dX"%s"' % (obj.size, str(each._init)) for
|
||||
|
@ -368,7 +368,7 @@ def _writeSigDecls(f, intf, siglist, memlist):
|
||||
if m._driven:
|
||||
k = m._driven
|
||||
|
||||
if toVerilog.initial_values:
|
||||
if toVerilog.initial_values and not k == 'wire':
|
||||
if all([each._init == m.mem[0]._init for each in m.mem]):
|
||||
|
||||
initialize_block_name = ('INITIALIZE_' + m.name).upper()
|
||||
|
@ -47,13 +47,31 @@ def initial_value_enum_bench(initial_val, **kwargs):
|
||||
|
||||
return state_walker, clkgen
|
||||
|
||||
@block
|
||||
def bool_writer(signal, clk):
|
||||
|
||||
@always(clk.posedge)
|
||||
def writer():
|
||||
print(int(signal))
|
||||
|
||||
return writer
|
||||
|
||||
@block
|
||||
def int_writer(signal, clk):
|
||||
|
||||
@always(clk.posedge)
|
||||
def writer():
|
||||
print(signal)
|
||||
|
||||
return writer
|
||||
|
||||
@block
|
||||
def initial_value_bench(initial_val, **kwargs):
|
||||
|
||||
clk = Signal(bool(0))
|
||||
|
||||
input_signal = Signal(initial_val)
|
||||
|
||||
|
||||
if 'change_input_signal' in kwargs.keys():
|
||||
|
||||
change_input_signal = kwargs['change_input_signal']
|
||||
@ -90,7 +108,7 @@ def initial_value_bench(initial_val, **kwargs):
|
||||
clk.next = not clk
|
||||
|
||||
raise StopSimulation()
|
||||
|
||||
|
||||
@always_comb
|
||||
def output_driver():
|
||||
output_signal.next = input_signal
|
||||
@ -107,9 +125,12 @@ def initial_value_bench(initial_val, **kwargs):
|
||||
else:
|
||||
assert output_signal == update_val
|
||||
|
||||
@always(clk.posedge)
|
||||
def output_writer():
|
||||
print(output_signal)
|
||||
if isinstance(initial_val, bool):
|
||||
output_writer = bool_writer(output_signal, clk)
|
||||
|
||||
else:
|
||||
output_writer = int_writer(output_signal, clk)
|
||||
|
||||
|
||||
return clkgen, output_driver, drive_and_check, output_writer
|
||||
|
||||
@ -122,7 +143,7 @@ def canonical_list_writer(output_signal_list, clk):
|
||||
def list_writer():
|
||||
for i in range(signal_list_length):
|
||||
print(str(output_signal_list[i]._val))
|
||||
|
||||
|
||||
canonical_list_writer.verilog_code = '''
|
||||
always @(posedge $clk) begin: INITIAL_VALUE_LIST_BENCH_CANONICAL_LIST_WRITER_0_LIST_WRITER
|
||||
integer i;
|
||||
@ -146,16 +167,89 @@ end process INITIAL_VALUE_BENCH_OUTPUT_WRITER;
|
||||
'''
|
||||
return list_writer
|
||||
|
||||
@block
|
||||
def bool_list_writer(output_signal_list, clk):
|
||||
|
||||
signal_list_length = len(output_signal_list)
|
||||
|
||||
@always(clk.posedge)
|
||||
def list_writer():
|
||||
for i in range(signal_list_length):
|
||||
print(int(output_signal_list[i]))
|
||||
|
||||
return list_writer
|
||||
|
||||
@block
|
||||
def initial_value_bool_list_bench(initial_vals, **kwargs):
|
||||
clk = Signal(bool(0))
|
||||
|
||||
input_signal_list = [Signal(initial_val) for initial_val in initial_vals]
|
||||
|
||||
output_signal_list = [
|
||||
Signal(not initial_val) for initial_val in initial_vals]
|
||||
|
||||
update_val = int(not initial_vals[0])
|
||||
|
||||
expected_output = [
|
||||
bool(each_input._init) for each_input in input_signal_list]
|
||||
|
||||
N = 10
|
||||
first = [True]
|
||||
|
||||
signal_list_length = len(initial_vals)
|
||||
|
||||
@instance
|
||||
def clkgen():
|
||||
|
||||
clk.next = 0
|
||||
for n in range(N):
|
||||
yield delay(10)
|
||||
clk.next = not clk
|
||||
|
||||
raise StopSimulation()
|
||||
|
||||
@always_comb
|
||||
def output_driver():
|
||||
for i in range(signal_list_length):
|
||||
output_signal_list[i].next = input_signal_list[i]
|
||||
|
||||
@always(clk.posedge)
|
||||
def drive_and_check():
|
||||
|
||||
for i in range(signal_list_length):
|
||||
input_signal_list[i].next = update_val
|
||||
|
||||
if __debug__:
|
||||
if first[0]:
|
||||
for i in range(signal_list_length):
|
||||
assert output_signal_list[i] == expected_output[i]
|
||||
first[0] = False
|
||||
else:
|
||||
for i in range(signal_list_length):
|
||||
assert output_signal_list[i] == update_val
|
||||
|
||||
output_writer = bool_list_writer(output_signal_list, clk)
|
||||
|
||||
return clkgen, output_driver, drive_and_check, output_writer
|
||||
|
||||
@block
|
||||
def assign_output(input_signal, output_signal):
|
||||
@always_comb
|
||||
def assignment():
|
||||
output_signal.next = input_signal
|
||||
|
||||
return assignment
|
||||
|
||||
@block
|
||||
def initial_value_list_bench(initial_vals, **kwargs):
|
||||
clk = Signal(bool(0))
|
||||
|
||||
input_signal_list = [Signal(initial_val) for initial_val in initial_vals]
|
||||
|
||||
|
||||
if len(initial_vals[0]) == 1:
|
||||
|
||||
output_signal_list = [
|
||||
Signal(intbv(not initial_val, min=0, max=2)) for
|
||||
Signal(intbv(not initial_val, min=0, max=2)) for
|
||||
initial_val in initial_vals]
|
||||
update_val = int(not initial_vals[0])
|
||||
else:
|
||||
@ -180,11 +274,15 @@ def initial_value_list_bench(initial_vals, **kwargs):
|
||||
clk.next = not clk
|
||||
|
||||
raise StopSimulation()
|
||||
|
||||
@always_comb
|
||||
def output_driver():
|
||||
for i in range(signal_list_length):
|
||||
output_signal_list[i].next = input_signal_list[i]
|
||||
|
||||
# We assign each of the output drivers independently.
|
||||
# This forces the output to be a wire (where appropriate) so we can
|
||||
# check this type is handled properly too.
|
||||
output_drivers = []
|
||||
for input_signal, output_signal in zip(
|
||||
input_signal_list, output_signal_list):
|
||||
|
||||
output_drivers.append(assign_output(input_signal, output_signal))
|
||||
|
||||
@always(clk.posedge)
|
||||
def drive_and_check():
|
||||
@ -203,7 +301,7 @@ def initial_value_list_bench(initial_vals, **kwargs):
|
||||
|
||||
output_writer = canonical_list_writer(output_signal_list, clk)
|
||||
|
||||
return clkgen, output_driver, drive_and_check, output_writer
|
||||
return clkgen, output_drivers, drive_and_check, output_writer
|
||||
|
||||
def runner(initial_val, tb=initial_value_bench, **kwargs):
|
||||
pre_toVerilog_initial_values = toVerilog.initial_values
|
||||
@ -214,7 +312,7 @@ def runner(initial_val, tb=initial_value_bench, **kwargs):
|
||||
|
||||
try:
|
||||
assert conversion.verify(tb(initial_val, **kwargs)) == 0
|
||||
|
||||
|
||||
finally:
|
||||
toVerilog.initial_values = pre_toVerilog_initial_values
|
||||
toVHDL.initial_values = pre_toVHDL_initial_values
|
||||
@ -236,16 +334,22 @@ def test_signed():
|
||||
'''
|
||||
min_val = -12
|
||||
max_val = 4
|
||||
|
||||
|
||||
initial_val = intbv(
|
||||
randrange(min_val, max_val), min=min_val, max=max_val)
|
||||
|
||||
runner(initial_val)
|
||||
|
||||
def test_bool():
|
||||
'''The correct initial value should be used for bool type signal.
|
||||
'''
|
||||
initial_val = bool(randrange(0, 2))
|
||||
runner(initial_val)
|
||||
|
||||
def test_modbv():
|
||||
'''The correct initial value should be used for modbv type signal.
|
||||
'''
|
||||
|
||||
|
||||
initial_val = modbv(randrange(0, 2**10))[10:]
|
||||
|
||||
runner(initial_val)
|
||||
@ -283,7 +387,7 @@ def test_unsigned_list():
|
||||
min_val = 0
|
||||
max_val = 34
|
||||
initial_vals = [intbv(
|
||||
randrange(min_val, max_val), min=min_val, max=max_val)
|
||||
randrange(min_val, max_val), min=min_val, max=max_val)
|
||||
for each in range(10)]
|
||||
|
||||
runner(initial_vals, tb=initial_value_list_bench)
|
||||
@ -305,7 +409,7 @@ def test_signed_list():
|
||||
|
||||
runner(initial_vals, tb=initial_value_list_bench)
|
||||
|
||||
# All the same case
|
||||
# All the same case
|
||||
initial_vals = [intbv(
|
||||
randrange(min_val, max_val), min=min_val, max=max_val)] * 10
|
||||
|
||||
@ -314,25 +418,25 @@ def test_signed_list():
|
||||
def test_modbv_list():
|
||||
'''The correct initial value should be used for modbv type signal lists
|
||||
'''
|
||||
|
||||
|
||||
initial_vals = [
|
||||
modbv(randrange(0, 2**10))[10:] for each in range(10)]
|
||||
|
||||
runner(initial_vals, tb=initial_value_list_bench)
|
||||
|
||||
# All the same case
|
||||
# All the same case
|
||||
initial_vals = [modbv(randrange(0, 2**10))[10:]] * 10
|
||||
runner(initial_vals, tb=initial_value_list_bench)
|
||||
|
||||
|
||||
def test_long_signals_list():
|
||||
'''The correct initial value should work with wide bitwidths (i.e. >32)
|
||||
'''The correct initial value should work with wide bitwidths (i.e. >32)
|
||||
signal lists
|
||||
'''
|
||||
min_val = -(2**71)
|
||||
max_val = 2**71 - 1
|
||||
initial_vals = [intbv(
|
||||
randrange(min_val, max_val), min=min_val, max=max_val)
|
||||
randrange(min_val, max_val), min=min_val, max=max_val)
|
||||
for each in range(10)]
|
||||
|
||||
runner(initial_vals, tb=initial_value_list_bench)
|
||||
@ -344,12 +448,12 @@ def test_long_signals_list():
|
||||
def test_bool_signals_list():
|
||||
'''The correct initial value should be used for a boolean type signal lists
|
||||
'''
|
||||
initial_vals = [intbv(0, min=0, max=2) for each in range(10)]
|
||||
initial_vals = [False for each in range(10)]
|
||||
|
||||
runner(initial_vals, tb=initial_value_list_bench)
|
||||
runner(initial_vals, tb=initial_value_bool_list_bench)
|
||||
|
||||
initial_vals = [intbv(0, min=0, max=2)] * 10
|
||||
runner(initial_vals, tb=initial_value_list_bench)
|
||||
initial_vals = [False] * 10
|
||||
runner(initial_vals, tb=initial_value_bool_list_bench)
|
||||
|
||||
|
||||
def test_init_used():
|
||||
@ -365,7 +469,7 @@ def test_init_used():
|
||||
runner(initial_val, change_input_signal=True)
|
||||
|
||||
#def test_init_used_list():
|
||||
# '''It should be the _init attribute of each element in the list
|
||||
# '''It should be the _init attribute of each element in the list
|
||||
# that is used for initialisation
|
||||
#
|
||||
# It should not be the current value, which should be ignored.
|
||||
@ -373,7 +477,7 @@ def test_init_used():
|
||||
# min_val = -34
|
||||
# max_val = 15
|
||||
# initial_val = [intbv(
|
||||
# randrange(min_val, max_val), min=min_val, max=max_val)
|
||||
# randrange(min_val, max_val), min=min_val, max=max_val)
|
||||
# for each in range(10)]
|
||||
#
|
||||
# list_runner(initial_val, change_input_signal=True)
|
||||
|
Loading…
x
Reference in New Issue
Block a user