Function isa_6502

Source
pub fn isa_6502() -> Vec<InstructionDef>
Expand description

Return all instruction definitions inside the 6502 instruction set.

Examples found in repository?
examples/codegen_opcodes.rs (line 22)
6fn main() {
7    fn format_opcode(result: &mut Vec<String>, instruction: &str, opcode: OpCode, post: &str) {
8        if opcode != NO_IMPLIED {
9            result.push(format!(
10                "/// OpCode for the {} instruction in addressing mode {}
11pub const {}_{}:OpCode = 0x{:02x};",
12                instruction,
13                post.to_lowercase(),
14                instruction.to_string().to_uppercase(),
15                post,
16                opcode
17            ));
18        }
19    }
20
21    let mut lines = Vec::<String>::default();
22    for def in isa_6502() {
23        format_opcode(&mut lines, def.instruction, def.implied, "IMPLIED");
24        format_opcode(&mut lines, def.instruction, def.immediate, "IMMEDIATE");
25        format_opcode(&mut lines, def.instruction, def.accumulator, "ACCUMULATOR");
26        format_opcode(&mut lines, def.instruction, def.absolute, "ABSOLUTE");
27        format_opcode(&mut lines, def.instruction, def.absolute_x, "ABSOLUTE_X");
28        format_opcode(&mut lines, def.instruction, def.absolute_y, "ABSOLUTE_Y");
29        format_opcode(&mut lines, def.instruction, def.zeropage, "ZEROPAGE");
30        format_opcode(&mut lines, def.instruction, def.zeropage_x, "ZEROPAGE_X");
31        format_opcode(&mut lines, def.instruction, def.zeropage_y, "ZEROPAGE_Y");
32        format_opcode(&mut lines, def.instruction, def.relative, "RELATIVE");
33        format_opcode(&mut lines, def.instruction, def.indirect, "INDIRECT");
34        format_opcode(&mut lines, def.instruction, def.indexed_indirect, "INDEXED_INDIRECT");
35        format_opcode(&mut lines, def.instruction, def.indirect_indexed, "INDIRECT_INDEXED");
36    }
37
38    println!("{}", lines.join("\n"));
39}
More examples
Hide additional examples
examples/codegen_instruction_definitions.rs (line 61)
10fn main() {
11    fn format(def: &InstructionDef, op_code: OpCode, no_opcode: OpCode, opcode_str: &str) -> String {
12        if op_code == no_opcode {
13            format!("NO_{}", opcode_str)
14        } else {
15            format!("{}_{}", def.instruction.to_uppercase(), opcode_str)
16        }
17    }
18
19    fn format_opcodes(result: &mut Vec<String>, def: &InstructionDef) {
20        let line = format!(
21            "
22/// Instruction definition for {1}
23/// 
24/// Includes the instruction name and its [OpCode] for a address mode. 
25pub const OPCODES_{0}:InstructionDef = InstructionDef {{
26    instruction: \"{1}\",
27    implied: {2},
28    immediate: {3},
29    accumulator: {4},
30    absolute: {5},
31    absolute_x: {6},
32    absolute_y: {7},
33    zeropage: {8},
34    zeropage_x: {9},
35    zeropage_y: {10},
36    relative: {11},
37    indirect: {12},
38    indexed_indirect: {13},
39    indirect_indexed: {14},
40}};",
41            def.instruction.to_uppercase(),
42            def.instruction.to_string(),
43            format(def, def.implied, NO_IMPLIED, "IMPLIED"),
44            format(def, def.immediate, NO_IMMEDIATE, "IMMEDIATE"),
45            format(def, def.accumulator, NO_ACCUMULATOR, "ACCUMULATOR"),
46            format(def, def.absolute, NO_ABSOLUTE, "ABSOLUTE"),
47            format(def, def.absolute_x, NO_ABSOLUTE_X, "ABSOLUTE_X"),
48            format(def, def.absolute_y, NO_ABSOLUTE_Y, "ABSOLUTE_Y"),
49            format(def, def.zeropage, NO_ZEROPAGE, "ZEROPAGE"),
50            format(def, def.zeropage_x, NO_ZEROPAGE_X, "ZEROPAGE_X"),
51            format(def, def.zeropage_y, NO_ZEROPAGE_Y, "ZEROPAGE_Y"),
52            format(def, def.relative, NO_RELATIVE, "RELATIVE"),
53            format(def, def.indirect, NO_INDIRECT, "INDIRECT"),
54            format(def, def.indexed_indirect, NO_INDEXED_INDIRECT, "INDEXED_INDIRECT"),
55            format(def, def.indirect_indexed, NO_INDIRECT_INDEXED, "INDIRECT_INDEXED"),
56        );
57        result.push(line);
58    }
59
60    let mut lines = Vec::<String>::default();
61    for def in &isa_6502() {
62        format_opcodes(&mut lines, def);
63    }
64
65    println!("{}", lines.join("\n"));
66}
examples/codegen_table.rs (line 12)
9fn main() {
10    println!("//! | **Instruction** | **Implied** | **Immediate** | **Accumulator** | **Absolute** | **Absolute,X** | **Absolute,Y** | **Zero Page** | **Zero Page,X** | **Zero Page,Y** | **Relative** | **Indirect** | **Indirect,X** | **Indirect,Y** |");
11    println!("//! | --------------- | ----------- | ------------- | --------------- | ------------ | -------------- | -------------- | ------------- | --------------- | --------------- | ------------ | ------------ | -------------- | -------------- |");
12    for instruction in isa_6502() {
13        println!("//! | {:15} | {:11} | {:13} | {:15} | {:12} | {:14} | {:14} | {:13} | {:15} | {:15} | {:12} | {:12} | {:14} | {:14} |", instruction.instruction.to_uppercase(),
14            if instruction.implied == NO_IMPLIED{"".to_string()}else {format!("0x{:02X}", instruction.implied)},
15            if instruction.immediate == NO_IMMEDIATE{"".to_string()}else {format!("0x{:02X}", instruction.immediate)},
16            if instruction.accumulator == NO_ACCUMULATOR{"".to_string()}else {format!("0x{:02X}", instruction.accumulator)},
17            if instruction.absolute == NO_ABSOLUTE{"".to_string()}else {format!("0x{:02X}", instruction.absolute)},
18            if instruction.absolute_x == NO_ABSOLUTE_X{"".to_string()}else {format!("0x{:02X}", instruction.absolute_x)},
19            if instruction.absolute_y == NO_ABSOLUTE_Y{"".to_string()}else {format!("0x{:02X}", instruction.absolute_y)},
20            if instruction.zeropage == NO_ZEROPAGE{"".to_string()}else {format!("0x{:02X}", instruction.zeropage)},
21            if instruction.zeropage_x == NO_ZEROPAGE_X{"".to_string()}else {format!("0x{:02X}", instruction.zeropage_x)},
22            if instruction.zeropage_y == NO_ZEROPAGE_Y{"".to_string()}else {format!("0x{:02X}", instruction.zeropage_y)},
23            if instruction.relative == NO_RELATIVE{"".to_string()}else {format!("0x{:02X}", instruction.relative)},
24            if instruction.indirect == NO_INDIRECT{"".to_string()}else {format!("0x{:02X}", instruction.indirect)},
25            if instruction.indexed_indirect == NO_INDEXED_INDIRECT{"".to_string()}else {format!("0x{:02X}", instruction.indexed_indirect)},
26            if instruction.indirect_indexed == NO_INDIRECT_INDEXED{"".to_string()}else {format!("0x{:02X}", instruction.indirect_indexed)},
27        );
28    }
29}
examples/codegen_instruction_test.rs (line 12)
9fn main() {
10    let mut lines = Vec::<String>::default();
11
12    for def in isa_6502() {
13        if def.implied != NO_IMPLIED {
14            lines.push(format!(
15                "
16                #[test]
17                fn {0}() {{
18                    test_first(instructions!({0}), Operation::{1}, AddressMode::Implied);
19                }}
20                ",
21                def.instruction.to_string(),
22                def.instruction.to_uppercase()
23            ));
24            continue;
25        }
26        lines.push(format!(
27            "
28            mod {0} {{
29                use c64_assembler::{{
30                    instruction::operation::Operation,
31                    memory::{{address_mode::*, label::AddressReference}},
32        }};
33                use c64_assembler_macro::instructions;
34
35                use crate::test_first;
36                const OP: Operation = Operation::{1};
37            ",
38            def.instruction,
39            def.instruction.to_string().to_uppercase()
40        ));
41
42        if def.immediate != NO_IMMEDIATE {
43            lines.push(format!(
44                "
45                #[test]
46                fn imm() {{
47                    test_first(
48                        instructions!({0} #99),
49                        OP,
50                        AddressMode::Immediate(Immediate::Byte(99)),
51                    );
52                    test_first(
53                        instructions!({0} #$99),
54                        OP,
55                        AddressMode::Immediate(Immediate::Byte(153)),
56                    )
57                }}
58
59                #[test]
60                fn imm_low() {{
61                    test_first(
62                        instructions!({0} #<test),
63                        OP,
64                        AddressMode::Immediate(Immediate::Low(AddressReference::new(\"test\"))),
65                    );
66                }}
67
68                #[test]
69                fn imm_high() {{
70                    test_first(
71                        instructions!({0} #>test),
72                        OP,
73                        AddressMode::Immediate(Immediate::High(AddressReference::new(\"test\"))),
74                    );
75                }}
76                ",
77                def.instruction.to_string()
78            ));
79        }
80
81        if def.accumulator != NO_ACCUMULATOR {
82            lines.push(format!(
83                "
84                #[test]
85                fn acc() {{
86                    test_first(
87                        instructions!({0} a),
88                        OP,
89                        AddressMode::Accumulator,
90                    );
91                }}
92                ",
93                def.instruction
94            ));
95        }
96
97        if def.absolute != NO_ABSOLUTE || def.zeropage != NO_ZEROPAGE {
98            lines.push(format!(
99                "
100                #[test]
101                fn addr() {{
102                    test_first(
103                        instructions!({0} test),
104                        OP,
105                        AddressMode::Absolute(AddressReference::new(\"test\")),
106                    );
107                }}
108
109                #[test]
110                fn addr_offs() {{
111                    test_first(
112                        instructions!({0} test+1),
113                        OP,
114                        AddressMode::Absolute(AddressReference::with_offset(\"test\", 1)),
115                    );
116                }}
117                ",
118                def.instruction.to_string()
119            ));
120        }
121        if def.relative != NO_RELATIVE {
122            lines.push(format!(
123                "
124                #[test]
125                fn addr() {{
126                    test_first(
127                        instructions!({0} test),
128                        OP,
129                        AddressMode::Relative(AddressReference::new(\"test\")),
130                    );
131                }}
132
133                #[test]
134                fn addr_offs() {{
135                    test_first(
136                        instructions!({0} test+1),
137                        OP,
138                        AddressMode::Relative(AddressReference::with_offset(\"test\", 1)),
139                    );
140                }}
141                ",
142                def.instruction.to_string()
143            ));
144        }
145
146        if def.absolute_x != NO_ABSOLUTE_X || def.zeropage_x != NO_ZEROPAGE_X {
147            lines.push(format!(
148                "
149                #[test]
150                fn addr_x() {{
151                    test_first(
152                        instructions!({0} test,x),
153                        OP,
154                        AddressMode::AbsoluteX(AddressReference::new(\"test\")),
155                    );
156                }}
157                ",
158                def.instruction
159            ));
160        }
161        if def.absolute_y != NO_ABSOLUTE_Y || def.zeropage_y != NO_ZEROPAGE_Y {
162            lines.push(format!(
163                "
164                #[test]
165                fn addr_y() {{
166                    test_first(
167                        instructions!({0} test,y),
168                        OP,
169                        AddressMode::AbsoluteY(AddressReference::new(\"test\")),
170                    );
171                }}
172                ",
173                def.instruction
174            ));
175        }
176
177        if def.indirect != NO_INDIRECT {
178            lines.push(format!(
179                "
180                #[test]
181                fn ind() {{
182                    test_first(
183                        instructions!({0} (test)),
184                        OP,
185                        AddressMode::Indirect(AddressReference::new(\"test\")),
186                        );
187                        }}
188                        ",
189                def.instruction.to_string()
190            ));
191        }
192        if def.indexed_indirect != NO_INDIRECT {
193            lines.push(format!(
194                "
195                #[test]
196                fn ind_x() {{
197                    test_first(
198                        instructions!({0} (test,x)),
199                        OP,
200                        AddressMode::IndexedIndirect(AddressReference::new(\"test\")),
201                    );
202                }}
203                ",
204                def.instruction.to_string()
205            ));
206        }
207        if def.indirect_indexed != NO_INDIRECT {
208            lines.push(format!(
209                "
210                #[test]
211                fn ind_y() {{
212                    test_first(
213                        instructions!({0} (test),y),
214                        OP,
215                        AddressMode::IndirectIndexed(AddressReference::new(\"test\")),
216                    );
217                }}
218                ",
219                def.instruction.to_string()
220            ));
221        }
222
223        // Close module
224        lines.push(
225            "
226            }
227            "
228            .to_string(),
229        );
230    }
231
232    print!("{}", lines.join("\n"));
233}
examples/codegen_instruction_builder.rs (line 12)
9fn main() {
10    let mut lines = Vec::<String>::default();
11
12    for def in isa_6502() {
13        if def.implied != NO_IMPLIED {
14            lines.push(format!(
15                "
16                /// Record a new {0} instruction (addressing mode is implied).
17                /// 
18                /// # Example
19                /// ```
20                /// use c64_assembler::builder::InstructionBuilder;
21                /// let instructions = InstructionBuilder::default()
22                ///     .{0}()
23                ///     .build();
24                /// ```
25                pub fn {0}(&mut self) -> &mut Self {{
26                    self.add_instruction(Operation::{1}, AddressMode::Implied);
27                    self
28                }}",
29                def.instruction.to_string(),
30                def.instruction.to_uppercase()
31            ));
32        } else {
33            lines.push(format!(
34                "
35                /// Record a new {0} instruction with the given addressing mode.
36                fn {0}(&mut self, addressing_mode: AddressMode) -> &mut Self {{
37                    self.add_instruction(Operation::{1}, addressing_mode);
38                    self
39                }}
40                ",
41                def.instruction.to_string(),
42                def.instruction.to_uppercase()
43            ));
44        }
45        if def.immediate != NO_IMMEDIATE {
46            lines.push(format!(
47                "
48                /// Record a {0} instruction with data (byte).
49                /// # Example
50                /// ```
51                /// use c64_assembler::builder::InstructionBuilder;
52                /// let instructions = InstructionBuilder::default()
53                ///     .{0}_imm(0xC0)
54                ///     .build();
55                /// ```
56                pub fn {0}_imm(&mut self, byte: u8) -> &mut Self {{
57                    self.{0}(AddressMode::Immediate(Immediate::Byte(byte)))
58                }}
59
60                /// Record a {0} instruction with lower byte of an address.
61                /// 
62                /// # Example
63                /// ```
64                /// use c64_assembler::builder::InstructionBuilder;
65                /// let instructions = InstructionBuilder::default()
66                ///     .{0}_imm_low(\"test_data\")
67                ///     .label(\"test_data\")
68                ///     .build();
69                /// ```
70                pub fn {0}_imm_low(&mut self, address_name: &str) -> &mut Self {{
71                    self.{0}(AddressMode::Immediate(Immediate::Low(
72                        AddressReference::new(address_name)
73                    )))
74                }}
75
76                /// Record a {0} instruction with higher byte of an address.
77                /// 
78                /// # Example
79                /// ```
80                /// use c64_assembler::builder::InstructionBuilder;
81                /// let instructions = InstructionBuilder::default()
82                ///     .{0}_imm_high(\"test_data\")
83                ///     .label(\"test_data\")
84                ///     .build();
85                /// ```
86                pub fn {0}_imm_high(&mut self, address_name: &str) -> &mut Self {{
87                    self.{0}(AddressMode::Immediate(Immediate::High(
88                        AddressReference::new(address_name)
89                    )))
90                }}
91                ",
92                def.instruction.to_string()
93            ));
94        }
95        if def.accumulator != NO_ACCUMULATOR {
96            lines.push(format!(
97                "
98                /// Record a {0} instruction that uses accumulator as address mode.
99                /// 
100                /// # Example
101                /// ```
102                /// use c64_assembler::builder::InstructionBuilder;
103                /// let instructions = InstructionBuilder::default()
104                ///     .{0}_acc()
105                ///     .build();
106                /// ```
107                pub fn {0}_acc(&mut self) -> &mut Self {{
108                    self.{0}(AddressMode::Accumulator)
109                }}
110                ",
111                def.instruction.to_string()
112            ));
113        }
114
115        if def.absolute != NO_ABSOLUTE || def.zeropage != NO_ZEROPAGE {
116            lines.push(format!(
117                "
118                /// Record a {0} instruction that use an absolute address. 
119                /// 
120                /// # Example
121                /// ```
122                /// use c64_assembler::builder::InstructionBuilder;
123                /// let instructions = InstructionBuilder::default()
124                ///     .{0}_addr(\"test_label\")
125                ///     .label(\"test_label\")
126                ///     .build();
127                /// ```
128                pub fn {0}_addr(&mut self, address_name: &str) -> &mut Self {{
129                    self.{0}(AddressMode::Absolute(AddressReference::new(address_name)))
130                }}
131        
132                /// Record a {0} instruction that use an absolute address with an offset.
133                /// Offset is in bytes.
134                /// 
135                /// # Example
136                /// ```
137                /// use c64_assembler::builder::InstructionBuilder;
138                /// let instructions = InstructionBuilder::default()
139                ///     .{0}_addr_offs(\"test_label\", 8)
140                ///     .label(\"test_label\")
141                ///     .build();
142                /// ```
143                pub fn {0}_addr_offs(&mut self, address_name: &str, offset: Address) -> &mut Self {{
144                    self.{0}(AddressMode::Absolute(AddressReference::with_offset(
145                        address_name, offset
146                    )))
147                }}
148                ",
149                def.instruction.to_string()
150            ));
151        }
152        if def.absolute_x != NO_ABSOLUTE_X || def.zeropage_x != NO_ZEROPAGE_X {
153            lines.push(format!(
154                "
155                /// Record a {0} instructon that use an absolute address with x-register as indexer.
156                /// 
157                /// # Example
158                /// ```
159                /// use c64_assembler::builder::InstructionBuilder;
160                /// let instructions = InstructionBuilder::default()
161                ///     .ldx_imm(0x08)
162                ///     .{0}_addr_x(\"test_label\")
163                ///     .label(\"test_label\")
164                ///     .build();
165                /// ```
166                pub fn {0}_addr_x(&mut self, address_name: &str) -> &mut Self {{
167                    self.{0}(AddressMode::AbsoluteX(AddressReference::new(address_name)))
168                }}
169                ",
170                def.instruction.to_string()
171            ));
172        }
173        if def.absolute_y != NO_ABSOLUTE_Y || def.zeropage_y != NO_ZEROPAGE_Y {
174            lines.push(format!(
175                "
176                /// Record a {0} instructon that use an absolute address with y-register as indexer.
177                /// 
178                /// # Example
179                /// ```
180                /// use c64_assembler::builder::InstructionBuilder;
181                /// let instructions = InstructionBuilder::default()
182                ///     .ldy_imm(0x08)
183                ///     .{0}_addr_y(\"test_label\")
184                ///     .label(\"test_label\")
185                ///     .build();
186                /// ```
187                pub fn {0}_addr_y(&mut self, address_name: &str) -> &mut Self {{
188                    self.{0}(AddressMode::AbsoluteY(AddressReference::new(address_name)))
189                }}
190                ",
191                def.instruction.to_string()
192            ));
193        }
194        if def.relative != NO_RELATIVE {
195            lines.push(format!(
196                "
197                /// Record a {0} instruction that use  relativeeeeeeeee address. 
198                /// 
199                /// # Example
200                /// ```
201                /// use c64_assembler::builder::InstructionBuilder;
202                /// let instructions = InstructionBuilder::default()
203                ///     .{0}_addr(\"test_label\")
204                ///     .label(\"test_label\")
205                ///     .build();
206                /// ```
207                pub fn {0}_addr(&mut self, address_name: &str) -> &mut Self {{
208                    self.{0}(AddressMode::Relative(AddressReference::new(address_name)))
209                }}
210        
211                /// Record a {0} instruction that use a relative address with an offset.
212                /// Offset is in bytes.
213                /// 
214                /// # Example
215                /// ```
216                /// use c64_assembler::builder::InstructionBuilder;
217                /// let instructions = InstructionBuilder::default()
218                ///     .{0}_addr_offs(\"test_label\", 8)
219                ///     .label(\"test_label\")
220                ///     .build();
221                /// ```
222                pub fn {0}_addr_offs(&mut self, address_name: &str, offset: Address) -> &mut Self {{
223                    self.{0}(AddressMode::Relative(AddressReference::with_offset(
224                        address_name, offset
225                    )))
226                }}
227                ",
228                def.instruction.to_string()
229            ));
230        }
231        if def.indirect != NO_INDIRECT {
232            lines.push(format!(
233                "
234                /// Record a {0} instruction that uses indirect addressing mode.
235                /// 
236                /// # Example
237                /// ```
238                /// use c64_assembler::builder::InstructionBuilder;
239                /// let instructions = InstructionBuilder::default()
240                ///     .{0}_ind(\"test_label\")
241                ///     .label(\"test_label\")
242                ///     .build();
243                /// ```
244                pub fn {0}_ind(&mut self, address_name: &str) -> &mut Self {{
245                    self.{0}(AddressMode::Indirect(AddressReference::new(address_name)))
246                }}
247                ",
248                def.instruction.to_string()
249            ));
250        }
251        if def.indexed_indirect != NO_INDEXED_INDIRECT {
252            lines.push(format!(
253                "
254                /// Record a {0} instruction that uses indexed indirect addressing mode.
255                /// 
256                /// # Example
257                /// ```
258                /// use c64_assembler::builder::InstructionBuilder;
259                /// let instructions = InstructionBuilder::default()
260                ///     .ldx_imm(0x08)
261                ///     .{0}_ind_x(\"test_label\")
262                ///     .label(\"test_label\")
263                ///     .build();
264                /// ```
265                pub fn {0}_ind_x(&mut self, address_name: &str) -> &mut Self {{
266                    self.{0}(AddressMode::IndexedIndirect(AddressReference::new(address_name)))
267                }}
268                ",
269                def.instruction.to_string()
270            ));
271        }
272        if def.indirect_indexed != NO_INDIRECT_INDEXED {
273            lines.push(format!(
274                "
275                /// Record a {0} instruction that uses indirect indexed addressing mode.
276                /// 
277                /// # Example
278                /// ```
279                /// use c64_assembler::builder::InstructionBuilder;
280                /// let instructions = InstructionBuilder::default()
281                ///     .ldy_imm(0x08)
282                ///     .{0}_ind_y(\"test_label\")
283                ///     .label(\"test_label\")
284                ///     .build();
285                /// ```
286                pub fn {0}_ind_y(&mut self, address_name: &str) -> &mut Self {{
287                    self.{0}(AddressMode::IndirectIndexed(AddressReference::new(address_name)))
288                }}
289                ",
290                def.instruction.to_string()
291            ));
292        }
293    }
294
295    print!("{}", lines.join("\n"));
296}