Skip to content

Commit

Permalink
Change keyboard configuration to match other emulators.
Browse files Browse the repository at this point in the history
  • Loading branch information
craigthomas committed Apr 23, 2024
1 parent 8820160 commit 93cbb5a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 44 deletions.
43 changes: 19 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,22 @@ customize the operation of the emulator. The Chip 8 has 16 keys:
### Keys
The original Chip 8 had a keypad with the numbered keys 0 - 9 and A - F (16
keys in total). Without any modifications to the emulator, the keys are mapped
as follows:
| Chip 8 Key | Keyboard Key |
| :--------: | :----------: |
| `1` | `4` |
| `2` | `5` |
| `3` | `6` |
| `4` | `7` |
| `5` | `R` |
| `6` | `T` |
| `7` | `Y` |
| `8` | `U` |
| `9` | `F` |
| `0` | `G` |
| `A` | `H` |
| `B` | `J` |
| `C` | `V` |
| `D` | `B` |
| `E` | `N` |
| `F` | `M` |
keys in total). The original key configuration was as follows:
| `1` | `2` | `3` | `C` |
|-----|-----|-----|-----|
| `4` | `5` | `6` | `D` |
| `7` | `8` | `9` | `E` |
| `A` | `0` | `B` | `F` |
The Chip8 emulator maps them to the following keyboard keys by default:
| `1` | `2` | `3` | `4` |
|-----|-----|-----|-----|
| `Q` | `W` | `E` | `R` |
| `A` | `S` | `D` | `F` |
| `Z` | `X` | `C` | `V` |
If you wish to configure a different key-mapping, simply change the `KEY_MAPPINGS` variable
in the configuration file to reflect the mapping that you want. The
Expand All @@ -249,9 +244,9 @@ list of all the valid constants for keyboard key values.
In addition to the key mappings specified in the configuration file, there are additional
keys that impact the execution of the emulator.
| Keyboard Key | Effect |
| :----------: | ------ |
| `ESC` | Quits the emulator |
| Keyboard Key | Effect |
| :----------: |--------------------|
| `ESC` | Quits the emulator |
## ROM Compatibility
Expand Down
32 changes: 16 additions & 16 deletions chip8/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@

# Sets which keys on the keyboard map to the Chip 8 keys
KEY_MAPPINGS = {
0x0: pygame.K_g,
0x1: pygame.K_4,
0x2: pygame.K_5,
0x3: pygame.K_6,
0x4: pygame.K_7,
0x5: pygame.K_r,
0x6: pygame.K_t,
0x7: pygame.K_y,
0x8: pygame.K_u,
0x9: pygame.K_f,
0xA: pygame.K_h,
0xB: pygame.K_j,
0xC: pygame.K_v,
0xD: pygame.K_b,
0xE: pygame.K_n,
0xF: pygame.K_m,
0x0: pygame.K_x,
0x1: pygame.K_1,
0x2: pygame.K_2,
0x3: pygame.K_3,
0x4: pygame.K_q,
0x5: pygame.K_w,
0x6: pygame.K_e,
0x7: pygame.K_a,
0x8: pygame.K_s,
0x9: pygame.K_d,
0xA: pygame.K_z,
0xB: pygame.K_c,
0xC: pygame.K_4,
0xD: pygame.K_r,
0xE: pygame.K_f,
0xF: pygame.K_v,
}

# The font file to use
Expand Down
2 changes: 1 addition & 1 deletion chip8/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def reset(self):
self.rpl = [0] * NUM_REGISTERS


class Chip8CPU(object):
class Chip8CPU:
"""
A class to emulate a Chip 8 CPU. There are several good resources out on
the web that describe the internals of the Chip 8 CPU. For example:
Expand Down
6 changes: 3 additions & 3 deletions test/test_chip8cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def test_operation_9E_pc_skips_if_key_pressed(self):
self.cpu.registers.v[0] = 1
self.cpu.registers.pc = 0
result_table = [False] * 512
result_table[pygame.K_4] = True
result_table[pygame.K_1] = True
with mock.patch("pygame.key.get_pressed", return_value=result_table) as key_mock:
self.cpu.keyboard_routines()
self.assertTrue(key_mock.asssert_called)
Expand Down Expand Up @@ -564,7 +564,7 @@ def test_operation_A1_pc_does_not_skip_if_key_pressed(self):
self.cpu.registers.v[0] = 1
self.cpu.registers.pc = 0
result_table = [False] * 512
result_table[pygame.K_4] = True
result_table[pygame.K_1] = True
with mock.patch("pygame.key.get_pressed", return_value=result_table) as key_mock:
self.cpu.keyboard_routines()
self.assertTrue(key_mock.asssert_called)
Expand Down Expand Up @@ -769,7 +769,7 @@ def test_wait_for_keypress(self):
self.cpu.operand = 0x0
with mock.patch("pygame.event.wait", return_value=event_mock):
result_table = [False] * 512
result_table[pygame.K_4] = True
result_table[pygame.K_1] = True
with mock.patch("pygame.key.get_pressed", return_value=result_table):
self.cpu.wait_for_keypress()
self.assertEqual(0x1, self.cpu.registers.v[0])
Expand Down

0 comments on commit 93cbb5a

Please sign in to comment.