forked from ZigEmbeddedGroup/microzig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtablegen.js
More file actions
112 lines (94 loc) · 3.54 KB
/
tablegen.js
File metadata and controls
112 lines (94 loc) · 3.54 KB
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
// ==UserScript==
// @name Register Table Ziggifier
// @namespace http://tampermonkey.net/
// @version 2025-07-28
// @description This userscript tries to catch register definition tables on the arm documentation and convert them into Zig code
// @author xq
// @match https://developer.arm.com/documentation/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=arm.com
// @grant none
// ==/UserScript==
/**
*
* Try on:
* - https://developer.arm.com/documentation/100235/0100/The-Cortex-M33-Peripherals/System-Control-Block/MemManage-Fault-Address-Register?lang=en
* - https://developer.arm.com/documentation/dui0662/b/Cortex-M0--Peripherals/System-Control-Block/System-Control-Register?lang=en
*
*/
(function() {
'use strict';
console.log("startup...");
let pattern = /\[(\d+)(?::(\d+))?\]/;
function process()
{
for(const table of document.querySelectorAll("table.c-table"))
{
console.log(table);
let fields = [];
for(const row of table.rows)
{
if(row.cells.length != 3) {
continue;
}
let cells = Array.from(row.cells).map(x => x.innerText);
const match = pattern.exec(cells[0]);
if(match == null) {
console.log("skip", cells);
continue;
}
let msb = Number(match[1]);
let lsb = Number(match[2] || match[1]);
fields.push({
lsb: lsb,
msb: msb,
name: cells[1],
func: cells[2],
});
}
fields.sort((a,b) => a.lsb - b.lsb);
var str = `packed struct(u${fields[fields.length-1].msb+1}) {\n`;
for(const fld of fields) {
let suffix = "";
if(fld.name == "-") {
fld.name = `_reserved${fld.lsb}`;
suffix = " = 0";
}
let size = `${fld.msb}:${fld.lsb}`;
if(fld.msb == fld.lsb) {
size = `${fld.msb}`;
}
str += "\n";
let docs = fld.func.replaceAll("\n\n", "\n").split("\n");
if(docs.length == 1) {
docs[0] = `[${size}] ` + docs[0];
} else {
docs = [`[${size}]`].concat(docs);
}
for(const line of docs)
{
str += ` /// ${line}\n`;
}
str += ` ${fld.name}: u${fld.msb - fld.lsb + 1}${suffix},\n`;
}
str += "}";
console.log(fields);
console.log(str);
const codeblock = document.createElement("textarea");
codeblock.value = str;
codeblock.style.width = "100%";
codeblock.style.fontFamily = "monospace";
codeblock.style.fontSize = "smaller";
codeblock.style.height = "20em";
codeblock.style.resize = "vertical";
const cell = document.createElement("TD");
cell.setAttribute("colspan", 3);
cell.appendChild(codeblock);
const appendix = document.createElement("TR");
appendix.appendChild(cell);
const tbody = document.createElement("TBODY");
tbody.appendChild(cell);
table.appendChild(tbody);
}
}
setTimeout( process, 1000);
})();