-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimulate.js
executable file
·626 lines (586 loc) · 36 KB
/
simulate.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
simulate.js
Author: Niraj Menon
File creation date: 1/5/2020
Code first written: 31/7/2019
Description:
Contains handler and supporting code for authorized incoming
WebSocket connection.
The handler checks if the incoming data is either Verilog code or
button inputs from the simulator. It remembers if Verilog code was
first sent by maintaining state information for each WebSocket
connection from every user.
The Verilog code is syntaxically checked, compiled, synthesized,
and finally simulated with the help of external tools like CVC/Icarus
and Yosys. Any errors in the code will cause the WebSocket to send
them back and immediately close the connection.
*/
const fs = require('fs-extra');
const os = require('os');
const cp = require('child_process');
const crypto = require('crypto');
const path = require('path');
const rimraf = require('rimraf'),
hostname = os.hostname();
/*
https://stackoverflow.com/questions/18052762/remove-directory-which-is-not-empty
*/
function deleteFile(dir, file) {
return new Promise(function (resolve, reject) {
try {
var filePath = path.join(dir, file);
fs.lstat(filePath, function (err, stats) {
if (err) {
return reject(err);
}
if (stats.isDirectory()) {
resolve(deleteDirectory(filePath));
} else {
fs.unlink(filePath, function (err) {
if (err) {
return reject(err);
}
resolve();
});
}
});
}
catch (err) {
reject (err)
}
});
}
function deleteDirectory(dir) {
return new Promise(function (resolve, reject) {
fs.access(dir, function (err) {
if (err) {
return reject(err);
}
fs.readdir(dir, function (err, files) {
if (err) {
return reject(err);
}
Promise.all(files.map(function (file) {
return deleteFile(dir, file);
})).then(function () {
fs.rmdir(dir, function (err) {
if (err) {
return reject(err);
}
resolve();
});
}).catch(reject);
});
});
});
}
function deleteFolderRecursive (path) {
rimraf (path, (err) => {
if (err) console.log (err)
})
};
/*****************************************************/
function debugLog(message) {
console.log(getTime() + ": " + message)
}
String.prototype.replaceAll = function (search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
function getTime() {
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var min = (today.getMinutes() < 10 ? '0' + today.getMinutes() : today.getMinutes())
var sec = (today.getSeconds() < 10 ? '0' + today.getSeconds() : today.getSeconds())
var time = today.getHours() + ":" + min + ":" + sec;
return (date + ' ' + time);
}
function connection(ws, request) {
ws.currentState = "INIT";
username = "demouser"
ws.on('message', function incoming(message) {
switch (ws.currentState) {
case "INIT":
var payload = JSON.parse (message);
var workspace = payload.files;
ws.simType = payload.settings.simType;
ws.codelist = [];
var hash = crypto.createHash('sha256');
workspace.forEach (file => { hash.update(file.code) });
hash.update ((new Date().getTime()).toString());
hash.update (username);
ws.unique_client = hash.digest('hex');
ws.simulator_object = null;
ws.filenames = [];
fs.mkdirSync(path.resolve('/var/tmp/tmpcode', ws.unique_client), (err) => {
if (err) { throw err; }
});
workspace.forEach (file => {
if (file.code.includes("give us a demo please")) {
file.code = fs.readFileSync(`${__dirname}/sim_modules/fpgademo.v`, 'utf8', function (err, data) {
if (err) throw err;
});
}
else if (file.code.includes("give us a uart demo please")) {
file.code = fs.readFileSync(`${__dirname}/sim_modules/uart_fpgademo.v`, 'utf8', function (err, data) {
if (err) throw err;
});
}
fs.writeFileSync(path.resolve('/var/tmp/tmpcode', ws.unique_client, file.name), file.code, 'utf8', function (err) {
if (err) { throw err; }
});
ws.filenames.push (file.name);
ws.codelist.push (file.code);
});
// Triggering "Synthesizing..." on webpage
ws.send("Processing Verilog code...")
var supports = payload.settings.support.map (e => 'support/' + e);
var error = [], modded_error = [], error_line = [];
var FILES;
try {
const VERILATOR = "verilator"
const VFLAGS = "--lint-only --top-module top"
SUPPORTS = supports.reduce ((p,n) => p + " " + n, "") + " "
FILES = `${__dirname}/sim_modules/cells_sim_timing.v ${__dirname}/sim_modules/cells_map_timing.v `
FILES += ws.filenames.filter (f => f.endsWith ('.sv')).map (f => path.resolve ('/var/tmp/tmpcode', ws.unique_client, f)).join (' ')
const WARNINGS = ['%Warning-WIDTH', '%Warning-SELRANGE', '%Warning-COMBDLY']
var err_regex = new RegExp ("(?:%Error|" + WARNINGS.join ('|') + ")(?:\-[A-Z0-9]+)?: " + '/var/tmp/tmpcode/[a-z0-9]+/([^:]+)' + ":([0-9]+):[0-9]+: (.+)", "g")
var err_regex_single = new RegExp ("(?:%Error|" + WARNINGS.join ('|') + ")(?:\-[A-Z0-9]+)?: " + '/var/tmp/tmpcode/[a-z0-9]+/([^:]+)' + ":([0-9]+):[0-9]+: (.+)")
var ignore_err_rgx = /(This may be because there\'s no search path specified with)/
cp.execSync ([VERILATOR, VFLAGS, SUPPORTS, FILES].join (" "), { cwd: __dirname })
}
catch (e) {
if (e) {
var errors = []
ws.verilatorLog = e.message
if (e.message.match (err_regex)) {
e.message.match (err_regex).forEach (elm => {
var json_ignore = elm.includes ('Cannot find file containing module') && ws.filenames.filter (f => f.endsWith ('.json')).length > 0
var ignore_errors = ignore_err_rgx.test (elm)
if (errors.length - 1 >= 0 && (errors[errors.length - 1].line == elm.match (err_regex_single)[2]) && !json_ignore && !ignore_errors)
errors[errors.length - 1].error = errors[errors.length - 1].error + "\n" + elm.match (err_regex_single)[3]
else if (!json_ignore && !ignore_errors) {
errors.push ({
name: elm.match (err_regex_single)[1],
line: elm.match (err_regex_single)[2],
error: elm.match (err_regex_single)[3]
})
}
})
}
error_line = errors
}
else {
error_line = []
}
}
error_line.forEach (err => {
modded_error.push (err.name + ": Line " + err.line + ": Verilator - " + err.error)
})
if (modded_error.length > 0) {
if (!fs.existsSync(__dirname + "/error_log/" + username)) {
fs.mkdirSync(path.resolve(__dirname + "/error_log/", username), (err) => {
if (err) { throw err; }
})
}
fs.moveSync("/var/tmp/tmpcode/" + ws.unique_client, __dirname + "/error_log/" + username + "/" + getTime().replaceAll(" ", "_") + "_" + ws.unique_client)
ws.send(ws.verilatorLog + "\nYosys did not run." + "\nCompilation failed with the following error:\n" + modded_error.join('\n'))
ws.close()
return
}
// 5/23/2021
// run Yosys only if we're doing mapped simulations
if (ws.simType == 'mapped') {
// remove yosys files from list
FILES = FILES.replace (`${__dirname}/sim_modules/cells_sim_timing.v ${__dirname}/sim_modules/cells_map_timing.v `, '')
JSONS = ws.filenames.filter (f => f.endsWith ('.json'));
SUPPORTS = supports.map (e => `${__dirname}/${e}`).reduce ((p,n) => `${p} ${n}`, "") + " ";
try {
yosys_out = cp.execSync('yosys -p ' +
(JSONS.length > 0 ? ('"read_json ' + JSONS.join (' ') + '; ') : '" ') +
`read_verilog -sv ${SUPPORTS} ${FILES}; ` +
'synth_ice40 -top top; ' +
'write_verilog /var/tmp/tmpcode/' + ws.unique_client + '/struct_code.v; ' +
'write_json /var/tmp/tmpcode/' + ws.unique_client + '/struct.json;" ' +
'-l /var/tmp/tmpcode/' + ws.unique_client + '/yosyslog', {timeout: 60000, cwd: path.resolve ('/var/tmp/tmpcode', ws.unique_client)})
ws.yosysJSON = fs.readFileSync(path.resolve('/var/tmp/tmpcode', ws.unique_client, 'struct.json')).toString()
fs.unlinkSync(path.resolve('/var/tmp/tmpcode', ws.unique_client, 'yosyslog'))
ws.initYosys = true
}
catch (ex) {
if (ex.errno == 'ETIMEDOUT') {
// Can't kill Yosys by PID since it changes after execSync for some weird reason
// So we got to use ps and find it by its unique client ID
psy = cp.execSync ("ps -eo pcpu,pid,args | sort -k1 -r -n | grep yosys").toString().split ("\n")
psy = psy.map (el => {
if (el.slice (0, 1) == ' ')
return el.slice (1)
else
return el
}).filter (el => el != '')
psy.forEach (proc => {
if (proc == '')
return
pcpu = parseFloat (proc.slice (0, proc.indexOf (" "))); proc = proc.slice (proc.indexOf (" ") + 1)
pid = parseInt (proc.slice (0, proc.indexOf (" "))); args = proc.slice (proc.indexOf (" ") + 1)
if (parseFloat (pcpu) > 90 && args.includes ("yosys") && args.includes (ws.unique_client)) {
try {
process.kill (pid);
}
catch (err) {
console.error ("Unable to kill Yosys PID " + pid || 'pid undefined');
console.error (err);
}
}
});
ws.send ("YOSYS HUNG: " + ex.output.toString())
ws.close()
return;
}
yosys_out = fs.readFileSync(path.resolve('/var/tmp/tmpcode', ws.unique_client, 'yosyslog'));
ws.yosysJSON = 'No JSON was produced because Yosys ran into an error.';
fs.unlinkSync(path.resolve('/var/tmp/tmpcode', ws.unique_client, 'yosyslog'));
ws.initYosys = false;
function indexOfReg(arr, search) {
for (var elm in arr) {
try {
if (typeof arr[elm] == 'string' && arr[elm].match(search)) {
d = new Object();
d.message = arr[elm];
d.lineno = arr.indexOf(arr[elm]);
return d;
}
}
catch (ex) {
debugLog('indexOfReg failed');
debugLog(arr);
d = new Object();
d.message = "[ERROR] Internal server error. Please try again later.";
d.lineno = 1;
return d;
}
}
}
// debugLog (yosys_out.toString())
logarray = yosys_out.toString().split("\n")
error_msg = indexOfReg(logarray, (/Error/i))
if (error_msg && error_msg.message.includes("server error")) {
debugLog("FATAL: Yosys encountered an unknown error unrelated to code.")
}
else // Is a mapping error
{
related_error = indexOfReg(logarray, (/Error/i))
skippable_error_regex = /syntax error, unexpected \$end/i
if (related_error && !related_error.message.match(skippable_error_regex)) {
// Lazy catch-all for if there was an error but no other details were captured.
if (related_error.message.includes('server error')) {
error.push("**/var/tmp/tmpcode/hiddentmpcodefolderpath/" + ws.filenames.filter (f => f.endsWith ('.sv'))[0] + "(1) [SERVER_ERROR] Your code has errors unrecognized by the server. Make sure to check for missing semicolons, wrong flip flop declarations and so on.")
}
// If students try to pull an output low when it is already connected to a high.
if (related_error.message.includes("Mismatch in directionality for cell port")) {
var codeline = related_error.message.replace(/ERROR: Mismatch in directionality for cell port [^ ]+ /, '')
error.push("**/var/tmp/tmpcode/hiddentmpcodefolderpath/" + ws.filenames.filter (f => f.endsWith ('.sv'))[0] + "(1) [SYNTHESIS_ERROR] You are trying to drive a signal with two different sources. More information is in: " + codeline)
}
// Wrong flip flop reset logic
else if (related_error.message.includes("Multiple edge sensitive events")) {
var codeline = logarray[logarray.indexOf(related_error.message) - 1]
var filename = codeline.match(/([\w]+\.sv):([0-9]+)/)[1]
codeline = parseInt(codeline.match(/([\w]+\.sv):([0-9]+)/)[2])
error.push("**/var/tmp/tmpcode/hiddentmpcodefolderpath/" + filename + "(" + codeline + ") [SYNTHESIS_ERROR] This flip flop does not have correct reset logic.")
}
// For all 'if (reset) perform invalid logic' statements
else if (related_error.message.match(/yields non-constant value/i)) {
var signal = logarray[related_error.lineno].match(/\\([\w]+) yields non-constant value/i)[1]
msg = "**/var/tmp/tmpcode/hiddentmpcodefolderpath/" + ws.filenames.filter (f => f.endsWith ('.sv'))[0] + "(" + related_error.lineno + ")"
msg += "Two asynchronous resets not allowed in flip flop. '" + signal + "' does not appear to be a valid reset for this FF."
error.push(msg)
}
// Edge triggering signals for the flip flop cannot be mapped to real flip-flop models.
else if (related_error.message.match(/Found non-synthesizable event list!/i)) {
var codeline = logarray[logarray.indexOf(related_error.message)]
var filename = codeline.match(/([\w]+\.sv):([0-9]+)/)[1]
codeline = parseInt(codeline.match(/([\w]+\.sv):([0-9]+)/)[2])
error.push("**/var/tmp/tmpcode/hiddentmpcodefolderpath/" + filename + "(" + codeline + ") [SYNTHESIS_ERROR] The port list for this flip flop cannot be mapped to real hardware.")
}
// missing module errors (safest if checked here since JSON would have been parsed)
else if (related_error.message.match(/referenced in module [^ ]+ in cell [^ ]+ is not part of the design/i)) {
error.push("**/var/tmp/tmpcode/hiddentmpcodefolderpath/" + ws.filenames.filter (f => f.endsWith ('.sv'))[0] + "(1) [SYNTHESIS_ERROR] " + related_error.message.replace ('ERROR: ', ''))
}
else if (related_error.message.match(/ERROR: Module `top' not found!/i)) {
error.push("**/var/tmp/tmpcode/hiddentmpcodefolderpath/" + ws.filenames.filter (f => f.endsWith ('.sv'))[0] + "(1) [MISSING_MODULE] The top module appears to be missing. ")
}
else if (related_error.message.match(/ERROR: Identifier `[^']+' doesn't map to any signal/i)) {
[ign, message, filename, lineno] = related_error.message.match (/ERROR: (Identifier `[^']+' doesn't map to any signal) at \/tmp\/tmpcode\/[a-z0-9]+\/([^\.]+\.sv):([0-9]+)/)
error.push("**/var/tmp/tmpcode/hiddentmpcodefolderpath/" + filename + "(" + lineno + ") " + message)
}
else {
general_error = /\/tmp\/tmpcode\/[a-z0-9]+\/([^\.]+\.sv)\:([0-9]+)\: ?(.+)/
try {
filename = related_error.message.match(general_error)[1]
lineno = related_error.message.match(general_error)[2] || "1"
message = related_error.message.match(general_error)[3]
error.push("**/var/tmp/tmpcode/hiddentmpcodefolderpath/" + filename + "(" + lineno + ") " + message)
}
catch (ex) {
var message = related_error.message
if (related_error.message.includes ("cmd error aborting 'source ")) { // known to appear when outputs are multiply driven
error.push("**/var/tmp/tmpcode/hiddentmpcodefolderpath/"+ ws.filenames.filter (f => f.endsWith ('.sv'))[0] +"(1)No line number information - " + message + "\nPossible duplicate connections to outputs.")
}
else {
error.push("**/var/tmp/tmpcode/hiddentmpcodefolderpath/"+ ws.filenames.filter (f => f.endsWith ('.sv'))[0] +"(1)No line number information - " + message)
}
}
}
}
}
}
// Custom error checks based on Regex
blif_unmapped_ffs = []
try {
if (error.length == 0) {
blif = fs.readFileSync('/var/tmp/tmpcode/' + ws.unique_client + '/temp.blif').toString().split('\n')
valid_flip_flops = ['_DFFE',
'_DFFSR',
'_DFFR',
'_DFFSS',
'_DFFS',
'_DFFESR',
'_DFFER',
'_DFFESS',
'_DFFES',
'_DFFN',
'_DFFNE',
'_DFFNSR',
'_DFFNR',
'_DFFNSS',
'_DFFNS',
'_DFFNESR',
'_DFFNER',
'_DFFNESS',
'_DFFNES']
blif.filter(function (v, i, a) {
if (v.match("_DFF") && !valid_flip_flops.includes(v.match("_DFF[^ ]+")[0])) {
blif_unmapped_ffs.push(a[i])
}
})
}
}
catch (ex) {
// ignore.
}
// Line-by-line checks (no inline logic initializations, etc.)
ws.codelist.forEach ((e, fi) => {
linebyline = e.split('\n')
linebyline.filter(function (v, i, a) {
// Test for flip flops with both asynchronous reset and set, which are not valid flip flops in hardware.
while (blif_unmapped_ffs.length > 0) {
missing_flip_flop = blif_unmapped_ffs[0]
clock = missing_flip_flop.match("C\=[^ ]+")
console.log("clock: " + clock)
reset = missing_flip_flop.match("R\=[^ ]+")
console.log("reset: " + reset)
always_regex = new RegExp("always(\_ff)? ?@ ?\\((?:pos|neg)edge [^ ,]+, ?(?:pos|neg)edge [^ ,]+, ?(?:pos|neg)edge [^\\)]+\\)?")
ws.verilogCode.split('\n').filter(function (v, i, a) {
if (v.match(always_regex)) {
// console.log (v)
message = "[CUSTOM_ERROR] Flip flops cannot have both an asynchronous set and reset. Use only either one."
error.push('**/var/tmp/tmpcode/hiddentmpcodefolderpath/' + ws.filenames[fi] + '(' + (i + 1).toString() + "): " + message)
}
})
blif_unmapped_ffs = blif_unmapped_ffs.slice(1)
}
ta_override = e.match (/give me a magic override against the inline logic assignment/gi)
startup_reg = !v.match(/(?:reg|logic) ?\[2\:0\] ?startup ?\= ?0\;/gi)
not_equating = !v.match(/(?:reg|logic) +[^=]+\={2}/i)
is_reg_or_logic = v.match(/(?:logic|reg) +[^=]+= *.+/gi)
// added 10/24/2020
not_typedef = !v.match(/typedef enum (?:logic|reg)? (?:[\[\]0-9\:]+) \{?/gi)
reg_regex = !ta_override && not_equating && !v.match(/(?:reg|logic) +\=+/i) && not_typedef && is_reg_or_logic && startup_reg && !v.match(/\<\=/gi)
if (reg_regex) {
message = "[CUSTOM_ERROR] You should not initialize a reg/logic outside an always block!"
error.push('**/var/tmp/tmpcode/hiddentmpcodefolderpath/' + ws.filenames[fi] + '(' + (i + 1).toString() + "): " + message)
}
})
})
}
if (ws.simType == 'mapped' && error.length != '0') {
modded_error = []
if (!fs.existsSync(__dirname + "/error_log/" + username)) {
fs.mkdirSync(path.resolve(__dirname + "/error_log/", username), (err) => {
if (err) { throw err; }
})
}
fs.moveSync("/var/tmp/tmpcode/" + ws.unique_client, "error_log/" + username + "/" + getTime().replaceAll(" ", "_") + "_" + ws.unique_client)
error.forEach(function (element) {
modded_err_rgx = /\*?\*?\/var\/tmp\/tmpcode\/[a-z0-9]+\/([\w]+\.sv):?\(? ?([0-9]+)\)?/
colon_check = /\.sv\([0-9]+\)\:/
try {
var name = element.match(modded_err_rgx)[1]
var num = parseInt(element.match(modded_err_rgx)[2])
if (colon_check.test (element))
modded_err_msg = element.replace(modded_err_rgx, name + ': Line ' + num.toString())
else
modded_err_msg = element.replace(modded_err_rgx, name + ': Line ' + num.toString() + ": ")
modded_err_msg = modded_err_msg.replace(/\/var\/tmp\/tmpcode\/[^\/]+\/([\w]+\.sv):[0-9]+: /, '')
modded_error.push(modded_err_msg)
}
catch (ex) {
debugLog("Cannot parse this error: " + element)
num = 1
}
})
if (modded_error.length != '0') {
ws.send("Verilator log:\n" + (ws.verilatorLog || 'Verilator produced no logs.') +
"\nYosys-produced JSON:\n" + ws.yosysJSON +
"\nYosys log:\n" + yosys_out +
"\nCompilation failed with the following error:\n" + modded_error.join('\n'))
ws.close()
}
}
else {
// Introduced Yosys-produced gate-level synthesis approach
ws.currentState = "SIMULATE";
ws.rxdata = []; ws.recvInput = '';
ws.rxclk = 'x'; ws.txclk = 'x';
ws.rxready = 'x';
var env = Object.create(process.env);
debugLog("Starting " + ws.unique_client + ' on IcarusVerilog');
// additional compile step - ugh
var IVL = 'iverilog ';
var VARGS = `-o /var/tmp/tmpcode/${ws.unique_client}/simcomm.vvp -g2012 -gspecify `;
if (ws.simType == 'source') {
var FILES = `${__dirname}/sim_modules/simcomm.sv /var/tmp/tmpcode/${ws.unique_client}/*.sv ` ;
var CELLS = ``; // no need to use cell files for source simulations
}
else {
var FILES = `${__dirname}/sim_modules/simcomm.sv /var/tmp/tmpcode/${ws.unique_client}/struct_code.v ` ;
var CELLS = `${__dirname}/sim_modules/cells_sim_timing.v ${__dirname}/sim_modules/cells_map_timing.v `;
}
// run compile step
try {
var output = cp.execSync(IVL + VARGS + FILES + CELLS, { cwd: `/var/tmp/tmpcode/${ws.unique_client}` });
}
catch (err) {
console.error (err)
ws.send('Error occurred in Icarus compile step: ' + err.stderr.toString().replace(new RegExp(`/var/tmp/tmpcode/${ws.unique_client}/`, "g"), ''));
ws.close();
return;
}
// now for the actual simulation
var sargs = `-M. -m ${__dirname}/sim_modules/simcomm /var/tmp/tmpcode/${ws.unique_client}/simcomm.vvp`.split(" ");
env.RECV_PIPE = '1';
env.SEND_PIPE = '0';
try {
// pipe stderr to stdout just in case we don't catch something
ws.simulator_object = cp.spawn('vvp', sargs, { cwd: `/var/tmp/tmpcode/${ws.unique_client}`, env: env, stdio: [ 'pipe', 'pipe', process.stdout ] });
}
catch (err) {
console.error (err)
console.error (err.stderr.toString())
ws.send('Error occurred in Icarus simulation: ' + err.stderr.toString());
ws.close();
return;
}
ws.send("Simulation successfully started!\nVerilator log:\n" + (ws.verilatorLog || 'Verilator produced no logs.') +
"\nYosys-produced JSON:\n" + ws.yosysJSON + "\nYosys log:\n" + (typeof(yosys_out) == "undefined" ? 'Yosys did not run - might be a source simulation.' : yosys_out))
ws.simTimeout = false;
ws.error_caught = false;
ws.simulator_object.stdout.on('data', (indata) => {
var data = indata.toString('utf8').trim()
var msgdata = data.replaceAll('\0', '')
try {
// normal simulation output will look like a JSON object: {'output_name': output_value...}
// if parsing of the object fails, it must be an error message
var parsed = JSON.parse(msgdata)
ws.send(JSON.stringify (parsed))
}
catch (ex) {
if (msgdata.includes("10 minutes exceeded")) {
ws.simTimeout = true;
ws.send("TIME LIMIT EXCEEDED");
ws.simulator_object.kill('SIGTERM');
}
else if (msgdata.includes("iming violation")) {
ws.send("TIMING VIOLATION");
}
else if (msgdata.includes("VCD info: dumpfile")) {
// ignore.
}
else if (ws.readyState == 1) {
if (msgdata.includes(" Continue ") || msgdata.includes("Flushing output streams")) {
// Meh. Skip.
// It's either literally continuing, or it's stopping.
// ws.simulator_object.kill('SIGTERM');
}
else if (!msgdata.includes ('{"LFTRED"') && !msgdata.includes ('10 minutes exceeded')) {
debugLog('IcarusVerilog gave unexpected output for ' + ws.unique_client + ' (' + username + '): ');
debugLog(msgdata);
ws.error_caught = true;
}
}
}
});
ws.simulator_object.on('exit', (code, signal) => {
if (fs.existsSync(`/var/tmp/tmpcode/${ws.unique_client}/trace.vcd`)) {
fs.readFile(`/var/tmp/tmpcode/${ws.unique_client}/trace.vcd`, (err, data) => {
if (err) ws.send(JSON.stringify({'vcd': 'Error in reading VCD data. Please try again.'}));
else ws.send(JSON.stringify({'vcd': data.toString()}));
ws.close()
});
}
else if (!ws.simTimeout && !ws.error_caught && ws.readyState == 1) {
debugLog("FATAL: Simulation has quit.")
debugLog('Code ' + code)
debugLog('Signal ' + signal)
if (signal == 'SIGINT')
ws.send("SIGINTED")
else if (signal == 'SIGKILL')
ws.send ('SIM HUNG')
ws.send("END SIMULATION")
ws.close()
}
});
}
break;
case "SIMULATE":
try {
ws.recvInput = message;
if (ws.recvInput == "END SIMULATION") {
ws.simulator_object.kill("SIGTERM");
}
else {
ws.simulator_object.stdin.write(ws.recvInput);
}
}
catch (ex) {
// debugLog('Tried writing a message: ' + ws.recvInput + ' to closed CVC process, ignoring...')
}
break;
}
});
ws.on('close',
function close() {
try {
if (ws.simulator_object) {
deleteFolderRecursive(path.resolve('/var/tmp/tmpcode', ws.unique_client))
debugLog("Stopped " + ws.unique_client)
ws.simulator_object.kill('SIGINT')
}
// try to find CVC process and kill it with a SIGTERM
cmd = "ps -eo pcpu,pid,args | grep " + ws.unique_client
psy = cp.execSync (cmd).toString().split ("\n")
psy.forEach ((ps) => {
extract = ps.match (/([0-9]+\.[0-9]) ([0-9]+) (.+)/)
if (extract && extract.length != 4)
console.log ('Malformed ps output: ' + extract)
else if (extract && extract.includes ("vvp -M"))
process.kill (extract[2], 'SIGTERM')
})
}
catch (ex) {
debugLog(ex.toString());
}
}
)
}
module.exports = connection