-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
53 lines (47 loc) · 934 Bytes
/
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
package nes
import (
"image"
"io"
)
type Console struct {
ppu *ppu
cpu *cpu
joypad1 *joypad
}
func NewConsole(r io.Reader) (*Console, error) {
c := &Console{}
err := c.loadROM(r)
return c, err
}
func (c *Console) loadROM(r io.Reader) error {
cart, err := readFile(r)
if err != nil {
return err
}
c.ppu = newPPU(cart)
c.joypad1 = &joypad{}
c.cpu = newCPU(cart, c.ppu, c.joypad1)
return nil
}
func (c *Console) RenderFrame(image *image.RGBA) {
for {
cycles := c.cpu.Step()
cycles *= 3
beforeNMI := c.ppu.nmiTriggered()
for ; cycles > 0; cycles-- {
c.ppu.step(image)
}
afterNMI := c.ppu.nmiTriggered()
if !beforeNMI && afterNMI {
c.cpu.triggerNMI()
break
}
}
}
func (c *Console) SetJoypad(button byte, pressed bool) {
if pressed {
c.joypad1.buttonState = setBits(c.joypad1.buttonState, button)
} else {
c.joypad1.buttonState = resetBits(c.joypad1.buttonState, button)
}
}