Skip to content

Commit

Permalink
separate mask digit count
Browse files Browse the repository at this point in the history
  • Loading branch information
drom committed Jun 1, 2022
1 parent d6894b5 commit df569a6
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 62 deletions.
1 change: 1 addition & 0 deletions bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const properties = {
value: 'ptr', // value of the signal on change event
mask: 'ptr', // mask (x, z) of the signal on change event
digitCount: 'i32',
maskCount: 'i32',
tmpStr: 'ptr',
timeStampStr: 'ptr',
idStr: 'ptr',
Expand Down
60 changes: 38 additions & 22 deletions lib/web-vcd-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ const dotProp = require('dot-prop');
// }

function h8ToBn(HEAPU8, start, len) {
if (len === 0) {
return 0n;
}
let str = '';
const fin = start + len;
const fin = start + len * 8;
for (let i = start; i < fin; i++) {
const val = HEAPU8[i];
str = val.toString(16) + str;
Expand Down Expand Up @@ -97,6 +100,39 @@ const getWrapper = wasm => {
let context = -1;


const onEE1 = (
/* const char* */ eventName,
/* const size_t */ eventNameLength, // strlen(name)
/* const int64_t */ time,
/* const int */ cmd,
/* const int */ valueWords,
/* uint64_t* */ value,
/* const int */ maskWords,
/* uint64_t* */ mask
) => {
const name = getString(eventName, eventNameLength);
// console.log(`event name`);
// console.log({name, time, command, valueWords});


// 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 = 0n;

// console.log(bigValue.toString(16));

if (cmd >= 14 && cmd <= 28) {
ee[1](name, time, cmd);
} else {
const bigValue = h8ToBn(wasm.HEAPU8, value, valueWords);
const bigMask = h8ToBn(wasm.HEAPU8, mask, maskWords);
ee[1](name, time, cmd, bigValue, bigMask);
}
};

// wasm.addFunction can't be called until after
// start finishes
bindCallback = () => {
Expand Down Expand Up @@ -150,27 +186,7 @@ const getWrapper = wasm => {
ee[0](getString(name, len));
}, 'vii');

// const char* name, const size_t len, const uint64_t time, const uint8_t command, const int valueWords, const uint64_t* aValue, const uint64_t* aMask);
// boundEE1 = wasm.addFunction(function(eventName, l0, time, command, valueWords, value, mask) {
boundEE1 = wasm.addFunction(function(eventName, l0, time, command, valueWords, value, mask) {
const name = getString(eventName, l0);
// console.log(`event name`);
// console.log({name, time, command, valueWords});


// 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 = 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');
boundEE1 = wasm.addFunction(onEE1, 'viijiiiii');

};

Expand Down
Binary file modified out/vcd.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions test/napi_any.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions test/napi_dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const chopper = require('../lib/chopper.js');

const expectaitions = [
{ id: '"}G', time: 100n, cmd: 14, value: 0n, mask: 0n },
{ id: '"}G', time: 200n, cmd: 15, value: 1n, mask: 0n },
{ id: '"}G', time: 200n, cmd: 15, value: 0n, mask: 0n },
{ id: '{u', time: 200n, cmd: 30, value: 0xf0f0f0f0f0f0f0f0n, mask: 0xff00ff00ff00ff00n },
{ id: '"}G', time: 300n, cmd: 14, value: 0n, mask: 0n },
{ id: '{u', time: 300n, cmd: 30, value: 0xf000000000000000n, mask: 0n },
Expand Down Expand Up @@ -44,7 +44,7 @@ const expectaitions = [
{ id: 'u)', time: 314n, cmd: 30, value: 14n, mask: 0n },
{ id: '{u', time: 315n, cmd: 30, value: 0x000000000000000fn, mask: 0n },
{ id: 'u)', time: 315n, cmd: 30, value: 15n, mask: 0n },
{ id: '"}G', time: 316n, cmd: 15, value: 1n, mask: 0n }
{ id: '"}G', time: 316n, cmd: 15, value: 0n, mask: 0n }
];

describe('napi dump', function () {
Expand Down
8 changes: 4 additions & 4 deletions test/wasm_any.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions test/wasm_dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const webVcdParser = require('../lib/web-vcd-parser.js');
const chopper = require('../lib/chopper.js');

const expectaitions = [
{ id: '"}G', time: 100n, cmd: 14, value: 0n, mask: 0n },
{ id: '"}G', time: 200n, cmd: 15, value: 1n, mask: 0n },
{ id: '"}G', time: 100n, cmd: 14, value: undefined, mask: undefined },
{ id: '"}G', time: 200n, cmd: 15, value: undefined, mask: undefined },
{ id: '{u', time: 200n, cmd: 30, value: 0xf0f0f0f0f0f0f0f0n, mask: 0xff00ff00ff00ff00n },
{ id: '"}G', time: 300n, cmd: 14, value: 0n, mask: 0n },
{ id: '"}G', time: 300n, cmd: 14, value: undefined, mask: undefined },
{ id: '{u', time: 300n, cmd: 30, value: 0xf000000000000000n, mask: 0n },
{ id: 'u)', time: 300n, cmd: 30, value: 0n, mask: 0n },
{ id: '{u', time: 301n, cmd: 30, value: 0x0f00000000000000n, mask: 0n },
Expand Down Expand Up @@ -45,7 +45,7 @@ const expectaitions = [
{ id: 'u)', time: 314n, cmd: 30, value: 14n, mask: 0n },
{ id: '{u', time: 315n, cmd: 30, value: 0x000000000000000fn, mask: 0n },
{ id: 'u)', time: 315n, cmd: 30, value: 15n, mask: 0n },
{ id: '"}G', time: 316n, cmd: 15, value: 1n, mask: 0n }
{ id: '"}G', time: 316n, cmd: 15, value: undefined, mask: undefined }
];

describe('wasm dump', () => {
Expand Down
1 change: 1 addition & 0 deletions vcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ METHOD(init) {
state->mask = maskBuf;
state->time = INT64_MAX;
state->digitCount = 0;
state->maskCount = 0;

napi_value status;
ASSERT(status, napi_create_string_latin1(env, "declaration", NAPI_AUTO_LENGTH, &status))
Expand Down
68 changes: 40 additions & 28 deletions vcd_spans.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,41 +220,45 @@ int onId (vcd_parser_t* state, const unsigned char* _p, const unsigned char* _en
const unsigned char* endp = p + plen - 1;
// printf("<onId|%s>\n", (char *)state->idStr);

const int valueWords = (state->digitCount >> 6) + 1;
const int valueWords = (state->digitCount + 63) >> 6;
const int maskWords = (state->maskCount + 63) >> 6;
uint64_t* value = state->value;
uint64_t* mask = state->mask;
if (stringEq((state->trigger), p, endp)) {
const uint8_t command = state->command;
// printf("{id:'%s',cmd:%d}", (char *)p, command);
if (command == 14) {
value[0] = 0;
mask[0] = 0;
} else
if (command == 15) {
value[0] = 1;
mask[0] = 0;
}
// if (command == 14) {
// value[0] = 0;
// mask[0] = 0;
// } else
// if (command == 15) {
// value[0] = 1;
// mask[0] = 0;
// }
#ifndef VCDWASM
napi_value undefined, eventName, aTime, aCommand, aValue, aMask, return_val;
ASSERT(undefined, napi_get_undefined(env, &undefined))
ASSERT(eventName, napi_create_string_latin1(env, (char*)p, NAPI_AUTO_LENGTH, &eventName))
ASSERT(aTime, napi_create_int64(env, state->time, &aTime))
ASSERT(aCommand, napi_create_int32(env, command, &aCommand))
ASSERT(aValue, napi_create_bigint_words(env, 0, valueWords, value, &aValue))
ASSERT(aMask, napi_create_bigint_words(env, 0, valueWords, mask, &aMask))
ASSERT(aMask, napi_create_bigint_words(env, 0, maskWords, mask, &aMask))
napi_value* argv[] = {&eventName, &aTime, &aCommand, &aValue, &aMask};
ASSERT(state->triee, napi_call_function(env, undefined, state->triee, 5, *argv, &return_val))
// printf("<id='%s'>", (char *)p);
#else
// strcopy(p, endp, state->tmpStr);
emit_triee((char *)p, state->time, command, valueWords, value, mask);
emit_triee((char *)p, state->time, command, valueWords, value, maskWords, mask);
#endif
}
for (int i = 0; i < valueWords; i++) {
value[i] = 0;
}
for (int i = 0; i < maskWords; i++) {
mask[i] = 0;
}
state->digitCount = 0;
state->maskCount = 0;
*(char *)state->idStr = 0;
return 0;
}
Expand All @@ -263,29 +267,36 @@ int onId (vcd_parser_t* state, const unsigned char* _p, const unsigned char* _en

int onDigit(
vcd_parser_t* state,
const unsigned char* p,
const unsigned char* endp,
const unsigned char* _p,
const unsigned char* _endp,
int digit
) {

unsigned int valueCin = (digit & 1);
unsigned int maskCin = ((digit >> 1) & 1);
unsigned int valueCout;
unsigned int maskCout;
uint64_t* value = state->value;
uint64_t* mask = state->mask;
const int valueWordsMinus = (state->digitCount >> 6);
for (int i = 0; i <= valueWordsMinus; i++) {

valueCout = value[i] >> 63;
value[i] = (value[i] << 1) + valueCin;
valueCin = valueCout;

maskCout = mask[i] >> 63;
mask[i] = (mask[i] << 1) + maskCin;
maskCin = maskCout;

if ((valueCin != 0) || (state->digitCount != 0)) {
unsigned int valueCout;
uint64_t* value = state->value;
const int valueWordsMinus = (state->digitCount >> 6);
for (int i = 0; i <= valueWordsMinus; i++) {
valueCout = value[i] >> 63;
value[i] = (value[i] << 1) + valueCin;
valueCin = valueCout;
}
state->digitCount += 1;
}
if ((maskCin != 0) || (state->maskCount != 0)) {
unsigned int maskCout;
uint64_t* mask = state->mask;
const int maskWordsMinus = (state->maskCount >> 6);
for (int i = 0; i <= maskWordsMinus; i++) {
maskCout = mask[i] >> 63;
mask[i] = (mask[i] << 1) + maskCin;
maskCin = maskCout;
}
state->maskCount += 1;
}
state->digitCount += 1;
return 0;
}

Expand All @@ -296,6 +307,7 @@ int onRecover(
int digit
) {
state->digitCount = 0;
state->maskCount = 0;
return 0;
}

Expand Down
4 changes: 4 additions & 0 deletions wasm_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef void externalJsMethodOne (
const int command,
const int valueWords,
const int aValue,
const int maskWords,
const int aMask
);

Expand Down Expand Up @@ -71,6 +72,7 @@ void emit_triee(
const int command,
const int valueWords,
uint64_t* aValue,
const int maskWords,
uint64_t* aMask
) {

Expand All @@ -91,6 +93,7 @@ void emit_triee(
command,
valueWords,
(int)aValue,
maskWords,
(int)aMask
);
}
Expand Down Expand Up @@ -142,6 +145,7 @@ int init(
state->mask = maskBuf;
state->time = INT64_MAX;
state->digitCount = 0;
state->maskCount = 0;

set_property_string("status", "declaration");

Expand Down
1 change: 1 addition & 0 deletions wasm_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ void emit_triee(
const int command,
const int valueWords,
uint64_t* aValue,
const int maskWords,
uint64_t* aMask
);

0 comments on commit df569a6

Please sign in to comment.