/* File: DFlipFlop.java * Copy: Copyright (c) 1998 David Green * Vers: 1.0.0 1998/10/16 dgg -- derive from idea in OODemo2.cpp by * Dennis Smith */ /** Simulation of D flip flop * * @author David Green * @note always set the d lead ahead of the clock lead when wiring */ public class DFlipFlop { // state of flip flop private boolean q; // present value of D input private boolean d; // present value of clock lead private boolean old_clock; /** default constructor */ public DFlipFlop() { q = false; d = false; old_clock = false; } /** return flip flop state */ public boolean getQ() { return q; } /** set the d value * * @param new_d - new value of d input lead * @note this does not quite model wiring */ public void setD( boolean new_d ) { d = new_d; } /** set clock * * @param new_clock - new clock lead value */ public void setClock( boolean new_clock) { // if rising edge, clock D FF if ( (old_clock == false ) && (new_clock == true ) ) q = d; old_clock = new_clock; } /** main - test driver */ public static void main( String [] args ) { DFlipFlop ff = new DFlipFlop(); // initialize ff.setD( false ); clockFF( ff ); System.out.println("Test Program for D FF Simulation" ); printFFState( ff ); ff.setD( true ); clockFF( ff ); printFFState( ff ); } // next methods are private methods for test driver only private static void clockFF( DFlipFlop ff ) { ff.setClock( true ); ff.setClock( false ); } private static void printFFState( DFlipFlop ff ) { System.out.println( "Flip-flop = " + ff.getQ() ); } }