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)
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
fn main() {
fn format_opcode(result: &mut Vec<String>, instruction: &str, opcode: OpCode, post: &str) {
if opcode != NO_IMPLIED {
result.push(format!(
"/// OpCode for the {} instruction in addressing mode {}
pub const {}_{}:OpCode = 0x{:02x};",
instruction,
post.to_lowercase(),
instruction.to_string().to_uppercase(),
post,
opcode
));
}
}
let mut lines = Vec::<String>::default();
for def in isa_6502() {
format_opcode(&mut lines, def.instruction, def.implied, "IMPLIED");
format_opcode(&mut lines, def.instruction, def.immediate, "IMMEDIATE");
format_opcode(&mut lines, def.instruction, def.accumulator, "ACCUMULATOR");
format_opcode(&mut lines, def.instruction, def.absolute, "ABSOLUTE");
format_opcode(&mut lines, def.instruction, def.absolute_x, "ABSOLUTE_X");
format_opcode(&mut lines, def.instruction, def.absolute_y, "ABSOLUTE_Y");
format_opcode(&mut lines, def.instruction, def.zeropage, "ZEROPAGE");
format_opcode(&mut lines, def.instruction, def.zeropage_x, "ZEROPAGE_X");
format_opcode(&mut lines, def.instruction, def.zeropage_y, "ZEROPAGE_Y");
format_opcode(&mut lines, def.instruction, def.relative, "RELATIVE");
format_opcode(&mut lines, def.instruction, def.indirect, "INDIRECT");
format_opcode(&mut lines, def.instruction, def.indexed_indirect, "INDEXED_INDIRECT");
format_opcode(&mut lines, def.instruction, def.indirect_indexed, "INDIRECT_INDEXED");
}
println!("{}", lines.join("\n"));
}More examples
examples/codegen_instruction_definitions.rs (line 61)
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
fn main() {
fn format(def: &InstructionDef, op_code: OpCode, no_opcode: OpCode, opcode_str: &str) -> String {
if op_code == no_opcode {
format!("NO_{}", opcode_str)
} else {
format!("{}_{}", def.instruction.to_uppercase(), opcode_str)
}
}
fn format_opcodes(result: &mut Vec<String>, def: &InstructionDef) {
let line = format!(
"
/// Instruction definition for {1}
///
/// Includes the instruction name and its [OpCode] for a address mode.
pub const OPCODES_{0}:InstructionDef = InstructionDef {{
instruction: \"{1}\",
implied: {2},
immediate: {3},
accumulator: {4},
absolute: {5},
absolute_x: {6},
absolute_y: {7},
zeropage: {8},
zeropage_x: {9},
zeropage_y: {10},
relative: {11},
indirect: {12},
indexed_indirect: {13},
indirect_indexed: {14},
}};",
def.instruction.to_uppercase(),
def.instruction.to_string(),
format(def, def.implied, NO_IMPLIED, "IMPLIED"),
format(def, def.immediate, NO_IMMEDIATE, "IMMEDIATE"),
format(def, def.accumulator, NO_ACCUMULATOR, "ACCUMULATOR"),
format(def, def.absolute, NO_ABSOLUTE, "ABSOLUTE"),
format(def, def.absolute_x, NO_ABSOLUTE_X, "ABSOLUTE_X"),
format(def, def.absolute_y, NO_ABSOLUTE_Y, "ABSOLUTE_Y"),
format(def, def.zeropage, NO_ZEROPAGE, "ZEROPAGE"),
format(def, def.zeropage_x, NO_ZEROPAGE_X, "ZEROPAGE_X"),
format(def, def.zeropage_y, NO_ZEROPAGE_Y, "ZEROPAGE_Y"),
format(def, def.relative, NO_RELATIVE, "RELATIVE"),
format(def, def.indirect, NO_INDIRECT, "INDIRECT"),
format(def, def.indexed_indirect, NO_INDEXED_INDIRECT, "INDEXED_INDIRECT"),
format(def, def.indirect_indexed, NO_INDIRECT_INDEXED, "INDIRECT_INDEXED"),
);
result.push(line);
}
let mut lines = Vec::<String>::default();
for def in &isa_6502() {
format_opcodes(&mut lines, def);
}
println!("{}", lines.join("\n"));
}examples/codegen_table.rs (line 12)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
fn main() {
println!("//! | **Instruction** | **Implied** | **Immediate** | **Accumulator** | **Absolute** | **Absolute,X** | **Absolute,Y** | **Zero Page** | **Zero Page,X** | **Zero Page,Y** | **Relative** | **Indirect** | **Indirect,X** | **Indirect,Y** |");
println!("//! | --------------- | ----------- | ------------- | --------------- | ------------ | -------------- | -------------- | ------------- | --------------- | --------------- | ------------ | ------------ | -------------- | -------------- |");
for instruction in isa_6502() {
println!("//! | {:15} | {:11} | {:13} | {:15} | {:12} | {:14} | {:14} | {:13} | {:15} | {:15} | {:12} | {:12} | {:14} | {:14} |", instruction.instruction.to_uppercase(),
if instruction.implied == NO_IMPLIED{"".to_string()}else {format!("0x{:02X}", instruction.implied)},
if instruction.immediate == NO_IMMEDIATE{"".to_string()}else {format!("0x{:02X}", instruction.immediate)},
if instruction.accumulator == NO_ACCUMULATOR{"".to_string()}else {format!("0x{:02X}", instruction.accumulator)},
if instruction.absolute == NO_ABSOLUTE{"".to_string()}else {format!("0x{:02X}", instruction.absolute)},
if instruction.absolute_x == NO_ABSOLUTE_X{"".to_string()}else {format!("0x{:02X}", instruction.absolute_x)},
if instruction.absolute_y == NO_ABSOLUTE_Y{"".to_string()}else {format!("0x{:02X}", instruction.absolute_y)},
if instruction.zeropage == NO_ZEROPAGE{"".to_string()}else {format!("0x{:02X}", instruction.zeropage)},
if instruction.zeropage_x == NO_ZEROPAGE_X{"".to_string()}else {format!("0x{:02X}", instruction.zeropage_x)},
if instruction.zeropage_y == NO_ZEROPAGE_Y{"".to_string()}else {format!("0x{:02X}", instruction.zeropage_y)},
if instruction.relative == NO_RELATIVE{"".to_string()}else {format!("0x{:02X}", instruction.relative)},
if instruction.indirect == NO_INDIRECT{"".to_string()}else {format!("0x{:02X}", instruction.indirect)},
if instruction.indexed_indirect == NO_INDEXED_INDIRECT{"".to_string()}else {format!("0x{:02X}", instruction.indexed_indirect)},
if instruction.indirect_indexed == NO_INDIRECT_INDEXED{"".to_string()}else {format!("0x{:02X}", instruction.indirect_indexed)},
);
}
}examples/codegen_instruction_test.rs (line 12)
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
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"));
}examples/codegen_instruction_builder.rs (line 12)
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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
fn main() {
let mut lines = Vec::<String>::default();
for def in isa_6502() {
if def.implied != NO_IMPLIED {
lines.push(format!(
"
/// Record a new {0} instruction (addressing mode is implied).
///
/// # Example
/// ```
/// use c64_assembler::builder::InstructionBuilder;
/// let instructions = InstructionBuilder::default()
/// .{0}()
/// .build();
/// ```
pub fn {0}(&mut self) -> &mut Self {{
self.add_instruction(Operation::{1}, AddressMode::Implied);
self
}}",
def.instruction.to_string(),
def.instruction.to_uppercase()
));
} else {
lines.push(format!(
"
/// Record a new {0} instruction with the given addressing mode.
fn {0}(&mut self, addressing_mode: AddressMode) -> &mut Self {{
self.add_instruction(Operation::{1}, addressing_mode);
self
}}
",
def.instruction.to_string(),
def.instruction.to_uppercase()
));
}
if def.immediate != NO_IMMEDIATE {
lines.push(format!(
"
/// Record a {0} instruction with data (byte).
/// # Example
/// ```
/// use c64_assembler::builder::InstructionBuilder;
/// let instructions = InstructionBuilder::default()
/// .{0}_imm(0xC0)
/// .build();
/// ```
pub fn {0}_imm(&mut self, byte: u8) -> &mut Self {{
self.{0}(AddressMode::Immediate(Immediate::Byte(byte)))
}}
/// Record a {0} instruction with lower byte of an address.
///
/// # Example
/// ```
/// use c64_assembler::builder::InstructionBuilder;
/// let instructions = InstructionBuilder::default()
/// .{0}_imm_low(\"test_data\")
/// .label(\"test_data\")
/// .build();
/// ```
pub fn {0}_imm_low(&mut self, address_name: &str) -> &mut Self {{
self.{0}(AddressMode::Immediate(Immediate::Low(
AddressReference::new(address_name)
)))
}}
/// Record a {0} instruction with higher byte of an address.
///
/// # Example
/// ```
/// use c64_assembler::builder::InstructionBuilder;
/// let instructions = InstructionBuilder::default()
/// .{0}_imm_high(\"test_data\")
/// .label(\"test_data\")
/// .build();
/// ```
pub fn {0}_imm_high(&mut self, address_name: &str) -> &mut Self {{
self.{0}(AddressMode::Immediate(Immediate::High(
AddressReference::new(address_name)
)))
}}
",
def.instruction.to_string()
));
}
if def.accumulator != NO_ACCUMULATOR {
lines.push(format!(
"
/// Record a {0} instruction that uses accumulator as address mode.
///
/// # Example
/// ```
/// use c64_assembler::builder::InstructionBuilder;
/// let instructions = InstructionBuilder::default()
/// .{0}_acc()
/// .build();
/// ```
pub fn {0}_acc(&mut self) -> &mut Self {{
self.{0}(AddressMode::Accumulator)
}}
",
def.instruction.to_string()
));
}
if def.absolute != NO_ABSOLUTE || def.zeropage != NO_ZEROPAGE {
lines.push(format!(
"
/// Record a {0} instruction that use an absolute address.
///
/// # Example
/// ```
/// use c64_assembler::builder::InstructionBuilder;
/// let instructions = InstructionBuilder::default()
/// .{0}_addr(\"test_label\")
/// .label(\"test_label\")
/// .build();
/// ```
pub fn {0}_addr(&mut self, address_name: &str) -> &mut Self {{
self.{0}(AddressMode::Absolute(AddressReference::new(address_name)))
}}
/// Record a {0} instruction that use an absolute address with an offset.
/// Offset is in bytes.
///
/// # Example
/// ```
/// use c64_assembler::builder::InstructionBuilder;
/// let instructions = InstructionBuilder::default()
/// .{0}_addr_offs(\"test_label\", 8)
/// .label(\"test_label\")
/// .build();
/// ```
pub fn {0}_addr_offs(&mut self, address_name: &str, offset: Address) -> &mut Self {{
self.{0}(AddressMode::Absolute(AddressReference::with_offset(
address_name, offset
)))
}}
",
def.instruction.to_string()
));
}
if def.absolute_x != NO_ABSOLUTE_X || def.zeropage_x != NO_ZEROPAGE_X {
lines.push(format!(
"
/// Record a {0} instructon that use an absolute address with x-register as indexer.
///
/// # Example
/// ```
/// use c64_assembler::builder::InstructionBuilder;
/// let instructions = InstructionBuilder::default()
/// .lda_imm(0x08)
/// .{0}_addr_x(\"test_label\")
/// .label(\"test_label\")
/// .build();
/// ```
pub fn {0}_addr_x(&mut self, address_name: &str) -> &mut Self {{
self.{0}(AddressMode::AbsoluteX(AddressReference::new(address_name)))
}}
",
def.instruction.to_string()
));
}
if def.absolute_y != NO_ABSOLUTE_Y || def.zeropage_y != NO_ZEROPAGE_Y {
lines.push(format!(
"
/// Record a {0} instructon that use an absolute address with y-register as indexer.
///
/// # Example
/// ```
/// use c64_assembler::builder::InstructionBuilder;
/// let instructions = InstructionBuilder::default()
/// .lda_imm(0x08)
/// .{0}_addr_y(\"test_label\")
/// .label(\"test_label\")
/// .build();
/// ```
pub fn {0}_addr_y(&mut self, address_name: &str) -> &mut Self {{
self.{0}(AddressMode::AbsoluteY(AddressReference::new(address_name)))
}}
",
def.instruction.to_string()
));
}
if def.relative != NO_RELATIVE {
lines.push(format!(
"
/// Record a {0} instruction that use relativeeeeeeeee address.
///
/// # Example
/// ```
/// use c64_assembler::builder::InstructionBuilder;
/// let instructions = InstructionBuilder::default()
/// .{0}_addr(\"test_label\")
/// .label(\"test_label\")
/// .build();
/// ```
pub fn {0}_addr(&mut self, address_name: &str) -> &mut Self {{
self.{0}(AddressMode::Relative(AddressReference::new(address_name)))
}}
/// Record a {0} instruction that use a relative address with an offset.
/// Offset is in bytes.
///
/// # Example
/// ```
/// use c64_assembler::builder::InstructionBuilder;
/// let instructions = InstructionBuilder::default()
/// .{0}_addr_offs(\"test_label\", 8)
/// .label(\"test_label\")
/// .build();
/// ```
pub fn {0}_addr_offs(&mut self, address_name: &str, offset: Address) -> &mut Self {{
self.{0}(AddressMode::Relative(AddressReference::with_offset(
address_name, offset
)))
}}
",
def.instruction.to_string()
));
}
if def.indirect != NO_INDIRECT {
lines.push(format!(
"
pub fn {0}_ind(&mut self, address_name: &str) -> &mut Self {{
self.{0}(AddressMode::Indirect(AddressReference::new(address_name)))
}}
",
def.instruction.to_string()
));
}
if def.indexed_indirect != NO_INDEXED_INDIRECT {
lines.push(format!(
"
pub fn {0}_ind_x(&mut self, address_name: &str) -> &mut Self {{
self.{0}(AddressMode::IndexedIndirect(AddressReference::new(address_name)))
}}
",
def.instruction.to_string()
));
}
if def.indirect_indexed != NO_INDIRECT_INDEXED {
lines.push(format!(
"
pub fn {0}_ind_y(&mut self, address_name: &str) -> &mut Self {{
self.{0}(AddressMode::IndirectIndexed(AddressReference::new(address_name)))
}}
",
def.instruction.to_string()
));
}
}
print!("{}", lines.join("\n"));
}