Debian Codegraph @

src/cpu.c

raw symbols SCIP
1 // ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code.
2 // Copyright (C) 1998-2024 Marco Baye
3 // Have a look at "acme.c" for further info
4 //
5 // CPU type stuff
6 #include "cpu.h"
7 #include "dynabuf.h"
8 #include "global.h"
9 #include "mnemo.h"
10 #include "tree.h"
11
12
13 // predefined stuff
14
15 static struct cpu_type cpu_type_6502 = {
16 keyword_is_6502_mnemo,
17 CPUFLAG_WARN_ABOUT_FF_PTR | CPUFLAG_INDIRECTJMPBUGGY, // warn about "XYZ ($ff),y" and "jmp ($XYff)"
18 // 0xffff,
19 0x200,
20 234 // !align fills with "NOP"
21 };
22 static struct cpu_type cpu_type_nmos6502 = {
23 keyword_is_nmos6502_mnemo,
24 CPUFLAG_WARN_ABOUT_FF_PTR | CPUFLAG_INDIRECTJMPBUGGY | CPUFLAG_8B_AND_AB_NEED_0_ARG, // ANE/LXA #$xx are unstable unless arg is $00
25 // 0xffff,
26 0x200,
27 234 // !align fills with "NOP"
28 };
29 static struct cpu_type cpu_type_c64dtv2 = {
30 keyword_is_c64dtv2_mnemo,
31 CPUFLAG_WARN_ABOUT_FF_PTR | CPUFLAG_INDIRECTJMPBUGGY | CPUFLAG_8B_AND_AB_NEED_0_ARG,
32 // 0xffff,
33 0x200,
34 234 // !align fills with "NOP"
35 };
36 static struct cpu_type cpu_type_65c02 = {
37 keyword_is_65c02_mnemo,
38 CPUFLAG_WARN_ABOUT_FF_PTR, // from WDC docs
39 // 0xffff,
40 0x200,
41 234 // !align fills with "NOP"
42 };
43 static struct cpu_type cpu_type_r65c02 = {
44 keyword_is_r65c02_mnemo,
45 CPUFLAG_WARN_ABOUT_FF_PTR, // from WDC docs
46 // 0xffff,
47 0x200,
48 234 // !align fills with "NOP"
49 };
50 static struct cpu_type cpu_type_w65c02 = {
51 keyword_is_w65c02_mnemo,
52 CPUFLAG_WARN_ABOUT_FF_PTR, // from WDC docs
53 // 0xffff,
54 0x200,
55 234 // !align fills with "NOP"
56 };
57 static struct cpu_type cpu_type_65816 = {
58 keyword_is_65816_mnemo,
59 // TODO - what about CPUFLAG_WARN_ABOUT_FF_PTR? only needed for old opcodes in emulation mode!
60 CPUFLAG_SUPPORTSLONGREGS, // allows A and XY to be 16bits wide
61 // 0xffff,
62 0x200,
63 234 // !align fills with "NOP"
64 };
65 static struct cpu_type cpu_type_65ce02 = {
66 keyword_is_65ce02_mnemo,
67 CPUFLAG_DECIMALSUBTRACTBUGGY, // SBC does not work reliably in decimal mode
68 // 0xffff,
69 0x200,
70 234 // !align fills with "NOP"
71 };
72 static struct cpu_type cpu_type_4502 = {
73 keyword_is_4502_mnemo,
74 CPUFLAG_DECIMALSUBTRACTBUGGY, // SBC does not work reliably in decimal mode
75 // 0xffff,
76 0x200,
77 234 // !align fills with "NOP"
78 };
79 static struct cpu_type cpu_type_m65 = {
80 keyword_is_m65_mnemo,
81 CPUFLAG_WARN_ABOUT_FF_PTR, // TODO - remove this? check datasheets/realhw!
82 // 0xffff,
83 0x200,
84 234 // !align fills with "NOP"
85 };
86
87 static struct ronode cputype_tree[] = {
88 PREDEF_START,
89 // PREDEFNODE("z80", &cpu_type_Z80),
90 PREDEFNODE("6502", &cpu_type_6502),
91 PREDEFNODE("nmos6502", &cpu_type_nmos6502),
92 PREDEFNODE("6510", &cpu_type_nmos6502),
93 PREDEFNODE("65c02", &cpu_type_65c02),
94 PREDEFNODE("r65c02", &cpu_type_r65c02),
95 PREDEFNODE("w65c02", &cpu_type_w65c02),
96 PREDEFNODE("65816", &cpu_type_65816),
97 PREDEFNODE("65ce02", &cpu_type_65ce02),
98 PREDEFNODE("4502", &cpu_type_4502),
99 PREDEFNODE("m65", &cpu_type_m65),
100 PREDEF_END("c64dtv2", &cpu_type_c64dtv2),
101 // ^^^^ this marks the last element
102 };
103 // string shown in CLI error message if cputype_find() returns NULL:
104 const char cputype_names[] = "'6502', 'nmos6502', '6510', '65c02', 'r65c02', 'w65c02', '65816', '65ce02', '4502', 'm65', 'c64dtv2'";
105
106
107 // variables
108 const struct cpu_type *cpu_current_type = NULL;
109 boolean cpu_a_is_long = FALSE;
110 boolean cpu_xy_are_long = FALSE;
111
112
113 // lookup cpu type held in DynaBuf and return its struct pointer (or NULL on failure)
114 const struct cpu_type *cputype_find(void)
115 {
116 void *node_body;
117
118 // perform lookup
119 if (!tree_easy_scan(cputype_tree, &node_body, GlobalDynaBuf))
120 return NULL;
121
122 return node_body;
123 }
124
125
126 // if cpu type and value match, set register length variable to value.
127 // if cpu type and value don't match, complain instead.
128 // FIXME - error message might be confusing if it is thrown not because of
129 // initial change, but because of reverting back to old cpu type after "{}" block!
130 void vcpu_check_and_set_reg_length(boolean *var, boolean make_long)
131 {
132 if (((cpu_current_type->flags & CPUFLAG_SUPPORTSLONGREGS) == 0) && make_long)
133 throw_error("Chosen CPU does not support long registers.");
134 else
135 *var = make_long;
136 }
137
138
139 // set default values for pass
140 void cputype_passinit(void)
141 {
142 // set cpu type (default to 6502)
143 if (config.initial_cpu_type != NULL)
144 cpu_current_type = config.initial_cpu_type;
145 else
146 cpu_current_type = &cpu_type_6502;
147 // start with short registers
148 cpu_a_is_long = FALSE;
149 cpu_xy_are_long = FALSE;
150 }