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