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
|
use rustv::isa;
use rustv::memory::{MemoryError, MemoryInterface, Result};
pub const WRITE_TRAP_VALUE: u8 = 0x42;
pub const AREA_TRAP_VALUE: u8 = 0x43;
pub const WRITE_TRAP_STALL: MemoryError = MemoryError::CacheMiss {
stall_cycles: 10000
};
/// A cache that stalls when a trap is triggered.
pub struct TrappedCache<T: MemoryInterface> {
cache: T,
half: isa::Address,
}
// TODO: move this t
enum WordOffset {
Byte0,
Byte1,
Byte2,
Byte3,
}
impl<T: MemoryInterface> TrappedCache<T> {
pub fn new(cache: T, half: isa::Address) -> TrappedCache<T> {
TrappedCache {
cache: cache,
half: half,
}
}
fn clear_trap(&mut self, address: isa::Address, offset: WordOffset) {
// Precondition: address is in the cache and can be
// read/written without stalling
match self.read_word(address) {
Ok(value) => {
let new_value = match offset {
WordOffset::Byte0 => value & 0xFFFFFF00,
WordOffset::Byte1 => value & 0xFFFF00FF,
WordOffset::Byte2 => value & 0xFF00FFFF,
WordOffset::Byte3 => value & 0x00FFFFFF,
};
match self.write_word(address, new_value) {
Ok(()) => {},
Err(e) => panic!("{:?}", e),
}
},
Err(e) => panic!("{:?}", e),
}
}
}
impl<T: MemoryInterface> MemoryInterface for TrappedCache<T> {
fn latency(&self) -> u32 {
self.cache.latency()
}
fn step(&mut self) {
self.cache.step();
}
fn is_address_accessible(&self, address: isa::Address) -> bool {
self.cache.is_address_accessible(address)
}
fn read_word(&mut self, address: isa::Address) -> Result<isa::Word> {
self.cache.read_word(address)
}
fn write_word(&mut self, address: isa::Address,
value: isa::Word) -> Result<()> {
if self.is_address_accessible(address) {
// No stall - check for trap
let old_value = self.read_word(address);
match old_value {
Ok(old_value) => {
// TODO: helper method on isa::Word (would require
// newtype) for accessing individual bytes?
if ((old_value & 0xFF) as u8) == WRITE_TRAP_VALUE &&
((value & 0xFF) as u8) != WRITE_TRAP_VALUE {
self.clear_trap(old_value, WordOffset::Byte0);
Err(WRITE_TRAP_STALL)
}
else if (((old_value >> 8) & 0xFF) as u8) == WRITE_TRAP_VALUE &&
(((value >> 8) & 0xFF) as u8) == WRITE_TRAP_VALUE {
self.clear_trap(old_value, WordOffset::Byte1);
Err(WRITE_TRAP_STALL)
}
else if (((old_value >> 16) & 0xFF) as u8) == WRITE_TRAP_VALUE &&
(((value >> 16) & 0xFF) as u8) == WRITE_TRAP_VALUE {
self.clear_trap(old_value, WordOffset::Byte2);
Err(WRITE_TRAP_STALL)
}
else if (((old_value >> 24) & 0xFF) as u8) == WRITE_TRAP_VALUE &&
(((value >> 24) & 0xFF) as u8) == WRITE_TRAP_VALUE {
self.clear_trap(old_value, WordOffset::Byte3);
Err(WRITE_TRAP_STALL)
}
else {
self.cache.write_word(address, value)
}
}
Err(e) => {
panic!("Could not read accessible value: {:?}", e)
}
}
}
else {
// Not in cache - defer to fetch
self.cache.write_word(address, value)
}
}
}
|