home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / ed8n1t2i / digsim.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  45.0 KB  |  1,028 lines

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // DigSim.java           v 1.00 b3
  5. // Written by:           I. van Rienen / E-mail ivr@bart.nl
  6. // URL:                  http://www/bart.nl/~ivr
  7. // Initial release:           v 1.00b1 01-03-1996
  8. //                            v 1.00b2 07-03-1996 AnalyzerPanel added
  9. //                            v 1.00b3 26-03-1996 Scrollbar bug fixed
  10. //                                                SimpleDialog bug fixed
  11. // Released in public domain: v 1.00b1 01-03-1996
  12. //
  13. // ---- Description ----
  14. // Java program for simulating digital schematics.
  15. //
  16. // This program and the Java source is in the public domain.
  17. // Permission to use, copy, modify, and distribute this software
  18. // and its documentation for NON-COMMERCIAL purposes and
  19. // without fee is hereby granted.
  20. //
  21. //    Copyright 1996
  22. //
  23. //    Iwan van Rienen
  24. //    Joan Maetsuyckerstr. 145
  25. //    2593 ZG  The Hague
  26. //    The Netherlands
  27. //
  28. // I am not responsible for any bugs in this program and
  29. // possible damage to hard- or software when using this program.
  30. //****************************************************************************
  31. import java.applet.Applet;
  32. import java.awt.*;
  33. import java.lang.InterruptedException;
  34. import java.lang.Integer;
  35. import java.lang.Math;
  36. import java.util.Vector;
  37. import java.io.StreamTokenizer;
  38. import java.io.InputStream;
  39. import java.io.IOException;
  40. import java.net.URL;
  41. import java.awt.image.*;
  42.  
  43. public class DigSim extends Applet implements Runnable {
  44.     DigSimFrame frame;
  45.     ControlPanel MyControlPanel = null;
  46.     StatusPanel MyStatusPanel = null;
  47.     SchematicPanel MySchematicPanel = null;
  48.     Button displayb, disposeb;
  49.     Thread killme = null;
  50.     String SchematicName = null;
  51.     Schematic MySchematic = null;
  52.     Pin PinGrid[][];
  53.     String message = null;
  54.     static final int MaxXPoints = 100, MaxYPoints = 100;
  55.     Image GridImage = null;
  56.     Image ImageBuffer = null;
  57.     Graphics ibg;
  58.     int OffScreenWidth = 0, OffScreenHeight = 0;
  59.     static final Color BackGroundColor = Color.black;
  60.     static final Color GridColor = Color.green;
  61.     static final int GridStep = 8;
  62.     static final int hgs = GridStep / 2;
  63.     boolean SchematicPanelPainted = false;
  64.     boolean HelpWanted = false;
  65.     String TextFileRequested = null;
  66.     boolean EnableFileOperations = false;
  67.     ExamplesFrame MyExamplesFrame = null;
  68.     String RequestedText = null;
  69.     boolean RequestedTextFileRead = false;
  70.     boolean RequestedTextFileError = false;
  71.     OptionsFrame MyOptionsFrame = null;
  72.     AnalyzerFrame MyAnalyzerFrame = null;
  73.     int SimulationSpeed = 10;
  74.     boolean StopAtShortCircuit = true;
  75.     boolean StopAtLoop = true;
  76.     boolean AnalyzerAutoPopUp = true;
  77.  
  78. //----------------------------------------------------------------------------
  79. // Initialize a new Digital simulator
  80. //----------------------------------------------------------------------------
  81.     public void init() {
  82.         String temp;
  83.  
  84.         setLayout(new FlowLayout());
  85.         add(displayb = new Button("Display simulator"));
  86.         add(disposeb = new Button("Dispose simulator"));
  87.         OffScreenWidth = 400;
  88.         OffScreenHeight = 300;
  89.         // System.out.println ("OffScreenWidth,Height = " + OffScreenWidth + ", " + OffScreenHeight);
  90.         SchematicName = getParameter("schematic");
  91.         temp = getParameter("fileop");
  92.         if (temp != null) {
  93.             if (temp.equals ("true")) {
  94.                 EnableFileOperations = true;
  95.             } else {
  96.                 EnableFileOperations = false;
  97.             }
  98.         } else {
  99.             EnableFileOperations = false;
  100.         }
  101.  
  102.         PinGrid = new Pin[MaxXPoints][MaxYPoints];
  103.         InitPinGrids();
  104.         SetUpImages();
  105.  
  106.         doFrame();
  107.         StatusMessage("Please wait.");
  108.     }
  109.  
  110. //----------------------------------------------------------------------------
  111. // Called when program starts
  112. //----------------------------------------------------------------------------
  113.     public void start() {
  114.         if(killme == null) {
  115.             killme = new Thread(this);
  116.             killme.start();
  117.         }
  118.     }
  119.  
  120. //----------------------------------------------------------------------------
  121. // Called when program stops
  122. //----------------------------------------------------------------------------
  123.     public void stop() {
  124.         killme = null;
  125.     }
  126.  
  127. //----------------------------------------------------------------------------
  128. // This is the main thread, it handels the loading of the initial
  129. // schematic, the loading of requested files and triggering simulation
  130. // cycles.
  131. //----------------------------------------------------------------------------
  132.     public void run() {
  133.         InputStream is = null;
  134.         String message = null;
  135.         int BUFSIZE = 100;
  136.         char Buf[];
  137.         int BufPtr = 0;
  138.         int BytesRead = 0;
  139.         int c;
  140.         String Result;
  141.  
  142.         Buf = new char [BUFSIZE];
  143.  
  144.         try {
  145.             Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  146.             if (SchematicName != null) {
  147.                 // System.out.println ("Load schematic");
  148.                 is = new URL(getDocumentBase(), SchematicName).openStream();
  149.                 MySchematic = new Schematic (PinGrid, is);
  150.             }
  151.         } catch(Exception e) {
  152.             message = e.toString();
  153.             String DlgButtons[] = { "OK" };
  154.             SimpleDialog ExceptionDialog = new SimpleDialog(frame, "Reading inital schematic", message, DlgButtons, 1, 0, 0, SimpleDialog.IMAGE_STOP);
  155.         }
  156.  
  157.         try {
  158.             if (is != null)
  159.                 is.close();
  160.         } catch(Exception e) {
  161.         }
  162.         repaint();
  163.  
  164.         if (MySchematic == null) {
  165.             // System.out.println ("Start with empty schematic");
  166.             MySchematic = new Schematic();
  167.             frame.setTitle("Digital Simulator [untitled]");
  168.         } else {
  169.             frame.setTitle("Digital Simulator [" + SchematicName + "]");
  170.             MySchematic.FileDir = "";
  171.             MySchematic.FileName = SchematicName;
  172.         }
  173.  
  174.         while (killme != null) {
  175.  
  176.             try {
  177.                 Thread.sleep(SimulationSpeed);
  178.             } catch (InterruptedException e){}
  179.  
  180.             //------------------------------------------------------------
  181.             if (TextFileRequested != null) {
  182.                 // System.out.println ("TextFile requested");
  183.                 BufPtr = 0;
  184.                 Result =  "";
  185.                 try {
  186.                     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  187.                     is = new URL(getDocumentBase(), TextFileRequested).openStream();
  188.  
  189.                     while ( (c = is.read()) != -1) {
  190.                         Buf[BufPtr++] = (char) c;
  191.                         if (BufPtr >= BUFSIZE) {
  192.                             Result += new String (Buf, 0, BUFSIZE);
  193.                             BufPtr = 0;
  194.                         }
  195.                     }
  196.                     Result += new String (Buf, 0, BufPtr);
  197.  
  198.                 } catch(Exception e) {
  199.                     Result += e.toString();
  200.                     RequestedText = Result;
  201.                     RequestedTextFileError = true;
  202.                 }
  203.  
  204.                 try {
  205.                     if (is != null) {
  206.                         is.close();
  207.                     }
  208.                 } catch(Exception e) {
  209.                     Result += e.toString();
  210.                     RequestedText = Result;
  211.                     RequestedTextFileError = true;
  212.                 }
  213.                 if (!RequestedTextFileError) {
  214.                     RequestedText = Result;
  215.                     RequestedTextFileRead = true;
  216.                 }
  217.  
  218.             }
  219.             //------------------------------------------------------------
  220.  
  221.             if (!SchematicPanelPainted) {
  222.                 if (ImagesReady()) {
  223.                     SchematicPanelPainted = true;
  224.                     MySchematicPanel.repaint();
  225.  
  226.                     if (MyControlPanel != null) {
  227.                         MyControlPanel.EnableAllButtons();
  228.                         frame.EnableAllMenus();
  229.                         StatusMessage("DigSim initialized. Make a choice or select a component.");
  230.                     }
  231.                 }
  232.             }
  233.             if (MyStatusPanel.SimulationRunning) {
  234.                 SimulateCycle();
  235.             }
  236.  
  237.         }
  238.         killme = null;
  239.     }
  240.  
  241. //----------------------------------------------------------------------------
  242. // Fill 2-dimensional PinGrid array with new Pins
  243. // The PinGrid is used to determine where components are placed in the
  244. // schematic, and before simulation it's used to set up a linked list
  245. // of all components connected to eachother.
  246. //----------------------------------------------------------------------------
  247.     public void InitPinGrids() {
  248.         int x, y;
  249.         // System.out.println ("Initialise PinGrid.");
  250.         for (x = 0; x < MaxXPoints; x++) {
  251.             for (y = 0; y < MaxYPoints; y++) {
  252.                PinGrid[x][y] = new Pin();
  253.             }
  254.         }
  255.         // System.out.println ("Initialise ready.");
  256.     }
  257.  
  258. //----------------------------------------------------------------------------
  259. // Preapare an off-screen ImageBuffer for some double-buffering,
  260. // and prepare an image with the Grid used for fast redrawing.
  261. // When the user resizes the Simulator frame this function is called again.
  262. //----------------------------------------------------------------------------
  263.     public void SetUpImages() {
  264.         int x, y;
  265.         // Set up big imagebuffer for off-screen painting.
  266.         ImageBuffer = createImage(OffScreenWidth, OffScreenHeight);
  267.         ibg = ImageBuffer.getGraphics();
  268.         // Set up a grid image for fast repainting.
  269.         GridImage = createImage(OffScreenWidth, OffScreenHeight);
  270.         Graphics g = GridImage.getGraphics();
  271.         g.setColor (BackGroundColor);
  272.         g.fillRect (0, 0, OffScreenWidth, OffScreenHeight);
  273.         g.setColor (GridColor);
  274.         for (x = 1; x <= MaxXPoints; x++) {
  275.             for (y = 1; y <= MaxYPoints; y++) {
  276.                 g.drawLine (x * GridStep, y * GridStep, x * GridStep, y * GridStep);
  277.             }
  278.         }
  279.     }
  280.  
  281. //----------------------------------------------------------------------------
  282. // The user want to dispose the simulator
  283. //----------------------------------------------------------------------------
  284.     public void destroyFrame() {
  285.         if (SimulateRunning()) {
  286.             SimulateStop();
  287.         }
  288.         if (MyAnalyzerFrame != null) {
  289.              MyAnalyzerFrame.dispose();
  290.              MyAnalyzerFrame = null;
  291.         }
  292.         if (MyExamplesFrame != null) {
  293.             MyExamplesFrame.dispose();
  294.             MyExamplesFrame = null;
  295.         }
  296.         if (MyOptionsFrame != null) {
  297.             MyOptionsFrame.dispose();
  298.             MyOptionsFrame = null;
  299.         }
  300.         if (frame != null) {
  301.             frame.dispose();
  302.             frame = null;
  303.             displayb.enable(true);
  304.             disposeb.enable(false);
  305.         }
  306.     }
  307.  
  308. //----------------------------------------------------------------------------
  309. // Called to show and initialize the main simulator frame
  310. //----------------------------------------------------------------------------
  311.     void doFrame() {
  312.         displayb.enable(false);
  313.         disposeb.enable(true);
  314.         frame = new DigSimFrame(this);
  315.         frame.EnableAllMenus();
  316.     }
  317.  
  318. //----------------------------------------------------------------------------
  319. // Handle the two buttons in the applet.
  320. //----------------------------------------------------------------------------
  321.     public boolean action(Event ev, Object arg) {
  322.         if (ev.target instanceof Button) {
  323.             String label = (String)arg;
  324.             if (label.equals("Display simulator")) {
  325.                 doFrame();
  326.                 MyControlPanel.EnableAllButtons();
  327.             } else if (label.equals("Dispose simulator")) {
  328.                 destroyFrame();
  329.             }
  330.             return true;
  331.         }
  332.         return false;
  333.     }
  334.  
  335. //----------------------------------------------------------------------------
  336. // Set up the digital simulator before simulating.
  337. //----------------------------------------------------------------------------
  338.     public void SimulateSetUp() {
  339.         // System.out.println ("Simulate Set up");
  340.         int x, y;
  341.         for (x = 0; x < MaxXPoints; x++) {
  342.             for (y = 0; y < MaxYPoints; y++) {
  343.                 if (PinGrid[x][y] != null) {
  344.                     PinGrid[x][y].SimulateSetUp(x, y);
  345.                 }
  346.             }
  347.         }
  348.         // System.out.println ("Simulate Set Up ready");
  349.     }
  350.  
  351. //----------------------------------------------------------------------------
  352. // The user wants to start simulating. Set up everthing, and load
  353. // the analyzer if there are probes in the schematic
  354. //----------------------------------------------------------------------------
  355.     public void SimulateStart() {
  356.         StatusMessage("Please wait. Initializing simulation.");
  357.         MyStatusPanel.repaint();
  358.         if (MyStatusPanel.SimulationInitializeInProgress) {
  359.             return;
  360.         } else {
  361.             if (AnalyzerAutoPopUp && MyAnalyzerFrame == null) {
  362.                 if (MySchematic.ProbesInSchematic()) {
  363.                     MyAnalyzerFrame = new AnalyzerFrame(this);
  364.                 }
  365.             }
  366.             MySchematicPanel.SelectSchematic.RemoveAllComponents();
  367.             MyStatusPanel.SimulationInitializeInProgress = true;
  368.             MyStatusPanel.repaint();
  369.             SimulateSetUp();
  370.             MyStatusPanel.SimulationInitializeInProgress = false;
  371.             MyStatusPanel.SimulationRunning = true;
  372.             StatusMessage("Simulation running. press 'Simulate' button again to stop or press a component in the schematic");
  373.     //      MyStatusPanel.repaint();
  374.         }
  375.     }
  376.  
  377. //----------------------------------------------------------------------------
  378. // Stop the siumulation process
  379. //----------------------------------------------------------------------------
  380.     public void SimulateStop() {
  381.             MyStatusPanel.SimulationRunning = false;
  382.             StatusMessage("Simulation stoppped. Make a choice above or change the schematic.");
  383.             MyStatusPanel.repaint();
  384.     }
  385.  
  386. //----------------------------------------------------------------------------
  387. // Determine if the program is simulating
  388. //----------------------------------------------------------------------------
  389.     public boolean IsSimulating() {
  390.         return (MyStatusPanel.SimulationRunning || MyStatusPanel.SimulationInitializeInProgress);
  391.     }
  392.  
  393. //----------------------------------------------------------------------------
  394. // Simulate one clockcycle. Check for short-circuit and loops
  395. // and redraw schematic and analyzer after the Simulate cycle.
  396. //----------------------------------------------------------------------------
  397.     public void SimulateCycle() {
  398.         ElectronicComponent TempComponent;
  399.  
  400.         MySchematic.InitBeforeSimulate();
  401.         for (int ix = 0; ix < 4; ix++) {
  402.             MySchematic.Simulate();
  403.         }
  404.         if (StopAtShortCircuit) {
  405.             TempComponent = MySchematic.TestShortCircuit();
  406.             if (TempComponent != null) {
  407.                 String DlgButtons[] = { "OK" };
  408.                 String message = "Short circuit detected at " + TempComponent.getName() + " pos. " + TempComponent.Pos.x + ", "  + TempComponent.Pos.y ;
  409.                 SimpleDialog ExceptionDialog = new SimpleDialog(frame, "Short circuit detected", message, DlgButtons, 1, 0, 0, SimpleDialog.IMAGE_STOP);
  410.                 UserWantsSimulate();
  411.                 MySchematicPanel.repaint();
  412.                 MyStatusPanel.repaint();
  413.                 return;
  414.             }
  415.         }
  416.         if (StopAtLoop) {
  417.             TempComponent = MySchematic.TestLoop();
  418.             if (TempComponent != null) {
  419.                 String DlgButtons[] = { "OK" };
  420.                 String message = "Loop detected at " + TempComponent.getName() + " pos. " + TempComponent.Pos.x + ", "  + TempComponent.Pos.y ;
  421.                 SimpleDialog ExceptionDialog = new SimpleDialog(frame, "Loop detected", message, DlgButtons, 1, 0, 0, SimpleDialog.IMAGE_STOP);
  422.                 UserWantsSimulate();
  423.                 MySchematicPanel.repaint();
  424.                 MyStatusPanel.repaint();
  425.                 return;
  426.             }
  427.         }
  428.         if (MyAnalyzerFrame != null && MyAnalyzerFrame.MyAnalyzerPanel != null) {
  429.             MyAnalyzerFrame.MyAnalyzerPanel.update();
  430.         }
  431.  
  432.         MySchematicPanel.repaint();
  433.         MyStatusPanel.repaint();
  434.     }
  435.  
  436. //----------------------------------------------------------------------------
  437. // Add an Electronic Component to the schematic
  438. //----------------------------------------------------------------------------
  439.     public void addComponent (ElectronicComponent NewComponent) {
  440.         if (MySchematic == null) return;
  441.         MySchematicPanel.SelectSchematic.RemoveAllComponents();
  442.         MySchematic.addComponent(NewComponent);
  443.         if (!(NewComponent instanceof Wire)) {
  444.             UserWantsPointer();
  445.         }
  446.         MySchematicPanel.repaint();
  447.     }
  448.  
  449. //----------------------------------------------------------------------------
  450. // Add an Electronic Component with name ComponentChoice to the schematic.
  451. //----------------------------------------------------------------------------
  452.     public void addComponent(String ComponentChoice) {
  453.         if (MySchematicPanel == null) return;
  454.         if ("Wire".equals(ComponentChoice)) {
  455.             UserWantsWireDrawing();
  456.             return;
  457.         } else if ("Junction".equals(ComponentChoice)) {
  458.             UserWantsJunctionDrawing();
  459.             return;
  460.         } else if ("Vcc".equals(ComponentChoice)) {
  461.             addComponent(new Vcc(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  462.         } else if ("GND".equals(ComponentChoice)) {
  463.             addComponent(new GND(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset ));
  464.         } else if ("Switch".equals(ComponentChoice)) {
  465.             addComponent(new Switch(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  466.         } else if ("Push button".equals(ComponentChoice)) {
  467.             addComponent(new PushButton(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  468.  
  469.         } else if ("Buffer".equals(ComponentChoice)) {
  470.             addComponent(new Buffer(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  471.         } else if ("Inverter".equals(ComponentChoice)) {
  472.             addComponent(new Inverter(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  473.         } else if ("2-input AND port".equals(ComponentChoice)) {
  474.             addComponent(new TwoAndPort(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  475.         } else if ("3-input AND port".equals(ComponentChoice)) {
  476.             addComponent(new ThreeAndPort(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  477.         } else if ("2-input OR port".equals(ComponentChoice)) {
  478.             addComponent(new TwoOrPort(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  479.         } else if ("3-input OR port".equals(ComponentChoice)) {
  480.             addComponent(new ThreeOrPort(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  481.         } else if ("2-input XOR port".equals(ComponentChoice)) {
  482.             addComponent(new TwoXorPort(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  483.  
  484.         } else if ("2-input NAND port".equals(ComponentChoice)) {
  485.             addComponent(new TwoNandPort(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  486.         } else if ("3-input NAND port".equals(ComponentChoice)) {
  487.             addComponent(new ThreeNandPort(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  488.         } else if ("2-input NOR port".equals(ComponentChoice)) {
  489.             addComponent(new TwoNorPort(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  490.         } else if ("3-input NOR port".equals(ComponentChoice)) {
  491.             addComponent(new ThreeNorPort(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  492.         } else if ("2-input XNOR port".equals(ComponentChoice)) {
  493.             addComponent(new TwoXnorPort(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  494.  
  495.         } else if ("SR-latch".equals(ComponentChoice)) {
  496.             addComponent(new SRLatch(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  497.         } else if ("Gated SR-latch".equals(ComponentChoice)) {
  498.             addComponent(new GatedSRLatch(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  499.         } else if ("D-latch".equals(ComponentChoice)) {
  500.             addComponent(new DLatch(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  501.         } else if ("D-flipflop".equals(ComponentChoice)) {
  502.             addComponent(new DFlipFlop(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  503.         } else if ("JK-flipflop".equals(ComponentChoice)) {
  504.             addComponent(new JKFlipFlop(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  505.         } else if ("Edge-triggered T-flipflop".equals(ComponentChoice)) {
  506.             addComponent(new EdgeTriggeredTFlipFlop(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  507.         } else if ("T-flipflop".equals(ComponentChoice)) {
  508.             addComponent(new TFlipFlop(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  509.         } else if ("Octal D-flipflop".equals(ComponentChoice)) {
  510.             addComponent(new OctalDFlipFlop(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  511.         } else if ("Octal latch".equals(ComponentChoice)) {
  512.             addComponent(new OctalLatch(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  513.  
  514.         } else if ("Red LED".equals(ComponentChoice)) {
  515.             addComponent(new LED(PinGrid, Color.red, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  516.         } else if ("Green LED".equals(ComponentChoice)) {
  517.             addComponent(new LED(PinGrid, Color.green, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  518.         } else if ("Yellow LED".equals(ComponentChoice)) {
  519.             addComponent(new LED(PinGrid, Color.yellow, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset ));
  520.         } else if ("Bi-color LED".equals(ComponentChoice)) {
  521.             addComponent(new BiColorLED(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  522.         } else if ("7-segment display".equals(ComponentChoice)) {
  523.             addComponent(new SevenSegmentDisplay(PinGrid, Color.red, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  524.  
  525.         } else if ("Oscilator".equals(ComponentChoice)) {
  526.             addComponent(new Oscilator(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  527.         } else if ("Analyzer probe".equals(ComponentChoice)) {
  528.             addComponent(new Probe(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  529.             updateAnalyzer();
  530.         } else if ("BCD to 7-segment decoder".equals(ComponentChoice)) {
  531.             addComponent(new BCDToSevenSegDecoder(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  532.         } else if ("3- to 8-line decoder".equals(ComponentChoice)) {
  533.             addComponent(new ThreeToEightLineDecoder(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  534.         } else if ("4-bit binary counter".equals(ComponentChoice)) {
  535.             addComponent(new FourBitBinaryCounter(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  536.         } else if ("8-bit serial in shift register".equals(ComponentChoice)) {
  537.             addComponent(new EightBitSerInShiftReg(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  538.         } else if ("8-bit parallel in shift register".equals(ComponentChoice)) {
  539.             addComponent(new EightBitParInShiftReg(PinGrid, MySchematicPanel.GridXOffset, MySchematicPanel.GridYOffset));
  540.         }
  541.  
  542.         MySchematicPanel.WireDrawing = false;
  543.         MySchematicPanel.JunctionDrawing = false;
  544.         StatusMessage("Click and hold the mouse button in the body of the new " + ComponentChoice + " to move it.");
  545.     }
  546.  
  547. //----------------------------------------------------------------------------
  548. // Determine if all Simulator images are ready.
  549. //----------------------------------------------------------------------------
  550.     public boolean ImagesReady() {
  551.         return (ibg == null || GridImage == null || ImageBuffer == null || MySchematic == null) ?  false : true;
  552.     }
  553.  
  554. //----------------------------------------------------------------------------
  555. // Show a message to the user
  556. //----------------------------------------------------------------------------
  557.     public void StatusMessage(String msg) {
  558.         MyStatusPanel.StatusMessage(msg);
  559.     }
  560.  
  561. //----------------------------------------------------------------------------
  562. // Determine if the simulation is running
  563. //----------------------------------------------------------------------------
  564.     public boolean SimulateRunning() {
  565.         return MyStatusPanel.SimulateRunning();
  566.     }
  567.  
  568. //----------------------------------------------------------------------------
  569. // return applet info for use in appletviewer
  570. //----------------------------------------------------------------------------
  571.     public String getAppletInfo() {
  572.         return ("DigSim v 0.01 (c) 1996 Iwan van Rienen\nE-mail: ivr@bart.nl\nHomepage: http://www.bart.nl/~ivr/");
  573.     }
  574.  
  575. //----------------------------------------------------------------------------
  576. // Show all parameters
  577. //----------------------------------------------------------------------------
  578.     public String[][] getParameterInfo() {
  579.         String pinfo[][] = {
  580.             { "schematic",  "String",   "Initial schematic to load"} ,
  581.             { "fileop",     "Boolean",  "Local file operations enable"} };
  582.         return pinfo;
  583.     }
  584.  
  585. //----------------------------------------------------------------------------
  586. // Enable the pointer in the control panel
  587. //----------------------------------------------------------------------------
  588.     public void UserWantsPointer() {
  589.         HelpWanted = false;
  590.         if (MyControlPanel != null) {
  591.             MyControlPanel.SelectButton ("Pointer");
  592.             MyControlPanel.UnselectButton ("Wire");
  593.             MyControlPanel.UnselectButton ("Junction");
  594.             MyControlPanel.UnselectButton ("Text");
  595.             MyControlPanel.UnselectButton ("Help");
  596.             MyControlPanel.UnselectButton ("Simulate");
  597.             MyControlPanel.UnselectButton ("New");
  598.             MyControlPanel.UnselectButton ("Open");
  599.             MyControlPanel.UnselectButton ("Save");
  600.         }
  601.         if (MySchematicPanel != null) {
  602.             MySchematicPanel.WireDrawing = false;
  603.             MySchematicPanel.JunctionDrawing = false;
  604.             StatusMessage("Move the cursor to a component body or wire-end and press a mouse button.");
  605.         }
  606.    }
  607.  
  608. //----------------------------------------------------------------------------
  609. // Enable the Wire-drawing button in the control panel
  610. //----------------------------------------------------------------------------
  611.     public void UserWantsWireDrawing() {
  612.         if (HelpWanted) {
  613.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Wire");
  614.             UserWantsPointer();
  615.             return;
  616.         }
  617.  
  618.         if (MyControlPanel != null) {
  619.             MyControlPanel.UnselectButton ("Pointer");
  620.             MyControlPanel.SelectButton ("Wire");
  621.             MyControlPanel.UnselectButton ("Junction");
  622.             MyControlPanel.UnselectButton ("Text");
  623.             MyControlPanel.UnselectButton ("Help");
  624.             MyControlPanel.UnselectButton ("Simulate");
  625.             MyControlPanel.UnselectButton ("New");
  626.             MyControlPanel.UnselectButton ("Open");
  627.             MyControlPanel.UnselectButton ("Save");
  628.         }
  629.         if (MySchematicPanel != null) {
  630.             StatusMessage("Move the mouse to the desired position, click and hold the mouse button to draw a wire.");
  631.             MySchematicPanel.WireDrawing = true;
  632.             MySchematicPanel.JunctionDrawing = false;
  633.             MySchematicPanel.SelectSchematic.RemoveAllComponents();
  634.             MySchematicPanel.repaint();
  635.         }
  636.     }
  637.  
  638. //----------------------------------------------------------------------------
  639. // Enable the Junction place button in the control panel
  640. //----------------------------------------------------------------------------
  641.     public void UserWantsJunctionDrawing() {
  642.         if (HelpWanted) {
  643.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Junction");
  644.             UserWantsPointer();
  645.             return;
  646.         }
  647.  
  648.         if (MyControlPanel != null) {
  649.             MyControlPanel.UnselectButton ("Pointer");
  650.             MyControlPanel.UnselectButton ("Wire");
  651.             MyControlPanel.SelectButton ("Junction");
  652.             MyControlPanel.UnselectButton ("Text");
  653.             MyControlPanel.UnselectButton ("Help");
  654.             MyControlPanel.UnselectButton ("Simulate");
  655.             MyControlPanel.UnselectButton ("New");
  656.             MyControlPanel.UnselectButton ("Open");
  657.             MyControlPanel.UnselectButton ("Save");
  658.         }
  659.         if (MySchematicPanel != null) {
  660.             MySchematicPanel.WireDrawing = false;
  661.             MySchematicPanel.JunctionDrawing = true;
  662.             StatusMessage("Move the mouse to the desired position, and click the mouse button to add a junction.");
  663.             MySchematicPanel.SelectSchematic.RemoveAllComponents();
  664.             MySchematicPanel.repaint();
  665.         }
  666.    }
  667.  
  668. //----------------------------------------------------------------------------
  669. // Enable the Text-drawing button in the control panel
  670. //----------------------------------------------------------------------------
  671.     public void UserWantsTextDrawing() {
  672.         if (HelpWanted) {
  673.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Text");
  674.             UserWantsPointer();
  675.             return;
  676.         }
  677.         if (MyControlPanel != null) {
  678.             MyControlPanel.UnselectButton ("Pointer");
  679.             MyControlPanel.UnselectButton ("Wire");
  680.             MyControlPanel.UnselectButton ("Junction");
  681.             MyControlPanel.SelectButton ("Text");
  682.             MyControlPanel.UnselectButton ("Help");
  683.             MyControlPanel.UnselectButton ("Simulate");
  684.             MyControlPanel.UnselectButton ("New");
  685.             MyControlPanel.UnselectButton ("Open");
  686.             MyControlPanel.UnselectButton ("Save");
  687.         }
  688.         if (frame != null) {
  689.             if (frame.MyTextDialog == null) {
  690.                 frame.MyTextDialog = new TextDialog(frame, "", frame.NEWTEXTDIALOG_ID);
  691.             }
  692.             StatusMessage("Please type a new text.");
  693.             UserWantsPointer();
  694.         }
  695.     }
  696.  
  697. //----------------------------------------------------------------------------
  698. // Enable the Help button in the control panel
  699. //----------------------------------------------------------------------------
  700.     public void UserWantsHelp() {
  701.         if (MyControlPanel != null) {
  702.             MyControlPanel.UnselectButton ("Pointer");
  703.             MyControlPanel.UnselectButton ("Wire");
  704.             MyControlPanel.UnselectButton ("Junction");
  705.             MyControlPanel.UnselectButton ("Text");
  706.             MyControlPanel.SelectButton ("Help");
  707.             MyControlPanel.UnselectButton ("Simulate");
  708.             MyControlPanel.UnselectButton ("New");
  709.             MyControlPanel.UnselectButton ("Open");
  710.             MyControlPanel.UnselectButton ("Save");
  711.         }
  712.         if (HelpWanted) {
  713.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Help");
  714.         } else {
  715.             StatusMessage ("Choose a component, menu or button to get help about it.");
  716.             HelpWanted = true;
  717.         }
  718.     }
  719.  
  720. //----------------------------------------------------------------------------
  721. // User wants to start / stop the simulation.
  722. //----------------------------------------------------------------------------
  723.     public void UserWantsSimulate() {
  724.         if (MyControlPanel != null) {
  725.             MyControlPanel.UnselectButton ("Pointer");
  726.             MyControlPanel.UnselectButton ("Wire");
  727.             MyControlPanel.UnselectButton ("Junction");
  728.             MyControlPanel.UnselectButton ("Text");
  729.             MyControlPanel.UnselectButton ("Help");
  730.             MyControlPanel.SelectButton ("Simulate");
  731.             MyControlPanel.UnselectButton ("New");
  732.             MyControlPanel.UnselectButton ("Open");
  733.             MyControlPanel.UnselectButton ("Save");
  734.         }
  735.         if (HelpWanted) {
  736.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Simulate");
  737.             UserWantsPointer();
  738.             return;
  739.         }
  740.         if (!IsSimulating()) {
  741.             if (MyControlPanel != null) {
  742.                 MyControlPanel.DisableButton ("Pointer");
  743.                 MyControlPanel.DisableButton ("Wire");
  744.                 MyControlPanel.DisableButton ("Junction");
  745.                 MyControlPanel.DisableButton ("Text");
  746.                 MyControlPanel.DisableButton ("New");
  747.                 MyControlPanel.DisableButton ("Open");
  748.                 MyControlPanel.DisableButton ("Save");
  749.             }
  750.             frame.DisableAllMenus();
  751.             frame.StartMenuItem.disable();
  752.             frame.StopMenuItem.enable();
  753.             SimulateStart();
  754.             repaint();
  755.         } else {
  756.             if (MyControlPanel != null) {
  757.                 MyControlPanel.EnableButton ("Pointer");
  758.                 MyControlPanel.EnableButton ("Wire");
  759.                 MyControlPanel.EnableButton ("Junction");
  760.                 MyControlPanel.EnableButton ("Text");
  761.                 MyControlPanel.EnableButton ("New");
  762.                 if (EnableFileOperations) {
  763.                     MyControlPanel.EnableButton ("Open");
  764.                     MyControlPanel.EnableButton ("Save");
  765.                 }
  766.             }
  767.             frame.EnableAllMenus();
  768.             frame.StopMenuItem.disable();
  769.             frame.StartMenuItem.enable();
  770.             SimulateStop();
  771.             UserWantsPointer();
  772.             repaint();
  773.         }
  774.     }
  775.  
  776. //----------------------------------------------------------------------------
  777. // User pressed the 'new' button or menuitem
  778. //----------------------------------------------------------------------------
  779.     public void UserWantsNewSchematic() {
  780.         if (MySchematic == null) return;
  781.         if (MyControlPanel != null) {
  782.             MyControlPanel.UnselectButton ("Pointer");
  783.             MyControlPanel.UnselectButton ("Wire");
  784.             MyControlPanel.UnselectButton ("Junction");
  785.             MyControlPanel.UnselectButton ("Text");
  786.             MyControlPanel.UnselectButton ("Help");
  787.             MyControlPanel.UnselectButton ("Simulate");
  788.             MyControlPanel.SelectButton ("New");
  789.             MyControlPanel.UnselectButton ("Open");
  790.             MyControlPanel.UnselectButton ("Save");
  791.         }
  792.         if (HelpWanted) {
  793.             HelpDialog MyHelpDialog = new HelpDialog(frame, "New");
  794.             UserWantsPointer();
  795.             return;
  796.         } else {
  797.             if (MySchematic.Modified) {
  798.                 if (frame.NewDialog == null) {
  799.                     String DlgButtons[] = {"OK",  "Cancel"};
  800.                     frame.NewDialog = new SimpleDialog(frame, "New schematic", "Discard changes?", DlgButtons, 2, 1, frame.NEWDIALOG_ID, SimpleDialog.IMAGE_WARNING);
  801.                     UserWantsPointer();
  802.                 }
  803.             } else {
  804.                 MySchematic.DestroyComponents(PinGrid); // Destroy schematic and remove components from grid.
  805.                 frame.setTitle ("Digital Simulator [untitled]");
  806.                 MySchematic.FileName = null;
  807.                 MySchematic.Modified = false;
  808.                 MySchematicPanel.repaint();
  809.                 UserWantsPointer();
  810.                 updateAnalyzer();
  811.             }
  812.         }
  813.     }
  814.  
  815. //----------------------------------------------------------------------------
  816. // User pressed the 'Open' button or menuitem.
  817. //----------------------------------------------------------------------------
  818.     public void UserWantsOpenSchematic() {
  819.         Schematic OpenSchematic;
  820.         if (MySchematic == null) return;
  821.         if (MyControlPanel != null) {
  822.             MyControlPanel.UnselectButton ("Pointer");
  823.             MyControlPanel.UnselectButton ("Wire");
  824.             MyControlPanel.UnselectButton ("Junction");
  825.             MyControlPanel.UnselectButton ("Text");
  826.             MyControlPanel.UnselectButton ("Help");
  827.             MyControlPanel.UnselectButton ("Simulate");
  828.             MyControlPanel.UnselectButton ("New");
  829.             MyControlPanel.SelectButton ("Open");
  830.             MyControlPanel.UnselectButton ("Save");
  831.         }
  832.         if (HelpWanted) {
  833.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Open");
  834.             UserWantsPointer();
  835.             return;
  836.         } else {
  837.             if (MySchematic.Modified) {
  838.                 if (frame.OpenDialog == null) {
  839.                     String DlgButtons[] = {"OK",  "Cancel"};
  840.                     frame.OpenDialog = new SimpleDialog(frame, "Open schematic", "Discard changes?", DlgButtons, 2, 1, frame.OPENDIALOG_ID, SimpleDialog.IMAGE_WARNING);
  841.                     UserWantsPointer();
  842.                 }
  843.             } else {
  844.                 OpenSchematic = frame.DoFileOpenDialog(PinGrid, "Open schematic");
  845.                 if (OpenSchematic != null) {
  846.                     MySchematic = OpenSchematic;
  847.                     frame.setTitle("Digital Simulator [" + MySchematic.FileName + "]");
  848.                     MySchematicPanel.repaint();
  849.                 }
  850.                 UserWantsPointer();
  851.             }
  852.         }
  853.     }
  854.  
  855. //----------------------------------------------------------------------------
  856. // User clicked an Example (FileName)
  857. //----------------------------------------------------------------------------
  858.     public void UserWantsOpenExample(String FileName) {
  859.         if (MySchematic == null) return;
  860.         if (MySchematic.Modified) {
  861.             if (frame.OpenExampleDialog == null) {
  862.                 String DlgButtons[] = {"OK",  "Cancel"};
  863.                 frame.ExampleFileName = FileName;
  864.                 frame.OpenExampleDialog = new SimpleDialog(frame, "Open example", "Discard changes?", DlgButtons, 2, 1, frame.OPENEXAMPLE_ID, SimpleDialog.IMAGE_WARNING);
  865.                 UserWantsPointer();
  866.             }
  867.         } else {
  868.             frame.LoadExample(FileName);
  869.             UserWantsPointer();
  870.         }
  871.     }
  872.  
  873. //----------------------------------------------------------------------------
  874. // User pressed the 'save' button in the control panel.
  875. //----------------------------------------------------------------------------
  876.     public void UserWantsSaveSchematic() {
  877.         if (MySchematic == null) return;
  878.         if (MyControlPanel != null) {
  879.             MyControlPanel.UnselectButton ("Pointer");
  880.             MyControlPanel.UnselectButton ("Wire");
  881.             MyControlPanel.UnselectButton ("Junction");
  882.             MyControlPanel.UnselectButton ("Text");
  883.             MyControlPanel.UnselectButton ("Help");
  884.             MyControlPanel.UnselectButton ("Simulate");
  885.             MyControlPanel.UnselectButton ("New");
  886.             MyControlPanel.UnselectButton ("Open");
  887.             MyControlPanel.SelectButton ("Save");
  888.         }
  889.         if (HelpWanted) {
  890.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Save");
  891.             UserWantsPointer();
  892.             return;
  893.         } else {
  894.             if (MySchematic.FileName != null &&
  895.                 MySchematic.FileDir != null) {
  896.                 frame.DoFileSaveDialog(MySchematic, false, "Save schematic"); // save
  897.             } else {
  898.                 frame.DoFileSaveDialog(MySchematic, false, "Save schematic as"); // save as
  899.             }
  900.             frame.setTitle("Digital Simulator [" + MySchematic.FileName + "]");
  901.             MySchematicPanel.repaint();
  902.             UserWantsPointer();
  903.             return;
  904.         }
  905.     }
  906.  
  907. //----------------------------------------------------------------------------
  908. // User pressed the 'copy' button or menuitem.
  909. //----------------------------------------------------------------------------
  910.     public void UserWantsCopySchematic() {
  911.         if (HelpWanted) {
  912.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Copy");
  913.         } else {
  914.             MySchematicPanel.Copy();
  915.             frame.PasteMenuItem.enable();
  916.             MyControlPanel.EnableButton ("Paste");
  917.         }
  918.         MyControlPanel.UnselectButton ("Copy");
  919.         UserWantsPointer();
  920.     }
  921.  
  922. //----------------------------------------------------------------------------
  923. // User pressed the 'paste' button or menuitem.
  924. //----------------------------------------------------------------------------
  925.     public void UserWantsPasteSchematic() {
  926.         if (HelpWanted) {
  927.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Paste");
  928.         } else {
  929.             MySchematicPanel.Paste();
  930.         }
  931.         MyControlPanel.UnselectButton ("Paste");
  932.         UserWantsPointer();
  933.     }
  934.  
  935. //----------------------------------------------------------------------------
  936. // User pressed the 'cut' button or menuitem.
  937. //----------------------------------------------------------------------------
  938.     public void UserWantsCutSchematic() {
  939.         if (HelpWanted) {
  940.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Cut");
  941.         } else {
  942.             MySchematicPanel.Cut();
  943.             frame.CutMenuItem.disable();
  944.             frame.CopyMenuItem.disable();
  945.             frame.PasteMenuItem.enable();
  946.             MyControlPanel.DisableButton ("Cut");
  947.             MyControlPanel.DisableButton ("Copy");
  948.             MyControlPanel.EnableButton ("Paste");
  949.         }
  950.         MyControlPanel.UnselectButton ("Cut");
  951.         UserWantsPointer();
  952.     }
  953.  
  954. //----------------------------------------------------------------------------
  955. // User pressed the 'exampes' menuitem
  956. //----------------------------------------------------------------------------
  957.     public void UserWantsOpenExample() {
  958.         if (HelpWanted) {
  959.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Open example");
  960.         } else {
  961.             if (MyExamplesFrame == null) {
  962.                 MyExamplesFrame = new ExamplesFrame(this);
  963.             }
  964.         }
  965.     }
  966.  
  967. //----------------------------------------------------------------------------
  968. // User pressed the 'copy to' menuitem.
  969. //----------------------------------------------------------------------------
  970.     public void UserWantsCopyToSchematic() {
  971.          if (HelpWanted) {
  972.             HelpDialog MyHelpDialog = new HelpDialog(frame, "CopyTo");
  973.         } else {
  974.             MySchematicPanel.CopyTo();
  975.             frame.PasteMenuItem.enable();
  976.             MyControlPanel.EnableButton ("Paste");
  977.         }
  978.         UserWantsPointer();
  979.    }
  980.  
  981. //----------------------------------------------------------------------------
  982. // User pressed the 'paste from' menuitem.
  983. //----------------------------------------------------------------------------
  984.     public void UserWantsPasteFromSchematic() {
  985.         if (HelpWanted) {
  986.             HelpDialog MyHelpDialog = new HelpDialog(frame, "PasteFrom");
  987.         } else {
  988.             MySchematicPanel.PasteFrom();
  989.         }
  990.         UserWantsPointer();
  991.    }
  992.  
  993. //----------------------------------------------------------------------------
  994. // User wants the options window
  995. //----------------------------------------------------------------------------
  996.     public void UserWantsOptions() {
  997.         if (HelpWanted) {
  998.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Options");
  999.         } else {
  1000.              if (MyOptionsFrame == null) {
  1001.                 MyOptionsFrame = new OptionsFrame(this);
  1002.             }
  1003.        }
  1004.        UserWantsPointer();
  1005.     }
  1006.  
  1007. //----------------------------------------------------------------------------
  1008. // User wants the logic analyzer
  1009. //----------------------------------------------------------------------------
  1010.     public void UserWantsAnalyzer() {
  1011.         if (HelpWanted) {
  1012.             HelpDialog MyHelpDialog = new HelpDialog(frame, "Analyzer");
  1013.         } else {
  1014.              if (MyAnalyzerFrame == null) {
  1015.                 MyAnalyzerFrame = new AnalyzerFrame(this);
  1016.             }
  1017.        }
  1018.     }
  1019.  
  1020. //----------------------------------------------------------------------------
  1021. // Update the analyzer data after a simulation cycle.
  1022. //----------------------------------------------------------------------------
  1023.     public void updateAnalyzer() {
  1024.         if (MyAnalyzerFrame != null) {
  1025.             MyAnalyzerFrame.repaint();
  1026.         }
  1027.     }
  1028. }