import byucc.jhdl.apps.Stimulator.Stimulator;
import byucc.jhdl.apps.Viewers.JL.CLIJL;
import byucc.jhdl.base.Cell;
import byucc.jhdl.base.HWSystem;
import byucc.jhdl.base.Node;
import byucc.jhdl.base.ProgrammaticTestBench;
import byucc.jhdl.base.Wire;
import byucc.jhdl.Logic.Logic;
import byucc.jhdl.Xilinx.Virtex.pullup;
import byucc.jhdl.Xilinx.Virtex.VirtexTechMapper;

/** Example test bench to demonstrate the use of tri-state devices and
 * user-defined clocks with Stimulators.
 * @author Anthony L. Slade */
public class tbCustomClockCell extends Logic implements ProgrammaticTestBench {
  public Cell design; 
  private static boolean interactive;
  public static void main(String argv[]) {
    HWSystem hws = new HWSystem();
    tbCustomClockCell tCust = new tbCustomClockCell(hws);
    new CLIJL( tCust );
    System.err.println("Returned from creation of JL");
  }
  public tbCustomClockCell(Node parent) {
    super(parent);
    setDefaultTechMapper(new VirtexTechMapper());
    Wire inClk = wire(1,"tbClock");
    Wire inAWire = wire(CustomClockCell.WIRE_WIDTH,"tbInAWire");
    Wire inBWire = wire(CustomClockCell.WIRE_WIDTH,"tbInBWire");
    Wire outWire = wire(CustomClockCell.WIRE_WIDTH,"tbOutWire");
    Wire enable = wire(1,"tbEnable");
    new pullup(this,enable);
    design = new CustomClockCell(this,inClk,inAWire,inBWire,
				 enable,outWire);
    /* Make sure that we have a user-defined clock at this level,
     * since the internal design also creates a custom clock */
    Wire globalDefaultClock = wire(1,"globalDefaultClock");
    clockDriver(globalDefaultClock,"11001100","globalDefaultClockDriver");
    setDefaultClock(globalDefaultClock);

    /* Make a Stimulator with this Testbench as parent */
    Stimulator stimulator = new Stimulator( this );
    stimulator.addWire(inBWire, "2 4 8 1 3 7 15 0" );
    stimulator.addWire(inAWire, "0 2 4 8 1 3 7 15" );
    stimulator.addWire( inClk, "1 0" );
    /* Since enable is also connected to a pullup, this will
     * automatically create a TriStateStimulator to drive the wire */
    stimulator.addWire( enable, "0 0 0 Z Z Z 0 1 0 Z" );
  }
  public void execute() {
    final int iterations = 1000;
    for (int itr=0; itr<iterations; itr++) {
      getSystem().cycle(1);
    }
  }
}
