Debian Codegraph @

src/asm/cpu.c

raw symbols SCIP
1 //
2 // ACME - a crossassembler for producing 6502/65c02/65816 code.
3 // Copyright (C) 1998-2006 Marco Baye
4 // Have a look at "acme.c" for further info
5 //
6 // CPU stuff
7
8 #include "config.h"
9 #include "alu.h"
10 #include "cpu.h"
11 #include "dynabuf.h"
12 #include "global.h"
13 #include "input.h"
14 #include "mnemo.h"
15 #include "output.h"
16 #include "tree.h"
17
18
19 // Constants
20 static struct cpu_t CPU_6502 = {
21 keyword_is_6502mnemo,
22 NULL, // no long registers
23 CPUFLAG_INDJMPBUGGY, // JMP ($xxFF) is buggy
24 234 // !align fills with "NOP"
25 };
26 static struct cpu_t CPU_6510 = {
27 keyword_is_6510mnemo,
28 NULL, // no long registers
29 CPUFLAG_INDJMPBUGGY, // JMP ($xxFF) is buggy
30 234 // !align fills with "NOP"
31 };
32 static struct cpu_t CPU_65c02= {
33 keyword_is_65c02mnemo,
34 NULL, // no long registers
35 0, // no flags
36 234 // !align fills with "NOP"
37 };
38 /*
39 static struct cpu_t CPU_Rockwell65c02 = {
40 keyword_is_Rockwell65c02mnemo,
41 NULL, // no long registers
42 0, // no flags
43 234 // !align fills with "NOP"
44 };
45 static struct cpu_t CPU_WDC65c02 = {
46 keyword_is_WDC65c02mnemo,
47 NULL, // no long registers
48 0, // no flags
49 234 // !align fills with "NOP"
50 };
51 */
52 static bool long_of_65816[2]; // 65816 struct needs array of 2 bools
53 static struct cpu_t CPU_65816 = {
54 keyword_is_65816mnemo,
55 long_of_65816, // two booleans for long accu/long regs
56 0, // no flags
57 234 // !align fills with "NOP"
58 };
59 #define s_rl (s_brl+1) // Yes, I know I'm sick
60
61
62 // Variables
63 struct cpu_t *CPU_now; // Struct of current CPU type (default 6502)
64 result_int_t CPU_pc; // (Pseudo) program counter at start of statement
65 int CPU_2add; // Increase PC by this after statement
66 static intval_t current_offset; // PseudoPC - MemIndex
67 static bool uses_pseudo_pc; // offset assembly active?
68 // predefined stuff
69 static node_t* CPU_tree = NULL;// tree to hold CPU types
70 static node_t CPUs[] = {
71 // PREDEFNODE("z80", &CPU_Z80),
72 PREDEFNODE("6502", &CPU_6502),
73 PREDEFNODE("6510", &CPU_6510),
74 PREDEFNODE("65c02", &CPU_65c02),
75 // PREDEFNODE("Rockwell65c02", &CPU_Rockwell65c02),
76 // PREDEFNODE("WDC65c02", &CPU_WDC65c02),
77 PREDEFLAST(s_65816, &CPU_65816),
78 // ^^^^ this marks the last element
79 };
80
81
82 // Insert byte until PC fits condition
83 static enum eos_t PO_align(void) {
84 intval_t and,
85 equal,
86 fill,
87 test = CPU_pc.intval;
88
89 // make sure PC is defined.
90 if((CPU_pc.flags & MVALUE_DEFINED) == 0) {
91 Throw_error(exception_pc_undefined);
92 CPU_pc.flags |= MVALUE_DEFINED; // do not complain again
93 return(SKIP_REMAINDER);
94 }
95 and = ALU_defined_int();
96 if(!Input_accept_comma())
97 Throw_error(exception_syntax);
98 equal = ALU_defined_int();
99 if(Input_accept_comma())
100 fill = ALU_any_int();
101 else
102 fill = CPU_now->default_align_value;
103 while((test++ & and) != equal)
104 Output_8b(fill);
105 return(ENSURE_EOS);
106 }
107
108 // Try to find CPU type held in DynaBuf. Returns whether succeeded.
109 bool CPU_find_cpu_struct(struct cpu_t** target) {
110 void* node_body;
111
112 if(!Tree_easy_scan(CPU_tree, &node_body, GlobalDynaBuf))
113 return(FALSE);
114 *target = node_body;
115 return(TRUE);
116 }
117
118 // Select CPU ("!cpu" pseudo opcode)
119 static enum eos_t PO_cpu(void) {
120 struct cpu_t* cpu_buffer = CPU_now; // remember current cpu
121
122 if(Input_read_and_lower_keyword())
123 if(!CPU_find_cpu_struct(&CPU_now))
124 Throw_error("Unknown processor.");
125 // If there's a block, parse that and then restore old value!
126 if(Parse_optional_block())
127 CPU_now = cpu_buffer;
128 return(ENSURE_EOS);
129 }
130
131 static const char Warning_old_offset_assembly[] =
132 "\"!pseudopc/!realpc\" is deprecated; use \"!pseudopc {}\" instead.";
133
134 // Start offset assembly
135 static enum eos_t PO_pseudopc(void) {
136 bool outer_state = uses_pseudo_pc;
137 intval_t new_pc,
138 outer_offset = current_offset;
139 int outer_flags = CPU_pc.flags;
140
141 // set new
142 new_pc = ALU_defined_int();
143 current_offset = (current_offset + new_pc - CPU_pc.intval) & 0xffff;
144 CPU_pc.intval = new_pc;
145 CPU_pc.flags |= MVALUE_DEFINED;
146 uses_pseudo_pc = TRUE;
147 // If there's a block, parse that and then restore old value!
148 if(Parse_optional_block()) {
149 // restore old
150 uses_pseudo_pc = outer_state;
151 CPU_pc.flags = outer_flags;
152 CPU_pc.intval = (outer_offset + CPU_pc.intval - current_offset) & 0xffff;
153 current_offset = outer_offset;
154 } else
155 Throw_first_pass_warning(Warning_old_offset_assembly);
156 return(ENSURE_EOS);
157 }
158
159 // End offset assembly
160 static enum eos_t PO_realpc(void) {
161 Throw_first_pass_warning(Warning_old_offset_assembly);
162 // deactivate offset assembly
163 CPU_pc.intval = (CPU_pc.intval - current_offset) & 0xffff;
164 current_offset = 0;
165 uses_pseudo_pc = FALSE;
166 return(ENSURE_EOS);
167 }
168
169 // return whether offset assembly is active
170 bool CPU_uses_pseudo_pc(void) {
171 return(uses_pseudo_pc);
172 }
173
174 // If cpu type and value match, set register length variable to value.
175 // If cpu type and value don't match, complain instead.
176 static void check_and_set_reg_length(bool *var, bool long_reg) {
177 if(long_reg && ((CPU_now->long_regs) == NULL))
178 Throw_error("Chosen CPU does not support long registers.");
179 else
180 *var = long_reg;
181 }
182
183 // Set register length, block-wise if needed.
184 static enum eos_t set_register_length(bool *var, bool long_reg) {
185 bool buffer = *var;
186
187 // Set new register length (or complain - whichever is more fitting)
188 check_and_set_reg_length(var, long_reg);
189 // If there's a block, parse that and then restore old value!
190 if(Parse_optional_block())
191 check_and_set_reg_length(var, buffer);// restore old length
192 return(ENSURE_EOS);
193 }
194
195 // Switch to long accu ("!al" pseudo opcode)
196 static enum eos_t PO_al(void) {
197 return(set_register_length(CPU_now->long_regs + LONGREG_IDX_A, TRUE));
198 }
199
200 // Switch to short accu ("!as" pseudo opcode)
201 static enum eos_t PO_as(void) {
202 return(set_register_length(CPU_now->long_regs + LONGREG_IDX_A, FALSE));
203 }
204
205 // Switch to long index registers ("!rl" pseudo opcode)
206 static enum eos_t PO_rl(void) {
207 return(set_register_length(CPU_now->long_regs + LONGREG_IDX_R, TRUE));
208 }
209
210 // Switch to short index registers ("!rs" pseudo opcode)
211 static enum eos_t PO_rs(void) {
212 return(set_register_length(CPU_now->long_regs + LONGREG_IDX_R, FALSE));
213 }
214
215 // pseudo opcode table
216 static node_t pseudo_opcodes[] = {
217 PREDEFNODE("align", PO_align),
218 PREDEFNODE("cpu", PO_cpu),
219 PREDEFNODE("pseudopc", PO_pseudopc),
220 PREDEFNODE("realpc", PO_realpc),
221 PREDEFNODE("al", PO_al),
222 PREDEFNODE("as", PO_as),
223 PREDEFNODE(s_rl, PO_rl),
224 PREDEFLAST("rs", PO_rs),
225 // ^^^^ this marks the last element
226 };
227
228 // Set default values for pass
229 void CPU_passinit(struct cpu_t* cpu_type) {
230 // handle cpu type (default is 6502)
231 CPU_now = cpu_type ? cpu_type : &CPU_6502;
232 CPU_pc.flags = 0; // not defined yet
233 CPU_pc.intval = 512; // actually, there should be no need to init
234 CPU_2add = 0; // Increase PC by this at end of statement
235 CPU_65816.long_regs[LONGREG_IDX_A] = FALSE; // short accu
236 CPU_65816.long_regs[LONGREG_IDX_R] = FALSE; // short index regs
237 uses_pseudo_pc = FALSE; // offset assembly is not active,
238 current_offset = 0; // so offset is 0
239 }
240
241 // create cpu type tree (is done early)
242 void CPUtype_init(void) {
243 Tree_add_table(&CPU_tree, CPUs);
244 }
245
246 // register pseudo opcodes (done later)
247 void CPU_init(void) {
248 Tree_add_table(&pseudo_opcode_tree, pseudo_opcodes);
249 }
250
251 // set program counter to defined value
252 void CPU_set_pc(intval_t new_pc) {
253 CPU_pc.flags |= MVALUE_DEFINED;
254 CPU_pc.intval = new_pc;
255 }