| 1 | //cxx . . $ `<file>/src/asm/cpu.c`/ |
| 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"cxx . . $ `<file>/src/asm/config.h`/ |
| 9 | #include "alu.h"cxx . . $ `<file>/src/asm/alu.h`/ |
| 10 | #include "cpu.h"cxx . . $ `<file>/src/asm/cpu.h`/ |
| 11 | #include "dynabuf.h"cxx . . $ `<file>/src/asm/dynabuf.h`/ |
| 12 | #include "global.h"cxx . . $ `<file>/src/asm/global.h`/ |
| 13 | #include "input.h"cxx . . $ `<file>/src/asm/input.h`/ |
| 14 | #include "mnemo.h"cxx . . $ `<file>/src/asm/mnemo.h`/ |
| 15 | #include "output.h"cxx . . $ `<file>/src/asm/output.h`/ |
| 16 | #include "tree.h"cxx . . $ `<file>/src/asm/tree.h`/ |
| 17 | |
| 18 | |
| 19 | // Constants |
| 20 | static struct cpu_tcxx . . $ cpu_t# CPU_6502cxx . . $ CPU_6502. = { |
| 21 | keyword_is_6502mnemocxx . . $ keyword_is_6502mnemo(83c267b20bac841a)., |
| 22 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, // no long registers |
| 23 | CPUFLAG_INDJMPBUGGYcxx . . $ `src/asm/cpu.h:24:9`!, // JMP ($xxFF) is buggy |
| 24 | 234 // !align fills with "NOP" |
| 25 | }; |
| 26 | static struct cpu_tcxx . . $ cpu_t# CPU_6510cxx . . $ CPU_6510. = { |
| 27 | keyword_is_6510mnemocxx . . $ keyword_is_6510mnemo(83c267b20bac841a)., |
| 28 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, // no long registers |
| 29 | CPUFLAG_INDJMPBUGGYcxx . . $ `src/asm/cpu.h:24:9`!, // JMP ($xxFF) is buggy |
| 30 | 234 // !align fills with "NOP" |
| 31 | }; |
| 32 | static struct cpu_tcxx . . $ cpu_t# CPU_65c02cxx . . $ CPU_65c02.= { |
| 33 | keyword_is_65c02mnemocxx . . $ keyword_is_65c02mnemo(83c267b20bac841a)., |
| 34 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, // 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 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! long_of_65816cxx . . $ long_of_65816.[2]; // 65816 struct needs array of 2 bools |
| 53 | static struct cpu_tcxx . . $ cpu_t# CPU_65816cxx . . $ CPU_65816. = { |
| 54 | keyword_is_65816mnemocxx . . $ keyword_is_65816mnemo(83c267b20bac841a)., |
| 55 | long_of_65816cxx . . $ long_of_65816., // two booleans for long accu/long regs |
| 56 | 0, // no flags |
| 57 | 234 // !align fills with "NOP" |
| 58 | }; |
| 59 | #define s_rlcxx . . $ `src/asm/cpu.c:59:9`! (s_brl+1) // Yes, I know I'm sick |
| 60 | |
| 61 | |
| 62 | // Variables |
| 63 | struct cpu_tcxx . . $ cpu_t# *CPU_nowcxx . . $ CPU_now.; // Struct of current CPU type (default 6502) |
| 64 | result_int_tcxx . . $ result_int_t# CPU_pccxx . . $ CPU_pc.; // (Pseudo) program counter at start of statement |
| 65 | int CPU_2addcxx . . $ CPU_2add.; // Increase PC by this after statement |
| 66 | static intval_tcxx . . $ intval_t# current_offsetcxx . . $ current_offset.; // PseudoPC - MemIndex |
| 67 | static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! uses_pseudo_pccxx . . $ uses_pseudo_pc.; // offset assembly active? |
| 68 | // predefined stuff |
| 69 | static node_tcxx . . $ node_t#* CPU_treecxx . . $ CPU_tree. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;// tree to hold CPU types |
| 70 | static node_tcxx . . $ node_t# CPUscxx . . $ CPUs.[] = { |
| 71 | // PREDEFNODE("z80", &CPU_Z80), |
| 72 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("6502", &CPU_6502), |
| 73 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("6510", &CPU_6510), |
| 74 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("65c02", &CPU_65c02), |
| 75 | // PREDEFNODE("Rockwell65c02", &CPU_Rockwell65c02), |
| 76 | // PREDEFNODE("WDC65c02", &CPU_WDC65c02), |
| 77 | PREDEFLASTcxx . . $ `src/asm/tree.h:16:9`!(s_65816, &CPU_65816), |
| 78 | // ^^^^ this marks the last element |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | // Insert byte until PC fits condition |
| 83 | static enum eos_tcxx . . $ eos_t# PO_aligncxx . . $ PO_align(6522f478c9c0d97c).(void) { |
| 84 | intval_tcxx . . $ intval_t# and, |
| 85 | equal, |
| 86 | fill, |
| 87 | test = CPU_pccxx . . $ CPU_pc..intvalcxx . . $ result_int_t#intval.; |
| 88 | |
| 89 | // make sure PC is defined. |
| 90 | if((CPU_pccxx . . $ CPU_pc..flagscxx . . $ result_int_t#flags. & MVALUE_DEFINEDcxx . . $ `src/asm/alu.h:27:9`!) == 0) { |
| 91 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).(exception_pc_undefinedcxx . . $ exception_pc_undefined.); |
| 92 | CPU_pccxx . . $ CPU_pc..flagscxx . . $ result_int_t#flags. |= MVALUE_DEFINEDcxx . . $ `src/asm/alu.h:27:9`!; // do not complain again |
| 93 | return(SKIP_REMAINDERcxx . . $ SKIP_REMAINDER.); |
| 94 | } |
| 95 | and = ALU_defined_intcxx . . $ ALU_defined_int(da6335ded91d79c2).(); |
| 96 | if(!Input_accept_commacxx . . $ Input_accept_comma(c9857a73d68e59ba).()) |
| 97 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).(exception_syntaxcxx . . $ exception_syntax.); |
| 98 | equal = ALU_defined_intcxx . . $ ALU_defined_int(da6335ded91d79c2).(); |
| 99 | if(Input_accept_commacxx . . $ Input_accept_comma(c9857a73d68e59ba).()) |
| 100 | fill = ALU_any_intcxx . . $ ALU_any_int(da6335ded91d79c2).(); |
| 101 | else |
| 102 | fill = CPU_nowcxx . . $ CPU_now.->default_align_valuecxx . . $ cpu_t#default_align_value.; |
| 103 | while((test++ & and) != equal) |
| 104 | Output_8bcxx . . $ Output_8b(1624ee8275b10bda).(fill); |
| 105 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 106 | } |
| 107 | |
| 108 | // Try to find CPU type held in DynaBuf. Returns whether succeeded. |
| 109 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! CPU_find_cpu_structcxx . . $ CPU_find_cpu_struct(b31f53e4fd6a1681).(struct cpu_tcxx . . $ cpu_t#** target) { |
| 110 | void* node_body; |
| 111 | |
| 112 | if(!Tree_easy_scancxx . . $ Tree_easy_scan(7f10be3e8224db50).(CPU_treecxx . . $ CPU_tree., &node_body, GlobalDynaBufcxx . . $ GlobalDynaBuf.)) |
| 113 | return(FALSEcxx . . $ `src/asm/config.h:54:9`!); |
| 114 | *target = node_body; |
| 115 | return(TRUEcxx . . $ `src/asm/config.h:55:9`!); |
| 116 | } |
| 117 | |
| 118 | // Select CPU ("!cpu" pseudo opcode) |
| 119 | static enum eos_tcxx . . $ eos_t# PO_cpucxx . . $ PO_cpu(6522f478c9c0d97c).(void) { |
| 120 | struct cpu_tcxx . . $ cpu_t#* cpu_buffer = CPU_nowcxx . . $ CPU_now.; // remember current cpu |
| 121 | |
| 122 | if(Input_read_and_lower_keywordcxx . . $ Input_read_and_lower_keyword(b126dc7c1de90089).()) |
| 123 | if(!CPU_find_cpu_structcxx . . $ CPU_find_cpu_struct(b31f53e4fd6a1681).(&CPU_nowcxx . . $ CPU_now.)) |
| 124 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).("Unknown processor."); |
| 125 | // If there's a block, parse that and then restore old value! |
| 126 | if(Parse_optional_blockcxx . . $ Parse_optional_block(c9857a73d68e59ba).()) |
| 127 | CPU_nowcxx . . $ CPU_now. = cpu_buffer; |
| 128 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 129 | } |
| 130 | |
| 131 | static const char Warning_old_offset_assemblycxx . . $ Warning_old_offset_assembly.[] = |
| 132 | "\"!pseudopc/!realpc\" is deprecated; use \"!pseudopc {}\" instead."; |
| 133 | |
| 134 | // Start offset assembly |
| 135 | static enum eos_tcxx . . $ eos_t# PO_pseudopccxx . . $ PO_pseudopc(6522f478c9c0d97c).(void) { |
| 136 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! outer_state = uses_pseudo_pccxx . . $ uses_pseudo_pc.; |
| 137 | intval_tcxx . . $ intval_t# new_pc, |
| 138 | outer_offset = current_offsetcxx . . $ current_offset.; |
| 139 | int outer_flags = CPU_pccxx . . $ CPU_pc..flagscxx . . $ result_int_t#flags.; |
| 140 | |
| 141 | // set new |
| 142 | new_pc = ALU_defined_intcxx . . $ ALU_defined_int(da6335ded91d79c2).(); |
| 143 | current_offsetcxx . . $ current_offset. = (current_offsetcxx . . $ current_offset. + new_pc - CPU_pccxx . . $ CPU_pc..intvalcxx . . $ result_int_t#intval.) & 0xffff; |
| 144 | CPU_pccxx . . $ CPU_pc..intvalcxx . . $ result_int_t#intval. = new_pc; |
| 145 | CPU_pccxx . . $ CPU_pc..flagscxx . . $ result_int_t#flags. |= MVALUE_DEFINEDcxx . . $ `src/asm/alu.h:27:9`!; |
| 146 | uses_pseudo_pccxx . . $ uses_pseudo_pc. = TRUEcxx . . $ `src/asm/config.h:55:9`!; |
| 147 | // If there's a block, parse that and then restore old value! |
| 148 | if(Parse_optional_blockcxx . . $ Parse_optional_block(c9857a73d68e59ba).()) { |
| 149 | // restore old |
| 150 | uses_pseudo_pccxx . . $ uses_pseudo_pc. = outer_state; |
| 151 | CPU_pccxx . . $ CPU_pc..flagscxx . . $ result_int_t#flags. = outer_flags; |
| 152 | CPU_pccxx . . $ CPU_pc..intvalcxx . . $ result_int_t#intval. = (outer_offset + CPU_pccxx . . $ CPU_pc..intvalcxx . . $ result_int_t#intval. - current_offsetcxx . . $ current_offset.) & 0xffff; |
| 153 | current_offsetcxx . . $ current_offset. = outer_offset; |
| 154 | } else |
| 155 | Throw_first_pass_warningcxx . . $ Throw_first_pass_warning(85c52e162fed56f9).(Warning_old_offset_assemblycxx . . $ Warning_old_offset_assembly.); |
| 156 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 157 | } |
| 158 | |
| 159 | // End offset assembly |
| 160 | static enum eos_tcxx . . $ eos_t# PO_realpccxx . . $ PO_realpc(6522f478c9c0d97c).(void) { |
| 161 | Throw_first_pass_warningcxx . . $ Throw_first_pass_warning(85c52e162fed56f9).(Warning_old_offset_assemblycxx . . $ Warning_old_offset_assembly.); |
| 162 | // deactivate offset assembly |
| 163 | CPU_pccxx . . $ CPU_pc..intvalcxx . . $ result_int_t#intval. = (CPU_pccxx . . $ CPU_pc..intvalcxx . . $ result_int_t#intval. - current_offsetcxx . . $ current_offset.) & 0xffff; |
| 164 | current_offsetcxx . . $ current_offset. = 0; |
| 165 | uses_pseudo_pccxx . . $ uses_pseudo_pc. = FALSEcxx . . $ `src/asm/config.h:54:9`!; |
| 166 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 167 | } |
| 168 | |
| 169 | // return whether offset assembly is active |
| 170 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! CPU_uses_pseudo_pccxx . . $ CPU_uses_pseudo_pc(c9857a73d68e59ba).(void) { |
| 171 | return(uses_pseudo_pccxx . . $ 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_lengthcxx . . $ check_and_set_reg_length(2f6681c2121ca5ab).(boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! *var, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! long_reg) { |
| 177 | if(long_reg && ((CPU_nowcxx . . $ CPU_now.->long_regscxx . . $ cpu_t#long_regs.) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)) |
| 178 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).("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_tcxx . . $ eos_t# set_register_lengthcxx . . $ set_register_length(7281b37ea2c78b60).(boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! *var, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! long_reg) { |
| 185 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! buffer = *var; |
| 186 | |
| 187 | // Set new register length (or complain - whichever is more fitting) |
| 188 | check_and_set_reg_lengthcxx . . $ check_and_set_reg_length(2f6681c2121ca5ab).(var, long_reg); |
| 189 | // If there's a block, parse that and then restore old value! |
| 190 | if(Parse_optional_blockcxx . . $ Parse_optional_block(c9857a73d68e59ba).()) |
| 191 | check_and_set_reg_lengthcxx . . $ check_and_set_reg_length(2f6681c2121ca5ab).(var, buffer);// restore old length |
| 192 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 193 | } |
| 194 | |
| 195 | // Switch to long accu ("!al" pseudo opcode) |
| 196 | static enum eos_tcxx . . $ eos_t# PO_alcxx . . $ PO_al(6522f478c9c0d97c).(void) { |
| 197 | return(set_register_lengthcxx . . $ set_register_length(7281b37ea2c78b60).(CPU_nowcxx . . $ CPU_now.->long_regscxx . . $ cpu_t#long_regs. + LONGREG_IDX_Acxx . . $ `src/asm/cpu.h:19:9`!, TRUEcxx . . $ `src/asm/config.h:55:9`!)); |
| 198 | } |
| 199 | |
| 200 | // Switch to short accu ("!as" pseudo opcode) |
| 201 | static enum eos_tcxx . . $ eos_t# PO_ascxx . . $ PO_as(6522f478c9c0d97c).(void) { |
| 202 | return(set_register_lengthcxx . . $ set_register_length(7281b37ea2c78b60).(CPU_nowcxx . . $ CPU_now.->long_regscxx . . $ cpu_t#long_regs. + LONGREG_IDX_Acxx . . $ `src/asm/cpu.h:19:9`!, FALSEcxx . . $ `src/asm/config.h:54:9`!)); |
| 203 | } |
| 204 | |
| 205 | // Switch to long index registers ("!rl" pseudo opcode) |
| 206 | static enum eos_tcxx . . $ eos_t# PO_rlcxx . . $ PO_rl(6522f478c9c0d97c).(void) { |
| 207 | return(set_register_lengthcxx . . $ set_register_length(7281b37ea2c78b60).(CPU_nowcxx . . $ CPU_now.->long_regscxx . . $ cpu_t#long_regs. + LONGREG_IDX_Rcxx . . $ `src/asm/cpu.h:20:9`!, TRUEcxx . . $ `src/asm/config.h:55:9`!)); |
| 208 | } |
| 209 | |
| 210 | // Switch to short index registers ("!rs" pseudo opcode) |
| 211 | static enum eos_tcxx . . $ eos_t# PO_rscxx . . $ PO_rs(6522f478c9c0d97c).(void) { |
| 212 | return(set_register_lengthcxx . . $ set_register_length(7281b37ea2c78b60).(CPU_nowcxx . . $ CPU_now.->long_regscxx . . $ cpu_t#long_regs. + LONGREG_IDX_Rcxx . . $ `src/asm/cpu.h:20:9`!, FALSEcxx . . $ `src/asm/config.h:54:9`!)); |
| 213 | } |
| 214 | |
| 215 | // pseudo opcode table |
| 216 | static node_tcxx . . $ node_t# pseudo_opcodescxx . . $ pseudo_opcodes.[] = { |
| 217 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("align", PO_align), |
| 218 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("cpu", PO_cpu), |
| 219 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("pseudopc", PO_pseudopc), |
| 220 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("realpc", PO_realpc), |
| 221 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("al", PO_al), |
| 222 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("as", PO_as), |
| 223 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_rlcxx . . $ `src/asm/cpu.c:59:9`!, PO_rl), |
| 224 | PREDEFLASTcxx . . $ `src/asm/tree.h:16:9`!("rs", PO_rs), |
| 225 | // ^^^^ this marks the last element |
| 226 | }; |
| 227 | |
| 228 | // Set default values for pass |
| 229 | void CPU_passinitcxx . . $ CPU_passinit(39d502d8bb4d749b).(struct cpu_tcxx . . $ cpu_t#* cpu_type) { |
| 230 | // handle cpu type (default is 6502) |
| 231 | CPU_nowcxx . . $ CPU_now. = cpu_type ? cpu_type : &CPU_6502cxx . . $ CPU_6502.; |
| 232 | CPU_pccxx . . $ CPU_pc..flagscxx . . $ result_int_t#flags. = 0; // not defined yet |
| 233 | CPU_pccxx . . $ CPU_pc..intvalcxx . . $ result_int_t#intval. = 512; // actually, there should be no need to init |
| 234 | CPU_2addcxx . . $ CPU_2add. = 0; // Increase PC by this at end of statement |
| 235 | CPU_65816cxx . . $ CPU_65816..long_regscxx . . $ cpu_t#long_regs.[LONGREG_IDX_Acxx . . $ `src/asm/cpu.h:19:9`!] = FALSEcxx . . $ `src/asm/config.h:54:9`!; // short accu |
| 236 | CPU_65816cxx . . $ CPU_65816..long_regscxx . . $ cpu_t#long_regs.[LONGREG_IDX_Rcxx . . $ `src/asm/cpu.h:20:9`!] = FALSEcxx . . $ `src/asm/config.h:54:9`!; // short index regs |
| 237 | uses_pseudo_pccxx . . $ uses_pseudo_pc. = FALSEcxx . . $ `src/asm/config.h:54:9`!; // offset assembly is not active, |
| 238 | current_offsetcxx . . $ current_offset. = 0; // so offset is 0 |
| 239 | } |
| 240 | |
| 241 | // create cpu type tree (is done early) |
| 242 | void CPUtype_initcxx . . $ CPUtype_init(49f6e7a06ebc5aa8).(void) { |
| 243 | Tree_add_tablecxx . . $ Tree_add_table(90ba9e887489f1c).(&CPU_treecxx . . $ CPU_tree., CPUscxx . . $ CPUs.); |
| 244 | } |
| 245 | |
| 246 | // register pseudo opcodes (done later) |
| 247 | void CPU_initcxx . . $ CPU_init(49f6e7a06ebc5aa8).(void) { |
| 248 | Tree_add_tablecxx . . $ Tree_add_table(90ba9e887489f1c).(&pseudo_opcode_treecxx . . $ pseudo_opcode_tree., pseudo_opcodescxx . . $ pseudo_opcodes.); |
| 249 | } |
| 250 | |
| 251 | // set program counter to defined value |
| 252 | void CPU_set_pccxx . . $ CPU_set_pc(1624ee8275b10bda).(intval_tcxx . . $ intval_t# new_pc) { |
| 253 | CPU_pccxx . . $ CPU_pc..flagscxx . . $ result_int_t#flags. |= MVALUE_DEFINEDcxx . . $ `src/asm/alu.h:27:9`!; |
| 254 | CPU_pccxx . . $ CPU_pc..intvalcxx . . $ result_int_t#intval. = new_pc; |
| 255 | } |