aboutsummaryrefslogtreecommitdiff
path: root/src/memory_tracker.rs
blob: 26c986a8b77e311f19ced6e08234890adc4ecac6 (plain)
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
use rustv::isa::{self, IsaType};
use rustv::memory::{Memory, MemoryInterface, Result};

pub struct MemoryTracker {
    memory: Memory,
    program1_byte: isa::Byte,
    program2_byte: isa::Byte,
    program1: i32,
    program2: i32,
}

impl MemoryTracker {
    pub fn new(memory: Memory,
               program1: isa::Byte, program2: isa::Byte) -> MemoryTracker {
        MemoryTracker {
            memory: memory,
            program1_byte: program1,
            program2_byte: program2,
            program1: 0,
            program2: 0,
        }
    }

    pub fn score(&self) -> (i32, i32) {
        (self.program1, self.program2)
    }
}

impl MemoryInterface for MemoryTracker {
    fn latency(&self) -> u32 {
        self.memory.latency()
    }

    fn step(&mut self) {
        self.memory.step();
    }

    fn is_address_accessible(&self, address: isa::Address) -> bool {
        self.memory.is_address_accessible(address)
    }

    fn read_word(&mut self, address: isa::Address) -> Result<isa::Word> {
        self.memory.read_word(address)
    }

    fn write_word(&mut self, address: isa::Address, value: isa::Word) -> Result<()> {
        let original = self.memory.read_word(address);
        let result = self.memory.write_word(address, value);

        if let Ok(original) = original {
            if let Ok(()) = result {
                let p1b = self.program1_byte.as_word();
                let mut p1orig = 0;
                let mut p1new = 0;
                if original & 0xFF == p1b { p1orig += 1; }
                if (original >> 8) & 0xFF == p1b { p1orig += 1; }
                if (original >> 16) & 0xFF == p1b { p1orig += 1; }
                if (original >> 24) & 0xFF == p1b { p1orig += 1; }

                if value & 0xFF == p1b { p1new += 1; }
                if (value >> 8) & 0xFF == p1b { p1new += 1; }
                if (value >> 16) & 0xFF == p1b { p1new += 1; }
                if (value >> 24) & 0xFF == p1b { p1new += 1; }

                self.program1 += p1new - p1orig;

                let p2b = self.program2_byte.as_word();
                let mut p2orig = 0;
                let mut p2new = 0;
                if original & 0xFF == p2b { p2orig += 1; }
                if (original >> 8) & 0xFF == p2b { p2orig += 1; }
                if (original >> 16) & 0xFF == p2b { p2orig += 1; }
                if (original >> 24) & 0xFF == p2b { p2orig += 1; }

                if value & 0xFF == p2b { p2new += 1; }
                if (value >> 8) & 0xFF == p2b { p2new += 1; }
                if (value >> 16) & 0xFF == p2b { p2new += 1; }
                if (value >> 24) & 0xFF == p2b { p2new += 1; }

                self.program2 += p2new - p2orig;
            }
        }

        result
    }
}