/
main.go
147 lines (125 loc) · 3.1 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
package main
import (
"flag"
"github.com/mr-tim/goboye/internal/pkg/display"
"github.com/mr-tim/goboye/internal/pkg/goboye"
"github.com/mr-tim/goboye/internal/pkg/goboye/button"
"github.com/mr-tim/goboye/internal/pkg/goboye/ui"
"github.com/pkg/profile"
"github.com/veandco/go-sdl2/sdl"
"log"
"time"
)
var (
rom = flag.String("rom", "", "ROM to run")
profileCpu = flag.Bool("profileCpu", false, "Profile CPU")
profileMem = flag.Bool("profileMem", false, "Profile memory")
)
func main() {
flag.Parse()
if *rom == "" {
panic("Please specify a ROM to run")
}
if *profileCpu {
defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()
} else if *profileMem {
defer profile.Start(profile.MemProfile, profile.ProfilePath(".")).Stop()
}
emulator := goboye.NewEmulator()
emulator.LoadRomImage(*rom)
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
panic(err)
}
defer sdl.Quit()
d, err := ui.NewSdlUi()
if err != nil {
panic(err)
}
defer d.Destroy()
running := true
showFrameRate := false
frameCount := 0
milliSecondsPerFrame := 1000 / 60
for running {
eventStart := time.Now()
start := int(sdl.GetTicks())
// handle events
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.KeyboardEvent:
keyEvent := event.(*sdl.KeyboardEvent)
btn := buttonMapping(keyEvent)
switch {
case btn == button.Quit && keyEvent.Type == sdl.KEYUP:
running = false
sdl.Quit()
case btn == button.Frames && keyEvent.Type == sdl.KEYUP:
showFrameRate = !showFrameRate
case btn.IsJoypad():
emulator.SetButtonState(btn, keyEvent.State == sdl.PRESSED)
}
case *sdl.QuitEvent:
println("Quit")
running = false
break
}
}
if !running {
continue
}
eventTime := time.Since(eventStart)
eventStart = time.Now()
// Run a single frame's worth of ops
emulator.StepFrame()
cpuTime := time.Since(eventStart)
eventStart = time.Now()
//redraw
i := emulator.DebugRender()
renderTime := time.Since(eventStart)
eventStart = time.Now()
d.UpdateScreen(i)
displayTime := time.Since(eventStart)
frameTime := int(sdl.GetTicks()) - start
sleepTime := milliSecondsPerFrame - frameTime
frameCount = (frameCount + 1) % display.FRAMES_PER_SECOND
if sleepTime < 0 {
continue
} else if sleepTime > 0 {
if showFrameRate && frameCount == 0 {
log.Printf("events: %4d\tcpu: %4d\trender: %4d\tdisplay: %4d",
eventTime.Microseconds(),
cpuTime.Microseconds(),
renderTime.Microseconds(),
displayTime.Microseconds())
}
sdl.Delay(uint32(sleepTime))
}
}
}
func buttonMapping(ke *sdl.KeyboardEvent) button.Button {
switch ke.Keysym.Sym {
// "meta" buttons
case sdl.K_ESCAPE:
return button.Quit
case sdl.K_f:
return button.Frames
// joypad
case sdl.K_a:
return button.Left
case sdl.K_d:
return button.Right
case sdl.K_s:
return button.Down
case sdl.K_w:
return button.Up
case sdl.K_p:
return button.A
case sdl.K_o:
return button.B
case sdl.K_n:
return button.Select
case sdl.K_m:
return button.Start
}
return button.Unbound
}