2017-11-11 19:45:32 +00:00
|
|
|
|
2018-07-11 04:04:28 +00:00
|
|
|
type V2JS_Var = {
|
|
|
|
wordlen:number,
|
|
|
|
name:string,
|
|
|
|
len:number,
|
|
|
|
ofs:number,
|
|
|
|
arrdim?:number[]
|
|
|
|
}
|
|
|
|
|
|
|
|
type V2JS_Code = {
|
|
|
|
name:string,
|
|
|
|
ports:V2JS_Var[],
|
|
|
|
signals:V2JS_Var[],
|
|
|
|
funcs:string[],
|
|
|
|
}
|
|
|
|
|
|
|
|
type V2JS_Output = {
|
|
|
|
output:{
|
|
|
|
code:V2JS_Code,
|
|
|
|
name:string,
|
|
|
|
ports:V2JS_Var[],
|
|
|
|
signals:V2JS_Var[],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function translateVerilatorOutputToJS(htext:string, cpptext:string) {
|
|
|
|
|
|
|
|
var moduleName : string;
|
|
|
|
var symsName : string;
|
2017-11-11 19:45:32 +00:00
|
|
|
|
2018-07-11 04:04:28 +00:00
|
|
|
function parseDecls(text:string, arr:V2JS_Var[], name:string, bin?:boolean, bout?:boolean) {
|
2017-11-22 14:42:07 +00:00
|
|
|
var re = new RegExp(name + "(\\d*)[(](\\w+),(\\d+),(\\d+)[)]", 'gm');
|
2017-11-11 19:45:32 +00:00
|
|
|
var m;
|
|
|
|
while ((m = re.exec(text))) {
|
|
|
|
arr.push({
|
|
|
|
wordlen:parseInt(m[1]),
|
|
|
|
name:m[2],
|
2018-02-26 23:18:23 +00:00
|
|
|
len:parseInt(m[3])+1,
|
2017-11-11 19:45:32 +00:00
|
|
|
ofs:parseInt(m[4]),
|
|
|
|
});
|
|
|
|
}
|
2017-11-22 14:42:07 +00:00
|
|
|
re = new RegExp(name + "(\\d*)[(](\\w+)\\[(\\d+)\\],(\\d+),(\\d+)[)]", 'gm');
|
2017-11-15 00:12:52 +00:00
|
|
|
var m;
|
|
|
|
while ((m = re.exec(text))) {
|
|
|
|
arr.push({
|
|
|
|
wordlen:parseInt(m[1]),
|
|
|
|
name:m[2],
|
2017-11-18 19:51:25 +00:00
|
|
|
arrdim:[parseInt(m[3])],
|
2018-02-26 23:18:23 +00:00
|
|
|
len:parseInt(m[4])+1,
|
2017-11-15 00:12:52 +00:00
|
|
|
ofs:parseInt(m[5]),
|
|
|
|
});
|
|
|
|
}
|
2017-11-22 14:42:07 +00:00
|
|
|
re = new RegExp(name + "(\\d*)[(](\\w+)\\[(\\d+)\\]\\[(\\d+)\\],(\\d+),(\\d+)[)]", 'gm');
|
2017-11-18 19:51:25 +00:00
|
|
|
var m;
|
|
|
|
while ((m = re.exec(text))) {
|
|
|
|
arr.push({
|
|
|
|
wordlen:parseInt(m[1]),
|
|
|
|
name:m[2],
|
|
|
|
arrdim:[parseInt(m[3]), parseInt(m[4])],
|
2018-02-26 23:18:23 +00:00
|
|
|
len:parseInt(m[5])+1,
|
2017-11-18 19:51:25 +00:00
|
|
|
ofs:parseInt(m[6]),
|
|
|
|
});
|
|
|
|
}
|
2017-11-11 19:45:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 04:04:28 +00:00
|
|
|
function buildModule(o : V2JS_Code) : string {
|
2017-11-11 19:45:32 +00:00
|
|
|
var m = '"use strict";\n';
|
|
|
|
m += '\tvar self = this;\n';
|
|
|
|
for (var i=0; i<o.ports.length; i++) {
|
|
|
|
m += "\tself." + o.ports[i].name + ";\n";
|
|
|
|
}
|
|
|
|
for (var i=0; i<o.signals.length; i++) {
|
2017-11-18 19:51:25 +00:00
|
|
|
var sig = o.signals[i];
|
|
|
|
if (sig.arrdim) {
|
|
|
|
if (sig.arrdim.length == 1) {
|
|
|
|
m += "\tvar " + sig.name + " = self." + sig.name + " = [];\n";
|
|
|
|
} else if (sig.arrdim.length == 2) {
|
|
|
|
m += "\tvar " + sig.name + " = self." + sig.name + " = [];\n";
|
|
|
|
m += "\tfor(var i=0; i<" + sig.arrdim[0] + "; i++) { " + sig.name + "[i] = []; }\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m += "\tself." + sig.name + ";\n";
|
|
|
|
}
|
2017-11-11 19:45:32 +00:00
|
|
|
}
|
|
|
|
for (var i=0; i<o.funcs.length; i++) {
|
|
|
|
m += o.funcs[i];
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:38:56 +00:00
|
|
|
function getStats(o : V2JS_Code) {
|
|
|
|
var nmembits = 0;
|
|
|
|
var nlines = 0;
|
|
|
|
for (var sig of o.signals.concat(o.ports)) {
|
|
|
|
var len = sig.len || 0;
|
|
|
|
if (sig.arrdim)
|
|
|
|
for (var n of sig.arrdim)
|
|
|
|
len *= n;
|
|
|
|
nmembits += len;
|
|
|
|
}
|
|
|
|
for (var fn of o.funcs) {
|
|
|
|
nlines += fn.split('\n').length;
|
|
|
|
}
|
|
|
|
//console.log(nmembits,'bits',nlines,'lines');
|
|
|
|
return {bits:nmembits, lines:nlines};
|
|
|
|
}
|
|
|
|
|
2018-07-11 04:04:28 +00:00
|
|
|
function translateFunction(text : string) : string {
|
2017-11-11 19:45:32 +00:00
|
|
|
text = text.trim();
|
|
|
|
var funcname = text.match(/(\w+)/)[1];
|
|
|
|
text = text.replace(symsName + "* __restrict ", "");
|
|
|
|
text = text.replace(moduleName + "* __restrict vlTOPp VL_ATTR_UNUSED", "var vlTOPp");
|
2017-11-22 21:31:30 +00:00
|
|
|
text = text.replace(/\bVL_DEBUG_IF\(([^]+?)\);\n/g,"/*VL_DEBUG_IF($1);*/\n");
|
|
|
|
//text = text.replace(/\bVL_DEBUG_IF/g,"!__debug__?0:\n");
|
|
|
|
text = text.replace(/\bVL_SIG(\d*)[(](\w+),(\d+),(\d+)[)]/g, 'var $2');
|
2017-11-11 19:45:32 +00:00
|
|
|
text = text.replace(/\b->\b/g, ".");
|
|
|
|
text = text.replace('VL_INLINE_OPT', '');
|
|
|
|
text = text.replace(/[(]IData[)]/g, '');
|
|
|
|
text = text.replace(/\b(0x[0-9a-f]+)U/gi, '$1');
|
|
|
|
text = text.replace(/\b([0-9]+)U/gi, '$1');
|
2017-11-18 19:51:25 +00:00
|
|
|
text = text.replace(/\bQData /g, 'var ');
|
|
|
|
text = text.replace(/\bbool /g, '');
|
|
|
|
text = text.replace(/\bint /g, 'var ');
|
2017-11-22 14:42:07 +00:00
|
|
|
text = text.replace(/(\w+ = VL_RAND_RESET_)/g, 'self.$1'); // TODO?
|
2017-11-22 23:23:00 +00:00
|
|
|
//text = text.replace(/^\s*(\w+ = \d+;)/gm, 'self.$1'); // TODO?
|
2017-11-15 00:12:52 +00:00
|
|
|
//text = text.replace(/(\w+\[\w+\] = VL_RAND_RESET_I)/g, 'self.$1');
|
2017-11-11 19:45:32 +00:00
|
|
|
text = text.replace(/^#/gm, '//#');
|
|
|
|
text = text.replace(/VL_LIKELY/g, '!!');
|
|
|
|
text = text.replace(/VL_UNLIKELY/g, '!!');
|
2018-07-24 15:38:56 +00:00
|
|
|
//[%0t] %Error: scoreboard.v:53: Assertion failed in %Nscoreboard_top.scoreboard_gen: reset 64 -935359306 Vscoreboard_top
|
2017-11-11 19:45:32 +00:00
|
|
|
text = text.replace(/Verilated::(\w+)Error/g, 'console.log');
|
2017-11-22 21:31:30 +00:00
|
|
|
text = text.replace(/vlSymsp.name[(][)]/g, '"'+moduleName+'"');
|
2017-11-11 19:45:32 +00:00
|
|
|
return "function " + text + "\nself." + funcname + " = " + funcname + ";\n";
|
|
|
|
}
|
|
|
|
|
2018-07-11 04:04:28 +00:00
|
|
|
function translateStaticVars(text : string) : string {
|
2017-11-13 05:24:19 +00:00
|
|
|
var s = "";
|
|
|
|
var m;
|
|
|
|
var re = /VL_ST_SIG(\d+)[(](\w+?)::(\w+).(\d+).,(\d+),(\d+)[)]/g;
|
|
|
|
while (m = re.exec(text)) {
|
|
|
|
s += "var " + m[3] + " = this." + m[3] + " = new Uint" + m[1] + "Array(" + m[4] + ");\n";
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-11-11 19:45:32 +00:00
|
|
|
// parse header file
|
|
|
|
moduleName = /VL_MODULE.(\w+)./.exec(htext)[1];
|
|
|
|
symsName = moduleName + "__Syms";
|
|
|
|
var ports = [];
|
|
|
|
parseDecls(htext, ports, 'VL_IN', true, false);
|
|
|
|
parseDecls(htext, ports, 'VL_OUT', false, true);
|
|
|
|
var signals = [];
|
|
|
|
parseDecls(htext, signals, 'VL_SIG');
|
|
|
|
|
|
|
|
// parse cpp file
|
|
|
|
// split functions
|
|
|
|
var re_fnsplit = new RegExp("(?:void|QData) " + moduleName + "::");
|
|
|
|
var functexts = cpptext.split(re_fnsplit);
|
|
|
|
var funcs = [];
|
2017-11-13 05:24:19 +00:00
|
|
|
funcs.push(translateStaticVars(functexts[0]));
|
2017-11-11 19:45:32 +00:00
|
|
|
for (var i=4; i<functexts.length; i++) {
|
|
|
|
var fntxt = translateFunction(functexts[i]);
|
|
|
|
funcs.push(fntxt);
|
|
|
|
}
|
2018-07-24 15:38:56 +00:00
|
|
|
|
|
|
|
var modinput = {
|
|
|
|
name:moduleName,
|
|
|
|
ports:ports,
|
|
|
|
signals:signals,
|
|
|
|
funcs:funcs,
|
|
|
|
};
|
|
|
|
|
2017-11-11 19:45:32 +00:00
|
|
|
return {
|
2017-11-14 14:33:15 +00:00
|
|
|
output:{
|
2018-07-24 15:38:56 +00:00
|
|
|
code:buildModule(modinput),
|
2017-11-11 19:45:32 +00:00
|
|
|
name:moduleName,
|
|
|
|
ports:ports,
|
|
|
|
signals:signals,
|
2018-07-24 15:38:56 +00:00
|
|
|
//stats:getStats(modinput),
|
2017-11-14 14:33:15 +00:00
|
|
|
}
|
2017-11-11 19:45:32 +00:00
|
|
|
};
|
2018-07-11 04:04:28 +00:00
|
|
|
|
2017-11-11 19:45:32 +00:00
|
|
|
}
|
2018-07-11 04:04:28 +00:00
|
|
|
|