Source code: engine/DisplayTask.java
1
2 /* **********************************
3 File:DisplayTask.java
4 Author: Alec(panovici@elcom.pub.ro)
5 Created on 12.02.1999 02:41:30
6 Updated by Power
7 Comments: Part of the vIDE Project
8 Copyright 1999 the vIDE Team.
9 ***********************************/
10
11 package engine;
12 import java.util.*;
13
14 /**
15 * This is the implementation of the <code>$display</code> task.
16 * Other tasks, such as <code>$strobe, $write</code> && stuff may also use this
17 * as a starting point.
18 */
19
20 class DisplayTask extends DefaultInstruction{
21 Vector target;
22 int defaultBase;
23
24 DisplayTask(NameSpace ns, int delayBefore,
25 InstructionDescription desc, int defaultBase,
26 Vector target, Instruction next){
27 super(ns, delayBefore, desc);
28 this.target = target;
29 this.defaultBase = defaultBase;
30 add(next);
31 }
32
33 DisplayTask(DisplayTask dt){
34 this(dt.ns, dt.delayBefore, dt.desc, dt.defaultBase, dt.target, dt.next);
35 }
36
37 public void execute()
38 throws InterpretTimeException, SimulationStoppedException
39 {
40 waitToRun();
41 xConsole.trace(this + "");
42 String output = "", format = null, s;
43 for(Enumeration e = target.elements(); e.hasMoreElements(); ) {
44 Object o = e.nextElement();
45 if (o == null) output += " ";
46 else
47 if (o instanceof String) {
48 s = o.toString();
49 if (s.equals("%m") || s.equals("%M")) output += ns.toString();
50 else if (s.startsWith("%")) format = s;
51 else output += s;
52 } else { // expression
53 if((format != null) && (format.equalsIgnoreCase("%v"))){
54 //strength:
55 WireSelection se = (WireSelection) o; //type is checked
56 //in instantiate
57 if(se.length() > 1){
58 output += "{";
59 for(int bit = se.length()-1 ; ; bit--){
60 int strength;
61 try{
62 strength = se.getStrength(bit);
63 }catch(ClassCastException cex){
64 throw new
65 InterpretTimeException(desc + ": error: a wire or wire concatenation must be used with %V format spec");
66 }
67 output += Strength.
68 strengthToString((byte) (strength & 0xff), strength >> 16);
69 if(bit == 0)break;
70 output += ", ";
71 }
72 output += "}";
73 }else{
74 int strength = se.getStrength(0);
75 output += Strength.
76 strengthToString((byte) (strength & 0xff), strength >> 16);
77 }
78 }else{
79 Result rs = null;
80 try{
81 rs = ((Expression)o).evaluate();
82 }catch(InterpretTimeException ex){
83 xConsole.dumpStack(ex);
84 throw new InterpretTimeException(desc + ": error:" + ex);
85 }
86 xConsole.debug("$display: " + rs);
87 if (format == null) output += rs.toString(defaultBase);
88 else output += format(rs, format.charAt(1));
89 format = null;
90 }
91 }
92 }
93 xConsole.display(output);
94 }
95
96 String format(Result rs, char f) {
97 switch (f) {
98 case 'h': case 'H': return rs.toString(16);
99 case 'd': case 'D': return rs.toString(10);
100 case 'o': case 'O': return rs.toString(8);
101 case 'b': case 'B': return rs.toString(2);
102 case 'v': case 'V':
103 xConsole.debug("WARNING: DisplayTask.format with argument %V");
104 break; //this is handled inside execute !
105 case 's': case 'S': return rs.toString(256); // in format String
106 case 't': case 'T': return "" + rs.getLong();
107 case 'c': case 'C': {
108 String r = rs.toString(256);
109 return r.substring(0,1);
110 }
111 case 'e': case 'E': return rs.getReal().toString(); // ieee 754
112 case 'f': case 'F': return rs.getReal().toDecimalString(); // a.b
113 case 'g': case 'G':
114 String s1 = rs.getReal().toString(), s2 = rs.getReal().toDecimalString();
115 return (s1.length() < s2.length() ? s1 : s2);
116 }
117 return "[?]";
118 }
119
120 public String toString(){
121 return desc.toString();
122 }
123 }
124