-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmain.go
97 lines (76 loc) · 1.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"flag"
"fmt"
"github.com/scottferg/Fergulator/nes"
"io/ioutil"
"log"
"os"
"runtime"
"runtime/pprof"
"strings"
)
var (
running = true
videoOut Video
audioOut *Audio
cpuprofile = flag.String("cprof", "", "write cpu profile to file")
debugfile string
jsHandler *nes.JsEventHandler
)
func init() {
flag.StringVar(&debugfile, "debug", "", "JS debugging file")
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Please specify a ROM file")
return
}
flag.Parse()
// TODO: Why don't flags work? Don't want to hardcode this.
debugfile = "debug.js"
contents, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Println(err.Error())
return
}
path := strings.Split(os.Args[1], "/")
nes.GameName = strings.Split(path[len(path)-1], ".")[0]
nes.SaveStateFile = fmt.Sprintf(".%s.state", nes.GameName)
nes.BatteryRamFile = fmt.Sprintf(".%s.battery", nes.GameName)
if debugfile != "" {
jsHandler = nes.NewJsEventHandler(debugfile)
nes.Handler = jsHandler
} else {
nes.Handler = nes.NewNoopEventHandler()
}
log.Println(nes.GameName, nes.SaveStateFile)
audioOut = NewAudio()
defer audioOut.Close()
videoTick, err := nes.Init(contents, audioOut.AppendSample, GetKey)
if err != nil {
fmt.Println(err)
}
videoOut.Init(videoTick, nes.GameName)
// Only increase the number of processors we can use after initialization,
// due to an unidentified race condition documented in issue #13. This
// workaround is effective yet unsatisfying.
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
fmt.Println(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
} else {
runtime.GOMAXPROCS(runtime.NumCPU())
}
// Main runloop, in a separate goroutine so that
// the video rendering can happen on this one
go nes.RunSystem()
// This needs to happen on the main thread for OSX
runtime.LockOSThread()
defer videoOut.Close()
videoOut.Render()
return
}