forked from jonbstanley/Tek405xEmulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TekDisplay.js
330 lines (254 loc) · 9.74 KB
/
TekDisplay.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
function TekDisplay(hw, canvas) {
var canvasctx = canvas.getContext("2d");
var width = canvas.width;
var height = canvas.height;
// console.log(canvas.height + "," + canvas.width);
var X_DA = 0;
var Y_DA = 0;
const pixel_erase_inten = 255;
const pixel_store_inten = 240;
const pixel_cursor_inten = 200;
// Adjust position of graphics relative to canvas
const screen_x_offset = 16;
const screen_y_offset = 10;
// ********************
// *** ***
// *** STD VIDEO. ***
// *** ***
// ********************
FGColourIndex = 0x2;
BGColourIndex = 0x0;
// Colours are indexed [0..7] and then [Red,Green,Blue] (where Red, Green and Blue are in the range 0..255).
//
ColourTable = [
[ 0, 0, 0 ], // [0] Black.
[ 0, 0, 255 ], // [1] Blue.
[ 0, 255, 0 ], // [2] Green.
[ 255, 0, 0 ], // [3] Red.
[ 0, 255, 255 ], // [4] Cyan.
[ 255, 0, 255 ], // [5] Magenta.
[ 255, 255, 0 ], // [6] Yellow.
[ 255, 255, 255 ], // [7] White.
];
canvasctx.fillStyle = "rgb("+ColourTable[0][0]+","+ColourTable[0][1]+","+ColourTable[0][2]+")";
canvasctx.fillRect( 0, 0, width, height );
// ****************
// *** ***
// *** VECTOR ***
// *** ***
// ****************
function VECTOR( x0, y0, x1, y1 ) {
var dx = Math.abs( x1 - x0 );
var dy = Math.abs( y1 - y0 );
var sx = -1;
var sy = -1;
if( x0 < x1 ) sx = 1;
if( y0 < y1 ) sy = 1;
var err = dx - dy;
do {
if (VEN_1 == 1) {
// double vector pixel width and height for normal vectors
/*
setPixel( x0+2, y0+2, 'VECTOR' );
setPixel( x0+1, y0+2, 'VECTOR' );
setPixel( x0+2, y0+1, 'VECTOR' );
setPixel( x0+1, y0+1, 'VECTOR' );
*/
setPixel(x0, y0, 'VECTOR', 2 );
} else {
// single vector pixel width and height for refresh vectors
// setPixel( x0+1, y0+1, 'RVECTOR' );
setPixel(x0+1, y0+1, 'RVECTOR', 1 );
}
if( (x0 == x1) && (y0 == y1) ) break;
var e2 = 2 * err;
if( e2 > -dy ) {
err -= dy;
x0 += sx;
} // End if.
if( e2 < dx ) {
err += dx;
y0 += sy;
} // End if.
} while( true );
} // End of function VECTOR.
// xpos, ypos = tektronix display co-ordinates
// Added psize to indicate the pixel size (values 1=1x1 or 2=2x2)
// to solve a FF rendering problem when 'Delete' key is pressed
function setPixel( xpos, ypos, type, pxsize ) {
if (pxsize < 1) pxsize = 1;
// my = height - ypos; // Convert to 'canvas' coordinates from Tektronix coordinates.
cypos = height - (ypos+1); // Both have Zero base. Offset of 1 down required when converting.
switch( type ) {
case 'ERASE' :
setPixelRGB( xpos, cypos, 0, 0, 0, pxsize ); // BLACK
break;
case 'SOT' : // cursor refresh dot
// Do not replace pixel if it already has been stored!
if(canvasctx.getImageData(xpos, cypos, 1, 1).data[1] != pixel_store_inten) {
setPixelRGB( xpos, cypos, 0, pixel_cursor_inten, 0, pxsize ); // DARK GREEN
}
break;
case 'RVECTOR' : // vector refresh dot -- added by mcm for refresh vector support
// Do not replace pixel if it already has been stored!
if(canvasctx.getImageData(xpos, my, 1, 1).data[1] != pixel_store_inten) {
setPixelRGB( xpos, cypos, 0, pixel_cursor_inten, 0, pxsize ); // DARK GREEN
}
break;
case 'ADOT' : // stored character dot
setPixelRGB( xpos, cypos, 0, pixel_store_inten, 0, pxsize ); // BRIGHT GREEN
break;
case 'VECTOR' :
setPixelRGB( xpos, cypos, 0, pixel_store_inten, 0, pxsize ); // BRIGHT GREEN
break;
default :
break;
}
}
// Draws a "pixel"
// cxpos,cypos = canvas co-ordinates
function setPixelRGB( cxpos, cypos, r, g, b, pxsize ) {
canvasctx.fillStyle = "rgb("+r+","+g+","+b+")";
canvasctx.fillRect( cxpos, cypos, pxsize, pxsize );
}
// ****************
// *** ***
// *** SCREEN ***
// *** ***
// ****************
var adotpending = false;
this.SCREEN = function( type ) {
//console.log("TekDisplay SCREEN");
DISP_INFO = hw.getDisplayControl();
X_CHAR = DISP_INFO[0];
Y_CHAR = DISP_INFO[1];
VECTOR_0 = DISP_INFO[2];
VEN_1 = DISP_INFO[3];
new_X = DISP_INFO[4] + screen_x_offset;
new_Y = DISP_INFO[5] + screen_y_offset;
if ( type == 'BUFCLK' ) {
var old_X = X_DA;
var old_Y = Y_DA;
// moved VEN_1 test to function VECTOR to enable persistent AND refresh vectors
if( (VECTOR_0 == 0) )
VECTOR( old_X, old_Y, new_X, new_Y );
// DER 9th August 2014 - Add debug code for drawing vectors.
if( (VEN_1 == 1) && (VECTOR_0 == 0) && 0 ) {
console.log( 'DER: TEKTRONIX4051.js - $$$' );
console.log( ' DRAW VECTOR ' );
console.log( ' old_X = ' ); console.log( old_X );
console.log( ' old_Y = ' ); console.log( old_Y );
console.log( ' new_X = ' ); console.log( new_X );
console.log( ' new_Y = ' ); console.log( new_Y );
console.log( ' VECTOR-0 = ' ); console.log( VECTOR_0 );
console.log( ' VEN-1 = ' ); console.log( VEN_1 );
} // End if.
X_DA = new_X;
Y_DA = new_Y;
} // End if bufclk.
// Check for cursor dot.
if( (type == 'SOT') && (VECTOR_0 == 1) && (VEN_1 == 0) ) {
if( adotpending ) {
//console.log("X_DA: " + X_DA + " Y_DA: " + Y_DA + " X_CHAR: " + X_CHAR + " Y_CHAR: " + Y_CHAR);
// setPixel( X_DA + X_CHAR, Y_DA + Y_CHAR, 'ADOT');
// double width and double height of character pixels
// 4 setPixel calls to interpolate between pixels for continuous font appearance
/*
setPixel( X_DA + ((2*X_CHAR)+2), Y_DA + (2*Y_CHAR), 'ADOT', 1 );
setPixel( X_DA + ((2*X_CHAR)+1), Y_DA + (2*Y_CHAR), 'ADOT', 1 );
setPixel( X_DA + ((2*X_CHAR)+2), Y_DA + ((2*Y_CHAR)-1), 'ADOT', 1 );
setPixel( X_DA + ((2*X_CHAR)+1), Y_DA + ((2*Y_CHAR)-1), 'ADOT', 1 );
*/
setPixel ( X_DA + (2*X_CHAR), Y_DA + (2*Y_CHAR), 'ADOT', 2 );
} else {
// setPixel( X_DA + X_CHAR, Y_DA + Y_CHAR, 'SOT');
// double width and double height of cursor pixels
// 2 setPixel calls instead of 4 because it's pixelated like on actual machine
// setPixel( X_DA + 2*X_CHAR+1, Y_DA + 2*Y_CHAR, 'SOT', 1 );
// setPixel( X_DA + 2*X_CHAR+2, Y_DA + 2*Y_CHAR-1, 'SOT', 1 );
setPixel( X_DA + (2*X_CHAR), Y_DA + (2*Y_CHAR), 'SOT', 1 );
setPixel( X_DA + ((2*X_CHAR)+1), Y_DA + ((2*Y_CHAR)+1), 'SOT', 1 );
}
adotpending = false;
} // End if sot.
// Check for alphanumeric dot.
if( (type == 'ADOT') && (VECTOR_0 == 1) && (VEN_1 == 0) ) {
adotpending = true;
}
// DER - Debug code...
if( 0 ) {
console.log( 'DER: TEKTRONIX4051.js - $$$' );
console.log( ' TYPE = ' ); console.log( type );
console.log( ' X_DA = ' ); console.log( X_DA );
console.log( ' Y_DA = ' ); console.log( Y_DA );
console.log( ' X_CHAR = ' ); console.log( X_CHAR );
console.log( ' Y_CHAR = ' ); console.log( Y_CHAR );
console.log( ' VECTOR-0 = ' ); console.log( VECTOR_0 );
console.log( ' VEN-1 = ' ); console.log( VEN_1 );
} // End if
} // End of function SCREEN.
// ***************
// *** ***
// *** ERASE ***
// *** ***
// ***************
this.ERASE = function() {
//console.log("TekDisplay: Erase screen");
var imgd = canvasctx.getImageData(0, 0, width, height);
var buffer = imgd.data.buffer;
var buf8 = new Uint8ClampedArray(buffer);
var data = new Uint32Array(buffer);
// Fill the screen bright green
for(i = 0; i < data.length; i++) {
// 31:24 = alpha
// 23:16 = blue
// 15:8 = green
// 7:0 = red
data[i] = 0xFF00FF00;
}
imgd.data.set(buf8);
canvasctx.putImageData(imgd, 0, 0);
// Blank the screen
for(i = 0; i < data.length; i++) {
// 31:24 = alpha
// 23:16 = blue
// 15:8 = green
// 7:0 = red
data[i] = 0xFF000000;
}
imgd.data.set(buf8);
// The DVST resets about 500 ms after the screen flash
setTimeout(function(){
canvasctx.putImageData(imgd, 0, 0);
hw.displayReady();
}, 500);
}
this.dvst_emulate = function() {
var imgd = canvasctx.getImageData(0, 0, width, height);
var buffer = imgd.data.buffer;
var buf8 = new Uint8ClampedArray(buffer);
var data = new Uint32Array(buffer);
for(i = 0; i < data.length; i++) {
// 31:24 = alpha
// 23:16 = blue
// 15:8 = green
// 7:0 = red
var pixel = (data[i] >> 8) & 0xFF;
// If the pixel is bright enough for the DVST to store it
// then it will stay that way. If the pixel brightness
// is below the threshold for the DVST to store the pixel
// then its brightness will decay to zero.
if(pixel_store_inten > pixel) {
data[i] = 0xFF000000; // Blank the pixel
}
}
imgd.data.set(buf8);
canvasctx.putImageData(imgd, 0, 0);
}
this.COPY = function() {
var link = document.createElement('a');
link.download = "screen.png";
link.href = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");;
link.click();
}
}