-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpu.go
More file actions
119 lines (96 loc) · 2.68 KB
/
Copy pathcpu.go
File metadata and controls
119 lines (96 loc) · 2.68 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 mos65xx implements MOS Technology 65xx CPU emulation.
package mos65xx
import (
"fmt"
"github.com/tehmaze/mos65xx/memory"
)
// CPU represents a MOS Technology 65xx Central Processing Unit
type CPU interface {
// Memory as observed by the CPU
memory.Memory
// Registers returns a pointer to the CPU registers
Registers() *Registers
// IRQ requests an interrupt
IRQ()
// NMI requests an non-maskable interrupt
NMI()
// Reset requests a cold reset
Reset()
// Ready
Ready(bool)
// Step fetches and executes the next instruction, returning the total
// number of cycles spent on performing the operation.
Step() int
// Run until the CPU receives a HLT instruction, returning the total
// number of cycles spent.
Run() int
// Halted returns true if the CPU received a HLT instruction
Halted() bool
// Attach a monitor
Attach(Monitor)
}
/*
// TODO: Not implemented
// Accurate is a CPU implementation optimized for cycle accuracy
type Accurate interface {
CPU
// Tick a single cycle
Tick()
}
*/
// Registers are the CPU registers
type Registers struct {
PC uint16 // Program counter
S uint8 // Stack pointer
P uint8 // Processor status register
A uint8 // Accumulator register
X uint8 // X index register
Y uint8 // Y index register
}
// setFlag sets a process status register flag
func setFlag(mask, flag uint8, set bool) uint8 {
if set {
return mask | flag
}
return mask & ^flag
}
// setZN sets the Z and N flags based on the value
func (reg *Registers) setZN(value uint8) {
reg.P = setFlag(reg.P, Z, value == 0x00)
reg.P = setFlag(reg.P, N, value&0x80 != 0x00)
}
// cmp compares two values and updates the Z, N and C flags accordingly
func (reg *Registers) cmp(a, b uint8) {
reg.P = setFlag(reg.P, C, a >= b)
reg.P = setFlag(reg.P, Z, a == b)
reg.P = setFlag(reg.P, N, (a-b)&0x80 == 0x80)
}
func (reg *Registers) String() string {
p := []rune("········")
for i, c := range []rune("NVUBDIZC") {
if reg.P&(1<<(7-uint(i))) != 0 {
p[i] = c
}
}
return fmt.Sprintf("PC:%04X A:%02X X:%02X Y:%02X S:%02X P:%02X(%s)",
reg.PC, reg.A, reg.X, reg.Y, reg.S, reg.P, string(p))
}
// Processor status register flags
const (
C uint8 = 1 << iota // Carry flag, 1 = true
Z // Zero, 1 = Result zero
I // IRQ disable, 1 = disable
D // Decimal mode, 1 = true
B // BRK command
U // Unused
V // Overflow, 1 = true
N // Negative, 1 = true
)
// Interrupt type
type Interrupt uint8
// Interrupt types
const (
None Interrupt = iota //
NMI // Non-Maskable interrupt
IRQ // Interrupt request
)