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
//! 0x10 Integral arithmetic (+, -, /, *, rnd(), <=>)
//! * `0x1001` [sum_integers]:`target` `[start:end]`
//! * `0x1002` [product_integers]:`target` `[start:end]`
//! * `0x1003` [difference_integers]:`target` `[start:end]`
//! * `0x1004` [quotient_integers]:`target` `[start:end]`
//! * TODO ... RANDOM
//! * TODO ... COMPARE

use std::collections::HashMap;

use osiris_process::operation::error::OperationResult;
use osiris_process::operation::{Operation, OperationSet};
use osiris_process::operation::scheme::{ArgumentType, InstructionScheme, OperationId};
use osiris_process::processor::Cpu;

use crate::range_applications::unsigned;

pub const SET_MASK: u16 = 0x1000;

pub const SUM_UNSIGNED: OperationId = OperationId::new(SET_MASK | 0x01);
pub const PRODUCT_UNSIGNED: OperationId = OperationId::new(SET_MASK | 0x02);
pub const DIFFERENCE_UNSIGNED: OperationId = OperationId::new(SET_MASK | 0x03);
pub const QUOTIENT_UNSIGNED: OperationId = OperationId::new(SET_MASK | 0x04);

/// # 0x1001 SUM INTEGERS
///
/// ## Target
///  - The register to store the result into
///
/// ## Arguments
///  - Range : the range to sum
///
/// ## Operation
///  - `REG[$target] = sum(REG[$start..=$end])`
///
/// ## Errors
///  - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::Range]
pub fn sum_integers(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
    cpu.bank_apply(scheme.target, scheme.argument.get_range()?, unsigned::sum);
    Ok(())
}

/// # 0x1002 PRODUCT INTEGERS
///
/// ## Target
///  - The register to store the result into
///
/// ## Arguments
///  - Range : the range to product
///
/// ## Operation
///  - `REG[$target] = product(REG[$start..=$end])`
///
/// ## Errors
///  - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::Range]
pub fn product_integers(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
    cpu.bank_apply(scheme.target, scheme.argument.get_range()?, unsigned::product);
    Ok(())
}

/// # 0x1003 DIFFERENCE INTEGERS
///
/// ## Target
///  - The register to store the result into
///
/// ## Arguments
///  - Range : the range to sum
///
/// ## Operation
///  - `REG[$target] = difference(REG[$start..=$end])`
///
/// ## Errors
///  - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::Range]
pub fn difference_integers(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
    cpu.bank_apply(scheme.target, scheme.argument.get_range()?, unsigned::difference);
    Ok(())
}

/// # 0x1004 QUOTIENT INTEGERS
///
/// ## Target
///  - The register to store the result into
///
/// ## Arguments
///  - Range : the range to product
///
/// ## Operation
///  - `REG[$target] = quotient(REG[$start..=$end])`
///
/// ## Errors
///  - [OperationError::InvalidArgumentType] if the provided argument is not an [ArgumentType::Range]
pub fn quotient_integers(cpu: &mut Cpu, scheme: InstructionScheme) -> OperationResult<()> {
    cpu.bank_apply(scheme.target, scheme.argument.get_range()?, unsigned::quotient);
    Ok(())
}

/// Returns integral arithmetic operations.
///
/// ## Operations
/// 
/// * `0x1001` [sum_integers]:`target` `[start:end]`
/// * `0x1002` [product_integers]:`target` `[start:end]`
/// * `0x1003` [difference_integers]:`target` `[start:end]`
/// * `0x1004` [quotient_integers]:`target` `[start:end]`
pub fn operation_set() -> OperationSet {
    let mut set: OperationSet = HashMap::new();
    set.insert(
        SUM_UNSIGNED,
        Operation::new(SUM_UNSIGNED, "sum-unsigned".to_string(), true, ArgumentType::Range, sum_integers),
    );
    set.insert(
        PRODUCT_UNSIGNED,
        Operation::new(PRODUCT_UNSIGNED, "product-unsigned".to_string(), true, ArgumentType::Range, product_integers),
    );
    set.insert(
        DIFFERENCE_UNSIGNED,
        Operation::new(DIFFERENCE_UNSIGNED, "difference-unsigned".to_string(), true, ArgumentType::Range, difference_integers),
    );
    set.insert(
        QUOTIENT_UNSIGNED,
        Operation::new(QUOTIENT_UNSIGNED, "quotient-unsigned".to_string(), true, ArgumentType::Range, quotient_integers),
    );
    set
}