codegen_instruction_test/
codegen_instruction_test.rs

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
use c64_assembler_6502::{
    isa_6502,
    opcodes::{
        NO_ABSOLUTE, NO_ABSOLUTE_X, NO_ABSOLUTE_Y, NO_ACCUMULATOR, NO_IMMEDIATE, NO_IMPLIED, NO_RELATIVE, NO_ZEROPAGE,
        NO_ZEROPAGE_X, NO_ZEROPAGE_Y,
    },
};

fn main() {
    let mut lines = Vec::<String>::default();

    for def in isa_6502() {
        if def.implied != NO_IMPLIED {
            lines.push(format!(
                "
                #[test]
                fn {0}() {{
                    test_first(instructions!({0}), Operation::{1}, AddressMode::Implied);
                }}
                ",
                def.instruction.to_string(),
                def.instruction.to_uppercase()
            ));
            continue;
        }
        lines.push(format!(
            "
            mod {0} {{
                use c64_assembler::{{
                    instruction::operation::Operation,
                    memory::{{address_mode::*, label::AddressReference}},
        }};
                use c64_assembler_macro::instructions;

                use crate::test_first;
                const OP: Operation = Operation::{1};
            ",
            def.instruction,
            def.instruction.to_string().to_uppercase()
        ));

        if def.immediate != NO_IMMEDIATE {
            lines.push(format!(
                "
                #[test]
                fn imm() {{
                    test_first(
                        instructions!({0} #99),
                        OP,
                        AddressMode::Immediate(Immediate::Byte(99)),
                    );
                    test_first(
                        instructions!({0} #$99),
                        OP,
                        AddressMode::Immediate(Immediate::Byte(153)),
                    )
                }}

                #[test]
                fn imm_low() {{
                    test_first(
                        instructions!({0} #<test),
                        OP,
                        AddressMode::Immediate(Immediate::Low(AddressReference::new(&\"test\"))),
                    );
                }}

                #[test]
                fn imm_high() {{
                    test_first(
                        instructions!({0} #>test),
                        OP,
                        AddressMode::Immediate(Immediate::High(AddressReference::new(&\"test\"))),
                    );
                }}
                ",
                def.instruction.to_string()
            ));
        }

        if def.accumulator != NO_ACCUMULATOR {
            lines.push(format!(
                "
                #[test]
                fn acc() {{
                    test_first(
                        instructions!({0} a),
                        OP,
                        AddressMode::Accumulator,
                    );
                }}
                ",
                def.instruction
            ));
        }

        if def.absolute != NO_ABSOLUTE || def.zeropage != NO_ZEROPAGE {
            lines.push(format!(
                "
                #[test]
                fn addr() {{
                    test_first(
                        instructions!({0} test),
                        OP,
                        AddressMode::Absolute(AddressReference::new(&\"test\")),
                    );
                }}

                #[test]
                fn addr_offs() {{
                    test_first(
                        instructions!({0} test+1),
                        OP,
                        AddressMode::Absolute(AddressReference::with_offset(&\"test\", 1)),
                    );
                }}
                ",
                def.instruction.to_string()
            ));
        }
        if def.relative != NO_RELATIVE {
            lines.push(format!(
                "
                #[test]
                fn addr() {{
                    test_first(
                        instructions!({0} test),
                        OP,
                        AddressMode::Relative(AddressReference::new(&\"test\")),
                    );
                }}

                #[test]
                fn addr_offs() {{
                    test_first(
                        instructions!({0} test+1),
                        OP,
                        AddressMode::Relative(AddressReference::with_offset(&\"test\", 1)),
                    );
                }}
                ",
                def.instruction.to_string()
            ));
        }

        if def.absolute_x != NO_ABSOLUTE_X || def.zeropage_x != NO_ZEROPAGE_X {
            lines.push(format!(
                "
                #[test]
                fn addr_x() {{
                    test_first(
                        instructions!({0} test,x),
                        OP,
                        AddressMode::AbsoluteX(AddressReference::new(&\"test\")),
                    );
                }}
                ",
                def.instruction
            ));
        }
        if def.absolute_y != NO_ABSOLUTE_Y || def.zeropage_y != NO_ZEROPAGE_Y {
            lines.push(format!(
                "
                #[test]
                fn addr_y() {{
                    test_first(
                        instructions!({0} test,y),
                        OP,
                        AddressMode::AbsoluteY(AddressReference::new(&\"test\")),
                    );
                }}
                ",
                def.instruction
            ));
        }
        /*
        if def.indirect != UNUSED {
            lines.push(format!(
                "
                #[test]
                fn ind() {{
                    test_first(
                        instructions!({0} (test)),
                        OP,
                        AddressMode::Indirect(AddressReference::new(&\"test\")),
                    );
                }}
                ",
                def.instruction.to_string()
            ));
        }
        if def.indexed_indirect != UNUSED {
            lines.push(format!(
                "
                #[test]
                fn ind_x() {{
                    test_first(
                        instructions!({0} (test,x)),
                        OP,
                        AddressMode::IndexedIndirect(AddressReference::new(&\"test\")),
                    );
                }}
                ",
                def.instruction.to_string()
            ));
        }

        if def.indirect_indexed != UNUSED {
            lines.push(format!(
                "
                #[test]
                fn ind_y() {{
                    test_first(
                        instructions!({0} (test),y),
                        OP,
                        AddressMode::IndirectIndexed(AddressReference::new(&\"test\")),
                    );
                }}
                ",
                def.instruction.to_string()
            ));
        }
        */

        // Close module
        lines.push(
            "
            }
            "
            .to_string(),
        );
    }

    print!("{}", lines.join("\n"));
}