-
Notifications
You must be signed in to change notification settings - Fork 0
/
chipeight.go
162 lines (136 loc) · 2.75 KB
/
chipeight.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
package chipeight
import (
"io/ioutil"
"log"
)
const (
fontStartLoc = 0x50
programStartLoc = 0x200
registerVF = 15
screenWidth = 64
screenHeight = 32
spriteWidth = 8
)
var (
fontSet = [80]uint8{
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80, // F
}
)
type Chipeight struct {
memory [4096]uint8
registers [16]uint8
indexRegister uint16
screen [screenWidth * screenHeight]uint8
keys [16]uint8
currentOpcode uint16
programCounter uint16
delayTimer uint8
soundTimer uint8
shouldDraw bool
stack Stack
}
func NewChipeight() *Chipeight {
c := &Chipeight{
programCounter: programStartLoc,
}
for i := 0; i < len(fontSet); i++ {
c.memory[fontStartLoc+i] = fontSet[i]
}
return c
}
func (c *Chipeight) LoadROM(filePath string) error {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
for i := 0; i < len(data); i++ {
c.memory[programStartLoc+i] = data[i]
}
return nil
}
func (c *Chipeight) LoadBytes(data []byte) error {
for i := 0; i < len(data); i++ {
c.memory[programStartLoc+i] = data[i]
}
return nil
}
// For testing without GUI
func (c *Chipeight) Run() {
for {
c.Step()
}
}
func (c *Chipeight) Step() {
c.currentOpcode = uint16(c.memory[c.programCounter])<<8 | uint16(c.memory[c.programCounter+1])
switch c.currentOpcode & 0xF000 {
case 0x0000:
op0000(c)
case 0x1000:
op1000(c)
case 0x2000:
op2000(c)
case 0x3000:
op3000(c)
case 0x4000:
op4000(c)
case 0x5000:
op5000(c)
case 0x6000:
op6000(c)
case 0x7000:
op7000(c)
case 0x8000:
op8000(c)
case 0x9000:
op9000(c)
case 0xA000:
opA000(c)
case 0xB000:
opB000(c)
case 0xC000:
opC000(c)
case 0xD000:
opD000(c)
case 0xE000:
opE000(c)
case 0xF000:
opF000(c)
default:
log.Printf("unknown opcode: 0x%X", c.currentOpcode)
}
// Increase PC except for special opcodes: CALL, JMP
if (c.currentOpcode&0xF000) != 0x1000 && (c.currentOpcode&0xF000) != 0x2000 {
c.programCounter += 2
}
if c.delayTimer > 0 {
c.delayTimer--
}
if c.soundTimer > 0 {
c.soundTimer--
}
}
func (c *Chipeight) ShouldDraw() bool {
sd := c.shouldDraw
if sd {
c.shouldDraw = false
}
return sd
}
func (c *Chipeight) GetScreen() [screenWidth * screenHeight]uint8 {
return c.screen
}