from bsmedit.bsm.pysim import *
from propgrid import formatters as fmt
# create a simulation
s = simulation(None, './examples/tb_dll/libtbdll.so')

assert s.is_valid(), 'Failed to load simulation'
# set the simulation parameters: step = 100us, run infinitely
s.set_parameter('100us', '-1us')

# read the register value and print it
print(s.read('top.sig_int'))

# run the simulation for one step (100us)
#s.step()

print(s.read('top.sig_int'))

# set the register to 1000
s.write({'top.sig_int': 1000})

print(s.read('top.sig_int'))

s.run(more='1ps')
# run the simulation for a small step to make the change effective
print(s.read('top.sig_int'))
s.run(more='1ps')

# dump the register value to file
s.trace_file('top.sig_sin')
s.trace_file('top.sig_sin', 'example', fmt='vcd')
s.trace_file('top.sig_cos', 'example', fmt='vcd')
s.trace_file('top.sig_char', 'example', fmt='vcd')
s.trace_file('top.sig_bool', 'example', fmt='vcd')
s.trace_file('top.sig_float', 'example', fmt='vcd')
s.trace_file('top.sig_double', 'example', fmt='vcd')
s.trace_file('top.sig_int', 'example', fmt='vcd')
s.trace_file('top.sig_long', 'example', fmt='vcd')
s.trace_file('top.sig_longlong', 'example', fmt='vcd')
s.trace_file('top.sig_sc_bigint', 'example', fmt='vcd')
s.trace_file('top.sig_sc_biguint', 'example', fmt='vcd')
s.trace_file('top.sig_sc_bit', 'example', fmt='vcd')
s.trace_file('top.sig_sc_bv', 'example', fmt='vcd', valid="top.CLOCK", trigger="pos")
s.trace_file('top.sig_sc_fixed', 'example', fmt='vcd')
s.trace_file('top.sig_sc_fixed_fast', 'example', fmt='vcd')
s.trace_file('top.sig_sc_int', 'example', fmt='vcd')
s.trace_file('top.sig_sc_logic', 'example', fmt='vcd')
s.trace_file('top.sig_sc_lv', 'example', fmt='vcd')
s.trace_file('top.sig_sc_ufixed', 'example', fmt='vcd')
s.trace_file('top.sig_sc_uint', 'example', fmt='vcd')
s.trace_file('top.sig_short', 'example', fmt='vcd')
#s.trace_file('top.sig_std_string', 'example', fmt='vcd')
s.trace_file('top.sig_uchar', 'example', fmt='vcd')
s.trace_file('top.sig_uint', 'example', fmt='vcd')
s.trace_file('top.sig_ulong', 'example', fmt='vcd')
s.trace_file('top.sig_ulonglong', 'example', fmt='vcd')
s.trace_file('top.sig_ushort', 'example', fmt='vcd')

#s.trace_file('top.interface.in_sin', 'example', fmt='vcd')
#s.trace_file('top.interface.in_cos', 'example', fmt='vcd')
s.trace_file('top.interface.in_char', 'example', fmt='vcd')
s.trace_file('top.interface.in_bool', 'example', fmt='vcd')
s.trace_file('top.interface.in_float', 'example', fmt='vcd')
s.trace_file('top.interface.in_double', 'example', fmt='vcd')
s.trace_file('top.interface.in_int', 'example', fmt='vcd')
s.trace_file('top.interface.in_long', 'example', fmt='vcd')
s.trace_file('top.interface.in_longlong', 'example', fmt='vcd')
s.trace_file('top.interface.in_sc_bigint', 'example', fmt='vcd')
s.trace_file('top.interface.in_sc_biguint', 'example', fmt='vcd')
s.trace_file('top.interface.in_sc_bit', 'example', fmt='vcd')
s.trace_file('top.interface.in_sc_bv', 'example', fmt='vcd')
s.trace_file('top.interface.in_sc_fixed', 'example', fmt='vcd')
s.trace_file('top.interface.in_sc_fixed_fast', 'example', fmt='vcd')
s.trace_file('top.interface.in_sc_int', 'example', fmt='vcd')
s.trace_file('top.interface.in_sc_logic', 'example', fmt='vcd')
s.trace_file('top.interface.in_sc_lv', 'example', fmt='vcd')
s.trace_file('top.interface.in_sc_ufixed', 'example', fmt='vcd')
s.trace_file('top.interface.in_sc_uint', 'example', fmt='vcd')
s.trace_file('top.interface.in_short', 'example', fmt='vcd')
#s.trace_file('top.interface.in_std_string', 'example', fmt='vcd')
s.trace_file('top.interface.in_uchar', 'example', fmt='vcd')
s.trace_file('top.interface.in_uint', 'example', fmt='vcd')
s.trace_file('top.interface.in_ulong', 'example', fmt='vcd')
s.trace_file('top.interface.in_ulonglong', 'example', fmt='vcd')
s.trace_file('top.interface.in_ushort', 'example', fmt='vcd')

#s.trace_file('top.interface.out_sin', 'example', fmt='vcd')
#s.trace_file('top.interface.out_cos', 'example', fmt='vcd')
s.trace_file('top.interface.out_char', 'example', fmt='vcd')
s.trace_file('top.interface.out_bool', 'example', fmt='vcd')
s.trace_file('top.interface.out_float', 'example', fmt='vcd')
s.trace_file('top.interface.out_double', 'example', fmt='vcd')
s.trace_file('top.interface.out_int', 'example', fmt='vcd')
s.trace_file('top.interface.out_long', 'example', fmt='vcd')
s.trace_file('top.interface.out_longlong', 'example', fmt='vcd')
s.trace_file('top.interface.out_sc_bigint', 'example', fmt='vcd')
s.trace_file('top.interface.out_sc_biguint', 'example', fmt='vcd')
s.trace_file('top.interface.out_sc_bit', 'example', fmt='vcd')
s.trace_file('top.interface.out_sc_bv', 'example', fmt='vcd')
s.trace_file('top.interface.out_sc_fixed', 'example', fmt='vcd')
s.trace_file('top.interface.out_sc_fixed_fast', 'example', fmt='vcd')
s.trace_file('top.interface.out_sc_int', 'example', fmt='vcd')
s.trace_file('top.interface.out_sc_logic', 'example', fmt='vcd')
s.trace_file('top.interface.out_sc_lv', 'example', fmt='vcd')
s.trace_file('top.interface.out_sc_ufixed', 'example', fmt='vcd')
s.trace_file('top.interface.out_sc_uint', 'example', fmt='vcd')
s.trace_file('top.interface.out_short', 'example', fmt='vcd')
#s.trace_file('top.interface.out_std_string', 'example', fmt='vcd')
s.trace_file('top.interface.out_uchar', 'example', fmt='vcd')
s.trace_file('top.interface.out_uint', 'example', fmt='vcd')
s.trace_file('top.interface.out_ulong', 'example', fmt='vcd')
s.trace_file('top.interface.out_ulonglong', 'example', fmt='vcd')
s.trace_file('top.interface.out_ushort', 'example', fmt='vcd')
# create the propgrid window and monitor the register value
s.monitor('top.sig_double')

p = s.monitor('top.sig_sc_logic')
p.SetFormatter(
    fmt.ChoiceFormatter(['1','0','Z','X']))
p.SetControlStyle('radiobox')

# dump the register value to a numpy array
s.trace_buf('top.sig_cos', 500, valid="top.CLOCK", trigger="pos")
s.trace_buf('top.sig_sin', 500, valid="top.CLOCK", trigger="pos")

plot_trace('top.sig_sin')
ylim([-1, 1])
figure()
plot_trace('top.sig_cos', 'top.sig_sin', relim=False)
xlim([-1, 1])
ylim([-1, 1])

s.run(to='1000us')

p = s.monitor('top.sig_bool')
p.SetControlStyle('CheckBox')
p = s.monitor('top.sig_char')
p.SetControlStyle('Slider', min_value=-128, max_value=127)
p = s.monitor('top.sig_cos')
p = s.monitor('top.sig_double')
p = s.monitor('top.sig_float')
p = s.monitor('top.sig_int')
# set breakpoint condition
p.SetBpCondition(cond='$>=6000000', hitcount='')
# enable breakpoint
p.SetChecked(True)
p = s.monitor('top.sig_long')
p.SetBpCondition(cond='$>=6000000', hitcount='#==4')
p.SetChecked(True)
p = s.monitor('top.sig_longlong')
p = s.monitor('top.sig_sc_bigint')
p = s.monitor('top.sig_sc_biguint')
p = s.monitor('top.sig_sc_bit')
p.SetControlStyle('CheckBox')
p = s.monitor('top.sig_sc_bv')
p = s.monitor('top.sig_sc_fixed')
p = s.monitor('top.sig_sc_fixed_fast')
p = s.monitor('top.sig_sc_int')
p = s.monitor('top.sig_sc_logic')
p.SetControlStyle('RadioBox', choice=['1', '0', 'Z', 'X'])
p.SetBpCondition(cond='', hitcount='')
p.SetChecked(True)
p = s.monitor('top.sig_sc_lv')
p = s.monitor('top.sig_sc_ufixed')
p = s.monitor('top.sig_sc_uint')
p = s.monitor('top.sig_short')
p = s.monitor('top.sig_sin')
p = s.monitor('top.sig_std_string')
p = s.monitor('top.sig_uchar')
p.SetControlStyle('Spin', min_value=0, max_value=255)
p = s.monitor('top.sig_uint')
p = s.monitor('top.sig_ulong')
p = s.monitor('top.sig_ulonglong')
p = s.monitor('top.sig_ushort')
