Tutorial 5: 4-way MUX: customized view, bundled data

A Plug-in Tutorial
...     Tutorial 5: 4-way MUX: customized view, bundled data

In this example of a 4-way MUX with a 4-bit datapath we concentrate on the view, showing a customized footprint and pin positioning. The plug-in uses bundled data: it bundles each of the 4 4-bit data path alternatives as a (signed) integer.

Document Class

package plugins.components.mux;

import dlsim.document.DLPlugIn;

public class MUX4 extends DLPlugIn {
  /**
   * 4-way, 4-bit wide multiplexer
   * @author rms
   */
  private static final long serialVersionUID = DLFrame.SID;
  private static int IN = 0, S = 4, OUT = 5;		// positions
  private static int BITWIDTH = 4, SS = 4, SWIDTH = 2;
  public static PluginInfo info 
		= new PluginInfo("4-way multiplexer using bundled data", "Bitwidth = 4");

  public MUX4() {
	setPrintName("MUX-4");
	setInputSize(SS*BITWIDTH+SWIDTH);
	setOutputSize(BITWIDTH);
	setInMap(BITWIDTH, BITWIDTH, BITWIDTH, BITWIDTH, SWIDTH);
	setOutMap(BITWIDTH);
	setLabels("I0(4)", "I1(4)", "I2(4)", "I3(4)", "S0-S1", "O(4)");
  }
	
  public void evalState(int source, int val) {
	int choice = (int)getBundle(S);
	putBundle(OUT, (int)putBundle(IN+choice));
  }
}

Notes

View Class

package plugins.components.mux;

import java.awt.Color;

import dlsim.document.DLPlugIn;
import dlsim.view.util.UserPluginView;

public class MUX4View extends UserPluginView {
  /**
   * Custom view for MUX4
   * @author rms
   */
  private static final Color BGC = new Color(100,100,255);
  private static final int HT = 70, WD = 170, NAMEX = 55, NAMEY = 40
  public MUX4View() {
        super();
  }
	
  public MUX4View(DLPlugIn plugin) {
        super(plugin);
  }

  public void setup() {
        setBGCOL(BGC);
        setHT(HT);			/* height */
        setWD(WD);			/* width */
        setNameX(NAMEX);	        /* where printname is printed */
        setNameY(NAMEY);
        setPolygon(new int[][]{{30, 140, 140, 30}, {0, 20, 50, 70}});
        PinDesc[] leftPins = 
            new PinDesc[]{
	    new PinDesc(0, 20, 30),
	    new PinDesc(1, 30, 30),
	    new PinDesc(2, 40, 30),
	    new PinDesc(3, 50, 30)
       };
       PinDesc[] rightPins = new PinDesc[]{new PinDesc(5, 30, 30)};
       PinDesc[] botPins = new PinDesc[]{new PinDesc(4, 110, 14)};
       setLeftPins(leftPins);
       setRightPins(rightPins);
       setBotPins(botPins);
  }
}

Notes

Run DLSim 3 (using "ant run") and build the circuit shown below using Mux4.

Exercises

  1. Change the bitwidth.
  2. Change the number of control bits.
  3. Use the action menu to make these changes dynamic (solution to this is given by components.mux.MUXn in the SDK distribution.)