Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 03a8df6

Browse filesBrowse files
general updates and clean ups after a few years
1 parent fabd915 commit 03a8df6
Copy full SHA for 03a8df6

File tree

Expand file treeCollapse file tree

5 files changed

+33
-33
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+33
-33
lines changed
Open diff view settings
Collapse file

‎cmd/root.go‎

Copy file name to clipboardExpand all lines: cmd/root.go
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package cmd
22

33
import (
44
"fmt"
5-
"os"
5+
"log"
66

77
"github.com/spf13/cobra"
88
)
99

1010
// currentReleaseVersion is used to print the version the user currently has downloaded
11-
const currentReleaseVersion = "v0.1.1"
11+
const currentReleaseVersion = "v0.2.0"
1212

1313
// rootCmd is the base for all commands.
1414
var rootCmd = &cobra.Command{
@@ -37,7 +37,6 @@ func init() {
3737
// Execute runs chippy according to the user's command/subcommand(s)/flag(s)
3838
func Execute() {
3939
if err := rootCmd.Execute(); err != nil {
40-
fmt.Println(err)
41-
os.Exit(1)
40+
log.Fatal(err)
4241
}
4342
}
Collapse file

‎cmd/run.go‎

Copy file name to clipboardExpand all lines: cmd/run.go
+3-5Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cmd
22

33
import (
4-
"fmt"
4+
"log"
55
"os"
66

77
"github.com/bradford-hamilton/chippy/internal/chip8"
@@ -18,15 +18,13 @@ var runCmd = &cobra.Command{
1818

1919
func runChippy(cmd *cobra.Command, args []string) {
2020
if len(args) != 1 {
21-
fmt.Println("The run command takes one argument: a `path/to/rom`")
22-
os.Exit(1)
21+
log.Fatal("The run command takes one argument: a `path/to/rom`")
2322
}
2423
pathToROM := os.Args[2]
2524

2625
vm, err := chip8.NewVM(pathToROM, refreshRate)
2726
if err != nil {
28-
fmt.Printf("\nerror creating a new chip-8 VM: %v\n", err)
29-
os.Exit(1)
27+
log.Fatalf("\nerror creating a new chip-8 VM: %v\n", err)
3028
}
3129

3230
go vm.ManageAudio()
Collapse file

‎cmd/version.go‎

Copy file name to clipboardExpand all lines: cmd/version.go
-5Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/spf13/cobra"
87
)
@@ -17,9 +16,5 @@ var versionCmd = &cobra.Command{
1716
}
1817

1918
func runVersion(cmd *cobra.Command, args []string) {
20-
if len(args) != 0 {
21-
fmt.Println("The version command does not take any arguments")
22-
os.Exit(1)
23-
}
2419
fmt.Println(currentReleaseVersion)
2520
}
Collapse file

‎go.mod‎

Copy file name to clipboardExpand all lines: go.mod
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/bradford-hamilton/chippy
22

3-
go 1.20
3+
go 1.22.1
44

55
require (
66
github.com/faiface/beep v1.1.0
Collapse file

‎internal/chip8/chip8.go‎

Copy file name to clipboardExpand all lines: internal/chip8/chip8.go
+26-18Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
package chip8
66

77
import (
8+
"errors"
89
"fmt"
9-
"io/ioutil"
10+
"log"
1011
"os"
1112
"time"
1213

@@ -15,6 +16,7 @@ import (
1516
"github.com/faiface/beep/speaker"
1617
)
1718

19+
//
1820
// System memory map
1921
// +---------------+= 0xFFF (4095) End Chip-8 RAM
2022
// | |
@@ -105,8 +107,7 @@ const (
105107
func NewVM(pathToROM string, clockSpeed int) (*VM, error) {
106108
window, err := pixel.NewWindow()
107109
if err != nil {
108-
fmt.Println(err)
109-
os.Exit(1)
110+
log.Fatal(err)
110111
}
111112

112113
vm := VM{
@@ -122,9 +123,7 @@ func NewVM(pathToROM string, clockSpeed int) (*VM, error) {
122123
ShutdownC: make(chan struct{}),
123124
}
124125

125-
vm.loadFontSet()
126-
127-
if err := vm.loadROM(pathToROM); err != nil {
126+
if err := vm.initialize(pathToROM); err != nil {
128127
return nil, err
129128
}
130129

@@ -134,6 +133,7 @@ func NewVM(pathToROM string, clockSpeed int) (*VM, error) {
134133
// Run starts the vm and emulates a clock that runs by default at 60MHz
135134
// This can be changed with a flag.
136135
func (vm *VM) Run() {
136+
outer:
137137
for {
138138
select {
139139
case <-vm.Clock.C:
@@ -145,32 +145,39 @@ func (vm *VM) Run() {
145145
vm.soundTimerTick()
146146
continue
147147
}
148-
break
148+
break outer
149149
case <-vm.ShutdownC:
150-
break
150+
break outer
151151
}
152-
break
153152
}
154153
vm.signalShutdown("Received signal - gracefully shutting down...")
155154
}
156155

156+
func (vm *VM) initialize(pathToROM string) error {
157+
vm.loadFontSet()
158+
if err := vm.loadROM(pathToROM); err != nil {
159+
return err
160+
}
161+
return nil
162+
}
163+
157164
// loads the font set into the first 80 bytes of memory
158165
func (vm *VM) loadFontSet() {
159-
for i := 0; i < 80; i++ {
166+
for i := range 80 {
160167
vm.memory[i] = pixel.FontSet[i]
161168
}
162169
}
163170

164171
func (vm *VM) loadROM(path string) error {
165-
rom, err := ioutil.ReadFile(path)
172+
rom, err := os.ReadFile(path)
166173
if err != nil {
167174
return err
168175
}
169176
if len(rom) > maxRomSize {
170-
panic("error: rom too large. Max size: 3583")
177+
return errors.New("error: rom too large. Max size: 3583")
171178
}
172179

173-
for i := 0; i < len(rom); i++ {
180+
for i := range len(rom) {
174181
vm.memory[0x200+i] = rom[i] // Write memory with pc offset
175182
}
176183

@@ -291,9 +298,7 @@ func (vm *VM) parseOpcode() error {
291298
return nil
292299
}
293300

294-
func (vm VM) getGraphics() [64 * 32]byte {
295-
return vm.gfx
296-
}
301+
func (vm VM) getGraphics() [64 * 32]byte { return vm.gfx }
297302

298303
func (vm *VM) setKeyDown(index byte) {
299304
vm.keypad[index] = 1
@@ -359,17 +364,20 @@ func (vm *VM) ManageAudio() {
359364
if err != nil {
360365
return
361366
}
367+
defer f.Close()
362368

363369
streamer, format, err := mp3.Decode(f)
364370
if err != nil {
365371
return
366372
}
367373
defer streamer.Close()
368374

369-
speaker.Init(
375+
if err := speaker.Init(
370376
format.SampleRate,
371377
format.SampleRate.N(time.Second/10),
372-
)
378+
); err != nil {
379+
panic("failed to initialize speakers")
380+
}
373381

374382
for range vm.audioC {
375383
speaker.Play(streamer)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.