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