Skip to content

Commit

Permalink
[verilator] Use Verilator context in C/Python wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ptpan committed Oct 23, 2023
1 parent 7c77d5b commit 4b681f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
// Verilator model
void * model;
VerilatedContext * context_ptr;
// VCD state
int _vcd_en;
Expand All @@ -64,7 +65,7 @@
void destroy_model( V{component_name}_t *);
void comb_eval( V{component_name}_t * );
void seq_eval( V{component_name}_t * );
void assert_en( bool en );
void assert_en( V{component_name}_t *, bool );
#if VLINETRACE
void trace( V{component_name}_t *, char * );
Expand All @@ -75,16 +76,14 @@
//------------------------------------------------------------------------
// sc_time_stamp
//------------------------------------------------------------------------
// Must be defined so the simulator knows the current time. Called by
// $time in Verilog. See:
// http://www.veripool.org/projects/verilator/wiki/Faq
vluint64_t g_main_time = 0;
// This is now a lgeacy function required only so linking works on Cygwin
// and MSVC++:
// https://github.com/verilator/verilator/blob/master/examples/make_tracing_c/sim_main.cpp
double sc_time_stamp()
{{
return g_main_time;
return 0;
}}
Expand All @@ -98,12 +97,16 @@
V{component_name}_t * m;
V{vl_component_name} * model;
VerilatedContext * context_ptr;
context_ptr = new VerilatedContext;
Verilated::randReset( {verilator_xinit_value} );
Verilated::randSeed( {verilator_xinit_seed} );
context_ptr->debug(0);
context_ptr->randReset( {verilator_xinit_value} );
context_ptr->randSeed( {verilator_xinit_seed} );
m = (V{component_name}_t *) malloc( sizeof(V{component_name}_t) );
model = new V{vl_component_name}();
model = new V{vl_component_name}(context_ptr);
m->model = (void *) model;
Expand All @@ -114,7 +117,7 @@
#if DUMP_VCD
if ( strlen( vcd_filename ) != 0 ) {{
m->_vcd_en = 1;
Verilated::traceEverOn( true );
context_ptr->traceEverOn( true );
VerilatedVcdC * tfp = new VerilatedVcdC();
model->trace( tfp, 99 );
Expand Down Expand Up @@ -159,6 +162,8 @@
delete model;
free(m);
}}
//------------------------------------------------------------------------
Expand Down Expand Up @@ -259,9 +264,9 @@
//------------------------------------------------------------------------
// Enable or disable assertions controlled by --assert
void assert_en( bool en ) {{
void assert_en( V{component_name}_t * m, bool en ) {{
Verilated::assertOn(en);
m->context_ptr->assertOn(en);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,28 @@ def finalize( s ):
assert s._finalization_count == 0,\
'Imported component can only be finalized once!'
s._finalization_count += 1
# Clean up python side FFI references
# del s._line_trace_str
s._ffi_inst.destroy_model( s._ffi_m )
s.ffi.dlclose( s._ffi_inst )
s.ffi = None
s._ffi_inst = None
del s._ffi_inst
del s.ffi
def __del__( s ):
if s._finalization_count == 0:
s._finalization_count += 1
# Clean up python side FFI references
# del s._line_trace_str
s._ffi_inst.destroy_model( s._ffi_m )
s.ffi.dlclose( s._ffi_inst )
s.ffi = None
s._ffi_inst = None
del s._ffi_inst
del s.ffi
def construct( s, *args, **kwargs ):
# Set up the VCD file name
Expand All @@ -106,11 +116,15 @@ def construct( s, *args, **kwargs ):
verilator_vcd_file = verilator_vcd_file.encode('ascii')
# Construct the model
s._ffi_m = s._ffi_inst.create_model( s.ffi.new("char[]", verilator_vcd_file) )
# PP: we need to keep the new'ed object alive by assigning it to
# a variable. See more about this:
# https://cffi.readthedocs.io/en/stable/ref.html#ffi-new
ffi_vl_vcd_file = s.ffi.new("char[]", verilator_vcd_file)
s._ffi_m = s._ffi_inst.create_model( ffi_vl_vcd_file )
# Buffer for line tracing
s._line_trace_str = s.ffi.new('char[512]')
s._convert_string = s.ffi.string
# s._line_trace_str = s.ffi.new('char[512]')
# s._convert_string = s.ffi.string
# Use non-attribute varialbe to reduce CPython bytecode count
_ffi_m = s._ffi_m
Expand Down Expand Up @@ -150,12 +164,13 @@ def assert_en( s, en ):
# at this moment I'm not sure if the C API's are compatible between
# PyPy and CPython).
assert isinstance( en, bool )
s._ffi_inst.assert_en( en )
s._ffi_inst.assert_en( s._ffi_m, en )
def line_trace( s ):
if {external_trace}:
s._ffi_inst.trace( s._ffi_m, s._line_trace_str )
return s._convert_string( s._line_trace_str ).decode('ascii')
# s._ffi_inst.trace( s._ffi_m, s._line_trace_str )
# return s._convert_string( s._line_trace_str ).decode('ascii')
print('no implemented')
else:
{line_trace}
Expand Down

0 comments on commit 4b681f3

Please sign in to comment.