`);return q}powercycle(){this.resetStartTimeMsec=new Date().getTime()-1,this.finished=!1,this.stopped=!1,this.basefuncs._ctor_var_reset(this.state),this.basefuncs._eval_initial(this.state);for(var q=0;q<100;q++){this.basefuncs._eval_settle(this.state),this.basefuncs._eval(this.state);var P0=this.basefuncs._change_request(this.state);if(!P0){this.settleTime=q;return}}throw new s4(null,"model did not converge on reset()")}eval(){var q=this.state.clk,P0=this.state.reset,q0=this.state.test_CPU16_top$cpu$state,J9=this.state.CPU$opcode,le=this.state.CPU$aluop,W9=q0;this.curfuncs=this.basefuncs;for(var oe=0;oe<100;oe++){this.curfuncs._eval(this.state);var vk=this.curfuncs._change_request(this.state);if(!vk){this.settleTime=oe;return}}throw new s4(null,"model did not converge on eval()")}tick2(q){for(;q-- >0;)this.state.clk=0,this.eval(),this.state.clk=1,this.eval()}defaultValue(q,P0){if(K7(q))return q.left<=31?0:BigInt(0);if(gA(q)&&typeof q.high.cvalue=="number"&&typeof q.low.cvalue=="number"){let q0,J9=q.high.cvalue-q.low.cvalue+1;if(J9<0&&(J9=-J9),K7(q.subtype))q.subtype.left<=7?q0=new Uint8Array(J9):q.subtype.left<=15?q0=new Uint16Array(J9):q.subtype.left<=31?q0=new Uint32Array(J9):q0=[];else{q0=[];for(let le=0;le<J9;le++)q0[le]=this.defaultValue(q.subtype)}if(P0!=null&&P0.initValue!=null)for(let le=0;le<P0.initValue.exprs.length;le++){let W9=P0.initValue.exprs[le];if(eR(W9)&&VI(W9.expr))q0[W9.index]=W9.expr.cvalue;else throw new s4(q,"non-const expr in initarray")}return q0}throw new s4(q,`nodefaultvalueforvartype:${P0.name}`)}constValue(q){if(VI(q))return q.cvalue;throw new s4(q,"no const value for expr")}block2js(q){return this.expr2js(q)}expr2js(q,P0){if(q==null)return"/*null*/";if(Va(q)){if(this.curconsts[q.refname]!=null&&!(P0||{}).store)return this.constused++,`${this.curconsts[q.refname]}`;if(this.locals[q.refname])return`${q.refname}`;if(this.globals[q.refname])return`o.${q.refname}`;throw new s4(q,`cannotfindvariable'${q.refname}'`)}else if(gZ(q)){this.locals[q.name]=q;let W9=`var${q.name}`;if(q.constValue!=null)W9+=`=${this.constValue(q)}`;else{if(q.initValue!=null)throw new s4(q,"can't init array here");K7(q.dtype)&&q.dtype.left>31&&(W9+=" = []")}return W9}else{if(VI(q))return`0x${q.cvalue.toString(16)}`;if(KF(q))return q.bigvalue.toString();if(PF(q))switch(q.op){case"if":return q.right==null||wZ(q.right)&&q.right.exprs.length==0?`if(${this.expr2js(q.cond,{cond:!0})}){${this.expr2js(q.left)}}`:`if(${this.expr2js(q.cond,{cond:!0})}){${this.expr2js(q.left)}}else{${this.expr2js(q.right)}}`;case"cond":case"condbound":return`(${this.expr2js(q.cond,{cond:!0})}?${this.expr2js(q.left)}:${this.expr2js(q.right)})`;default:throw new s4(q,`unknowntriop${q.op}`)}else if(Yg(q))switch(q.op){case"contassign":case"assign":case"assignpre":case"assigndly":case"assignpost":return`${this.expr2js(q.right,{store:!0})}=${this.expr2js(q.left)}`;case"arraysel":case"wordsel":return`${this.expr2js(q.left)}[${this.expr2js(q.right)}]`;case"changedet":return`$$req|=(${this.expr2js(q.left)}^${this.expr2js(q.right)});${this.expr2js(q.right)}=${this.expr2js(q.left)}`;default:var q0=ca3[q.op];if(!q0)throw new s4(q,`unknownbinop${q.op}`);return q0.startsWith("?")&&(q0=q0.substr(1),!P0||!P0.cond)?`((${this.expr2js(q.left)}${q0}${this.expr2js(q.right)})?1:0)`:`(${this.expr2js(q.left)}${q0}${this.expr2js(q.right)})`}else if(OF(q))switch(q.op){case"ccast":return this.expr2js(q.left);case"creturn":return`return${this.expr2js(q.left)}`;case"creset":return this.expr2reset(q.left);case"not":return`(~${this.expr2js(q.left)})`;case"negate":return`(-${this.expr2js(q.left)})`;case"extends":let W9=32-q.widthminv;return`((${this.expr2js(q.left)}<<${W9})>>${W9})`;case"redxor":return`this.$$${q.op}(${this.expr2js(q.left)})`;default:throw new s4(q,`unknownunop${q.op}`)}else if(wZ(q)){var J9=q.exprs.map(W9=>this.expr2js(W9)).join(`;
;return$$req;`;if(q.blocktype=="sformatf"){var le=q.exprs.map(W9=>this.expr2js(W9));return le=[JSON.stringify(q.name)].concat(le),le.join(", ")}}return J9}else{if(qF(q))return`for(${this.expr2js(q.precond)};${this.expr2js(q.loopcond)};${this.expr2js(q.inc)}){${this.expr2js(q.body)}}`;if($F(q))return(q.funcname=="$stop"||q.funcname=="$finish")&&q.$loc?`this.${q.funcname}(o,${JSON.stringify(q.$loc)})`:q.args==null||q.args.length==0?`this.${q.funcname}(o)`:`this.${q.funcname}(o,${q.args.map(W9=>this.expr2js(W9)).join(", ")})`}}throw console.log(q),new Error(`unrecognizedexpr:${JSON.stringify(q)}`)}expr2reset(q){if(Va(q)){if(this.curconsts[q.refname]!=null)return`${q.refname}`;if(K7(q.dtype))return q.dtype.left<=31?`${this.expr2js(q)}=0`:`${this.expr2js(q)}=BigInt(0)`;if(gA(q.dtype)){if(K7(q.dtype.subtype))return`${this.expr2js(q)}.fill(0)`;if(gA(q.dtype.subtype)&&K7(q.dtype.subtype.subtype))return`${this.expr2js(q)}.forEach((a)=>a.fill(0))`;throw new s4(q,`unsupporteddatatypeforreset:${JSON.stringify(q.dtype)}`)}}else throw new s4(q,"can only reset var refs")}$finish(q,P0){this.finished||(console.log("Simulation $finish",P0),this.finished=!0)}$stop(q,P0){this.stopped||(console.log("Simulation $stop",P0),this.stopped=!0)}$rand(q){return Math.random()|0}$display(q,P0,...q0){console.log(P0,q0)}$readmem(q,P0,q0,J9,le,W9){for(var oe=[],vk=0;vk<P0.length;vk++)oe.push(P0[vk]>>0&255),oe.push(P0[vk]>>8&255),oe.push(P0[vk]>>16&255),oe.push(P0[vk]>>24&255);oe=oe.filter(G7=>G7!=0),oe.reverse();var Y4=qy0(oe),Nn=this.getFileData(Y4);if(Nn==null)throw new s4(null,"Could not $readmem '"+Y4+"'");var If=Nn.split(`
`).filter(G7=>G7!=="").map(G7=>parseInt(G7,W9?16:2));if(console.log("$readmem",W9,Y4,If.length),q0===null)throw new s4(null,"No destination array to $readmem "+Y4);if(q0.length<If.length)throw new s4(null,"Destination array too small to $readmem "+Y4);for(vk=0;vk<If.length;vk++)q0[vk]=If[vk]}$time(q){return new Date().getTime()-this.resetStartTimeMsec}$$redxor(q){return q=q^q>>1,q=q^q>>2,q=q^q>>4,q=q^q>>8,q=q^q>>16,q}isStopped(){return this.stopped}isFinished(){return this.finished}tick(){this.state.clk^=1,this.eval()}get(q){return this.state[q]}set(q,P0){q in this.state&&(this.state[q]=P0)}saveState(){return xP(!0,{},this.state)}getGlobals(){return this.saveState()}loadState(q){xP(!0,this.state,q)}},ca3={eq:"?===",neq:"?!==",gt:"?>",lt:"?<",gte:"?>=",lte:"?<=",and:"&",or:"|",xor:"^",add:"+",sub:"-",shiftr:">>>",shiftl:"<<",mul:"*",moddiv:"%",div:"/",muls:"*",moddivs:"%",divs:"/",gts:"?>",gtes:"?>=",lts:"?<",ltes:"?<="};var mk=na3(sm0());var ua3=["_ctor_var_reset","_eval_initial","_eval_settle","_eval","_change_request"],Xg=0,Am0="$$MEM",sa3="$$GLOBAL",jP="$$CHANGE",SP="$$treclen",JP="$$tofs",DP="$$tend",_P="$$tbuf";function Pt(y){if(K7(y))return y.left<=7?1:y.left<=15?2:y.left<=31?4:y.left<=63?8:(y.left>>6)*8+8;if(gA(y))return(Math.abs(y.high.cvalue-y.low.cvalue)+1)*Pt(y.subtype);throw new s4(y,"don't know data type")}function Aa3(y){return Pt(y)>8}function om0(y){return gA(y)?om0(y.subtype):Pt(y)}function lm0(y){if(yl(y)&&gA(y.dtype))return Pt(y.dtype.subtype);throw yl(y)&&K7(y.dtype)&&y.dtype.left>63?new s4(y,"elements > 64 bits not supported"):new s4(y,"cannot figure out array element size")}function oa3(y){return Pt(kR(y))}function kR(y){if(Va(y)){for(var q=y.dtype;gA(q);)q=q.subtype;return q}else{if(Yg(y)&&y.op=="arraysel")return kR(y.left);if(Yg(y)&&y.op=="wordsel")return kR(y.left)}throw new s4(y,"cannot figure out array value type")}function la3(y){return y<=1?1:y<=2?2:y<=4?4:8}function iR(y){return y<=4?mk.i32:y<=8?mk.i64:mk.none}var TP=class{constructor(){this.len=0;this.vars={};this.locals=[];this.params=[]}addVar(q){var P0=Pt(q.dtype),q0=this.addEntry(q.name,P0,iR(P0),q.dtype,!1);return q0.init=q.initValue,q0.constval=q.constValue,q0}alignTo(q){for(;this.len%q;)this.len++}addEntry(q,P0,q0,J9,le){this.alignTo(la3(P0));var W9={name:q,type:J9,size:P0,itype:q0,index:this.params.length+this.locals.length,offset:this.len,init:null,constval:null,reset:!1};return this.len+=P0,W9.name!=null&&(this.vars[W9.name]=W9),le?this.params.push(W9):this.locals.push(W9),W9}getLocals(){var q=[];for(let P0 of this.locals)q.push(P0.itype);return q}lookup(q){return this.vars[q]}},dZ=class{constructor(q,P0,q0){this.getFileData=null;this.optimize=!1;this.maxEvalIterations=8;this.traceBufferSize=1044480;this.randomizeOnReset=!1;this.labelseq=0;this.hdlmod=q,this.constpool=P0,this.maxMemoryMB=q0||16,this.genMemory(),this.genFuncs(),this.validate()}async init(){await this.genModule(),this.genStateInterface(),this.enableTracing()}initSync(){this.genModuleSync(),this.genStateInterface(),this.enableTracing()}powercycle(){this.resetStartTimeMsec=new Date().getTime()-1,this.finished=!1,this.stopped=!1,this.clearMutableState(),this.setInitialValues(),this.instance.exports._ctor_var_reset(Xg),this.instance.exports._eval_initial(Xg);for(var q=0;q<100;q++){this.instance.exports._eval_settle(Xg),this.instance.exports._eval(Xg);var P0=this.instance.exports._change_request(Xg);if(!P0)return}throw new s4(null,"model did not converge on reset()")}eval(){this.instance.exports.eval(Xg)}tick(){this.state.clk^=1,this.eval()}tick2(q){this.instance.exports.tick2(Xg,q)}isFinished(){return this.finished}isStopped(){return this.stopped}saveState(){return{o:this.data8.slice(0,this.statebytes)}}loadState(q){this.data8.set(q.o)}getGlobals(){var q={};for(let[le,W9]of Object.entries(this.hdlmod.vardefs)){var P0=q,q0=le.split("$");for(var J9 of q0.slice(0,-1))P0[J9]=P0[J9]||{},P0=P0[J9];P0[q0[q0.length-1]]=this.state[le]}return q}enableTracing(){if(this.outputbytes==0)throw new Error("outputbytes == 0");if(this.outputbytes%8)throw new Error("outputbytes must be 8-
`).filter(Y4=>Y4!=="").map(Y4=>parseInt(Y4,oe?16:2)),J9=0;J9<vk.length;J9++)this.data8[P0+J9]=vk[J9];return 0}label(q){return`@${q||"label"}_${++this.labelseq}`}addCopyTraceRecFunction(){let q=this.bmod,P0=this.globals.lookup(SP).offset,q0=this.globals.lookup(JP).offset,J9=this.globals.lookup(DP).offset,le=this.globals.lookup(_P).offset;var W9=mk.i32,oe=mk.none,vk=this.label("@block"),Y4=this.label("@loop");q.addFunction("copyTraceRec",mk.createType([]),oe,[W9,W9,W9],q.block(vk,[q.local.set(0,q.i32.const(Xg)),q.local.set(1,q.i32.load(0,4,q.i32.const(P0))),q.local.set(2,q.i32.load(0,4,q.i32.const(q0))),q.loop(Y4,q.block(null,[q.i64.store(0,8,q.local.get(2,W9),q.i64.load(0,8,q.local.get(0,W9))),q.local.set(0,q.i32.add(q.local.get(0,W9),q.i32.const(8))),q.local.set(2,q.i32.add(q.local.get(2,W9),q.i32.const(8))),q.local.set(1,q.i32.sub(q.local.get(1,W9),q.i32.const(8))),this.bmod.br_if(Y4,q.local.get(1,W9))])),q.i32.store(0,4,q.i32.const(q0),q.i32.add(q.i32.load(0,4,q.i32.const(q0)),q.i32.load(0,4,q.i32.const(P0)))),q.br_if(vk,q.i32.lt_u(q.i32.load(0,4,q.i32.const(q0)),q.i32.load(0,4,q.i32.const(J9)))),q.i32.store(0,4,q.i32.const(q0),q.i32.const(le))]))}addTick2Function(){let q=this.bmod;var P0=this.label("@loop");if(this.globals.lookup("clk")){var q0=q.local.get(0,mk.i32);q.addFunction("tick2",mk.createType([mk.i32,mk.i32]),mk.none,[],q.loop(P0,q.block(null,[this.makeSetVariableFunction("clk",0),q.drop(q.call("eval",[q0],mk.i32)),this.makeSetVariableFunction("clk",1),q.drop(q.call("eval",[q0],mk.i32)),q.call("copyTraceRec",[],mk.none),q.br_if(P0,q.local.tee(1,q.i32.sub(q.local.get(1,mk.i32),q.i32.const(1)),mk.i32))]))),q.addFunctionExport("tick2","tick2")}else q.addFunctionExport("eval","tick2")}addEvalFunction(){this.bmod.addFunction("eval",mk.createType([mk.i32]),mk.i32,[],this.makeTickFuncBody(0)),this.bmod.addFunctionExport("eval","eval")}makeGetVariableFunction(q,P0){var q0=this.globals.lookup(q).type,J9={refname:q,dtype:q0};return this.e2w(J9)}makeSetVariableFunction(q,P0){var q0=this.globals.lookup(q).type,J9={refname:q,dtype:q0},le={cvalue:P0,bigvalue:null,dtype:q0};return this.assign2wasm(J9,le)}makeTickFuncBody(q){var P0=this.bmod.local.get(0,mk.i32);return q>this.maxEvalIterations?this.bmod.i32.const(q):this.bmod.block(null,[this.bmod.call("_eval",[P0],mk.none),this.bmod.if(this.bmod.call("_change_request",[P0],mk.i32),this.makeTickFuncBody(q+1),this.bmod.return(this.bmod.local.get(0,mk.i32)))],mk.i32)}funcResult(q){return q.startsWith("_change_request")?mk.i32:q=="$time"?mk.i64:q=="$rand"?mk.i32:mk.none}pushScope(q){q.parent=this.locals,this.locals=q}popScope(){this.locals=this.locals.parent}i3264(q){var P0=Pt(q),q0=iR(P0);if(q0==mk.i32)return this.bmod.i32;if(q0==mk.i64)return this.bmod.i64;throw new s4(q,"data types > 64 bits not supported")}i3264rel(q){if(yl(q.left)&&yl(q.right)){var P0=Pt(q.left.dtype),q0=Pt(q.right.dtype);return P0>q0?this.i3264(q.left.dtype):this.i3264(q.right.dtype)}throw new s4(q,`can't${q.op}arguments`)}dataptr(){return this.bmod.local.get(0,mk.i32)}e2w(q,P0){if(q==null)return this.bmod.nop();if(wZ(q))return this.block2wasm(q,P0);if(gZ(q))return this.local2wasm(q,P0);if(Va(q))return this.varref2wasm(q,P0);if(VI(q)||KF(q))return this.const2wasm(q,P0);if($F(q))return this.funccall2wasm(q,P0);if(OF(q)||Yg(q)||PF(q)||qF(q)){var q0=`_${q.op}2wasm`,J9=this[q0];if(J9==null)throw new s4(q,`nosuchmethod${q0}`);return this[q0](q,P0)}else throw new s4(q,"could not translate expr")}block2wasm(q,P0){var q0=q.exprs.map(le=>this.e2w(le)),J9=P0&&P0.funcblock?this.funcResult(P0.funcblock.name):mk.none;return J9==mk.i32&&q0.push(this.bmod.return(this.bmod.local.get(this.locals.lookup(jP).index,J9))),P0&&P0.resulttype&&(J9=mk.i32),this.bmod.block(q.name,q0,J9)}funccall2wasm(q,P0){var q0=[this.dataptr()];for(var J9 of q.args)q0.push(this.e2w(J9,{funcarg:!0}));var le=q.funcname;q.funcname.startsWith("$")&&((q.funcname=="$stop"||q.funcname=="$finish")&&q.$loc&&(q0=[this.bmod.i32.const(q.$loc.line)]),le+="_"+(q0.length-1));var W9=this.funcResult(q.funcname);return this.bmod.call(le,q0,W9)}const2wasm(q,P