-
Notifications
You must be signed in to change notification settings - Fork 2
/
disassemble.vhdl
246 lines (222 loc) · 9.82 KB
/
disassemble.vhdl
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use WORK.ALL;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;
use Std.TextIO.all;
use work.debugtools.all;
use work.instructions.all;
use work.alu.all;
use work.extra_instruction_equations.all;
use work.addressing_modes.all;
use work.types.all;
use work.instruction_types.all;
package disassemble is
type instruction is (
-- 4510 opcodes
I_ADC,I_AND,I_ASL,I_ASR,I_ASW,I_BBR,I_BBS,I_BCC,
I_BCS,I_BEQ,I_BIT,I_BMI,I_BNE,I_BPL,I_BRA,I_BRK,
I_BSR,I_BVC,I_BVS,I_CLC,I_CLD,I_CLE,I_CLI,I_CLV,
I_CMP,I_CPX,I_CPY,I_CPZ,I_DEC,I_DEW,I_DEX,I_DEY,
I_DEZ,I_EOM,I_EOR,I_INC,I_INW,I_INX,I_INY,I_INZ,
I_JMP,I_JSR,I_LDA,I_LDX,I_LDY,I_LDZ,I_LSR,I_MAP,
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X,I_LDY,I_LDA,I_LDX,I_LAX,I_CLV,I_LDA,I_TSX,I_LAS,I_LDY,I_LDA,I_LDX,I_LAX,
I_CPY,I_CMP,I_NOP,I_DCP,I_CPY,I_CMP,I_DEC,I_DCP,I_INY,I_CMP,I_DEX,I_AXS,I_CPY,I_CMP,I_DEC,I_DCP,
I_BNE,I_CMP,I_KIL,I_DCP,I_NOP,I_CMP,I_DEC,I_DCP,I_CLD,I_CMP,I_NOP,I_DCP,I_NOP,I_CMP,I_DEC,I_DCP,
I_CPX,I_SBC,I_NOP,I_ISC,I_CPX,I_SBC,I_INC,I_ISC,I_INX,I_SBC,I_NOP,I_SBC,I_CPX,I_SBC,I_INC,I_ISC,
I_BEQ,I_SBC,I_KIL,I_ISC,I_NOP,I_SBC,I_INC,I_ISC,I_SED,I_SBC,I_NOP,I_ISC,I_NOP,I_SBC,I_INC,I_ISC
);
function disassemble_instruction(pc : unsigned(15 downto 0);
bytes : instruction_bytes;
cpu_mode : cpu_personality) return string;
end package;
package body disassemble is
function disassemble_instruction(pc : unsigned(15 downto 0);
bytes : instruction_bytes;
cpu_mode : cpu_personality) return string is
variable justification : side := RIGHT;
variable size : width := 0;
variable s : string(1 to 110) := (others => ' ');
variable t : string(1 to 100) := (others => ' ');
variable opcode : std_logic_vector(8 downto 0);
variable branch_pc : unsigned(15 downto 0);
variable offset : integer;
variable addr_mode : addressing_mode;
begin
--pragma synthesis_off
opcode(7 downto 0) := std_logic_vector(bytes.opcode);
if cpu_mode = CPU6502 then
opcode(8) := '1';
else
opcode(8) := '0';
end if;
t(1 to 5) := instruction'image(instruction_lut(to_integer(unsigned(opcode))));
addr_mode := get_addressing_modes(opcode);
offset := 1;
s(offset to offset) := "$";
offset := offset + 1;
s(offset to (offset+3)) := to_hstring(pc);
offset := offset + 4;
s(offset to (offset+1)) := " ";
offset := offset + 2;
s(offset to (offset+1)) := to_hstring(bytes.opcode);
s((offset+2) to (offset+2)) := " ";
offset := offset + 3;
if addr_mode.imm8 or addr_mode.rel8 or addr_mode.rel16
or addr_mode.addr8 or addr_mode.addr16 or addr_mode.imm16
or addr_mode.rel8byte3 then
s(offset to (offset+1)) := to_hstring(bytes.arg1);
s((offset+2) to (offset+2)) := " ";
offset := offset + 3;
if addr_mode.imm16 or addr_mode.rel16 or addr_mode.addr16 then
s(offset to (offset+1)) := to_hstring(bytes.arg2);
s((offset+2) to (offset+2)) := " ";
offset := offset + 3;
else
s(offset to (offset+2)) := " ";
offset := offset + 3;
end if;
else
s(offset to (offset+5)) := " ";
offset := offset + 6;
end if;
-- Space before instruction name
s(offset to offset) := " ";
offset := offset + 1;
-- Actual instruction name
s(offset to (offset+2)) := t(3 to 5);
offset := offset + 3;
-- Draw 0-7 digit on BBS/BBR instructions
case instruction_lut(to_integer(unsigned(opcode))) is
when I_BBS =>
s(offset to offset) := to_hstring("0"&bytes.opcode(6 downto 4))(1 to 1);
offset := offset + 1;
when I_BBR =>
s(offset to offset) := to_hstring("0"&bytes.opcode(6 downto 4))(1 to 1);
offset := offset + 1;
when others =>
null;
end case;
-- Space after instruction name
s(offset to offset) := " ";
offset := offset + 1;
if addr_mode.indirect then
s(offset to offset) := "(";
offset := offset + 1;
end if;
if addr_mode.imm8 or addr_mode.imm16 then
s(offset to offset) := "#";
offset := offset + 1;
end if;
if addr_mode.a then
s(offset to offset) := "A";
offset := offset + 1;
elsif addr_mode.imm8 or addr_mode.rel8 or addr_mode.rel16
or addr_mode.addr8 or addr_mode.addr16 or addr_mode.imm16
or addr_mode.rel8byte3 then
s(offset to offset) := "$";
offset := offset + 1;
end if;
if addr_mode.imm16 or addr_mode.addr16 then
s(offset to (offset+1)) := to_hstring(bytes.arg2)(1 to 2);
offset := offset + 2;
end if;
if addr_mode.imm8 or addr_mode.addr8 or addr_mode.rel8byte3
or addr_mode.imm16 or addr_mode.addr16 then
s(offset to (offset+1)) := to_hstring(bytes.arg1)(1 to 2);
offset := offset + 2;
end if;
if addr_mode.prex then
s(offset to (offset+1)) := ",X";
offset := offset + 2;
end if;
if addr_mode.presp then
s(offset to (offset+2)) := ",SP";
offset := offset + 3;
end if;
if addr_mode.indirect then
s(offset to offset) := ")";
offset := offset + 1;
end if;
if addr_mode.rel8byte3 then
s(offset to offset +1 ) := ",$";
offset := offset + 2;
end if;
branch_pc :=
to_unsigned(65539 + to_integer(pc) + to_integer(bytes.arg2&bytes.arg1),16);
if addr_mode.rel8byte3 then
branch_pc :=
to_unsigned(65538 + to_integer(pc) + to_integer(
bytes.arg2(7)&bytes.arg2(7)&bytes.arg2(7)&bytes.arg2(7)&
bytes.arg2(7)&bytes.arg2(7)&bytes.arg2(7)&bytes.arg2(7)&
bytes.arg2),16);
end if;
if addr_mode.rel8 then
branch_pc :=
to_unsigned(65539 + to_integer(pc) + to_integer(
bytes.arg1(7)&bytes.arg1(7)&bytes.arg1(7)&bytes.arg1(7)&
bytes.arg1(7)&bytes.arg1(7)&bytes.arg1(7)&bytes.arg1(7)&
bytes.arg1),16);
end if;
if addr_mode.rel8 or addr_mode.rel16 or addr_mode.rel8byte3 then
s(offset to offset+3) := to_hstring(branch_pc);
offset := offset + 4;
end if;
if addr_mode.postx then
s(offset to (offset+1)) := ",X";
offset := offset + 2;
end if;
if addr_mode.posty then
s(offset to (offset+1)) := ",Y";
offset := offset + 2;
end if;
if addr_mode.postz then
s(offset to (offset+1)) := ",Z";
offset := offset + 2;
end if;
return s(1 to offset-1);
--pragma synthesis_on
end function;
end package body;