yosys2digitaljs 0.8.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/core.js ADDED
@@ -0,0 +1,1082 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.yosys2digitaljs = yosys2digitaljs;
5
+ exports.io_ui = io_ui;
6
+ exports.prepare_yosys_script = prepare_yosys_script;
7
+ exports.prepare_verilator_args = prepare_verilator_args;
8
+ const HashMap = require("hashmap");
9
+ const bigInt = require("big-integer");
10
+ const _3vl_1 = require("3vl");
11
+ const topsort = require('topsort');
12
+ function assert_fallback(val, msg) {
13
+ if (!val)
14
+ throw new Error(msg || "Assertion failed");
15
+ }
16
+ const isNode = typeof process !== "undefined" &&
17
+ process.versions != null &&
18
+ process.versions.node != null;
19
+ const assert = isNode
20
+ ? require('assert')
21
+ : assert_fallback;
22
+ ;
23
+ var Yosys;
24
+ (function (Yosys) {
25
+ Yosys.ConstChars = ["0", "1", "x", "z"];
26
+ })(Yosys || (Yosys = {}));
27
+ ;
28
+ const unary_gates = new Set([
29
+ '$not', '$neg', '$pos', '$reduce_and', '$reduce_or', '$reduce_xor',
30
+ '$reduce_xnor', '$reduce_bool', '$logic_not'
31
+ ]);
32
+ const binary_gates = new Set([
33
+ '$and', '$or', '$xor', '$xnor',
34
+ '$add', '$sub', '$mul', '$div', '$mod', '$pow',
35
+ '$lt', '$le', '$eq', '$ne', '$ge', '$gt', '$eqx', '$nex',
36
+ '$shl', '$shr', '$sshl', '$sshr', '$shift', '$shiftx',
37
+ '$logic_and', '$logic_or'
38
+ ]);
39
+ const gate_subst = new Map([
40
+ ['$not', 'Not'],
41
+ ['$and', 'And'],
42
+ ['$nand', 'Nand'],
43
+ ['$or', 'Or'],
44
+ ['$nor', 'Nor'],
45
+ ['$xor', 'Xor'],
46
+ ['$xnor', 'Xnor'],
47
+ ['$reduce_and', 'AndReduce'],
48
+ ['$reduce_nand', 'NandReduce'],
49
+ ['$reduce_or', 'OrReduce'],
50
+ ['$reduce_nor', 'NorReduce'],
51
+ ['$reduce_xor', 'XorReduce'],
52
+ ['$reduce_xnor', 'XnorReduce'],
53
+ ['$reduce_bool', 'OrReduce'],
54
+ ['$logic_not', 'NorReduce'],
55
+ ['$repeater', 'Repeater'],
56
+ ['$shl', 'ShiftLeft'],
57
+ ['$shr', 'ShiftRight'],
58
+ ['$lt', 'Lt'],
59
+ ['$le', 'Le'],
60
+ ['$eq', 'Eq'],
61
+ ['$ne', 'Ne'],
62
+ ['$gt', 'Gt'],
63
+ ['$ge', 'Ge'],
64
+ ['$constant', 'Constant'],
65
+ ['$neg', 'Negation'],
66
+ ['$pos', 'UnaryPlus'],
67
+ ['$add', 'Addition'],
68
+ ['$sub', 'Subtraction'],
69
+ ['$mul', 'Multiplication'],
70
+ ['$div', 'Division'],
71
+ ['$mod', 'Modulo'],
72
+ ['$pow', 'Power'],
73
+ ['$mux', 'Mux'],
74
+ ['$pmux', 'Mux1Hot'],
75
+ ['$mem', 'Memory'],
76
+ ['$mem_v2', 'Memory'],
77
+ ['$lut', 'Memory'],
78
+ ['$fsm', 'FSM'],
79
+ ['$clock', 'Clock'],
80
+ ['$button', 'Button'],
81
+ ['$lamp', 'Lamp'],
82
+ ['$numdisplay', 'NumDisplay'],
83
+ ['$numentry', 'NumEntry'],
84
+ ['$input', 'Input'],
85
+ ['$output', 'Output'],
86
+ ['$busgroup', 'BusGroup'],
87
+ ['$busungroup', 'BusUngroup'],
88
+ ['$busslice', 'BusSlice'],
89
+ ['$zeroextend', 'ZeroExtend'],
90
+ ['$signextend', 'SignExtend'],
91
+ ['$reduce_bool', 'OrReduce'],
92
+ ['$eqx', 'Eq'],
93
+ ['$nex', 'Ne'],
94
+ ['$sshl', 'ShiftLeft'],
95
+ ['$sshr', 'ShiftRight'],
96
+ ['$shift', 'ShiftRight'],
97
+ ['$shiftx', 'ShiftRight'],
98
+ ['$logic_and', 'And'],
99
+ ['$logic_or', 'Or'],
100
+ ['$dff', 'Dff'],
101
+ ['$dffe', 'Dff'],
102
+ ['$adff', 'Dff'],
103
+ ['$adffe', 'Dff'],
104
+ ['$sdff', 'Dff'],
105
+ ['$sdffe', 'Dff'],
106
+ ['$sdffce', 'Dff'],
107
+ ['$dlatch', 'Dff'],
108
+ ['$adlatch', 'Dff'],
109
+ ['$sr', 'Dff'],
110
+ ['$dffsr', 'Dff'],
111
+ ['$dffsre', 'Dff'],
112
+ ['$aldff', 'Dff'],
113
+ ['$aldffe', 'Dff']
114
+ ]);
115
+ function module_deps(data) {
116
+ const out = [];
117
+ for (const [name, mod] of Object.entries(data.modules)) {
118
+ out.push([name, 1 / 0]);
119
+ for (const cname in mod.cells) {
120
+ const cell = mod.cells[cname];
121
+ if (cell.type in data.modules)
122
+ out.push([cell.type, name]);
123
+ }
124
+ }
125
+ return out;
126
+ }
127
+ function order_ports(data) {
128
+ const unmap = { A: 'in', Y: 'out' };
129
+ const binmap = { A: 'in1', B: 'in2', Y: 'out' };
130
+ const out = {
131
+ '$mux': { A: 'in0', B: 'in1', S: 'sel', Y: 'out' },
132
+ '$dff': { CLK: 'clk', D: 'in', Q: 'out' },
133
+ '$dffe': { CLK: 'clk', EN: 'en', D: 'in', Q: 'out' },
134
+ '$adff': { CLK: 'clk', ARST: 'arst', D: 'in', Q: 'out' },
135
+ '$adffe': { CLK: 'clk', EN: 'en', ARST: 'arst', D: 'in', Q: 'out' },
136
+ '$sdff': { CLK: 'clk', SRST: 'srst', D: 'in', Q: 'out' },
137
+ '$sdffe': { CLK: 'clk', EN: 'en', SRST: 'srst', D: 'in', Q: 'out' },
138
+ '$sdffce': { CLK: 'clk', EN: 'en', SRST: 'srst', D: 'in', Q: 'out' },
139
+ '$dlatch': { EN: 'en', D: 'in', Q: 'out' },
140
+ '$adlatch': { EN: 'en', ARST: 'arst', D: 'in', Q: 'out' },
141
+ '$dffsr': { CLK: 'clk', SET: 'set', CLR: 'clr', D: 'in', Q: 'out' },
142
+ '$dffsre': { CLK: 'clk', EN: 'en', SET: 'set', CLR: 'clr', D: 'in', Q: 'out' },
143
+ '$aldff': { CLK: 'clk', ALOAD: 'aload', AD: 'ain', D: 'in', Q: 'out' },
144
+ '$aldffe': { CLK: 'clk', EN: 'en', ALOAD: 'aload', AD: 'ain', D: 'in', Q: 'out' },
145
+ '$sr': { SET: 'set', CLR: 'clr', Q: 'out' },
146
+ '$fsm': { ARST: 'arst', CLK: 'clk', CTRL_IN: 'in', CTRL_OUT: 'out' }
147
+ };
148
+ binary_gates.forEach((nm) => out[nm] = binmap);
149
+ unary_gates.forEach((nm) => out[nm] = unmap);
150
+ for (const [name, mod] of Object.entries(data.modules)) {
151
+ const portmap = {};
152
+ const ins = [], outs = [];
153
+ for (const pname in mod.ports) {
154
+ portmap[pname] = pname;
155
+ }
156
+ out[name] = portmap;
157
+ }
158
+ return out;
159
+ }
160
+ function decode_json_bigint(param) {
161
+ if (typeof param == 'string')
162
+ return bigInt(param, 2);
163
+ else if (typeof param == 'number')
164
+ return bigInt(param);
165
+ else
166
+ assert(false);
167
+ }
168
+ function decode_json_number(param) {
169
+ if (typeof param == 'string')
170
+ return Number.parseInt(param, 2);
171
+ else if (typeof param == 'number')
172
+ return param;
173
+ else
174
+ assert(false);
175
+ }
176
+ function decode_json_bigint_as_array(param) {
177
+ return decode_json_bigint(param).toArray(2).value;
178
+ }
179
+ function decode_json_constant(param, bits, fill = '0') {
180
+ if (typeof param == 'number')
181
+ return bigInt(param).toArray(2).value.map(String).reverse()
182
+ .concat(Array(bits).fill(fill)).slice(0, bits).reverse().join('');
183
+ else
184
+ return param;
185
+ }
186
+ function parse_source_positions(str) {
187
+ const ret = [];
188
+ for (const entry of str.split('|')) {
189
+ const colonIdx = entry.lastIndexOf(':');
190
+ const name = entry.slice(0, colonIdx);
191
+ const pos = entry.slice(colonIdx + 1);
192
+ const [from, to] = pos.split('-').map(s => s.split('.').map(v => Number(v))).map(([line, column]) => ({ line, column }));
193
+ ret.push({ name, from, to });
194
+ }
195
+ return ret;
196
+ }
197
+ function yosys_to_digitaljs(data, portmaps, options = {}) {
198
+ const out = {};
199
+ for (const [name, mod] of Object.entries(data.modules)) {
200
+ out[name] = yosys_to_digitaljs_mod(name, mod, portmaps, options);
201
+ }
202
+ return out;
203
+ }
204
+ function yosys_to_digitaljs_mod(name, mod, portmaps, options = {}) {
205
+ function constbit(bit) {
206
+ return Yosys.ConstChars.includes(bit.toString());
207
+ }
208
+ const nets = new HashMap();
209
+ const netnames = new HashMap();
210
+ const netsrc = new HashMap();
211
+ const bits = new Map();
212
+ const devnets = new Map();
213
+ let n = 0, pn = 0;
214
+ function gen_name() {
215
+ const nm = `dev${n++}`;
216
+ devnets.set(nm, new Map());
217
+ return nm;
218
+ }
219
+ function gen_bitname() {
220
+ return `bit${pn++}`;
221
+ }
222
+ function get_net(k) {
223
+ // create net if does not exist yet
224
+ if (!nets.has(k)) {
225
+ const nms = netnames.get(k);
226
+ const src = netsrc.get(k);
227
+ nets.set(k, { source: undefined, targets: [], name: nms ? nms[0] : undefined, source_positions: src || [] });
228
+ }
229
+ return nets.get(k);
230
+ }
231
+ function add_net_source(k, d, p, primary = false) {
232
+ if (k.length == 0)
233
+ return; // for unconnected ports
234
+ const net = get_net(k);
235
+ if (net.source !== undefined) {
236
+ // multiple sources driving one net, disallowed in digitaljs
237
+ throw Error('Multiple sources driving net: ' + net.name);
238
+ }
239
+ net.source = { id: d, port: p };
240
+ if (primary)
241
+ for (const [nbit, bit] of k.entries()) {
242
+ bits.set(bit, { id: d, port: p, num: nbit });
243
+ }
244
+ devnets.get(d).set(p, k);
245
+ }
246
+ function add_net_target(k, d, p) {
247
+ if (k.length == 0)
248
+ return; // for unconnected ports
249
+ const net = get_net(k);
250
+ net.targets.push({ id: d, port: p });
251
+ devnets.get(d).set(p, k);
252
+ }
253
+ const mout = {
254
+ devices: {},
255
+ connectors: []
256
+ };
257
+ function add_device(dev) {
258
+ const dname = gen_name();
259
+ if (options.propagation !== undefined)
260
+ dev.propagation = options.propagation;
261
+ mout.devices[dname] = dev;
262
+ return dname;
263
+ }
264
+ function add_busgroup(nbits, groups) {
265
+ if (get_net(nbits).source !== undefined)
266
+ return; // the bits were already grouped
267
+ const dname = add_device({
268
+ type: 'BusGroup',
269
+ groups: groups.map(g => g.length)
270
+ });
271
+ add_net_source(nbits, dname, 'out');
272
+ for (const [gn, group] of groups.entries()) {
273
+ add_net_target(group, dname, 'in' + gn);
274
+ }
275
+ }
276
+ function connect_device(dname, cell, portmap) {
277
+ for (const [pname, pdir] of Object.entries(cell.port_directions)) {
278
+ const pconn = cell.connections[pname];
279
+ switch (pdir) {
280
+ case 'input':
281
+ add_net_target(pconn, dname, portmap[pname]);
282
+ break;
283
+ case 'output':
284
+ add_net_source(pconn, dname, portmap[pname], true);
285
+ break;
286
+ default:
287
+ throw Error('Invalid port direction: ' + pdir);
288
+ }
289
+ }
290
+ }
291
+ function connect_pmux(dname, cell) {
292
+ add_net_target(cell.connections.A, dname, 'in0');
293
+ add_net_target(cell.connections.S.slice().reverse(), dname, 'sel');
294
+ add_net_source(cell.connections.Y, dname, 'out', true);
295
+ for (const i of Array(decode_json_number(cell.parameters.S_WIDTH)).keys()) {
296
+ const p = (decode_json_number(cell.parameters.S_WIDTH) - i - 1) * decode_json_number(cell.parameters.WIDTH);
297
+ add_net_target(cell.connections.B.slice(p, p + decode_json_number(cell.parameters.WIDTH)), dname, 'in' + (i + 1));
298
+ }
299
+ }
300
+ function connect_mem(dname, cell, dev) {
301
+ for (const [k, port] of dev.rdports.entries()) {
302
+ const portname = "rd" + k;
303
+ add_net_target(cell.connections.RD_ADDR.slice(dev.abits * k, dev.abits * (k + 1)), dname, portname + "addr");
304
+ add_net_source(cell.connections.RD_DATA.slice(dev.bits * k, dev.bits * (k + 1)), dname, portname + "data", true);
305
+ if ('clock_polarity' in port)
306
+ add_net_target([cell.connections.RD_CLK[k]], dname, portname + "clk");
307
+ if ('enable_polarity' in port)
308
+ add_net_target([cell.connections.RD_EN[k]], dname, portname + "en");
309
+ if ('arst_polarity' in port)
310
+ add_net_target([cell.connections.RD_ARST[k]], dname, portname + "arst");
311
+ if ('srst_polarity' in port)
312
+ add_net_target([cell.connections.RD_SRST[k]], dname, portname + "srst");
313
+ }
314
+ for (const [k, port] of dev.wrports.entries()) {
315
+ const portname = "wr" + k;
316
+ add_net_target(cell.connections.WR_ADDR.slice(dev.abits * k, dev.abits * (k + 1)), dname, portname + "addr");
317
+ add_net_target(cell.connections.WR_DATA.slice(dev.bits * k, dev.bits * (k + 1)), dname, portname + "data");
318
+ if ('clock_polarity' in port)
319
+ add_net_target([cell.connections.WR_CLK[k]], dname, portname + "clk");
320
+ if ('enable_polarity' in port) {
321
+ if (port.no_bit_enable)
322
+ add_net_target([cell.connections.WR_EN[dev.bits * k]], dname, portname + "en");
323
+ else
324
+ add_net_target(cell.connections.WR_EN.slice(dev.bits * k, dev.bits * (k + 1)), dname, portname + "en");
325
+ }
326
+ }
327
+ }
328
+ // Find net names
329
+ for (const [nname, data] of Object.entries(mod.netnames)) {
330
+ if (data.hide_name)
331
+ continue;
332
+ let l = netnames.get(data.bits);
333
+ if (l === undefined) {
334
+ l = [];
335
+ netnames.set(data.bits, l);
336
+ }
337
+ l.push(nname);
338
+ if (typeof data.attributes == 'object' && data.attributes.src) {
339
+ let l = netsrc.get(data.bits);
340
+ if (l === undefined) {
341
+ l = [];
342
+ netsrc.set(data.bits, l);
343
+ }
344
+ const positions = parse_source_positions(data.attributes.src);
345
+ l.push(...positions);
346
+ }
347
+ }
348
+ // Add inputs/outputs
349
+ for (const [pname, port] of Object.entries(mod.ports)) {
350
+ const dir = port.direction == "input" ? "Input" :
351
+ port.direction == "output" ? "Output" :
352
+ undefined;
353
+ const dname = add_device({
354
+ type: dir,
355
+ net: pname,
356
+ order: n,
357
+ bits: port.bits.length
358
+ });
359
+ switch (port.direction) {
360
+ case 'input':
361
+ add_net_source(port.bits, dname, 'out', true);
362
+ break;
363
+ case 'output':
364
+ add_net_target(port.bits, dname, 'in');
365
+ break;
366
+ default: throw Error('Invalid port direction: ' + port.direction);
367
+ }
368
+ }
369
+ // Add gates
370
+ for (const [cname, cell] of Object.entries(mod.cells)) {
371
+ const dev = {
372
+ label: cname,
373
+ type: gate_subst.get(cell.type)
374
+ };
375
+ if (dev.type == undefined) {
376
+ dev.type = 'Subcircuit';
377
+ dev.celltype = cell.type;
378
+ }
379
+ if (typeof cell.attributes == 'object' && cell.attributes.src) {
380
+ dev.source_positions = parse_source_positions(cell.attributes.src);
381
+ }
382
+ const dname = add_device(dev);
383
+ function match_port(con, nsig, sz) {
384
+ const sig = decode_json_number(nsig);
385
+ if (con.length > sz)
386
+ con.splice(sz);
387
+ else if (con.length < sz) {
388
+ const ccon = con.slice();
389
+ const pad = sig ? con.slice(-1)[0] : '0';
390
+ con.splice(con.length, 0, ...Array(sz - con.length).fill(pad));
391
+ if (!con.every(constbit) && get_net(con).source === undefined) {
392
+ // WARNING: potentially troublesome hack for readability
393
+ // handled generally in the grouping phase,
394
+ // but it's hard to add sign extensions there
395
+ const extname = add_device({
396
+ type: sig ? 'SignExtend' : 'ZeroExtend',
397
+ extend: { input: ccon.length, output: con.length }
398
+ });
399
+ add_net_target(ccon, extname, 'in');
400
+ add_net_source(con, extname, 'out');
401
+ }
402
+ }
403
+ }
404
+ function zero_extend_output(con) {
405
+ if (con.length > 1) {
406
+ const ccon = con.slice();
407
+ con.splice(1);
408
+ const extname = add_device({
409
+ type: 'ZeroExtend',
410
+ extend: { input: con.length, output: ccon.length }
411
+ });
412
+ add_net_source(ccon, extname, 'out');
413
+ add_net_target(con, extname, 'in');
414
+ }
415
+ }
416
+ if (unary_gates.has(cell.type)) {
417
+ assert(cell.connections.A.length == decode_json_number(cell.parameters.A_WIDTH));
418
+ assert(cell.connections.Y.length == decode_json_number(cell.parameters.Y_WIDTH));
419
+ assert(cell.port_directions.A == 'input');
420
+ assert(cell.port_directions.Y == 'output');
421
+ }
422
+ if (binary_gates.has(cell.type)) {
423
+ assert(cell.connections.A.length == decode_json_number(cell.parameters.A_WIDTH));
424
+ assert(cell.connections.B.length == decode_json_number(cell.parameters.B_WIDTH));
425
+ assert(cell.connections.Y.length == decode_json_number(cell.parameters.Y_WIDTH));
426
+ assert(cell.port_directions.A == 'input');
427
+ assert(cell.port_directions.B == 'input');
428
+ assert(cell.port_directions.Y == 'output');
429
+ }
430
+ if (['$dff', '$dffe', '$adff', '$adffe', '$sdff', '$sdffe', '$sdffce', '$dlatch', '$adlatch', '$dffsr', '$dffsre', '$aldff', '$aldffe'].includes(cell.type)) {
431
+ assert(cell.connections.D.length == decode_json_number(cell.parameters.WIDTH));
432
+ assert(cell.connections.Q.length == decode_json_number(cell.parameters.WIDTH));
433
+ assert(cell.port_directions.D == 'input');
434
+ assert(cell.port_directions.Q == 'output');
435
+ if (cell.type != '$dlatch' && cell.type != '$adlatch') {
436
+ assert(cell.connections.CLK.length == 1);
437
+ assert(cell.port_directions.CLK == 'input');
438
+ }
439
+ }
440
+ if (['$dffe', '$adffe', '$sdffe', '$sdffce', '$dffsre', '$aldffe', '$dlatch', '$adlatch'].includes(cell.type)) {
441
+ assert(cell.connections.EN.length == 1);
442
+ assert(cell.port_directions.EN == 'input');
443
+ }
444
+ if (['$adff', '$adffe', '$adlatch'].includes(cell.type)) {
445
+ assert(cell.connections.ARST.length == 1);
446
+ assert(cell.port_directions.ARST == 'input');
447
+ }
448
+ if (['$sdff', '$sdffe', '$sdffce'].includes(cell.type)) {
449
+ assert(cell.connections.SRST.length == 1);
450
+ assert(cell.port_directions.SRST == 'input');
451
+ }
452
+ if (['$dffsr', '$dffsre'].includes(cell.type)) {
453
+ assert(cell.connections.SET.length == decode_json_number(cell.parameters.WIDTH));
454
+ assert(cell.connections.CLR.length == decode_json_number(cell.parameters.WIDTH));
455
+ assert(cell.port_directions.SET == 'input');
456
+ assert(cell.port_directions.CLR == 'input');
457
+ }
458
+ switch (cell.type) {
459
+ case '$neg':
460
+ case '$pos':
461
+ dev.bits = {
462
+ in: cell.connections.A.length,
463
+ out: cell.connections.Y.length
464
+ };
465
+ dev.signed = Boolean(decode_json_number(cell.parameters.A_SIGNED));
466
+ break;
467
+ case '$not':
468
+ match_port(cell.connections.A, cell.parameters.A_SIGNED, cell.connections.Y.length);
469
+ dev.bits = cell.connections.Y.length;
470
+ break;
471
+ case '$add':
472
+ case '$sub':
473
+ case '$mul':
474
+ case '$div':
475
+ case '$mod':
476
+ case '$pow':
477
+ dev.bits = {
478
+ in1: cell.connections.A.length,
479
+ in2: cell.connections.B.length,
480
+ out: cell.connections.Y.length
481
+ };
482
+ dev.signed = {
483
+ in1: Boolean(decode_json_number(cell.parameters.A_SIGNED)),
484
+ in2: Boolean(decode_json_number(cell.parameters.B_SIGNED))
485
+ };
486
+ break;
487
+ case '$and':
488
+ case '$or':
489
+ case '$xor':
490
+ case '$xnor':
491
+ match_port(cell.connections.A, cell.parameters.A_SIGNED, cell.connections.Y.length);
492
+ match_port(cell.connections.B, cell.parameters.B_SIGNED, cell.connections.Y.length);
493
+ dev.bits = cell.connections.Y.length;
494
+ break;
495
+ case '$reduce_and':
496
+ case '$reduce_or':
497
+ case '$reduce_xor':
498
+ case '$reduce_xnor':
499
+ case '$reduce_bool':
500
+ case '$logic_not':
501
+ dev.bits = cell.connections.A.length;
502
+ zero_extend_output(cell.connections.Y);
503
+ if (dev.bits == 1) {
504
+ if (['$reduce_xnor', '$logic_not'].includes(cell.type))
505
+ dev.type = 'Not';
506
+ else
507
+ dev.type = 'Repeater';
508
+ }
509
+ break;
510
+ case '$eq':
511
+ case '$ne':
512
+ case '$lt':
513
+ case '$le':
514
+ case '$gt':
515
+ case '$ge':
516
+ case '$eqx':
517
+ case '$nex':
518
+ dev.bits = {
519
+ in1: cell.connections.A.length,
520
+ in2: cell.connections.B.length
521
+ };
522
+ dev.signed = {
523
+ in1: Boolean(decode_json_number(cell.parameters.A_SIGNED)),
524
+ in2: Boolean(decode_json_number(cell.parameters.B_SIGNED))
525
+ };
526
+ zero_extend_output(cell.connections.Y);
527
+ break;
528
+ case '$shl':
529
+ case '$shr':
530
+ case '$sshl':
531
+ case '$sshr':
532
+ case '$shift':
533
+ case '$shiftx':
534
+ dev.bits = {
535
+ in1: cell.connections.A.length,
536
+ in2: cell.connections.B.length,
537
+ out: cell.connections.Y.length
538
+ };
539
+ dev.signed = {
540
+ in1: Boolean(decode_json_number(cell.parameters.A_SIGNED)),
541
+ in2: Boolean(decode_json_number(cell.parameters.B_SIGNED) && ['$shift', '$shiftx'].includes(cell.type)),
542
+ out: Boolean(decode_json_number(cell.parameters.A_SIGNED) && ['$sshl', '$sshr'].includes(cell.type))
543
+ };
544
+ dev.fillx = cell.type == '$shiftx';
545
+ break;
546
+ case '$logic_and':
547
+ case '$logic_or': {
548
+ function reduce_input(con) {
549
+ const ccon = con.slice();
550
+ con.splice(0, con.length, gen_bitname());
551
+ const extname = add_device({
552
+ type: 'OrReduce',
553
+ bits: ccon.length
554
+ });
555
+ add_net_source(con, extname, 'out');
556
+ add_net_target(ccon, extname, 'in');
557
+ }
558
+ if (cell.connections.A.length > 1)
559
+ reduce_input(cell.connections.A);
560
+ if (cell.connections.B.length > 1)
561
+ reduce_input(cell.connections.B);
562
+ zero_extend_output(cell.connections.Y);
563
+ break;
564
+ }
565
+ case '$mux':
566
+ assert(cell.connections.A.length == decode_json_number(cell.parameters.WIDTH));
567
+ assert(cell.connections.B.length == decode_json_number(cell.parameters.WIDTH));
568
+ assert(cell.connections.Y.length == decode_json_number(cell.parameters.WIDTH));
569
+ assert(cell.port_directions.A == 'input');
570
+ assert(cell.port_directions.B == 'input');
571
+ assert(cell.port_directions.Y == 'output');
572
+ dev.bits = {
573
+ in: decode_json_number(cell.parameters.WIDTH),
574
+ sel: 1
575
+ };
576
+ break;
577
+ case '$pmux':
578
+ assert(cell.connections.B.length == decode_json_number(cell.parameters.WIDTH) * decode_json_number(cell.parameters.S_WIDTH));
579
+ assert(cell.connections.A.length == decode_json_number(cell.parameters.WIDTH));
580
+ assert(cell.connections.S.length == decode_json_number(cell.parameters.S_WIDTH));
581
+ assert(cell.connections.Y.length == decode_json_number(cell.parameters.WIDTH));
582
+ assert(cell.port_directions.A == 'input');
583
+ assert(cell.port_directions.B == 'input');
584
+ assert(cell.port_directions.S == 'input');
585
+ assert(cell.port_directions.Y == 'output');
586
+ dev.bits = {
587
+ in: decode_json_number(cell.parameters.WIDTH),
588
+ sel: decode_json_number(cell.parameters.S_WIDTH)
589
+ };
590
+ break;
591
+ case '$dff':
592
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
593
+ dev.polarity = {
594
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY))
595
+ };
596
+ break;
597
+ case '$dffe':
598
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
599
+ dev.polarity = {
600
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY)),
601
+ enable: Boolean(decode_json_number(cell.parameters.EN_POLARITY))
602
+ };
603
+ break;
604
+ case '$aldff':
605
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
606
+ dev.polarity = {
607
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY)),
608
+ aload: Boolean(decode_json_number(cell.parameters.ALOAD_POLARITY))
609
+ };
610
+ break;
611
+ case '$aldffe':
612
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
613
+ dev.polarity = {
614
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY)),
615
+ enable: Boolean(decode_json_number(cell.parameters.EN_POLARITY)),
616
+ aload: Boolean(decode_json_number(cell.parameters.ALOAD_POLARITY))
617
+ };
618
+ break;
619
+ case '$adff':
620
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
621
+ dev.polarity = {
622
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY)),
623
+ arst: Boolean(decode_json_number(cell.parameters.ARST_POLARITY))
624
+ };
625
+ dev.arst_value = decode_json_constant(cell.parameters.ARST_VALUE, dev.bits);
626
+ break;
627
+ case '$sdff':
628
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
629
+ dev.polarity = {
630
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY)),
631
+ srst: Boolean(decode_json_number(cell.parameters.SRST_POLARITY))
632
+ };
633
+ dev.srst_value = decode_json_constant(cell.parameters.SRST_VALUE, dev.bits);
634
+ break;
635
+ case '$adffe':
636
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
637
+ dev.polarity = {
638
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY)),
639
+ arst: Boolean(decode_json_number(cell.parameters.ARST_POLARITY)),
640
+ enable: Boolean(decode_json_number(cell.parameters.EN_POLARITY))
641
+ };
642
+ dev.arst_value = decode_json_constant(cell.parameters.ARST_VALUE, dev.bits);
643
+ break;
644
+ case '$sdffe':
645
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
646
+ dev.polarity = {
647
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY)),
648
+ srst: Boolean(decode_json_number(cell.parameters.SRST_POLARITY)),
649
+ enable: Boolean(decode_json_number(cell.parameters.EN_POLARITY))
650
+ };
651
+ dev.srst_value = decode_json_constant(cell.parameters.SRST_VALUE, dev.bits);
652
+ break;
653
+ case '$sdffce':
654
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
655
+ dev.polarity = {
656
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY)),
657
+ srst: Boolean(decode_json_number(cell.parameters.SRST_POLARITY)),
658
+ enable: Boolean(decode_json_number(cell.parameters.EN_POLARITY))
659
+ };
660
+ dev.enable_srst = true;
661
+ dev.srst_value = decode_json_constant(cell.parameters.SRST_VALUE, dev.bits);
662
+ break;
663
+ case '$dlatch':
664
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
665
+ dev.polarity = {
666
+ enable: Boolean(decode_json_number(cell.parameters.EN_POLARITY))
667
+ };
668
+ break;
669
+ case '$adlatch':
670
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
671
+ dev.polarity = {
672
+ enable: Boolean(decode_json_number(cell.parameters.EN_POLARITY)),
673
+ arst: Boolean(decode_json_number(cell.parameters.ARST_POLARITY))
674
+ };
675
+ dev.arst_value = decode_json_constant(cell.parameters.ARST_VALUE, dev.bits);
676
+ break;
677
+ case '$dffsr':
678
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
679
+ dev.polarity = {
680
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY)),
681
+ set: Boolean(decode_json_number(cell.parameters.SET_POLARITY)),
682
+ clr: Boolean(decode_json_number(cell.parameters.CLR_POLARITY))
683
+ };
684
+ break;
685
+ case '$dffsre':
686
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
687
+ dev.polarity = {
688
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY)),
689
+ enable: Boolean(decode_json_number(cell.parameters.EN_POLARITY)),
690
+ set: Boolean(decode_json_number(cell.parameters.SET_POLARITY)),
691
+ clr: Boolean(decode_json_number(cell.parameters.CLR_POLARITY))
692
+ };
693
+ break;
694
+ case '$sr':
695
+ assert(cell.connections.Q.length == decode_json_number(cell.parameters.WIDTH));
696
+ assert(cell.port_directions.Q == 'output');
697
+ dev.no_data = true;
698
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
699
+ dev.polarity = {
700
+ set: Boolean(decode_json_number(cell.parameters.SET_POLARITY)),
701
+ clr: Boolean(decode_json_number(cell.parameters.CLR_POLARITY))
702
+ };
703
+ break;
704
+ case '$fsm': {
705
+ assert(cell.connections.ARST.length == 1);
706
+ assert(cell.connections.CLK.length == 1);
707
+ assert(cell.connections.CTRL_IN.length == decode_json_number(cell.parameters.CTRL_IN_WIDTH));
708
+ assert(cell.connections.CTRL_OUT.length == decode_json_number(cell.parameters.CTRL_OUT_WIDTH));
709
+ const TRANS_NUM = decode_json_number(cell.parameters.TRANS_NUM);
710
+ const STATE_NUM_LOG2 = decode_json_number(cell.parameters.STATE_NUM_LOG2);
711
+ const step = 2 * STATE_NUM_LOG2
712
+ + decode_json_number(cell.parameters.CTRL_IN_WIDTH)
713
+ + decode_json_number(cell.parameters.CTRL_OUT_WIDTH);
714
+ const tt = typeof (cell.parameters.TRANS_TABLE) == "number"
715
+ ? _3vl_1.Vector3vl.fromBin(bigInt(cell.parameters.TRANS_TABLE).toString(2), TRANS_NUM * step).toBin() // workaround for yosys silliness
716
+ : cell.parameters.TRANS_TABLE;
717
+ assert(tt.length == TRANS_NUM * step);
718
+ dev.polarity = {
719
+ clock: Boolean(decode_json_number(cell.parameters.CLK_POLARITY)),
720
+ arst: Boolean(decode_json_number(cell.parameters.ARST_POLARITY))
721
+ };
722
+ dev.wirename = cell.parameters.NAME;
723
+ dev.bits = {
724
+ in: decode_json_number(cell.parameters.CTRL_IN_WIDTH),
725
+ out: decode_json_number(cell.parameters.CTRL_OUT_WIDTH)
726
+ };
727
+ dev.states = decode_json_number(cell.parameters.STATE_NUM);
728
+ dev.init_state = decode_json_number(cell.parameters.STATE_RST);
729
+ dev.trans_table = [];
730
+ for (let i = 0; i < TRANS_NUM; i++) {
731
+ let base = i * step;
732
+ const f = (sz) => {
733
+ const ret = tt.slice(base, base + sz);
734
+ base += sz;
735
+ return ret;
736
+ };
737
+ const o = {
738
+ state_in: parseInt(f(STATE_NUM_LOG2), 2),
739
+ ctrl_in: f(decode_json_number(cell.parameters.CTRL_IN_WIDTH)).replace(/-/g, 'x'),
740
+ state_out: parseInt(f(STATE_NUM_LOG2), 2),
741
+ ctrl_out: f(decode_json_number(cell.parameters.CTRL_OUT_WIDTH))
742
+ };
743
+ dev.trans_table.push(o);
744
+ }
745
+ break;
746
+ }
747
+ case '$mem':
748
+ case '$mem_v2': {
749
+ const RD_PORTS = decode_json_number(cell.parameters.RD_PORTS);
750
+ const WR_PORTS = decode_json_number(cell.parameters.WR_PORTS);
751
+ assert(cell.connections.RD_EN.length == RD_PORTS);
752
+ assert(cell.connections.RD_CLK.length == RD_PORTS);
753
+ assert(cell.connections.RD_DATA.length == RD_PORTS * decode_json_number(cell.parameters.WIDTH));
754
+ assert(cell.connections.RD_ADDR.length == RD_PORTS * decode_json_number(cell.parameters.ABITS));
755
+ assert(cell.connections.WR_EN.length == WR_PORTS * decode_json_number(cell.parameters.WIDTH));
756
+ assert(cell.connections.WR_CLK.length == WR_PORTS);
757
+ assert(cell.connections.WR_DATA.length == WR_PORTS * decode_json_number(cell.parameters.WIDTH));
758
+ assert(cell.connections.WR_ADDR.length == WR_PORTS * decode_json_number(cell.parameters.ABITS));
759
+ if (cell.type == "$mem_v2") {
760
+ assert(cell.connections.RD_ARST.length == RD_PORTS);
761
+ assert(cell.connections.RD_SRST.length == RD_PORTS);
762
+ }
763
+ dev.bits = decode_json_number(cell.parameters.WIDTH);
764
+ dev.abits = decode_json_number(cell.parameters.ABITS);
765
+ dev.words = decode_json_number(cell.parameters.SIZE);
766
+ dev.offset = decode_json_number(cell.parameters.OFFSET);
767
+ dev.rdports = [];
768
+ dev.wrports = [];
769
+ const rdpol = decode_json_bigint_as_array(cell.parameters.RD_CLK_POLARITY).reverse();
770
+ const rden = decode_json_bigint_as_array(cell.parameters.RD_CLK_ENABLE).reverse();
771
+ const rdtr = cell.type == "$mem_v2"
772
+ ? []
773
+ : decode_json_bigint_as_array(cell.parameters.RD_TRANSPARENT).reverse();
774
+ const wrpol = decode_json_bigint_as_array(cell.parameters.WR_CLK_POLARITY).reverse();
775
+ const wren = decode_json_bigint_as_array(cell.parameters.WR_CLK_ENABLE).reverse();
776
+ const init = typeof (cell.parameters.INIT) == 'number'
777
+ ? bigInt(cell.parameters.INIT).toArray(2).value.map(String).reverse()
778
+ : cell.parameters.INIT.split('').reverse();
779
+ const v2_feature = (param) => cell.type == "$mem_v2" ? decode_json_bigint_as_array(param).reverse() : [];
780
+ const v2_feature_const = (param, size) => cell.type == "$mem_v2" ? decode_json_constant(param, size) : "";
781
+ const rdtrmask = v2_feature(cell.parameters.RD_TRANSPARENCY_MASK);
782
+ const rdcolmask = v2_feature(cell.parameters.RD_COLLISION_X_MASK);
783
+ const rdensrst = v2_feature(cell.parameters.RD_CE_OVER_SRST);
784
+ const rdinit = v2_feature_const(cell.parameters.RD_INIT_VALUE, dev.bits * RD_PORTS);
785
+ const rdarst = v2_feature_const(cell.parameters.RD_ARST_VALUE, dev.bits * RD_PORTS);
786
+ const rdsrst = v2_feature_const(cell.parameters.RD_SRST_VALUE, dev.bits * RD_PORTS);
787
+ if (cell.parameters.INIT) {
788
+ const l = init.slice(-1)[0] == 'x' ? 'x' : '0';
789
+ const memdata = new _3vl_1.Mem3vl(dev.bits, dev.words);
790
+ for (const k of Array(dev.words).keys()) {
791
+ const wrd = init.slice(dev.bits * k, dev.bits * (k + 1));
792
+ while (wrd.length < dev.bits)
793
+ wrd.push(l);
794
+ memdata.set(k, _3vl_1.Vector3vl.fromBin(wrd.reverse().join('')));
795
+ }
796
+ dev.memdata = memdata.toJSON();
797
+ }
798
+ for (const k of Array(RD_PORTS).keys()) {
799
+ const port = {};
800
+ if (rden[k]) {
801
+ port.clock_polarity = Boolean(rdpol[k]);
802
+ if (cell.connections.RD_EN[k] != '1')
803
+ port.enable_polarity = true;
804
+ }
805
+ ;
806
+ if (rdtr[k])
807
+ port.transparent = true;
808
+ if (cell.type == "$mem_v2") {
809
+ if (rdensrst[k])
810
+ port.enable_srst = true;
811
+ function mk_init(s, f) {
812
+ const v = s.slice(dev.bits * k, dev.bits * (k + 1));
813
+ if (!v.split('').every(c => c == 'x'))
814
+ f(v);
815
+ }
816
+ ;
817
+ mk_init(rdinit, v => port.init_value = v);
818
+ if (cell.connections.RD_ARST[k] != '0') {
819
+ port.arst_polarity = true;
820
+ mk_init(rdarst, v => port.arst_value = v);
821
+ }
822
+ if (cell.connections.RD_SRST[k] != '0') {
823
+ port.srst_polarity = true;
824
+ mk_init(rdsrst, v => port.srst_value = v);
825
+ }
826
+ function mk_mask(s, f) {
827
+ const v = Array(WR_PORTS).fill(0);
828
+ s.slice(WR_PORTS * k, WR_PORTS * (k + 1)).map((c, i) => { v[i] = c; });
829
+ if (v.every(c => c))
830
+ f(true);
831
+ else if (v.some(c => c))
832
+ f(v.map(c => Boolean(c)));
833
+ }
834
+ mk_mask(rdtrmask, v => port.transparent = v);
835
+ mk_mask(rdcolmask, v => port.collision = v);
836
+ }
837
+ dev.rdports.push(port);
838
+ }
839
+ for (const k of Array(WR_PORTS).keys()) {
840
+ const port = {};
841
+ if (wren[k]) {
842
+ port.clock_polarity = Boolean(wrpol[k]);
843
+ const wr_en_connections = cell.connections.WR_EN.slice(dev.bits * k, dev.bits * (k + 1));
844
+ if (wr_en_connections.some(z => z != '1')) {
845
+ port.enable_polarity = true;
846
+ if (wr_en_connections.every(z => z == wr_en_connections[0]))
847
+ port.no_bit_enable = true;
848
+ }
849
+ }
850
+ ;
851
+ dev.wrports.push(port);
852
+ }
853
+ break;
854
+ }
855
+ case '$lut':
856
+ assert(cell.connections.A.length == decode_json_number(cell.parameters.WIDTH));
857
+ assert(cell.connections.Y.length == 1);
858
+ assert(cell.port_directions.A == 'input');
859
+ assert(cell.port_directions.Y == 'output');
860
+ dev.abits = cell.connections.A.length;
861
+ dev.bits = cell.connections.Y.length;
862
+ dev.rdports = [{}];
863
+ dev.wrports = [];
864
+ dev.memdata = cell.parameters.LUT.split('').reverse();
865
+ assert(dev.memdata.length == Math.pow(2, dev.abits));
866
+ // Rewrite cell connections to be $mem compatible for port mapping
867
+ cell.connections.RD_ADDR = cell.connections.A;
868
+ cell.connections.RD_DATA = cell.connections.Y;
869
+ delete cell.connections.A;
870
+ delete cell.connections.Y;
871
+ break;
872
+ default:
873
+ }
874
+ if (dev.type == 'Dff') {
875
+ // find register initial value, if exists
876
+ // Yosys puts initial values in net attributes; there can be many for single actual net!
877
+ const nms = netnames.get(cell.connections.Q);
878
+ if (nms !== undefined) {
879
+ for (const nm of nms) {
880
+ if (mod.netnames[nm].attributes.init !== undefined)
881
+ dev.initial = decode_json_constant(mod.netnames[nm].attributes.init, dev.bits);
882
+ }
883
+ }
884
+ }
885
+ const portmap = portmaps[cell.type];
886
+ if (portmap)
887
+ connect_device(dname, cell, portmap);
888
+ else if (cell.type == '$pmux')
889
+ connect_pmux(dname, cell);
890
+ else if (cell.type == '$mem')
891
+ connect_mem(dname, cell, dev);
892
+ else if (cell.type == '$mem_v2')
893
+ connect_mem(dname, cell, dev);
894
+ else if (cell.type == '$lut')
895
+ connect_mem(dname, cell, dev);
896
+ else
897
+ throw Error('Invalid cell type: ' + cell.type);
898
+ }
899
+ // Group bits into nets for complex sources
900
+ for (const [nbits, net] of nets.entries()) {
901
+ if (net.source !== undefined)
902
+ continue;
903
+ const groups = [[]];
904
+ let pbitinfo = undefined;
905
+ for (const bit of nbits) {
906
+ let bitinfo = bits.get(bit);
907
+ if (bitinfo == undefined && constbit(bit))
908
+ bitinfo = 'const';
909
+ if (groups.slice(-1)[0].length > 0 &&
910
+ (typeof bitinfo != typeof pbitinfo ||
911
+ typeof bitinfo == 'object' &&
912
+ typeof pbitinfo == 'object' &&
913
+ (bitinfo.id != pbitinfo.id ||
914
+ bitinfo.port != pbitinfo.port ||
915
+ bitinfo.num != pbitinfo.num + 1))) {
916
+ groups.push([]);
917
+ }
918
+ groups.slice(-1)[0].push(bit);
919
+ pbitinfo = bitinfo;
920
+ }
921
+ if (groups.length == 1)
922
+ continue;
923
+ if (groups.slice(-1)[0].every(x => x == '0')) {
924
+ // infer zero-extend
925
+ const ilen = nbits.length - groups.slice(-1)[0].length;
926
+ const dname = add_device({
927
+ type: 'ZeroExtend',
928
+ extend: { output: nbits.length, input: ilen }
929
+ });
930
+ const zbits = nbits.slice(0, ilen);
931
+ add_net_source(nbits, dname, 'out');
932
+ add_net_target(zbits, dname, 'in');
933
+ if (groups.length > 2)
934
+ add_busgroup(zbits, groups.slice(0, groups.length - 1));
935
+ }
936
+ else
937
+ add_busgroup(nbits, groups);
938
+ }
939
+ // Add constants
940
+ for (const [nbits, net] of nets.entries()) {
941
+ if (net.source !== undefined)
942
+ continue;
943
+ if (!nbits.every(constbit))
944
+ continue;
945
+ const dname = add_device({
946
+ // label: String(val), // TODO
947
+ type: 'Constant',
948
+ constant: nbits.slice().reverse().join('')
949
+ });
950
+ add_net_source(nbits, dname, 'out');
951
+ }
952
+ // Select bits from complex targets
953
+ for (const [nbits, net] of nets.entries()) {
954
+ if (net.source !== undefined)
955
+ continue;
956
+ // constants should be already handled!
957
+ assert(nbits.every(x => constbit(x) || (typeof x == 'number' && x > 1)));
958
+ const bitinfos = nbits.map(x => bits.get(x));
959
+ if (!bitinfos.every(x => typeof x == 'object'))
960
+ continue; // ignore not fully driven ports
961
+ // complex sources should be already handled!
962
+ assert(bitinfos.every(info => info.id == bitinfos[0].id &&
963
+ info.port == bitinfos[0].port));
964
+ const cconn = devnets.get(bitinfos[0].id).get(bitinfos[0].port);
965
+ const dname = add_device({
966
+ type: 'BusSlice',
967
+ slice: {
968
+ first: bitinfos[0].num,
969
+ count: bitinfos.length,
970
+ total: cconn.length
971
+ }
972
+ });
973
+ add_net_source(nbits, dname, 'out');
974
+ add_net_target(cconn, dname, 'in');
975
+ }
976
+ // Generate connections between devices
977
+ for (const [nbits, net] of nets.entries()) {
978
+ if (net.source === undefined) {
979
+ console.warn('Undriven net in ' + name + ': ' + nbits);
980
+ continue;
981
+ }
982
+ let first = true;
983
+ for (const target in net.targets) {
984
+ const conn = {
985
+ to: net.targets[target],
986
+ from: net.source
987
+ };
988
+ if (net.name)
989
+ conn.name = net.name;
990
+ if (net.source_positions)
991
+ conn.source_positions = net.source_positions;
992
+ if (!first && mout.devices[conn.from.id].type == "Constant") {
993
+ // replicate constants for better clarity
994
+ const dname = add_device({
995
+ type: 'Constant',
996
+ constant: mout.devices[conn.from.id].constant
997
+ });
998
+ conn.from = { id: dname, port: 'out' };
999
+ }
1000
+ mout.connectors.push(conn);
1001
+ first = false;
1002
+ }
1003
+ }
1004
+ return mout;
1005
+ }
1006
+ function yosys2digitaljs(obj, options = {}) {
1007
+ const portmaps = order_ports(obj);
1008
+ const out = yosys_to_digitaljs(obj, portmaps, options);
1009
+ const toporder = topsort(module_deps(obj));
1010
+ toporder.pop();
1011
+ const toplevel = toporder.pop();
1012
+ const output = Object.assign({ subcircuits: {} }, out[toplevel]);
1013
+ for (const x of toporder)
1014
+ output.subcircuits[x] = out[x];
1015
+ return output;
1016
+ }
1017
+ function io_ui(output) {
1018
+ for (const [name, dev] of Object.entries(output.devices)) {
1019
+ if (dev.type == 'Input' || dev.type == 'Output') {
1020
+ dev.label = dev.net;
1021
+ }
1022
+ // use clock for clocky named inputs
1023
+ if (dev.type == 'Input' && dev.bits == 1 && (dev.label == 'clk' || dev.label == 'clock')) {
1024
+ dev.type = 'Clock';
1025
+ dev.propagation = 100;
1026
+ }
1027
+ if (dev.type == 'Input')
1028
+ dev.type = dev.bits == 1 ? 'Button' : 'NumEntry';
1029
+ if (dev.type == 'Output') {
1030
+ if (dev.bits == 1)
1031
+ dev.type = 'Lamp';
1032
+ else if (dev.bits == 8 && (dev.label == 'display7' || dev.label.startsWith('display7_')))
1033
+ dev.type = 'Display7';
1034
+ else
1035
+ dev.type = 'NumDisplay';
1036
+ }
1037
+ }
1038
+ }
1039
+ function ansi_c_escape_contents(cmd) {
1040
+ function func(ch) {
1041
+ if (ch == '\t')
1042
+ return '\\t';
1043
+ if (ch == '\r')
1044
+ return '\\r';
1045
+ if (ch == '\n')
1046
+ return '\\n';
1047
+ return '\\x' + ch.charCodeAt(0).toString(16).padStart(2, '0');
1048
+ }
1049
+ return cmd.replace(/(["'\\])/g, '\\$1')
1050
+ .replace(/[\x00-\x1F\x7F-\x9F]/g, func);
1051
+ }
1052
+ function ansi_c_escape(cmd) {
1053
+ return '"' + ansi_c_escape_contents(cmd) + '"';
1054
+ }
1055
+ function shell_escape_contents(cmd) {
1056
+ return cmd.replace(/(["\r\n$`\\])/g, '\\$1');
1057
+ }
1058
+ function shell_escape(cmd) {
1059
+ return '"' + shell_escape_contents(cmd) + '"';
1060
+ }
1061
+ function process_filename(filename) {
1062
+ const flags = /\.sv$/.test(filename) ? "-sv" : "";
1063
+ return `read_verilog ${flags} ${ansi_c_escape(filename)}`;
1064
+ }
1065
+ function prepare_yosys_script(filenames, options) {
1066
+ const optimize_simp = options.optimize ? "opt" : "opt_clean";
1067
+ const optimize = options.optimize ? "opt -full" : "opt_clean";
1068
+ const fsmexpand = options.fsmexpand ? " -expand" : "";
1069
+ const fsmpass = options.fsm == "nomap"
1070
+ ? "fsm -nomap" + fsmexpand
1071
+ : options.fsm
1072
+ ? "fsm" + fsmexpand
1073
+ : "";
1074
+ const readVerilogFilesScript = filenames
1075
+ .map((filename) => process_filename(filename));
1076
+ const yosysScript = [...readVerilogFilesScript, 'hierarchy -auto-top', 'proc', optimize_simp, fsmpass, 'memory -nomap', 'wreduce -memx', optimize];
1077
+ return yosysScript.join('; ');
1078
+ }
1079
+ function prepare_verilator_args(filenames) {
1080
+ const processed_filenames = filenames.map(shell_escape);
1081
+ return ['-lint-only', '-Wall', '-Wno-DECLFILENAME', '-Wno-UNOPT', '-Wno-UNOPTFLAT', ...processed_filenames];
1082
+ }