-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
236 lines (199 loc) · 5.98 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//go:generate $HOME/go/bin/go-bindata -pkg data -o data/data.go data/...
//go:generate $HOME/go/bin/go-bindata -fs -prefix debug -pkg debug -o debug/data.go debug/...
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"time"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/laullon/b2t80s/emulator"
"github.com/laullon/b2t80s/gui"
"github.com/laullon/b2t80s/machines/atetris"
"github.com/laullon/b2t80s/machines/cpc"
"github.com/laullon/b2t80s/machines/gameboy"
"github.com/laullon/b2t80s/machines/msx"
"github.com/laullon/b2t80s/machines/nes"
"github.com/laullon/b2t80s/machines/zx"
"github.com/veandco/go-sdl2/sdl"
"golang.design/x/mainthread"
_ "net/http/pprof"
)
func main() {
emulator.CartFile = flag.String("cart", "", "NESncart file to load")
emulator.TapFile = flag.String("tap", "", "tap file to load")
emulator.RomFile = flag.String("rom", "", "msx1 rom file to load - format: [mapper::]filename - Mappers:konami")
z80File := flag.String("z80", "", "z80 file to load")
mode := flag.String("mode", "48k", "Spectrum model to emulate [48k|128k|plus3|cpc464|cpc6128|msx1]")
emulator.Debug = flag.Bool("debug", false, "shows debugger")
// turbo := flag.Bool("turbo", false, "run faster")
emulator.Breaks = flag.String("bp", "", "Breakpoints [0xXXXX[,0xXXXX,...]]")
emulator.WatchPoints = flag.String("wp", "", "Memory Watch Points [0xXXXX[,0xXXXX,...]]")
emulator.LoadSlow = flag.Bool("slow", false, "Real Spectrum loading process")
emulator.DskAFile = flag.String("dskA", "", "disc file to load on drive A")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
var machine emulator.Machine
var name string
if len(*z80File) > 0 {
machine = zx.LoadZ80File(*z80File)
name = "ZX Spectrum"
} else {
switch *mode {
case "48k":
machine = zx.NewZX48K()
name = "ZX Spectrum 48k"
case "128k":
machine = zx.NewZX128K()
name = "ZX Spectrum 128k"
case "plus3":
machine = zx.NewZXPlus3()
name = "ZX Spectrum +3"
case "cpc464", "cpc":
machine = cpc.NewCPC(true)
name = "Amstrad CPC 464"
case "cpc6128":
machine = cpc.NewCPC(false)
name = "Amstrad CPC 6128"
case "msx":
machine = msx.NewMSX()
name = "MSX 1"
case "atetris":
machine = atetris.NewATetris()
name = "Tetris"
case "nes":
machine = nes.NewNES()
name = "Nes"
case "gb":
machine = gameboy.New()
name = "GameBoy"
default:
panic(fmt.Errorf("mode '%s' not valid", *mode))
}
}
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
panic(err)
}
if err := gl.Init(); err != nil {
panic(err)
}
game := emulator.NewGame(name, machine)
game.SetOnKey(machine.OnKey)
log.Printf("opengl version %s", gl.GoStr(gl.GetString(gl.VERSION)))
if *emulator.Debug {
db := emulator.NewDebugger(machine.Clock())
machine.SetDebugger(db)
emulator.NewDebugWindow(name, machine, db)
}
wait := time.Duration(time.Second)
ticker := time.NewTicker(wait)
go func() {
for range ticker.C {
str := fmt.Sprintf("time: %s - FPS: %03.2f", machine.Clock().Stats(), machine.Monitor().FPS())
println(str)
game.SetStatus(str)
}
}()
go func() {
machine.Clock().Run()
}()
mainthread.Init(func() {
stop := make(chan struct{}, 1)
poolWait := time.Duration(time.Second / 120)
poolTicker := time.NewTicker(poolWait)
for {
select {
case <-poolTicker.C:
mainthread.Call(func() {
gui.PoolEvents(stop)
})
case <-stop:
return
}
}
})
// ui.App = app.New()
// w := ui.App.NewWindow(name + " - b2t80s Emulator")
// status := widget.NewLabelWithStyle("", fyne.TextAlignLeading, fyne.TextStyle{Monospace: true})
// statusControl := container.New(layout.NewHBoxLayout())
// for _, control := range machine.UIControls() {
// statusControl.Add(control.Widget())
// }
// display := machine.Monitor().Canvas()
// var debugTabs *container.AppTabs
// var controls map[string]gui.GUIObject
// if *emulator.Debug {
// controls = machine.Control()
// debugTabs = container.NewAppTabs()
// for n, ctl := range controls {
// debugTabs.Append(container.NewTabItem(n, ctl.Widget()))
// }
// debugTabs.SelectTabIndex(0)
// debugger := container.New(layout.NewBorderLayout(db.UI(), nil, nil, nil),
// db.UI(),
// debugTabs,
// )
// statusBar := fyne.NewContainerWithLayout(
// layout.NewBorderLayout(nil, nil, status, statusControl),
// status,
// statusControl,
// )
// w.SetContent(
// fyne.NewContainerWithLayout(
// layout.NewBorderLayout(nil, statusBar, nil, debugger),
// display,
// debugger,
// statusBar,
// ),
// )
// ui.App.Settings().SetTheme(theme.LightTheme())
// } else {
// ui.App.Settings().SetTheme(theme.DarkTheme())
// w.SetContent(
// fyne.NewContainerWithLayout(
// layout.NewBorderLayout(nil, nil, nil, nil),
// display,
// ),
// )
// }
// w.Canvas().(desktop.Canvas).SetOnKeyDown(machine.OnKeyEvent)
// w.Canvas().(desktop.Canvas).SetOnKeyUp(machine.OnKeyEvent)
// if *emulator.Debug {
// wait := time.Duration(20 * time.Millisecond)
// ticker := time.NewTicker(wait)
// go func() {
// for range ticker.C {
// controls[debugTabs.CurrentTab().Text].Update()
// status.SetText(fmt.Sprintf("time: %s - FPS: %03.2f", machine.Clock().Stats(), machine.Monitor().FPS()))
// }
// }()
// }
// // w.CenterOnScreen()
// w.ShowAndRun()
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close()
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
}