<s> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> import os <EOL> import sys <EOL> import subprocess <EOL> import tempfile <EOL> import veriloggen . core . vtypes as vtypes <EOL> import veriloggen . core . module as module <EOL> def setup_waveform ( m , * uuts ) : <EOL> new_uuts = [ ] <EOL> for u in uuts : <EOL> if isinstance ( u , ( tuple , list ) ) : <EOL> new_uuts . extend ( u ) <EOL> elif isinstance ( u , dict ) : <EOL> new_uuts . extend ( list ( u . values ( ) ) ) <EOL> else : <EOL> new_uuts . append ( u ) <EOL> uuts = new_uuts <EOL> ret = m . Initial ( <EOL> vtypes . Systask ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> vtypes . Systask ( '<STR_LIT>' , <NUM_LIT:0> , * uuts ) <EOL> ) <EOL> return ret <EOL> def setup_clock ( m , clk , hperiod = <NUM_LIT:5> ) : <EOL> ret = m . Initial ( <EOL> clk ( <NUM_LIT:0> ) , <EOL> vtypes . Forever ( clk ( vtypes . Not ( clk ) , ldelay = hperiod ) ) <EOL> ) <EOL> return ret <EOL> def setup_reset ( m , reset , * statement , ** kwargs ) : <EOL> period = kwargs [ '<STR_LIT>' ] if '<STR_LIT>' in kwargs else <NUM_LIT:100> <EOL> ret = m . Initial ( <EOL> reset ( <NUM_LIT:0> ) , <EOL> statement , <EOL> vtypes . Delay ( <NUM_LIT:100> ) , <EOL> reset ( <NUM_LIT:1> ) , <EOL> vtypes . Delay ( <NUM_LIT:100> ) , <EOL> reset ( <NUM_LIT:0> ) , <EOL> ) <EOL> return ret <EOL> def next_clock ( clk ) : <EOL> return ( vtypes . Event ( vtypes . Posedge ( clk ) ) , vtypes . Delay ( <NUM_LIT:1> ) ) <EOL> def finish ( ) : <EOL> return Systask ( '<STR_LIT>' ) <EOL> class Simulator ( object ) : <EOL> def __init__ ( self , * objs , ** options ) : <EOL> sim = '<STR_LIT>' if '<STR_LIT>' not in options else options [ '<STR_LIT>' ] <EOL> wave = '<STR_LIT>' if '<STR_LIT>' not in options else options [ '<STR_LIT>' ] <EOL> files = None if '<STR_LIT>' not in options else options [ '<STR_LIT>' ] <EOL> top = '<STR_LIT:test>' if '<STR_LIT>' not in options else options [ '<STR_LIT>' ] <EOL> self . _type_check_sim ( sim ) <EOL> self . _type_check_wave ( wave ) <EOL> self . objs = objs <EOL> self . files = files <EOL> self . sim = sim <EOL> self . wave = wave <EOL> self . top = top <EOL> def _type_check_sim ( self , sim ) : <EOL> if sim == '<STR_LIT>' or sim == '<STR_LIT>' : <EOL> return <EOL> if sim == '<STR_LIT>' or sim == '<STR_LIT>' : <EOL> return <EOL> if sim == '<STR_LIT>' : <EOL> raise NotImplementedError ( "<STR_LIT>" % sim ) <EOL> raise ValueError ( "<STR_LIT>" % sim ) <EOL> def _type_check_wave ( self , wave ) : <EOL> if wave == '<STR_LIT>' : <EOL> return <EOL> raise ValueError ( "<STR_LIT>" % wave ) <EOL> def run ( self , display = False , outputfile = '<STR_LIT>' , include = None , define = None ) : <EOL> if self . sim == '<STR_LIT>' or self . sim == '<STR_LIT>' : <EOL> return self . _run_iverilog ( display , outputfile , include , define ) <EOL> if self . sim == '<STR_LIT>' or self . sim == '<STR_LIT>' : <EOL> return self . _run_modelsim ( display , self . top , include , define ) <EOL> raise NotImplementedError ( "<STR_LIT>" % self . sim ) <EOL> def _run_iverilog ( self , display = False , outputfile = '<STR_LIT>' , include = None , define = None ) : <EOL> cmd = [ ] <EOL> cmd . append ( '<STR_LIT>' ) <EOL> if include : <EOL> for inc in include : <EOL> cmd . append ( '<STR_LIT>' ) <EOL> cmd . append ( inc ) <EOL> if define : <EOL> for d in define : <EOL> cmd . append ( '<STR_LIT>' ) <EOL> if isinstance ( d , ( tuple , list ) ) : <EOL> if d [ <NUM_LIT:1> ] is None : <EOL> cmd . append ( d [ <NUM_LIT:0> ] ) <EOL> else : <EOL> cmd . append ( '<STR_LIT>' . join ( [ d [ <NUM_LIT:0> ] , '<STR_LIT:=>' , str ( d [ <NUM_LIT:1> ] ) ] ) ) <EOL> else : <EOL> cmd . append ( d ) <EOL> cmd . append ( '<STR_LIT>' ) <EOL> cmd . append ( outputfile ) <EOL> encode = sys . getdefaultencoding ( ) <EOL> code = self . _to_code ( ) <EOL> tmp = tempfile . NamedTemporaryFile ( ) <EOL> tmp . write ( code . encode ( encode ) ) <EOL> tmp . read ( ) <EOL> filename = tmp . name <EOL> cmd . append ( filename ) <EOL> p = subprocess . Popen ( '<STR_LIT:U+0020>' . join ( cmd ) , shell = True , stdout = subprocess . PIPE ) <EOL> syn_rslt = [ ] <EOL> while True : <EOL> stdout_data = p . stdout . readline ( ) <EOL> syn_rslt . append ( stdout_data . decode ( encode ) ) <EOL> if display : print ( stdout_data , end = '<STR_LIT>' ) <EOL> if not stdout_data : break <EOL> p . wait ( ) <EOL> p . stdout . close ( ) <EOL> syn_rslt = '<STR_LIT>' . join ( syn_rslt ) <EOL> p = subprocess . Popen ( '<STR_LIT>' + outputfile , shell = True , stdout = subprocess . PIPE ) <EOL> sim_rslt = [ ] <EOL> while True : <EOL> stdout_data = p . stdout . readline ( ) <EOL> sim_rslt . append ( stdout_data . decode ( encode ) ) <EOL> if display : print ( stdout_data , end = '<STR_LIT>' ) <EOL> if not stdout_data : break <EOL> p . wait ( ) <EOL> p . stdout . close ( ) <EOL> sim_rslt = '<STR_LIT>' . join ( sim_rslt ) <EOL> tmp . close ( ) <EOL> return '<STR_LIT>' . join ( [ syn_rslt , sim_rslt ] ) <EOL> def _run_modelsim ( self , display = False , top = "<STR_LIT:test>" , include = None , define = None ) : <EOL> cmd = [ ] <EOL> cmd . append ( '<STR_LIT>' ) <EOL> if include : <EOL> for inc in include : <EOL> cmd . append ( '<STR_LIT>' ) <EOL> cmd . append ( inc ) <EOL> if define : <EOL> for d in define : <EOL> cmd . append ( '<STR_LIT>' ) <EOL> if isinstance ( d , ( tuple , list ) ) : <EOL> if d [ <NUM_LIT:1> ] is None : <EOL> cmd . append ( d [ <NUM_LIT:0> ] ) <EOL> else : <EOL> cmd . append ( '<STR_LIT>' . join ( [ d [ <NUM_LIT:0> ] , '<STR_LIT:=>' , str ( d [ <NUM_LIT:1> ] ) ] ) ) <EOL> else : <EOL> cmd . append ( d ) <EOL> encode = sys . getdefaultencoding ( ) <EOL> code = self . _to_code ( ) <EOL> tmp = tempfile . NamedTemporaryFile ( ) <EOL> tmp . write ( code . encode ( encode ) ) <EOL> tmp . read ( ) <EOL> filename = tmp . name <EOL> cmd . append ( filename ) <EOL> p = subprocess . Popen ( '<STR_LIT:U+0020>' . join ( cmd ) , shell = True , stdout = subprocess . PIPE ) <EOL> syn_rslt = [ ] <EOL> while True : <EOL> stdout_data = p . stdout . readline ( ) <EOL> syn_rslt . append ( stdout_data . decode ( encode ) ) <EOL> if display : print ( stdout_data , end = '<STR_LIT>' ) <EOL> if not stdout_data : break <EOL> p . wait ( ) <EOL> p . stdout . close ( ) <EOL> syn_rslt = '<STR_LIT>' . join ( syn_rslt ) <EOL> p = subprocess . Popen ( '<STR_LIT>' + top + '<STR_LIT>' , shell = True , stdout = subprocess . PIPE ) <EOL> sim_rslt = [ ] <EOL> while True : <EOL> stdout_data = p . stdout . readline ( ) <EOL> sim_rslt . append ( stdout_data . decode ( encode ) ) <EOL> if display : print ( stdout_data , end = '<STR_LIT>' ) <EOL> if not stdout_data : break <EOL> p . wait ( ) <EOL> p . stdout . close ( ) <EOL> sim_rslt = '<STR_LIT>' . join ( sim_rslt ) <EOL> tmp . close ( ) <EOL> return '<STR_LIT>' . join ( [ syn_rslt , sim_rslt ] ) <EOL> def _to_code ( self ) : <EOL> code = [ ] <EOL> for obj in self . objs : <EOL> if isinstance ( obj , module . Module ) : <EOL> code . append ( obj . to_verilog ( ) ) <EOL> code . append ( '<STR_LIT:\n>' ) <EOL> if isinstance ( obj , str ) : <EOL> code . append ( obj ) <EOL> code . append ( '<STR_LIT:\n>' ) <EOL> return '<STR_LIT>' . join ( code ) <EOL> def view_waveform ( self , filename = '<STR_LIT>' , background = False ) : <EOL> return self . _view_waveform_gtkwave ( filename , background ) <EOL> def _view_waveform_gtkwave ( self , filename = '<STR_LIT>' , background = False ) : <EOL> cmd = [ ] <EOL> cmd . append ( '<STR_LIT>' ) <EOL> cmd . append ( '<STR_LIT>' ) <EOL> cmd . append ( filename ) <EOL> if background : <EOL> cmd . append ( '<STR_LIT:&>' ) <EOL> subprocess . call ( '<STR_LIT:U+0020>' . join ( cmd ) , shell = True ) </s>
