forked from izuzak/FRISCjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frisc-console.js
244 lines (207 loc) · 7.8 KB
/
frisc-console.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
var fs = require("fs");
var path = require("path");
var readline = require('readline');
var asm = require("./friscasm.js");
var sim = require("./friscsim.js").FRISC;
var argv = process.argv;
var isVerboseMode = argv.indexOf("-v") > -1;
if (isVerboseMode) {
argv.splice(argv.indexOf("-v"), 1);
}
var debug = function(str) {
if (isVerboseMode) {
console.error(str);
}
};
var cpufreq = 1000;
if (argv.indexOf("-cpufreq") > -1) {
cpufreq = parseInt(argv[argv.indexOf("-cpufreq") + 1], 10);
argv.splice(argv.indexOf("-cpufreq") + 1, 1);
argv.splice(argv.indexOf("-cpufreq"), 1);
}
var memsize = 256*1024;
if (argv.indexOf("-memsize") > -1) {
memsize = 1024*parseInt(argv[argv.indexOf("-memsize") + 1], 10);
argv.splice(argv.indexOf("-memsize") + 1, 1);
argv.splice(argv.indexOf("-memsize"), 1);
}
console.error("");
console.error("*********************************************************");
console.error("** FRISCjs - FRISC simulator in JavaScript");
console.error("** ");
console.error("** Usage instructions:");
console.error("** ");
console.error("** pass filename argument with the FRISC program:");
console.error("** > node main.js filename");
console.error("** or input the FRISC program via stdin:");
console.error("** > cat filename | node main.js");
console.error("** ");
console.error("** Note: the simulator has a memory module of 256KB,");
console.error("** i.e. from 0x00000000 to 0x0003FFFF.");
console.error("** ");
console.error("** Verbose debugging mode can be turned on by passing");
console.error("** specifying the -v flag, e.g.:");
console.error("** ");
console.error("** > node main.js -v filename");
console.error("** or");
console.error("** > cat filename | node main.js -v");
console.error("** ");
console.error("** The CPU frequency (in Hz) can be set with the -cpufreq");
console.error("** flag argument (default value is 1000):");
console.error("** ");
console.error("** > node main.js -cpufreq 2 filename");
console.error("** or");
console.error("** > cat filename | node main.js -cpufreq 2");
console.error("** ");
console.error("** Memory size (in number of 1K locations) can be set with");
console.error("** the -memsize flag argument (default value is 256):");
console.error("** ");
console.error("** > node main.js -memsize 64 filename");
console.error("** or");
console.error("** > cat filename | node main.js -memsize 64");
console.error("** ");
console.error("** Execution flow:");
console.error("** ");
console.error("** 1) compilation of the FRISC program to machine code ");
console.error("** 2) step-by-step execution of FRISC program ");
console.error("** with logging of processor state at each step ");
console.error("** to stderr (if in verbose mode)");
console.error("** 3) output of r6 to stdout after program execution");
console.error("** ");
console.error("** GUI version of FRISC simulator is available at:");
console.error("** ");
console.error("** http://fer-ppj.github.com/FRISCjs/main.html");
console.error("** ");
console.error("** Bug reports:");
console.error("** ");
console.error("** mailto:[email protected], or ");
console.error("** http://github.com/fer-ppj/FRISCjs/issues");
console.error("*********************************************************");
console.error("");
// determine method for acquiring the input program
if (argv.length > 2) {
// the process is passed an argument which points to a filename with the
// program
var filename = path.normalize(argv[2]);
console.error("");
console.error("*********************************************************");
console.error("Reading program from file: " + filename);
console.error("*********************************************************");
console.error("");
if (!(fs.existsSync(filename))) {
console.error("ERROR: File does not exist!");
console.log("ERROR");
} else {
var program = fs.readFileSync(filename);
runProgram(program);
}
} else {
// the process should read the program from process.stdin
console.error("");
console.error("*********************************************************");
console.error("Reading program from stdin, line-by-line.");
console.error("Press CTRL+D to stop reading and execute program.");
console.error("*********************************************************");
console.error("");
var program = "";
process.stdin.setEncoding('utf8');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.setPrompt("");
rl.on('line', function (line) {
program += line + '\n';
});
rl.on('close', function () {
runProgram(program);
});
rl.on('SIGINT', function () {
process.exit(0);
});
}
function cpuStateToString(simulator) {
var retVal = "";
for (var key in simulator.CPU._r) {
if (key !== 'sr') {
retVal += key + ": " + simulator.CPU._r[key].toString() + " ";
} else {
retVal += key + ": " + simulator.CPU._r[key].toString() + " ( ";
for (var flag in simulator.CPU._f) {
retVal += flag + ": " + simulator.CPU._getFlag(simulator.CPU._f[flag]) + " ";
}
retVal += ") ";
}
}
return retVal;
}
function instructionToString(instruction) {
if (typeof instruction.args === 'undefined' || instruction.args === null) {
return instruction.op;
} else {
return instruction.op + " " + instruction.args.join(" ");
}
}
function runProgram(frisc_asmsource) {
frisc_asmsource = frisc_asmsource.toString();
if (frisc_asmsource[frisc_asmsource.length-1] != '\n') {
frisc_asmsource += '\n'; // the parser blows up if there's no newline at the end of input
}
console.error("");
console.error("*********************************************************");
console.error("Input FRISC program:");
console.error("*********************************************************");
console.error("");
console.error(frisc_asmsource);
console.error("");
console.error("*********************************************************");
console.error("Parsing input FRISC program.");
console.error("*********************************************************");
console.error("");
var result;
try {
result = asm.parse(frisc_asmsource.toString());
} catch (e) {
console.error("Parsing error on line " + e.line + " column " + e.column + " -- " + e.toString());
return;
}
simulator = new sim();
simulator.MEM._size = memsize;
simulator.CPU._frequency = cpufreq;
simulator.CPU.onBeforeRun = function() {
console.error("");
console.error("*********************************************************");
console.error("Starting simulation!");
console.error("*********************************************************");
console.error("");
};
simulator.CPU.onBeforeCycle = function() {
debug("");
debug("*********************************************************");
debug("New CPU cycle starting!");
debug("*********************************************************");
debug("");
debug("## CPU state: " + cpuStateToString(simulator));
};
simulator.CPU.onAfterCycle = function() {
debug("## CPU state: " + cpuStateToString(simulator));
};
simulator.CPU.onBeforeExecute = function(instruction) {
debug("## Executing FRISC instruction: " + instructionToString(instruction));
};
simulator.CPU.onStop = function() {
console.error("");
console.error("*********************************************************");
console.error("FRISC processor stopped! Status of CPU R6: " + simulator.CPU._r.r6);
console.error("*********************************************************");
console.error("");
console.log(simulator.CPU._r.r6);
};
try {
simulator.MEM.loadBinaryString(result.mem);
} catch (e) {
console.error("Loading error -- " + e.toString());
return;
}
simulator.CPU.run(true); // run in "fastests" mode
}