<s> import os <EOL> import subprocess <EOL> import sys <EOL> from migen . fhdl . structure import _Fragment <EOL> from migen . build . generic_platform import * <EOL> from migen . build import tools <EOL> from migen . build . xilinx import common <EOL> def _format_constraint ( c ) : <EOL> if isinstance ( c , Pins ) : <EOL> return "<STR_LIT>" + c . identifiers [ <NUM_LIT:0> ] <EOL> elif isinstance ( c , IOStandard ) : <EOL> return "<STR_LIT>" + c . name <EOL> elif isinstance ( c , Drive ) : <EOL> return "<STR_LIT>" + str ( c . strength ) <EOL> elif isinstance ( c , Misc ) : <EOL> return "<STR_LIT>" + c . misc . replace ( "<STR_LIT:=>" , "<STR_LIT:U+0020>" ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( c ) ) <EOL> def _format_xdc ( signame , resname , * constraints ) : <EOL> fmt_c = [ _format_constraint ( c ) for c in constraints ] <EOL> fmt_r = resname [ <NUM_LIT:0> ] + "<STR_LIT::>" + str ( resname [ <NUM_LIT:1> ] ) <EOL> if resname [ <NUM_LIT:2> ] is not None : <EOL> fmt_r += "<STR_LIT:.>" + resname [ <NUM_LIT:2> ] <EOL> r = "<STR_LIT>" . format ( fmt_r ) <EOL> for c in fmt_c : <EOL> r += c + "<STR_LIT>" + signame + "<STR_LIT>" <EOL> return r <EOL> def _build_xdc ( named_sc , named_pc ) : <EOL> r = "<STR_LIT>" <EOL> for sig , pins , others , resname in named_sc : <EOL> if len ( pins ) > <NUM_LIT:1> : <EOL> for i , p in enumerate ( pins ) : <EOL> r += _format_xdc ( sig + "<STR_LIT:[>" + str ( i ) + "<STR_LIT:]>" , resname , Pins ( p ) , * others ) <EOL> elif pins : <EOL> r += _format_xdc ( sig , resname , Pins ( pins [ <NUM_LIT:0> ] ) , * others ) <EOL> else : <EOL> r += _format_xdc ( sig , resname , * others ) <EOL> if named_pc : <EOL> r += "<STR_LIT:\n>" + "<STR_LIT>" . join ( named_pc ) <EOL> return r <EOL> def _run_vivado ( build_name , vivado_path , source , ver = None ) : <EOL> if sys . platform == "<STR_LIT:win32>" or sys . platform == "<STR_LIT>" : <EOL> build_script_contents = "<STR_LIT>" <EOL> build_script_contents += "<STR_LIT>" + build_name + "<STR_LIT>" <EOL> build_script_file = "<STR_LIT>" + build_name + "<STR_LIT>" <EOL> tools . write_to_file ( build_script_file , build_script_contents ) <EOL> r = subprocess . call ( [ build_script_file ] ) <EOL> else : <EOL> build_script_contents = "<STR_LIT>" <EOL> settings = common . settings ( vivado_path , ver ) <EOL> build_script_contents += "<STR_LIT>" + settings + "<STR_LIT:\n>" <EOL> build_script_contents += "<STR_LIT>" + build_name + "<STR_LIT>" <EOL> build_script_file = "<STR_LIT>" + build_name + "<STR_LIT>" <EOL> tools . write_to_file ( build_script_file , build_script_contents ) <EOL> r = subprocess . call ( [ "<STR_LIT>" , build_script_file ] ) <EOL> if r != <NUM_LIT:0> : <EOL> raise OSError ( "<STR_LIT>" ) <EOL> class XilinxVivadoToolchain : <EOL> def __init__ ( self ) : <EOL> self . bitstream_commands = [ ] <EOL> self . additional_commands = [ ] <EOL> self . pre_synthesis_commands = [ ] <EOL> self . with_phys_opt = False <EOL> def _build_batch ( self , platform , sources , build_name ) : <EOL> tcl = [ ] <EOL> for filename , language , library in sources : <EOL> filename_tcl = "<STR_LIT:{>" + filename + "<STR_LIT:}>" <EOL> tcl . append ( "<STR_LIT>" + filename_tcl ) <EOL> tcl . append ( "<STR_LIT>" <EOL> . format ( library , filename_tcl ) ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> tcl . extend ( c . format ( build_name = build_name ) for c in self . pre_synthesis_commands ) <EOL> tcl . append ( "<STR_LIT>" . format ( platform . device , "<STR_LIT:U+0020>" . join ( platform . verilog_include_paths ) ) ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> tcl . append ( "<STR_LIT>" ) <EOL> if self . with_phys_opt : <EOL> tcl . append ( "<STR_LIT>" ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> tcl . append ( "<STR_LIT>" ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> for bitstream_command in self . bitstream_commands : <EOL> tcl . append ( bitstream_command . format ( build_name = build_name ) ) <EOL> tcl . append ( "<STR_LIT>" . format ( build_name ) ) <EOL> for additional_command in self . additional_commands : <EOL> tcl . append ( additional_command . format ( build_name = build_name ) ) <EOL> tcl . append ( "<STR_LIT>" ) <EOL> tools . write_to_file ( build_name + "<STR_LIT>" , "<STR_LIT:\n>" . join ( tcl ) ) <EOL> def build ( self , platform , fragment , build_dir = "<STR_LIT>" , build_name = "<STR_LIT>" , <EOL> toolchain_path = "<STR_LIT>" , source = True , run = True ) : <EOL> tools . mkdir_noerror ( build_dir ) <EOL> cwd = os . getcwd ( ) <EOL> os . chdir ( build_dir ) <EOL> if not isinstance ( fragment , _Fragment ) : <EOL> fragment = fragment . get_fragment ( ) <EOL> platform . finalize ( fragment ) <EOL> v_output = platform . get_verilog ( fragment ) <EOL> named_sc , named_pc = platform . resolve_signals ( v_output . ns ) <EOL> v_file = build_name + "<STR_LIT>" <EOL> v_output . write ( v_file ) <EOL> sources = platform . sources | { ( v_file , "<STR_LIT>" , "<STR_LIT>" ) } <EOL> self . _build_batch ( platform , sources , build_name ) <EOL> tools . write_to_file ( build_name + "<STR_LIT>" , _build_xdc ( named_sc , named_pc ) ) <EOL> if run : <EOL> _run_vivado ( build_name , toolchain_path , source ) <EOL> os . chdir ( cwd ) <EOL> return v_output . ns <EOL> def add_period_constraint ( self , platform , clk , period ) : <EOL> platform . add_platform_command ( <EOL> "<STR_LIT>" + str ( period ) + <EOL> "<STR_LIT>" , clk = clk ) <EOL> def add_false_path_constraint ( self , platform , from_ , to ) : <EOL> platform . add_platform_command ( <EOL> "<STR_LIT>" , <EOL> from_ = from_ , to = to ) </s>
