-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen6502.rb
200 lines (174 loc) · 6.56 KB
/
gen6502.rb
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
require './genutil.rb'
cases = []
code_cache = {}
profiles = []
File.readlines('6502.tsv').each_with_index do |line, idx|
next if idx == 0
elem = line.strip.split(/\t/)
profile = OpenStruct.new
op = elem[0]
num = elem[0].to_i(16)
name = elem[1]
memory = elem[2]
official = elem[3] == "T"
pchfix = elem[4] == "T"
mode = elem[5]
code = elem[6]
enum = "OP_#{ op }_#{ name }#{ official ? '' : '_UNOFFICIAL'}"
profile.op = op
profile.num = num
profile.enum = enum
profile.name = name
profile.mode = mode
profile.official = official
profile.pchfix = pchfix
profile.code = code
profile.memory = memory
case mode
when 'IMPLIED','ACCUMULATOR'; length = 1
when 'ABSOLUTE', 'ABSOLUTE_X', 'ABSOLUTE_Y', 'INDIRECT'; length = 3
else; length = 2
end
profile.length = length
profiles << profile
impl = nil
fetch = "fetch_#{ mode.downcase }"
gen = []
if memory == 'W' || mode == 'IMPLIED'
# none
elsif mode == 'IMPLIED' || mode == 'ACCUMULATOR' || memory == 'RMW' || memory == 'W'
gen << "uint16_t tmp;"
else
gen << "uint16_t tmp = value;"
end
if code
code.split(/,\s*/).each do |elem|
m = /([^(.]+)(?:\((.*)\))?/.match(elem)
action = m[1]
content_raw = (m[2] || 'T')
content = content_raw
.gsub('PULL16','cpu_pull16(self)')
.gsub('PULL','cpu_pull(self)')
.gsub('PC','self->reg.pc')
.gsub('T','tmp')
.gsub('O','value')
.gsub('M','addr')
.gsub('R','cpu_read(self, addr)')
.gsub('C','self->reg.carry')
.gsub('V','self->reg.overflow')
.gsub('N','self->reg.negative')
.gsub('Z','self->reg.zero')
.gsub('S','self->reg.s')
.gsub('X','self->reg.x')
.gsub('Y','self->reg.y')
.gsub('P','self->reg.p')
.gsub('A','self->reg.a')
gen << "// #{action}(#{content_raw})"
case action
when 'T'; gen << "tmp = #{ content };"
when 'M'; gen << "addr = #{ content };"
when 'C'; gen << "self->reg.carry = (bool)(#{ content });"
when 'N'; gen << "self->reg.negative = ((#{ content }) >> 7) & 1;"
when 'Z'; gen << "self->reg.zero = !((#{ content }) & 0xff);"
when 'I'; gen << "self->reg.int_disable = (bool)(#{ content });"
when 'V'; gen << "self->reg.overflow = (bool)(#{ content });"
when 'D'; gen << "self->reg.decimal = (bool)(#{ content });"
when 'W'; gen << "cpu_write(self, addr, #{ content });"
when 'R'; gen << "(void)cpu_read(self, #{ content });"
when 'B'; gen << "cpu_decode_branch(self, addr, #{ content });"
when 'A'; gen << "self->reg.a = #{ content };"
when 'P'; gen << "self->reg.p = #{ content };"
when 'X'; gen << "self->reg.x = #{ content };"
when 'S'; gen << "self->reg.s = #{ content };"
when 'Y'; gen << "self->reg.y = #{ content };"
when 'G'; gen << "bus_clock_cpu(self->shared_bus, #{ content });"
when 'PC'; gen << "self->reg.pc = #{ content };"
when 'VEC'; gen << "self->reg.pc = cpu_read_vector(self, #{ content });"
when 'PUSH16'; gen << "cpu_push16(self, #{ content });"
when 'PUSH'; gen << "cpu_push(self, #{ content });"
when 'IRQ_DELAY'; gen << "self->irq_poll_delay = #{ content };"
when 'UNUSED'; gen << "(void)(#{content});"
when 'NOTIMPL'; gen << "WARN(\"#{name}[#{op}] not implemented\\n\");"
else
if /@\s*([0-9A-F]{2})/ =~ action && code_cache.has_key?(Regexp.last_match[1]) && impl.nil?
impl = code_cache[Regexp.last_match[1]]
code_cache[op] = impl
else
throw "undefined action: #{action}"
end
end
end
if impl.nil?
fname = "cpu_#{ official ? 'decode' : 'decode_unofficial' }_#{op}_#{ name.downcase }"
if memory == "W" || memory == "RMW"
impl = "#{fname}(self, addr);"
signature = "static inline void #{fname}(cpu_t *self, uint16_t addr)"
elsif mode == 'IMPLIED' || mode == 'ACCUMULATOR'
impl = "#{fname}(self);"
signature = "static inline void #{fname}(cpu_t *self)"
else
impl = "#{fname}(self, value);"
signature = "static inline void #{fname}(cpu_t *self, uint8_t value)"
end
code_cache[op] = impl
profile.gencode = gen
profile.fname = signature
profile.func = <<-EOS
// [#{name}] OP:#{op} Mode:#{mode} Length:#{length}
// Semantics:#{code}
#{signature}
{
#{indent gen.join("\n")}
}
EOS
end
end
codes = []
memory_hint = pchfix || (mode.include?('INDIRECT') && mode != 'INDIRECT') || mode.include?('ABSOLUTE')
case memory
when '-'
case mode
when 'IMPLIED', 'ACCUMULATOR'
codes << (impl || "// NOP")
codes << "// fetch next instruction and throw it away"
codes << "(void)cpu_read(self, self->reg.pc);"
else
if code
codes << "uint8_t value = #{ fetch }(self);" << impl
else
codes << "(void)#{ fetch }(self);" << "// NOP"
end
end
when 'R'
codes << "uint16_t addr = #{ fetch }(self#{ memory_hint ? ', true' : ''});"
if code
codes << "uint8_t value = cpu_read(self, addr);" << impl
else
codes << "(void)cpu_read(self, addr);" << "// NOP"
end
when 'W', 'RMW'
codes << "uint16_t addr = #{ fetch }(self#{ memory_hint ? ', false' : '' });"
codes << (impl || "// NOP")
end
cases << indent([
"case #{ enum }: {",
indent(codes.join("\n")),
" break;",
"}"
].join("\n"))
end
ErbalT.new({
:profiles => profiles.select{|p| p.official && p.gencode}
}).render('cpu_decode.template.h', 'cpu_decode_official.h')
ErbalT.new({
:profiles => profiles.select{|p| !p.official && p.gencode}
}).render('cpu_decode.template.h', 'cpu_decode_unofficial.h')
ErbalT.new({
:cases => cases
}).render('cpu_decode_internal.template.h')
ErbalT.new({
:profiles => profiles,
}).render('cpu_opcode.template.h')
ErbalT.new({
:profiles => profiles
}).render('cpu_opcode.template.c')