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
// Copyright © 2022
// Author: Antonio Caggiano <info@antoniocaggiano.eu>
// SPDX-License-Identifier: MIT

use crate::{asm::instruction::AsmInstruction, Signal15, Signal16, Unit};

/// Instruction memory, preloaded with the required program.
pub struct Rom32k {
    address: Signal15,
    out: Signal16,

    instructions: Vec<AsmInstruction>,
}

impl Rom32k {
    /// Carries out the simulation taking as input an address and
    /// returns the corresponding instruction
    pub fn sim(&mut self, address: Signal15) -> Signal16 {
        self.address = address;
        self.eval();
        self.out
    }

    pub fn out(&self) -> Signal16 {
        self.out
    }

    pub fn set_address(&mut self, address: impl Into<Signal15>) {
        self.address = address.into();
    }

    pub fn set_instructions(&mut self, mut instructions: Vec<AsmInstruction>) {
        instructions.resize(self.instructions.len(), AsmInstruction::default());
        self.instructions.clone_from_slice(&instructions);
    }

    pub fn get_index(&self) -> usize {
        let index: usize = self.address.into();
        assert!(index < (1 << 15));
        index
    }

    pub fn get_instruction(&self) -> &AsmInstruction {
        &self.instructions[self.get_index()]
    }
}

impl Default for Rom32k {
    fn default() -> Self {
        Self {
            address: Default::default(),
            out: Default::default(),
            instructions: vec![AsmInstruction::default(); 1 << 15],
        }
    }
}

impl Unit for Rom32k {
    fn eval(&mut self) {
        self.out = Signal16::from(self.get_instruction());
    }
}