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

pub struct MemoryTracker {
    memory: Memory,
    program1_byte: u8,
    program2_byte: u8,
    program1: i64,
    program2: i64,
}

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

    pub fn score(&self) -> (i64, i64) {
        (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 u32;
                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 u32;
                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
    }
}