Skip to content

Commit

Permalink
vscode plugin rework
Browse files Browse the repository at this point in the history
  • Loading branch information
drom committed Jan 22, 2024
1 parent 6ec03a5 commit fc85554
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
45 changes: 45 additions & 0 deletions lib/get-vcd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const maxChunkLength = 1 << 17; // Number.MAX_SAFE_INTEGER; // 5e6; // 300000; // 1 << 23;

const getVcd = async (readers, content, inst) => {
const r = readers.find(reader => reader.ext === 'vcd');
if (r) {
// console.log('VCD', r);
document.title = r.baseName;
content.innerHTML = '<div class="wd-progress">LOADING...</div>';
let total = 0;
outerLoop:
for (let i = 0; i < 1e5; i++) {
const { done, value } = await r.reader.read();

if (done && (value === undefined)) {
// console.log('the end');
inst.end();
break outerLoop;
}
const len = value.length;
for (let j = 0; j < len; j += maxChunkLength) {
const value1 = value.slice(j, j + maxChunkLength);
const len1 = value1.length;
total += len1;

// const vh = u8toStr(value1.slice(0, 100));
// const vt = u8toStr(value1.slice(-100));
// console.log({len1, done, total, vh, vt});

content.innerHTML = '<div class="wd-progress">' + total.toLocaleString() + '</div>';
if (done && ((j + maxChunkLength) >= len)) {
// console.log('last chunk');
inst.end(value1);
break outerLoop;
}
inst.write(value1);
}
}
}
};

module.exports = getVcd;

/* eslint-env browser */
95 changes: 95 additions & 0 deletions lib/vcd-pipe-deso.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict';

const parseTimescale = require('./parse-time-scale.js');

const MAX_SAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);

const numberOrString = val => {
if (val < MAX_SAFE_INTEGER) {
return Number(val);
}
return '0x' + val.toString(16);
};

const gcd = (a, b) => {
if (a === undefined) {
return b;
}
let r;
while (b !== 0) {
r = a % b;
a = b;
b = r;
}
return (a < 0) ? -a : a;
};

const tNorm = o => {
const {tgcd, chango} = o;

o.t0 /= tgcd;
o.time /= tgcd;
Object.keys(chango).map(key => {
const {wave} = chango[key];
wave.map(e => {
e[0] /= tgcd;
});
});

const exp = Math.log10(tgcd) |0;
if (exp > 0) {
const scale = Math.pow(10, exp);
const tgcd1 = tgcd / scale;
if (tgcd1 === (tgcd1 |0)) {
o.tgcd = tgcd1;
o.timescale += exp;
}
}
return o;
};


module.exports = async (deso, inst, done) => {
const chango = {};
let tgcd;
deso.chango = chango;
deso.view = [];

const onAnyChange = (id, time, cmd, value, mask) => {
// console.log(id, time, cmd, value, mask);
const time53 = Number(time);
tgcd = gcd(tgcd, time53);
chango[id] = chango[id] || {wave: []};
if (cmd >= 14 && cmd <= 28) {
chango[id].kind = 'bit';
chango[id].wave.push([time53, cmd - 14]);
} else {
chango[id].kind = 'vec';
const point = [time53, numberOrString(value)];
if (mask !== 0n) {
point.push(numberOrString(mask));
}
chango[id].wave.push(point);
}
};

const t0 = Date.now();

inst.on('$enddefinitions', () => {
// console.log('$enddefinitions');
Object.assign(deso.wires, inst.info.wires);
deso.timescale = parseTimescale(inst.info.timescale);
});

inst.change.any(onAnyChange);

inst.on('finish', () => {
console.log((Date.now() - t0) / 1000);
deso.tgcd = tgcd;
deso.t0 = (inst.info.t0 || 0);
// console.log(inst.getTime());
deso.time = Number(inst.getTime());
tNorm(deso);
done(deso);
});
};
2 changes: 1 addition & 1 deletion out/vcd.js

Large diffs are not rendered by default.

Binary file modified out/vcd.wasm
Binary file not shown.

0 comments on commit fc85554

Please sign in to comment.