Skip to content

Commit

Permalink
Promisify the API, bump version number.
Browse files Browse the repository at this point in the history
  • Loading branch information
tilk committed Aug 22, 2018
1 parent 95b96c6 commit 08d5c2b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 221 deletions.
78 changes: 15 additions & 63 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"use strict";

const tmp = require('tmp');
const shell = require('shelljs');
const child_process = require('child_process');
const assert = require('assert');
const topsort = require('topsort');
const fs = require('fs');
const dagre = require('dagre');
const HashMap = require('hashmap');
const bigInt = require('big-integer');
const {promisify} = require('util');

const ltr2bit = {
'1': 1,
Expand Down Expand Up @@ -587,64 +587,15 @@ function yosys_to_simcir_mod(name, mod, portmaps) {
return mout;
}

function layout_circuit(circ) {
const g = new dagre.graphlib.Graph();
const devmap = {};
let maxx = 0, maxy = 0;

g.setGraph({rankdir: 'RL'});
g.setDefaultEdgeLabel(function() { return {}; });

for (const dev of circ.devices) {
g.setNode(dev.id, {
id: dev.id,
width: 32,
height: 32
});
devmap[dev.id] = dev;
}

for (const conn of circ.connectors) {
g.setEdge(conn.from.id, conn.to.id);
}

dagre.layout(g);

for (const nname of g.nodes()) {
const node = g.node(nname);
devmap[node.id].x = node.x;
devmap[node.id].y = node.y;
maxx = Math.max(maxx, node.x);
maxy = Math.max(maxy, node.y);
//console.log(nname + ":" + JSON.stringify(node));
}

circ.width = maxx + 256;
circ.height = maxy + 64;
}

function layout_circuits(circs) {
for (const name in circs) {
layout_circuit(circs[name]);
}
}

function process(filenames) {
const tmpjson = tmp.tmpNameSync({ postfix: '.json' });
const yosys_result = shell.exec(
async function process(filenames) {
const tmpjson = await promisify(tmp.tmpName)({ postfix: '.json' });
const yosys_result = await promisify(child_process.exec)(
'yosys -p "hierarchy; proc; fsm; memory -nomap" -o "' + tmpjson + '" ' + filenames.join(' '),
{silent: true});
if (yosys_result.code !== 0) {
return {
status: false,
yosys_stdout: yosys_result.stdout
};
}
{maxBuffer: 1000000});
const obj = JSON.parse(fs.readFileSync(tmpjson, 'utf8'));
shell.rm(tmpjson);
await promisify(fs.unlink)(tmpjson);
const portmaps = order_ports(obj);
const out = yosys_to_simcir(obj, portmaps);
//layout_circuits(out);
const toporder = topsort(module_deps(obj));
toporder.pop();
const toplevel = toporder.pop();
Expand All @@ -660,16 +611,17 @@ function process(filenames) {
return {
status: true,
output: output,
yosys_stdout: yosys_result.stdout
yosys_stdout: yosys_result.stdout,
yosys_stderr: yosys_result.stderr
};
}

function process_sv(text) {
const tmpsv = tmp.fileSync({ postfix: '.sv' });
fs.writeSync(tmpsv.fd, text);
fs.closeSync(tmpsv.fd);
const ret = process([tmpsv.name]);
shell.rm(tmpsv.name);
async function process_sv(text) {
const tmpsv = await promisify(tmp.fileSync)({ postfix: '.sv' });
await promisify(fs.writeSync)(tmpsv.fd, text);
await promisify(fs.closeSync)(tmpsv.fd);
const ret = await process([tmpsv.name]);
await promisify(fs.unlink)(tmpsv.name);
return ret;
}

Expand Down
135 changes: 0 additions & 135 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yosys2digitaljs",
"version": "0.0.1",
"version": "0.0.2",
"description": "Export Yosys netlists to a logic simulator",
"main": "index.js",
"scripts": {
Expand All @@ -10,10 +10,8 @@
"license": "BSD-2-Clause",
"dependencies": {
"big-integer": "^1.6.34",
"dagre": "^0.8.0",
"hashmap": "^2.3.0",
"minimist": "^1.2.0",
"shelljs": "^0.8.2",
"tmp": "0.0.33",
"topsort": "^0.0.2"
},
Expand Down
41 changes: 21 additions & 20 deletions process.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"use strict";

const argv = require('minimist')(process.argv.slice(2));
const shell = require('shelljs');

const header = `<!doctype html>
<html>
Expand All @@ -15,25 +14,27 @@ const header = `<!doctype html>

if (argv._.length === 0) {
console.error('No Verilog files passed!');
shell.exit(1);
process.exit(1);
}
const res = require('./index.js').process(argv._);
if (!res.status) {
require('./index.js').process(argv._)
.then(res => {
if (argv.html) {
console.log(header);
console.log('<div id="paper"></div><script>const circuit = new digitaljs.Circuit(');
};
if (argv.yosys_out) {
console.log('/*');
console.log(res.yosys_stdout);
console.log('*/');
}
console.log(JSON.stringify(res.output, null, 2));
if (argv.html) {
console.log(');const paper = circuit.displayOn($(\'#paper\'));</script></body></html>');
};
})
.catch(res => {
console.error('Yosys failed!');
console.error(res.yosys_stdout);
shell.exit(1);
}
if (argv.html) {
console.log(header);
console.log('<div id="paper"></div><script>const circuit = new digitaljs.Circuit(');
};
if (argv.yosys_out) {
console.log('/*');
console.log(res.yosys_stdout);
console.log('*/');
}
console.log(JSON.stringify(res.output, null, 2));
if (argv.html) {
console.log(');const paper = circuit.displayOn($(\'#paper\'));</script></body></html>');
};
console.error(res.stderr);
process.exit(1);
});

0 comments on commit 08d5c2b

Please sign in to comment.