1 /*
2 * MythSim
3 *
4 * Copyright (C) 2002-2004 Jason Vroustouris <jasonv@jasonv.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 package org.mythsim.swing;
22
23 import javax.swing.*;
24 import javax.swing.event.*;
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.io.*;
28 import java.net.*;
29 import org.mythsim.core.*;
30 import org.mythsim.swing.*;
31 import org.mythsim.swing.plugin.*;
32
33 /***
34 * Swing interface to the simulator.
35 * @author Jason Vroustouris
36 */
37 public class MythSimSwing
38 extends MythSim {
39 public static final int OPEN_ACTION = 0;
40 public static final int RELOAD_ACTION = 1;
41 public static final int RESET_ACTION = 2;
42 public static final int LAST_ACTION = 3;
43 public static final int MINUS100_ACTION = 4;
44 public static final int MINUS10_ACTION = 5;
45 public static final int MINUS1_ACTION = 6;
46 public static final int PLUS1_ACTION = 7;
47 public static final int PLUS10_ACTION = 8;
48 public static final int PLUS100_ACTION = 9;
49 public static final int NEXT_ACTION = 10;
50 public static final int RUN_ACTION = 11;
51 public static final int EXIT_ACTION = 12;
52
53 private Action mainAction[] = new MainAction[13];
54
55 JCheckBoxMenuItem pmi[] = null;
56 MythPlugInFrame plugin[] = new MythPlugInFrame[7];
57 File currentDirectory = null;
58 MainJFrame f = null;
59 JMenu pluginJMenu = new JMenu("Window");
60
61
62
63 /* ******************************************************
64 Constructors
65 ****************************************************** */
66 public MythSimSwing(MainJFrame _f) {
67 super();
68 f = _f;
69 plugin[0] = new ALUFrame(this, f);
70 plugin[1] = new DatapathFrame(this, f);
71 plugin[2] = new MemoryFrame(this, f);
72 plugin[3] = new MemorySourceFrame(this, f);
73 plugin[4] = new RegisterFrame(this, f);
74 plugin[5] = new MicrostoreFrame(this, f);
75 plugin[6] = new MicrocodeSourceFrame(this, f);
76 //plugin[4] = new LocationFrame(this,f);
77 //((LocationFrame)plugin[4]).setup(plugin);
78
79 pmi = new JCheckBoxMenuItem[plugin.length];
80
81 for (int i = 0; i < plugin.length; i++) {
82 //pmi[i] = new JCheckBoxMenuItem(plugin[i].getTitle());
83 pmi[i] = new JCheckBoxMenuItem(plugin[i].getAction());
84 plugin[i].setJCheckBoxMenuItem(pmi[i]);
85
86 pmi[i].addActionListener(plugin[i]);
87 pmi[i].setState(plugin[i].isVisible());
88 pluginJMenu.add(pmi[i]);
89 }
90 mainAction[OPEN_ACTION] = new MainAction(OPEN_ACTION, "Open",
91 "Open source files.");
92 mainAction[RELOAD_ACTION] = new MainAction(RELOAD_ACTION, "Reload",
93 "Reload source files.",
94 "images/reload.gif");
95 mainAction[RESET_ACTION] = new MainAction(RESET_ACTION, "Restart",
96 "Start again at address zero.",
97 "images/reset.gif");
98 mainAction[LAST_ACTION] = new MainAction(LAST_ACTION, "Previous",
99 "Step back to previous opcode.",
100 "images/last.gif");
101 mainAction[MINUS100_ACTION] = new MainAction(MINUS100_ACTION, "-100",
102 "Step back 100 clock cycles.",
103 "images/minus100.gif");
104 mainAction[MINUS10_ACTION] = new MainAction(MINUS10_ACTION, "-10",
105 "Step back 10 clock cycles.",
106 "images/minus10.gif");
107 mainAction[MINUS1_ACTION] = new MainAction(MINUS1_ACTION, "-1",
108 "Step back 1 clock cycle.",
109 "images/minus1.gif");
110 mainAction[PLUS1_ACTION] = new MainAction(PLUS1_ACTION, "+1",
111 "Step forward 1 clock cycle.",
112 "images/plus1.gif");
113 mainAction[PLUS10_ACTION] = new MainAction(PLUS10_ACTION, "+10",
114 "Step forward 10 clock cycles.",
115 "images/plus10.gif");
116 mainAction[PLUS100_ACTION] = new MainAction(PLUS100_ACTION, "+100",
117 "Step forward 100 clock cycles.", "images/plus100.gif");
118 mainAction[NEXT_ACTION] = new MainAction(NEXT_ACTION, "Next",
119 "Step forward to next opcode.",
120 "images/next.gif");
121 mainAction[RUN_ACTION] = new MainAction(RUN_ACTION, "Run",
122 "Run until end.",
123 "images/run.gif");
124 mainAction[EXIT_ACTION] = new MainAction(EXIT_ACTION, "Exit",
125 "Exit the simulator.");
126 setMode(NO_FILES_MODE);
127 }
128
129 /* ******************************************************
130 Action Functions
131 ****************************************************** */
132 /*** OPEN_ACTION */
133 public void open() throws Exception {
134 FileJDialog fileJDialog = new FileJDialog(this);
135 if (fileJDialog.isValid()) {
136 reload();
137 stepPlugins();
138 }
139 fileJDialog.dispose();
140 }
141
142 /*** RELOAD_ACTION */
143 public void reload() throws Exception {
144 f.clean();
145 System.out.print("Loading Source Files...\n");
146 System.out.print("Mem File: " + getMemoryFilePath() +
147 "\n");
148 System.out.print("Ucode File: " + getMicrocodeFilePath() +
149 "\n");
150 f.setTitleLine(" " + getMemoryFileName() + " - " + getMicrocodeFileName());
151 super.parse();
152 System.out.print("Parsing...\n");
153 setMode(VALID_FILES_MODE);
154 boot();
155 System.out.print("Started.\n");
156 }
157
158 /*** RESET_ACTION */
159 public void reset() throws Exception {
160 f.clean();
161 super.boot();
162 stepPlugins();
163 System.out.print("Started.\n");
164 }
165
166 /*** LAST_ACTION */
167 public void last() throws MythError {
168 super.last();
169 stepPlugins();
170 }
171
172 /*** MINUS100_ACTION, MINUS10_ACTION, MINUS1_ACTION,
173 PLUS1_ACTION, PLUS10_ACTION, PLUS100_ACTION */
174 public void step(int x) throws MythError {
175 super.step(x);
176 stepPlugins();
177 }
178
179 /*** NEXT_ACTION */
180 public void next() throws MythError {
181 super.next();
182 stepPlugins();
183 }
184
185 /*** RUN_ACTION */
186 public void run() throws MythError {
187 System.out.print("Running.\n");
188 super.run();
189 stepPlugins();
190 }
191
192 /*** EXIT_ACTION */
193 public void exit_app() {
194 System.out.print("Exiting...\n");
195 System.exit(0);
196 }
197
198 /* ******************************************************
199 Action Class
200 ****************************************************** */
201 /*** Most actions in the swing interface go thorough this system. */
202 class MainAction
203 extends AbstractAction {
204 int actionNumber = 0;
205 public MainAction(int an, String title, String description) {
206 super(title);
207 actionNumber = an;
208 putValue(SHORT_DESCRIPTION, description);
209 }
210
211 public MainAction(int an, String title, String description, ImageIcon img) {
212 super(title, img);
213 actionNumber = an;
214 putValue(SHORT_DESCRIPTION, description);
215 }
216
217 public MainAction(int an, String title, String description, ImageIcon img,
218 Integer mnemonic) {
219 super(title, img);
220 actionNumber = an;
221 putValue(SHORT_DESCRIPTION, description);
222 putValue(ACCELERATOR_KEY, mnemonic);
223 }
224
225 public MainAction(int an, String title, String description, String img) {
226 super(title);
227 try {
228 ClassLoader cl = this.getClass().getClassLoader();
229 URL url = null;
230 url = cl.getResource(img);
231 putValue(SMALL_ICON, new ImageIcon(url));
232 actionNumber = an;
233 putValue(SHORT_DESCRIPTION, description);
234 }
235 catch (Exception ex) {
236 System.out.println("Not found: " + img);
237 }
238 }
239
240 public MainAction(int an, String title, String description, String img,
241 Integer mnemonic) {
242 super(title);
243 try {
244 ClassLoader cl = this.getClass().getClassLoader();
245 URL url = cl.getResource(img);
246 putValue(SMALL_ICON, new ImageIcon(url));
247 actionNumber = an;
248 putValue(SHORT_DESCRIPTION, description);
249 putValue(ACCELERATOR_KEY, mnemonic);
250 }
251 catch (Exception ex) {
252 System.out.println("Not found: " + img);
253 }
254 }
255
256 public void actionPerformed(ActionEvent e) {
257 try {
258 switch (actionNumber) {
259 case OPEN_ACTION:
260 open();
261 break;
262 case RELOAD_ACTION:
263 reload();
264 break;
265 case RESET_ACTION:
266 reset();
267 break;
268 case LAST_ACTION:
269 last();
270 break;
271 case MINUS100_ACTION:
272 step( -100);
273 break;
274 case MINUS10_ACTION:
275 step( -10);
276 break;
277 case MINUS1_ACTION:
278 step( -1);
279 break;
280 case PLUS1_ACTION:
281 step(1);
282 break;
283 case PLUS10_ACTION:
284 step(10);
285 break;
286 case PLUS100_ACTION:
287 step(100);
288 break;
289 case NEXT_ACTION:
290 next();
291 break;
292 case RUN_ACTION:
293 run();
294 break;
295 case EXIT_ACTION:
296 exit_app();
297 break;
298 default:
299 break;
300 }
301 }
302 catch (MythError me) {
303 //System.out.print("MythError: " + me.toString() + "\n");
304 //me.printStackTrace();
305 System.out.print(me.message() + "\n");
306 stepPlugins();
307 }
308 catch (MythParserError mpe) {
309 setMode(INVALID_FILES_MODE);
310 f.step();
311 System.out.print(mpe.message() + "\n");
312 mpe.printStackTrace();
313 }
314 catch (Exception ee) {
315 System.out.print("Internal Error: ");
316 ee.printStackTrace();
317 }
318 }
319 }
320
321 /* ******************************************************
322 Action Support Functions
323 ****************************************************** */
324
325 /*** Freezes buttons and menus based on a current mode value. */
326 public void setMode(int _mode) {
327 mode = _mode;
328 switch (mode) {
329 case NO_FILES_MODE:
330 mainAction[RELOAD_ACTION].setEnabled(false);
331 mainAction[RESET_ACTION].setEnabled(false);
332 mainAction[LAST_ACTION].setEnabled(false);
333 mainAction[MINUS100_ACTION].setEnabled(false);
334 mainAction[MINUS10_ACTION].setEnabled(false);
335 mainAction[MINUS1_ACTION].setEnabled(false);
336 mainAction[PLUS1_ACTION].setEnabled(false);
337 mainAction[PLUS10_ACTION].setEnabled(false);
338 mainAction[PLUS100_ACTION].setEnabled(false);
339 mainAction[NEXT_ACTION].setEnabled(false);
340 mainAction[RUN_ACTION].setEnabled(false);
341 for (int i = 0; i < plugin.length; i++) {
342 plugin[i].setEnabled(false);
343 }
344
345 break;
346 case INVALID_FILES_MODE:
347 mainAction[RELOAD_ACTION].setEnabled(true);
348 mainAction[RESET_ACTION].setEnabled(false);
349 mainAction[LAST_ACTION].setEnabled(false);
350 mainAction[MINUS100_ACTION].setEnabled(false);
351 mainAction[MINUS10_ACTION].setEnabled(false);
352 mainAction[MINUS1_ACTION].setEnabled(false);
353 mainAction[PLUS1_ACTION].setEnabled(false);
354 mainAction[PLUS10_ACTION].setEnabled(false);
355 mainAction[PLUS100_ACTION].setEnabled(false);
356 mainAction[NEXT_ACTION].setEnabled(false);
357 mainAction[RUN_ACTION].setEnabled(false);
358 for (int i = 0; i < plugin.length; i++) {
359 plugin[i].setEnabled(false);
360 }
361 break;
362 case VALID_FILES_MODE:
363 mainAction[RELOAD_ACTION].setEnabled(true);
364 mainAction[RESET_ACTION].setEnabled(true);
365 mainAction[LAST_ACTION].setEnabled(true);
366 mainAction[MINUS100_ACTION].setEnabled(true);
367 mainAction[MINUS10_ACTION].setEnabled(true);
368 mainAction[MINUS1_ACTION].setEnabled(true);
369 mainAction[PLUS1_ACTION].setEnabled(true);
370 mainAction[PLUS10_ACTION].setEnabled(true);
371 mainAction[PLUS100_ACTION].setEnabled(true);
372 mainAction[NEXT_ACTION].setEnabled(true);
373 mainAction[RUN_ACTION].setEnabled(true);
374 for (int i = 0; i < plugin.length; i++) {
375 plugin[i].setEnabled(true);
376 }
377 break;
378 default:
379 break;
380 }
381 }
382
383 public int getMode() {
384 return mode;
385 }
386
387 public Action getAction(int index) {
388 return mainAction[index];
389 }
390
391 public JMenuItem createJMenuItem(int index) {
392 return new JMenuItem(mainAction[index]);
393 }
394
395 public JButton createJButton(int index) {
396 JButton temp = new JButton(mainAction[index]);
397 temp.setText("");
398 return temp;
399 }
400
401 /*** @return A JMenu will plugin in it. */
402 public JMenu getPluginJMenu() {
403 return pluginJMenu;
404 }
405
406 /*** Setup each of the plugins. */
407 public void setupPlugins() {
408 for (int i = 0; i < plugin.length; i++) {
409 plugin[i].setup();
410 }
411 f.step();
412 }
413
414 /*** Step each of the plugins. */
415 public void stepPlugins() {
416 for (int i = 0; i < plugin.length; i++) {
417 plugin[i].step();
418 }
419 f.step();
420 }
421
422 /*** Boot comptuer. */
423 public void boot() throws MythError {
424 super.boot();
425 setupPlugins();
426 stepPlugins();
427 }
428
429 }
This page was automatically generated by Maven