-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.go
47 lines (37 loc) · 931 Bytes
/
cpu.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
package vm
import (
"fmt"
"github.com/tvanriel/vm/operations"
)
const PC_START_LOCATION = 0x1_ff_f6 // 9-bytes under rom-top. Enough for JMP instr + 8 byte addressing.
const SP_START_LOCATION = 0xFF_FF // Starts at last address of memory.
type CPU struct {
// Flags is the sum of all flags that are currently set in the CPU.
Flags Flag
// Program-counter. Contains the address of the next instruction.
PC uint64
SP uint64
// Accumulator register
A uint64
// General-purpose registers
B uint64
X uint64
Y uint64
}
func (c *CPU) IsHalted() bool {
return c.Flags&FLAG_HALT == 1
}
func (c *CPU) Execute() {
// Fetch the instruction from memory.
fmt.Println("fetch")
instr := operations.Opcode(GetFromAddressSpace(Address(c.PC)))
fmt.Printf("instr: %x\n", instr)
// Decode the instruction
if action, ok := Instructions[instr]; ok {
// Execute
action(c)
c.PC++
return
}
c.Flags &= FLAG_HALT
}