-
Notifications
You must be signed in to change notification settings - Fork 10
/
trace.js
100 lines (88 loc) · 2.43 KB
/
trace.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
class Trace {
constructor() {
this.samples = [];
}
reset() {
this.samples = [];
}
sample(graph) {
const currentSample = {};
this.samples.push(currentSample);
graph.forEach(s => {
currentSample[s.id] = s.state;
});
}
getTraces(names) {
let longestName = '';
let waves = {};
for (let name of names) {
waves[name] = ['', ''];
if (name.length > longestName.length) {
longestName = name;
}
}
for (let i = 0; i < this.samples.length; i++) {
const trace = this.samples[i];
const prevTrace = this.samples[i - 1];
Object.entries(trace).forEach(([signal, value]) => {
if (!names.includes(signal)) {
return;
}
if (prevTrace) {
if (prevTrace[signal] !== value) {
if (prevTrace[signal] === 'x') {
// From X to valid
waves[signal][0] += ']';
} else if (value === 'x') {
// From valid to X
waves[signal][0] += '[';
} else {
// From one logic level to another
waves[signal][0] += '|';
}
waves[signal][1] += ' ';
} else {
if (value === 0) {
waves[signal][0] += '_';
waves[signal][1] += ' ';
} else if (value === 1) {
waves[signal][0] += ' ';
waves[signal][1] += '_';
} else if (value === 'x') {
waves[signal][0] += 'X';
waves[signal][1] += ' ';
}
}
}
if (value === 0) {
waves[signal][0] += '__';
waves[signal][1] += ' ';
} else if (value === 1) {
waves[signal][0] += ' ';
waves[signal][1] += '__';
} else if (value === 'x') {
waves[signal][0] += 'XX';
waves[signal][1] += ' ';
}
});
}
return Object.entries(waves).map(([signal, wave]) => {
const waveOutput = [
`${' '.repeat(longestName.length + 2)}${wave[1]}`,
`${signal}${' '.repeat(longestName.length - signal.length + 2)}${wave[0]}`,
''
].join('\n');
return waveOutput;
});
}
getAllTraces() {
return this.getTraces(Object.keys(this.samples[0]));
}
getSample(n, names) {
return names.reduce((acc, signal) => {
acc[signal] = this.samples[n][signal];
return acc;
}, {});
}
}
module.exports = Trace;