-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddr.go
125 lines (111 loc) · 2.7 KB
/
addr.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
package mos65xx
import (
"github.com/tehmaze/mos65xx/memory"
)
// Vectors
const (
NMIVector = 0xfffa
ResetVector = 0xfffc
IRQVector = 0xfffe
)
// zeros is empty memory
var zeros = make([]byte, 256)
func init() {
for i := range zeros {
zeros[i] = 0xff
}
}
// AddressMode determines how the CPU will fetch the address
type AddressMode uint8
// Address modes
const (
Implied AddressMode = iota
Accumulator
Immediate
ZeroPage
ZeroPageX
ZeroPageY
Relative
Absolute
AbsoluteX
AbsoluteY
Indirect
IndexedIndirect
IndirectIndexed
)
var (
addressModeName = map[AddressMode]string{
Implied: "implied",
Accumulator: "accumulator",
Immediate: "immediate",
ZeroPage: "zero-page",
ZeroPageX: "zero-page indexed X",
ZeroPageY: "zero-page indexed Y",
Relative: "relative",
Absolute: "absolute",
AbsoluteX: "absolute indexed X",
AbsoluteY: "absolute indexed Y",
Indirect: "indirect",
IndexedIndirect: "indexed indirect",
IndirectIndexed: "indirect indexed",
}
addressModeCycles = map[AddressMode]int{
Implied: 2,
Accumulator: 0,
Immediate: 2,
ZeroPage: 3,
ZeroPageX: 4,
ZeroPageY: 4,
Relative: 2, // +1 on branch, +1 if branch to different page
Absolute: 4,
AbsoluteX: 4, // +1 on page cross
AbsoluteY: 4, // +1 on page cross
Indirect: 0,
IndexedIndirect: 6,
IndirectIndexed: 5, // +1 on page cross
}
)
// Cycles to fetch the operand address
func (mode AddressMode) Cycles() int {
return addressModeCycles[mode]
}
// FetchPenalty is the cycle penalty for doing page cross or branch on a fetch operation.
func (mode AddressMode) FetchPenalty() int {
return 0 // TODO
}
// StorePenalty is the cycle penalty for doing page cross on a store operation.
func (mode AddressMode) StorePenalty() int {
switch mode {
case AbsoluteX, AbsoluteY, IndirectIndexed:
return 1
default:
return 0
}
}
func (mode AddressMode) String() string {
if s, ok := addressModeName[mode]; ok {
return s
}
return "Invalid"
}
// FetchWord is a helper to fetch a 16-bit word from memory
func FetchWord(mem memory.Memory, addr uint16) uint16 {
var (
lo = uint16(mem.Fetch(addr))
hi = uint16(mem.Fetch(addr+1)) << 8
)
return lo | hi
}
// FetchWordBug is a helper to fetch a 16-bit word from memory
func FetchWordBug(mem memory.Memory, addr uint16) uint16 {
var (
lo = uint16(mem.Fetch(addr))
hi = uint16(mem.Fetch(addr&0xff00)) | uint16(uint8(addr+1))<<8
)
return lo | hi
}
// StoreWord is a helper to store a 16-bit word on a bus
func StoreWord(mem memory.Memory, addr, value uint16) {
mem.Store(addr+0, uint8(value))
mem.Store(addr+1, uint8(value>>8))
}