Skip to content

Commit

Permalink
Add pychip8 CLI util
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoklosowski committed Jan 27, 2023
1 parent c1bb344 commit e47abd7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

Chip-8 emulator written in Python

## Example of use

```sh
pychip8 path/to/rom.ch8

pychip8 --sdl path/to/rom.ch8
```

## Links about Chip-8

- https://en.wikipedia.org/wiki/CHIP-8
Expand Down
78 changes: 78 additions & 0 deletions pychip8/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from argparse import ArgumentParser
from importlib import import_module
from typing import Tuple


def parse_size(value: str) -> Tuple[int, int]:
width, height = value.split('x')
return int(width), int(height)


parser = ArgumentParser(
prog='pychip8',
description='Chip-8 emulator written in Python',
)
parser.add_argument(
'file',
type=lambda x: open(x, 'rb'), # noqa: SIM115
help='path of the ROM to be executed',
)
parser.add_argument(
'-c',
'--clock',
type=int,
default=960,
help='clock of CPU in Hertz (default: 960)',
)
parser.add_argument(
'-i',
'--instructions_per_update',
type=int,
default=16,
help='number of instructions executed in an update (default: 16)',
)
parser.add_argument(
'--interface',
metavar='INTERFACE',
choices=['ncurses', 'sdl'],
default='ncurses',
help='select interface (default: ncurses)',
)
parser.add_argument(
'--ncurses',
dest='interface',
action='store_const',
const='ncurses',
help='use ncurses interface',
)
parser.add_argument(
'--sdl',
dest='interface',
action='store_const',
const='sdl',
help='use SDL interface',
)
parser.add_argument(
'-s',
'--size',
type=parse_size,
default=(800, 400),
help='size for GUI window (default: 800x400)',
)


def main() -> None:
try:
args = parser.parse_args()
import_module(f'.{args.interface}', 'pychip8.interface').main(
program=args.file,
instructions_per_update=args.instructions_per_update,
clock=args.clock,
size=args.size,
)
except KeyboardInterrupt:
...


if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pytest = "^7.2"
pytest-cov = "^4.0"
pytest-xdist = "^3.1"

[tool.poetry.scripts]
pychip8 = 'pychip8.cli:main'

[tool.isort]
float_to_top = true
multi_line_output = "VERTICAL_GRID"
Expand Down

0 comments on commit e47abd7

Please sign in to comment.