forked from HFO4/gameboy.live
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
128 lines (106 loc) · 2.41 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"flag"
"io/ioutil"
"path/filepath"
fyneAPI "fyne.io/fyne/v2"
"fyne.io/fyne/v2/storage"
"github.com/andydotxyz/fynegameboy/fyne"
"github.com/andydotxyz/fynegameboy/gb"
)
var (
h bool
romPath string
SoundOn bool
FPS int
Debug bool
)
func init() {
flag.BoolVar(&h, "h", false, "This help")
flag.BoolVar(&SoundOn, "m", true, "Turn on sound in GUI mode")
flag.BoolVar(&Debug, "d", false, "Use Debugger in GUI mode")
flag.IntVar(&FPS, "f", 60, "Set the `FPS` in GUI mode")
}
func loadRom(romPath string) ([]byte, fyneAPI.URI) {
data, err := ioutil.ReadFile(romPath)
if err != nil {
return nil, nil
}
return data, storage.NewFileURI(romPath)
}
func newCore(d *fyne.LCD) *gb.Core {
core := new(gb.Core)
core.FPS = FPS
core.Clock = 4194304
core.Debug = Debug
core.DisplayDriver = d
core.Controller = d
core.DrawSignal = make(chan bool)
core.SpeedMultiple = 0
core.ToggleSound = SoundOn
return core
}
func startGUI() {
d := fyne.NewDriver()
uri, _ := storage.ParseURI(fyneAPI.CurrentApp().Preferences().String("RomURI"))
var data []byte
if romPath == "" && uri != nil && uri.String() != "" {
read, err := storage.Reader(uri)
if err == nil {
data, _ = ioutil.ReadAll(read)
}
}
var core *gb.Core
start := func() {
core = newCore(d)
d.DrawSignal = core.DrawSignal
if data != nil && len(data) > 0 {
core.Init(data, uri)
} else {
core.Init(loadRom(romPath))
}
go core.Run()
}
start()
const volumeControlMemoryLocation = 0xFF26
d.Reset = func() {
core.Sound.Trigger(volumeControlMemoryLocation, 0, core.Memory.MainMemory[0xFF10:0xFF40])
core.Exit = true
start()
}
d.Open = func(r fyneAPI.URIReadCloser) {
core.Exit = true
bytes, err := ioutil.ReadAll(r)
if err != nil {
fyneAPI.LogError("Unable to load ROM", err)
return
}
_ = r.Close()
data = bytes
uri = r.URI()
d.Reset()
}
d.Pause = func() {
core.Pause()
core.Sound.Trigger(volumeControlMemoryLocation, 0, core.Memory.MainMemory[0xFF10:0xFF40])
}
d.Resume = func() {
data := core.Memory.MainMemory[volumeControlMemoryLocation]
core.Sound.Trigger(volumeControlMemoryLocation, data, core.Memory.MainMemory[0xFF10:0xFF40])
core.Resume()
}
d.Run(core.DrawSignal, func() {
core.SaveRAM()
})
}
func main() {
flag.Parse()
if h {
flag.Usage()
return
}
if len(flag.Args()) == 1 { // probably a ROM parameter
romPath, _ = filepath.Abs(flag.Arg(0))
}
startGUI()
}