|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const parseTimescale = require('./parse-time-scale.js'); |
| 4 | + |
| 5 | +const MAX_SAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER); |
| 6 | + |
| 7 | +const numberOrString = val => { |
| 8 | + if (val < MAX_SAFE_INTEGER) { |
| 9 | + return Number(val); |
| 10 | + } |
| 11 | + return '0x' + val.toString(16); |
| 12 | +}; |
| 13 | + |
| 14 | +const gcd = (a, b) => { |
| 15 | + if (a === undefined) { |
| 16 | + return b; |
| 17 | + } |
| 18 | + let r; |
| 19 | + while (b !== 0) { |
| 20 | + r = a % b; |
| 21 | + a = b; |
| 22 | + b = r; |
| 23 | + } |
| 24 | + return (a < 0) ? -a : a; |
| 25 | +}; |
| 26 | + |
| 27 | +const tNorm = o => { |
| 28 | + const {tgcd, chango} = o; |
| 29 | + |
| 30 | + o.t0 /= tgcd; |
| 31 | + o.time /= tgcd; |
| 32 | + Object.keys(chango).map(key => { |
| 33 | + const {wave} = chango[key]; |
| 34 | + wave.map(e => { |
| 35 | + e[0] /= tgcd; |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + const exp = Math.log10(tgcd) |0; |
| 40 | + if (exp > 0) { |
| 41 | + const scale = Math.pow(10, exp); |
| 42 | + const tgcd1 = tgcd / scale; |
| 43 | + if (tgcd1 === (tgcd1 |0)) { |
| 44 | + o.tgcd = tgcd1; |
| 45 | + o.timescale += exp; |
| 46 | + } |
| 47 | + } |
| 48 | + return o; |
| 49 | +}; |
| 50 | + |
| 51 | + |
| 52 | +module.exports = async (deso, inst, done) => { |
| 53 | + const chango = {}; |
| 54 | + let tgcd; |
| 55 | + deso.chango = chango; |
| 56 | + deso.view = []; |
| 57 | + |
| 58 | + const onAnyChange = (id, time, cmd, value, mask) => { |
| 59 | + // console.log(id, time, cmd, value, mask); |
| 60 | + const time53 = Number(time); |
| 61 | + tgcd = gcd(tgcd, time53); |
| 62 | + chango[id] = chango[id] || {wave: []}; |
| 63 | + if (cmd >= 14 && cmd <= 28) { |
| 64 | + chango[id].kind = 'bit'; |
| 65 | + chango[id].wave.push([time53, cmd - 14]); |
| 66 | + } else { |
| 67 | + chango[id].kind = 'vec'; |
| 68 | + const point = [time53, numberOrString(value)]; |
| 69 | + if (mask !== 0n) { |
| 70 | + point.push(numberOrString(mask)); |
| 71 | + } |
| 72 | + chango[id].wave.push(point); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + const t0 = Date.now(); |
| 77 | + |
| 78 | + inst.on('$enddefinitions', () => { |
| 79 | + // console.log('$enddefinitions'); |
| 80 | + Object.assign(deso.wires, inst.info.wires); |
| 81 | + deso.timescale = parseTimescale(inst.info.timescale); |
| 82 | + }); |
| 83 | + |
| 84 | + inst.change.any(onAnyChange); |
| 85 | + |
| 86 | + inst.on('finish', () => { |
| 87 | + console.log((Date.now() - t0) / 1000); |
| 88 | + deso.tgcd = tgcd; |
| 89 | + deso.t0 = (inst.info.t0 || 0); |
| 90 | + // console.log(inst.getTime()); |
| 91 | + deso.time = Number(inst.getTime()); |
| 92 | + tNorm(deso); |
| 93 | + done(deso); |
| 94 | + }); |
| 95 | +}; |
0 commit comments