Skip to content

Commit ea00b42

Browse files
authored
Merge pull request #13 from craigthomas/shl-shr-fix
Update SHL and SHR to use target register.
2 parents cb38c52 + a7d11f1 commit ea00b42

File tree

5 files changed

+84
-48
lines changed

5 files changed

+84
-48
lines changed

README.md

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
2. [Screen Scale](#screen-scale)
1919
3. [Execution Delay](#execution-delay)
2020
5. [Customization](#customization)
21+
1. [Keys](#keys)
22+
2. [Debug Keys](#debug-keys)
2123
6. [Further Documentation](#further-documentation)
2224

2325
## What is it?
@@ -45,7 +47,7 @@ Copy the source files to a directory of your choice. In addition to
4547
the source, you will need the following required software packages:
4648

4749
* [Python 2.7](http://www.python.org)
48-
* [pygame](http://http://www.pygame.org)
50+
* [pygame](http://www.pygame.org)
4951

5052
I strongly recommend creating a virtual environment using the
5153
[virtualenv](http://pypi.python.org/pypi/virtualenv) builder as well as the
@@ -199,18 +201,46 @@ set amount of time).
199201
## Customization
200202
201203
The file `chip8/config.py` contains several variables that can be changed to
202-
customize the operation of the emulator. The most important one is the
203-
`KEY_MAPPINGS` variable. The Chip 8 has 16 keys:
204-
205-
* The keys 0-9
206-
* The letters A-F
207-
208-
The default configuration of the emulator will map the keypad numeric keys
209-
0-9 to the keys 0-9, and the keyboard keys a-f onto A-F. If you wish to
210-
configure a different key-mapping, simply change the variable to reflect
211-
the mapping that you want. The [pygame.key](http://pygame.readthedocs.org/en/latest/ref/key.html)
212-
documentation contains a list of all the valid constants for keyboard
213-
key values.
204+
customize the operation of the emulator. The Chip 8 has 16 keys:
205+
206+
### Keys
207+
208+
The original Chip 8 had a keypad with the numbered keys 0 - 9 and A - F (16
209+
keys in total). Without any modifications to the emulator, the keys are mapped
210+
as follows:
211+
212+
| Chip 8 Key | Keyboard Key |
213+
| :--------: | :----------: |
214+
| `1` | `4` |
215+
| `2` | `5` |
216+
| `3` | `6` |
217+
| `4` | `7` |
218+
| `5` | `R` |
219+
| `6` | `T` |
220+
| `7` | `Y` |
221+
| `8` | `U` |
222+
| `9` | `F` |
223+
| `0` | `G` |
224+
| `A` | `H` |
225+
| `B` | `J` |
226+
| `C` | `V` |
227+
| `D` | `B` |
228+
| `E` | `N` |
229+
| `F` | `M` |
230+
231+
If you wish to configure a different key-mapping, simply change the `KEY_MAPPINGS` variable
232+
in the configuration file to reflect the mapping that you want. The
233+
[pygame.key](https://www.pygame.org/docs/ref/key.html) documentation contains a
234+
list of all the valid constants for keyboard key values.
235+
236+
### Debug Keys
237+
238+
In addition to the key mappings specified in the configuration file, there are additional
239+
keys that impact the execution of the emulator.
240+
241+
| Keyboard Key | Effect |
242+
| :----------: | ------ |
243+
| `ESC` | Quits the emulator |
214244
215245
216246
## Further Documentation

chip8/config.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (C) 2012 Craig Thomas
2+
Copyright (C) 2012-2019 Craig Thomas
33
This project uses an MIT style license - see LICENSE for details.
44
55
A simple Chip 8 emulator - see the README file for more information.
@@ -21,22 +21,22 @@
2121

2222
# Sets which keys on the keyboard map to the Chip 8 keys
2323
KEY_MAPPINGS = {
24-
0x0: pygame.K_KP0,
25-
0x1: pygame.K_KP1,
26-
0x2: pygame.K_KP2,
27-
0x3: pygame.K_KP3,
28-
0x4: pygame.K_KP4,
29-
0x5: pygame.K_KP5,
30-
0x6: pygame.K_KP6,
31-
0x7: pygame.K_KP7,
32-
0x8: pygame.K_KP8,
33-
0x9: pygame.K_KP9,
34-
0xA: pygame.K_a,
35-
0xB: pygame.K_b,
36-
0xC: pygame.K_c,
37-
0xD: pygame.K_d,
38-
0xE: pygame.K_e,
39-
0xF: pygame.K_f,
24+
0x0: pygame.K_g,
25+
0x1: pygame.K_4,
26+
0x2: pygame.K_5,
27+
0x3: pygame.K_6,
28+
0x4: pygame.K_7,
29+
0x5: pygame.K_r,
30+
0x6: pygame.K_t,
31+
0x7: pygame.K_y,
32+
0x8: pygame.K_u,
33+
0x9: pygame.K_f,
34+
0xA: pygame.K_h,
35+
0xB: pygame.K_j,
36+
0xC: pygame.K_v,
37+
0xD: pygame.K_b,
38+
0xE: pygame.K_n,
39+
0xF: pygame.K_m,
4040
}
4141

4242
# The font file to use

chip8/cpu.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (C) 2012 Craig Thomas
2+
Copyright (C) 2012-2019 Craig Thomas
33
This project uses an MIT style license - see LICENSE for details.
44
55
A Chip 8 CPU - see the README file for more information.
@@ -495,18 +495,20 @@ def subtract_reg_from_reg(self):
495495

496496
def right_shift_reg(self):
497497
"""
498-
8s06 - SHR Vs
498+
8st6 - SHR Vs, Vt
499499
500-
Shift the bits in the specified register 1 bit to the right. Bit
501-
0 will be shifted into register vf. The register calculation is
502-
as follows:
500+
Shift the bits in the source register 1 bit to the right and
501+
stores the result in the target register, leaving source
502+
with its original value. Bit 0 will be shifted into register
503+
vf. The register calculation is as follows:
503504
504505
Bits: 15-12 11-8 7-4 3-0
505-
unused source 0 6
506+
unused source target 6
506507
"""
507508
source = (self.operand & 0x0F00) >> 8
509+
target = (self.operand & 0x00F0) >> 4
508510
bit_zero = self.registers['v'][source] & 0x1
509-
self.registers['v'][source] = self.registers['v'][source] >> 1
511+
self.registers['v'][target] = self.registers['v'][source] >> 1
510512
self.registers['v'][0xF] = bit_zero
511513

512514
def subtract_reg_from_reg1(self):
@@ -536,18 +538,20 @@ def subtract_reg_from_reg1(self):
536538

537539
def left_shift_reg(self):
538540
"""
539-
8s0E - SHL Vs
541+
8stE - SHL Vs, Vt
540542
541-
Shift the bits in the specified register 1 bit to the left. Bit
542-
7 will be shifted into register vf. The register calculation is
543-
as follows:
543+
Shift the bits in the source register 1 bit to the left and
544+
stores the result in the target register, leaving the source
545+
register with its original value. Bit 7 will be shifted into
546+
register vf. The register calculation is as follows:
544547
545548
Bits: 15-12 11-8 7-4 3-0
546-
unused source 0 E
549+
unused source target E
547550
"""
548551
source = (self.operand & 0x0F00) >> 8
552+
target = (self.operand & 0x00F0) >> 4
549553
bit_seven = (self.registers['v'][source] & 0x80) >> 8
550-
self.registers['v'][source] = self.registers['v'][source] << 1
554+
self.registers['v'][target] = self.registers['v'][source] << 1
551555
self.registers['v'][0xF] = bit_seven
552556

553557
def skip_if_reg_not_equal_reg(self):

chip8/yac8e.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (C) 2012 Craig Thomas
2+
Copyright (C) 2012-2019 Craig Thomas
33
This project uses an MIT style license - see LICENSE for details.
44
55
A simple Chip 8 emulator - see the README file for more information.
@@ -68,7 +68,7 @@ def main_loop(args):
6868
cpu.running = False
6969
if event.type == pygame.KEYDOWN:
7070
keys_pressed = pygame.key.get_pressed()
71-
if keys_pressed[pygame.K_q]:
71+
if keys_pressed[pygame.K_ESCAPE]:
7272
cpu.running = False
7373

7474

test/test_chip8cpu.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from mock import patch, call
1414

1515
from chip8.cpu import Chip8CPU, UnknownOpCodeException, MODE_EXTENDED
16-
from chip8.screen import Chip8Screen, DEFAULT_HEIGHT, DEFAULT_WIDTH
16+
from chip8.screen import Chip8Screen
1717

1818
# C O N S T A N T S ###########################################################
1919

@@ -281,6 +281,7 @@ def test_right_shift_reg(self):
281281
for value in range(0, 0xFF, 0x10):
282282
self.cpu.registers['v'][register] = value
283283
self.cpu.operand = register << 8
284+
self.cpu.operand = self.cpu.operand + (register << 4)
284285
for index in range(1, 8):
285286
shifted_val = value >> index
286287
self.cpu.registers['v'][0xF] = 0
@@ -319,6 +320,7 @@ def test_left_shift_reg(self):
319320
for value in range(256):
320321
self.cpu.registers['v'][register] = value
321322
self.cpu.operand = register << 8
323+
self.cpu.operand = self.cpu.operand + (register << 4)
322324
for index in range(1, 8):
323325
shifted_val = value << index
324326
bit_seven = (shifted_val & 0x100) >> 9
@@ -530,7 +532,7 @@ def test_operation_9E_pc_skips_if_key_pressed(self):
530532
self.cpu.registers['v'][0] = 1
531533
self.cpu.registers['pc'] = 0
532534
result_table = [False] * 512
533-
result_table[pygame.K_KP0 + 1] = True
535+
result_table[pygame.K_4] = True
534536
with mock.patch("pygame.key.get_pressed", return_value=result_table) as key_mock:
535537
self.cpu.keyboard_routines()
536538
self.assertTrue(key_mock.asssert_called)
@@ -561,7 +563,7 @@ def test_operation_A1_pc_does_not_skip_if_key_pressed(self):
561563
self.cpu.registers['v'][0] = 1
562564
self.cpu.registers['pc'] = 0
563565
result_table = [False] * 512
564-
result_table[pygame.K_KP0 + 1] = True
566+
result_table[pygame.K_4] = True
565567
with mock.patch("pygame.key.get_pressed", return_value=result_table) as key_mock:
566568
self.cpu.keyboard_routines()
567569
self.assertTrue(key_mock.asssert_called)

0 commit comments

Comments
 (0)