2025-01-13 09:49:08 -05:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
|
|
|
|
2024-12-17 18:11:06 -05:00
|
|
|
use crate::{Instruction, Operation};
|
2024-11-26 07:14:30 -05:00
|
|
|
|
2025-02-06 12:42:55 -05:00
|
|
|
use super::InstructionFields;
|
2025-01-13 06:01:38 -05:00
|
|
|
|
2024-11-26 07:14:30 -05:00
|
|
|
pub struct Test {
|
2025-01-13 06:01:38 -05:00
|
|
|
pub operand_register: u16,
|
2024-11-28 05:36:10 -05:00
|
|
|
pub test_value: bool,
|
2024-11-26 07:14:30 -05:00
|
|
|
}
|
|
|
|
|
2025-01-08 04:21:01 -05:00
|
|
|
impl From<Instruction> for Test {
|
|
|
|
fn from(instruction: Instruction) -> Self {
|
2024-12-17 18:11:06 -05:00
|
|
|
let operand_register = instruction.b_field();
|
2024-12-11 06:49:43 -05:00
|
|
|
let test_value = instruction.c_field() != 0;
|
2024-12-08 08:01:15 -05:00
|
|
|
|
2024-11-26 07:14:30 -05:00
|
|
|
Test {
|
2024-12-17 18:11:06 -05:00
|
|
|
operand_register,
|
2024-12-08 08:01:15 -05:00
|
|
|
test_value,
|
2024-11-26 07:14:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Test> for Instruction {
|
|
|
|
fn from(test: Test) -> Self {
|
2025-01-13 06:01:38 -05:00
|
|
|
let b_field = test.operand_register;
|
|
|
|
let c_field = test.test_value as u16;
|
|
|
|
|
2025-02-06 12:42:55 -05:00
|
|
|
InstructionFields {
|
2025-01-13 06:01:38 -05:00
|
|
|
operation: Operation::TEST,
|
|
|
|
b_field,
|
|
|
|
c_field,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.build()
|
2024-11-26 07:14:30 -05:00
|
|
|
}
|
|
|
|
}
|
2025-01-13 09:49:08 -05:00
|
|
|
|
|
|
|
impl Display for Test {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
let Test {
|
|
|
|
operand_register,
|
|
|
|
test_value,
|
|
|
|
} = self;
|
|
|
|
let bang = if *test_value { "" } else { "!" };
|
|
|
|
|
2025-02-08 17:36:30 -05:00
|
|
|
write!(f, "if {bang}R_BOOL_{operand_register} {{ JUMP +1 }}")
|
2025-01-13 09:49:08 -05:00
|
|
|
}
|
|
|
|
}
|