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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
use std::collections::HashMap;
use osiris_data::data::atomic::{Word, WordTransmuter};
use osiris_process::compare::Compare;
use osiris_process::operation::{Operation, OperationSet};
use osiris_process::operation::error::{OperationError, OperationResult};
use osiris_process::operation::scheme::{ArgumentType, InstructionScheme, OperationId};
use osiris_process::processor::Cpu;
use osiris_process::register::RegisterId;
use crate::range_applications::reduce;
pub const TRUE: Word = Word::new(0xFFFFFFFFFFFFFFFF);
pub const FALSE: Word = Word::new(0x0);
pub const SET_MASK: u16 = 0x0400;
pub const IS: OperationId = OperationId::new(SET_MASK | 0x00);
pub const NOT: OperationId = OperationId::new(SET_MASK | 0x01);
pub const OR: OperationId = OperationId::new(SET_MASK | 0x02);
pub const AND: OperationId = OperationId::new(SET_MASK | 0x03);
pub const XOR: OperationId = OperationId::new(SET_MASK | 0x04);
pub const NOR: OperationId = OperationId::new(SET_MASK | 0x05);
pub const NAND: OperationId = OperationId::new(SET_MASK | 0x06);
pub const NXOR: OperationId = OperationId::new(SET_MASK | 0x07);
/// # `0x0400` [is]:`CMP` `target:16`:`compare:16`
///
/// ## Target
/// - CMP : LT(<) / LE(<=) / NE(!=) / EQ(==) / GE(>=) / GT(>)
///
/// ## Arguments
/// - target:register
/// - compare:register
///
/// ## Operation
/// - `REG[$target] = CMP == $compare`
///
/// ## Errors
/// - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::TwoU16],
pub fn is(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
let (target, compare) = scheme.argument.get_two_u16()?;
let compare = WordTransmuter::i64(cpu.bank_get(RegisterId::new(compare)));
let cmp = Compare::from(scheme.target.to_u16());
let result = cmp.unwrap().is(compare);
cpu.bank_set(RegisterId::new(target), if result { TRUE } else { FALSE });
Ok(())
}
/// # `0x0401` [not]:`target` 0:`origin:16`
///
/// ## Target
/// - The bank register to write the value of the compare register.
///
/// ## Arguments
/// - 0:`$origin`
///
/// ## Operation
/// - `REG[$target] = not REG[$origin]`
///
/// ## Errors
/// - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::TwoU16],
pub fn not(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
let (_, origin) = scheme.argument.get_two_u16()?;
let origin = RegisterId::new(origin);
cpu.bank_set(scheme.target, Word::new(!cpu.bank_get(origin).to_u64()));
Ok(())
}
/// # `0x0402` [or]:`target` `[start:end]`
///
/// ## Target
/// - The register to store the result value.
///
/// ## Arguments
/// - The register range onto apply the operator.
///
/// ## Operation
/// - `REG[$target] = 0x00... or REG[start..end]...`
///
/// ## Errors
/// - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::Range],
pub fn or(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
let range = scheme.argument.get_range()?;
cpu.bank_apply(
scheme.target,
range,
|slice| reduce(
slice,
|carry, current| Word::new(carry.to_u64() | current.to_u64()),
FALSE,
),
);
Ok(())
}
/// # `0x0403` [and]:`target` `[start:end]`
///
/// ## Target
/// - The register to store the result value.
///
/// ## Arguments
/// - The register range onto apply the operator.
///
/// ## Operation
/// - `REG[$target] = 0xFF... and REG[start..end]...`
///
/// ## Errors
/// - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::Range],
pub fn and(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
let range = scheme.argument.get_range()?;
cpu.bank_apply(
scheme.target,
range,
|slice| reduce(
slice,
|carry, current| Word::new(carry.to_u64() & current.to_u64()),
TRUE,
),
);
Ok(())
}
/// # `0x0404` [xor]:`target` `[start:end]`
///
/// ## Target
/// - The register to store the result value.
///
/// ## Arguments
/// - The register range onto apply the operator.
///
/// ## Operation
/// - `REG[$target] = 0x00... xor REG[start..end]...`
///
/// ## Errors
/// - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::Range],
pub fn xor(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
let range = scheme.argument.get_range()?;
cpu.bank_apply(
scheme.target,
range,
|slice| reduce(
slice,
|carry, current| Word::new(carry.to_u64() ^ current.to_u64()),
FALSE,
),
);
Ok(())
}
/// # `0x0405` [nor]:`target` `[start:end]`
///
/// ## Target
/// - The register to store the result value.
///
/// ## Arguments
/// - The register range onto apply the operator.
///
/// ## Operation
/// - `REG[$target] = 0x00... nor REG[start..end]...`
///
/// ## Errors
/// - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::Range],
pub fn nor(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
let range = scheme.argument.get_range()?;
cpu.bank_apply(
scheme.target,
range,
|slice| reduce(
slice,
|carry, current| Word::new(!(carry.to_u64() | current.to_u64())),
TRUE,
),
);
Ok(())
}
/// # `0x0406` [nand]:`target` `[start:end]`
///
/// ## Target
/// - The register to store the result value.
///
/// ## Arguments
/// - The register range onto apply the operator.
///
/// ## Operation
/// - `REG[$target] = 0x00... nand REG[start..end]...`
///
/// ## Errors
/// - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::Range],
pub fn nand(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
let range = scheme.argument.get_range()?;
cpu.bank_apply(
scheme.target,
range,
|slice| reduce(
slice,
|carry, current| Word::new(!(carry.to_u64() & current.to_u64())),
FALSE,
),
);
Ok(())
}
/// # `0x0407` [nxor]:`target` `[start:end]`
///
/// ## Target
/// - The register to store the result value.
///
/// ## Arguments
/// - The register range onto apply the operator.
///
/// ## Operation
/// - `REG[$target] = 0x00... nxor REG[start..end]...`
///
/// ## Errors
/// - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::Range],
pub fn nxor(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
let range = scheme.argument.get_range()?;
cpu.bank_apply(
scheme.target,
range,
|slice| reduce(
slice,
|carry, current| Word::new(!(carry.to_u64() ^ current.to_u64())),
TRUE,
),
);
Ok(())
}
/// Returns logic operations.
///
/// ## Operations
///
/// * `0x0400` [is]:`CMP` `target:16`:`compare:16`
/// * `0x0401` [not]:`target` 0:`origin:16`
/// * `0x0402` [or]:`target` `[start:end]`
/// * `0x0403` [and]:`target` `[start:end]`
/// * `0x0404` [xor]:`target` `[start:end]`
/// * `0x0405` [nor]:`target` `[start:end]`
/// * `0x0406` [nand]:`target` `[start:end]`
/// * `0x0407` [nxor]:`target` `[start:end]`
pub fn operation_set() -> OperationSet {
let mut set: OperationSet = HashMap::new();
set.insert(IS, Operation::new(IS, "is".to_string(), true, ArgumentType::TwoU16, is));
set.insert(NOT, Operation::new(NOT, "not".to_string(), true, ArgumentType::TwoU16, not));
set.insert(OR, Operation::new(OR, "or".to_string(), true, ArgumentType::Range, or));
set.insert(AND, Operation::new(AND, "and".to_string(), true, ArgumentType::Range, and));
set.insert(XOR, Operation::new(XOR, "xor".to_string(), true, ArgumentType::Range, xor));
set.insert(NOR, Operation::new(NOR, "nor".to_string(), true, ArgumentType::Range, nor));
set.insert(NAND, Operation::new(NAND, "nand".to_string(), true, ArgumentType::Range, nand));
set.insert(NXOR, Operation::new(NXOR, "nxor".to_string(), true, ArgumentType::Range, nxor));
set
}