/* Copyright: (C) 2002 Brigham Young University This program is free software; you can redistribute it and/or modify it under the terms of the Open Source Software License Agreement for JHDL. You should have received a copy of the Open Source Software License for JHDL along with this program. If you have not, download the License Agreement at http://www.jhdl.org/license.html or write to: Brad Hutchings 459 Clyde Bldg Brigham Young University Provo, UT 84602 */ import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import javax.swing.JComponent; import byucc.jhdl.base.Browser; import byucc.jhdl.base.HWSystem; import byucc.jhdl.base.SimulatorCallback; import byucc.jhdl.base.Wire; import byucc.jhdl.base.WireValueException; /** File: CustomAccumView.java * Custom visualization component that shows the amount full of the * accumulator's capacity. * @author Anthony L. Slade */ public class CustomAccumView extends JComponent implements SimulatorCallback, Browser { CustomAccumView() { _size = new Dimension(); } /** This method registers this CustomAccumView with the simulator so * that this view will get notification of important simulator * events (e.g. the end of simulation runs, when this view should * update its display). This method also gets a hold of the * accumulator output wire to then check its value after each * simulation run. */ void setSimulatorCallback(tbNBitAccum design) { HWSystem hwsystem = design.getSystem(); hwsystem.addSimulatorCallback(this); _accumValue = (Wire)hwsystem.findNamed("tbNBitAccum/output"); if ( null == _accumValue ) { System.err.println("Can't find the wire \"input\""); System.exit(1); } _maxValue = (double)((1 << _accumValue.getWidth()) - 1); simulatorReset(); } // ******** SIMULATOR CALLBACK METHODS ******** // /** This method is called after each simulation run (i.e. one or * more clock cycles). */ public void simulatorRefresh(int cycle, int phase) { repaint();// make the new value appear } public void simulatorUpdate(int cycle, int phase) {} /** Called by simulator when the circuit is reset */ public void simulatorReset() { repaint();// make the new value appear } // ****** END SIMULATOR CALLBACK METHODS ****** // /** Paints a pie chart and a label showing to what extent the * capacity of the accumulator is full. */ public void paint(Graphics gr) { // first calculate how full the accumulator is. long value; try { value = _accumValue.getL(this); } catch ( WireValueException wve ) { value = 0; } _percentFull = (double)value / _maxValue; _size = getSize(_size); int width = (int)_size.getWidth(); int height = (int)_size.getHeight(); int diameter = width; if ( height < diameter ) diameter = height; int x = (width - diameter)/2; int y = (height - diameter)/2; gr.setColor(Color.white); gr.fillOval(x,y,diameter,diameter); gr.setColor(Color.pink); int arcAngle = (int)(_percentFull * (-360)); gr.fillArc(x,y,diameter,diameter,90,arcAngle); gr.setColor(Color.black); gr.drawOval(x,y,diameter,diameter); String percentStr = (_percentFull * 100)+"0000"; String labelStr = "Percent Full: " + percentStr.substring(0,5) +"%"; gr.setFont(new Font(null,Font.BOLD,18)); int strWidth = gr.getFontMetrics().stringWidth(labelStr); gr.setXORMode(Color.pink); gr.drawString(labelStr,width/2-strWidth/2,height/2); } // Size of this component Dimension _size; // The percent amount that the accumulator is full double _percentFull; // The maximum value that the accumulator may reach double _maxValue; // The accumulator output wire Wire _accumValue; }