forked from dgchurchill/nanowaspjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crtcmemory.js
183 lines (154 loc) · 6.35 KB
/
crtcmemory.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
/* NanoWasp - A MicroBee emulator
* Copyright (C) 2007, 2011 David G. Churchill
*
* This file is part of NanoWasp.
*
* NanoWasp 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.
*
* NanoWasp 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
var nanowasp = nanowasp || {};
nanowasp.CrtcMemory = function (charRomData, graphicsContext) {
this._charRom = new nanowasp.Rom(charRomData);
this._pcgRam = new nanowasp.Ram(this.PCG_RAM_SIZE);
this._videoRam = new nanowasp.Ram(this.VIDEO_RAM_SIZE);
this._graphicsContext = graphicsContext;
this._pcgImages = {};
this._charRomImages = {};
this._buildAllCharacters(this._charRomImages, this._charRom);
this.clearDirtyStatus();
};
nanowasp.CrtcMemory.prototype = {
GRAPHICS_MEMORY_SIZE: 4096,
VIDEO_RAM_SIZE: 2048,
PCG_RAM_SIZE: 2048,
BIT_MA13: 13,
CHAR_WIDTH: 8,
MAX_CHAR_HEIGHT: 16,
FOREGROUND_COLOR: [247, 211, 49, 220],
BACKGROUND_COLOR: [0, 0, 0, 0],
BACKGROUND_COLOR_CSS: "rgba(0, 0, 0, 0)",
BIT_PCG: 7,
INDEX_START: 0,
INDEX_COUNT: 7,
reset: function () {
this._charRom.reset();
this._pcgRam.reset();
this._videoRam.reset();
},
restoreState: function (state) {
this._videoRam.restoreState(state);
this._pcgRam.restoreState(state);
this._buildAllCharacters(this._pcgImages, this._pcgRam);
},
connect: function (crtc, latchRom) {
this._crtc = crtc;
this._latchRom = latchRom;
},
getSize: function () {
return this.GRAPHICS_MEMORY_SIZE;
},
read: function (address) {
if (address < this.VIDEO_RAM_SIZE) {
if (this._latchRom.isLatched()) {
var baseAddress = utils.getBit(this._crtc.getDisplayStart(), this.BIT_MA13) * this.VIDEO_RAM_SIZE;
return this._charRom.read(baseAddress + address);
} else {
return this._videoRam.read(address);
}
} else {
return this._pcgRam.read((address - this.VIDEO_RAM_SIZE) % this.PCG_RAM_SIZE);
}
},
write: function (address, value) {
if (address < this.VIDEO_RAM_SIZE) {
if (!this._latchRom.isLatched()) {
this._videoRam.write(address, value);
this._dirtyVideoRam[address] = true; // Assume the new value is different.
}
}
else {
var pcgAddress = (address - this.VIDEO_RAM_SIZE) % this.PCG_RAM_SIZE;
this._pcgRam.write(pcgAddress, value);
var character = pcgAddress / this.MAX_CHAR_HEIGHT | 0;
var row = pcgAddress % this.MAX_CHAR_HEIGHT;
this._pcgImages[character] = this._buildCharacterRow(this._pcgImages[character], value, row);
this._dirtyPcgImages[character] = true;
}
},
/* Determines whether the character data for the given address has changed since the last render.
*
* The character data is dirty if the character in video memory or its corresponding bitmap data has changed.
*/
isDirty: function (crtcAddress) {
var videoAddress = crtcAddress % this.VIDEO_RAM_SIZE;
if (videoAddress in this._dirtyVideoRam) {
return true;
}
var b = this._videoRam.read(crtcAddress % this.VIDEO_RAM_SIZE);
var isPcg = utils.getBit(b, this.BIT_PCG) == 1;
if (!isPcg) {
return false; // ROM-based bitmaps cannot change.
}
var character = utils.getBits(b, this.INDEX_START, this.INDEX_COUNT);
return character in this._dirtyPcgImages;
},
clearDirtyStatus: function () {
this._dirtyVideoRam = {};
this._dirtyPcgImages = {};
},
getCharacterData: function (crtcAddress, scansPerRow, cursor) {
var b = this._videoRam.read(crtcAddress % this.VIDEO_RAM_SIZE);
var character = utils.getBits(b, this.INDEX_START, this.INDEX_COUNT);
var isPcg = utils.getBit(b, this.BIT_PCG) == 1;
if (!isPcg) {
// Select character ROM bank
character += utils.getBit(crtcAddress, this.BIT_MA13) * this.VIDEO_RAM_SIZE / this.MAX_CHAR_HEIGHT;
}
if (cursor == null || cursor == undefined) {
var imageCache = isPcg ? this._pcgImages : this._charRomImages;
return imageCache[character];
} else {
var memory = isPcg ? this._pcgRam : this._charRom;
return this._buildCharacter(null, memory, character * this.MAX_CHAR_HEIGHT, cursor);
}
},
_buildAllCharacters: function (cache, memory) {
for (var i = 0; i < memory.getSize() / this.MAX_CHAR_HEIGHT; ++i) {
cache[i] = this._buildCharacter(cache[i], memory, i * this.MAX_CHAR_HEIGHT);
}
},
_buildCharacter: function (image, memory, offset, cursor) {
var haveCursor = cursor != null && cursor != undefined;
for (var i = 0; i < this.MAX_CHAR_HEIGHT; ++i) {
var data = memory.read(offset + i);
if (haveCursor && i >= cursor[0] && i <= cursor[1]) {
data ^= 0xff;
}
image = this._buildCharacterRow(image, data, i);
}
return image;
},
_buildCharacterRow: function (image, data, row) {
if (image == null || image == undefined) {
image = this._graphicsContext.createImageData(this.CHAR_WIDTH, this.MAX_CHAR_HEIGHT);
}
var imageOffset = row * this.CHAR_WIDTH * 4;
for (var i = this.CHAR_WIDTH - 1; i >= 0; --i) {
var color = ((data & (1 << i)) != 0) ? this.FOREGROUND_COLOR : this.BACKGROUND_COLOR;
for (var j = 0; j < color.length; ++j) {
image.data[imageOffset++] = color[j];
}
}
return image;
}
};