-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.go
138 lines (124 loc) · 3.68 KB
/
memory.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
package main
import (
"strconv"
)
type Memory struct {
zero_page [0x0100 - 0x000]int
system_stack [0x0200 - 0x0100]int
gen_memory [0xFFFA - 0x0200]int
interrupt_handler [0x2]int
power_reset [0x2]int
interrupt_reset [0x2]int
}
func (m *Memory) immediate_addr(mem_addr int) *int {
return &m.zero_page[mem_addr]
}
func (m *Memory) zero_page_addr(mem_addr int) *int {
return &m.zero_page[mem_addr]
}
func (m *Memory) zero_page_x_addr(mem_addr int, cpu *CPU) *int {
if mem_addr+cpu.register_x > 0xff {
return m.zero_page_x_addr(mem_addr-0x100, cpu)
}
return &m.zero_page[mem_addr+cpu.register_x]
}
func (m *Memory) zero_page_y_addr(mem_addr int, cpu *CPU) *int {
if mem_addr+cpu.register_y > 0xff {
return m.zero_page_y_addr(mem_addr-0x100, cpu)
}
return &m.zero_page[mem_addr+cpu.register_y]
}
func (m *Memory) absolute_addr(mem_addr int, cpu *CPU) *int {
if mem_addr < 0x100 {
return &m.zero_page[mem_addr]
} else if mem_addr < 512 {
return &m.system_stack[mem_addr-0x100]
} else if mem_addr < 0x10000 {
return &m.gen_memory[mem_addr-0x200]
} else {
return m.absolute_addr(mem_addr-0x10000, cpu)
}
}
func (m *Memory) absolute_x_addr(mem_addr int, check bool, cpu *CPU) *int {
if check {
check_cycle(mem_addr, cpu.register_x, cpu)
}
if mem_addr+cpu.register_x < 0x100 {
return &m.zero_page[mem_addr+cpu.register_x]
} else if mem_addr+cpu.register_x < 0x200 {
return &m.system_stack[mem_addr-0x100+cpu.register_x]
} else if mem_addr+cpu.register_x < 0x10000 {
return &m.gen_memory[mem_addr-0x200+cpu.register_x]
} else {
cpu.cycles++
return m.absolute_x_addr(mem_addr-0x10000, false, cpu)
}
}
func check_cycle(mem_addr int, offset int, cpu *CPU) {
if (mem_addr&0b11111111)+offset > 0xFF {
cpu.cycles++
}
}
func (m *Memory) absolute_y_addr(mem_addr int, cpu *CPU) *int {
if mem_addr+cpu.register_y < 0x100 {
check_cycle(mem_addr, cpu.register_y, cpu)
return &m.zero_page[mem_addr+cpu.register_y]
} else if mem_addr+cpu.register_y < 0x200 {
check_cycle(mem_addr, cpu.register_y, cpu)
return &m.system_stack[mem_addr-0x100+cpu.register_y]
} else if mem_addr+cpu.register_y < 0x10000 {
check_cycle(mem_addr, cpu.register_y, cpu)
return &m.gen_memory[mem_addr-0x200+cpu.register_y]
} else {
return m.absolute_y_addr(mem_addr-0x10000, cpu)
}
}
func (m *Memory) indirect(first_half string, second_half string) {
flipped_str := second_half + first_half
retval := string_to_int(flipped_str)
println(retval, "implement indirect??")
}
func (m *Memory) indexed_indirect_addr(val int, cpu *CPU) *int {
if val+cpu.register_x > 0xff {
val -= 0x100
}
lower := strconv.FormatInt(int64(m.zero_page[val+cpu.register_x]), 16)
var upper string
if val == 0xff {
upper = strconv.FormatInt(int64(m.zero_page[cpu.register_x]), 16)
if m.zero_page[cpu.register_x] < 0x10 {
upper = "0" + upper
}
} else {
upper = strconv.FormatInt(int64(m.zero_page[val+cpu.register_x+1]), 16)
if m.zero_page[val+cpu.register_x+1] < 0x10 {
upper = "0" + upper
}
}
if m.zero_page[val+cpu.register_x] < 0x10 {
lower = "0" + lower
}
ret_val := string_to_int("$" + upper + lower)
return m.absolute_addr(ret_val, cpu)
}
func (m *Memory) indirect_indexed_addr(val int, cpu *CPU) *int {
upper_val := val + 1
if upper_val > 0xFF {
upper_val -= 0x100
cpu.cycles++
}
lower := strconv.FormatInt(int64(m.zero_page[val]), 16)
upper := strconv.FormatInt(int64(m.zero_page[upper_val]), 16)
if m.zero_page[val] < 0x10 {
lower = "0" + lower
}
if m.zero_page[upper_val] < 0x10 {
upper = "0" + upper
}
ret_val := string_to_int("$"+upper+lower) + cpu.register_y
if ret_val > 0xFFFF {
ret_val -= 0x10000
cpu.cycles++
}
return m.absolute_addr(ret_val, cpu)
}