Skip to content

Commit aa51567

Browse files
authored
Implement clip quirks (#55)
1 parent 185a15c commit aa51567

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

src/main/java/ca/craigthomas/chip8java/emulator/components/CentralProcessingUnit.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public class CentralProcessingUnit extends Thread
118118
// Whether index quirks are enabled
119119
private boolean indexQuirks = false;
120120

121+
// Whether clip quirks are enabled
122+
private boolean clipQuirks = false;
123+
121124
CentralProcessingUnit(Memory memory, Keyboard keyboard, Screen screen) {
122125
this.random = new Random();
123126
this.memory = memory;
@@ -179,6 +182,15 @@ public void setIndexQuirks(boolean enableQuirk) {
179182
indexQuirks = enableQuirk;
180183
}
181184

185+
/**
186+
* Sets the clipQuirks to true or false.
187+
*
188+
* @param enableQuirk a boolean enabling clip quirks or disabling clip quirks
189+
*/
190+
public void setClipQuirks(boolean enableQuirk) {
191+
clipQuirks = enableQuirk;
192+
}
193+
182194
/**
183195
* Fetch the next instruction from memory, increment the program counter
184196
* to the next instruction, and execute the instruction.
@@ -883,14 +895,16 @@ private void drawExtendedSprite(int xPos, int yPos, int bitplane, int activeInde
883895

884896
for (int xIndex = 0; xIndex < 8; xIndex++) {
885897
int xCoord = xPos + xIndex + (xByte * 8);
886-
xCoord = xCoord % screen.getWidth();
898+
if ((!clipQuirks) || (xCoord < screen.getWidth())) {
899+
xCoord = xCoord % screen.getWidth();
887900

888-
boolean turnedOn = (colorByte & mask) > 0;
889-
boolean currentOn = screen.getPixel(xCoord, yCoord, bitplane);
901+
boolean turnedOn = (colorByte & mask) > 0;
902+
boolean currentOn = screen.getPixel(xCoord, yCoord, bitplane);
890903

891-
v[0xF] += (turnedOn && currentOn) ? (short) 1 : (short) 0;
892-
screen.drawPixel(xCoord, yCoord, turnedOn ^ currentOn, bitplane);
893-
mask = (short) (mask >> 1);
904+
v[0xF] += (turnedOn && currentOn) ? (short) 1 : (short) 0;
905+
screen.drawPixel(xCoord, yCoord, turnedOn ^ currentOn, bitplane);
906+
mask = (short) (mask >> 1);
907+
}
894908
}
895909
} else {
896910
v[0xF] += 1;
@@ -912,20 +926,22 @@ private void drawNormalSprite(int xPos, int yPos, int numBytes, int bitplane, in
912926
for (int yIndex = 0; yIndex < numBytes; yIndex++) {
913927
short colorByte = memory.read(activeIndex + yIndex);
914928
int yCoord = yPos + yIndex;
915-
yCoord = yCoord % screen.getHeight();
916-
917-
short mask = 0x80;
918-
919-
for (int xIndex = 0; xIndex < 8; xIndex++) {
920-
int xCoord = xPos + xIndex;
921-
xCoord = xCoord % screen.getWidth();
929+
if ((!clipQuirks) || (yCoord < screen.getHeight())) {
930+
yCoord = yCoord % screen.getHeight();
931+
short mask = 0x80;
932+
for (int xIndex = 0; xIndex < 8; xIndex++) {
933+
int xCoord = xPos + xIndex;
934+
if ((!clipQuirks) || (xCoord < screen.getWidth())) {
935+
xCoord = xCoord % screen.getWidth();
922936

923-
boolean turnedOn = (colorByte & mask) > 0;
924-
boolean currentOn = screen.getPixel(xCoord, yCoord, bitplane);
937+
boolean turnedOn = (colorByte & mask) > 0;
938+
boolean currentOn = screen.getPixel(xCoord, yCoord, bitplane);
925939

926-
v[0xF] |= (turnedOn && currentOn) ? (short) 1 : (short) 0;
927-
screen.drawPixel(xCoord, yCoord, turnedOn ^ currentOn, bitplane);
928-
mask = (short) (mask >> 1);
940+
v[0xF] |= (turnedOn && currentOn) ? (short) 1 : (short) 0;
941+
screen.drawPixel(xCoord, yCoord, turnedOn ^ currentOn, bitplane);
942+
mask = (short) (mask >> 1);
943+
}
944+
}
929945
}
930946
}
931947
}

src/main/java/ca/craigthomas/chip8java/emulator/components/Emulator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class Emulator
5454
* screen scale, a cycle time of 0, a null rom, and trace mode off.
5555
*/
5656
public Emulator() {
57-
this(1, 0, null, false, "#000000", "#666666", "#BBBBBB", "#FFFFFF", false, false, false, false);
57+
this(1, 0, null, false, "#000000", "#666666", "#BBBBBB", "#FFFFFF", false, false, false, false, false);
5858
}
5959

6060
/**
@@ -71,6 +71,7 @@ public Emulator() {
7171
* @param shiftQuirks whether to enable shift quirks or not
7272
* @param logicQuirks whether to enable logic quirks or not
7373
* @param jumpQuirks whether to enable logic quirks or not
74+
* @param clipQuirks whether to enable clip quirks or not
7475
*/
7576
public Emulator(
7677
int scale,
@@ -84,7 +85,8 @@ public Emulator(
8485
boolean shiftQuirks,
8586
boolean logicQuirks,
8687
boolean jumpQuirks,
87-
boolean indexQuirks
88+
boolean indexQuirks,
89+
boolean clipQuirks
8890
) {
8991
if (color0.length() != 6) {
9092
System.out.println("color_0 parameter must be 6 characters long");
@@ -147,6 +149,7 @@ public Emulator(
147149
cpu.setLogicQuirks(logicQuirks);
148150
cpu.setJumpQuirks(jumpQuirks);
149151
cpu.setIndexQuirks(indexQuirks);
152+
cpu.setClipQuirks(clipQuirks);
150153

151154
// Load the font file into memory
152155
InputStream fontFileStream = IO.openInputStreamFromResource(FONT_FILE);

src/main/java/ca/craigthomas/chip8java/emulator/runner/Arguments.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ public class Arguments
4747

4848
@Parameter(names={"--index_quirks"}, description="enable index quirks")
4949
public Boolean indexQuirks = false;
50+
51+
@Parameter(names={"--clip_quirks"}, description="enable clip quirks")
52+
public Boolean clipQuirks = false;
5053
}

src/main/java/ca/craigthomas/chip8java/emulator/runner/Runner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public static void main(String[] argv) {
3939
args.shiftQuirks,
4040
args.logicQuirks,
4141
args.jumpQuirks,
42-
args.indexQuirks
42+
args.indexQuirks,
43+
args.clipQuirks
4344
);
4445
emulator.start();
4546
}

0 commit comments

Comments
 (0)