MOS6567R56A.java
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License.
*/
package libsidplay.components.mos656x;
import libsidplay.common.Event;
import libsidplay.common.Event.Phase;
import libsidplay.common.EventScheduler;
import libsidplay.components.pla.PLA;
/**
* Early NTSC VIC-II: 64 cycles per line, 262 lines per frame. Cycle 10 in the shared renderer is hardware raster cycle
* 1. Sprite pointers occupy hardware cycles 59, 61, 63, 1, 3, 5, 7 and 9, unlike the 65-cycle VIC. See VICE's
* viciisc/vicii-chip-model.c, cycle_tab_ntsc_old.
*/
public final class MOS6567R56A extends VIC {
public static final int CYCLES_PER_LINE = 64;
public static final int MAX_RASTERS = 262;
private static final int FIRST_DISPLAY_LINE = 40;
private static final int LAST_DISPLAY_LINE = 13;
public static final int BORDER_HEIGHT = LAST_DISPLAY_LINE - FIRST_DISPLAY_LINE + MAX_RASTERS;
public MOS6567R56A(PLA pla, EventScheduler context) {
super(pla, context, CYCLES_PER_LINE, MAX_RASTERS);
}
@Override
protected int getFirstSpriteFetchCycle() {
return 4;
}
private void doPHI1Fetch() {
if (lineCycle >= 4 && lineCycle <= 19) {
int n = (lineCycle - 4) >> 1;
phi1Data = vicReadMemoryPHI1((lineCycle & 1) == 0
? videoMatrixBase | 0x03f8 | n
: sprites[n].isDMA() ? sprites[n].getCurrentByteAddress() : 0x3fff);
} else if (lineCycle >= 20 && lineCycle <= 24) {
phi1Data = vicReadMemoryPHI1(0x3f00 | (0xff - rasterY * 5 - (lineCycle - 20) & 0xff));
} else if (lineCycle >= 25) {
int address = graphicsIdleFetchAddress();
if (isDisplayActive) {
address = graphicsFetchAddress(vc - vcBase & 0x3ff);
vc = vc + 1 & 0x3ff;
}
phi1Data = vicReadMemoryPHI1(address);
} else {
phi1Data = vicReadMemoryPHI1(0x3fff);
}
}
private void checkSpriteDMA() {
for (Sprite sprite : sprites) {
if (sprite.isEnabled() && sprite.getY() == (rasterY & 0xff)) {
sprite.beginDMA();
}
}
}
private void beginRaster() throws InterruptedException {
if (rasterY == MAX_RASTERS - 1) {
vcBase = 0;
// The raster-zero IRQ edge follows the last-line check by one cycle.
startOfFrame = true;
} else {
rasterY++;
rasterYIRQEdgeDetector.event();
}
if (rasterY == FIRST_DMA_LINE) {
areBadLinesEnabled = readDEN();
}
isBadLine = evaluateIsBadLine();
isDisplayActive |= isBadLine;
int narrowing = readRSEL() ? 0 : 4;
if (rasterY == FIRST_DMA_LINE + 3 + narrowing && readDEN()) {
showBorderVertical = false;
}
if (rasterY == LAST_DMA_LINE + 4 - narrowing) {
showBorderVertical = true;
}
latchedXscroll = xscroll << 2;
if (palEmulation != PALEmulation.NONE) {
palEmulation.determineCurrentPalette(rasterY, rasterY == FIRST_DISPLAY_LINE);
}
if (rasterY == FIRST_DISPLAY_LINE) {
graphicsRendering = true;
}
if (rasterY == LAST_DISPLAY_LINE + 1) {
graphicsRendering = false;
videoDriver.accept(this);
}
}
private final Event event = Event.of("MOS6567R56A", event -> {
lineCycle = lineCycle == CYCLES_PER_LINE ? 1 : lineCycle + 1;
if (graphicsRendering && (lineCycle >= 23 || lineCycle < 71 - CYCLES_PER_LINE)) {
drawSpritesAndGraphics();
} else {
spriteCollisionsOnly();
}
isDisplayActiveForGraphics = isDisplayActive;
doPHI1Fetch();
latchD011FetchMode();
switch (lineCycle) {
case 1: { // Hardware cycle 56: first DMA check and Y expansion.
boolean wasDMA = sprites[0].isDMA();
checkSpriteDMA();
for (Sprite sprite : sprites) sprite.expandYFlipFlop();
setBA(!sprites[0].isDMA(), !wasDMA && sprites[0].isDMA());
break;
}
case 2: // Hardware cycle 57: second DMA check.
checkSpriteDMA();
setBA(!sprites[0].isDMA());
break;
case 3: // Hardware cycle 58: display check precedes sprite 0's fetch.
if (rc == 7) {
vcBase = vc;
isDisplayActive = isBadLine;
}
if (isDisplayActive) rc = rc + 1 & 7;
for (Sprite sprite : sprites) {
if (sprite.isEnabled() && sprite.getY() == (rasterY & 0xff)) sprite.setDisplay(true);
if (!sprite.isDMA()) sprite.setDisplay(false);
sprite.initDmaAccess();
}
setBA(!sprites[0].isDMA() && !sprites[1].isDMA());
break;
case 4:
for (Sprite sprite : sprites) sprite.commitDisplayLatch();
break;
case 10:
beginRaster();
break;
case 11:
if (startOfFrame) {
startOfFrame = false;
rasterY = 0;
rasterYIRQEdgeDetector.event();
lpTriggered = false;
lightpenEdgeDetector();
}
break;
case 20:
setBA(true);
break;
case 21:
setBA(!isBadLine);
break;
case 23:
vc = vcBase;
if (isBadLine) rc = 0;
break;
case 24:
if (isBadLine) doVideoMatrixAccess();
break;
case 25:
if (isBadLine) doVideoMatrixAccess();
for (Sprite sprite : sprites) if (sprite.isDMA()) sprite.finishDmaAccess();
break;
case 64:
setBA(true);
break;
default:
if (lineCycle > 25 && lineCycle < 64 && isBadLine) doVideoMatrixAccess();
break;
}
if (lineCycle >= 4 && lineCycle <= 19) {
int n = (lineCycle - 4) >> 1;
boolean pointer = (lineCycle & 1) == 0;
if (pointer) fetchSpritePointer(n);
else fetchSpriteData(n);
boolean available = !sprites[n].isDMA();
if (n < 7) available &= !sprites[n + 1].isDMA();
if (!pointer && n < 6) available &= !sprites[n + 2].isDMA();
setBA(available);
}
context.schedule(event, 1);
});
@Override
public void reset() {
super.reset();
startOfFrame = false;
lpTriggered = false;
lineCycle = 9;
context.schedule(event, 0, Phase.PHI1);
}
@Override
public int getBorderHeight() {
return BORDER_HEIGHT;
}
@Override
public int getRasterYMax() {
return MAX_RASTERS;
}
}