JHDL 0.3.39

| JHDL Doc Home Page | User's Manual Home Page |


Wires and Simulation : General Concepts

Correct handling of wires is critical to writing a good behavioral model or test bench.Generally we interact with wires via get() and put() calls inside of the clock(), propagate(), and reset() methods that describe the behavior of our circuits.Behavioral methods are described in the section entitled, "Behavioral Modeling in JHDL". In calling get() and put() you should remember four main principles:

Get the sink / source right

Each time we access a wire value the simulator checks to see if we are "authorized" to do so, i.e. if the cell is a valid sink or source. This is an important check in making sure that the circuit structure is correct (and therefore netlistable). A wire is a valid sink if it was passed through any port of the cell that is calling get(). A wire is a valid source if it was passed through an out-port of the cell that is calling put(). Also, if a cell constructs a new wire and passes itself as the parent of that wire, it can do both get()s and put()s on that wire. The critical point to remember is that there are two general methods to get your wires to have the proper scope: Two examples: First, the correct example:
   Wire myPortWire, myConstructedWire;
   /* The constructor */
   public MyCell(Node parent, Wire t1) {
     super(this);
     myPortWire = connect("someport", t1);
     myConstructedWire = wire(5);
   }

   public void clock() {
     myPortWire.get(this, 2);
     myConstructedWire.get(this);
     myConstructedWire.put(this, 15);
   }
This clock() method is correct -- myPortWire has correct scope in this cell because it is the return value of a connect() call. myConstructedWire has correct scope because it was constructed in this cell. Now for the incorrect example:
   Wire myPortWire, myConstructedWire1, myConstructedWire2;
   /* The constructor */
   public MyCell(Node parent, Wire t1) {
     super(this);
     connect("someport", t1);
     myPortWire = t1;
     myConstructedWire1 = Logic.wire(parent, 5);
     myConstructedWire2 = myConstructedWire1.getWire(2);
   }

   public void clock() {
     myPortWire.get(this, 2);
     myConstructedWire1.get(this);
     myConstructedWire2.put(this, 15);
   }
This clock() method will die a hard death. myPortWire is still pointing to the "t1" parameter, but did not get the return value of the corresponding connect() call. Therefore it has incorrect scope. myConstructedWire1 fails because it was constructed with a different cell (parent) as its parent. myConstructedWire2 fails because it was constructed with no parent. A correct alternative for this case would be to construct it as follows:
myConstructedWire2 = myConstructedWire1.range(this, 2, 2)
The JHDL simulator will report these errors with a message like this:
byucc.jhdl.base.PutGetException: Illegal put by Cell: tb_MyCell-0/MyCell-0 on Wire: tb_MyCell-0/q
Also, beware of this common error, which is a Java-scope problem:
   Wire sum;
   public MyCell(Node parent, Wire sum) {
     super(parent);
     sum = connect("sum", sum);
   }
   public void clock() {
     sum.put(this, 1);
   }
Do you see the problem? Why will this cause a null-pointer exception?

Get the width right

Wire get()s and put()s come in three flavors, depending on the width of the wire: Note that in the last case, the wire value is broken up into 32-bit chunks, with the least significant chunk in array index 0. So if I wanted to put the value 0x11112222333344445555666677778888 on my 128-bit wire, I would call
   public void clock() {
     int[] a = { 0x77778888, 0x55556666, 0x33334444, 0x11112222 };
     myWire.putA(this, a);
   }
To summarize: you can call getA() on a 32-bit wire, but you cannot call get() on a 128-bit wire. If you make a call that isn't wide enough for the wire, you will get an error message like this:
byucc.jhdl.base.WireSizeException: Cell: tb_MyCell-0/MyCell-0 is put()ing a value
not wide enough for wire: tb_MyCell-0/MyCell-0/Xwire-0. Input width: 32 bits, wire width: 128

Get the direction right

Simply put, you cannot call put() on a wire that is connected to an in-port. If a wire is connected to an out-port, or if the wire was constructed in this cell, you can call both put() and get() on that wire.

Get the "put-count" right

The simulator counts the number of get()s and put()s on each wire in each simulation cycle. This helps it detect multiple drivers on a wire, dangling wires, etc. for reporting purposes. You should be aware of two basic rules: Keep these rules in mind when coding up your behavioral models -- each output wire to your cell should be put() exactly once in each and every execution of your clock() or propagate() methods. This requires particular attention if you have any branching statements in your model -- for example, this model of an enabled flip-flop will produce annoying "wire undriven" messages whenever clock-enable is not asserted:
   public void clock() {
     if (ce.get(this) == 1)
       q.put(this, d.get(this);
   }
Therefore a better model (for the sanity of those who use your flip-flop and don't like the messages) would be:
   public void clock() {
     if (ce.get(this) == 1)
       q.put(this, d.get(this));
     else
       q.put(this, q.get(this));
   }

| JHDL Doc Home Page | User's Manual Home Page |

Copyright (c) 1998-2003 Brigham Young University. All rights reserved.
Reproduction in whole or in part in any form or medium without express permission of Brigham Young University is prohibited.

Documentation Revision: 19 Mar, 2001