This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
video.js
1568 lines (1447 loc) · 67.5 KB
/
video.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
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileoverview Implements the PC8080 Video component.
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2020 Jeff Parsons
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*
* PCjs is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* PCjs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every modified copy of this work
* and to display that copyright notice when the software starts running; see COPYRIGHT in
* <https://www.pcjs.org/modules/shared/lib/defines.js>.
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of PCjs
* for purposes of the GNU General Public License, and the author does not claim any copyright
* as to their contents.
*/
"use strict";
if (typeof module !== "undefined") {
var Str = require("../../shared/lib/strlib");
var Web = require("../../shared/lib/weblib");
var DumpAPI = require("../../shared/lib/dumpapi");
var Component = require("../../shared/lib/component");
var State = require("../../shared/lib/state");
var PC8080 = require("./defines");
var ChipSet8080 = require("./chipset");
var Memory8080 = require("./memory");
var Messages8080 = require("./messages");
}
/**
* TODO: The Closure Compiler treats ES6 classes as 'struct' rather than 'dict' by default,
* which would force us to declare all class properties in the constructor, as well as prevent
* us from defining any named properties. So, for now, we mark all our classes as 'unrestricted'.
*
* @unrestricted
*/
class Video8080 extends Component {
/**
* Video8080(parmsVideo, canvas, context, textarea, container)
*
* The Video8080 component can be configured with the following (parmsVideo) properties:
*
* screenWidth: width of the screen canvas, in pixels
* screenHeight: height of the screen canvas, in pixels
* screenColor: background color of the screen canvas (default is black)
* screenRotate: the amount of counter-clockwise screen rotation required (eg, -90 or 270)
* aspectRatio (eg, 1.33)
* bufferAddr: the starting address of the frame buffer (eg, 0x2400)
* bufferRAM: true to use existing RAM (default is false)
* bufferFormat: if defined, one of the recognized formats in Video8080.FORMATS (eg, "vt100")
* bufferCols: the width of a single frame buffer row, in pixels (eg, 256)
* bufferRows: the number of frame buffer rows (eg, 224)
* bufferBits: the number of bits per column (default is 1)
* bufferLeft: the bit position of the left-most pixel in a byte (default is 0; CGA uses 7)
* bufferRotate: the amount of counter-clockwise buffer rotation required (eg, -90 or 270)
* interruptRate: normally the same as (or some multiple of) refreshRate (eg, 120)
* refreshRate: how many times updateScreen() should be performed per second (eg, 60)
*
* In addition, if a text-only display is being emulated, define the following properties:
*
* fontROM: URL of font ROM
* fontColor: default is white
* cellWidth: number (eg, 10 for VT100)
* cellHeight: number (eg, 10 for VT100)
*
* We record all the above values now, but we defer creation of the frame buffer until our initBus()
* handler is called. At that point, we will also compute the extent of the frame buffer, determine the
* appropriate "cell" size (ie, the number of pixels that updateScreen() will fetch and process at once),
* and then allocate our cell cache.
*
* Why interruptRate in addition to refreshRate? A higher interrupt rate is required for Space Invaders,
* because even though the CRT refreshes at 60Hz, the CRT controller interrupts the CPU *twice* per
* refresh (once after the top half of the screen has been redrawn, and again after the bottom half has
* been redrawn), so we need an interrupt rate of 120Hz. We pass the higher rate on to the CPU, so that
* it will call updateScreen() more frequently, but we still limit our screen updates to every *other* call.
*
* bufferRotate is an alternative to screenRotate; you may set one or the other (but not both) to -90 to
* enable different approaches to counter-clockwise 90-degree image rotation. screenRotate uses canvas
* transformation methods (translate(), rotate(), and scale()), while bufferRotate inverts the dimensions
* of the off-screen buffer and then relies on setPixel() to "rotate" the data into the proper location.
*
* @this {Video8080}
* @param {Object} parmsVideo
* @param {Object} [canvas]
* @param {Object} [context]
* @param {Object} [textarea]
* @param {Object} [container]
*/
constructor(parmsVideo, canvas, context, textarea, container)
{
super("Video", parmsVideo, Messages8080.VIDEO);
var video = this, sProp, sEvent;
this.fGecko = Web.isUserAgent("Gecko/");
this.cxScreen = parmsVideo['screenWidth'];
this.cyScreen = parmsVideo['screenHeight'];
this.addrBuffer = parmsVideo['bufferAddr'];
this.fUseRAM = parmsVideo['bufferRAM'];
var sFormat = parmsVideo['bufferFormat'];
this.nFormat = sFormat && Video8080.FORMATS[sFormat.toUpperCase()] || Video8080.FORMAT.UNKNOWN;
this.nColsBuffer = parmsVideo['bufferCols'];
this.nRowsBuffer = parmsVideo['bufferRows'];
this.cxCellDefault = this.cxCell = parmsVideo['cellWidth'] || 1;
this.cyCellDefault = this.cyCell = parmsVideo['cellHeight'] || 1;
this.abFontData = null;
this.fDotStretcher = false;
this.nBitsPerPixel = parmsVideo['bufferBits'] || 1;
this.iBitFirstPixel = parmsVideo['bufferLeft'] || 0;
this.rotateBuffer = parmsVideo['bufferRotate'];
if (this.rotateBuffer) {
this.rotateBuffer = this.rotateBuffer % 360;
if (this.rotateBuffer > 0) this.rotateBuffer -= 360;
if (this.rotateBuffer != -90) {
this.notice("unsupported buffer rotation: " + this.rotateBuffer);
this.rotateBuffer = 0;
}
}
this.rateInterrupt = parmsVideo['interruptRate'];
this.rateRefresh = parmsVideo['refreshRate'] || 60;
this.canvasScreen = canvas;
this.contextScreen = context;
this.textareaScreen = textarea;
this.inputScreen = textarea || canvas || null;
/*
* These variables are here in case we want/need to add support for borders later...
*/
this.xScreenOffset = this.yScreenOffset = 0;
this.cxScreenOffset = this.cxScreen;
this.cyScreenOffset = this.cyScreen;
this.cxScreenCell = (this.cxScreen / this.nColsBuffer)|0;
this.cyScreenCell = (this.cyScreen / this.nRowsBuffer)|0;
/*
* Now that we've finished using nRowsBuffer to help define the screen size, we add one more
* row for text modes, to account for the VT100's scroll line buffer (used for smooth scrolling).
*/
if (this.cyCell > 1) {
this.nRowsBuffer++;
this.bScrollOffset = 0;
this.fSkipSingleCellUpdate = false;
}
/*
* Support for disabling (or, less commonly, enabling) image smoothing, which all browsers
* seem to support now (well, OK, I still have to test the latest MS Edge browser), despite
* it still being labelled "experimental technology". Let's hope the browsers standardize
* on this. I see other options emerging, like the CSS property "image-rendering: pixelated"
* that's apparently been added to Chrome. Sigh.
*/
var fSmoothing = parmsVideo['smoothing'];
var sSmoothing = Web.getURLParm('smoothing');
if (sSmoothing) fSmoothing = (sSmoothing == "true");
this.fSmoothing = fSmoothing;
this.sSmoothing = Web.findProperty(this.contextScreen, 'imageSmoothingEnabled');
this.rotateScreen = parmsVideo['screenRotate'];
if (this.rotateScreen) {
this.rotateScreen = this.rotateScreen % 360;
if (this.rotateScreen > 0) this.rotateScreen -= 360;
/*
* TODO: Consider also disallowing any rotateScreen value if bufferRotate was already set; setting
* both is most likely a mistake, but who knows, maybe someone wants to use both for 180-degree rotation?
*/
if (this.rotateScreen != -90) {
this.notice("unsupported screen rotation: " + this.rotateScreen);
this.rotateScreen = 0;
} else {
this.contextScreen.translate(0, this.cyScreen);
this.contextScreen.rotate((this.rotateScreen * Math.PI)/180);
this.contextScreen.scale(this.cyScreen/this.cxScreen, this.cxScreen/this.cyScreen);
}
}
/*
* Here's the gross code to handle full-screen support across all supported browsers. The lack of standards
* is exasperating; browsers can't agree on 'Fullscreen' (most common) or 'FullScreen' (least common), and while
* some browsers honor other browser prefixes, most don't. Event handlers tend to be more consistent (ie, all
* lower-case).
*/
this.container = container;
if (this.container) {
sProp = Web.findProperty(container, 'requestFullscreen') || Web.findProperty(container, 'requestFullScreen');
if (sProp) {
this.container.doFullScreen = container[sProp];
sEvent = Web.findProperty(document, 'on', 'fullscreenchange');
if (sEvent) {
var sFullScreen = Web.findProperty(document, 'fullscreenElement') || Web.findProperty(document, 'fullScreenElement');
document.addEventListener(sEvent, function onFullScreenChange() {
video.notifyFullScreen(document[sFullScreen] != null);
}, false);
}
sEvent = Web.findProperty(document, 'on', 'fullscreenerror');
if (sEvent) {
document.addEventListener(sEvent, function onFullScreenError() {
video.notifyFullScreen();
}, false);
}
}
}
this.sFontROM = parmsVideo['fontROM'];
if (this.sFontROM) {
var sFileExt = Str.getExtension(this.sFontROM);
if (sFileExt != "json") {
this.sFontROM = Web.getHostOrigin() + DumpAPI.ENDPOINT + '?' + DumpAPI.QUERY.FILE + '=' + this.sFontROM + '&' + DumpAPI.QUERY.FORMAT + '=' + DumpAPI.FORMAT.BYTES;
}
Web.getResource(this.sFontROM, null, true, function(sURL, sResponse, nErrorCode) {
video.doneLoad(sURL, sResponse, nErrorCode);
});
}
this.ledBindings = {};
}
/**
* initBuffers()
*
* @this {Video8080}
* @return {boolean}
*/
initBuffers()
{
/*
* Allocate off-screen buffers now
*/
this.cxBuffer = this.nColsBuffer * this.cxCell;
this.cyBuffer = this.nRowsBuffer * this.cyCell;
var cxBuffer = this.cxBuffer;
var cyBuffer = this.cyBuffer;
if (this.rotateBuffer) {
cxBuffer = this.cyBuffer;
cyBuffer = this.cxBuffer;
}
this.sizeBuffer = 0;
if (!this.fUseRAM) {
this.sizeBuffer = ((this.cxBuffer * this.nBitsPerPixel) >> 3) * this.cyBuffer;
if (!this.bus.addMemory(this.addrBuffer, this.sizeBuffer, Memory8080.TYPE.VIDEO)) {
return false;
}
}
/*
* imageBuffer is only used for graphics modes. For text modes, we create a canvas
* for each font and draw characters by drawing from the font canvas to the target canvas.
*/
if (this.sizeBuffer) {
this.imageBuffer = this.contextScreen.createImageData(cxBuffer, cyBuffer);
this.nPixelsPerCell = (16 / this.nBitsPerPixel)|0;
this.initCellCache(this.sizeBuffer >> 1);
} else {
/*
* We add an extra column per row to store the visible line length at the start of every row.
*/
this.initCellCache((this.nColsBuffer + 1) * this.nRowsBuffer);
}
this.canvasBuffer = document.createElement("canvas");
this.canvasBuffer.width = cxBuffer;
this.canvasBuffer.height = cyBuffer;
this.contextBuffer = this.canvasBuffer.getContext("2d");
this.aFonts = {};
this.initColors();
if (this.nFormat == Video8080.FORMAT.VT100) {
/*
* Beyond fonts, VT100 support requires that we maintain a number of additional properties:
*
* rateMonitor: must be either 50 or 60 (defaults to 60); we don't emulate the monitor refresh rate,
* but we do need to keep track of which rate has been selected, because that affects the number of
* "fill lines" present at the top of the VT100's frame buffer: 2 lines for 60Hz, 5 lines for 50Hz.
*
* The VT100 July 1982 Technical Manual, p. 4-89, shows the following sample frame buffer layout:
*
* 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
* --------------------------------------------------------------
* 0x2000: 7F 70 03 7F F2 D0 7F 70 06 7F 70 0C 7F 70 0F 7F
* 0x2010: 70 03 .. .. .. .. .. .. .. .. .. .. .. .. .. ..
* ...
* 0x22D0: 'D' 'A' 'T' 'A' ' ' 'F' 'O' 'R' ' ' 'F' 'I' 'R' 'S' 'T' ' ' 'L'
* 0x22E0: 'I' 'N' 'E' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
* ...
* 0x2320: 7F F3 23 'D' 'A' 'T' 'A' ' ' 'F' 'O' 'R' ' ' 'S' 'E' 'C' 'O'
* 0x2330: 'N' 'D' ' ' 'L' 'I' 'N' 'E' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
* ...
* 0x2BE0: ' ' ' ' 'E' 'N' 'D' ' ' 'O' 'F' ' ' 'L' 'A' 'S' 'T' ' ' 'L' 'I'
* 0x2BF0: 'N' 'E' 7F 70 06 .. .. .. .. .. .. .. .. .. .. ..
* 0x2C00: [AVO SCREEN RAM, IF ANY, BEGINS HERE]
*
* ERRATA: The manual claims that if you change the byte at 0x2002 from 03 to 09, the number of "fill
* lines" will change from 2 to 5 (for 50Hz operation), but it shows 06 instead of 0C at location 0x200B;
* if you follow the links, it's pretty clear that byte has to be 0C to yield 5 "fill lines". Since the
* address following the terminator at 0x2006 points to itself, it never makes sense for that terminator
* to be used EXCEPT at the end of the frame buffer.
*
* As an alternative to tracking the monitor refresh rate, we could hard-code some knowledge about how
* the VT100's 8080 code uses memory, and simply ignore lines below address 0x22D0. But the VT100 Video
* Processor makes no such assumption, and it would also break our test code in createFonts(), which
* builds a contiguous screen of test data starting at the default frame buffer address (0x2000).
*/
this.rateMonitor = 60;
/*
* The default character-selectable attribute (reverse video vs. underline) is controlled by fUnderline.
*/
this.fUnderline = false;
this.abLineBuffer = new Array(this.nColsBuffer);
}
/*
* Our 'smoothing' parameter defaults to null (which we treat the same as undefined), which means that
* image smoothing will be selectively enabled (ie, true for text modes, false for graphics modes); otherwise,
* we'll set image smoothing to whatever value was provided for ALL modes -- assuming the browser supports it.
*/
if (this.sSmoothing) {
this.contextScreen[this.sSmoothing] = (this.fSmoothing == null? false /* (this.nFormat == Video8080.FORMAT.VT100? true : false) */ : this.fSmoothing);
}
return true;
}
/**
* setBinding(sHTMLType, sBinding, control, sValue)
*
* @this {Video8080}
* @param {string} sHTMLType is the type of the HTML control (eg, "button", "list", "text", "submit", "textarea", "canvas")
* @param {string} sBinding is the value of the 'binding' parameter stored in the HTML control's "data-value" attribute (eg, "refresh")
* @param {HTMLElement} control is the HTML control DOM object (eg, HTMLButtonElement)
* @param {string} [sValue] optional data value
* @return {boolean} true if binding was successful, false if unrecognized binding request
*/
setBinding(sHTMLType, sBinding, control, sValue)
{
var video = this;
/*
* TODO: A more general-purpose binding mechanism would be nice someday....
*/
if (sHTMLType == "led" || sHTMLType == "rled") {
this.ledBindings[sBinding] = control;
return true;
}
switch (sBinding) {
case "fullScreen":
this.bindings[sBinding] = control;
if (this.container && this.container.doFullScreen) {
control.onclick = function onClickFullScreen() {
if (DEBUG) video.printMessage("fullScreen()");
video.doFullScreen();
};
} else {
if (DEBUG) this.log("FullScreen API not available");
control.parentNode.removeChild(/** @type {Node} */ (control));
}
return true;
default:
break;
}
return false;
}
/**
* initBus(cmp, bus, cpu, dbg)
*
* @this {Video8080}
* @param {Computer8080} cmp
* @param {Bus8080} bus
* @param {CPUState8080} cpu
* @param {Debugger8080} dbg
*/
initBus(cmp, bus, cpu, dbg)
{
this.cmp = cmp;
this.bus = bus;
this.cpu = cpu;
this.dbg = dbg;
/*
* Allocate the frame buffer (as needed) along with all other buffers.
*/
this.initBuffers();
/*
* If we have an associated keyboard, then ensure that the keyboard will be notified
* whenever the canvas gets focus and receives input.
*/
this.kbd = /** @type {Keyboard8080} */ (cmp.getMachineComponent("Keyboard"));
if (this.kbd) {
for (var s in this.ledBindings) {
this.kbd.setBinding("led", s, this.ledBindings[s]);
}
if (this.canvasScreen) {
this.kbd.setBinding(this.textareaScreen? "textarea" : "canvas", "screen", /** @type {HTMLElement} */ (this.inputScreen));
}
}
var video = this;
this.timerUpdateNext = this.cpu.addTimer(this.id, function() {
video.updateScreen();
});
this.cpu.setTimer(this.timerUpdateNext, this.getRefreshTime());
this.nUpdates = 0;
if (!this.sFontROM) this.setReady();
}
/**
* doneLoad(sURL, sFontData, nErrorCode)
*
* @this {Video8080}
* @param {string} sURL
* @param {string} sFontData
* @param {number} nErrorCode (response from server if anything other than 200)
*/
doneLoad(sURL, sFontData, nErrorCode)
{
if (nErrorCode) {
this.notice("Unable to load font ROM (error " + nErrorCode + ": " + sURL + ")");
return;
}
Component.addMachineResource(this.idMachine, sURL, sFontData);
try {
/*
* The most likely source of any exception will be here: parsing the JSON-encoded data.
*/
var ab = eval("(" + sFontData + ")");
var abFontData = ab['bytes'] || ab;
if (!abFontData || !abFontData.length) {
Component.error("Empty font ROM: " + sURL);
return;
}
else if (abFontData.length == 1) {
Component.error(abFontData[0]);
return;
}
/*
* Minimal font data validation, just to make sure we're not getting garbage from the server.
*/
if (abFontData.length == 2048) {
this.abFontData = abFontData;
this.createFonts();
}
else {
this.notice("Unrecognized font data length (" + abFontData.length + ")");
return;
}
} catch (e) {
this.notice("Font ROM data error: " + e.message);
return;
}
/*
* If we're still here, then we're ready!
*
* UPDATE: Per issue #21, I'm issuing setReady() *only* if a valid contextScreen exists *or* a Debugger is attached.
*
* TODO: Consider a more general-purpose solution for deciding whether or not the user wants to run in a "headless" mode.
*/
if (this.contextScreen || this.dbg) this.setReady();
}
/**
* createFonts()
*
* @this {Video8080}
* @return {boolean}
*/
createFonts()
{
/*
* We retain abFontData in case we have to rebuild the fonts (eg, when we switch from 80 to 132 columns)
*/
if (this.abFontData) {
this.fDotStretcher = (this.nFormat == Video8080.FORMAT.VT100);
this.aFonts[Video8080.VT100.FONT.NORML] = [
this.createFontVariation(this.cxCell, this.cyCell),
this.createFontVariation(this.cxCell, this.cyCell, this.fUnderline)
];
this.aFonts[Video8080.VT100.FONT.DWIDE] = [
this.createFontVariation(this.cxCell*2, this.cyCell),
this.createFontVariation(this.cxCell*2, this.cyCell, this.fUnderline)
];
this.aFonts[Video8080.VT100.FONT.DHIGH] = this.aFonts[Video8080.VT100.FONT.DHIGH_BOT] = [
this.createFontVariation(this.cxCell*2, this.cyCell*2),
this.createFontVariation(this.cxCell*2, this.cyCell*2, this.fUnderline)
];
return true;
}
return false;
}
/**
* createFontVariation(cxCell, cyCell, fUnderline)
*
* This creates a 16x16 character grid for the requested font variation. Variations include:
*
* 1) no variation (cell size is this.cxCell x this.cyCell)
* 2) double-wide characters (cell size is this.cxCell*2 x this.cyCell)
* 3) double-high double-wide characters (cell size is this.cxCell*2 x this.cyCell*2)
* 4) any of the above with either reverse video or underline enabled (default is neither)
*
* @this {Video8080}
* @param {number} cxCell is the target width of each character in the grid
* @param {number} cyCell is the target height of each character in the grid
* @param {boolean} [fUnderline] (null for unmodified font, false for reverse video, true for underline)
* @return {Object}
*/
createFontVariation(cxCell, cyCell, fUnderline)
{
/*
* On a VT100, cxCell,cyCell is initially 10,10, but may change to 9,10 for 132-column mode.
*/
this.assert(cxCell == this.cxCell || cxCell == this.cxCell*2);
this.assert(cyCell == this.cyCell || cyCell == this.cyCell*2);
/*
* Create a font canvas that is both 16 times the target character width and the target character height,
* ensuring that it will accommodate 16x16 characters (for a maximum of 256). Note that the VT100 font ROM
* defines only 128 characters, so that canvas will contain only 16x8 entries.
*/
var nFontBytesPerChar = this.cxCellDefault <= 8? 8 : 16;
var nFontByteOffset = nFontBytesPerChar > 8? 15 : 0;
var nChars = this.abFontData.length / nFontBytesPerChar;
/*
* The absence of a boolean for fUnderline means that both fReverse and fUnderline are "falsey". The presence
* of a boolean means that fReverse will be true OR fUnderline will be true, but NOT both.
*/
var fReverse = (fUnderline === false);
var font = {cxCell: cxCell, cyCell: cyCell};
font.canvas = document.createElement("canvas");
font.canvas.width = cxCell * 16;
font.canvas.height = cyCell * (nChars / 16);
font.context = font.canvas.getContext("2d");
var imageChar = font.context.createImageData(cxCell, cyCell);
for (var iChar = 0; iChar < nChars; iChar++) {
for (var y = 0, yDst = y; y < this.cyCell; y++) {
var offFontData = iChar * nFontBytesPerChar + ((nFontByteOffset + y) & (nFontBytesPerChar - 1));
var bits = (fUnderline && y == 8? 0xff : this.abFontData[offFontData]);
for (var nRows = 0; nRows < (cyCell / this.cyCell); nRows++) {
var bitPrev = 0;
for (var x = 0, xDst = x; x < this.cxCell; x++) {
/*
* While x goes from 0 to cxCell-1, obviously we will run out of bits after x is 7;
* since the final bit must be replicated all the way to the right edge of the cell
* (so that line-drawing characters seamlessly connect), we ensure that the effective
* shift count remains stuck at 7 once it reaches 7.
*/
var bitReal = bits & (0x80 >> (x > 7? 7 : x));
var bit = (this.fDotStretcher && !bitReal && bitPrev)? bitPrev : bitReal;
for (var nCols = 0; nCols < (cxCell / this.cxCell); nCols++) {
if (fReverse) bit = !bit;
this.setPixel(imageChar, xDst, yDst, bit? 1 : 0);
xDst++;
}
bitPrev = bitReal;
}
yDst++;
}
}
/*
* (iChar >> 4) performs the integer equivalent of Math.floor(iChar / 16), and (iChar & 0xf) is the equivalent of (iChar % 16).
*/
font.context.putImageData(imageChar, (iChar & 0xf) * cxCell, (iChar >> 4) * cyCell);
}
return font;
}
/**
* powerUp(data, fRepower)
*
* @this {Video8080}
* @param {Object|null} data
* @param {boolean} [fRepower]
* @return {boolean} true if successful, false if failure
*/
powerUp(data, fRepower)
{
if (!fRepower) {
if (data) {
if (!this.restore(data)) return false;
}
}
/*
* Because the VT100 frame buffer can be located anywhere in RAM (above 0x2000), we must defer this
* test code until the powerUp() notification handler is called, when all RAM has (hopefully) been allocated.
*
* NOTE: The following test screen was useful for early testing, but a *real* VT100 doesn't display a test screen,
* so this code is no longer enabled by default. Remove MAXDEBUG if you want to see it again.
*/
if (MAXDEBUG && this.nFormat == Video8080.FORMAT.VT100) {
/*
* Build a test screen in the VT100 frame buffer; we'll mimic the "SET-UP A" screen, since it uses
* all the font variations. The process involves iterating over 0-based row numbers -2 (or -5 if 50Hz
* operation is selected) through 24, checking aLineData for a matching row number, and converting the
* corresponding string(s) to appropriate byte values. Negative row numbers correspond to "fill lines"
* and do not require a row entry. If multiple strings are present for a given row, we invert the
* default character attribute for subsequent strings. An empty array ends the screen build process.
*/
var aLineData = {
0: [Video8080.VT100.FONT.DHIGH, 'SET-UP A'],
2: [Video8080.VT100.FONT.DWIDE, 'TO EXIT PRESS "SET-UP"'],
22: [Video8080.VT100.FONT.NORML, ' T T T T T T T T T'],
23: [Video8080.VT100.FONT.NORML, '1234567890', '1234567890', '1234567890', '1234567890', '1234567890', '1234567890', '1234567890', '1234567890'],
24: []
};
var addr = this.addrBuffer;
var addrNext = -1, font = -1;
var b, nFill = (this.rateMonitor == 60? 2 : 5);
for (var iRow = -nFill; iRow < this.nRowsBuffer; iRow++) {
var lineData = aLineData[iRow];
if (addrNext >= 0) {
var fBreak = false;
addrNext = addr + 2;
if (!lineData) {
if (font == Video8080.VT100.FONT.DHIGH) {
lineData = aLineData[iRow-1];
font = Video8080.VT100.FONT.DHIGH_BOT;
}
}
else {
if (lineData.length) {
font = lineData[0];
} else {
addrNext = addr - 1;
fBreak = true;
}
}
b = (font & Video8080.VT100.LINEATTR.FONTMASK) | ((addrNext >> 8) & Video8080.VT100.LINEATTR.ADDRMASK) | Video8080.VT100.LINEATTR.ADDRBIAS;
this.bus.setByteDirect(addr++, b);
this.bus.setByteDirect(addr++, addrNext & 0xff);
if (fBreak) break;
}
if (lineData) {
var attr = 0;
for (var j = 1; j < lineData.length; j++) {
var s = lineData[j];
for (var k = 0; k < s.length; k++) {
this.bus.setByteDirect(addr++, s.charCodeAt(k) | attr);
}
attr ^= 0x80;
}
}
this.bus.setByteDirect(addr++, Video8080.VT100.LINETERM);
addrNext = addr;
}
/*
* NOTE: By calling updateVT100() directly, we are bypassing any checks that might block the update.
*/
this.updateVT100();
}
return true;
}
/**
* powerDown(fSave, fShutdown)
*
* @this {Video8080}
* @param {boolean} [fSave]
* @param {boolean} [fShutdown]
* @return {Object|boolean} component state if fSave; otherwise, true if successful, false if failure
*/
powerDown(fSave, fShutdown)
{
return !fSave || this.save();
}
/**
* save()
*
* This implements save support for the Video8080 component.
*
* @this {Video8080}
* @return {Object|null}
*/
save()
{
var state = new State(this);
state.set(0, []);
return state.data();
}
/**
* restore(data)
*
* This implements restore support for the Video8080 component.
*
* @this {Video8080}
* @param {Object} data
* @return {boolean} true if restore successful, false if not
*/
restore(data)
{
return true;
}
/**
* updateDimensions(nCols, nRows)
*
* Called from the ChipSet component whenever the screen dimensions have been dynamically altered.
*
* @this {Video8080}
* @param {number} nCols (should be either 80 or 132; 80 is the default)
* @param {number} nRows (should be either 24 or 14; 24 is the default)
*/
updateDimensions(nCols, nRows)
{
this.printMessage("updateDimensions(" + nCols + "," + nRows + ")");
this.nColsBuffer = nCols;
/*
* Even when the number of effective rows is 14 (or 15 counting the scroll line buffer), we want
* to leave the number of rows at 24 (or 25 counting the scroll line buffer), because the VT100 doesn't
* actually change character height (only character width).
*
* this.nRowsBuffer = nRows+1; // +1 for scroll line buffer
*/
this.cxCell = this.cxCellDefault;
if (nCols > 80) this.cxCell--; // VT100 font cells are 9x10 instead of 10x10 in 132-column mode
if (this.initBuffers()) {
this.createFonts();
}
}
/**
* updateRate(nRate)
*
* Called from the ChipSet component whenever the monitor refresh rate has been dynamically altered.
*
* @this {Video8080}
* @param {number} nRate (should be either 50 or 60; 60 is the default)
*/
updateRate(nRate)
{
this.printMessage("updateRate(" + nRate + ")");
this.rateMonitor = nRate;
}
/**
* updateScrollOffset(bScroll)
*
* Called from the ChipSet component whenever the screen scroll offset has been dynamically altered.
*
* @this {Video8080}
* @param {number} bScroll
*/
updateScrollOffset(bScroll)
{
this.printMessage("updateScrollOffset(" + bScroll + ")");
if (this.bScrollOffset !== bScroll) {
this.bScrollOffset = bScroll;
/*
* WARNING: If we immediately redraw the screen on the first wrap of the scroll offset back to zero,
* we end up "slamming" the screen's contents back down again, because it seems that the frame buffer
* contents haven't actually been scrolled yet. So we redraw now ONLY if bScroll is non-zero, lest
* we ruin the smooth-scroll effect.
*
* And this change, while necessary, is not sufficient, because another intervening updateScreen()
* call could still occur before the frame buffer contents are actually scrolled; and ordinarily, if the
* buffer hasn't changed, updateScreen() would do nothing, but alas, if the cursor happens to get toggled
* in the interim, updateScreen() will want to update exactly ONE cell.
*
* So we deal with that by setting the fSkipSingleCellUpdate flag. Now of course, there's no guarantee
* that the next update of only ONE cell will always be a cursor update, but even if it isn't, skipping
* that update doesn't seem like a huge cause for concern.
*/
if (bScroll) {
this.updateScreen(true);
} else {
this.fSkipSingleCellUpdate = true;
}
}
}
/**
* doFullScreen()
*
* @this {Video8080}
* @return {boolean} true if request successful, false if not (eg, failed OR not supported)
*/
doFullScreen()
{
var fSuccess = false;
if (this.container) {
if (this.container.doFullScreen) {
/*
* Styling the container with a width of "100%" and a height of "auto" works great when the aspect ratio
* of our virtual screen is at least roughly equivalent to the physical screen's aspect ratio, but now that
* we support virtual VGA screens with an aspect ratio of 1.33, that's very much out of step with modern
* wide-screen monitors, which usually have an aspect ratio of 1.6 or greater.
*
* And unfortunately, none of the browsers I've tested appear to make any attempt to scale our container to
* the physical screen's dimensions, so the bottom of our screen gets clipped. To prevent that, I reduce
* the width from 100% to whatever percentage will accommodate the entire height of the virtual screen.
*
* NOTE: Mozilla recommends both a width and a height of "100%", but all my tests suggest that using "auto"
* for height works equally well, so I'm sticking with it, because "auto" is also consistent with how I've
* implemented a responsive canvas when the browser window is being resized.
*/
var sWidth = "100%";
var sHeight = "auto";
if (screen && screen.width && screen.height) {
var aspectPhys = screen.width / screen.height;
var aspectVirt = this.cxScreen / this.cyScreen;
if (aspectPhys > aspectVirt) {
sWidth = Math.round(aspectVirt / aspectPhys * 100) + '%';
}
// TODO: We may need to someday consider the case of a physical screen with an aspect ratio < 1.0....
}
if (!this.fGecko) {
this.container.style.width = sWidth;
this.container.style.height = sHeight;
} else {
/*
* Sadly, the above code doesn't work for Firefox, because as http://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode
* explains:
*
* 'It's worth noting a key difference here between the Gecko and WebKit implementations at this time:
* Gecko automatically adds CSS rules to the element to stretch it to fill the screen: "width: 100%; height: 100%".
*
* Which would be OK if Gecko did that BEFORE we're called, but apparently it does that AFTER, effectively
* overwriting our careful calculations. So we style the inner element (canvasScreen) instead, which
* requires even more work to ensure that the canvas is properly centered. FYI, this solution is consistent
* with Mozilla's recommendation for working around their automatic CSS rules:
*
* '[I]f you're trying to emulate WebKit's behavior on Gecko, you need to place the element you want
* to present inside another element, which you'll make fullscreen instead, and use CSS rules to adjust
* the inner element to match the appearance you want.'
*/
this.canvasScreen.style.width = sWidth;
this.canvasScreen.style.width = sWidth;
this.canvasScreen.style.display = "block";
this.canvasScreen.style.margin = "auto";
}
this.container.style.backgroundColor = "black";
this.container.doFullScreen();
fSuccess = true;
}
this.setFocus();
}
return fSuccess;
}
/**
* notifyFullScreen(fFullScreen)
*
* @this {Video8080}
* @param {boolean} [fFullScreen] (undefined if there was a full-screen error)
*/
notifyFullScreen(fFullScreen)
{
if (!fFullScreen && this.container) {
if (!this.fGecko) {
this.container.style.width = this.container.style.height = "";
} else {
this.canvasScreen.style.width = this.canvasScreen.style.height = "";
}
}
this.printMessage("notifyFullScreen(" + fFullScreen + ")");
}
/**
* setFocus()
*
* @this {Video8080}
*/
setFocus()
{
if (this.inputScreen) this.inputScreen.focus();
}
/**
* getRefreshTime()
*
* @this {Video8080}
* @return {number} (number of milliseconds per refresh)
*/
getRefreshTime()
{
return 1000 / Math.max(this.rateRefresh, this.rateInterrupt);
}
/**
* initCellCache(nCells)
*
* Initializes the contents of our internal cell cache.
*
* @this {Video8080}
* @param {number} nCells
*/
initCellCache(nCells)
{
this.nCellCache = nCells;
this.fCellCacheValid = false;
if (this.aCellCache === undefined || this.aCellCache.length != this.nCellCache) {
this.aCellCache = new Array(this.nCellCache);
}
}
/**
* initColors()
*
* This creates an array of nColors, with additional OVERLAY_TOTAL colors tacked on to the end of the array.
*
* @this {Video8080}
*/
initColors()
{
var rgbBlack = [0x00, 0x00, 0x00, 0xff];
var rgbWhite = [0xff, 0xff, 0xff, 0xff];
this.nColors = (1 << this.nBitsPerPixel);
this.aRGB = new Array(this.nColors + Video8080.COLORS.OVERLAY_TOTAL);
this.aRGB[0] = rgbBlack;
this.aRGB[1] = rgbWhite;
if (this.nFormat == Video8080.FORMAT.SI1978) {
var rgbGreen = [0x00, 0xff, 0x00, 0xff];
//noinspection UnnecessaryLocalVariableJS
var rgbYellow = [0xff, 0xff, 0x00, 0xff];
this.aRGB[this.nColors + Video8080.COLORS.OVERLAY_TOP] = rgbYellow;
this.aRGB[this.nColors + Video8080.COLORS.OVERLAY_BOTTOM] = rgbGreen;
}
}
/**
* setPixel(image, x, y, bPixel)
*
* @this {Video8080}
* @param {Object} image
* @param {number} x
* @param {number} y
* @param {number} bPixel (ie, an index into aRGB)
*/
setPixel(image, x, y, bPixel)
{
var index;
if (!this.rotateBuffer) {
index = (x + y * image.width);
} else {
index = (image.height - x - 1) * image.width + y;
}
if (bPixel && this.nFormat == Video8080.FORMAT.SI1978) {
if (x >= 208 && x < 236) {
bPixel = this.nColors + Video8080.COLORS.OVERLAY_TOP;
}
else if (x >= 28 && x < 72) {
bPixel = this.nColors + Video8080.COLORS.OVERLAY_BOTTOM;
}
}
var rgb = this.aRGB[bPixel];
index *= rgb.length;
image.data[index] = rgb[0];
image.data[index+1] = rgb[1];
image.data[index+2] = rgb[2];
image.data[index+3] = rgb[3];
}
/**
* updateChar(idFont, col, row, data, context)
*
* Updates a particular character cell (row,col) in the associated window.
*
* @this {Video8080}
* @param {number} idFont