Skip to content

Commit 20afe71

Browse files
committed
Fix problems with wait for keypresses.
1 parent 89c2b76 commit 20afe71

File tree

7 files changed

+548
-90
lines changed

7 files changed

+548
-90
lines changed

README.md

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,112 @@ with links to public domain repositories where applicable.
196196

197197
### Chip 8 ROMs
198198

199-
| ROM Name | Working | Flags |
200-
|:----------------------------------------------------------------------|:------------------:|:-------------:|
201-
| [down8](https://johnearnest.github.io/chip8Archive/play.html?p=down8) | :heavy_check_mark: | |
199+
| ROM Name | Working | Flags |
200+
|:--------------------------------------------------------------------------------------------------|:------------------:|:-------------:|
201+
| [1D Cellular Automata](https://johnearnest.github.io/chip8Archive/play.html?p=1dcell) | :heavy_check_mark: | |
202+
| [8CE Attourny - Disc 1](https://johnearnest.github.io/chip8Archive/play.html?p=8ceattourny_d1) | :heavy_check_mark: | |
203+
| [8CE Attourny - Disc 2](https://johnearnest.github.io/chip8Archive/play.html?p=8ceattourny_d2) | :question: | |
204+
| [8CE Attourny - Disc 3](https://johnearnest.github.io/chip8Archive/play.html?p=8ceattourny_d3) | :question: | |
205+
| [Bad Kaiju Ju](https://johnearnest.github.io/chip8Archive/play.html?p=BadKaiJuJu) | :heavy_check_mark: | |
206+
| [Br8kout](https://johnearnest.github.io/chip8Archive/play.html?p=br8kout) | :heavy_check_mark: | |
207+
| [Carbon8](https://johnearnest.github.io/chip8Archive/play.html?p=carbon8) | :heavy_check_mark: | |
208+
| [Cave Explorer](https://johnearnest.github.io/chip8Archive/play.html?p=caveexplorer) | :heavy_check_mark: | |
209+
| [Chipquarium](https://johnearnest.github.io/chip8Archive/play.html?p=chipquarium) | :heavy_check_mark: | |
210+
| [Danm8ku](https://johnearnest.github.io/chip8Archive/play.html?p=danm8ku) | :x: | |
211+
| [down8](https://johnearnest.github.io/chip8Archive/play.html?p=down8) | :heavy_check_mark: | |
212+
| [Falling Ghosts](https://veganjay.itch.io/falling-ghosts) | :question: | |
213+
| [Flight Runner](https://johnearnest.github.io/chip8Archive/play.html?p=flightrunner) | :question: | |
214+
| [Fuse](https://johnearnest.github.io/chip8Archive/play.html?p=fuse) | :question: | |
215+
| [Ghost Escape](https://johnearnest.github.io/chip8Archive/play.html?p=ghostEscape) | :question: | |
216+
| [Glitch Ghost](https://johnearnest.github.io/chip8Archive/play.html?p=glitchGhost) | :question: | |
217+
| [Horse World Online](https://johnearnest.github.io/chip8Archive/play.html?p=horseWorldOnline) | :question: | |
218+
| [Invisible Man](https://mremerson.itch.io/invisible-man) | :question: | `clip_quirks` |
219+
| [Knumber Knower](https://internet-janitor.itch.io/knumber-knower) | :question: | |
220+
| [Masquer8](https://johnearnest.github.io/chip8Archive/play.html?p=masquer8) | :question: | |
221+
| [Mastermind](https://johnearnest.github.io/chip8Archive/play.html?p=mastermind) | :question: | |
222+
| [Mini Lights Out](https://johnearnest.github.io/chip8Archive/play.html?p=mini-lights-out) | :question: | |
223+
| [Octo: a Chip 8 Story](https://johnearnest.github.io/chip8Archive/play.html?p=octoachip8story) | :question: | |
224+
| [Octogon Trail](https://tarsi.itch.io/octogon-trail) | :question: | |
225+
| [Octojam 1 Title](https://johnearnest.github.io/chip8Archive/play.html?p=octojam1title) | :question: | |
226+
| [Octojam 2 Title](https://johnearnest.github.io/chip8Archive/play.html?p=octojam2title) | :question: | |
227+
| [Octojam 3 Title](https://johnearnest.github.io/chip8Archive/play.html?p=octojam3title) | :question: | |
228+
| [Octojam 4 Title](https://johnearnest.github.io/chip8Archive/play.html?p=octojam4title) | :question: | |
229+
| [Octojam 5 Title](https://johnearnest.github.io/chip8Archive/play.html?p=octojam5title) | :question: | |
230+
| [Octojam 6 Title](https://johnearnest.github.io/chip8Archive/play.html?p=octojam6title) | :question: | |
231+
| [Octojam 7 Title](https://johnearnest.github.io/chip8Archive/play.html?p=octojam7title) | :question: | |
232+
| [Octojam 8 Title](https://johnearnest.github.io/chip8Archive/play.html?p=octojam8title) | :question: | |
233+
| [Octojam 9 Title](https://johnearnest.github.io/chip8Archive/play.html?p=octojam9title) | :question: | |
234+
| [Octojam 10 Title](https://johnearnest.github.io/chip8Archive/play.html?p=octojam10title) | :question: | |
235+
| [Octo Rancher](https://johnearnest.github.io/chip8Archive/play.html?p=octorancher) | :question: | |
236+
| [Outlaw](https://johnearnest.github.io/chip8Archive/play.html?p=outlaw) | :question: | |
237+
| [Pet Dog](https://johnearnest.github.io/chip8Archive/play.html?p=petdog) | :question: | |
238+
| [Piper](https://johnearnest.github.io/chip8Archive/play.html?p=piper) | :question: | |
239+
| [Pumpkin "Dress" Up](https://johnearnest.github.io/chip8Archive/play.html?p=pumpkindressup) | :question: | |
240+
| [RPS](https://johnearnest.github.io/chip8Archive/play.html?p=RPS) | :question: | |
241+
| [Slippery Slope](https://johnearnest.github.io/chip8Archive/play.html?p=slipperyslope) | :question: | |
242+
| [Snek](https://johnearnest.github.io/chip8Archive/play.html?p=snek) | :question: | |
243+
| [Space Jam](https://johnearnest.github.io/chip8Archive/play.html?p=spacejam) | :question: | |
244+
| [Spock Paper Scissors](https://johnearnest.github.io/chip8Archive/play.html?p=spockpaperscissors) | :question: | |
245+
| [Super Pong](https://johnearnest.github.io/chip8Archive/play.html?p=superpong) | :question: | |
246+
| [Tank!](https://johnearnest.github.io/chip8Archive/play.html?p=tank) | :question: | |
247+
| [TOMB STON TIPP](https://johnearnest.github.io/chip8Archive/play.html?p=tombstontipp) | :question: | |
248+
| [WDL](https://johnearnest.github.io/chip8Archive/play.html?p=wdl) | :question: | |
249+
250+
### Super Chip ROMs
251+
252+
| ROM Name | Working | Flags |
253+
|:---------------------------------------------------------------------------------------------|:-----------:|:-----:|
254+
| [Applejak](https://johnearnest.github.io/chip8Archive/play.html?p=applejak) | :question: | |
255+
| [Bulb](https://johnearnest.github.io/chip8Archive/play.html?p=bulb) | :question: | |
256+
| [Black Rainbow](https://johnearnest.github.io/chip8Archive/play.html?p=blackrainbow) | :question: | |
257+
| [Chipcross](https://tobiasvl.itch.io/chipcross) | :question: | |
258+
| [Chipolarium](https://tobiasvl.itch.io/chipolarium) | :question: | |
259+
| [Collision Course](https://ninjaweedle.itch.io/collision-course) | :question: | |
260+
| [Dodge](https://johnearnest.github.io/chip8Archive/play.html?p=dodge) | :question: | |
261+
| [DVN8](https://johnearnest.github.io/chip8Archive/play.html?p=DVN8) | :question: | |
262+
| [Eaty the Alien](https://johnearnest.github.io/chip8Archive/play.html?p=eaty) | :question: | |
263+
| [Grad School Simulator 2014](https://johnearnest.github.io/chip8Archive/play.html?p=gradsim) | :question: | |
264+
| [Horsey Jump](https://johnearnest.github.io/chip8Archive/play.html?p=horseyJump) | :question: | |
265+
| [Knight](https://johnearnest.github.io/chip8Archive/play.html?p=knight) | :x: | |
266+
| [Mondri8](https://johnearnest.github.io/chip8Archive/play.html?p=mondri8) | :question: | |
267+
| [Octopeg](https://johnearnest.github.io/chip8Archive/play.html?p=octopeg) | :question: | |
268+
| [Octovore](https://johnearnest.github.io/chip8Archive/play.html?p=octovore) | :question: | |
269+
| [Rocto](https://johnearnest.github.io/chip8Archive/play.html?p=rockto) | :question: | |
270+
| [Sens8tion](https://johnearnest.github.io/chip8Archive/play.html?p=sens8tion) | :question: | |
271+
| [Snake](https://johnearnest.github.io/chip8Archive/play.html?p=snake) | :question: | |
272+
| [Squad](https://johnearnest.github.io/chip8Archive/play.html?p=squad) | :question: | |
273+
| [Sub-Terr8nia](https://johnearnest.github.io/chip8Archive/play.html?p=sub8) | :question: | |
274+
| [Super Octogon](https://johnearnest.github.io/chip8Archive/play.html?p=octogon) | :question: | |
275+
| [Super Square](https://johnearnest.github.io/chip8Archive/play.html?p=supersquare) | :question: | |
276+
| [The Binding of COSMAC](https://johnearnest.github.io/chip8Archive/play.html?p=binding) | :question: | |
277+
| [Turnover '77](https://johnearnest.github.io/chip8Archive/play.html?p=turnover77) | :question: | |
278+
279+
### XO Chip ROMs
280+
281+
| ROM Name | Working | Flags |
282+
|:------------------------------------------------------------------------------------------------------|:-----------:|:-----:|
283+
| [An Evening to Die For](https://johnearnest.github.io/chip8Archive/play.html?p=anEveningToDieFor) | :question: | |
284+
| [Business Is Contagious](https://johnearnest.github.io/chip8Archive/play.html?p=businessiscontagious) | :question: | |
285+
| [Chicken Scratch](https://johnearnest.github.io/chip8Archive/play.html?p=chickenScratch) | :question: | |
286+
| [Civiliz8n](https://johnearnest.github.io/chip8Archive/play.html?p=civiliz8n) | :question: | |
287+
| [Flutter By](https://johnearnest.github.io/chip8Archive/play.html?p=flutterby) | :question: | |
288+
| [Into The Garlicscape](https://johnearnest.github.io/chip8Archive/play.html?p=garlicscape) | :question: | |
289+
| [jub8 Song 1](https://johnearnest.github.io/chip8Archive/play.html?p=jub8-1) | :question: | |
290+
| [jub8 Song 2](https://johnearnest.github.io/chip8Archive/play.html?p=jub8-2) | :question: | |
291+
| [Kesha Was Biird](https://johnearnest.github.io/chip8Archive/play.html?p=keshaWasBiird) | :question: | |
292+
| [Kesha Was Niinja](https://johnearnest.github.io/chip8Archive/play.html?p=keshaWasNiinja) | :question: | |
293+
| [Octo paint](https://johnearnest.github.io/chip8Archive/play.html?p=octopaint) | :question: | |
294+
| [Octo Party Mix!](https://johnearnest.github.io/chip8Archive/play.html?p=OctoPartyMix) | :question: | |
295+
| [Octoma](https://johnearnest.github.io/chip8Archive/play.html?p=octoma) | :question: | |
296+
| [Red October V](https://johnearnest.github.io/chip8Archive/play.html?p=redOctober) | :question: | |
297+
| [Skyward](https://johnearnest.github.io/chip8Archive/play.html?p=skyward) | :question: | |
298+
| [Spock Paper Scissors](https://johnearnest.github.io/chip8Archive/play.html?p=spockpaperscissors) | :question: | |
299+
| [T8NKS](https://johnearnest.github.io/chip8Archive/play.html?p=t8nks) | :question: | |
300+
| [Tapeworm](https://tarsi.itch.io/tapeworm) | :question: | |
301+
| [Truck Simul8or](https://johnearnest.github.io/chip8Archive/play.html?p=trucksimul8or) | :question: | |
302+
| [SK8 H8 1988](https://johnearnest.github.io/chip8Archive/play.html?p=sk8) | :question: | |
303+
| [Super NeatBoy](https://johnearnest.github.io/chip8Archive/play.html?p=superneatboy) | :question: | |
304+
| [Wonky Pong](https://johnearnest.github.io/chip8Archive/play.html?p=wonkypong) | :question: | |
202305

203306
## Third Party Licenses and Attributions
204307

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

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ public class CentralProcessingUnit extends Thread
101101
// The current operating mode for the CPU
102102
protected int mode;
103103

104-
public static final int DEFAULT_CPU_CYCLE_TIME = 0;
104+
public static final int DEFAULT_CPU_CYCLE_TIME = 1;
105+
106+
private boolean awaitingKeypress = false;
105107

106108
CentralProcessingUnit(Memory memory, Keyboard keyboard, Screen screen) {
107109
this.random = new Random();
@@ -698,31 +700,29 @@ protected void generateRandomNumber() {
698700
* will be set to 1.
699701
*/
700702
protected void drawSprite() {
701-
int xRegister = (operand & 0x0F00) >> 8;
702-
int yRegister = (operand & 0x00F0) >> 4;
703-
int xPos = v[xRegister];
704-
int yPos = v[yRegister];
703+
int x = (operand & 0x0F00) >> 8;
704+
int y = (operand & 0x00F0) >> 4;
705705
int numBytes = (operand & 0xF);
706706
v[0xF] = 0;
707707

708708
String drawOperation = "DRAW";
709709
if ((mode == MODE_EXTENDED) && (numBytes == 0)) {
710710
if (bitplane == 3) {
711-
drawExtendedSprite(xPos, yPos, 1);
712-
drawExtendedSprite(xPos, yPos, 2);
711+
drawExtendedSprite(v[x], v[y], 1, index);
712+
drawExtendedSprite(v[x], v[y], 2, index + 32);
713713
} else {
714-
drawExtendedSprite(xPos, yPos, bitplane);
714+
drawExtendedSprite(v[x], v[y], bitplane, index);
715715
}
716716
drawOperation = "DRAWEX";
717717
} else {
718718
if (bitplane == 3) {
719-
drawNormalSprite(xPos, yPos, numBytes, 1);
720-
drawNormalSprite(xPos, yPos, numBytes, 2);
719+
drawNormalSprite(v[x], v[y], numBytes, 1, index);
720+
drawNormalSprite(v[x], v[y], numBytes, 2, index + numBytes);
721721
} else {
722-
drawNormalSprite(xPos, yPos, numBytes, bitplane);
722+
drawNormalSprite(v[x], v[y], numBytes, bitplane, index);
723723
}
724724
}
725-
lastOpDesc = drawOperation + " V" + toHex(xRegister, 1) + ", V" + toHex(yRegister, 1);
725+
lastOpDesc = drawOperation + " V" + toHex(x, 1) + ", V" + toHex(y, 1);
726726
}
727727

728728
/**
@@ -732,26 +732,30 @@ protected void drawSprite() {
732732
* @param xPos the x position to draw the sprite at
733733
* @param yPos the y position to draw the sprite at
734734
* @param bitplane the bitplane to draw to
735+
* @param activeIndex the effective index to use when loading sprite data
735736
*/
736-
private void drawExtendedSprite(int xPos, int yPos, int bitplane) {
737+
private void drawExtendedSprite(int xPos, int yPos, int bitplane, int activeIndex) {
737738
for (int yIndex = 0; yIndex < 16; yIndex++) {
738739
for (int xByte = 0; xByte < 2; xByte++) {
739-
short colorByte = memory.read(index + (yIndex * 2) + xByte);
740+
short colorByte = memory.read(activeIndex + (yIndex * 2) + xByte);
740741
int yCoord = yPos + yIndex;
741-
yCoord = yCoord % screen.getHeight();
742-
743-
int mask = 0x80;
744-
745-
for (int xIndex = 0; xIndex < 8; xIndex++) {
746-
int xCoord = xPos + xIndex + (xByte * 8);
747-
xCoord = xCoord % screen.getWidth();
748-
749-
boolean turnOn = (colorByte & mask) > 0;
750-
boolean currentOn = screen.getPixel(xCoord, yCoord, bitplane);
751-
752-
v[0xF] += (turnOn && currentOn) ? (short) 1 : (short) 0;
753-
screen.drawPixel(xCoord, yCoord, turnOn ^ currentOn, bitplane);
754-
mask = mask >> 1;
742+
if (yCoord < screen.getHeight()) {
743+
yCoord = yCoord % screen.getHeight();
744+
int mask = 0x80;
745+
746+
for (int xIndex = 0; xIndex < 8; xIndex++) {
747+
int xCoord = xPos + xIndex + (xByte * 8);
748+
xCoord = xCoord % screen.getWidth();
749+
750+
boolean turnOn = (colorByte & mask) > 0;
751+
boolean currentOn = screen.getPixel(xCoord, yCoord, bitplane);
752+
753+
v[0xF] += (turnOn && currentOn) ? (short) 1 : (short) 0;
754+
screen.drawPixel(xCoord, yCoord, turnOn ^ currentOn, bitplane);
755+
mask = mask >> 1;
756+
}
757+
} else {
758+
v[0xF] += 1;
755759
}
756760
}
757761
}
@@ -764,10 +768,11 @@ private void drawExtendedSprite(int xPos, int yPos, int bitplane) {
764768
* @param yPos the Y position of the sprite
765769
* @param numBytes the number of bytes to draw
766770
* @param bitplane the bitplane to draw to
771+
* @param activeIndex the effective index to use when loading sprite data
767772
*/
768-
private void drawNormalSprite(int xPos, int yPos, int numBytes, int bitplane) {
773+
private void drawNormalSprite(int xPos, int yPos, int numBytes, int bitplane, int activeIndex) {
769774
for (int yIndex = 0; yIndex < numBytes; yIndex++) {
770-
short colorByte = memory.read(index + yIndex);
775+
short colorByte = memory.read(activeIndex + yIndex);
771776
int yCoord = yPos + yIndex;
772777
yCoord = yCoord % screen.getHeight();
773778

@@ -909,18 +914,32 @@ protected void moveDelayTimerIntoRegister() {
909914
* into the specified register.
910915
*/
911916
protected void waitForKeypress() {
912-
int x = (operand & 0x0F00) >> 8;
917+
awaitingKeypress = true;
918+
}
919+
920+
/**
921+
* Returns whether the CPU is waiting for a keypress before continuing.
922+
*
923+
* @return false if the CPU is waiting for a keypress, true otherwise
924+
*/
925+
protected boolean isAwaitingKeypress() {
926+
return awaitingKeypress;
927+
}
928+
929+
/**
930+
* Reads a keypress from keyboard, decodes it, and places the value in the
931+
* specified register. If no key is waiting, returns without doing anything.
932+
*/
933+
protected void decodeKeypressAndContinue() {
913934
int currentKey = keyboard.getCurrentKey();
914-
while (currentKey == 0) {
915-
try {
916-
Thread.sleep(300);
917-
} catch (InterruptedException e) {
918-
e.printStackTrace();
919-
}
920-
currentKey = keyboard.getCurrentKey();
935+
if (currentKey == -1) {
936+
return;
921937
}
938+
939+
int x = (operand & 0x0F00) >> 8;
922940
v[x] = (short) currentKey;
923941
lastOpDesc = "KEYD V" + toHex(x, 1);
942+
awaitingKeypress = false;
924943
}
925944

926945
/**
@@ -1077,6 +1096,7 @@ public void reset() {
10771096
if (screen != null) {
10781097
screen.clearScreen(bitplane);
10791098
}
1099+
awaitingKeypress = false;
10801100
}
10811101

10821102
/**

0 commit comments

Comments
 (0)