Skip to content

Commit 796038d

Browse files
authored
XO chip documentation and keyboard bugfix (#59)
* Update XO documentation and XO fixes. * Add more games testing to the readme. * Add compatibility notes, bump version number to build. * Update single line in README to describe octo compatibility.
1 parent cebe8a9 commit 796038d

File tree

7 files changed

+200
-117
lines changed

7 files changed

+200
-117
lines changed

README.md

Lines changed: 145 additions & 98 deletions
Large diffs are not rendered by default.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212
}
1313

1414
group = 'ca.craigthomas'
15-
version = '1.0.1'
15+
version = '2.0.0'
1616

1717
mainClassName = 'ca.craigthomas.chip8java.emulator.runner.Runner'
1818

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ private void drawNormalSprite(int xPos, int yPos, int numBytes, int bitplane, in
976976
protected void skipIfKeyPressed() {
977977
int x = (operand & 0x0F00) >> 8;
978978
int keyToCheck = v[x];
979-
if (keyboard.getCurrentKey() == keyToCheck) {
979+
if (keyboard.isKeyPressed(keyToCheck)) {
980980
pc += 2;
981981
if (memory.read(pc - 2) == 0xF0 && memory.read(pc - 1) == 0x00) {
982982
pc += 2;
@@ -993,7 +993,7 @@ protected void skipIfKeyPressed() {
993993
protected void skipIfKeyNotPressed() {
994994
int x = (operand & 0x0F00) >> 8;
995995
int keyToCheck = v[x];
996-
if (keyboard.getCurrentKey() != keyToCheck) {
996+
if (!keyboard.isKeyPressed(keyToCheck)) {
997997
pc += 2;
998998
if (memory.read(pc - 2) == 0xF0 && memory.read(pc - 1) == 0x00) {
999999
pc += 2;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public Emulator(
141141
}
142142

143143
cpuCycleTime = cycleTime;
144-
keyboard = new Keyboard(this);
144+
keyboard = new Keyboard();
145145
memory = new Memory(memSize4k);
146146
screen = new Screen(scale, converted_color0, converted_color1, converted_color2, converted_color3);
147147
cpu = new CentralProcessingUnit(memory, keyboard, screen);

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public class Keyboard extends KeyAdapter
1515
{
1616
// Map from a keypress event to key values
17-
public static final int[] sKeycodeMap = {
17+
public static final int[] keycodeMap = {
1818
KeyEvent.VK_X, // 0x0
1919
KeyEvent.VK_1, // 0x1
2020
KeyEvent.VK_2, // 0x2
@@ -33,6 +33,10 @@ public class Keyboard extends KeyAdapter
3333
KeyEvent.VK_V, // 0xF
3434
};
3535

36+
public boolean [] keypressMap = {
37+
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
38+
};
39+
3640
// The current key being pressed, -1 if no key
3741
protected int currentKeyPressed = -1;
3842

@@ -42,17 +46,27 @@ public class Keyboard extends KeyAdapter
4246
// The key to quit the emulator
4347
protected static final int CHIP8_QUIT = KeyEvent.VK_ESCAPE;
4448

45-
public Keyboard(Emulator emulator) {}
49+
public Keyboard() {}
4650

4751
@Override
4852
public void keyPressed(KeyEvent e) {
4953
rawKeyPressed = e.getKeyCode();
54+
for (int x = 0; x < 16; x++) {
55+
if (rawKeyPressed == keycodeMap[x]) {
56+
keypressMap[x] = true;
57+
}
58+
}
5059
currentKeyPressed = mapKeycodeToChip8Key(rawKeyPressed);
5160
}
5261

5362
@Override
5463
public void keyReleased(KeyEvent e) {
55-
currentKeyPressed = -1;
64+
rawKeyPressed = e.getKeyCode();
65+
for (int x = 0; x < 16; x++) {
66+
if (rawKeyPressed == keycodeMap[x]) {
67+
keypressMap[x] = false;
68+
}
69+
}
5670
}
5771

5872
/**
@@ -65,8 +79,8 @@ public void keyReleased(KeyEvent e) {
6579
* @return The Chip 8 key value for the specified keycode
6680
*/
6781
public int mapKeycodeToChip8Key(int keycode) {
68-
for (int i = 0; i < sKeycodeMap.length; i++) {
69-
if (sKeycodeMap[i] == keycode) {
82+
for (int i = 0; i < keycodeMap.length; i++) {
83+
if (keycodeMap[i] == keycode) {
7084
return i;
7185
}
7286
}
@@ -82,6 +96,20 @@ public int getCurrentKey() {
8296
return currentKeyPressed;
8397
}
8498

99+
/**
100+
* Returns true if the specified key in the keymap is currently reported
101+
* as being pressed.
102+
*
103+
* @param key the key number in the keymap to check for
104+
* @return true if the key is pressed
105+
*/
106+
public boolean isKeyPressed(int key) {
107+
if (key >= 0 && key < 16) {
108+
return keypressMap[key];
109+
}
110+
return false;
111+
}
112+
85113
/**
86114
* Returns the currently pressed debug key. Will return 0 if no debug key was
87115
* pressed.

src/test/java/ca/craigthomas/chip8java/emulator/components/CentralProcessingUnitTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@ public void testReadRegistersFromMemoryIndexQuirks() {
705705

706706
@Test
707707
public void testSkipIfKeyPressedSkipsCorrectly() {
708+
Keyboard keyboard = new Keyboard();
709+
keyboard.keypressMap[9] = true;
710+
cpu = new CentralProcessingUnit(memory, keyboard, screen);
708711
for (int register = 0; register < 0xF; register++) {
709712
cpu.v[register] = 9;
710713
cpu.operand = register << 8;
@@ -738,6 +741,9 @@ public void testSkipIfKeyNotPressedSkipsCorrectly() {
738741

739742
@Test
740743
public void testSkipIfKeyNotPressedDoesNotSkipIfPressed() {
744+
Keyboard keyboard = new Keyboard();
745+
cpu = new CentralProcessingUnit(memory, keyboard, screen);
746+
keyboard.keypressMap[9] = true;
741747
for (int register = 0; register < 0xF; register++) {
742748
cpu.v[register] = 9;
743749
cpu.operand = register << 8;

src/test/java/ca/craigthomas/chip8java/emulator/components/KeyboardTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ public class KeyboardTest
2525

2626
@Before
2727
public void setUp() {
28-
emulator = Mockito.mock(Emulator.class);
29-
keyboard = new Keyboard(emulator);
28+
keyboard = new Keyboard();
3029
event = mock(KeyEvent.class);
3130
}
3231

3332
@Test
3433
public void testMapKeycodeToChip8Key() {
35-
for (int index = 0; index < Keyboard.sKeycodeMap.length; index++) {
36-
assertEquals(index, keyboard.mapKeycodeToChip8Key(Keyboard.sKeycodeMap[index]));
34+
for (int index = 0; index < Keyboard.keycodeMap.length; index++) {
35+
assertEquals(index, keyboard.mapKeycodeToChip8Key(Keyboard.keycodeMap[index]));
3736
}
3837
}
3938

@@ -54,16 +53,19 @@ public void testGetDebugKey() {
5453
}
5554

5655
@Test
57-
public void testKeyReleased() {
58-
keyboard.currentKeyPressed = 1;
59-
keyboard.keyReleased(null);
60-
assertEquals(-1, keyboard.currentKeyPressed);
56+
public void testKeyPressedWorksCorrectly() {
57+
when(event.getKeyCode()).thenReturn(KeyEvent.VK_2);
58+
keyboard.keyPressed(event);
59+
assertEquals(2, keyboard.currentKeyPressed);
6160
}
6261

6362
@Test
64-
public void testKeyPressedWorksCorrectly() {
63+
public void testKeyReleased() {
6564
when(event.getKeyCode()).thenReturn(KeyEvent.VK_2);
6665
keyboard.keyPressed(event);
67-
assertEquals(2, keyboard.currentKeyPressed);
66+
assertTrue(keyboard.isKeyPressed(2));
67+
68+
keyboard.keyReleased(event);
69+
assertFalse(keyboard.isKeyPressed(2));
6870
}
6971
}

0 commit comments

Comments
 (0)