-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
252 lines (211 loc) · 5.22 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"fmt"
"github.com/pkg/profile"
//"github.com/pkg/profile"
"github.com/szymonkups/nesgo/core"
"github.com/szymonkups/nesgo/ui"
"github.com/veandco/go-sdl2/sdl"
"os"
)
const (
screenFPS uint32 = 60
screenTicksPerFrame = 1000 / screenFPS
)
//func main() {
// p := profile.Start()
// // os.Exit(..) must run AFTER sdl.Main(..) below; so keep track of exit
// // status manually outside the closure passed into sdl.Main(..) below
// var exitcode int
// sdl.Main(func() {
// exitcode = run()
// })
//
// p.Stop()
// // os.Exit(..) must run here! If run in sdl.Main(..) above, it will cause
// // premature quitting of sdl.Main(..) function; resource cleaning deferred
// // calls/closing of channels may never run
// os.Exit(exitcode)
//}
func main() {
// CPU profiling by default
defer profile.Start().Stop()
// Create two main buses:
// 1. CPU bus where RAM, ppu and cartridge are connected and used by CPU,
// 2. ppu bus where cartridge is connected and used by ppu.
cpuBus := core.NewCPUBus()
ppuBus := core.NewPPUBus()
// Crate main system components: RAM, VRAM, ppu and cartridge.
ram := new(core.Ram)
ppu := core.NewPPU(ppuBus)
crt := new(core.Cartridge)
// TODO: think about better separation of vRam and crt
vRam := core.NewVRam(crt)
controller := new(core.Controller)
// Connect devices to CPU bus.
cpuBus.ConnectDevice(crt) // This must be first to allow grab any address and map it as it want s.
cpuBus.ConnectDevice(ram)
cpuBus.ConnectDevice(ppu)
cpuBus.ConnectDevice(controller)
// Connect devices to PPU bus.
ppuBus.ConnectDevice(crt) // This must be first to allow grab any address and map it as it wants.
ppuBus.ConnectDevice(vRam)
err := crt.LoadFile("/home/szymon/Downloads/nes/dk.nes")
if err != nil {
fmt.Printf("Could not load a file: %s.\n", err)
os.Exit(1)
}
cpu := core.NewCPU(cpuBus)
var gui *ui.UI
gui = new(ui.UI)
err = gui.Init(cpu, ppu, crt)
if err != nil {
panic(err)
}
cycles := 0
running := true
stepMode := false
fpsTimer := new(SDLTimer)
capTimer := new(SDLTimer)
countedFrames := uint32(0)
fpsTimer.Start()
//paletteId := uint8(0)
tick := func() {
ppu.Clock()
if cycles%3 == 0 {
cpu.Clock()
}
cycles++
if ppu.NMI {
ppu.NMI = false
cpu.ScheduleNMI()
}
}
screen := make([]uint8, 256*240*4)
ppu.SetDrawMethod(func(x, y int16, pixel *core.PPUColor) {
if x >= 256 || y >= 240 {
return
}
offset := (256 * 4 * int32(y)) + int32(x)*4
screen[offset+0] = pixel.B
screen[offset+1] = pixel.G
screen[offset+2] = pixel.R
screen[offset+3] = 0xFF
})
for running {
// Timer for FPS cap
capTimer.Start()
//Calculate and correct FPS
avgFPS := float32(countedFrames) / (float32(fpsTimer.GetTicks()) / 1000)
if avgFPS > 2000000 {
avgFPS = 0
}
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
running = false
case *sdl.KeyboardEvent:
if t.GetType() == sdl.KEYDOWN {
switch t.Keysym.Sym {
case sdl.K_ESCAPE:
running = false
//
//case sdl.K_RETURN:
// messages <- "step"
//
case sdl.K_RETURN:
controller.PressButton(core.ButtonStart)
case sdl.K_SPACE:
controller.PressButton(core.ButtonSelect)
case sdl.K_UP:
controller.PressButton(core.ButtonUp)
case sdl.K_DOWN:
controller.PressButton(core.ButtonDown)
//case sdl.K_p:
// paletteId = (paletteId + 1) % 8
//
//case sdl.K_r:
// messages <- "reset"
}
}
if t.GetType() == sdl.KEYUP {
switch t.Keysym.Sym {
case sdl.K_SPACE:
controller.ReleaseButton(core.ButtonSelect)
case sdl.K_RETURN:
controller.ReleaseButton(core.ButtonStart)
case sdl.K_UP:
controller.ReleaseButton(core.ButtonUp)
case sdl.K_DOWN:
controller.ReleaseButton(core.ButtonDown)
}
}
}
}
if !stepMode {
for !ppu.IsFrameComplete {
tick()
}
ppu.IsFrameComplete = false
}
gui.DrawScreen(screen)
fmt.Println("FPS: ", avgFPS)
countedFrames++
//If frame finished early
frameTicks := capTimer.GetTicks()
if frameTicks < screenTicksPerFrame {
//Wait remaining time
//sdl.Delay(screenTicksPerFrame - frameTicks)
}
}
}
// Kudos to https://lazyfoo.net/tutorials/SDL/23_advanced_timers/index.php
type SDLTimer struct {
startTicks uint32
pausedTicks uint32
paused bool
started bool
}
func (t *SDLTimer) Start() {
t.started = true
t.paused = false
t.startTicks = sdl.GetTicks()
t.pausedTicks = 0
}
func (t *SDLTimer) Stop() {
t.started = false
t.paused = false
t.startTicks = 0
t.pausedTicks = 0
}
func (t *SDLTimer) Pause() {
if t.started && !t.paused {
t.paused = true
t.pausedTicks = sdl.GetTicks() - t.startTicks
t.startTicks = 0
}
}
func (t *SDLTimer) Unpause() {
if t.started && t.paused {
t.paused = false
t.startTicks = sdl.GetTicks() - t.pausedTicks
t.pausedTicks = 0
}
}
func (t *SDLTimer) GetTicks() uint32 {
time := uint32(0)
if t.started {
if t.paused {
time = t.pausedTicks
} else {
time = sdl.GetTicks() - t.startTicks
}
}
return time
}
func (t *SDLTimer) IsStarted() bool {
return t.started
}
func (t *SDLTimer) IsPaused() bool {
return t.paused && t.started
}