-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathvdp.js
1197 lines (1008 loc) · 30.7 KB
/
vdp.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
'use strict';
/** @const */ var NTSC = 0;
/** @const */ var PAL = 1;
/**
* X Pixels, including blanking.
* @const
*/
var SMS_X_PIXELS = 342;
/**
* Y Pixels (NTSC), including blanking.
* @const
*/
var SMS_Y_PIXELS_NTSC = 262;
/**
* Y Pixels (PAL), including blanking.
* @const
*/
var SMS_Y_PIXELS_PAL = 313;
/**
* SMS visible screen width.
* @const
*/
var SMS_WIDTH = 256;
/**
* SMS visible screen height.
* @const
*/
var SMS_HEIGHT = 192;
/**
* GG visible screen width.
* @const
*/
var GG_WIDTH = 160;
/**
* GG visible screen height.
* @const
*/
var GG_HEIGHT = 144;
/**
* GG visible window starts here (x).
* @const
*/
var GG_X_OFFSET = 48;
/**
* GG window starts here (y).
* @const
*/
var GG_Y_OFFSET = 24;
/** @const */ var STATUS_VINT = 0x80; // Frame Interrupt Pending
/** @const */ var STATUS_OVERFLOW = 0x40; // Sprite Overflow
/** @const */ var STATUS_COLLISION = 0x20; // Sprite Collision
/** @const */ var STATUS_HINT = 0x04; // Line interrupt Pending
/** This would be different in 224 line mode. */
/** @const */ var BGT_LENGTH = 32 * 28 * 2;
/** Max number of sprites hardware can handle per scanline. */
/** @const */ var SPRITES_PER_LINE = 8;
/** References into lineSprites table. */
/** @const */ var SPRITE_COUNT = 0; // Number of sprites on line
/** @const */ var SPRITE_X = 1; // Sprite X Position
/** @const */ var SPRITE_Y = 2; // Sprite Y Position
/** @const */ var SPRITE_N = 3; // Sprite Pattern
// Total number of tiles in VRAM
/** @const */ var TOTAL_TILES = 512;
// Tile size
/** @const */ var TILE_SIZE = 8;
/**
* @constructor
* @param {JSSMS} sms
*/
JSSMS.Vdp = function(sms) {
this.main = sms;
var i = 0;
/**
* NTSC / PAL emulation.
* @type {number}
*/
this.videoMode = NTSC;
// VDP Emulation
/**
* Video RAM.
* 16K of Video RAM.
* @type {Uint8Array}
*/
this.VRAM = new JSSMS.Utils.Uint8Array(0x4000);
/**
* Colour RAM.
* Note, we don't directly emulate CRAM but actually store the converted Java palette
* in it. Therefore the length is different to on the real Game Gear where it's actually
* 64 bytes.
* @type {Uint8Array}
*/
this.CRAM = new JSSMS.Utils.Uint8Array(0x20 * 3);
for (i = 0; i < 0x20 * 3; i++) {
this.CRAM[i] = 0xff;
}
/**
* VDP registers.
* 15 Registers, (0-10) used by SMS, but some programs write > 10.
* @type {Uint8Array}
*/
this.vdpreg = new JSSMS.Utils.Uint8Array(16);
/**
* Status register.
* @type {number}
*/
this.status = 0;
/**
* First or second byte of command word.
* @type {boolean}
*/
this.firstByte = false;
/**
* Command word first byte latch.
* @type {number}
*/
this.commandByte = 0;
/**
* Location in VRAM.
* @type {number}
*/
this.location = 0;
/**
* Store type of operation taking place.
* @type {number}
*/
this.operation = 0;
/**
* Buffer VRAM reads.
* @type {number}
*/
this.readBuffer = 0;
/**
* Current line number to render.
* @type {number}
*/
this.line = 0;
/**
* Vertical line interrupt counter.
* @type {number}
*/
this.counter = 0;
/**
* Background priorities.
* @type {Uint8Array}
*/
this.bgPriority = new JSSMS.Utils.Uint8Array(SMS_WIDTH);
/** Sprite collisions. */
if (VDP_SPRITE_COLLISIONS) {
/**
* @type {Uint8Array}
*/
this.spriteCol = new JSSMS.Utils.Uint8Array(SMS_WIDTH);
}
/**
* Address of background table (32x28x2 = 0x700 bytes).
* @type {number}
*/
this.bgt = 0;
/**
* As vscroll cannot be changed during the active display period.
* @type {number}
*/
this.vScrollLatch = 0;
// Emulation Related
/**
* Emulated display.
* @type {Uint8ClampedArray}
*/
this.display = /** @type {Uint8ClampedArray} */ (sms.ui.canvasImageData.data);
/** SMS colours converted to RGB hex. */
/** @type {Uint8Array} */ this.main_JAVA_R = new JSSMS.Utils.Uint8Array(0x40);
/** @type {Uint8Array} */ this.main_JAVA_G = new JSSMS.Utils.Uint8Array(0x40);
/** @type {Uint8Array} */ this.main_JAVA_B = new JSSMS.Utils.Uint8Array(0x40);
/** GG colours converted to RGB hex. */
/** @type {Uint8Array} */ this.GG_JAVA_R = new JSSMS.Utils.Uint8Array(0x100);
/** @type {Uint8Array} */ this.GG_JAVA_G = new JSSMS.Utils.Uint8Array(0x100);
/** @type {Uint8Array} */ this.GG_JAVA_B = new JSSMS.Utils.Uint8Array(0x10);
/**
* Horizontal viewport start.
* @type {number}
*/
this.h_start = 0;
/**
* Horizontal viewport end.
* @type {number}
*/
this.h_end = 0;
// Decoded SAT Table
/**
* Address of sprite attribute table (256 bytes).
* @type {number}
*/
this.sat = 0;
/**
* Determine whether SAT has been written to.
* @type {boolean}
*/
this.isSatDirty = false;
/**
* Decoded SAT by each scanline.
* @type {Array.<Uint8Array>}
*/
this.lineSprites = new Array(SMS_HEIGHT);
for (i = 0; i < SMS_HEIGHT; i++) {
this.lineSprites[i] = new JSSMS.Utils.Uint8Array(1 + 3 * SPRITES_PER_LINE);
}
// Decoded Tiles
/**
* Decoded tile data.
* @type {Array.<Uint8Array>}
*/
this.tiles = new Array(TOTAL_TILES);
/**
* Store whether tile has been written to.
* @type {Uint8Array}
*/
this.isTileDirty = new JSSMS.Utils.Uint8Array(TOTAL_TILES);
/** Min / Max of dirty tile index. */
/** @type {number} */ this.minDirty = 0;
/** @type {number} */ this.maxDirty = 0;
this.createCachedImages();
this.generateConvertedPals();
};
JSSMS.Vdp.prototype = {
/**
* Reset VDP.
*/
reset: function() {
var i;
this.firstByte = true;
this.location = 0;
this.counter = 0;
this.status = 0;
this.operation = 0;
for (i = 0; i < 16; i++) {
this.vdpreg[i] = 0;
}
this.vdpreg[2] = 0x0e; // B1-B3 high on startup
this.vdpreg[5] = 0x7e; // B1-B6 high on startup
this.vScrollLatch = 0;
this.main.cpu.interruptLine = false;
this.isSatDirty = true;
this.minDirty = TOTAL_TILES;
this.maxDirty = -1;
for (i = 0x0000; i < 0x4000; i++) {
this.VRAM[i] = 0;
}
for (i = 0; i < SMS_WIDTH * SMS_HEIGHT * 4; i = i + 4) {
this.display[i] = 0x00;
this.display[i + 1] = 0x00;
this.display[i + 2] = 0x00;
this.display[i + 3] = 0xff; // Alpha channel
}
},
/**
* Force full redraw of entire cache.
*/
forceFullRedraw: function() {
this.bgt = (this.vdpreg[2] & 0x0f & ~0x01) << 10;
this.minDirty = 0;
this.maxDirty = TOTAL_TILES - 1;
for (var i = 0; i < TOTAL_TILES; i++) {
this.isTileDirty[i] = 1;
}
this.sat = (this.vdpreg[5] & ~0x01 & ~0x80) << 7;
this.isSatDirty = true;
},
/**
* Read Vertical Port
*
* @return {number} VCounter Value.
*/
getVCount: function() {
if (this.videoMode === NTSC) {
if (this.line > 0xda) {
return this.line - 0x06; // Values from 00 to DA, then jump to D5-FF
}
} else {
// PAL
if (this.line > 0xf2) {
return this.line - 0x39;
}
}
return this.line;
},
/**
* Read VDP control port (0xBF).
*
* @return {number} Copy of status register.
*/
controlRead: function() {
// Reset flag
this.firstByte = true;
// Create copy, as we'll need to clear bits of status reg
var statuscopy = this.status;
// Clear b7, b6, b5 when status register read
this.status = 0; // other bits never used anyway
// Clear IRQ Line
this.main.cpu.interruptLine = false;
return statuscopy;
},
/**
* Write to VDP control port (0xBF).
*
* @param {number} value Value to write.
*/
controlWrite: function(value) {
// Store First Byte of Command Word
if (this.firstByte) {
this.firstByte = false;
this.commandByte = value;
this.location = (this.location & 0x3f00) | value;
} else {
this.firstByte = true;
this.operation = (value >> 6) & 3;
this.location = this.commandByte | (value << 8);
// Read value from VRAM
if (this.operation === 0) {
this.readBuffer = this.VRAM[this.location++ & 0x3fff] & 0xff;
} else if (this.operation === 2) {
// Set VDP Register
var reg = value & 0x0f;
switch (reg) {
// Interrupt Control 0 (Verified using Charles MacDonald test program)
// Bit 4 of register $00 acts like a on/off switch for the VDP's IRQ line.
// As long as the line interrupt pending flag is set, the VDP will assert the
// IRQ line if bit 4 of register $00 is set, and it will de-assert the IRQ line
// if the same bit is cleared.
case 0:
if (
ACCURATE_INTERRUPT_EMULATION &&
(this.status & STATUS_HINT) !== 0
) {
this.main.cpu.interruptLine = (this.commandByte & 0x10) !== 0;
}
break;
// Interrupt Control 1
case 1:
if (
(this.status & STATUS_VINT) !== 0 &&
(this.commandByte & 0x20) !== 0
) {
this.main.cpu.interruptLine = true;
}
// By writing here we've updated the height of the sprites and need to update
// the sprites on each line
if ((this.commandByte & 3) !== (this.vdpreg[reg] & 3)) {
this.isSatDirty = true;
}
break;
// BGT Written
case 2:
// Address of Background Table in VRAM
this.bgt = (this.commandByte & 0x0f & ~0x01) << 10;
break;
// SAT Written
case 5:
var old = this.sat;
// Address of Sprite Attribute Table in RAM
this.sat = (this.commandByte & ~0x01 & ~0x80) << 7;
if (old !== this.sat) {
// Should also probably update tiles here?
this.isSatDirty = true;
//JSSMS.Utils.console.log('New address written to SAT: ' + old + ' -> ' + this.sat);
}
break;
}
this.vdpreg[reg] = this.commandByte; // Set reg to previous byte
}
}
},
/**
* Read VDP data port (0xBE).
*
* @return {number} Buffered read from VRAM.
*/
dataRead: function() {
// 0xBE
this.firstByte = true; // Reset flag
var value = this.readBuffer; // Stores value to be returned
this.readBuffer = this.VRAM[this.location++ & 0x3fff] & 0xff;
return value;
},
/**
* Write to VDP data port (0xBE).
*
* @param {number} value Value to Write.
*/
dataWrite: function(value) {
var temp = 0;
// Reset flag
this.firstByte = true;
switch (this.operation) {
// VRAM Write
case 0x00:
case 0x01:
case 0x02:
var address = this.location & 0x3fff;
// Check VRAM value has actually changed
if (value !== (this.VRAM[address] & 0xff)) {
//if (address >= bgt && address < bgt + BGT_LENGTH); // Don't write dirty to BGT
if (
(address >= this.sat && address < this.sat + 64) ||
(address >= this.sat + 128 && address < this.sat + 256)
) {
// Don't write dirty to SAT
this.isSatDirty = true;
} else {
var tileIndex = address >> 5;
// Get tile number that's being written to (divide VRAM location by 32).
this.isTileDirty[tileIndex] = 1;
if (tileIndex < this.minDirty) {
this.minDirty = tileIndex;
}
if (tileIndex > this.maxDirty) {
this.maxDirty = tileIndex;
}
}
this.VRAM[address] = value;
}
break;
// CRAM Write
// Instead of writing real colour to CRAM, write converted Java palette colours for speed.
// Slightly inaccurate, as CRAM doesn't contain real values, but it is never read by software.
case 0x03:
if (this.main.is_sms) {
temp = (this.location & 0x1f) * 3;
this.CRAM[temp] = this.main_JAVA_R[value];
this.CRAM[temp + 1] = this.main_JAVA_G[value];
this.CRAM[temp + 2] = this.main_JAVA_B[value];
} else {
temp = ((this.location & 0x3f) >> 1) * 3;
if (!(this.location & 0x01)) {
// first byte
this.CRAM[temp] = this.GG_JAVA_R[value];
this.CRAM[temp + 1] = this.GG_JAVA_G[value];
} else {
this.CRAM[temp + 2] = this.GG_JAVA_B[value];
}
}
break;
}
if (ACCURATE) {
this.readBuffer = value;
}
this.location++;
},
/**
* Generate VDP Interrupts.
* Assert the IRQ line as necessary for a particular scanline.
*
* @param {number} lineno Line to check for interrupts.
*
* @see http://www.smspower.org/forums/viewtopic.php?t=9366&highlight=chicago
*/
interrupts: function(lineno) {
if (lineno <= 192) {
// This can cause hangs as interrupts are only taken between instructions,
// if the IRQ status flag is set *during* the execution of an instruction the
// CPU will be able to read it without the interrupt occurring.
//
// e.g. Chicago Syndicate on GG
if (!ACCURATE_INTERRUPT_EMULATION && lineno === 192) {
this.status |= STATUS_VINT;
}
// Counter Expired = Line Interrupt Pending
if (this.counter === 0) {
// Reload Counter
this.counter = this.vdpreg[10];
this.status |= STATUS_HINT;
} else {
// Otherwise Decrement Counter
this.counter--;
}
// Line Interrupts Enabled and Pending. Assert IRQ Line.
if ((this.status & STATUS_HINT) !== 0 && (this.vdpreg[0] & 0x10) !== 0) {
this.main.cpu.interruptLine = true;
}
} else {
// lineno >= 193
// Reload counter on every line outside active display + 1
this.counter = this.vdpreg[10];
// Frame Interrupts Enabled and Pending. Assert IRQ Line.
if (
(this.status & STATUS_VINT) !== 0 &&
(this.vdpreg[1] & 0x20) !== 0 &&
lineno < 224
) {
this.main.cpu.interruptLine = true;
}
// Update the VSCROLL latch for the next active display period
if (ACCURATE && lineno === this.main.no_of_scanlines - 1) {
this.vScrollLatch = this.vdpreg[9];
}
}
},
setVBlankFlag: function() {
this.status |= STATUS_VINT;
},
/**
* Render Line of SMS/GG Display.
*
* @param {number} lineno Line Number to Render.
*/
drawLine: function(lineno) {
var x = 0;
var location = 0;
var colour = 0;
// Check we are in the visible drawing region
if (
this.main.is_gg &&
(lineno < GG_Y_OFFSET || lineno >= GG_Y_OFFSET + GG_HEIGHT)
) {
return;
}
// Clear sprite collision array if enabled
if (VDP_SPRITE_COLLISIONS) {
for (x = 0; x < SMS_WIDTH /* this.spriteCol.length */; x++) {
this.spriteCol[x] = false;
}
}
// Check Screen is switched on
if ((this.vdpreg[1] & 0x40) !== 0) {
// Draw Background Layer
if (this.maxDirty !== -1) {
this.decodeTiles();
}
this.drawBg(lineno);
// Draw Sprite Layer
if (this.isSatDirty) {
this.decodeSat();
}
if (this.lineSprites[lineno][SPRITE_COUNT] !== 0) {
this.drawSprite(lineno);
}
// Blank Leftmost Column (SMS Only)
if (this.main.is_sms && this.vdpreg[0] & 0x20) {
location = (lineno << 8) * 4;
colour = ((this.vdpreg[7] & 0x0f) + 16) * 3;
for (x = location; x < location + 8 * 4; x = x + 4) {
this.display[x] = this.CRAM[colour];
this.display[x + 1] = this.CRAM[colour + 1];
this.display[x + 2] = this.CRAM[colour + 2];
}
}
} else {
// Blank Display
this.drawBGColour(lineno);
}
},
/**
* @param {number} lineno
*/
drawBg: function(lineno) {
var pixX = 0;
var colour = 0;
var temp = 0;
var temp2 = 0;
// Horizontal Scroll
var hscroll = this.vdpreg[8];
// Vertical Scroll
var vscroll = ACCURATE ? this.vScrollLatch : this.vdpreg[9];
// Top Two Rows Not Affected by Horizontal Scrolling (SMS Only)
// We don't actually need the SMS check here as we don't draw this line for GG now
if (lineno < 16 && (this.vdpreg[0] & 0x40) !== 0 /*&& this.main.is_sms*/) {
hscroll = 0;
}
// Lock Right eight columns
var lock = this.vdpreg[0] & 0x80;
// Column to start drawing at (0 - 31) [Add extra columns for GG]
var tile_column = 32 - (hscroll >> 3) + this.h_start;
// Row to start drawing at (0 - 27)
var tile_row = (lineno + vscroll) >> 3;
if (tile_row > 27) {
tile_row -= 28;
}
// Actual y position in tile (0 - 7) (Also times by 8 here for quick access to pixel)
var tile_y = ((lineno + (vscroll & 7)) & 7) << 3;
// Array Position
var row_precal = lineno << 8;
// Cycle through background table
for (var tx = this.h_start; tx < this.h_end; tx++) {
var tile_props = this.bgt + ((tile_column & 0x1f) << 1) + (tile_row << 6);
var secondbyte = this.VRAM[tile_props + 1];
// Select Palette (Either 0 or 16)
var pal = (secondbyte & 0x08) << 1;
// Screen X Position
var sx = (tx << 3) + (hscroll & 7);
// Do V-Flip (take into account the fact that everything is times 8)
var pixY = (secondbyte & 0x04) === 0 ? tile_y : (7 << 3) - tile_y;
// Pattern Number (0 - 512)
var tile = this.tiles[
(this.VRAM[tile_props] & 0xff) + ((secondbyte & 0x01) << 8)
];
// Plot 8 Pixel Row (No H-Flip)
if (!(secondbyte & 0x02)) {
for (pixX = 0; pixX < 8 && sx < SMS_WIDTH; pixX++, sx++) {
colour = tile[pixX + pixY];
temp = (sx + row_precal) * 4;
temp2 = (colour + pal) * 3;
// Set Priority Array (Sprites over/under background tile)
this.bgPriority[sx] = (secondbyte & 0x10) !== 0 && colour !== 0;
if (sx >= this.h_start * 8 && sx < this.h_end * 8) {
this.display[temp] = this.CRAM[temp2];
this.display[temp + 1] = this.CRAM[temp2 + 1];
this.display[temp + 2] = this.CRAM[temp2 + 2];
}
}
} else {
// Plot 8 Pixel Row (H-Flip)
for (pixX = 7; pixX >= 0 && sx < SMS_WIDTH; pixX--, sx++) {
colour = tile[pixX + pixY];
temp = (sx + row_precal) * 4;
temp2 = (colour + pal) * 3;
// Set Priority Array (Sprites over/under background tile)
this.bgPriority[sx] = (secondbyte & 0x10) !== 0 && colour !== 0;
if (sx >= this.h_start * 8 && sx < this.h_end * 8) {
this.display[temp] = this.CRAM[temp2];
this.display[temp + 1] = this.CRAM[temp2 + 1];
this.display[temp + 2] = this.CRAM[temp2 + 2];
}
}
}
tile_column++;
// Rightmost 8 columns Not Affected by Vertical Scrolling
if (lock !== 0 && tx === 23) {
tile_row = lineno >> 3;
tile_y = (lineno & 7) << 3;
}
}
},
/**
* Render Line of Sprite Layer.
* - Notes: Sprites do not wrap on the x-axis.
*
* @param {number} lineno Line Number to Render.
*/
drawSprite: function(lineno) {
var colour = 0;
var temp = 0;
var temp2 = 0;
var i = 0;
// Reference to the sprites that should appear on this line
var sprites = this.lineSprites[lineno];
// Number of sprites to draw on this scanline
var count = Math.min(SPRITES_PER_LINE, sprites[SPRITE_COUNT]);
// Zoom Sprites (0 = off, 1 = on)
var zoomed = this.vdpreg[1] & 0x01;
var row_precal = lineno << 8;
// Get offset into array
var off = count * 3;
// Have to iterate backwards here as we've already cached tiles
for (; i < count; i++) {
// Sprite Pattern Index
// Also mask on Pattern Index from 100 - 1FFh (if reg 6 bit 3 set)
var n = sprites[off--] | ((this.vdpreg[6] & 0x04) << 6);
// Sprite Y Position
var y = sprites[off--];
// Sprite X Position
// Shift pixels left by 8 if necessary
var x = sprites[off--] - (this.vdpreg[0] & 0x08);
// Row of tile data to render (0-7)
var tileRow = (lineno - y) >> zoomed;
// When using 8x16 sprites LSB has no effect
if ((this.vdpreg[1] & 0x02) !== 0) {
n &= ~0x01;
}
// Pattern Number (0 - 512)
var tile = this.tiles[n + ((tileRow & 0x08) >> 3)];
// If X Co-ordinate is negative, do a fix to draw from position 0
var pix = 0;
if (x < 0) {
pix = -x;
x = 0;
}
// Offset into decoded tile data
var offset = pix + ((tileRow & 7) << 3);
// Plot Normal Sprites (Width = 8)
if (!zoomed) {
for (; pix < 8 && x < this.h_end * 8; pix++, x++) {
colour = tile[offset++];
if (x >= this.h_start * 8 && colour !== 0 && !this.bgPriority[x]) {
temp = (x + row_precal) * 4;
temp2 = (colour + 16) * 3;
this.display[temp] = this.CRAM[temp2];
this.display[temp + 1] = this.CRAM[temp2 + 1];
this.display[temp + 2] = this.CRAM[temp2 + 2];
// Emulate sprite collision (when two opaque pixels overlap)
if (VDP_SPRITE_COLLISIONS) {
if (!this.spriteCol[x]) {
this.spriteCol[x] = true;
} else {
this.status |= STATUS_COLLISION; // Bit 5 of status flag indicates collision
}
}
}
}
} else {
// Plot Zoomed Sprites (Width = 16)
for (; pix < 8 && x < this.h_end * 8; pix++, x += 2) {
colour = tile[offset++];
// Plot first pixel
if (x >= this.h_start * 8 && colour !== 0 && !this.bgPriority[x]) {
temp = (x + row_precal) * 4;
temp2 = (colour + 16) * 3;
this.display[temp] = this.CRAM[temp2];
this.display[temp + 1] = this.CRAM[temp2 + 1];
this.display[temp + 2] = this.CRAM[temp2 + 2];
if (VDP_SPRITE_COLLISIONS) {
if (!this.spriteCol[x]) {
this.spriteCol[x] = true;
} else {
this.status |= STATUS_COLLISION; // Bit 5 of status flag indicates collision
}
}
}
// Plot second pixel
if (
x + 1 >= this.h_start * 8 &&
colour !== 0 &&
!this.bgPriority[x + 1]
) {
temp = (x + row_precal + 1) * 4;
temp2 = (colour + 16) * 3;
this.display[temp] = this.CRAM[temp2];
this.display[temp + 1] = this.CRAM[temp2 + 1];
this.display[temp + 2] = this.CRAM[temp2 + 2];
if (VDP_SPRITE_COLLISIONS) {
if (!this.spriteCol[x + 1]) {
this.spriteCol[x + 1] = true;
} else {
this.status |= STATUS_COLLISION; // Bit 5 of status flag indicates collision
}
}
}
}
}
}
// Sprite Overflow (more than 8 sprites on line)
if (sprites[SPRITE_COUNT] >= SPRITES_PER_LINE) {
this.status |= STATUS_OVERFLOW;
}
},
/**
* Draw a line of the current background colour.
*
* @param {number} lineno Line number to render.
*/
drawBGColour: function(lineno) {
var x = 0;
var location = (lineno << 8) * 4;
var colour = ((this.vdpreg[7] & 0x0f) + 16) * 3;
for (
x = location + this.h_start * 8 * 4;
x < location + this.h_end * 8 * 4;
x = x + 4
) {
this.display[x] = this.CRAM[colour];
this.display[x + 1] = this.CRAM[colour + 1];
this.display[x + 2] = this.CRAM[colour + 2];
}
},
// Note we should try not to update the bgt/sat locations?
decodeTiles: function() {
//JSSMS.Utils.console.log('[' + this.line + ']' + ' min dirty:' + this.minDirty + ' max: ' + this.maxDirty);
for (var i = this.minDirty; i <= this.maxDirty; i++) {
// Only decode tiles that have changed since the last iteration
if (!this.isTileDirty[i]) {
continue;
}
// Note that we've updated the tile
this.isTileDirty[i] = 0;
//JSSMS.Utils.console.log('tile ' + i + ' is dirty');
var tile = this.tiles[i];
var pixel_index = 0;
// 4 bytes per row, total of 32 bytes per tile
var address = i << 5;
// Plot column of 8 pixels
for (var y = 0; y < TILE_SIZE; y++) {
var address0 = this.VRAM[address++];
var address1 = this.VRAM[address++];
var address2 = this.VRAM[address++];
var address3 = this.VRAM[address++];
// Plot row of 8 pixels
for (var bit = 0x80; bit !== 0; bit >>= 1) {
var colour = 0;
// Set Colour of Pixel (0-15)
if ((address0 & bit) !== 0) {
colour |= 0x01;
}
if ((address1 & bit) !== 0) {
colour |= 0x02;
}
if ((address2 & bit) !== 0) {
colour |= 0x04;
}
if ((address3 & bit) !== 0) {
colour |= 0x08;
}
tile[pixel_index++] = colour;
}
}
}
// Reset min/max dirty counters
this.minDirty = TOTAL_TILES;
this.maxDirty = -1;
},
// DECODE SAT TABLE
//
// Each sprite is defined in the sprite attribute table (SAT), a 256-byte
// table located in VRAM. The SAT has the following layout:
//
// 00: yyyyyyyyyyyyyyyy
// 10: yyyyyyyyyyyyyyyy
// 20: yyyyyyyyyyyyyyyy
// 30: yyyyyyyyyyyyyyyy
// 40: ????????????????
// 50: ????????????????
// 60: ????????????????
// 70: ????????????????
// 80: xnxnxnxnxnxnxnxn
// 90: xnxnxnxnxnxnxnxn
// A0: xnxnxnxnxnxnxnxn
// B0: xnxnxnxnxnxnxnxn
// C0: xnxnxnxnxnxnxnxn
// D0: xnxnxnxnxnxnxnxn
// E0: xnxnxnxnxnxnxnxn
// F0: xnxnxnxnxnxnxnxn
//
// y = Y coordinate + 1
// x = X coordinate
// n = Pattern index
// ? = Unused
/**
* Creates a list of sprites per scanline.
*/
decodeSat: function() {
this.isSatDirty = false;
// Clear Existing Table
for (var i = 0; i < this.lineSprites.length; i++) {