-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhtmlwave.html
More file actions
796 lines (659 loc) · 28.5 KB
/
htmlwave.html
File metadata and controls
796 lines (659 loc) · 28.5 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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTMLwave</title>
<style>
/* Global color palette definition. */
:root {
--light-blue: #E9F1FA;
--bright-blue: #00ABE4;
--dark-blue: #000080;
--white: #FFFFFF;
--black: #000000;
--rule: var(--dark-blue);
--fil-bk: var(--light-blue);
--fil-ft: var(--black);
--stt-bk: yellow;
--stt-ft: black;
--ttl-bk: var(--bright-blue);
--ttl-ft: var(--black);
--ctr-bk: var(--light-blue);
--ctr-ft: var(--black);
--sel-bk: var(--light-blue);
--sel-ft: var(--black);
--sel-ov: var(--bright-blue);
--val-bk: var(--white);
--val-ft: var(--black);
--val-ov: var(--bright-blue);
}
/* Style for the horizontal rules. */
hr {
height: 2px;
background-color: var(--rule);
border: none;
}
/* Style for the file reader. */
.file {
background-color: var(--fil-bk);
color: var(--fil-ft);
font-family: Arial;
}
/* Styles for the status. */
.status-clear {
font-family: Arial;
height: 22px;
}
.status-warning {
background-color: var(--stt-bk);
color: var(--stt-ft);
border-color: var(--stt-ft);
font-family: Arial;
outline: solid 1px;
height: 22px;
}
/* Style for the titles. */
.title {
background-color: var(--ttl-bk);
color: var(--ttl-ft);
border-color: var(--rule);
font-family: Arial;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
outline: solid 1px;
float: top;
height: 28px;
line-height: 28px;
text-align: center;
}
/* Style for the control pane. */
.control {
background-color: var(--ctr-bk);
color: var(--ctr-ft);
border-color: var(--rule);
font-family: Arial;
outline: solid 1px;
float: top;
height: 28px;
line-height: 28px;
text-align: center;
}
/* Style for vertical space div. */
.vspace {
/* No specified color since used in several kinds of panes. */
}
/* Style for the signals values. */
.value {
background-color: var(--val-bk);
color: var(--val-ft);
outline: solid 2px var(--dark-blue);
margin-top: 2px;
}
/* Style for the signal selection. */
.select {
background-color: var(--sel-bk);
color: var(--sel-ft);
outline: solid 2px var(--dark-blue);
margin-top: 2px;
}
/* Remove default bullets */
ul, #sigsel {
list-style-type: none;
padding-left: 18px;
}
/* Remove margins and padding from the parent ul */
#sigsel {
margin: 0;
padding: 0;
}
/* Style the caret/arrow */
.caret {
cursor: pointer;
user-select: none; /* Prevent text selection */
}
/* Create the caret/arrow with a unicode, and style it */
.caret::before {
content: "\25B6";
color: black;
display: inline-block;
margin-right: 6px;
}
/* Rotate the caret/arrow icon when clicked on (using JavaScript) */
.caret-down::before {
transform: rotate(90deg);
}
/* Hide the nested list */
.nested {
display: none;
}
/* Show the nested list when the user clicks on the caret/arrow (with JavaScript) */
.active {
display: block;
}
</style>
</head>
<body>
<div id="file" style="display:flex; float:top;">
<input type="file" accept=".vcd, .VCD" id="fileSelect"
class="file" style="float:left" onchange="readFile(this)" />
<button id="reloadButton"
style="float:left; margin-left:-5px;">↻</button>
<div id="status" class="status-clear"
style="flex-grow:1; margin-left:6px"></div>
<a style="float:right; margin-right:6px; margin-left:6px"
href="LICENSE.txt">License</a>
<button id="help" style="float:right; margin-right:2%"
onclick="window.open('README.txt')">Help</button>
</div>
<hr/>
<div id="window">
<div style="float:left; width:10%;">
<div id="selectPane"
style ="float:top; height:91vh; font-family:Arial;"
ondrop="dropDelSignal(event)" ondragover="allowDrop(event)">
<div class="title">
Signal selection
</div>
<div id="selectScroll" class="select"
style="height:95%; overflow-x:scroll; overflow-y:scroll;">
<div class="vspace" style="height:5px;"></div>
<ul id="sigsel">
<div style="text-align:center;margin:14px;">No signal</div>
</ul>
</div>
</div>
</div>
<div style="float:left; width:10%;">
<div id="valuePane"
style="float:top; height:91vh; font-family:Arial;"
ondrop="dropAddSignal(event)" ondragover="allowDrop(event)">
<div class="title">
Signal value
</div>
<div id="valueScroll" class="value"
style="height:95%; overflow-x:scroll;"
onscroll="scrollViewer(event)">
<div class="vspace" style="height:4px;"></div>
<ul id="sigval"
style="font-family:Courier New; padding:0px; text-indent:1em;">
No signal
</ul>
</div>
</div>
</div>
<div id="controlPane" style="float:left; height:94vh; width:79%;">
<div style="float:top; height:4%;">
<div class="title" style="float:left; width:10%;">
Wave view
</div>
<div class="control">
<div >
<div id="zoom"
style="float:left; padding-left:10px; padding-right:10px;">
zoom:
</div>
<input type="range" min="1" max="99" value="50" class="zoomSlider" id="zoomSlider" style="float:left; margin-top:4px;">
<button class="zoomButton" id="zoomDownButton" style="float:left; margin-top:2px;">-</button>
<button class="zoomButton" id="zoomUpButton" style="float:left; margin-top:2px;">+</button>
</div>
<button id="centerButton"
style="float:right; margin-top:2px; margin-right:2px;">center</button>
<div id="ruler" style="float:right; margin-right:6px">
ruler:
</div>
</div>
</div>
<div id="viewerPane"
style="height:92%; width:100%; overflow-y:scroll; overflow-x:hidden; outline: black solid 1px;"
onscroll="scrollValue(event)" >
<canvas id="viewer" style="background-color:#000000; object-fit:contain;">
</canvas>
</div>
<div id="posPane" style="height:4%; width:100%;display:flex;">
<button class="posButton" id="posDownButton" style="float:left;">◀</button>
<input type="range" min="0" max="1000" value="0" class="posSlider" id="posSlider" style="float:left; display:flex; flex-grow:1; vertical-align:middle;">
<button class="posButton" id="posUpButton" style="float:left;">▶</button>
</div>
</div>
</div>
<script>
// Handle the reload of file.
const reloadButton = document.getElementById("reloadButton");
const inputFile = document.getElementById("fileSelect");
// From the down button.
reloadButton.onclick = function() {
// Reread the file.
readFile(inputFile);
}
// Handles the hierarchical display of the signal selector.
// Code opbtained from the W3C recommendations: How TO - Tree View
function runSelectSignal() {
let toggler = document.getElementsByClassName("caret");
for (let i = 0; i < toggler.length; i++) {
toggler[i].addEventListener("click", function() {
this.parentElement.querySelector(".nested").classList.toggle("active");
this.classList.toggle("caret-down");
});
}
}
// Display the selected signals.
function showSelected() {
let sigvals = undefined;
if (selectedSignals.length > 0) {
// There are some signals to show.
sigvals = '';
let first = true;
for(let sig of selectedSignals) {
sigvals += '<li id="' + sig.id + '"' +
' style="border-bottom:1px solid Gray; ';
if(first) {
sigvals += 'border-top: 1px solid Gray; ';
first = false;
}
sigvals +=
' line-height:' + (viewer.heightS-1) + 'px;" ' +
' draggable="true" ondragstart="dragSignal(event)" onmouseover="showDragVal(event)" onmouseout="unShowDragVal(event)" onclick="modeSignal(event)">' +
sig.name + '=' + viewer.value(sig) +
'</li>'
}
}
else {
// There are no signals yet.
sigvals = 'No signal.';
}
document.getElementById('sigval').innerHTML = sigvals;
}
// Handles the display of the selected signals.
// The id->signal map.
const id2signal = new Map();
// The list of selected signals.
const selectedSignals = [];
// The scroll showing the selected signals.
const valueScroll = document.getElementById('valueScroll');
// Refresh the signal selection variables for the full wave or a
// signal
function refreshSelectSignal(wavesig) {
if (wavesig instanceof Wave) {
// Wave case.
// Clears the tables.
id2signal.clear();
selectedSignals.splice(0,selectedSignals.length);
// Refill the id map.
for(let sig of wavesig.signals) {
// Recruse.
refreshSelectSignal(sig);
}
// Display the selection in the current state.
showSelected();
}
else {
// Signal case.
if (wavesig.subs.length > 0) {
// There are sub signals, recurse.
for(let sub of wavesig.subs) {
refreshSelectSignal(sub);
}
}
else {
// This a leaf signal that can be selected.
id2signal.set(wavesig.id,wavesig);
}
}
}
// Handles add and remove of signal from the selector to the viewer.
// Can be done through drag and drop or double click.
// Add a signal.
function addSignal(sig) {
// If the signal already present, skip it.
if (selectedSignals.includes(sig)) { return; }
// Add the signal.
selectedSignals.push(sig);
viewer.add(sig);
// Display the selection in the current state.
showSelected();
// Adjust the size of the viewer.
valueSize = document.getElementById('sigval').getBoundingClientRect().height;
if (valueSize+42 > viewer.height) {
setViewerSize(0,valueSize+42 - viewerPane.getBoundingClientRect().height);
}
// Display the viewer.
showViewer();
}
// Delete a signal.
function delSignal(sig) {
let idx = selectedSignals.indexOf(sig);
if (idx > -1) { selectedSignals.splice(idx,1); }
viewer.del(sig);
// Display the selection in the current state.
showSelected();
// Display the viewer.
showViewer();
}
// The interface for adding signals.
function allowDrop(ev) {
ev.preventDefault();
}
function showDragSel(ev) {
ev.target.style.backgroundColor =
getComputedStyle(ev.target).getPropertyValue("--sel-ov");
}
function unShowDragSel(ev) {
ev.target.style.backgroundColor =
getComputedStyle(ev.target).getPropertyValue("--sel-bk");
}
function showDragVal(ev) {
ev.target.style.backgroundColor =
getComputedStyle(ev.target).getPropertyValue("--val-ov");
}
function unShowDragVal(ev) {
ev.target.style.backgroundColor =
getComputedStyle(ev.target).getPropertyValue("--val-bk");
}
// Drag a signal.
function dragSignal(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
// Drop for adding a signal.
function dropAddSignal(ev) {
ev.preventDefault();
let data = ev.dataTransfer.getData("text");
// ev.target.appendChild(document.getElementById(data));
let sig = id2signal.get(data);
if(!sig) { console.log("Invalid data: " + data); }
addSignal(sig);
}
// Add a signal on double click.
function dblAddSignal(ev) {
let sig = id2signal.get(ev.target.id);
if(!sig) { console.log("Invalid data: " + data); }
addSignal(sig);
}
// Drop for removing a signal.
function dropDelSignal(ev) {
ev.preventDefault();
let data = ev.dataTransfer.getData("text");
let sig = id2signal.get(data);
if(!sig) { console.log("Invalid data: " + data); }
delSignal(sig);
}
// Delete a signal on double click.
function dblDelSignal(ev) {
let sig = id2signal.get(ev.target.id);
if(!sig) { console.log("Invalid data: " + data); }
delSignal(sig);
}
// Scroll the signal values.
function scrollValue(ev) {
valueScroll.scrollTop = ev.target.scrollTop;
}
// Handle the display mode of signals (binary, decimal or hexa).
// Roll the display mode.
function modeSignal(ev) {
let sig = id2signal.get(ev.target.id);
if(!sig) { console.log("Invalid data: " + data); }
// Update the mode of the signal.
sig.mode = -1;
// Display the selection in the current state.
showSelected();
// Display the viewer.
showViewer();
}
// Handle the zoom.
let zoom = 50;
// let zoomStep = 1;
let zoomScale = 100.0;
let zoomSlider = document.getElementById("zoomSlider");
let zoomUpButton = document.getElementById("zoomUpButton");
let zoomDownButton = document.getElementById("zoomDownButton");
let zoomOutput = document.getElementById("zoom");
// // Refresh the zoom for a wave.
// function refreshZoom(wave) {
// zoomStep = Math.max(0.1,1000 / wave.length);
// }
// Display the zoom (and scale) value.
function showZoom() {
let zVal = zoom.toFixed(2);
if (zVal.length<5) {
zoomOutput.innerHTML = "zoom: 0" + zoom.toFixed(4) +
" (digit: " + Math.log10(zoomScale/100).toFixed(0) + ")";
}
else {
zoomOutput.innerHTML = "zoom: " + zoom.toFixed(4) +
" (digit: " + Math.log10(zoomScale/100).toFixed(0) + ")";
}
}
// Show the zoom a first time.
showZoom();
// Update the current slider value.
// From the slider.
zoomSlider.oninput = function() {
zoom = Math.trunc(zoom/zoomScale)*zoomScale +
parseFloat(this.value/100.0)*zoomScale;
showZoom();
showViewer();
}
// From the down button.
zoomDownButton.onclick = function() {
// if (zoom > 1) { zoom -= zoomStep; }
// if (zoom < 1) { zoom = 1; }
zoomScale /= 100;
// zoomSlider.value = zoom;
zoomSlider.value = zoom-Math.trunc(zoom/zoomScale)*zoomScale;
showZoom();
showViewer();
}
// From the up button.
zoomUpButton.onclick = function() {
// if (zoom < 99) { zoom += zoomStep; }
// if (zoom > 99) { zoom = 99; }
zoomScale *= 100;
if (zoomScale > 100.0) { zoomScale = 100.0; }
// zoomSlider.value = zoom;
zoomSlider.value = zoom-Math.trunc(zoom/zoomScale)*zoomScale;
showZoom();
showViewer();
}
// Handle the start position of the viewer.
let position = 0;
// let posStep = 1;
let posSlider = document.getElementById("posSlider");
let posUpButton = document.getElementById("posUpButton");
let posDownButton = document.getElementById("posDownButton");
// // Refresh the position handling for a wave.
// function refreshPos(wave) {
// posStep = 100 / wave.length;
// }
// Update the current slider value.
// From the slider.
posSlider.oninput = function() {
position = parseFloat(this.value);
showViewer();
}
// From the down button.
posDownButton.onclick = function() {
// if (position > 0) { position -= posStep; }
if (position > 0) { position -= zoomScale/100; }
if (position < 0) { position = 0; }
posSlider.value = position;
showViewer();
}
// From the up button.
posUpButton.onclick = function() {
// if (position < 1000) { position += posStep; }
if (position < 1000) { position += zoomScale/100; }
if (position > 1000) { position = 1000; }
posSlider.value = position;
showViewer();
}
</script>
<script src="./canvaswave.js"></script>
<script>
// Sets the viewer variables and style.
let viewer = undefined;
const viewerPane = document.getElementById('viewerPane');
const canvas = document.getElementById('viewer');
const context = canvas.getContext("2d");
const viewerRect = viewerPane.getBoundingClientRect();
// The color palette.
const palette = { back: 'black', text: 'white',
signal: 'yellow', fill: 'red' };
// Set the size of the canvas to viewerPane + (dx,dy) pixels.
function setViewerSize(dx,dy) {
// console.log("dx=" + dx + " dy=" + dy);
// Get the DPR and size of the canvas
const dpr = window.devicePixelRatio;
// Set the "actual" size of the canvas
canvas.width = (viewerRect.width+dx) * dpr;
canvas.height = (viewerRect.height+dy) * dpr;
// Scale the context to ensure correct drawing operations
context.scale(dpr, dpr);
// Set the "drawn" size of the canvas
canvas.style.width = `${viewerRect.width+dx}px`;
canvas.style.height = `${viewerRect.height+dy}px`;
}
// First set the size without increase.
setViewerSize(0,0);
// Show the viewer if existing.
function showViewer() {
if (viewer) {
viewer.zoom = zoom;
viewer.position = position * (viewer.wave.length-viewer.widthT) / 1000;
viewer.clear();
viewer.draw();
}
}
// Build the viewer with a new wave.
function buildViewer(wave) {
viewer = new CanvasWave(canvas,palette,wave);
showViewer();
}
// Scroll the viewer.
function scrollViewer(ev) {
viewerPane.scrollTop = ev.target.scrollTop;
}
// Handle the clicks on the canvas.
// The ruler.
let rulerOutput = document.getElementById("ruler");
let rulerHolding = false; // Tell the ruler is holded.
// The center on ruler button.
let centerButton = document.getElementById("centerButton");
// Updating the ruler.
function updateRuler(ev) {
// Reposition the ruler.
const x = ev.clientX - viewerRect.left;
// const y = ev.clientY - viewerRect.top;
viewer.ruler = viewer.toT(x);
// Update the selected signals view.
showSelected();
// Display the viewer.
showViewer();
// Update the ruler viewer.
showRuler();
}
canvas.addEventListener('mousedown', function(ev) {
// The mouse start to be held.
rulerHolding = true;
// Update the ruler.
updateRuler(ev);
});
canvas.addEventListener('mouseup', function(ev) {
// The mouse in not held any longer.
rulerHolding = false;
});
canvas.addEventListener('mouseleave', function(ev) {
// The mouse maybe held, but out of the canvas.
rulerHolding = false;
});
canvas.addEventListener('mousemove', function(ev) {
if(rulerHolding == true) {
// Update the ruler.
updateRuler(ev);
}
});
// Display the ruler time.
function showRuler() {
rulerOutput.innerHTML = "ruler: " + Math.round(viewer.ruler) + ' ' + viewer.wave.unit;
}
// Center on the ruler.
centerButton.onclick = function() {
position = (viewer.ruler - viewer.widthT/2) * 1000 / (viewer.wave.length-viewer.widthT)
if (position < 0) { position = 0; }
if (position > 1000) { position = 1000; }
posSlider.value = position;
showViewer();
}
</script>
<script src="./htmlwave.js"></script>
<script>
let str = ''
const reader = new FileReader();
// Displays a signal and its hierachy.
function showSignal(sig) {
let showsig = '<li style="white-space:nowrap;" id="' + sig.id + '"';
if (sig.subs.length > 0) {
showsig += '><span class="caret">' + sig.name + '</span>\n';
showsig += '<ul class="nested">\n';
for(let sub of sig.subs) { showsig += showSignal(sub); }
showsig += '</ul>\n';
}
else {
// Leaf signals are to be draggable and double clickable.
showsig += 'draggable="true" ondblclick="dblAddSignal(event)" ondragstart="dragSignal(event)" onmouseover="showDragSel(event)" onmouseout="unShowDragSel(event)">' + sig.name;
}
showsig += '</li>\n'
return showsig;
}
// Displays a signal selector from a wave.
function selectSignal(wave) {
let selsigs = '';
for(sig of wave.signals) { selsigs += showSignal(sig); }
selsigs += "<hr/><br/>"
return selsigs;
}
// Read a wave and update the wave viewer accordingly.
function readWave(str) {
const wave = read_vcd(str);
const status = document.getElementById('status');
if (wave instanceof Wave) {
document.getElementById('sigsel').innerHTML =
selectSignal(wave);
// Update the signal selector if new file.
refreshSelectSignal(wave);
runSelectSignal();
// Update the zoom.
// refreshZoom(wave);
showZoom();
// Build the viewer.
buildViewer(wave);
// Update the ruler viewer.
showRuler();
// Indicate the status is OK (i.e., clears the status).
status.innerHTML='';
status.classList.remove('status-warning');
status.classList.add('status-clear');
}
else {
// The file could not be load, show it in the status.
status.innerHTML=' ⚠ Invalid file!';
status.classList.remove('status-clear');
status.classList.add('status-warning');
}
}
// Read a file.
function readFile(input) {
const file = input.files[0];
reader.readAsText(file);
reader.onload = function () {
readWave(reader.result);
}
reader.onerror = function () {
console.log(reader.error);
};
}
// First load.
readFile(document.getElementById('fileSelect'))
//const wave = read_vcd(str);
</script>
</body>
</html>