-
Notifications
You must be signed in to change notification settings - Fork 0
/
LcdController.java
499 lines (412 loc) · 16.2 KB
/
LcdController.java
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
/**
* @author Clément Petit (282626)
* @author Yanis Berkani (271348)
*/
package ch.epfl.gameboj.component.lcd;
import java.util.Arrays;
import java.util.Objects;
import ch.epfl.gameboj.Bus;
import ch.epfl.gameboj.Preconditions;
import ch.epfl.gameboj.Register;
import ch.epfl.gameboj.RegisterFile;
import ch.epfl.gameboj.bits.Bit;
import ch.epfl.gameboj.bits.BitVector;
import ch.epfl.gameboj.bits.Bits;
import ch.epfl.gameboj.component.Clocked;
import ch.epfl.gameboj.component.Component;
import ch.epfl.gameboj.component.cpu.Cpu;
import ch.epfl.gameboj.component.cpu.Cpu.Interrupt;
import ch.epfl.gameboj.component.memory.Ram;
import ch.epfl.gameboj.AddressMap;
import static ch.epfl.gameboj.AddressMap.VIDEO_RAM_START;
import static ch.epfl.gameboj.AddressMap.VIDEO_RAM_END;
import static ch.epfl.gameboj.AddressMap.OAM_START;
import static ch.epfl.gameboj.AddressMap.OAM_END;
import static ch.epfl.gameboj.AddressMap.REGS_LCDC_START;
import static ch.epfl.gameboj.AddressMap.REGS_LCDC_END;
/**
* represents a Liquid Crystal Display controller.
*/
public final class LcdController implements Component, Clocked {
private final Cpu cpu;
private final Ram OAM;
private Bus bus;
private LcdImage.Builder nextImageBuilder;
private LcdImage currentImage;
/**
* the screen width in pixels
*/
public static final int LCD_WIDTH = 160;
/**
* the screen height in pixels
*/
public static final int LCD_HEIGHT = 144;
private static final int LY_MAX = 153;
private static final int BG_LINE_SIZE = 256;
private static final int WIN_LINE_SIZE = LCD_WIDTH;
private static final int TILE_EDGE_SIZE = 8;
private static final int NB_OF_SPRITES = 40;
private static final int BYTES_PER_TILE = 16;
private static final int ATTRIBUTE_BYTES_PER_SPRITE = 4;
private static final int TILES_PER_LINE = 32;
private long nextNonIdleCycle;
private int winY;
private int copyDestination;
private int copySource;
private enum Reg implements Register {
LCDC, STAT, SCY, SCX, LY, LYC, DMA, BGP, OBP0, OBP1, WY, WX
};
private final RegisterFile<Reg> lcdBank = new RegisterFile<>(
Reg.values());
private static enum LcdcBits implements Bit {
BG, OBJ, OBJ_SIZE, BG_AREA, TILE_SOURCE, WIN, WIN_AREA, LCD_STATUS
};
private static enum StatBits implements Bit {
MODE0, MODE1, LYC_EQ_LY, INT_MODE0, INT_MODE1, INT_MODE2, INT_LYC
};
private static enum spritesAttributes {
Y_COORD, X_COORD, INDEX, SPECS
};
private static enum SpriteSpec implements Bit {
UNUSED_0, UNUSED_1, UNUSED_2, UNUSED_3, PALETTE, FLIP_H, FLIP_V, BEHIND_BG
};
private static Ram videoRam = new Ram(AddressMap.VIDEO_RAM_SIZE);
/**
* Constructs the LCD controller (that is initially disabled) with an
* assigned cpu.
*
* @param cpu
* the Game Boy processor from which the LCD controller can
* request interruptions
*/
public LcdController(Cpu cpu) {
this.cpu = cpu;
OAM = new Ram(AddressMap.OAM_RAM_SIZE);
nextNonIdleCycle = Long.MAX_VALUE;
copyDestination = AddressMap.OAM_END;
}
/**
* returns the image currently displayed on the screen, or an empty image if
* the first image has not been drawn yet.
*
* @return the image currently displayed on the screen or an empty image
*/
public LcdImage currentImage() {
return currentImage == null
? new LcdImage.Builder(LCD_WIDTH, LCD_HEIGHT).build()
: currentImage;
}
/**
* Attaches the LcdController to the given bus and stores the bus in the
* LcdController.
*
* @param bus
* the bus
*/
@Override
public void attachTo(Bus bus) {
Component.super.attachTo(bus);
this.bus = bus;
}
/**
* gives access to the video ram, the lcd controller registers and the
* object attributes memory.
*
* @param address
* the address (must be a 16 bits value)
* @throws IllegalArgumentException
* if the address is invalid
*/
@Override
public int read(int address) {
Preconditions.checkBits16(address);
if (address >= VIDEO_RAM_START && address < VIDEO_RAM_END) {
return videoRam.read(address - VIDEO_RAM_START);
} else if (address >= REGS_LCDC_START && address < REGS_LCDC_END) {
Reg r = Reg.values()[address - REGS_LCDC_START];
return lcdBank.get(r);
} else if (address >= OAM_START && address < OAM_END) {
return OAM.read(address - OAM_START);
} else {
return NO_DATA;
}
}
/**
* gives access to the video ram, the lcd controller registers and the
* object attributes memory.
* Initiates the copy process if any 8 bits value
* is written in the DMA register.
*
* @param address
* the address (must be a 16 bits value)
* @param data
* the data (must be an 8 bits value)
* @throws IllegalArgumentException
* if the address or the data is invalid
*/
@Override
public void write(int address, int data) {
Preconditions.checkBits16(address);
Preconditions.checkBits8(data);
boolean prevLcdStatus = (lcdBank.testBit(Reg.LCDC,
LcdcBits.LCD_STATUS));
if (address >= VIDEO_RAM_START && address < VIDEO_RAM_END) {
videoRam.write(address - VIDEO_RAM_START, data);
} else if (address >= OAM_START && address < OAM_END) {
OAM.write(address - OAM_START, data);
} else if (address >= REGS_LCDC_START && address < REGS_LCDC_END) {
Reg r = Reg.values()[address - REGS_LCDC_START];
if (!((r == Reg.LY) | (r == Reg.STAT))) {
lcdBank.set(r, data);
if (r == Reg.LCDC && prevLcdStatus
&& !(lcdBank.testBit(Reg.LCDC, LcdcBits.LCD_STATUS))) {
setMode(0);
lcdBank.set(Reg.LY, 0);
LycEqLy();
nextNonIdleCycle = Long.MAX_VALUE;
} else if (r == Reg.LYC) {
LycEqLy();
} else if (r == Reg.DMA) {
copyDestination = OAM_START;
copySource = lcdBank.get(Reg.DMA) << Byte.SIZE;
}
} else if (r == Reg.STAT) {
int mask = data & (~0 << 3);
lcdBank.set(Reg.STAT,
mask | Bits.clip(3, (lcdBank.get(Reg.STAT))));
}
}
}
/**
* Manages the writings in the object attributes memory when a copy is in
* progress.
* Manages the activation of the screen when required, and set LY
* to 153 so it can be set to 0 in mode 0.
* Determines if the lcd controller
* needs to do something during the given cycle, and if so, calls the method
* reallyCycle.
*
* @param cycle
* the cycle
*/
@Override
public void cycle(long cycle) {
if (copyDestination != OAM_END)
write(copyDestination++, bus.read(copySource++));
if (nextNonIdleCycle == Long.MAX_VALUE
&& lcdBank.testBit(Reg.LCDC, LcdcBits.LCD_STATUS)) {
nextNonIdleCycle = cycle;
lcdBank.set(Reg.LY, 153);
}
if (cycle == nextNonIdleCycle)
reallyCycle(cycle);
}
private void reallyCycle(long cycle) {
switch (getMode()) {
case 2: {
setMode(3);
computeLine(lcdBank.get(Reg.LY));
nextNonIdleCycle += 43;
}
break;
case 3: {
setMode(0);
nextNonIdleCycle += 51;
}
break;
case 0: {
if (lcdBank.get(Reg.LY) == LCD_HEIGHT - 1) {
setMode(1);
cpu.requestInterrupt(Interrupt.VBLANK);
currentImage = nextImageBuilder.build();
lcdBank.set(Reg.LY, LCD_HEIGHT);
LycEqLy();
nextNonIdleCycle += 114;
} else {
lcdBank.set(Reg.LY, (lcdBank.get(Reg.LY) + 1) % (LY_MAX + 1));
LycEqLy();
setMode(2);
if (lcdBank.get(Reg.LY) == 0) {
nextImageBuilder = new LcdImage.Builder(LCD_WIDTH,
LCD_HEIGHT);
winY = 0;
}
nextNonIdleCycle += 20;
}
}
break;
case 1: {
if (lcdBank.get(Reg.LY) >= LCD_HEIGHT) {
lcdBank.set(Reg.LY, ((lcdBank.get(Reg.LY) + 1)) % LY_MAX);
LycEqLy();
nextNonIdleCycle += 114;
} else {
setMode(2);
nextImageBuilder = new LcdImage.Builder(LCD_WIDTH, LCD_HEIGHT);
winY = 0;
nextNonIdleCycle += 20;
}
}
}
}
private void LycEqLy() {
if (lcdBank.get(Reg.LY) == lcdBank.get(Reg.LYC)) {
lcdBank.setBit(Reg.STAT, StatBits.LYC_EQ_LY, true);
if (lcdBank.testBit(Reg.STAT, StatBits.INT_LYC)) {
cpu.requestInterrupt(Interrupt.LCD_STAT);
}
} else {
lcdBank.setBit(Reg.STAT, StatBits.LYC_EQ_LY, false);
}
}
private int getMode() {
return Bits.clip(2, lcdBank.get(Reg.STAT));
}
private void setMode(int mode) {
Objects.checkIndex(mode, 4);
switch (mode) {
case 0: {
if (lcdBank.testBit(Reg.STAT, StatBits.INT_MODE0))
cpu.requestInterrupt(Interrupt.LCD_STAT);
}
break;
case 1: {
if (lcdBank.testBit(Reg.STAT, StatBits.INT_MODE1))
cpu.requestInterrupt(Interrupt.LCD_STAT);
}
break;
case 2: {
if (lcdBank.testBit(Reg.STAT, StatBits.INT_MODE2))
cpu.requestInterrupt(Interrupt.LCD_STAT);
}
}
lcdBank.set(Reg.STAT, ((lcdBank.get(Reg.STAT) >>> 2) << 2) | mode);
}
private void computeLine(int y) {
int bgLineIndex = Bits.clip(8, lcdBank.get(Reg.SCY) + y);
LcdImageLine bgLine = extractBgLine(bgLineIndex);
int WX = Math.max(0, lcdBank.get(Reg.WX) - 7);
if ((lcdBank.testBit(Reg.LCDC, LcdcBits.WIN)) && WX < LCD_WIDTH
&& y >= lcdBank.get(Reg.WY)) {
LcdImageLine winLine = extractLine(winY, WIN_LINE_SIZE,
LcdcBits.WIN_AREA);
winY++;
bgLine = bgLine.join(winLine.shift(WX), WX);
}
if (lcdBank.testBit(Reg.LCDC, LcdcBits.OBJ)) {
int size = Bits.test(lcdBank.get(Reg.LCDC), LcdcBits.OBJ_SIZE)
? 16
: 8;
int[] spritesList = spritesIntersectingLine(y, size);
LcdImageLine spritesBGLine = extractSpritesLine(y, spritesList,
size, true);
LcdImageLine spritesFGLine = extractSpritesLine(y, spritesList,
size, false);
BitVector bgOpacity = bgLine.opacity()
.or(spritesBGLine.opacity().not());
bgLine = spritesBGLine.below(bgLine, bgOpacity)
.below(spritesFGLine);
}
nextImageBuilder.setLine(y, bgLine);
}
private LcdImageLine extractBgLine(int lineIndex) {
if ((lcdBank.testBit(Reg.LCDC, LcdcBits.BG)))
return extractLine(lineIndex, BG_LINE_SIZE, LcdcBits.BG_AREA)
.extractWrapped(LCD_WIDTH, lcdBank.get(Reg.SCX));
else
return emptyLine();
}
private LcdImageLine extractLine(int lineIndex, int size, Bit area) {
LcdImageLine.Builder lineBuilder = new LcdImageLine.Builder(size);
int tileSource = lcdBank.testBit(Reg.LCDC, LcdcBits.TILE_SOURCE) ? 1
: 0;
int tileSourceStart = AddressMap.TILE_SOURCE[tileSource];
int tileLineIndex = lineIndex % TILE_EDGE_SIZE;
int address = memoryStart(area)
+ TILES_PER_LINE * (lineIndex / TILE_EDGE_SIZE);
for (int i = 0; i < size / Byte.SIZE; i++) {
int tileIndex = read(address + i);
if (tileSource == 0)
tileIndex = Bits.clip(8, tileIndex + 0x80);
lineBuilder.setBytes(i,
getTileLineVector(tileSourceStart, tileIndex, tileLineIndex,
true),
getTileLineVector(tileSourceStart, tileIndex, tileLineIndex,
false));
}
return lineBuilder.build().mapColors(lcdBank.get(Reg.BGP));
}
private int memoryStart(Bit area) {
int start = lcdBank.testBit(Reg.LCDC, area) ? 1 : 0;
return AddressMap.BG_DISPLAY_DATA[start];
}
private LcdImageLine extractSpritesLine(int y, int[] spritesList, int size,
boolean background) {
LcdImageLine fullSpriteLine = emptyLine();
int spriteTileSourceStart = AddressMap.TILE_SOURCE[1];
for (int spriteIndex : spritesList) {
int spriteMemoryIndex = OAM_START
+ ATTRIBUTE_BYTES_PER_SPRITE * spriteIndex;
int spriteSpec = read(
spriteMemoryIndex + spritesAttributes.SPECS.ordinal());
if (Bits.test(spriteSpec, SpriteSpec.BEHIND_BG) == background) {
int spriteOrdinate = read(spriteMemoryIndex) - 16;
int tileIndex = read(
spriteMemoryIndex + spritesAttributes.INDEX.ordinal());
int tileLineIndex = ((Bits.test(spriteSpec, SpriteSpec.FLIP_V)
? size - 1 - (y - spriteOrdinate)
: (y - spriteOrdinate)));
int tileLineMsb = getTileLineVector(spriteTileSourceStart,
tileIndex, tileLineIndex, true);
int tileLineLsb = getTileLineVector(spriteTileSourceStart,
tileIndex, tileLineIndex, false);
if (Bits.test(spriteSpec, SpriteSpec.FLIP_H)) {
tileLineMsb = Bits.reverse8(tileLineMsb);
tileLineLsb = Bits.reverse8(tileLineLsb);
}
Reg paletteReg = Bits.test(spriteSpec, SpriteSpec.PALETTE)
? Reg.OBP1
: Reg.OBP0;
int palette = lcdBank.get(paletteReg);
int xCoord = read(
spriteMemoryIndex + spritesAttributes.X_COORD.ordinal())
- 8;
LcdImageLine spriteLine = new LcdImageLine.Builder(LCD_WIDTH)
.setBytes(0, tileLineMsb, tileLineLsb).build()
.shift(xCoord)
.mapColors(palette);
fullSpriteLine = spriteLine.below(fullSpriteLine);
}
}
return fullSpriteLine;
}
private int[] spritesIntersectingLine(int y, int size) {
int[] sprites = new int[10];
int nbOfSprites = 0;
for (int i = 0; i < NB_OF_SPRITES; i++) {
int spriteMemoryIndex = OAM_START + ATTRIBUTE_BYTES_PER_SPRITE * i;
int spriteOrdinate = read(spriteMemoryIndex) - 16;
if (nbOfSprites < sprites.length && y >= spriteOrdinate
&& y < spriteOrdinate + size)
sprites[nbOfSprites++] = (read(spriteMemoryIndex
+ spritesAttributes.X_COORD.ordinal()) << Byte.SIZE)
| i;
}
Arrays.sort(sprites, 0, nbOfSprites);
int[] spriteIndex = new int[nbOfSprites];
for (int j = 0; j < spriteIndex.length; j++)
spriteIndex[j] = Bits.clip(8, sprites[j]);
return spriteIndex;
}
private int getTileLineVector(int tileSourceStart, int tileIndex,
int tileLineIndex, boolean msb) {
int address = tileSourceStart + BYTES_PER_TILE * tileIndex
+ 2 * tileLineIndex;
if (msb)
address++;
return Bits.reverse8(read(address));
}
private LcdImageLine emptyLine() {
return new LcdImageLine.Builder(LCD_WIDTH).build();
}
}