From 3a56ab2bcc37bcdf4c1079f8b28445ac1b1882cd Mon Sep 17 00:00:00 2001
From: Ben <bjmorse@gmail.com>
Date: Sun, 27 Sep 2020 18:15:46 -0700
Subject: [PATCH] close, off by 1 somewhere

---
 lib/wasmparser.js |  8 ++--
 lib/wrapper.js    | 43 ++++++++++++++++++---
 test/wasmdump.js  | 95 ++++++++++++++++++++++++++++++++---------------
 vcd_spans.c       | 44 ++++++++++++++++------
 wasm_main.cpp     | 37 ++++++++++++++++--
 wasm_main.hpp     |  1 +
 6 files changed, 175 insertions(+), 53 deletions(-)

diff --git a/lib/wasmparser.js b/lib/wasmparser.js
index 7f42996..52f4bb6 100644
--- a/lib/wasmparser.js
+++ b/lib/wasmparser.js
@@ -35,10 +35,12 @@ module.exports = async () => {
   const cxt = lib.init(lifemit, triemit, info);
 
   s._write = function (chunk, encoding, callback) {
-    console.log('about to write', chunk);
+    // console.log('about to write', chunk);
     lib.execute(cxt, lifemit, triemit2, info, chunk);
-    console.log(util.inspect(info, {showHidden: true, depth : null, colorize: true}));
-    // console.log(info);
+    // console.log(util.inspect(info, {showHidden: true, depth : null, colorize: true}));
+    // console.log(info.stack[0].top);
+    // console.log(info.stack[1]);
+    // console.log(info.stack[0].top == info.stack[1]);
     callback();
   };
 
diff --git a/lib/wrapper.js b/lib/wrapper.js
index 22e7463..641dee2 100644
--- a/lib/wrapper.js
+++ b/lib/wrapper.js
@@ -8,6 +8,21 @@ function _waitForStart(mod) {
   });
 }
 
+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);
+  });
+
+  return BigInt('0x' + hex.join(''));
+}
+
+
+
 module.exports = () => {
   // let state = 0; // idle
   // let total = 0;
@@ -56,6 +71,7 @@ module.exports = () => {
 
   let context = -1;
 
+
   // wasm.addFunction can't be called until after
   // start finishes
   bindCallback = () => {
@@ -77,17 +93,17 @@ module.exports = () => {
         // set string to path
         case 2:
           dotProp.set(boundInfo, prop, getString(v0, v1));
-          console.log(`setting ${prop} to ${getString(v0, v1)}`);
+          // console.log(`setting ${prop} to ${getString(v0, v1)}`);
           break;
         // path to path (any type)
         case 3:
           let tmp = dotProp.get(boundInfo, getString(v0, v1));
-          console.log(`for ${getString(v0, v1)} got ${tmp}, set to ${prop}`);
+          // console.log(`for ${getString(v0, v1)} got ${tmp}, set to ${prop}`);
           dotProp.set(boundInfo, prop, tmp);
           break;
         // create empty object at path
         case 4:
-          console.log(`${prop} is new {}`);
+          // console.log(`${prop} is new {}`);
           dotProp.set(boundInfo, prop, {});
           break;
 
@@ -108,9 +124,24 @@ module.exports = () => {
       ee[0](getString(name, len));
     }, 'vii');
 
-    boundEE1 = wasm.addFunction(function(eventName, l0, time, command, value, mask) {
-      ee[1](getString(eventName, l0), time, command, value, mask);
-    }, 'viiiiii');
+    // 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(`event ${name} time ${time} cmd ${command} wrds ${valueWords}`);
+
+
+      const view0 = wasm.HEAPU8.subarray(value, value+(valueWords*8));
+      const view1 = wasm.HEAPU8.subarray(mask,  mask+(valueWords*8));
+
+      let big_value = u8ToBn(view0);
+      let big_mask = u8ToBn(view1);
+
+      console.log(big_value.toString(16));
+
+      ee[1](name, time, command, big_value, big_mask);
+    }, 'viiiiiii');
 
   };
 
diff --git a/test/wasmdump.js b/test/wasmdump.js
index df63d40..4fb6647 100644
--- a/test/wasmdump.js
+++ b/test/wasmdump.js
@@ -32,30 +32,30 @@ describe('dump', () => {
       );
 
     inst.on('finish', () => {
-      // expect(inst.getTime()).to.eq(316n);
-      // expect(dump).to.deep.eq([
-      //   { id: '"}G', time: 100, cmd: 14, value: 0n, mask: 0n },
-      //   { id: '"}G', time: 200, cmd: 15, value: 1n, mask: 0n },
-      //   { id: '{u', time: 200, cmd: 19, value: 0xf0f0f0f0f0f0f0f0n, mask: 0xff00ff00ff00ff00n },
-      //   { id: '"}G', time: 300, cmd: 14, value: 0n, mask: 0n },
-      //   { id: '{u', time: 300, cmd: 19, value: 0xf000000000000000n, mask: 0n },
-      //   { id: '{u', time: 301, cmd: 19, value: 0x0f00000000000000n, mask: 0n },
-      //   { id: '{u', time: 302, cmd: 19, value: 0x00f0000000000000n, mask: 0n },
-      //   { id: '{u', time: 303, cmd: 19, value: 0x000f000000000000n, mask: 0n },
-      //   { id: '{u', time: 304, cmd: 19, value: 0x0000f00000000000n, mask: 0n },
-      //   { id: '{u', time: 305, cmd: 19, value: 0x00000f0000000000n, mask: 0n },
-      //   { id: '{u', time: 306, cmd: 19, value: 0x000000f000000000n, mask: 0n },
-      //   { id: '{u', time: 307, cmd: 19, value: 0x0000000f00000000n, mask: 0n },
-      //   { id: '{u', time: 308, cmd: 20, value: 0x00000000f0000000n, mask: 0n },
-      //   { id: '{u', time: 309, cmd: 19, value: 0x000000000f000000n, mask: 0n },
-      //   { id: '{u', time: 310, cmd: 19, value: 0x0000000000f00000n, mask: 0n },
-      //   { id: '{u', time: 311, cmd: 19, value: 0x00000000000f0000n, mask: 0n },
-      //   { id: '{u', time: 312, cmd: 19, value: 0x000000000000f000n, mask: 0n },
-      //   { id: '{u', time: 313, cmd: 19, value: 0x0000000000000f00n, mask: 0n },
-      //   { id: '{u', time: 314, cmd: 19, value: 0x00000000000000f0n, mask: 0n },
-      //   { id: '{u', time: 315, cmd: 19, value: 0x000000000000000fn, mask: 0n },
-      //   { id: '"}G', time: 316, cmd: 15, value: 1n, mask: 0n },
-      // ])
+      expect(inst.getTime()).to.eq(316n);
+      expect(dump).to.deep.eq([
+        { id: '"}G', time: 100, cmd: 14, value: 0n, mask: 0n },
+        { id: '"}G', time: 200, cmd: 15, value: 1n, mask: 0n },
+        { id: '{u', time: 200, cmd: 19, value: 0xf0f0f0f0f0f0f0f0n, mask: 0xff00ff00ff00ff00n },
+        { id: '"}G', time: 300, cmd: 14, value: 0n, mask: 0n },
+        { id: '{u', time: 300, cmd: 19, value: 0xf000000000000000n, mask: 0n },
+        { id: '{u', time: 301, cmd: 19, value: 0x0f00000000000000n, mask: 0n },
+        { id: '{u', time: 302, cmd: 19, value: 0x00f0000000000000n, mask: 0n },
+        { id: '{u', time: 303, cmd: 19, value: 0x000f000000000000n, mask: 0n },
+        { id: '{u', time: 304, cmd: 19, value: 0x0000f00000000000n, mask: 0n },
+        { id: '{u', time: 305, cmd: 19, value: 0x00000f0000000000n, mask: 0n },
+        { id: '{u', time: 306, cmd: 19, value: 0x000000f000000000n, mask: 0n },
+        { id: '{u', time: 307, cmd: 19, value: 0x0000000f00000000n, mask: 0n },
+        { id: '{u', time: 308, cmd: 20, value: 0x00000000f0000000n, mask: 0n },
+        { id: '{u', time: 309, cmd: 19, value: 0x000000000f000000n, mask: 0n },
+        { id: '{u', time: 310, cmd: 19, value: 0x0000000000f00000n, mask: 0n },
+        { id: '{u', time: 311, cmd: 19, value: 0x00000000000f0000n, mask: 0n },
+        { id: '{u', time: 312, cmd: 19, value: 0x000000000000f000n, mask: 0n },
+        { id: '{u', time: 313, cmd: 19, value: 0x0000000000000f00n, mask: 0n },
+        { id: '{u', time: 314, cmd: 19, value: 0x00000000000000f0n, mask: 0n },
+        { id: '{u', time: 315, cmd: 19, value: 0x000000000000000fn, mask: 0n },
+        { id: '"}G', time: 316, cmd: 15, value: 1n, mask: 0n },
+      ])
       // console.log(dump);
       done();
     });
@@ -68,6 +68,12 @@ $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
@@ -75,11 +81,42 @@ $timescale   1ns $end
 0"}G
 #200
 1"}G
-`);
-
-// break in the middle of the number scan
-
-     inst.write(                       `x11110000zzzzxxxx11110000 {u
+bzzzzxxxx11110000ZZZZXXXX11110000zzzzxxxx11110000zzzzxxxx11110000 {u
+#300
+0"}G
+b1111000000000000000000000000000000000000000000000000000000000000 {u
+#301
+b0000111100000000000000000000000000000000000000000000000000000000 {u
+#302
+b0000000011110000000000000000000000000000000000000000000000000000 {u
+#303
+b0000000000001111000000000000000000000000000000000000000000000000 {u
+#304
+b0000000000000000111100000000000000000000000000000000000000000000 {u
+#305
+b0000000000000000000011110000000000000000000000000000000000000000 {u
+#306
+b0000000000000000000000001111000000000000000000000000000000000000 {u
+#307
+b0000000000000000000000000000111100000000000000000000000000000000 {u
+#308
+B0000000000000000000000000000000011110000000000000000000000000000 {u
+#309
+b0000000000000000000000000000000000001111000000000000000000000000 {u
+#310
+b0000000000000000000000000000000000000000111100000000000000000000 {u
+#311
+b0000000000000000000000000000000000000000000011110000000000000000 {u
+#312
+b0000000000000000000000000000000000000000000000001111000000000000 {u
+#313
+b0000000000000000000000000000000000000000000000000000111100000000 {u
+#314
+b0000000000000000000000000000000000000000000000000000000011110000 {u
+#315
+b0000000000000000000000000000000000000000000000000000000000001111 {u
+#316
+1"}G
 `);
     inst.end();
   });
diff --git a/vcd_spans.c b/vcd_spans.c
index dfeda1b..3ca324a 100644
--- a/vcd_spans.c
+++ b/vcd_spans.c
@@ -98,7 +98,7 @@ int commandSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char
 }
 
 int scopeIdentifierSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* endp) {
-  LOGSPAN;
+  // LOGSPAN;
 #ifndef VCDWASM
   napi_env env = state->napi_env;
   strcopy(p, endp, state->tmpStr);
@@ -107,14 +107,14 @@ int scopeIdentifierSpan(vcd_parser_t* state, const unsigned char* p, const unsig
   ASSERT(state->info, napi_get_named_property(env, state->info, "stack", &stack))
 
   // get the top of the stack in top
-  printf("Got stack %d\n", state->stackPointer);
+  // printf("Got stack %d\n", state->stackPointer);
   ASSERT(top, napi_get_element(env, stack, state->stackPointer, &top))
 
   // set top.prop to new object
-  printf("Set top of stack[%d].%s to {}\n", state->stackPointer, (char*)state->tmpStr);
+  // printf("Set top of stack[%d].%s to {}\n", state->stackPointer, (char*)state->tmpStr);
   ASSERT(top, napi_set_named_property(env, top, state->tmpStr, obj))
 
-  printf("Set top+1 of stack to top %d, %d\n", state->stackPointer, state->stackPointer+1);
+  // printf("Set top+1 of stack to top %d, %d\n", state->stackPointer, state->stackPointer+1);
   state->stackPointer += 1;
   ASSERT(top, napi_set_element(env, stack, state->stackPointer, obj))
 #else
@@ -136,13 +136,13 @@ int scopeIdentifierSpan(vcd_parser_t* state, const unsigned char* p, const unsig
 }
 
 int varSizeSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* endp) {
-  LOGSPAN;
+  // LOGSPAN;
   state->size = strtol((const char *)p, (char **)&endp, 10);
   return 0;
 }
 
 int varIdSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* endp) {
-  LOGSPAN;
+  // LOGSPAN;
 #ifndef VCDWASM
   napi_env env = state->napi_env;
   napi_value varId;
@@ -156,7 +156,7 @@ int varIdSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char*
 }
 
 int varNameSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* endp) {
-  LOGSPAN;
+  // LOGSPAN;
 #ifndef VCDWASM
   napi_env env = state->napi_env;
   // *(endp - 1) = 0; // FIXME NULL termination of ASCII string
@@ -177,10 +177,18 @@ int varNameSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char
 }
 
 int idSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* endp) {
+  LOGSPAN;
 #ifndef VCDWASM
   napi_env env = state->napi_env;
+#endif
+
+  // uint64_t foo = 0x123456789abcdef0;
+
+  // printf(" '%s' ", state->trigger);
+
   const int valueWords = (state->digitCount >> 6) + 1;
   uint64_t* value = state->value;
+  // value = &foo;
   uint64_t* mask = state->mask;
   if (stringEq((state->trigger), p, endp)) {
     const uint8_t command = state->command;
@@ -192,6 +200,9 @@ int idSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* end
       value[0] = 1;
       mask[0] = 0;
     }
+    printf("valueWords %d %d %d\n", valueWords, (int)command, state->digitCount);
+    printf("\ntriee %lx\n", *value);
+#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, (endp - p - 1), &eventName))
@@ -199,15 +210,23 @@ int idSpan(vcd_parser_t* state, const unsigned char* p, const unsigned char* end
     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))
+
+
     napi_value* argv[] = {&eventName, &aTime, &aCommand, &aValue, &aMask};
     ASSERT(state->triee, napi_call_function(env, undefined, state->triee, 5, *argv, &return_val))
+
+#else
+
+    // printf("\ntriee %lx\n", *value);
+    strcopy(p, endp, state->tmpStr);
+    emit_triee(state->tmpStr, state->time, command, valueWords, value, mask);
+#endif
   }
   for (int i = 0; i < valueWords; i++) {
     value[i] = 0;
     mask[i] = 0;
   }
   state->digitCount = 0;
-#endif
   return 0;
 }
 
@@ -217,7 +236,6 @@ int onDigit(
   const unsigned char* endp,
   int digit
 ) {
-#ifndef VCDWASM
   unsigned int valueCin = (digit & 1);
   unsigned int maskCin = ((digit >> 1) & 1);
   unsigned int valueCout;
@@ -234,10 +252,14 @@ int onDigit(
     maskCout = mask[i] >> 63;
     mask[i]  = (mask[i] << 1) + maskCin;
     maskCin = maskCout;
-
+    // unsigned char* c = p;
+    // while(c != endp) {
+    //   puts(c); c++;
+    // }
   }
+  char c = *p;
+  printf("%c", c);
   state->digitCount += 1;
-#endif
   return 0;
 }
 
diff --git a/wasm_main.cpp b/wasm_main.cpp
index 31f9327..38a6d45 100644
--- a/wasm_main.cpp
+++ b/wasm_main.cpp
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <iostream>
 #include "vcd_parser.h"
 
@@ -8,7 +9,8 @@ using namespace std;
 
 /// Typedef used as part of c->js call
 typedef void externalJsMethodZero(const char* name, const size_t len);
-typedef void externalJsMethodOne (const char*, const uint64_t time, const uint8_t command, const int dnc0, const int dnc1);
+// typedef void externalJsMethodOne (const char* name, const size_t len, const uint64_t time, const uint8_t command, const int valueWords, const int aValue, const int aMask);
+typedef void externalJsMethodOne (const char* name, const size_t len, const int time, const int command, const int valueWords, const int aValue, const int aMask);
 
 typedef int  externalJsGetProperty(const char* name, const size_t len);
 typedef void externalJsSetProperty(const char* name, const size_t len, const int type, const int v0, const int v1);
@@ -52,6 +54,29 @@ void emit_lifee(const char* name) {
   externalZero(name, strlen(name));
 }
 
+void emit_triee(const char* name, const int64_t time, const int command, const int valueWords, uint64_t* aValue, uint64_t* aMask) {
+
+  // return;
+  // externalOne(
+  //   "hi"
+  //   ,2
+  //   ,time
+  //   ,command
+  //   ,0
+  //   ,0
+  //   ,0
+  // );
+  externalOne(
+    name,
+    strlen(name),
+    time,
+    command,
+    valueWords,
+    (int)aValue,
+    (int)aMask
+    );
+}
+
 
 
 
@@ -77,11 +102,12 @@ int init(
   externalZero = f0;
   externalOne  = f1;
 
-  state->lifee = (void*) externalZero;
-  state->triee = (void*) externalOne;
+  state->lifee = 0;
+  state->triee = 0;
 
   static char triggerString [4096] = "       ";
   static char tmpStr [4096] = "       ";
+  static char tmpStr2 [4096] = "       ";
   static uint64_t valueBuf [4096] = {};
   static uint64_t maskBuf [4096] = {};
 
@@ -89,6 +115,7 @@ int init(
   state->reason = "NO REASON";
   state->napi_env = 0;
   state->tmpStr = tmpStr;
+  state->tmpStr2 = tmpStr2;
   state->value = valueBuf;
   state->mask = maskBuf;
   state->digitCount = 0;
@@ -129,6 +156,8 @@ int32_t execute(
 }
 
 int setTrigger(const int context, char* triggerString) {
+  state->trigger = malloc(strlen(triggerString));
+  strcpy((char*)state->trigger, triggerString);
   // cout << "setTrigger() got " << triggerString << "\n";
   return 0;
 }
@@ -165,7 +194,7 @@ int getTime(const int context) {
 // }
 
 int main(void) {
-  cout << "main()\n";
+  // cout << "main()\n";
   return 0;
 }
 
diff --git a/wasm_main.hpp b/wasm_main.hpp
index c83b02a..6472298 100644
--- a/wasm_main.hpp
+++ b/wasm_main.hpp
@@ -8,3 +8,4 @@ void set_path_to_path(const char* name, const char* value);
 void new_object_path(const char* name);
 int get_property_int(const char* name);
 void emit_lifee(const char* name);
+void emit_triee(const char* name, const int64_t time, const int command, const int valueWords, uint64_t* aValue, uint64_t* aMask);