forked from dgchurchill/nanowaspjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrtc.js
383 lines (306 loc) · 12.9 KB
/
crtc.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
/* 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.Crtc = function (graphicsContext) {
this.reset();
this._graphicsContext = graphicsContext;
};
nanowasp.Crtc.prototype = {
reset: function () {
this._selectedRegister = 0;
this._lpenValid = false;
this._lpen = 0;
this._emulationTime = 0;
this._lastFrameTime = 0;
this._frameTime = 1; // _frameTime is assumed != 0
this._vblankTime = 0;
this._cursorPosition = 0;
this._cursorStart = 0;
this._cursorEnd = 0;
this._cursorMode = this.CursorMode.NoBlink;
this._cursorOn = false;
this._blinkRate = 0;
this._frameCounter = 0;
this._displayStart = 0;
this._hTotal = 0;
this._hDisplayed = 0;
this._vTotal = 0;
this._vTotalAdjustment = 0;
this._vDisplayed = 0;
this._scansPerRow = 0;
this._memoryAddress = 0;
this._previousRenderState = []; // Used to determine if any state has changed that should force a full render.
this._lastCursorPosition = 0;
},
restoreState: function (state) {
this._selectedRegister = state.readByte();
this._memoryAddress = state.readWord();
this._displayStart = state.readWord();
this._hTotal = state.readWord();
this._hDisplayed = state.readWord();
this._vTotal = state.readWord();
this._vTotalAdjustment = state.readWord();
this._vDisplayed = state.readWord();
this._scansPerRow = state.readWord();
this._cursorStart = state.readWord();
this._cursorEnd = state.readWord();
this._cursorMode = state.readWord();
this._cursorPosition = state.readWord();
this._cursorOn = state.readBool();
this._blinkRate = state.readWord();
this._lpen = state.readWord();
this._lpenValid = state.readBool();
this._calculateVBlank();
},
getSize: function () {
return this.PortIndex.NumPorts;
},
connect: function (microbee, keyboard, crtcMemory) {
this._microbee = microbee;
this._keyboard = keyboard;
this._crtcMemory = crtcMemory;
},
read: function (address) {
switch (address % this.PortIndex.NumPorts) {
case this.PortIndex.Status:
var STATUS_STROBE = 0x80;
var STATUS_LPEN = 0x40;
var STATUS_VBLANK = 0x20;
var status = STATUS_STROBE;
if (!this._lpenValid) {
this._keyboard.checkAll();
}
if (this._lpenValid) {
status |= STATUS_LPEN;
}
if (this._microbee.getTime() % this._frameTime < this._vblankTime) {
status |= STATUS_VBLANK;
}
return status;
case this.PortIndex.Data:
switch (this._selectedRegister) {
case this.RegisterIndex.CursorPosH:
return utils.getBits(this._cursorPosition, 8, 6);
case this.RegisterIndex.CursorPosL:
return utils.getBits(this._cursorPosition, 0, 8);
case this.RegisterIndex.LPenH:
this._lpenValid = false;
return utils.getBits(this._lpen, 8, 6);
case this.RegisterIndex.LPenL:
this._lpenValid = false;
return utils.getBits(this._lpen, 0, 8);
default:
return 0xFF;
}
default:
return 0xFF;
}
},
write: function (address, value) {
switch (address % this.PortIndex.NumPorts) {
case this.PortIndex.Address:
this._selectedRegister = value % this.RegisterIndex.NumRegs;
break;
case this.PortIndex.Data:
switch (this._selectedRegister) {
case this.RegisterIndex.HTot:
this._hTotal = value + 1;
this._calculateVBlank();
break;
case this.RegisterIndex.HDisp:
this._hDisplayed = value;
break;
case this.RegisterIndex.VTot:
this._vTotal = utils.getBits(value, 0, 7) + 1;
this._calculateVBlank();
break;
case this.RegisterIndex.VTotAdj:
this._vTotalAdjustment = utils.getBits(value, 0, 5);
this._calculateVBlank();
break;
case this.RegisterIndex.VDisp:
this._vDisplayed = utils.getBits(value, 0, 7);
break;
case this.RegisterIndex.Scanlines:
this._scansPerRow = utils.getBits(value, 0, 5) + 1;
this._calculateVBlank();
break;
case this.RegisterIndex.CursorStart:
var BLINK_MODE_OFFSET = 5;
this._cursorStart = utils.getBits(value, 0, 5);
this._cursorMode = utils.getBits(value, BLINK_MODE_OFFSET, 2);
switch (this._cursorMode) {
case this.CursorMode.NoBlink:
this._cursorOn = true;
this._blinkRate = 0;
break;
case this.CursorMode.NoCursor:
this._cursorOn = false;
this._blinkRate = 0;
break;
case this.CursorMode.Blink16:
this._blinkRate = 16;
break;
case this.CursorMode.Blink32:
this._blinkRate = 32;
break;
}
break;
case this.RegisterIndex.CursorEnd:
this._cursorEnd = utils.getBits(value, 0, 5);
break;
case this.RegisterIndex.DispStartH:
this._displayStart = utils.copyBits(this._displayStart, 8, 6, value);
break;
case this.RegisterIndex.DispStartL:
this._displayStart = utils.copyBits(this._displayStart, 0, 8, value);
break;
case this.RegisterIndex.CursorPosH:
this._cursorPosition = utils.copyBits(this._cursorPosition, 8, 6, value);
break;
case this.RegisterIndex.CursorPosL:
this._cursorPosition = utils.copyBits(this._cursorPosition, 0, 8, value);
break;
case this.RegisterIndex.SetAddrH:
this._memoryAddress = utils.copyBits(this._memoryAddress, 8, 6, value);
break;
case this.RegisterIndex.SetAddrL:
this._memoryAddress = utils.copyBits(this._memoryAddress, 0, 8, value);
break;
case this.RegisterIndex.DoSetAddr:
this._keyboard.check(this._memoryAddress);
break;
}
break;
}
},
execute: function (time, duration) {
this._emulationTime = time + duration; // Time to update up to.
var delta = this._emulationTime - this._lastFrameTime;
if (delta >= this._frameTime) {
this._render(); // duration may be longer than one frame(?), so this could skip frames.
this._frameCounter += Math.floor(delta / this._frameTime); // FIXME: This probably drops frames as a result of ignoring the fraction, but it's only used for cursor blinking.
this._lastFrameTime = this._emulationTime - delta % this._frameTime; // The emulated time the frame really finished.
if (this._blinkRate > 0 && this._frameCounter > this._blinkRate) {
this._cursorOn = !this._cursorOn; // TODO: Verify this. Modified during porting because the old code didn't seem to make any sense (if condition would always be true?).
this._frameCounter %= this._blinkRate;
}
}
return this._lastFrameTime + this._frameTime - this._emulationTime;
},
triggerLpen: function (address) {
if (this._lpenValid) {
return; // Already triggered, ignore new triggers until previous value is read.
}
this._lpenValid = true;
this._lpen = address;
},
getDisplayStart: function () {
return this._displayStart;
},
_calculateVBlank: function () {
var CHAR_CLOCK_HZ = 1687500;
this._graphicsContext.canvas.width = this._hDisplayed * nanowasp.CrtcMemory.prototype.CHAR_WIDTH;
this._graphicsContext.canvas.height = this._vDisplayed * this._scansPerRow;
this._frameTime = this._hTotal * (this._vTotal * this._scansPerRow + this._vTotalAdjustment) * 1000000 / CHAR_CLOCK_HZ;
this._vblankTime = this._hTotal * ((this._vTotal - this._vDisplayed) * this._scansPerRow + this._vTotalAdjustment) * 1000000 / CHAR_CLOCK_HZ;
if (this._frameTime == 0) {
this._frameTime = 1; // _frameTime is assumed != 0
}
},
_render: function () {
var newRenderState = [this._displayStart, this._vDisplayed, this._hDisplayed, this._scansPerRow];
var fullRenderRequired = false;
if (!utils.listsMatch(this._previousRenderState, newRenderState)) {
fullRenderRequired = true;
this._previousRenderState = newRenderState;
}
if (fullRenderRequired) {
this._graphicsContext.fillStyle = nanowasp.CrtcMemory.prototype.BACKGROUND_COLOR_CSS;// do we lookup bg colour to clear screen??
this._graphicsContext.fillRect(0, 0, this._graphicsContext.canvas.width, this._graphicsContext.canvas.height);
}
var address = this._displayStart;
var x = 0;
var y = 0;
for (var row = 0; row < this._vDisplayed; ++row) {
for (var column = 0; column < this._hDisplayed; ++column) {
var cursor = null;
if (this._cursorOn && address == this._cursorPosition) {
cursor = [this._cursorStart, this._cursorEnd];
}
if (fullRenderRequired || cursor != null || address == this._lastCursorPosition || this._crtcMemory.isDirty(address)) {
var characterImage = this._crtcMemory.getCharacterData(address, this._scansPerRow, cursor);
// we need to get colour information from colour ram. similar to above
// var characterCol = this._crtcMemory.getCharacterCol(address);
// this._graphicsContext.fillStyle = characterCol;
this._graphicsContext.putImageData(characterImage, x, y, 0, 0, nanowasp.CrtcMemory.prototype.CHAR_WIDTH, this._scansPerRow);
}
x += nanowasp.CrtcMemory.prototype.CHAR_WIDTH;
var CRTC_ADDRESS_SIZE = 16384;
address = (address + 1) % CRTC_ADDRESS_SIZE;
}
y += this._scansPerRow;
x = 0;
}
this._lastCursorPosition = this._cursorPosition;
this._crtcMemory.clearDirtyStatus();
},
RegisterIndex: {
HTot: 0,
HDisp: 1,
HSyncPos: 2,
SyncWidth: 3,
VTot: 4,
VTotAdj: 5,
VDisp: 6,
VSyncPos: 7,
Mode: 8,
Scanlines: 9,
CursorStart: 10,
CursorEnd: 11,
DispStartH: 12,
DispStartL: 13,
CursorPosH: 14,
CursorPosL: 15,
LPenH: 16,
LPenL: 17,
SetAddrH: 18,
SetAddrL: 19,
DoSetAddr: 31,
NumRegs: 32
},
PortIndex: {
Address: 0,
Status: 0,
Data: 1,
NumPorts: 2
},
CursorMode: {
NoBlink: 0,
NoCursor: 1,
Blink16: 2,
Blink32: 3
}
};
if (Object.freeze != undefined) {
var p = nanowasp.Crtc.prototype;
Object.freeze(p.RegisterIndex);
Object.freeze(p.PortIndex);
Object.freeze(p.CursorMode);
}