-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml2svdgen.js
188 lines (162 loc) · 5.72 KB
/
yaml2svdgen.js
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
import YAML from 'yaml';
import fs from 'fs/promises';
import {XMLBuilder, XMLParser} from 'fast-xml-parser';
import path from 'path';
import { fileExists } from './utils.js';
import assert from 'assert';
const numToHex = (num, pad = 8) => '0x' + num.toString(16).toUpperCase().padStart(pad, 0);
export default class Yaml2SvdGenerator {
constructor (chipFolder) {
this._chipFolder = chipFolder;
this._svd = null;
this._chipInfo = null;
this._xmlbuilder = new XMLBuilder({
processEntities: true,
format: true,
ignoreAttributes: false,
// commentPropName: "phone"
});
}
async _genDeviceBlock() {
const chip = this._chipInfo;
const device = {
"@_schemaVersion": "1.1",
"@_xmlns:xs": "http://www.w3.org/2001/XMLSchema-instance",
"@_xs:noNamespaceSchemaLocation": "CMSIS-SVD.xsd",
vendor: 'Bouffalo Lab',
vendorid: 'bouffalolab',
name: chip.name,
version: '0.1', // TODO:
description: 'To be added', // TODO: This is required
// TODO: License text!!
// TODO: CPU, not required, but might be good
addressUnitBits: 8,
width: 32,
size: 32,
access: 'read-write',
resetValue: '0x00000000',
resetMask: '0xFFFFFFFF',
peripherals: {
peripheral: []
}
};
this._svd.device = device;
this._derivedFromTable = {};
this._periInfoCache = {};
}
async _genRegistersBlock(chipPeri, periBlock) {
let periInfo = this._periInfoCache[chipPeri.peripheral];
if (periInfo === undefined) {
const regFileNames = [ `${chipPeri.peripheral}.yaml`, `${chipPeri.peripheral}_reg.yaml` ];
periInfo = null;
this._periInfoCache[chipPeri.peripheral] = null;
for (const regFileName of regFileNames) {
const periInfoPath = path.join(this._chipFolder, 'peripherals', regFileName);
if (await fileExists(periInfoPath)) {
periInfo = YAML.parse(await fs.readFile(periInfoPath, 'utf8'));
this._periInfoCache[chipPeri.peripheral] = periInfo;
break;
}
}
}
if (periInfo === null) return null;
const registers = [];
for (const register of periInfo.registers) {
const reg = {
name: register.name, // TODO: uppercase???
description: register.description !== '' ? register.description : undefined,
addressOffset: numToHex(register.offset_bytes, 2),
size: register.size_bytes * 8,
};
if (register.fieldset !== undefined) {
const fields = [];
const fieldset = periInfo.fieldsets.find(f => f.name === register.fieldset);
assert(fieldset != undefined);
const regFields = fieldset.fields;
for (const regField of regFields) {
let access = undefined;
switch (regField.access) {
case 'rsvd': access = 'read-only'; break;
case 'r': access = 'read-only'; break;
case 'w': access = 'write-only'; break;
case 'r/w': access = 'read-write'; break;
case 'rw': access = 'read-write'; break;
case 'w1c': case 'w1p': case 'otp': /* TODO: */ break;
default: {
if (regField.access != undefined) {
console.log(`Unknown access ${regField.access}`);
}
}
}
const field = {
name: regField.name, // TODO: uppercase???
description: regField.description !== '' ? regField.description : undefined,
bitOffset: regField.offset_bits,
bitWidth: regField.size_bits,
access: access, // TODO: better access with modifiedWriteValues and readAction
// TODO: enumeratedValues
};
fields.push(field);
}
reg.fields = {
field: fields
};
}
// TODO: Implement derived from???
registers.push(reg);
}
return registers;
}
async _genPeripheralBlock(chipPeri) {
const peripherals = this._svd.device.peripherals.peripheral;
const name = chipPeri.name.toUpperCase();
const peripheral = chipPeri.peripheral?.toUpperCase();
// TODO: There can be multiple address blocks.
let addressBlock = chipPeri.size != undefined ? {
offset: 0, // TODO: Figure out how this works
size: numToHex(chipPeri.size), // TODO: We can get real real size from reg yaml file
usage: 'registers' // TODO: Use this appropriately
} : undefined;
const block = {
name: name,
description: chipPeri.description !== '' ? chipPeri.description : undefined,
groupName: peripheral,
baseAddress: numToHex(chipPeri.address),
addressBlock: addressBlock,
registers: undefined,
};
const derivedFrom = this._derivedFromTable[peripheral];
if (peripheral != undefined) {
if (derivedFrom === undefined) {
this._derivedFromTable[peripheral] = block.name;
} else {
block["@_derivedFrom"] = derivedFrom;
block.addressBlock = undefined;
}
}
const registerBlocks = await this._genRegistersBlock(chipPeri, block);
if (registerBlocks !== null) {
block.registers = {
register: registerBlocks
};
}
peripherals.push(block);
}
async generateFor(chipName, svdPath) {
this._svd = {
"?xml": {
"@_version": "1.0",
"@_encoding": "utf-8"
}
};
this._chipInfo = YAML.parse(await fs.readFile(path.join(this._chipFolder, 'chips', `${chipName}.yaml`), 'utf8'));
await this._genDeviceBlock();
for (const peripheral of this._chipInfo.peripherals) {
await this._genPeripheralBlock(peripheral);
}
const svdContent = this._xmlbuilder.build(this._svd);
await fs.writeFile(svdPath, svdContent);
// cleanup
this._svd = null;
}
}