📄 simulatorfaultycircuit.java~
字号:
/*
* SimulatorFaultyCircuit.java
*
* Created on 24 November 2001, 15:40
*/
package jaga.pj.circuits.fpgaft;
import jaga.*;
import jaga.pj.circuits.*;
import java.io.Serializable;
import java.awt.Point;
/** A circuit to be simulated. Includes faults, currently only single stuck at
* faults are simulated. Must be extended to provide the reconfigure method
* which will take a BitSet and build a network of SimulatorFaultyDelayLE logic
* elements of whichever type. <p>
* There are two possible runModes: the NORMAL one is same as the normal circuit
* and faults are introduced by using the set and get methods. The other mode
* FAULTS_AS_INPUTS allows the fault definitions as the first lines of the
* inputs array of the run method.
*
* @author Michael Garvie
* @version
*/
public class SimulatorFaultyCircuit implements SimulatorCircuit, FaultyCircuit, Serializable, StateManipulableSimulatorCircuit
{
public final int FAULTS_AS_INPUTS = 1, NORMAL = 0;
protected SimulatorFaultyDelayLE[] elements;
protected SimulatorLogicElement[] inputs;
protected SimulatorLogicElement[] outputs;
protected SimulatorLogicElement[][] inoutels = new SimulatorLogicElement[ 3 ][];
protected int bitsPerVar;
protected int runMode = NORMAL;
protected int inputsOffset = 0;
protected int nrSSAFaults = 0;
protected int[] currentFaults;
protected int[] persistentFaultsUnits;
protected int[] persistentFaultsValues;
protected CircuitMapping circuitMapping;
protected FaultyOptimizedMapping faultyOptimizedMapping = null;
public SimulatorFaultyCircuit(CircuitMapping mapping)
{
circuitMapping = mapping;
if( mapping instanceof FaultyOptimizedMapping )
{
faultyOptimizedMapping = ( FaultyOptimizedMapping ) mapping;
}
}
/** Creates new SimulatorCircuit */
public SimulatorFaultyCircuit(int bitsPerVar, int nrSSAFaults, CircuitMapping mapping)
{
circuitMapping = mapping;
this.bitsPerVar = bitsPerVar;
this.nrSSAFaults = nrSSAFaults;
currentFaults = new int[ nrSSAFaults ];
int nrEls = 1 << bitsPerVar;
for( int ftl = 0; ftl < nrSSAFaults; ftl++ )
{
currentFaults[ ftl ] = nrEls - 1; // start with fault at input 0 (= no fault)
}
if( nrSSAFaults > 0 )
{
setRunMode( FAULTS_AS_INPUTS );
}
}
/** Implements this gentoype's phenotype in this circuit.
*/
public void reconfigure( BitSet genotype )
{
SimulatorLogicElement[][] tinoutels = circuitMapping.map( genotype );
inoutels[ 0 ] = inputs = tinoutels[ 0 ];
inoutels[ 1 ] = outputs = tinoutels[ 1 ];
inoutels[ 2 ] = tinoutels[ 2 ];
elements = ( SimulatorFaultyDelayLE[] ) tinoutels[ 2 ];
// Set persistent faults
if( persistentFaultsUnits != null )
{
for( int flp = 0; flp < persistentFaultsUnits.length; flp++ )
{
setFault( persistentFaultsUnits[ flp ], persistentFaultsValues[ flp ] );
}
}
}
public SampleData[] run( SampleData[] inputData )
{
int length = inputData[ 0 ].length();
return run( inputData, 0, length );
}
/** Sends these inputs to the circuit and returns the outputs.
* The input sample separation is taken into account to sample the outputs
* at the appropriate frequency.
* @param inputData The inputs
* @param startFrom What input data index to start running from
* @param endBefore What input data index to end running just before
*/
public SampleData[] run( SampleData[] inputData, int startFrom, int endBefore )
{
// 1. Set up stuff
SampleData[] rv = new SampleData[ outputs.length ];
int inputSampleSeparation = inputData[ 0 ].getSampleSeparation();
int inputDataLength = endBefore - startFrom;
// 2. Init output sample data
for( int odl = 0; odl < outputs.length; odl++ )
{
rv[ odl ] = new SampleData( 1, inputDataLength * inputSampleSeparation );
}
// 3. Run the circuit!
for( int dl = startFrom; dl < endBefore; dl++ )
{
// 3.1 Implement faults! **!! Should be done at lower freq than outer loop.. not really in gral case.
if( runMode == FAULTS_AS_INPUTS )
{
for( int ftl = 0; ftl < nrSSAFaults; ftl++ )
{
int newFault = ESLib.getLine( inputData, dl, ftl * ( bitsPerVar + 1 ) , ftl * ( bitsPerVar + 1 ) + bitsPerVar - 1 );
if( newFault < elements.length ) // illegal values = no faults (coded input) ** should go in here as well!
{
if( ( currentFaults[ ftl ] != newFault ) || ( inputData[ ftl * ( bitsPerVar + 1 ) + bitsPerVar ].get( dl ) != elements[ newFault ].SSAV ) ) // make more efficient
{
if( currentFaults[ ftl ] < elements.length )
{
elements[ currentFaults[ ftl ] ].SSA = false; // restore previous faults
}
// 3.1.1 Reset circuit state
reset();
}
elements[ newFault ].SSA = true;
elements[ newFault ].SSAV = inputData[ ftl * ( bitsPerVar + 1 ) + bitsPerVar ].get( dl ); // stuck at what
}else
{
if( currentFaults[ ftl ] != newFault )
{
if( currentFaults[ ftl ] < elements.length )
{
elements[ currentFaults[ ftl ] ].SSA = false;
}
reset();
}
}
currentFaults[ ftl ] = newFault;
}
}// finished if faultsAsInputs
// 3.2 Set Inputs
for( int ivl = 0; ivl < inputs.length; ivl++ )
{
inputs[ ivl ].setNow( inputData[ inputsOffset + ivl ].get( dl ) );
}
// Let it run for inputSampleSeparation
int rdl = dl - startFrom;
for( int sdl = 0; sdl < inputSampleSeparation; sdl++ )
{
//System.out.println( sdl );
// Sample outputs
updateAll( elements );
for( int ovl = 0; ovl < outputs.length; ovl++ )
{
rv[ ovl ].setTo( rdl * inputSampleSeparation + sdl, outputs[ ovl ].getNow() );
}
}
}
return rv;
}
protected void updateAll( SimulatorFaultyDelayLE[] elements )
{
// Update state
for( int vl = 0; vl < elements.length; vl++ )
{
elements[ vl ].sampleInputs();
}
for( int vl = 0; vl < elements.length; vl++ )
{
elements[ vl ].refreshOutput();
}
}
/*
public boolean[] runTick( boolean[] inputValues )
{
boolean[] outputValues = new boolean[ outputs.length ];
for( int i = 0; i < inputs.length; i++ )
{
inputs[ i ].setNow( inputValues[ i ] );
}
for( int el = 0; el < elements.length; el++ )
{
elements[ el ].sampleInputs();
}
for( int el = 0; el < elements.length; el++ )
{
elements[ el ].refreshOutput();
}
for( int ol = 0; ol < inputs.length; ol++ )
{
outputValues[ ol ] = outputs[ ol ].getNow();
}
return outputValues;
}
*/
public SimulatorLogicElement[] getElements()
{
return elements;
}
/** Does not affect faults. Just wipes out all state.
*/
public void reset()
{
for( int el = 0; el < elements.length; el++ )
{
elements[ el ].reset();
}
}
/** Does not affect faults. Just wipes out all state to random value.
*/
public void randomReset()
{
for( int el = 0; el < elements.length; el++ )
{
elements[ el ].randomReset();
}
}
/** Sets the runMode to the appropriate mode as defined by constants
*/
public void setRunMode( int rm )
{
runMode = rm;
switch( runMode )
{
case FAULTS_AS_INPUTS:
{
inputsOffset = ( bitsPerVar + 1 ) * nrSSAFaults;
break;
}
case NORMAL:
{
inputsOffset = 0;
break;
}
}
}
/** Sets an element to a fault.
* @param elNr number of element to define fault for.
* @param faultID ID of fault to set as defined in FTLib
*/
public void setFault( int elNr, int faultID )
{
elements[ elNr ].setFault( faultID );
//System.out.println("Setting fault at " + elements[ elNr ] ); //D
}
public void resetDelays(Object delayDef) {
circuitMapping.resetDelays( delayDef );
}
public String toString()
{
String rv = "Faulty Simulator Circuit with mapping: " + circuitMapping;
return rv;
}
public SimulatorLogicElement[][] getInOutEls()
{
return inoutels;
}
public void setPersistentFaults( Point[] pfs )
{
if( faultyOptimizedMapping != null )
{
faultyOptimizedMapping.setPersistentFaults( pfs );
}else
{
int nrFaults = pfs.length;
persistentFaultsUnits = new int[ nrFaults ];
persistentFaultsValues = new int[ nrFaults ];
for( int flp = 0; flp < nrFaults; flp++ )
{
persistentFaultsUnits[ flp ] = pfs[ flp ].x;
persistentFaultsValues[ flp ] = pfs[ flp ].y;
}
}
}
public void setFault(java.awt.Point fault) {
setFault( fault.x, fault.y );
}
public void removeFault(java.awt.Point fault) {
setFault( fault.x, FTLib.NOFAULT );
}
public void removeFault(int pos) {
setFault( pos, FTLib.NOFAULT );
}
public CircuitState getState() {
return new CircuitState( elements, inputs );
}
public void setState(CircuitState cs) {
int[] qPos = new int[ outputs.length ];
for( int ql = 0; ql < outputs.length; ql++ )
{
qPos[ ql ] = jaga.ESLib.indexOf( outputs[ ql ], elements );
if( qPos[ ql ] == -1 )
{
int inpIx = jaga.ESLib.indexOf( outputs[ ql ], inputs );
if( inpIx < 0 )
{
System.out.println( "Q not in els nor ins: " + outputs[ ql ] );
}
qPos[ ql ] = ( inpIx * -1 ) - 1;
}
}
SimulatorLogicElement[] els = cs.getElements( inputs );
for( int el = 0; el < els.length; el++ )
{
elements[ el ] = ( SimulatorFaultyDelayLE )els[ el ]; // should be linked to inputs OK.
}
// need to link outputs
for( int ol = 0; ol < outputs.length; ol++ )
{
if( qPos[ ol ] >= 0 )
{
outputs[ ol ] = elements[ qPos[ ol ] ];
}else
{
System.out.println( "Connecting output " + ol );
outputs[ ol ] = inputs[ ( qPos[ ol ] + 1 ) * -1 ];
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -