-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchip8.go
More file actions
119 lines (99 loc) · 2.46 KB
/
chip8.go
File metadata and controls
119 lines (99 loc) · 2.46 KB
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
package main
import (
"bufio"
"os"
"sync"
"time"
"github.com/veandco/go-sdl2/sdl"
)
const (
displayRows = 32
displayColumns = 64
memSize = 4096
stackDepth = 16
programOffset = 0x200
hz = time.Duration(600) // m/s b/w emulation steps
cellSize = 15
)
type address uint16
type chip8 struct {
sync.Mutex
reg [16]byte // registers, reg[15] is carry
stack [stackDepth]address
pc address // program counter
sp uint8 // stack pointer
dt byte // delay timer
st byte // sound timer
I uint16 // special reg, stores addresses
mem [memSize]byte // rom and program work ram
disp []byte // graphics mem
keys [16]bool // stores keypress state
graphics Graphics
input chan sdl.KeyboardEvent
sound chan bool
tick chan bool
}
// NewCHIP8 performs needed setup and binds for chip emulation
func NewCHIP8(g Graphics) *chip8 {
chip := &chip8{
graphics: g,
}
chip.initRegisters()
// TODO(Monkeyanator) replace some of these with standard calls
chip.input = make(chan sdl.KeyboardEvent, 32)
chip.sound = make(chan bool, 32)
chip.tick = make(chan bool, 32)
// bind graphics buffer
chip.disp = make([]byte, displayRows*displayColumns)
chip.graphics.BindBuffer(chip.disp)
// initialize remaining systems
chip.InitCharset()
return chip
}
func (chip *chip8) initRegisters() {
// note that reg defaults elems to 0x00
chip.dt = 0x0
chip.st = 0x0
chip.pc = programOffset // program execution starts here by convention
}
func (chip *chip8) LoadProgram(path string) {
progBuff, err := createProgramBuffer(path)
if err != nil {
panic(progBuff)
}
// load the mem in at offset
for i := 0; i < len(progBuff); i++ {
chip.mem[programOffset+i] = progBuff[i]
}
}
/* register manipulation */
func (chip *chip8) SetRegister(index uint8, value byte) {
chip.reg[index] = value
}
func (chip *chip8) ReadRegister(index uint8) byte {
return chip.reg[index]
}
/* pc manipulation */
func (chip *chip8) IncrementPC() {
chip.pc += 2
}
func (chip *chip8) SetPC(val address) {
chip.pc = address(val)
}
/* helpers */
func createProgramBuffer(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
stats, statsErr := f.Stat()
if statsErr != nil {
return nil, statsErr
}
size := stats.Size()
bytes := make([]byte, size)
buff := bufio.NewReader(f)
_, err = buff.Read(bytes)
return bytes, err
}