Editor for the icestorm asc files, for ice40 FPGA

root / iceMod.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<!-- IceMod a program that edits asc streams from iceStorm project.
    Copyright (C) 2019 Eduard Timotei BUDULEA

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
-->
<html>
<header>
<title>Modify ice bit stream.</title>
<style>
form a { 
    appearance: button; -moz-appearance: button; -webkit-appearance: button;
    text-decoration: none; font: menu; color:ButtonText;
    display: inline-block; padding: 2px 8px;
}
</style>
<script>

var dbFile = null, ascFile = null;

var device = null;

var instance = null;

function createChipTable() {
    var tbl = document.getElementById("chipTable");
    tbl.innerHTML = "";
    var tiles = device.tiles;
    for (row = 0; row < tiles.length; ++row) {
        var el = document.createElement("tr");
        for (col = 0; col < tiles[row].length; ++col) {
            var cel = document.createElement("td");
            if (tiles[row][col]) {
                var tileType = tiles[row][col].type;
                cel.innerHTML = "<span onclick='populate".concat(tileType, "Tile(", col, ",", row, ")'><pre>", tileType, "\n(", col, " ", row, ")</pre></span>");
                switch (tileType) {
                case "io": cel.setAttribute("bgcolor", "#aee"); break;
                case "logic": cel.setAttribute("bgcolor", "#eae"); break;
                case "ramt":
                case "ramb": cel.setAttribute("bgcolor", "#eea");
                }
                cel.style.cursor="pointer";
            }else cel.innerHTML = "&nbsp;";
            el.appendChild(cel);
        }
        el.setAttribute("align", "center");
        el.setAttribute("valign", "center");
        tbl.appendChild(el);
    }
}


function recordNotRouting(tile, toks) {
    if (toks.length > 2) tile[toks[0]] = toks.slice(1);
    else tile[toks[0]] = toks[1];
}

function recordNet(netId, netCell, toks) {
    var tilename = "x" + toks[0] + "y" + toks[1];
    netCell[tilename] = toks[2];
    device.tiles[parseInt(toks[1], 10)][parseInt(toks[0], 10)].nets[toks[2]] = netId;
}

function recordBR(tile, tilename, bits, toks) {
    var bitVal = [];
    for (i = 0; i < bits.length; ++i)
        if (toks[0].charAt(i) == "1") bitVal.push(bits[i]);
    tile[bitVal.join(" ")] = device.nets[parseInt(toks[1], 10)][tilename];
}

function recordNothing() { }

function recordTile(tile, txt) {
    tile.push(txt);
}

function compile() {
    if (!dbFile || !ascFile) return;
    var lns = dbFile.split("\n");
    var state = recordNothing;
    device = {};
    for (ln of lns) {
        if (ln.trim().startsWith("#")) continue;
        if (ln.trim().length == 0) continue;
        var toks = ln.split(" ");
        switch(toks[0]) {
        case ".device":
            device.name = toks[1];
            device.width = parseInt(toks[2], 10);
            device.height = parseInt(toks[3], 10);
            device.nets = new Array(parseInt(toks[4], 10));
            device.tiles = new Array(device.height);
            for (i = 0; i < device.tiles.length; ++i) device.tiles[i] = new Array();
            device.notRouting = {};
            break;
        case ".io_tile":
        case ".logic_tile":
        case ".ramb_tile":
        case ".ramt_tile":
            var tileType = toks[0].slice(1, toks[0].length - 5);
            device.tiles[parseInt(toks[2], 10)][parseInt(toks[1], 10)] = {"type":tileType, "buffers":{}/*, "routings":{}*/, "nets":{}};
            break;
        case ".io_tile_bits":
        case ".logic_tile_bits":
        case ".ramb_tile_bits":
        case ".ramt_tile_bits":
            var tileType = toks[0].slice(1, toks[0].length - 10);
            device.notRouting[tileType] = {};
            var notRTile = device.notRouting[tileType];
            notRTile.columns = parseInt(toks[1], 10);
            notRTile.rows = parseInt(toks[2], 10);
            state = function (txtLine) {recordNotRouting(notRTile, txtLine);};
            break;
        case ".net":
            var netId = parseInt(toks[1], 10);
            device.nets[netId] = {};
            state = function (txtLine) {recordNet(netId, device.nets[netId], txtLine);};
            break;
        case ".buffer":
        case ".routing":
            //var brType = "buffers"; //toks[0].slice(1) + "s";
            var tile = device.tiles[parseInt(toks[2], 10)][parseInt(toks[1], 10)];
            var tilename = "x" + toks[1] + "y" + toks[2];
            var netname = device.nets[parseInt(toks[3], 10)][tilename];
            if (! tile.buffers[netname]) tile.buffers[netname] = {};
            var tileNet = tile.buffers[netname];
            var bits = toks.slice(4);
            state = function (txtLine) {recordBR(tileNet, tilename, bits, txtLine);};
        break;
        default:
            if (toks[0].startsWith(".")) state = recordNothing;
            else state(toks);
        }
    }
    dbFile = null;
    lns = ascFile.split("\n");
    instance = {"comments":[], "tiles":[], "syms":{}};
    state = recordNothing;
    for (ln of lns) {
        var toks = ln.split(" ");
        switch(toks[0]){
        case ".comment":
            instance.comments.push(ln.slice(9));
            state = recordNothing;
            break;
        case ".device":
            instance.device = ln;
            state = recordNothing;
            if (toks[1] != device.name) alert ("Unexpected " + toks[1] + " instance for device " + device.name + "!");
            break;
        case ".io_tile":
        case ".logic_tile":
        case ".ramb_tile":
        case ".ramt_tile":
            var iotile = {"type":toks[0], "row": parseInt(toks[2], 10), "col": parseInt(toks[1], 10), "value":[]};
            state = function(txtLine) {recordTile(iotile.value, txtLine);};
            instance.tiles.push(iotile);
            break;
        case ".sym":
            instance.syms[toks[1]] = toks.slice(2).join(" ");
            state = recordNothing;
            break;
        default:
            state(ln);
        }
    }
    ascFile = null;
    createChipTable();
}

function loadFile() {
    var reader = new FileReader();
    reader.onload = function (myFileTxt) {dbFile = myFileTxt.target.result; compile();}
    reader.readAsText(document.fileManager.db.files[0]);
    reader = new FileReader();
    reader.onload = function (myFileTxt) {ascFile = myFileTxt.target.result; compile();}
    reader.readAsText(document.fileManager.asc.files[0]);
}

function serializeInstance() {
    var outText = [];
    for (com of instance.comments) outText.push(".comment " + com);
    outText.push(instance.device);
    for (tile of instance.tiles) {
        outText.push(tile.type + " " + tile.col + " " + tile.row);
        for (ln of tile.value) outText.push(ln);
    }
    for (sym in instance.syms)
        outText.push(".sym " + sym + " " + instance.syms[sym]);
    return outText.join("\n");
}

function saveFile() {
    var aEl = document.getElementById("saveLink");
    var fname = document.fileManager.asc.value;
    fname = fname.slice(fname.lastIndexOf("\\") + 1);
    fname = fname.slice(fname.lastIndexOf("/") + 1);
    aEl.setAttribute("download", fname);
    var textul = encodeURIComponent(serializeInstance())
    aEl.setAttribute("href", "data:text/plain;charset=utf-8," + textul);
    return true;
}

function populateRaw(col, row) {
    var found = false;
    var tileType = device.tiles[row][col].type;
    for (tile of instance.tiles) if (tile.row == row && tile.col == col) {
        found = true;
        document.rawData.textIn.value = tile.value.join("\n");
    }
    if (!found) {
        var insttile = {"type":tileType, "row":row, "col":col, "value":new Array(device.notRouting[tileType].rows)};
        insttile.value.fill("0".repeat(device.notRouting[tileType].cols));
        instance.tiles.push(insttile);
        document.rawData.textIn.value = insttile.value.join("\n");
    }
    document.rawData.row.value = row;
    document.rawData.col.value = col;
    document.rawData.tileType.value = tileType;
}

function getTileData(col, row) {
    for (tile of instance.tiles) if (tile.row == row && tile.col == col) {
        return tile;
    }
    return undefined;
}

function idx2txt(row, col) { return "B".concat(row, "[", col, "]"); }

function decode(origText) {
    var inText = origText.split("\n");
    var barr = {};
    for (i = 0; i < inText.length; ++i) for (j = 0; j < inText[i].length; ++j)
        if ("1" == inText[i].charAt(j)) barr[idx2txt(i, j)] = true;
    return barr;
}

function encode(barr, w, h) {
    var outText = [];
    for (i = 0; i < h; ++i) {
        var ln = [];
        for (j = 0; j < w; ++j)
            if (barr[idx2txt(i, j)]) ln.push("1"); else ln.push("0");
        outText.push(ln.join(""));
    }
    return outText.join("\n");
}

function encodeSW() {
    var outTexts = {};
    for (elem of document.switches.elements) {
        if (elem.tagName == "INPUT") {
            if (elem.checked) for (val of elem.value.split(" "))
                outTexts[val] = true;
        } else if (elem.value) for (bitVal of elem.value.split(" "))
            outTexts[bitVal] = true;
    }
    var tileType = device.notRouting[device.tiles[document.rawData.row.value][document.rawData.col.value].type];
    document.rawData.textOut.value = encode(outTexts, tileType.columns, tileType.rows);
}

function getBufferValue(bnm, tileBP, bits) {
    var curValue = ""
    for (optn in tileBP.buffers[bnm]) if (optn.length > curValue.length) {
        var matching = true;
        for (val of optn.split(" ")) if (bits[val]); else matching = false;
        if (matching) curValue = optn;
    }
    return curValue;
}

function mkBuffInput(bnm, tileBP, bits) {
    var p = document.createElement("tr");
    var t = document.createElement("td");
    t.innerHTML = bnm;
    p.appendChild(t);
    t = document.createElement("td");
    var iel = document.createElement("input");
    iel.setAttribute("name", bnm + "_alias");
    var alias = instance.syms[tileBP.nets[bnm]];
    if (alias) iel.setAttribute("value", alias);
    iel.setAttribute("type", "text");
    t.appendChild(iel);
    p.appendChild(t);
    t = document.createElement("td");
    iel = document.createElement("select");
    iel.setAttribute("name", bnm);
    var opt = document.createElement("option");
    opt.innerHTML = "none";
    opt.setAttribute("value", "");
    iel.appendChild(opt);
    for (optn in tileBP.buffers[bnm]) {
        opt = document.createElement("option");
        opt.setAttribute("value", optn);
        var srcn = tileBP.buffers[bnm][optn];
        var srcalias = instance.syms[tileBP.nets[srcn]]
        if (srcalias) opt.innerHTML = srcn + "(" + srcalias + ")";
        else opt.innerHTML = srcn;
        iel.appendChild(opt);
    }
    iel.value = getBufferValue(bnm, tileBP, bits);
    t.appendChild(iel);
    p.appendChild(t);
    return p;
}

function addBuffer(event) {
    var el = event.target.value;
    if (0 == el.length) return;
    for (cld of event.target.children)
        if (cld.value == el) event.target.removeChild(cld);
    var tileBP = device.tiles[document.rawData.row.value][document.rawData.col.value];
    var bits = decode(document.rawData.textIn.value);
    document.getElementById("buffersSW").appendChild(mkBuffInput(el, tileBP, bits));
}

function populateSwithces(col, row) {
    var swEl = document.switches;
    var tileData = getTileData(col, row);
    swEl.innerHTML = '<p>LUTS</p><table id="LUTS" border="1">'
        + '<tr align="center"><td rowspan="2" >LUT<br />Name</td>'
        + '<td colspan="16">Output for input value.</td>'
        + '<td rowspan="2">Carry<br />enable</td>'
        + '<td rowspan="2">D FlipFlop<br /> enable</td>'
        + '<td rowspan="2">Set/~Reset</td>'
        + '<td rowspan="2">Async on<br />Set/~Reset</td></tr>'
        + '<tr><td>0000</td><td>0001</td><td>0010</td><td>0011</td>'
        + '<td>0100</td><td>0101</td><td>0110</td><td>0111</td>'
        + '<td>1000</td><td>1001</td><td>1010</td><td>1011</td>'
        + '<td>1100</td><td>1101</td><td>1110</td><td>1111</td></tr>'
        + '</table>'
        + '<p>NotRouting</p><table id="notRoutingSW" border="1"></table>'
        + '<p>Buffers</p><table id="buffersSW" border="1"></table>';
    var nrdev = device.notRouting[document.rawData.tileType.value];
    var nrtlb = document.getElementById("notRoutingSW");
    var lutlb = document.getElementById("LUTS");
    var bits = decode(document.rawData.textIn.value);
    var lkss = [];
    for (lks in nrdev) lkss.push(lks);
    lkss.sort();
    for (lks of lkss) {
        if (lks.startsWith("LC_")) {
            var p = document.createElement("tr");
            var t = document.createElement("td");
            t.innerHTML = lks;
            p.appendChild(t);
            function addInput(i) {
                var t = document.createElement("td");
                var iel = document.createElement("input");
                iel.setAttribute("type", "checkbox");
                iel.setAttribute("name", lks + "_" + i);
                iel.setAttribute("value", nrdev[lks][i]);
                if (bits[nrdev[lks][i]]) iel.setAttribute("checked", "checked");
                t.appendChild(iel);
                p.appendChild(t);
            }
            addInput(4); addInput(14); addInput(15); addInput(5);
            addInput(6); addInput(16); addInput(17); addInput(7);
            addInput(3); addInput(13); addInput(12); addInput(2);
            addInput(1); addInput(11); addInput(10); addInput(0);
            addInput(8); addInput(9); addInput(18); addInput(19);
            lutlb.appendChild(p);
        } else if (Array.isArray(nrdev[lks])) {
            var p = document.createElement("tr");
            var t = document.createElement("td");
            t.innerHTML = lks;
            p.appendChild(t);
            t = document.createElement("td");
            for (i = 0; i < nrdev[lks].length; ++i) {
                var iel = document.createElement("input");
                iel.setAttribute("type", "checkbox");
                if (i < 10)
                    iel.setAttribute("name", lks + "_" + String.fromCharCode(i + 48));
                else
                    iel.setAttribute("name", lks + "_" + String.fromCharCode(i + 55));
                iel.setAttribute("value", nrdev[lks][i]);
                if (bits[nrdev[lks][i]]) iel.setAttribute("checked", "checked");
                t.appendChild(iel);
            }
            p.appendChild(t);
            nrtlb.appendChild(p);
        } else if (isNaN(nrdev[lks])) {
            var p = document.createElement("tr");
            p.innerHTML = "<td>" + lks + '</td><td><input type="checkbox" name="' + lks + '" value="' + nrdev[lks] + '" ' +
                (bits[nrdev[lks]] ? 'checked="checked" ' : "") + '/></td>';
            nrtlb.appendChild(p);
        }
    }
    var bubtlb = document.getElementById("buffersSW");
    var tileBP = device.tiles[row][col];
    bubtlb.innerHTML = "<tr><td>Name</td><td>Alias</td><td>Selected source</td></tr>";
    var bnms = []
    for (bnm in tileBP.buffers) bnms.push(bnm);
    bnms.sort();
    var emptySelect = document.createElement("select");
    emptySelect.setAttribute("name", "emptySelects");
    emptySelect.innerHTML = "<option>---</option>";
    emptySelect.addEventListener("change", addBuffer);
    for (bnm of bnms) {
        if (getBufferValue(bnm, tileBP, bits))
            bubtlb.appendChild(mkBuffInput(bnm, tileBP, bits));
        else {
            var opt = document.createElement("option");
            opt.setAttribute("value", bnm);
            var alias = instance.syms[tileBP.nets[bnm]];
            opt.innerHTML = alias ? bnm + "(" + alias + ")" : bnm;
            emptySelect.appendChild(opt);
        }
    }
    document.newBuffers.innerHTML = "";
    document.newBuffers.appendChild(emptySelect);
}

function populateioTile(col, row) {
    populateRaw(col, row);
    populateSwithces(col, row);
}

function populatelogicTile(col, row) {
    populateRaw(col, row);
    populateSwithces(col, row);
}

function populateramtTile(col, row) {
    populateRaw(col, row);
    populateSwithces(col, row);
}

function populaterambTile(col, row) {
    populateRaw(col, row);
    populateSwithces(col, row);
}

function saveTile() {
    var row = parseInt(document.rawData.row.value, 10);
    var col = parseInt(document.rawData.col.value, 10);
    for (elem of document.switches.elements) {
        var netName = elem.name;
        if (!netName.endsWith("_alias")) continue;
        var netId = device.tiles[row][col].nets[netName.slice(0, netName.length - 6)];
        if (elem.value == "") delete instance.syms[netId];
        else instance.syms[netId] = elem.value;
    }
    for (tile of instance.tiles) if (tile.row == row && tile.col == col) {
        tile.value = document.rawData.textOut.value.split("\n");
    }
}

</script>
</header>
<body>
<form name="fileManager">
<p>Please select an db file:<input type="file" name="db" /></p>
<p>Please select an asc file:<input type="file" name="asc" /></p>
<p><a href="#" onclick="loadFile();return false;">Load asc</a>
<a href="#" id="saveLink" onclick="saveFile();return true;">Save asc</a></p>
</form>
<table id="chipTable" border="1" style="font-size:100%">
</table>
<form name="rawData">
<p>Tile at column <input type="text" name="col" size="3" readonly="readonly" /> and row
<input type="text" name="row" size="3" readonly="readonly" /> is a
<input type="text" name="tileType" size="8" readonly="readonly" /> tile.</p>
<textarea cols="60" rows="20" name="textIn"></textarea>
<textarea cols="60" rows="20" name="textOut"></textarea>
<p><a href="#" onclick="encodeSW();return false;">Encode switches</a>
<a href="#" onclick="saveTile();return false;">Save tile</a></p>
</form>
<form name="switches">
</form>
<form name="newBuffers">
</form>
</body>
</html>