2021-03-15 19:51:40 +00:00
|
|
|
import { byte, Color, memory, MemoryPages, rom } from './types';
|
2020-11-24 16:48:14 +00:00
|
|
|
import { allocMemPages } from './util';
|
2021-02-22 02:38:21 +00:00
|
|
|
import {
|
|
|
|
GraphicsState,
|
|
|
|
HiresPage,
|
|
|
|
LoresPage,
|
|
|
|
Region,
|
|
|
|
VideoModes,
|
|
|
|
VideoModesState,
|
|
|
|
bank,
|
|
|
|
pageNo
|
|
|
|
} from './videomodes';
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
const dim = (c: Color): Color => {
|
2020-11-24 16:48:14 +00:00
|
|
|
return [
|
|
|
|
c[0] * 0.75 & 0xff,
|
|
|
|
c[1] * 0.75 & 0xff,
|
|
|
|
c[2] * 0.75 & 0xff
|
|
|
|
];
|
2021-04-19 02:54:15 +00:00
|
|
|
};
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
// hires colors
|
|
|
|
const orangeCol: Color = [255, 106, 60];
|
|
|
|
const greenCol: Color = [20, 245, 60];
|
|
|
|
const blueCol: Color = [20, 207, 253];
|
|
|
|
const violetCol: Color = [255, 68, 253];
|
|
|
|
const whiteCol: Color = [255, 255, 255];
|
|
|
|
const blackCol: Color = [0, 0, 0];
|
|
|
|
|
|
|
|
// lores colors
|
|
|
|
const _colors: Color[] = [
|
|
|
|
[0, 0, 0], // 0x0 black
|
|
|
|
[227, 30, 96], // 0x1 deep red
|
|
|
|
[96, 78, 189], // 0x2 dark blue
|
|
|
|
[255, 68, 253], // 0x3 purple
|
|
|
|
[0, 163, 96], // 0x4 dark green
|
|
|
|
[156, 156, 156], // 0x5 dark gray
|
|
|
|
[20, 207, 253], // 0x6 medium blue
|
|
|
|
[208, 195, 255], // 0x7 light blue
|
|
|
|
[96, 114, 3], // 0x8 brown
|
|
|
|
[255, 106, 60], // 0x9 orange
|
|
|
|
[156, 156, 156], // 0xa light gray
|
|
|
|
[255, 160, 208], // 0xb pink
|
|
|
|
[20, 245, 60], // 0xc green
|
|
|
|
[208, 221, 141], // 0xd yellow
|
|
|
|
[114, 255, 208], // 0xe aquamarine
|
|
|
|
[255, 255, 255], // 0xf white
|
|
|
|
];
|
|
|
|
|
|
|
|
//
|
|
|
|
const r4 = [
|
|
|
|
0, // Black
|
|
|
|
2, // Dark Blue
|
|
|
|
4, // Dark Green
|
|
|
|
6, // Medium Blue
|
|
|
|
|
|
|
|
8, // Brown
|
|
|
|
5, // Gray 1
|
|
|
|
12, // Light Green
|
|
|
|
14, // Aqua
|
|
|
|
|
|
|
|
1, // Red
|
|
|
|
3, // Purple
|
|
|
|
10, // Gray 2
|
|
|
|
7, // Pink
|
|
|
|
|
|
|
|
9, // Orange
|
|
|
|
11, // Light Blue
|
|
|
|
13, // Yellow
|
|
|
|
15 // White
|
2021-05-25 19:08:10 +00:00
|
|
|
] as const;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
const dcolors: Color[] = [
|
|
|
|
[0, 0, 0], // 0x0 black
|
|
|
|
[227, 30, 96], // 0x1 deep red
|
|
|
|
[96, 78, 189], // 0x2 dark blue
|
|
|
|
[255, 68, 253], // 0x3 purple
|
|
|
|
[0, 163, 96], // 0x4 dark green
|
|
|
|
[156, 156, 156], // 0x5 dark gray
|
|
|
|
[20, 207, 253], // 0x6 medium blue
|
|
|
|
[208, 195, 255], // 0x7 light blue
|
|
|
|
[96, 114, 3], // 0x8 brown
|
|
|
|
[255, 106, 60], // 0x9 orange
|
|
|
|
[156, 156, 156], // 0xa light gray
|
|
|
|
[255, 160, 208], // 0xb pink
|
|
|
|
[20, 245, 60], // 0xc green
|
|
|
|
[208, 221, 141], // 0xd yellow
|
|
|
|
[114, 255, 208], // 0xe aquamarine
|
|
|
|
[255, 255, 255], // 0xf white
|
|
|
|
];
|
|
|
|
|
2021-02-22 02:38:21 +00:00
|
|
|
const notDirty: Region = {
|
2021-04-19 02:54:15 +00:00
|
|
|
top: 193,
|
2021-02-22 02:38:21 +00:00
|
|
|
bottom: -1,
|
|
|
|
left: 561,
|
|
|
|
right: -1
|
2021-05-25 19:08:10 +00:00
|
|
|
} as const;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* Text/Lores Graphics
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2021-02-22 02:38:21 +00:00
|
|
|
export class LoresPage2D implements LoresPage {
|
2020-11-24 16:48:14 +00:00
|
|
|
// $00-$3F inverse
|
|
|
|
// $40-$7F flashing
|
|
|
|
// $80-$FF normal
|
|
|
|
|
|
|
|
private _buffer: memory[] = [];
|
|
|
|
private _refreshing = false;
|
|
|
|
private _blink = false;
|
|
|
|
|
2021-11-29 00:20:25 +00:00
|
|
|
private highColorTextMode = false;
|
2021-05-25 19:08:10 +00:00
|
|
|
|
2022-07-23 22:51:12 +00:00
|
|
|
dirty: Region = { ...notDirty };
|
2021-02-22 02:38:21 +00:00
|
|
|
imageData: ImageData;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
constructor(
|
|
|
|
private vm: VideoModes,
|
|
|
|
private page: pageNo,
|
2021-03-06 23:04:13 +00:00
|
|
|
private readonly charset: rom,
|
2021-05-25 19:08:10 +00:00
|
|
|
private readonly e: boolean
|
|
|
|
) {
|
|
|
|
this.imageData = this.vm.context.createImageData(560, 192);
|
|
|
|
this.imageData.data.fill(0xff);
|
2020-11-24 16:48:14 +00:00
|
|
|
this._buffer[0] = allocMemPages(0x4);
|
|
|
|
this._buffer[1] = allocMemPages(0x4);
|
2021-05-25 19:08:10 +00:00
|
|
|
|
|
|
|
this.vm.setLoresPage(page, this);
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _drawPixel(data: Uint8ClampedArray, off: number, color: Color) {
|
2020-11-24 16:53:43 +00:00
|
|
|
const c0 = color[0], c1 = color[1], c2 = color[2];
|
2020-11-24 16:48:14 +00:00
|
|
|
data[off + 0] = data[off + 4] = c0;
|
|
|
|
data[off + 1] = data[off + 5] = c1;
|
|
|
|
data[off + 2] = data[off + 6] = c2;
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _drawHalfPixel(data: Uint8ClampedArray, off: number, color: Color) {
|
2020-11-24 16:53:43 +00:00
|
|
|
const c0 = color[0], c1 = color[1], c2 = color[2];
|
2020-11-24 16:48:14 +00:00
|
|
|
data[off + 0] = c0;
|
|
|
|
data[off + 1] = c1;
|
|
|
|
data[off + 2] = c2;
|
2021-04-19 02:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private _checkInverse(val: byte) {
|
|
|
|
let inverse = false;
|
|
|
|
if (this.e) {
|
2021-05-25 19:08:10 +00:00
|
|
|
if (!this.vm._80colMode && !this.vm.altCharMode) {
|
2022-05-18 15:19:45 +00:00
|
|
|
inverse = ((val & 0xc0) === 0x40) && this._blink;
|
2021-04-19 02:54:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inverse = !((val & 0x80) || (val & 0x40) && this._blink);
|
|
|
|
}
|
|
|
|
return inverse;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 23:04:13 +00:00
|
|
|
bank0(): MemoryPages {
|
2020-11-24 16:48:14 +00:00
|
|
|
return {
|
|
|
|
start: () => this._start(),
|
|
|
|
end: () => this._end(),
|
|
|
|
read: (page, off) => this._read(page, off, 0),
|
|
|
|
write: (page, off, val) => this._write(page, off, val, 0),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-06 23:04:13 +00:00
|
|
|
bank1(): MemoryPages {
|
2020-11-24 16:48:14 +00:00
|
|
|
return {
|
|
|
|
start: () => this._start(),
|
|
|
|
end: () => this._end(),
|
|
|
|
read: (page, off) => this._read(page, off, 1),
|
|
|
|
write: (page, off, val) => this._write(page, off, val, 1),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are used by both bank 0 and 1
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _start() {
|
2020-11-24 16:48:14 +00:00
|
|
|
return (0x04 * this.page);
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _end() { return (0x04 * this.page) + 0x03; }
|
|
|
|
|
|
|
|
private _read(page: byte, off: byte, bank: bank) {
|
2020-11-24 16:53:43 +00:00
|
|
|
const addr = (page << 8) | off, base = addr & 0x3FF;
|
2020-11-24 16:48:14 +00:00
|
|
|
return this._buffer[bank][base];
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _write(page: byte, off: byte, val: byte, bank: bank) {
|
2020-11-24 16:53:43 +00:00
|
|
|
const addr = (page << 8) | off;
|
|
|
|
const base = addr & 0x3FF;
|
2020-11-24 16:48:14 +00:00
|
|
|
let fore, back;
|
|
|
|
|
2022-05-18 15:19:45 +00:00
|
|
|
if (this._buffer[bank][base] === val && !this._refreshing) {
|
2020-11-24 16:48:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._buffer[bank][base] = val;
|
|
|
|
|
2020-11-24 16:53:43 +00:00
|
|
|
const col = (base % 0x80) % 0x28;
|
|
|
|
const adj = off - col;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
// 000001cd eabab000 -> 000abcde
|
2020-11-24 16:53:43 +00:00
|
|
|
const ab = (adj & 0x18);
|
|
|
|
const cd = (page & 0x03) << 1;
|
|
|
|
const ee = adj >> 7;
|
|
|
|
const row = ab | cd | ee;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-02-22 02:38:21 +00:00
|
|
|
const data = this.imageData.data;
|
2020-11-24 16:48:14 +00:00
|
|
|
if ((row < 24) && (col < 40)) {
|
2021-04-19 02:54:15 +00:00
|
|
|
let y = row << 3;
|
2021-02-22 02:38:21 +00:00
|
|
|
if (y < this.dirty.top) { this.dirty.top = y; }
|
2021-04-19 02:54:15 +00:00
|
|
|
y += 8;
|
2021-02-22 02:38:21 +00:00
|
|
|
if (y > this.dirty.bottom) { this.dirty.bottom = y; }
|
2020-11-24 16:48:14 +00:00
|
|
|
let x = col * 14;
|
2021-02-22 02:38:21 +00:00
|
|
|
if (x < this.dirty.left) { this.dirty.left = x; }
|
2020-11-24 16:48:14 +00:00
|
|
|
x += 14;
|
2021-02-22 02:38:21 +00:00
|
|
|
if (x > this.dirty.right) { this.dirty.right = x; }
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
if (this.vm.textMode || this.vm.hiresMode || (this.vm.mixedMode && row > 19)) {
|
|
|
|
if (this.vm._80colMode) {
|
2021-04-19 02:54:15 +00:00
|
|
|
const inverse = this._checkInverse(val);
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
fore = inverse ? blackCol : whiteCol;
|
|
|
|
back = inverse ? whiteCol : blackCol;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
if (!this.vm.altCharMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
val = (val >= 0x40 && val < 0x80) ? val - 0x40 : val;
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
let offset = (col * 14 + (bank ? 0 : 1) * 7 + row * 560 * 8) * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
for (let jdx = 0; jdx < 8; jdx++) {
|
|
|
|
let b = this.charset[val * 8 + jdx];
|
|
|
|
for (let idx = 0; idx < 7; idx++) {
|
2021-04-19 02:54:15 +00:00
|
|
|
const color = (b & 0x01) ? back : fore;
|
|
|
|
this._drawHalfPixel(data, offset, color);
|
2020-11-24 16:48:14 +00:00
|
|
|
b >>= 1;
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 553 * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
val = this._buffer[0][base];
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
const inverse = this._checkInverse(val);
|
|
|
|
|
|
|
|
fore = inverse ? blackCol : whiteCol;
|
|
|
|
back = inverse ? whiteCol : blackCol;
|
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
if (!this.vm.altCharMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
val = (val >= 0x40 && val < 0x80) ? val - 0x40 : val;
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
let offset = (col * 14 + row * 560 * 8) * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
if (this.highColorTextMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
fore = _colors[this._buffer[1][base] >> 4];
|
|
|
|
back = _colors[this._buffer[1][base] & 0x0f];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.e) {
|
|
|
|
for (let jdx = 0; jdx < 8; jdx++) {
|
|
|
|
let b = this.charset[val * 8 + jdx];
|
|
|
|
for (let idx = 0; idx < 7; idx++) {
|
2021-04-19 02:54:15 +00:00
|
|
|
const color = (b & 0x01) ? back : fore;
|
|
|
|
this._drawPixel(data, offset, color);
|
2020-11-24 16:48:14 +00:00
|
|
|
b >>= 1;
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 8;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 546 * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-05-25 19:08:10 +00:00
|
|
|
const colorMode = this.vm.mixedMode && !this.vm.textMode && !this.vm.monoMode;
|
2020-11-24 16:48:14 +00:00
|
|
|
// var val0 = col > 0 ? _buffer[0][base - 1] : 0;
|
|
|
|
// var val2 = col < 39 ? _buffer[0][base + 1] : 0;
|
|
|
|
|
|
|
|
for (let jdx = 0; jdx < 8; jdx++) {
|
|
|
|
let odd = !(col & 0x1);
|
|
|
|
let b = this.charset[val * 8 + jdx] << 1;
|
|
|
|
if (colorMode) {
|
|
|
|
// var b0 = charset[val0 * 8 + jdx];
|
|
|
|
// var b2 = charset[val2 * 8 + jdx];
|
|
|
|
if (inverse) { b ^= 0x1ff; }
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let idx = 0; idx < 7; idx++) {
|
2021-04-19 02:54:15 +00:00
|
|
|
let color;
|
2020-11-24 16:48:14 +00:00
|
|
|
if (colorMode) {
|
|
|
|
if (b & 0x80) {
|
2022-05-18 15:19:45 +00:00
|
|
|
if ((b & 0x1c0) !== 0x80) {
|
2020-11-24 16:48:14 +00:00
|
|
|
color = whiteCol;
|
|
|
|
} else {
|
|
|
|
color = odd ? violetCol : greenCol;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
color = blackCol;
|
|
|
|
}
|
|
|
|
odd = !odd;
|
|
|
|
} else {
|
|
|
|
color = (b & 0x80) ? fore : back;
|
|
|
|
}
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawPixel(data, offset, color);
|
2020-11-24 16:48:14 +00:00
|
|
|
b <<= 1;
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 8;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 546 * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-25 19:08:10 +00:00
|
|
|
if (this.vm._80colMode && !this.vm.an3State) {
|
2021-04-19 02:54:15 +00:00
|
|
|
let offset = (col * 14 + (bank ? 0 : 1) * 7 + row * 560 * 8) * 4;
|
2021-05-25 19:08:10 +00:00
|
|
|
if (this.vm.monoMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
for (let jdx = 0; jdx < 8; jdx++) {
|
2021-05-25 19:08:10 +00:00
|
|
|
let b = (jdx < 4) ? (val & 0x0f) : (val >> 4);
|
2020-11-24 16:48:14 +00:00
|
|
|
b |= (b << 4);
|
2021-05-25 19:08:10 +00:00
|
|
|
b |= (b << 8);
|
|
|
|
if (col & 0x1) {
|
|
|
|
b >>= 2;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
for (let idx = 0; idx < 7; idx++) {
|
2021-05-25 19:08:10 +00:00
|
|
|
const color = (b & 0x01) ? whiteCol : blackCol;
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, color);
|
2021-05-25 19:08:10 +00:00
|
|
|
b >>= 1;
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 553 * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (bank & 0x1) {
|
|
|
|
val = ((val & 0x77) << 1) | ((val & 0x88) >> 3);
|
|
|
|
}
|
|
|
|
for (let jdx = 0; jdx < 8; jdx++) {
|
2021-04-19 02:54:15 +00:00
|
|
|
const color = _colors[(jdx < 4) ?
|
2020-11-24 16:48:14 +00:00
|
|
|
(val & 0x0f) : (val >> 4)];
|
|
|
|
for (let idx = 0; idx < 7; idx++) {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, color);
|
2021-05-25 19:08:10 +00:00
|
|
|
offset += 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 553 * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-25 19:08:10 +00:00
|
|
|
} else if (bank === 0) {
|
2021-04-19 02:54:15 +00:00
|
|
|
let offset = (col * 14 + row * 560 * 8) * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
if (this.vm.monoMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
for (let jdx = 0; jdx < 8; jdx++) {
|
|
|
|
let b = (jdx < 4) ? (val & 0x0f) : (val >> 4);
|
|
|
|
b |= (b << 4);
|
|
|
|
b |= (b << 8);
|
|
|
|
if (col & 0x1) {
|
2021-05-25 19:08:10 +00:00
|
|
|
b >>= 2;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
for (let idx = 0; idx < 14; idx++) {
|
2021-05-25 19:08:10 +00:00
|
|
|
const color = (b & 0x0001) ? whiteCol : blackCol;
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, color);
|
2021-05-25 19:08:10 +00:00
|
|
|
b >>= 1;
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 546 * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (let jdx = 0; jdx < 8; jdx++) {
|
2021-04-19 02:54:15 +00:00
|
|
|
const color = _colors[(jdx < 4) ? (val & 0x0f) : (val >> 4)];
|
2020-11-24 16:48:14 +00:00
|
|
|
for (let idx = 0; idx < 7; idx++) {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawPixel(data, offset, color);
|
|
|
|
offset += 8;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
2021-04-19 02:54:15 +00:00
|
|
|
offset += 546 * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
2021-05-25 19:08:10 +00:00
|
|
|
this.highColorTextMode = !this.vm.an3State && this.vm.textMode && !this.vm._80colMode;
|
|
|
|
|
2020-11-24 16:48:14 +00:00
|
|
|
let addr = 0x400 * this.page;
|
|
|
|
this._refreshing = true;
|
|
|
|
for (let idx = 0; idx < 0x400; idx++, addr++) {
|
|
|
|
this._write(addr >> 8, addr & 0xff, this._buffer[0][idx], 0);
|
2021-05-25 19:08:10 +00:00
|
|
|
if (this.vm._80colMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
this._write(addr >> 8, addr & 0xff, this._buffer[1][idx], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._refreshing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
blink() {
|
|
|
|
let addr = 0x400 * this.page;
|
|
|
|
this._refreshing = true;
|
|
|
|
this._blink = !this._blink;
|
|
|
|
for (let idx = 0; idx < 0x400; idx++, addr++) {
|
2020-11-24 16:53:43 +00:00
|
|
|
const b = this._buffer[0][idx];
|
2022-05-18 15:19:45 +00:00
|
|
|
if ((b & 0xC0) === 0x40) {
|
2020-11-24 16:48:14 +00:00
|
|
|
this._write(addr >> 8, addr & 0xff, this._buffer[0][idx], 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._refreshing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
setInterval(() => this.blink(), 267);
|
|
|
|
return this._start();
|
|
|
|
}
|
|
|
|
|
|
|
|
end() {
|
|
|
|
return this._end();
|
|
|
|
}
|
|
|
|
|
|
|
|
read(page: byte, off: byte) {
|
|
|
|
return this._read(page, off, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
write(page: byte, off: byte, val: byte) {
|
|
|
|
return this._write(page, off, val, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
getState(): GraphicsState {
|
|
|
|
return {
|
|
|
|
buffer: [
|
2021-02-28 03:17:36 +00:00
|
|
|
new Uint8Array(this._buffer[0]),
|
|
|
|
new Uint8Array(this._buffer[1]),
|
2020-11-24 16:48:14 +00:00
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
2021-04-19 02:54:15 +00:00
|
|
|
|
2020-11-24 16:48:14 +00:00
|
|
|
setState(state: GraphicsState) {
|
2021-02-28 03:17:36 +00:00
|
|
|
this._buffer[0] = new Uint8Array(state.buffer[0]);
|
|
|
|
this._buffer[1] = new Uint8Array(state.buffer[1]);
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private rowToBase(row: number) {
|
|
|
|
const ab = (row >> 3) & 3;
|
|
|
|
const cd = (row >> 1) & 0x3;
|
|
|
|
const e = row & 1;
|
|
|
|
return (cd << 8) | (e << 7) | (ab << 5) | (ab << 3);
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:48:14 +00:00
|
|
|
private mapCharCode(charCode: byte) {
|
|
|
|
charCode &= 0x7F;
|
|
|
|
if (charCode < 0x20) {
|
|
|
|
charCode += 0x40;
|
|
|
|
}
|
|
|
|
if (!this.e && (charCode >= 0x60)) {
|
|
|
|
charCode -= 0x40;
|
|
|
|
}
|
|
|
|
return charCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
getText() {
|
2020-11-24 16:53:43 +00:00
|
|
|
let buffer = '', line, charCode;
|
|
|
|
let row, col, base;
|
2020-11-24 16:48:14 +00:00
|
|
|
for (row = 0; row < 24; row++) {
|
2021-04-19 02:54:15 +00:00
|
|
|
base = this.rowToBase(row);
|
2020-11-24 16:48:14 +00:00
|
|
|
line = '';
|
2021-05-25 19:08:10 +00:00
|
|
|
if (this.e && this.vm._80colMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
for (col = 0; col < 80; col++) {
|
|
|
|
charCode = this.mapCharCode(this._buffer[1 - col % 2][base + Math.floor(col / 2)]);
|
|
|
|
line += String.fromCharCode(charCode);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (col = 0; col < 40; col++) {
|
|
|
|
charCode = this.mapCharCode(this._buffer[0][base + col]);
|
|
|
|
line += String.fromCharCode(charCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line = line.trimRight();
|
|
|
|
buffer += line + '\n';
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* Hires Graphics
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2021-02-22 02:38:21 +00:00
|
|
|
export class HiresPage2D implements HiresPage {
|
|
|
|
public imageData: ImageData;
|
2022-07-23 22:51:12 +00:00
|
|
|
dirty: Region = { ...notDirty };
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
private _buffer: memory[] = [];
|
|
|
|
private _refreshing = false;
|
2021-05-25 19:08:10 +00:00
|
|
|
|
|
|
|
highColorHGRMode: boolean;
|
|
|
|
oneSixtyMode: boolean;
|
|
|
|
mixedDHRMode: boolean;
|
|
|
|
monoDHRMode: boolean;
|
|
|
|
colorDHRMode: boolean = true;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
constructor(
|
2021-05-25 19:08:10 +00:00
|
|
|
private vm: VideoModes,
|
|
|
|
private page: pageNo,
|
|
|
|
) {
|
|
|
|
this.imageData = this.vm.context.createImageData(560, 192);
|
|
|
|
this.imageData.data.fill(0xff);
|
2020-11-24 16:48:14 +00:00
|
|
|
this._buffer[0] = allocMemPages(0x20);
|
|
|
|
this._buffer[1] = allocMemPages(0x20);
|
2021-05-25 19:08:10 +00:00
|
|
|
|
|
|
|
this.vm.setHiresPage(page, this);
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _drawPixel(data: Uint8ClampedArray, off: number, color: Color) {
|
2020-11-24 16:53:43 +00:00
|
|
|
const c0 = color[0], c1 = color[1], c2 = color[2];
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
data[off + 0] = data[off + 4] = c0;
|
|
|
|
data[off + 1] = data[off + 5] = c1;
|
|
|
|
data[off + 2] = data[off + 6] = c2;
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _drawHalfPixel(data: Uint8ClampedArray, off: number, color: Color) {
|
2020-11-24 16:53:43 +00:00
|
|
|
const c0 = color[0], c1 = color[1], c2 = color[2];
|
2021-04-19 02:54:15 +00:00
|
|
|
|
2020-11-24 16:48:14 +00:00
|
|
|
data[off + 0] = c0;
|
|
|
|
data[off + 1] = c1;
|
|
|
|
data[off + 2] = c2;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 160x192 pixels alternate 3 and 4 base pixels wide
|
|
|
|
//
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _draw3Pixel(data: Uint8ClampedArray, off: number, color: Color) {
|
2020-11-24 16:53:43 +00:00
|
|
|
const c0 = color[0], c1 = color[1], c2 = color[2];
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
data[off + 0] = data[off + 4] = data[off + 8] = c0;
|
|
|
|
data[off + 1] = data[off + 5] = data[off + 9] = c1;
|
|
|
|
data[off + 2] = data[off + 6] = data[off + 10] = c2;
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _draw4Pixel(data: Uint8ClampedArray, off: number, color: Color) {
|
2020-11-24 16:53:43 +00:00
|
|
|
const c0 = color[0], c1 = color[1], c2 = color[2];
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
data[off + 0] = data[off + 4] = data[off + 8] = data[off + 12] = c0;
|
|
|
|
data[off + 1] = data[off + 5] = data[off + 9] = data[off + 13] = c1;
|
|
|
|
data[off + 2] = data[off + 6] = data[off + 10] = data[off + 14] = c2;
|
|
|
|
}
|
|
|
|
|
2021-03-06 23:04:13 +00:00
|
|
|
bank0(): MemoryPages {
|
2020-11-24 16:48:14 +00:00
|
|
|
return {
|
|
|
|
start: () => this._start(),
|
|
|
|
end: () => this._end(),
|
|
|
|
read: (page, off) => this._read(page, off, 0),
|
|
|
|
write: (page, off, val) => this._write(page, off, val, 0),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-06 23:04:13 +00:00
|
|
|
bank1(): MemoryPages {
|
2020-11-24 16:48:14 +00:00
|
|
|
return {
|
|
|
|
start: () => this._start(),
|
|
|
|
end: () => this._end(),
|
|
|
|
read: (page, off) => this._read(page, off, 1),
|
|
|
|
write: (page, off, val) => this._write(page, off, val, 1),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _start() { return (0x20 * this.page); }
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _end() { return (0x020 * this.page) + 0x1f; }
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _read(page: byte, off: byte, bank: bank) {
|
2020-11-24 16:53:43 +00:00
|
|
|
const addr = (page << 8) | off, base = addr & 0x1FFF;
|
2020-11-24 16:48:14 +00:00
|
|
|
return this._buffer[bank][base];
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
private _write(page: byte, off: byte, val: byte, bank: bank) {
|
2020-11-24 16:53:43 +00:00
|
|
|
const addr = (page << 8) | off;
|
|
|
|
const base = addr & 0x1FFF;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2022-05-18 15:19:45 +00:00
|
|
|
if (this._buffer[bank][base] === val && !this._refreshing) {
|
2020-11-24 16:48:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._buffer[bank][base] = val;
|
|
|
|
|
2020-11-24 16:53:43 +00:00
|
|
|
const col = (base % 0x80) % 0x28;
|
|
|
|
const adj = off - col;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
// 000001cd eabab000 -> 000abcde
|
2020-11-24 16:53:43 +00:00
|
|
|
const ab = (adj & 0x18);
|
|
|
|
const cd = (page & 0x03) << 1;
|
|
|
|
const e = adj >> 7;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2020-11-24 16:53:43 +00:00
|
|
|
const rowa = ab | cd | e,
|
2020-11-24 16:48:14 +00:00
|
|
|
rowb = base >> 10;
|
|
|
|
|
2021-02-22 02:38:21 +00:00
|
|
|
const data = this.imageData.data;
|
2020-11-24 16:53:43 +00:00
|
|
|
let dx, dy;
|
2022-07-14 03:34:50 +00:00
|
|
|
if ((rowa < 24) && (col < 40) && (this.vm.hiresMode || this._refreshing)) {
|
2020-11-24 16:48:14 +00:00
|
|
|
let y = rowa << 4 | rowb << 1;
|
2021-02-22 02:38:21 +00:00
|
|
|
if (y < this.dirty.top) { this.dirty.top = y; }
|
2021-04-19 02:54:15 +00:00
|
|
|
y += 1;
|
2021-02-22 02:38:21 +00:00
|
|
|
if (y > this.dirty.bottom) { this.dirty.bottom = y; }
|
2020-11-24 16:48:14 +00:00
|
|
|
let x = col * 14 - 2;
|
2021-02-22 02:38:21 +00:00
|
|
|
if (x < this.dirty.left) { this.dirty.left = x; }
|
2020-11-24 16:48:14 +00:00
|
|
|
x += 18;
|
2021-02-22 02:38:21 +00:00
|
|
|
if (x > this.dirty.right) { this.dirty.right = x; }
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
dy = rowa << 4 | rowb << 1;
|
2022-07-23 22:51:12 +00:00
|
|
|
let bz, b0, b1, b2, b3, b4, c;
|
2021-05-25 19:08:10 +00:00
|
|
|
if (this.oneSixtyMode && !this.vm.monoMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
// 1 byte = two pixels, but 3:4 ratio
|
2020-11-24 16:53:43 +00:00
|
|
|
const c3 = val & 0xf;
|
|
|
|
const c4 = val >> 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
dx = col * 2 + (bank ^ 1);
|
2021-04-19 02:54:15 +00:00
|
|
|
const offset = dx * 28 + dy * 280 * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
this._draw3Pixel(data, offset, dcolors[c3]);
|
|
|
|
this._draw4Pixel(data, offset + 12, dcolors[c4]);
|
2021-05-25 19:08:10 +00:00
|
|
|
} else if (this.vm.doubleHiresMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
val &= 0x7f;
|
|
|
|
|
|
|
|
// Every 4 bytes is 7 pixels
|
|
|
|
// 2 bytes per bank
|
|
|
|
|
|
|
|
// b0 b1 b2 b3
|
|
|
|
// c0 c1 c2 c3 c4 c5 c6
|
|
|
|
// 76543210 76543210 76543210 76543210
|
|
|
|
// 1111222 2333344 4455556 6667777
|
|
|
|
|
2020-11-24 16:53:43 +00:00
|
|
|
const mod = col % 2, mcol = col - mod, baseOff = base - mod;
|
2020-11-24 16:48:14 +00:00
|
|
|
bz = this._buffer[0][baseOff - 1];
|
|
|
|
b0 = this._buffer[1][baseOff];
|
|
|
|
b1 = this._buffer[0][baseOff];
|
|
|
|
b2 = this._buffer[1][baseOff + 1];
|
|
|
|
b3 = this._buffer[0][baseOff + 1];
|
|
|
|
b4 = this._buffer[1][baseOff + 2];
|
|
|
|
c = [
|
|
|
|
0,
|
|
|
|
((b0 & 0x0f) >> 0), // 0
|
|
|
|
((b0 & 0x70) >> 4) | ((b1 & 0x01) << 3), // 1
|
|
|
|
((b1 & 0x1e) >> 1), // 2
|
|
|
|
((b1 & 0x60) >> 5) | ((b2 & 0x03) << 2), // 3
|
|
|
|
((b2 & 0x3c) >> 2), // 4
|
|
|
|
((b2 & 0x40) >> 6) | ((b3 & 0x07) << 1), // 5
|
|
|
|
((b3 & 0x78) >> 3), // 6
|
|
|
|
0
|
2022-07-23 22:51:12 +00:00
|
|
|
]; // 7
|
|
|
|
const hb = [
|
2020-11-24 16:53:43 +00:00
|
|
|
0,
|
|
|
|
b0 & 0x80, // 0
|
|
|
|
b0 & 0x80, // 1
|
|
|
|
b1 & 0x80, // 2
|
|
|
|
b2 & 0x80, // 3
|
|
|
|
b2 & 0x80, // 4
|
|
|
|
b3 & 0x80, // 5
|
|
|
|
b3 & 0x80, // 6
|
|
|
|
0
|
|
|
|
]; // 7
|
2020-11-24 16:48:14 +00:00
|
|
|
if (col > 0) {
|
|
|
|
c[0] = (bz & 0x78) >> 3;
|
|
|
|
hb[0] = bz & 0x80;
|
|
|
|
}
|
|
|
|
if (col < 39) {
|
|
|
|
c[8] = b4 & 0x0f;
|
|
|
|
hb[8] = b4 & 0x80;
|
|
|
|
}
|
|
|
|
dx = mcol * 14;
|
2021-04-19 02:54:15 +00:00
|
|
|
let offset = dx * 4 + dy * 280 * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
let monoColor = null;
|
2021-05-25 19:08:10 +00:00
|
|
|
if (this.vm.monoMode || this.monoDHRMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
monoColor = whiteCol;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let idx = 1; idx < 8; idx++) {
|
2021-04-19 02:54:15 +00:00
|
|
|
const hbs = hb[idx];
|
2020-11-24 16:53:43 +00:00
|
|
|
const dcolor = dcolors[r4[c[idx]]];
|
2020-11-24 16:48:14 +00:00
|
|
|
let bits = c[idx - 1] | (c[idx] << 4) | (c[idx + 1] << 8);
|
2021-04-19 02:54:15 +00:00
|
|
|
for (let jdx = 0; jdx < 4; jdx++, offset += 4) {
|
2020-11-24 16:48:14 +00:00
|
|
|
if (monoColor) {
|
|
|
|
if (bits & 0x10) {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, monoColor);
|
2020-11-24 16:48:14 +00:00
|
|
|
} else {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, blackCol);
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
2021-05-25 19:08:10 +00:00
|
|
|
} else if (this.mixedDHRMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
if (hbs) {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, dcolor);
|
2020-11-24 16:48:14 +00:00
|
|
|
} else {
|
|
|
|
if (bits & 0x10) {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, whiteCol);
|
2020-11-24 16:48:14 +00:00
|
|
|
} else {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, blackCol);
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-25 19:08:10 +00:00
|
|
|
} else if (this.colorDHRMode) {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, dcolor);
|
2020-11-24 16:48:14 +00:00
|
|
|
} else if (
|
2022-05-18 15:19:45 +00:00
|
|
|
((c[idx] !== c[idx - 1]) && (c[idx] !== c[idx + 1])) &&
|
|
|
|
(((bits & 0x1c) === 0x1c) ||
|
|
|
|
((bits & 0x70) === 0x70) ||
|
|
|
|
((bits & 0x38) === 0x38))
|
2020-11-24 16:48:14 +00:00
|
|
|
) {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, whiteCol);
|
2020-11-24 16:48:14 +00:00
|
|
|
} else if (
|
|
|
|
(bits & 0x38) ||
|
2022-05-18 15:19:45 +00:00
|
|
|
(c[idx] === c[idx + 1]) ||
|
|
|
|
(c[idx] === c[idx - 1])
|
2020-11-24 16:48:14 +00:00
|
|
|
) {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, dcolor);
|
2020-11-24 16:48:14 +00:00
|
|
|
} else if (bits & 0x28) {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, dim(dcolor));
|
2020-11-24 16:48:14 +00:00
|
|
|
} else {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawHalfPixel(data, offset, blackCol);
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
bits >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._refreshing) {
|
|
|
|
this._refreshing = true;
|
2020-11-24 16:53:43 +00:00
|
|
|
const bb: bank = bank ? 0 : 1;
|
2020-11-24 16:48:14 +00:00
|
|
|
for (let rr = addr - 1; rr <= addr + 1; rr++) {
|
2021-03-07 02:35:55 +00:00
|
|
|
const vv = this._read(rr >> 8, rr & 0xff, bb);
|
2020-11-24 16:48:14 +00:00
|
|
|
this._write(rr >> 8, rr & 0xff, vv, bb);
|
|
|
|
}
|
|
|
|
this._refreshing = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
val = this._buffer[0][base];
|
2021-04-19 02:54:15 +00:00
|
|
|
const hbs = val & 0x80;
|
2020-11-24 16:48:14 +00:00
|
|
|
val &= 0x7f;
|
|
|
|
dx = col * 14 - 2;
|
|
|
|
b0 = col > 0 ? this._buffer[0][base - 1] : 0;
|
|
|
|
b2 = col < 39 ? this._buffer[0][base + 1] : 0;
|
|
|
|
val |= (b2 & 0x3) << 7;
|
2020-11-24 16:53:43 +00:00
|
|
|
let v0 = b0 & 0x20, v1 = b0 & 0x40, v2 = val & 0x1,
|
2020-11-24 16:48:14 +00:00
|
|
|
odd = !(col & 0x1),
|
2020-11-24 16:53:43 +00:00
|
|
|
color;
|
|
|
|
const oddCol = (hbs ? orangeCol : greenCol);
|
|
|
|
const evenCol = (hbs ? blueCol : violetCol);
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
let offset = dx * 4 + dy * 280 * 4;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
const monoColor = this.vm.monoMode ? whiteCol : null;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
for (let idx = 0; idx < 9; idx++, offset += 8) {
|
2020-11-24 16:48:14 +00:00
|
|
|
val >>= 1;
|
|
|
|
|
|
|
|
if (v1) {
|
|
|
|
if (monoColor) {
|
|
|
|
color = monoColor;
|
2021-05-25 19:08:10 +00:00
|
|
|
} else if (this.highColorHGRMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
color = dcolors[this._buffer[1][base] >> 4];
|
|
|
|
} else if (v0 || v2) {
|
|
|
|
color = whiteCol;
|
|
|
|
} else {
|
|
|
|
color = odd ? oddCol : evenCol;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (monoColor) {
|
|
|
|
color = blackCol;
|
2021-05-25 19:08:10 +00:00
|
|
|
} else if (this.highColorHGRMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
color = dcolors[this._buffer[1][base] & 0x0f];
|
|
|
|
} else if (odd && v2 && v0) {
|
|
|
|
color = v0 ? dim(evenCol) : evenCol;
|
|
|
|
} else if (!odd && v0 && v2) {
|
|
|
|
color = v2 ? dim(oddCol) : oddCol;
|
|
|
|
} else {
|
|
|
|
color = blackCol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dx > -1 && dx < 560) {
|
2021-04-19 02:54:15 +00:00
|
|
|
this._drawPixel(data, offset, color);
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
dx += 2;
|
|
|
|
|
|
|
|
v0 = v1;
|
|
|
|
v1 = v2;
|
|
|
|
v2 = val & 0x01;
|
|
|
|
odd = !odd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
2021-05-25 19:08:10 +00:00
|
|
|
this.highColorHGRMode = !this.vm.an3State && this.vm.hiresMode && !this.vm._80colMode;
|
2022-05-18 15:19:45 +00:00
|
|
|
this.oneSixtyMode = this.vm.flag === 1 && this.vm.doubleHiresMode;
|
|
|
|
this.mixedDHRMode = this.vm.flag === 2 && this.vm.doubleHiresMode;
|
|
|
|
this.monoDHRMode = this.vm.flag === 3 && this.vm.doubleHiresMode;
|
2021-05-25 19:08:10 +00:00
|
|
|
|
2020-11-24 16:48:14 +00:00
|
|
|
let addr = 0x2000 * this.page;
|
|
|
|
this._refreshing = true;
|
|
|
|
for (let idx = 0; idx < 0x2000; idx++, addr++) {
|
|
|
|
const page = addr >> 8;
|
|
|
|
const off = addr & 0xff;
|
|
|
|
this._write(page, off, this._buffer[0][idx], 0);
|
2021-05-25 19:08:10 +00:00
|
|
|
if (this.vm._80colMode) {
|
2020-11-24 16:48:14 +00:00
|
|
|
this._write(page, off, this._buffer[1][idx], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._refreshing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
return this._start();
|
|
|
|
}
|
|
|
|
|
|
|
|
end() {
|
|
|
|
return this._end();
|
|
|
|
}
|
|
|
|
|
|
|
|
read(page: byte, off: byte) {
|
|
|
|
return this._read(page, off, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
write(page: byte, off: byte, val: byte) {
|
|
|
|
return this._write(page, off, val, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
getState(): GraphicsState {
|
|
|
|
return {
|
|
|
|
buffer: [
|
2021-02-28 03:17:36 +00:00
|
|
|
new Uint8Array(this._buffer[0]),
|
|
|
|
new Uint8Array(this._buffer[1]),
|
2020-11-24 16:48:14 +00:00
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(state: GraphicsState) {
|
2021-02-28 03:17:36 +00:00
|
|
|
this._buffer[0] = new Uint8Array(state.buffer[0]);
|
|
|
|
this._buffer[1] = new Uint8Array(state.buffer[1]);
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:38:21 +00:00
|
|
|
export class VideoModes2D implements VideoModes {
|
2021-05-25 19:08:10 +00:00
|
|
|
private _grs: LoresPage[] = [];
|
|
|
|
private _hgrs: HiresPage[] = [];
|
|
|
|
private _screenContext: CanvasRenderingContext2D;
|
|
|
|
private _canvas: HTMLCanvasElement;
|
2021-02-22 02:38:21 +00:00
|
|
|
private _left: number;
|
|
|
|
private _top: number;
|
2021-04-21 01:24:47 +00:00
|
|
|
private _refreshFlag: boolean = true;
|
2021-02-22 02:38:21 +00:00
|
|
|
|
|
|
|
public ready = Promise.resolve();
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
textMode: boolean;
|
|
|
|
mixedMode: boolean;
|
|
|
|
hiresMode: boolean;
|
|
|
|
pageMode: pageNo;
|
|
|
|
_80colMode: boolean;
|
|
|
|
altCharMode: boolean;
|
|
|
|
an3State: boolean;
|
|
|
|
doubleHiresMode: boolean;
|
|
|
|
|
|
|
|
flag = 0;
|
|
|
|
monoMode = false;
|
|
|
|
|
|
|
|
context: CanvasRenderingContext2D;
|
|
|
|
|
2020-11-24 16:48:14 +00:00
|
|
|
constructor(
|
2021-05-25 19:08:10 +00:00
|
|
|
private screen: HTMLCanvasElement,
|
|
|
|
private e: boolean
|
|
|
|
) {
|
|
|
|
this._canvas = document.createElement('canvas');
|
|
|
|
const context = this._canvas.getContext('2d');
|
|
|
|
const screenContext = this.screen.getContext('2d');
|
|
|
|
if (!context || !screenContext) {
|
|
|
|
throw new Error('No 2d context');
|
|
|
|
}
|
|
|
|
this.context = context;
|
|
|
|
|
|
|
|
const { width, height } = { width: 560, height: 192 };
|
|
|
|
this._canvas.width = width;
|
|
|
|
this._canvas.height = height;
|
|
|
|
|
|
|
|
this._screenContext = screenContext;
|
2021-09-27 21:38:04 +00:00
|
|
|
this._screenContext.imageSmoothingEnabled = false;
|
2021-05-25 19:08:10 +00:00
|
|
|
this._left = (this.screen.width - 560) / 2;
|
|
|
|
this._top = (this.screen.height - 384) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
_refresh() {
|
|
|
|
this.doubleHiresMode = !this.an3State && this.hiresMode && this._80colMode;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-04-21 01:24:47 +00:00
|
|
|
this._refreshFlag = true;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
this._refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
2021-05-25 19:08:10 +00:00
|
|
|
this.textMode = true;
|
|
|
|
this.mixedMode = false;
|
|
|
|
this.hiresMode = true;
|
|
|
|
this.pageMode = 1;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
this._80colMode = false;
|
|
|
|
this.altCharMode = false;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
this.flag = 0;
|
|
|
|
this.an3State = true;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
this._refresh();
|
|
|
|
}
|
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
setLoresPage(page: pageNo, lores: LoresPage) {
|
|
|
|
this._grs[page - 1] = lores;
|
|
|
|
}
|
|
|
|
|
|
|
|
setHiresPage(page: pageNo, hires: HiresPage) {
|
|
|
|
this._hgrs[page - 1] = hires;
|
|
|
|
}
|
|
|
|
|
2022-07-14 03:34:50 +00:00
|
|
|
getLoresPage(page: pageNo) {
|
|
|
|
return this._grs[page - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
getHiresPage(page: pageNo) {
|
|
|
|
return this._hgrs[page - 1];
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:48:14 +00:00
|
|
|
text(on: boolean) {
|
2021-05-25 19:08:10 +00:00
|
|
|
const old = this.textMode;
|
|
|
|
this.textMode = on;
|
2021-03-14 22:15:54 +00:00
|
|
|
if (on) {
|
2021-05-25 19:08:10 +00:00
|
|
|
this.flag = 0;
|
2021-03-14 22:15:54 +00:00
|
|
|
}
|
2022-05-18 15:19:45 +00:00
|
|
|
if (old !== on) {
|
2020-11-24 16:48:14 +00:00
|
|
|
this._refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_80col(on: boolean) {
|
|
|
|
if (!this.e) { return; }
|
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
const old = this._80colMode;
|
|
|
|
this._80colMode = on;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2022-05-18 15:19:45 +00:00
|
|
|
if (old !== on) {
|
2020-11-24 16:48:14 +00:00
|
|
|
this._refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
altChar(on: boolean) {
|
2020-11-24 16:48:14 +00:00
|
|
|
if (!this.e) { return; }
|
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
const old = this.altCharMode;
|
|
|
|
this.altCharMode = on;
|
2022-05-18 15:19:45 +00:00
|
|
|
if (old !== on) {
|
2020-11-24 16:48:14 +00:00
|
|
|
this._refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hires(on: boolean) {
|
2021-05-25 19:08:10 +00:00
|
|
|
const old = this.hiresMode;
|
|
|
|
this.hiresMode = on;
|
2021-03-14 22:15:54 +00:00
|
|
|
if (!on) {
|
2021-05-25 19:08:10 +00:00
|
|
|
this.flag = 0;
|
2021-03-14 22:15:54 +00:00
|
|
|
}
|
2020-11-24 16:48:14 +00:00
|
|
|
|
2022-05-18 15:19:45 +00:00
|
|
|
if (old !== on) {
|
2020-11-24 16:48:14 +00:00
|
|
|
this._refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
an3(on: boolean) {
|
|
|
|
if (!this.e) { return; }
|
|
|
|
|
2021-05-25 19:08:10 +00:00
|
|
|
const old = this.an3State;
|
|
|
|
this.an3State = on;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
if (on) {
|
2021-05-25 19:08:10 +00:00
|
|
|
this.flag = ((this.flag << 1) | (this._80colMode ? 0x0 : 0x1)) & 0x3;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:19:45 +00:00
|
|
|
if (old !== on) {
|
2020-11-24 16:48:14 +00:00
|
|
|
this._refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doubleHires(on: boolean) {
|
|
|
|
this.an3(!on);
|
|
|
|
}
|
|
|
|
|
|
|
|
mixed(on: boolean) {
|
2021-05-25 19:08:10 +00:00
|
|
|
const old = this.mixedMode;
|
|
|
|
this.mixedMode = on;
|
2022-05-18 15:19:45 +00:00
|
|
|
if (old !== on) {
|
2020-11-24 16:48:14 +00:00
|
|
|
this._refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
page(pageNo: pageNo) {
|
2021-05-25 19:08:10 +00:00
|
|
|
const old = this.pageMode;
|
|
|
|
this.pageMode = pageNo;
|
2022-05-18 15:19:45 +00:00
|
|
|
if (old !== pageNo) {
|
2020-11-24 16:48:14 +00:00
|
|
|
this._refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isText() {
|
2021-05-25 19:08:10 +00:00
|
|
|
return this.textMode;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isMixed() {
|
2021-05-25 19:08:10 +00:00
|
|
|
return this.mixedMode;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isPage2() {
|
2022-05-18 15:19:45 +00:00
|
|
|
return this.pageMode === 2;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isHires() {
|
2021-05-25 19:08:10 +00:00
|
|
|
return this.hiresMode;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isDoubleHires() {
|
2021-05-25 19:08:10 +00:00
|
|
|
return this.doubleHiresMode;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
is80Col() {
|
2021-05-25 19:08:10 +00:00
|
|
|
return this._80colMode;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isAltChar() {
|
2021-05-25 19:08:10 +00:00
|
|
|
return this.altCharMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildScreen(mainData: ImageData, mixData?: ImageData | null) {
|
|
|
|
// TODO(whscullin): - figure out 80 column offset
|
|
|
|
const { x, y } = this._80colMode ? { x: 0, y: 0 } : { x: 0, y: 0 };
|
|
|
|
|
|
|
|
if (mixData) {
|
|
|
|
this.context.putImageData(mainData, x, y, 0, 0, 560, 160);
|
|
|
|
this.context.putImageData(mixData, x, y, 0, 160, 560, 32);
|
|
|
|
} else {
|
|
|
|
this.context.putImageData(mainData, x, y);
|
|
|
|
}
|
|
|
|
return this._canvas;
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:38:21 +00:00
|
|
|
updateImage(
|
|
|
|
mainData: ImageData,
|
|
|
|
mainDirty: Region,
|
|
|
|
mixData?: ImageData | null,
|
|
|
|
mixDirty?: Region | null
|
|
|
|
) {
|
2021-04-19 02:54:15 +00:00
|
|
|
let blitted = false;
|
|
|
|
|
2021-02-22 02:38:21 +00:00
|
|
|
if (mainDirty.bottom !== -1 || (mixDirty && mixDirty.bottom !== -1)) {
|
2021-05-25 19:08:10 +00:00
|
|
|
const imageData = this.buildScreen(mainData, mixData);
|
|
|
|
this._screenContext.drawImage(
|
2021-04-19 02:54:15 +00:00
|
|
|
imageData,
|
|
|
|
0, 0, 560, 192,
|
|
|
|
this._left, this._top, 560, 384
|
|
|
|
);
|
|
|
|
blitted = true;
|
2021-02-22 02:38:21 +00:00
|
|
|
}
|
2021-04-19 02:54:15 +00:00
|
|
|
return blitted;
|
2021-02-22 02:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blit(altData?: ImageData) {
|
|
|
|
let blitted = false;
|
2021-05-25 19:08:10 +00:00
|
|
|
const hgr = this._hgrs[this.pageMode - 1];
|
|
|
|
const gr = this._grs[this.pageMode - 1];
|
2021-02-22 02:38:21 +00:00
|
|
|
|
2021-04-21 01:24:47 +00:00
|
|
|
if (this._refreshFlag) {
|
|
|
|
hgr.refresh();
|
|
|
|
gr.refresh();
|
|
|
|
this._refreshFlag = false;
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:38:21 +00:00
|
|
|
if (altData) {
|
|
|
|
blitted = this.updateImage(
|
|
|
|
altData,
|
2021-04-19 02:54:15 +00:00
|
|
|
{ top: 0, left: 0, right: 560, bottom: 192 }
|
2021-02-22 02:38:21 +00:00
|
|
|
);
|
2021-05-25 19:08:10 +00:00
|
|
|
} else if (this.hiresMode && !this.textMode) {
|
2021-02-22 02:38:21 +00:00
|
|
|
blitted = this.updateImage(
|
|
|
|
hgr.imageData,
|
|
|
|
hgr.dirty,
|
2021-05-25 19:08:10 +00:00
|
|
|
this.mixedMode ? gr.imageData : null,
|
|
|
|
this.mixedMode ? gr.dirty : null
|
2021-02-22 02:38:21 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
blitted = this.updateImage(
|
|
|
|
gr.imageData, gr.dirty
|
|
|
|
);
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
2022-07-23 22:51:12 +00:00
|
|
|
hgr.dirty = { ...notDirty };
|
|
|
|
gr.dirty = { ...notDirty };
|
2021-04-19 02:54:15 +00:00
|
|
|
|
2020-11-24 16:48:14 +00:00
|
|
|
return blitted;
|
|
|
|
}
|
|
|
|
|
2021-04-19 02:54:15 +00:00
|
|
|
getState(): VideoModesState {
|
2020-11-24 16:48:14 +00:00
|
|
|
return {
|
|
|
|
grs: [this._grs[0].getState(), this._grs[1].getState()],
|
|
|
|
hgrs: [this._hgrs[0].getState(), this._hgrs[1].getState()],
|
2021-05-25 19:08:10 +00:00
|
|
|
textMode: this.textMode,
|
|
|
|
mixedMode: this.mixedMode,
|
|
|
|
hiresMode: this.hiresMode,
|
|
|
|
pageMode: this.pageMode,
|
|
|
|
_80colMode: this._80colMode,
|
|
|
|
altCharMode: this.altCharMode,
|
|
|
|
an3State: this.an3State,
|
|
|
|
flag: this.flag
|
2021-04-19 02:54:15 +00:00
|
|
|
};
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setState(state: VideoModesState) {
|
2021-05-25 19:08:10 +00:00
|
|
|
this.textMode = state.textMode;
|
|
|
|
this.mixedMode = state.mixedMode;
|
|
|
|
this.hiresMode = state.hiresMode;
|
|
|
|
this.pageMode = state.pageMode;
|
|
|
|
this._80colMode = state._80colMode;
|
|
|
|
this.altCharMode = state.altCharMode;
|
|
|
|
this.an3State = state.an3State;
|
|
|
|
this.flag = state.flag;
|
2020-11-24 16:48:14 +00:00
|
|
|
|
|
|
|
this._grs[0].setState(state.grs[0]);
|
|
|
|
this._grs[1].setState(state.grs[1]);
|
|
|
|
this._hgrs[0].setState(state.hgrs[0]);
|
|
|
|
this._hgrs[1].setState(state.hgrs[1]);
|
2021-04-19 02:54:15 +00:00
|
|
|
this._refresh();
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mono(on: boolean) {
|
2021-04-21 00:42:32 +00:00
|
|
|
if (on) {
|
2021-05-25 19:08:10 +00:00
|
|
|
this.screen.classList.add('mono');
|
2021-04-21 00:42:32 +00:00
|
|
|
} else {
|
2021-05-25 19:08:10 +00:00
|
|
|
this.screen.classList.remove('mono');
|
2021-04-21 00:42:32 +00:00
|
|
|
}
|
2021-05-25 19:08:10 +00:00
|
|
|
this.monoMode = on;
|
2021-04-19 02:54:15 +00:00
|
|
|
this._refresh();
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
2021-02-22 02:38:21 +00:00
|
|
|
|
2021-04-21 00:42:32 +00:00
|
|
|
scanlines(on: boolean) {
|
|
|
|
// Can't apply scanline filter to canvas
|
2021-05-25 19:08:10 +00:00
|
|
|
const parent = this.screen.parentElement;
|
2021-04-21 00:42:32 +00:00
|
|
|
if (parent) {
|
|
|
|
if (on) {
|
|
|
|
parent.classList.add('scanlines');
|
|
|
|
} else {
|
|
|
|
parent.classList.remove('scanlines');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:48:14 +00:00
|
|
|
getText() {
|
2021-05-25 19:08:10 +00:00
|
|
|
return this._grs[this.pageMode - 1].getText();
|
2020-11-24 16:48:14 +00:00
|
|
|
}
|
|
|
|
}
|