Skip to content

Commit

Permalink
Add argument handling
Browse files Browse the repository at this point in the history
  • Loading branch information
soumitradev committed Apr 7, 2021
1 parent 22dffce commit c699122
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ My first attempt at emulation. This Go project aims to emulate a fully working C

Everything works as far as I have tested, except for audio.

## Usage
```
Usage: chip8 [--scaling SCALING] [--fps FPS] [--cpuspeed CPUSPEED] ROMPATH
Positional arguments:
ROMPATH Path to CHIP8 ROM file.
Options:
--scaling SCALING, -s SCALING
Pixel scaling. Adjusts size of display. [default: 15]
--fps FPS, -f FPS FPS to run display at. [default: 60]
--cpuspeed CPUSPEED, -c CPUSPEED
Speed of CPU relative to FPS. [default: 10]
--help, -h display this help and exit
--version display version and exit
```

## Controls
Keys

Expand Down Expand Up @@ -42,7 +59,6 @@ respectively


## TODO (in no particular order):
- Create a way to customize controls and settings (maybe using config files, maybe some GUI)
- Add more invasive features (e.g. Soft reset, Hard reset, memory manipulation, etc.)
- Implement VIP/Super chip8
- Implement audio (needs a better understanding of goroutines and channels)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/soumitradev/chip8
go 1.16

require (
github.com/alexflint/go-arg v1.3.0 // indirect
github.com/faiface/pixel v0.10.0
github.com/go-gl/gl v0.0.0-20210315015930-ae072cafe09d // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210311203641-62640a716d48 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
github.com/alexflint/go-arg v1.3.0 h1:UfldqSdFWeLtoOuVRosqofU4nmhI1pYEbT4ZFS34Bdo=
github.com/alexflint/go-arg v1.3.0/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM=
github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70=
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380 h1:FvZ0mIGh6b3kOITxUnxS3tLZMh7yEoHo75v3/AgUqg0=
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380/go.mod h1:zqnPFFIuYFFxl7uH2gYByJwIVKG7fRqlqQCbzAnHs9g=
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 h1:baVdMKlASEHrj19iqjARrPbaRisD7EuZEVJj6ZMLl1Q=
Expand All @@ -22,6 +27,7 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
Expand Down
35 changes: 29 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ package main

import (
"time"

"github.com/alexflint/go-arg"
)

// Adjust these according to your game/preferences
const SCALING = 15
const FPS = 60
const speed = 10
// Adjust these according to your game preferences
var SCALING = 15
var FPS = 60
var speed = 10

type args struct {
ROMPATH string `arg:"positional" help:"Path to CHIP8 ROM file."`
SCALING int `default:"15" arg:"-s" help:"Pixel scaling. Adjusts size of display."`
FPS int `default:"60" arg:"-f" help:"FPS to run display at."`
CPUspeed int `default:"10" arg:"-c" help:"Speed of CPU relative to FPS."`
}

// Return version info
func (args) Version() string {
return "chip8 v0.1.0"
}

// Timing vars
var startTime time.Time
Expand All @@ -17,14 +31,23 @@ var elapsed time.Duration
var fpsInterval time.Duration

func main() {
// Use prefs in args
var args args

arg.MustParse(&args)
var ROMPATH = args.ROMPATH
SCALING = args.SCALING
FPS = args.FPS
speed = args.CPUspeed

// Decide how often we render a frame
fpsInterval = time.Duration(time.Millisecond * 1000.0 / FPS)
fpsInterval = time.Duration(time.Millisecond * 1000.0 / time.Duration(FPS))
then = time.Now()
startTime = then

// Load the chip8 font and ROM into memory
loadSprites()
loadROM("ROMs/SPACEINVADERS.ch8")
loadROM(ROMPATH)

// Start display and take one step forward (render one frame)
startDisplay()
Expand Down
6 changes: 3 additions & 3 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func run() {
// Configure window
cfg := pixelgl.WindowConfig{
Title: "Chip8",
Bounds: pixel.R(0, 0, 64*SCALING, 32*SCALING),
Bounds: pixel.R(0, 0, float64(64*SCALING), float64(32*SCALING)),
VSync: true,
}
window, err := pixelgl.NewWindow(cfg)
Expand Down Expand Up @@ -134,10 +134,10 @@ func screenRender() {
imd.Clear()
for i := 0; i < 64*32; i++ {
x := float64((i % 64) * SCALING)
y := 31*SCALING - float64((i/64)*SCALING)
y := float64(31*SCALING - (i/64)*SCALING)
if display[i] {
imd.Push(pixel.V(x, y))
imd.Push(pixel.V(x+SCALING, y+SCALING))
imd.Push(pixel.V(x+float64(SCALING), y+float64(SCALING)))
imd.Rectangle(0)
}
}
Expand Down

0 comments on commit c699122

Please sign in to comment.