Skip to content

Commit

Permalink
fixed issues with cross chunk Spans, chunk string->array, and better …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
drom committed May 26, 2022
1 parent afbee0d commit 1d920a3
Show file tree
Hide file tree
Showing 14 changed files with 424 additions and 285 deletions.
6 changes: 4 additions & 2 deletions bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const properties = {
mask: 'ptr', // mask (x, z) of the signal on change event
digitCount: 'i32',
tmpStr: 'ptr',
timeStampStr: 'ptr',
idStr: 'ptr',
tmpStr2: 'ptr',
stackPointer: 'i32',
id: 'ptr',
Expand Down Expand Up @@ -255,7 +257,7 @@ const generate = (cb) => {
.skipTo(inSimulation);

simulationTime
.match(spaces, timeSpan.end(simulation))
.match(spaces, timeSpan.end(p.invoke(p.code.span('onTime'), simulation)))
.skipTo(simulationTime);

simulationVector
Expand Down Expand Up @@ -298,7 +300,7 @@ const generate = (cb) => {
.skipTo(simulationVectorRecovery);

simulationId
.match(spaces, idSpan.end(simulation))
.match(spaces, idSpan.end(p.invoke(p.code.span('onId'), simulation)))
.skipTo(simulationId);

const artifacts = p.build(declaration);
Expand Down
35 changes: 35 additions & 0 deletions lib/chopper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

function xoshiro128ss(a, b, c, d) {
return function() {
var t = b << 9, r = a * 5; r = (r << 7 | r >>> 25) * 9;
c ^= a; d ^= b;
b ^= c; a ^= d; c ^= t;
d = d << 11 | d >>> 21;
return (r >>> 0) / 4294967296;
};
}


function * chopper (src, step) {

const chunker = len => {
const chunk = src.slice(0, len);
src = src.slice(chunk.length);
return chunk;
};

const random = xoshiro128ss(1134534345, 2145245245624, 313442566456, 4245642456456);
// const random = Math.random;
// const random = () => 0;
// yield(chunker(395));
// yield(chunker(145));
while(true) {
if (src.length === 0) {
return;
}
yield(chunker(random() * step + step));
}
}

module.exports = chopper;
10 changes: 8 additions & 2 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@ module.exports = () => {
const cxt = lib.init(lifemit, triemit, info);

s._write = function (chunk, encoding, callback) {
lib.execute(cxt, lifemit, triemit2, info, chunk);
const err = lib.execute(cxt, lifemit, triemit2, info, chunk);
if (err) {
// console.log(info);
console.log(err);
// throw new Error(err);
}
callback();
};

s.change = {
on: (id, fn) => {
// console.log(id);
triemit2 = triemit;
triee.on(id, fn);
const triggerString = triee.eventNames().join(' ') + ' ';
const triggerString = triee.eventNames().join('\0') + '\0\0';
lib.setTrigger(cxt, triggerString);
},
any: fn => {
Expand Down
76 changes: 49 additions & 27 deletions lib/web-vcd-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,36 @@ const dotProp = require('dot-prop');
// });
// }

function u8ToBn(u8) {
var hex = [];
// let u8 = Uint8Array.from(buf);

u8.forEach(function (i) {
var h = i.toString(16);
if (h.length % 2) { h = '0' + h; }
hex.push(h);
});

hex.reverse();
// function u8ToBn(u8) {
// let str = '';
// for (let i = 0; i < u8.length; i++) {
// const val = u8[i];
// str = val.toString(16) + str;
// if (val < 16) {
// str = '0' + str;
// }
// }
// return BigInt('0x' + str);
// }

return BigInt('0x' + hex.join(''));
function h8ToBn(HEAPU8, start, len) {
let str = '';
const fin = start + len;
for (let i = start; i < fin; i++) {
const val = HEAPU8[i];
str = val.toString(16) + str;
if (val < 16) {
str = '0' + str;
}
}
return BigInt('0x' + str);
}

// let startCalled = 0;

const bindCWrap = (c, wasm) => {
const w = wasm.cwrap;
c.execute = w('execute', 'number', ['number', 'number', 'number', 'number', 'number', 'string']);
c.execute = w('execute', 'number', ['number', 'number', 'number', 'number', 'number', 'array', 'number']);
c.init = w('init', 'number', ['number', 'number', 'number', 'number']);
c.getTime = w('getTime', 'number', ['number']);
c.setTrigger = w('setTrigger', 'number', ['number', 'string']);
Expand All @@ -57,13 +67,21 @@ const getWrapper = wasm => {

// gets a string from a c heap pointer and length
const getString = (name, len) => {
const view = wasm.HEAPU8.subarray(name, name+len);

// const view = wasm.HEAPU8.subarray(name, name+len);
// let string = '';
// for (let i = 0; i < len; i++) {
// string += String.fromCharCode(view[i]);
// }
// return string;

let string = '';
for (let i = 0; i < len; i++) {
string += String.fromCharCode(view[i]);
const end = name + len;
for (let i = name; i < end; i++) {
string += String.fromCharCode(wasm.HEAPU8[i]);
}
return string;

};

let boundInfo;
Expand Down Expand Up @@ -137,16 +155,19 @@ const getWrapper = wasm => {
boundEE1 = wasm.addFunction(function(eventName, l0, time, command, valueWords, value, mask) {
const name = getString(eventName, l0);
// console.log(`event name`);
// console.log(`event ${name} time ${time} cmd ${command} wrds ${valueWords}`);
// console.log({name, time, command, valueWords});


const view0 = wasm.HEAPU8.subarray(value, value+(valueWords*8));
const view1 = wasm.HEAPU8.subarray(mask, mask+(valueWords*8));
// const view0 = wasm.HEAPU8.subarray(value, value+(valueWords*8));
// const view1 = wasm.HEAPU8.subarray(mask, mask+(valueWords*8));

let bigValue = u8ToBn(view0);
let bigMask = u8ToBn(view1);
// let bigValue = u8ToBn(view0);
// let bigMask = u8ToBn(view1);
// let bigValue = 0n;

// console.log(bigValue.toString(16));
const bigValue = h8ToBn(wasm.HEAPU8, value, valueWords * 8);
const bigMask = h8ToBn(wasm.HEAPU8, mask, valueWords * 8);

ee[1](name, time, command, bigValue, bigMask);
}, 'viijiiii');
Expand All @@ -167,7 +188,7 @@ const getWrapper = wasm => {
boundInfo = info;
ee[0] = cb0;
ee[1] = cb1;
c.execute(ctx, boundEE0, boundEE1, boundSet, boundGet, chunk.toString());
return c.execute(ctx, boundEE0, boundEE1, boundSet, boundGet, chunk, chunk.length);
},
setTrigger: (ctx, triggerString) => {
return c.setTrigger(ctx, triggerString);
Expand Down Expand Up @@ -208,8 +229,11 @@ module.exports = async wasm => {
const cxt = lib.init(lifemit, triemit, info);

s._write = function (chunk, encoding, callback) {
// console.log('about to write', chunk);
lib.execute(cxt, lifemit, triemit2, info, chunk);
// console.log(cxt, info);
const err = lib.execute(cxt, lifemit, triemit2, info, chunk);
if (err) {
console.log(err);
}
// console.log(util.inspect(info, {showHidden: true, depth : null, colorize: true}));
// console.log(info.stack[0].top);
// console.log(info.stack[1]);
Expand All @@ -220,9 +244,9 @@ module.exports = async wasm => {
s.change = {
on: (id, fn) => {
triemit2 = triemit;
// console.log(id, fn);
triee.on(id, fn);
const triggerString = triee.eventNames().join(' ') + ' ';
// console.log(id, Buffer.from(triggerString));
lib.setTrigger(cxt, triggerString);
},
any: fn => {
Expand All @@ -239,5 +263,3 @@ module.exports = async wasm => {

return s;
};

/* global BigInt */
2 changes: 0 additions & 2 deletions lib/wrapper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

/* global BigInt */

const dotProp = require('dot-prop');

function _waitForStart(mod) {
Expand Down
4 changes: 2 additions & 2 deletions out/vcd.js

Large diffs are not rendered by default.

Binary file modified out/vcd.wasm
Binary file not shown.
72 changes: 72 additions & 0 deletions test/dump.vcd
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
$version Generated by VerilatedVcd $end
$date Wed Sep 18 22:59:07 2019
$end
$timescale 1ns $end

$scope module top $end
$var wire 1 "}G clock $end
$scope module leaf $end
$var wire 64 {u counter [63:0] $end
$upscope $end
$scope module fruit $end
$var wire 4 u) point [3:0] $end
$upscope $end
$upscope $end

$enddefinitions $end
#100
0"}G
#200
1"}G
bzzzzxxxx11110000ZZZZXXXX11110000zzzzxxxx11110000zzzzxxxx11110000 {u
#300
0"}G
b1111000000000000000000000000000000000000000000000000000000000000 {u
b0000 u)
#301
b0000111100000000000000000000000000000000000000000000000000000000 {u
b0001 u)
#302
b0000000011110000000000000000000000000000000000000000000000000000 {u
b0010 u)
#303
b0000000000001111000000000000000000000000000000000000000000000000 {u
b0011 u)
#304
b0000000000000000111100000000000000000000000000000000000000000000 {u
b0100 u)
#305
b0000000000000000000011110000000000000000000000000000000000000000 {u
b0101 u)
#306
b0000000000000000000000001111000000000000000000000000000000000000 {u
b0110 u)
#307
b0000000000000000000000000000111100000000000000000000000000000000 {u
b0111 u)
#308
B0000000000000000000000000000000011110000000000000000000000000000 {u
b1000 u)
#309
b0000000000000000000000000000000000001111000000000000000000000000 {u
b1001 u)
#310
b0000000000000000000000000000000000000000111100000000000000000000 {u
b1010 u)
#311
b0000000000000000000000000000000000000000000011110000000000000000 {u
b1011 u)
#312
b0000000000000000000000000000000000000000000000001111000000000000 {u
b1100 u)
#313
b0000000000000000000000000000000000000000000000000000111100000000 {u
b1101 u)
#314
b0000000000000000000000000000000000000000000000000000000011110000 {u
b1110 u)
#315
b0000000000000000000000000000000000000000000000000000000000001111 {u
b1111 u)
#316
1"}G
14 changes: 9 additions & 5 deletions test/napi_basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ describe('basic', () => {

it('fail: foo bar', done => {
const inst = parser();
expect(inst.write(Buffer.from(' foo bar ???'))).to.eq(true);
expect(() => {
inst.write(Buffer.from(' foo bar ???'));
}).not.to.throw();
expect(inst.info).to.deep.eq({
stack: [{}],
status: 'declaration',
Expand All @@ -26,11 +28,13 @@ describe('basic', () => {
done();
});

it('$comment', done => {
it('fail: $comment', done => {
const inst = parser();
expect(inst.write(Buffer.from(
' \n $comment some text $end $comment more text $end ???'
))).to.eq(true);
expect(() => {
inst.write(Buffer.from(
' \n $comment some text $end $comment more text $end ???'
));
}).not.to.throw();
expect(inst.info).to.deep.eq({
comment: ' more text ',
stack: [{}],
Expand Down
Loading

0 comments on commit 1d920a3

Please sign in to comment.