-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
154 lines (134 loc) · 2.98 KB
/
console.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
package gophernes
import (
"io"
"github.com/tomnz/gophernes/internal/apu"
"github.com/tomnz/gophernes/internal/cartridge"
"github.com/tomnz/gophernes/internal/cpu"
"github.com/tomnz/gophernes/internal/ppu"
"image"
"time"
)
// Console implements the main console.
type Console struct {
config *config
ram []byte
cpu *cpu.CPU
ppu *ppu.PPU
apu *apu.APU
img *image.RGBA
cartridge cartridge.Cartridge
}
const (
internalRAMSize uint16 = 0x800
frameTime = 1.0 / 60
)
// NewConsole initializes a new console.
func NewConsole(rom io.Reader, cpuopts []cpu.Option, ppuopts []ppu.Option, apuopts []apu.Option, opts ...Option) (*Console, error) {
config := defaultConfig()
for _, opt := range opts {
opt(config)
}
cartridge, err := loadINES(rom)
if err != nil {
return nil, err
}
console := &Console{
config: config,
ram: make([]byte, internalRAMSize),
img: image.NewRGBA(image.Rect(0, 0, ppu.DisplayWidth, ppu.DisplayHeight)),
cartridge: cartridge,
}
cpu := cpu.NewCPU(&cpuMemory{console}, cpuopts...)
ppu := ppu.NewPPU(&ppuMemory{console}, ppuopts...)
apu := apu.NewAPU(cpu, apuopts...)
cpu.Reset()
ppu.Reset()
apu.Reset()
console.cpu = cpu
console.ppu = ppu
console.apu = apu
return console, nil
}
const (
cpuClockDivisor = 12
ppuClockDivisor = 4
apuClockDivisor = 12
)
func (c *Console) Run() {
startTime := time.Now()
var clock, frames uint64
for {
if clock%cpuClockDivisor == 0 {
c.cpu.Step()
}
if clock%ppuClockDivisor == 0 {
c.ppu.Step()
currFrames := c.ppu.Frames()
if frames != currFrames {
frames = currFrames
c.handleFrame(startTime, frames)
}
}
if clock%apuClockDivisor == 0 {
c.apu.Step()
}
clock++
}
}
func (c *Console) RunFrames(frames uint64) {
startTime := time.Now()
var clock, currFrames uint64
for currFrames <= frames {
if clock%cpuClockDivisor == 0 {
c.cpu.Step()
}
if clock%ppuClockDivisor == 0 {
c.ppu.Step()
nextFrames := c.ppu.Frames()
if currFrames != nextFrames {
currFrames = nextFrames
c.handleFrame(startTime, currFrames)
}
}
clock++
}
}
func (c *Console) RunCycles(cycles uint64) {
startTime := time.Now()
var clock, frames uint64
for clock < cycles {
if clock%cpuClockDivisor == 0 {
c.cpu.Step()
}
if clock%ppuClockDivisor == 0 {
c.ppu.Step()
currFrames := c.ppu.Frames()
if frames != currFrames {
frames = currFrames
c.handleFrame(startTime, frames)
}
}
clock++
}
}
func (c *Console) handleFrame(startTime time.Time, frames uint64) {
if c.config.draw != nil {
c.drawFrame()
c.config.draw(c.img)
}
if c.config.rate <= 0 {
return
}
expected := startTime.Add(time.Duration(
float64(time.Second) * frameTime * float64(frames) / c.config.rate))
sleepDuration := expected.Sub(time.Now())
time.Sleep(sleepDuration)
}
func (c *Console) drawFrame() {
buf := c.ppu.Buffer()
for y, row := range buf {
for x, val := range row {
c.img.Set(x, y, c.config.palette[val])
}
}
}