-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathral.js
More file actions
447 lines (393 loc) · 14.2 KB
/
ral.js
File metadata and controls
447 lines (393 loc) · 14.2 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
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
// This file is part of PeakRDL-html <https://github.com/SystemRDL/PeakRDL-html>.
// and can be redistributed under the terms of GNU LGPLv3 <https://www.gnu.org/licenses/>.
class RAL {
static #ral_data_files = new Array(N_RAL_FILES).fill(null);
static async load_ral_data(){
this.#init_progressbar();
// Dispatch all JSON fetch requests in parallel
var fetches = [];
for(var i=0; i<N_RAL_FILES; i++){
var awaitable = this.#load_ral_file(i);
fetches.push(awaitable);
}
await Promise.all(fetches);
}
static async #load_ral_file(idx){
var path = "data/ral-data-" + idx + ".json?ts=" + BUILD_TS;
var awaitable = fetch(path)
.then(response => {
if(!response.ok){
throw new Error("fetch failed");
}
return response.json();
})
.then(data => {
this.#increment_progressbar();
this.#ral_data_files[idx] = data;
})
.catch(e => {
this.#destroy_progressbar();
throw new Error("fetch failed");
});
return awaitable;
}
static #progress_points = 0;
static #progressbar = null;
static #init_progressbar(){
this.#progressbar = new ProgressBar.Circle("#_ProgressBar", {
strokeWidth: 10, // forces extra padding
trailWidth: 3,
text: {
value: "Loading..."
}
});
if(N_RAL_FILES == 1){
// Only one object to load. animate progress as stand-in.
this.#progressbar.animate(1, {duration:400});
}
}
static #increment_progressbar(){
this.#progress_points++;
this.#progressbar.set(this.#progress_points / N_RAL_FILES);
if(this.#progress_points == N_RAL_FILES) {
this.#destroy_progressbar();
}
}
static #destroy_progressbar(){
this.#progressbar.destroy();
this.#progressbar = null;
}
static get_node(id){
var file_idx = Math.floor(id / N_RAL_NODES_PER_FILE);
var idx = id % N_RAL_NODES_PER_FILE;
var node = this.#ral_data_files[file_idx][idx];
this.#expand_bigint(node);
return node;
}
static #expand_bigint(node){
// Check if RAL entry has been converted yet
if(typeof node.offset !== 'string') return;
// Needs conversion from base-16 string --> BigInt object
node.offset = BigInt("0x" + node.offset);
node.size = BigInt("0x" + node.size);
if('stride' in node) node.stride = BigInt("0x" + node.stride);
if(RAL.is_register_node(node)) {
for(var i=0; i<node.fields.length; i++){
node.fields[i].reset = BigInt("0x" + node.fields[i].reset);
}
}
}
static number_of_ids(){
return (
(this.#ral_data_files.length - 1) * N_RAL_NODES_PER_FILE
+ this.#ral_data_files[this.#ral_data_files.length - 1].length
);
}
static is_register(id) {
return(this.is_register_node(this.get_node(id)));
}
static is_register_node(node) {
return("fields" in node);
}
static is_array(id) {
return("dims" in this.get_node(id));
}
static get_common_ancestor(id1, id2) {
var lineage1;
var lineage2;
lineage1 = this.get_ancestors(id1);
lineage1.push(id1);
lineage2 = this.get_ancestors(id2);
lineage2.push(id2);
var id = 0;
while(lineage1.length && lineage2.length && lineage1[0] == lineage2[0]) {
id = lineage1[0];
lineage1.shift();
lineage2.shift();
}
return(id);
}
static get_ancestors(id) {
// Returns a list of id's ancestors
// first in list is root of the tree
var ancestors = [];
while(this.get_node(id).parent !== null) {
id = this.get_node(id).parent;
ancestors.unshift(id);
}
return(ancestors);
}
static get_child_by_name(id, name){
// get child of id that matches name
// returns null if not found
var node = this.get_node(id);
for(var i=0; i<node.children.length; i++){
var cid = node.children[i];
if(this.get_node(cid).name == name){
return(cid);
}
}
return(null);
}
static reset_indexes(from_id, to_id){
// Reset array indexes of all nodes in range.
// from_id is the one closest to the root
// Does not reset the index of from_id
var id = to_id;
var node = this.get_node(id);
while(node.parent !== null) {
if(id == from_id) break;
if(this.is_array(id)){
// reset idxs to all 0s
node.idxs = node.dims.slice();
for(var i=0; i<node.idxs.length; i++){
node.idxs[i] = 0;
}
}
id = node.parent;
node = this.get_node(id);
}
}
static reset_indexes_to_next(id){
// When switching pages, make sure any array nodes that are new to the hier
// path get their indexes reset
var common_anc = this.get_common_ancestor(id, CurrentID);
this.reset_indexes(common_anc, id);
}
static parse_path(path){
// Parses the given path
// If the path is invalid, null is returned
// Otherwise, the following two values are returned as an array:
// [id, idx_stack]
// Any invalid indexes in the path are fixed silently
if(path == null){
return null;
}
// Decompose the path
var pathparts = path.split(".");
var segments = [];
var segment_idxs = [];
for(var i=0; i<pathparts.length; i++){
if(pathparts[i] == "") return(null);
var idxs = [];
var split_element = pathparts[i].split("[");
segments.push(split_element.shift());
for(var dim=0; dim<split_element.length; dim++){
if(!split_element[dim].endsWith("]")) return(null);
var n = Number(split_element[dim].slice(0, -1));
if(!isPositiveInteger(n)) return(null);
if(n<0) return(null);
idxs.push(n);
}
segment_idxs.push(idxs);
}
// Validate first node in path
var id = null;
for(var i=0; i<RootNodeIds.length; i++){
if(segments[0] == this.get_node(RootNodeIds[i]).name) {
id = RootNodeIds[i];
break;
}
}
if(id == null) return(null);
if(this.is_array(id)){
var sanitized_idxs = [];
for(var dim=0; dim<this.get_node(id).dims.length; dim++){
if(dim >= segment_idxs[0].length){
sanitized_idxs.push(0);
} else {
sanitized_idxs.push(Math.min(segment_idxs[0][dim], this.get_node(id).dims[dim]-1));
}
}
segment_idxs[0] = sanitized_idxs;
} else {
if(segment_idxs[0].length != 0) segment_idxs[0] = [];
}
// Validate the path and find the end ID
for(var i=1; i<segments.length; i++){
// try to get the child by name
var next_id = this.get_child_by_name(id, segments[i]);
if(next_id == null) return(null);
id = next_id;
// sanitize indexes
if(this.is_array(id)){
var sanitized_idxs = [];
for(var dim=0; dim<this.get_node(id).dims.length; dim++){
if(dim >= segment_idxs[i].length){
sanitized_idxs.push(0);
} else {
sanitized_idxs.push(Math.min(segment_idxs[i][dim], this.get_node(id).dims[dim]-1));
}
}
segment_idxs[i] = sanitized_idxs;
} else {
if(segment_idxs[i].length != 0) segment_idxs[i] = [];
}
}
return([id, segment_idxs]);
}
static apply_idx_stack(id, idx_stack){
// Applies the given index stack onto the RAL
// Assumes the indexes are valid
for(var i=idx_stack.length-1; i>=0; i--){
if(idx_stack[i].length){
this.get_node(id).idxs = idx_stack[i];
}
id = this.get_node(id).parent;
}
}
static get_path(id, idx_stack, show_idx){
if(typeof idx_stack === "undefined") idx_stack = null;
if(typeof show_idx === "undefined") show_idx = true;
// Get string representation of the hierarchical path
if(show_idx && (idx_stack == null)){
idx_stack = this.get_current_idx_stack(id);
}
var ids = this.get_ids_in_path(id);
var pathparts = [];
for(var i=0; i<ids.length; i++){
var segment = this.get_node(ids[i]).name;
if(show_idx && idx_stack[i].length){
for(var dim=0; dim<idx_stack[i].length; dim++){
segment += "[" + idx_stack[i][dim] + "]";
}
}
pathparts.push(segment);
}
return(pathparts.join("."));
}
static get_ids_in_path(id){
// Get a list of ids that represent the path
var ids = [];
while(id !== null) {
ids.unshift(id);
id = this.get_node(id).parent;
}
return(ids);
}
static get_current_idx_stack(id){
var idx_stack = [];
while(id !== null) {
if(this.is_array(id)){
idx_stack.unshift(this.get_node(id).idxs);
} else {
idx_stack.unshift([]);
}
id = this.get_node(id).parent;
}
return(idx_stack);
}
static get_addr_offset(id){
var node = this.get_node(id);
if(this.is_array(id)){
var flat_idx = 0;
for(var i=0; i<node.idxs.length; i++){
var sz = 1;
for(var j=i+1; j<node.dims.length; j++){
sz *= node.dims[j];
}
flat_idx += sz * node.idxs[i];
}
return(node.offset + node.stride * BigInt(flat_idx));
} else {
return(node.offset);
}
}
static get_absolute_addr(id){
var node = this.get_node(id);
if(node.parent != null){
return(this.get_absolute_addr(node.parent) + this.get_addr_offset(id));
} else {
return(this.get_addr_offset(id));
}
}
static get_total_size(id){
var node = this.get_node(id);
// Total size of entire array of this node
if(this.is_array(id)){
var num_elements = 1;
for(var i=0; i<node.dims.length; i++){
num_elements *= node.dims[i];
}
return(node.stride * BigInt(num_elements - 1) + node.size);
}else{
return(node.size);
}
}
static lookup_by_address(addr, root_id){
// Finds the deepest RAL node that contains addr
// If found, returns:
// [id, idx_stack]
// Otherwise, returns null
if(typeof root_id === "undefined") root_id = 0;
var id=root_id;
var idx_stack = [];
var iter_count = 0;
if(addr < this.get_node(id).offset) return(null);
if(addr >= (this.get_node(id).offset + this.get_total_size(id))) return(null);
while(iter_count < 100){
iter_count++;
// addr is definitely inside this node
// Adjust addr to be relative to this node
addr = addr - this.get_node(id).offset;
// Determine index stack entry for this node
if(this.is_array(id)){
var idxs = [];
// First check if address lands between sparse array entries
if((addr % this.get_node(id).stride) >= this.get_node(id).size) {
// missed! Give up and just return the parent node
if(this.get_node(id).parent == null){
return(null);
}else{
return([this.get_node(id).parent, idx_stack]);
}
}
// index of the flattened array
var flat_idx = Number(addr / this.get_node(id).stride);
// Re-construct dimensions
for(var dim=this.get_node(id).dims.length-1; dim>=0; dim--){
var idx;
idx = flat_idx % this.get_node(id).dims[dim];
flat_idx = Math.floor(flat_idx / this.get_node(id).dims[dim]);
idxs.unshift(idx);
}
idx_stack.push(idxs);
// Adjust addr offset to be relative to this index
addr = addr % this.get_node(id).stride;
} else {
idx_stack.push([]);
}
// Search this node's children to see which child 'addr' is in
var found_match = false;
for(var i=0; i<this.get_node(id).children.length; i++) {
var child = this.get_node(id).children[i];
if((addr >= this.get_node(child).offset) && (addr < (this.get_node(child).offset + this.get_total_size(child)))){
// hit!
id = child;
found_match = true;
break;
}
}
if(!found_match){
// No further match. Current node is the result
return([id, idx_stack]);
}
}
// Hit iteration limit. Something is wrong :-(
throw "Agh! iteration limit reached while looking up by address";
}
static lookup_field_idx(name) {
var node = this.get_node(CurrentID);
for(var i=0; i<node.fields.length; i++){
if(name == node.fields[i].name){
return(i);
}
}
return(-1);
}
static get_node_uid(id) {
var path = this.get_path(id, null, false);
var uid = SHA1(path);
return uid;
}
}