📄 combinationalblifexperiment.java~
字号:
/* * CombinationalBLIFExperiment.java * * Created on 10 June 2003, 13:56 */package jaga.pj.circuits.experiment;import jaga.BitSet;import jaga.SampleData;import jaga.pj.circuits.SimulatorLogicElement;import jaga.pj.circuits.SimulatorDelayLE;import jaga.pj.circuits.fpgaft.SimulatorLUT;import jaga.experiment.*;import java.util.Hashtable;import java.util.Vector;import java.util.StringTokenizer;import java.util.Enumeration;import java.io.BufferedReader;import java.io.FileReader;import java.io.File;/** From a BLIF file (standard for banchmarks used with Sis). * * @author mmg20 */public class CombinationalBLIFExperiment extends MultiOutputExperiment implements ConfigurableRandomInputExperiment { protected String circuitName = null; public final String TOKEN_START = "."; public final String CIRCUIT_NAME_TOKEN = TOKEN_START + "model"; public final String INPUTS_DECL_TOKEN = TOKEN_START + "inputs"; public final String OUTPUTS_DECL_TOKEN = TOKEN_START + "outputs"; public final String GATE_DECL_TOKEN = TOKEN_START + "names"; public final String COMMENT_TOKEN = "#"; public final Experiment[] dummy = {}; protected FitnessFunction fitnessFunction; protected TestPatternGenerator tpg; protected double tSetup; /** @param alwaysRnd Always randomzie generateInput */ public CombinationalBLIFExperiment( String BLIFFileName, double tSetup ) throws java.io.IOException { this( BLIFFileName, tSetup, new CorrelationFitnessFunction(), new CompleteShuffledTPG() ); } /** Creates a new instance of CombinationalBLIFExperiment */ public CombinationalBLIFExperiment( String BLIFFileName, double tSetup, FitnessFunction fitnessFunction, TestPatternGenerator tpg ) throws java.io.IOException { this.tSetup = tSetup; this.fitnessFunction = fitnessFunction; this.tpg = tpg; // Init vars int nrInputs = -1; Hashtable els = new Hashtable(); Vector outputNames = null; Vector inputs = null; // Phase 1 - Get ins/ous/names //System.out.println("Phase 1"); BufferedReader bur = new BufferedReader( new FileReader( new File( BLIFFileName ) ) ); String line; while( ( line = getNextLine( bur ) ) != null ) { StringTokenizer stk = new StringTokenizer( line ); String t0; if( stk.hasMoreTokens() && !( t0 = stk.nextToken() ).equals( "#" ) ) { //System.out.println( t0 ); //debug if( ( circuitName == null ) && t0.equals( CIRCUIT_NAME_TOKEN ) ) { circuitName = stk.nextToken(); //System.out.println("Circuit Name " + circuitName ); // debug }else if( ( nrInputs < 0 ) && t0.equals( INPUTS_DECL_TOKEN ) ) { nrInputs = 0; inputs = new Vector(); while( stk.hasMoreTokens() ) { String currToken = stk.nextToken(); nrInputs++; SimulatorDelayLE ce = new SimulatorDelayLE( 0 ); els.put( currToken, ce ); inputs.add( ce ); //System.out.println("Adding input " + currToken ); // debug } }else if( ( outputNames == null ) && t0.equals( OUTPUTS_DECL_TOKEN ) ) { outputNames = new Vector(); while( stk.hasMoreTokens() ) { String currT = stk.nextToken(); outputNames.add( currT ); //System.out.println("Adding out " + currT ); } }else if( t0.equals( GATE_DECL_TOKEN ) ) { int gateIns = 0; String currToken = stk.nextToken(); while( stk.hasMoreTokens() ) { gateIns++; currToken = stk.nextToken(); } els.put( currToken, new SimulatorLUT( new BitSet( 1 << gateIns ), 0 ) ); //System.out.println("Adding gate " + currToken + ": " + els.get( currToken ) ); } } } //System.out.println(outputNames); //System.out.println(inputs); // debug //System.out.println(els); // Phase 2 - Calculate tables and connect //System.out.println("Phase 2"); bur = new BufferedReader( new FileReader( new File( BLIFFileName ) ) ); line = getNextLine( bur ); while( line != null ) { if( line.startsWith( GATE_DECL_TOKEN ) ) { StringTokenizer stk = new StringTokenizer( line ); stk.nextToken(); // GATE_DECL_TOKEN Vector gateInputNames = new Vector(); int gateIns = 0; String currToken = stk.nextToken(); while( stk.hasMoreTokens() ) { gateInputNames.add( currToken ); gateIns++; currToken = stk.nextToken(); } String outputName = currToken; SimulatorLUT currGate = ( SimulatorLUT ) els.get( outputName ); // 2.2 Connect Inputs SimulatorLogicElement[] gateInputs = new SimulatorLogicElement[ gateIns ]; for( int igl = 0; igl < gateIns; igl++ ) { gateInputs[ igl ] = ( SimulatorLogicElement ) els.get( gateInputNames.get( igl ) ); } currGate.connect( gateInputs ); // 2.3 Calc Table boolean RHV = true; // default // 2.3.1 Check if should change RHV line = getNextLine( bur ); if( !line.startsWith( TOKEN_START ) ) { stk = new StringTokenizer( line ); if( gateIns > 0 ) { stk.nextToken(); String outVal = stk.nextToken(); RHV = outVal.equals( "1" ); }else { String outVal = stk.nextToken(); RHV = outVal.equals( "0" ); } } // 2.3.2 Init table for( int tl = 0; tl < currGate.table.length; tl++ ) { currGate.table[ tl ] = !RHV; } if( gateIns > 0 ) { // 2.3.3 add lines while( !line.startsWith( TOKEN_START ) ) { stk = new StringTokenizer( line ); String inDef = stk.nextToken(); if( inDef.indexOf("-") >= 0 ) { int[] xPos = getDontCarePos( inDef ); int xCount = xPos.length; for( int xl = 0; xl < ( 1 << xCount ); xl++ ) { String xVals = jaga.ESLib.int2BinStr( xl, xCount ); char[] newIns = inDef.toCharArray(); for( int dcl = 0; dcl < xCount; dcl++ ) { newIns[ xPos[ dcl ] ] = xVals.charAt( dcl ); } int tablePos = Integer.parseInt( new String( newIns ), 2 ); currGate.table[ tablePos ] = RHV; } }else { currGate.table[ Integer.parseInt( inDef, 2 ) ] = RHV; } //System.out.println("Gate " + outputName + " table is " + jaga.ESLib.boolArr2String( currGate.table )); line = getNextLine( bur ); }//while } }else { line = getNextLine( bur ); } } // Phase 3 - Generate output tables //System.out.println("Phase 3"); int nrOuts = outputNames.size(); boolean[][] outputTables = new boolean[ nrOuts ][ 1 << nrInputs ]; for( int idl = 0; idl < ( 1 << nrInputs ); idl++ ) { //System.out.println("Setting input pattern " + idl); String iVals = jaga.ESLib.int2BinStr( idl, nrInputs ); for( int il = 0; il < nrInputs; il++ ) { SimulatorDelayLE currEl = ( SimulatorDelayLE ) inputs.get( il ); currEl.setNow( iVals.charAt( il ) == '1' ); // first inp is MSB } for( int enl = 0; enl < els.size(); enl++ ) // - nrIns ignored for safety { Enumeration elsEnum = els.elements(); while( elsEnum.hasMoreElements() ) { try { SimulatorLUT currLUT = ( SimulatorLUT ) elsEnum.nextElement(); currLUT.sampleInputs(); currLUT.refreshOutput(); }catch( ClassCastException e ) { ////System.out.println( e ); // the inputs } } } ////System.out.println("Now sample input pattern " + idl); for( int ol = 0; ol < nrOuts; ol++ ) { SimulatorLUT currQ = ( SimulatorLUT )els.get( ( String ) outputNames.get( ol ) ); outputTables[ ol ][ idl ] = currQ.getNow(); } } BooleanFunction[] outFuns = new BooleanFunction[ nrOuts ]; ArbitraryFunctionExperiment[] outExps = new ArbitraryFunctionExperiment[ nrOuts ]; for( int ol = 0; ol < nrOuts; ol++ ) { //System.out.println("Q"+ol + ":" + jaga.ESLib.boolArr2String( outputTables[ ol ] ) ); outFuns[ ol ] = new TableBooleanFunction( nrInputs, outputTables[ ol ], tpg ); outExps[ ol ] = new ArbitraryFunctionExperiment( outFuns[ ol ], tSetup, fitnessFunction ); } exps = outExps; } protected String getNextLine( BufferedReader bur ) throws java.io.IOException { String rv; do { rv = bur.readLine(); }while( ( rv != null ) && ( rv.startsWith( COMMENT_TOKEN ) ) ); return rv; } public String toString() { String rv = "Combinational Circuit Experiment loaded from:"; rv += "\n BLIF Model Name: " + circuitName; rv += "\n Nr. Inputs: " + getNumOfInputs(); rv += "\n Nr. Outputs: " + getNumOfOutputs(); rv += "\n tSetup = " + tSetup; rv += "\n Test Pattern Generator: " + tpg; rv += "\n Fitness Function: " + fitnessFunction; return rv; } protected static int[] getDontCarePos( String ins ) { int howMany = 0; for( int ll = 0; ll < ins.length(); ll++ ) { if( ins.charAt( ll ) == '-' ) { howMany++; } } int[] rv = new int[ howMany ]; for( int ll = 0; ll < ins.length(); ll++ ) { if( ins.charAt( ll ) == '-' ) { rv[ --howMany ] = ll; } } return rv; } public Object get(Object param) { return ( ( ConfigurableRandomInputExperiment ) exps[ mainExp ]).get( param ); } public void set(Object param) { ( ( ConfigurableRandomInputExperiment )exps[ mainExp ]) .set( param ); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -