-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCircuitSim.java
More file actions
executable file
·71 lines (61 loc) · 1.83 KB
/
Copy pathCircuitSim.java
File metadata and controls
executable file
·71 lines (61 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// CircuitSim.java
// by David Cline
// Make jar file: jar cvfe CircuitSim.jar CircuitSim *.class
public class CircuitSim
{
public static void main(String args[])
{
// Check to see if we have enough args
System.out.println();
if (args.length < 1) {
System.out.println("Usage: circuitName [testFile]");
System.exit(0);
}
// Massage the circuit name to get rid of .txt if necessary
String circuitName = args[0];
if (circuitName.endsWith(".txt")) {
circuitName = circuitName.substring(0,circuitName.length()-4);
}
// Load the circuit
Circuit c = new Circuit();
c.loadFromFile(circuitName, "");
System.out.println("");
// Propagation delay
for (int i=0; i<c.inputs.length; i++) c.inputs[i]=0;
c.simulatePropagationDelay();
System.out.print("Propagation Delays: ");
for (int i=0; i<c.outputs.length; i++) System.out.print(c.outputs[i] + " ");
System.out.println();
// If there are no more arguments, print truth table lines.
if (args.length == 1) {
c.printTruthTable(1024);
}
// Otherwise we have an input, run the circuit on it
else if (args[1].charAt(0) == '0' || args[1].charAt(0) == '1') {
int inputNum = 0;
for (int argNum=1; argNum<args.length; argNum++) {
String arg = args[argNum];
for (int i=0; i<arg.length(); i++) {
if (inputNum >= c.inputs.length) break;
char d = arg.charAt(i);
if (d=='0' || d=='1') {
c.inputs[inputNum] = d - '0';
inputNum++;
}
}
}
c.simulate();
System.out.println("");
c.printInputOutputNames();
c.printInputOutput();
}
// Otherwise we have a test file
else {
String testFile = args[1];
if (testFile.endsWith(".txt")) {
testFile = testFile.substring(0,testFile.length()-4);
}
c.runTestCase(testFile);
}
}
}