aboutsummaryrefslogtreecommitdiff
path: root/src/shareable_cache.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/shareable_cache.rs')
-rw-r--r--src/shareable_cache.rs46
1 files changed, 27 insertions, 19 deletions
diff --git a/src/shareable_cache.rs b/src/shareable_cache.rs
index 09d514f..9f558cd 100644
--- a/src/shareable_cache.rs
+++ b/src/shareable_cache.rs
@@ -3,8 +3,8 @@ use rustv::memory::{MemoryInterface, Result, SharedMemory};
/// A cache that can be used as two separate caches or one
/// set-associative cache.
-#[derive(Clone)]
pub struct ShareableCache<'a> {
+ core_id: usize,
primary: SharedMemory<'a>,
secondary: SharedMemory<'a>,
secondary_enabled: bool,
@@ -12,20 +12,16 @@ pub struct ShareableCache<'a> {
}
impl<'a> ShareableCache<'a> {
- pub fn new(cache1: SharedMemory<'a>, cache2: SharedMemory<'a>)
- -> (ShareableCache<'a>,
- ShareableCache<'a>) {
- (ShareableCache {
+ pub fn new(core_id: usize,
+ cache1: SharedMemory<'a>, cache2: SharedMemory<'a>)
+ -> ShareableCache<'a> {
+ ShareableCache {
+ core_id: core_id,
primary: cache1.clone(),
secondary: cache2.clone(),
secondary_enabled: false,
use_secondary: false,
- }, ShareableCache {
- primary: cache2.clone(),
- secondary: cache1.clone(),
- secondary_enabled: false,
- use_secondary: false,
- })
+ }
}
pub fn enable_secondary(&mut self) {
@@ -36,6 +32,19 @@ impl<'a> ShareableCache<'a> {
pub fn disable_secondary(&mut self) {
self.secondary_enabled = false;
}
+
+ fn address_accessible(&self, address: isa::Address) -> (bool, bool) {
+ // Use scopes to make sure these borrows end before the
+ // branches of the if statement begin
+ let primary_accessible = {
+ self.primary.borrow().is_address_accessible(address)
+ };
+ let secondary_accessible = {
+ self.secondary.borrow().is_address_accessible(address)
+ };
+
+ (primary_accessible, secondary_accessible)
+ }
}
impl<'a> MemoryInterface for ShareableCache<'a> {
@@ -58,10 +67,13 @@ impl<'a> MemoryInterface for ShareableCache<'a> {
fn read_word(&mut self, address: isa::Address) -> Result<isa::Word> {
if self.secondary_enabled {
- if self.primary.borrow().is_address_accessible(address) {
+ let (primary_accessible, secondary_accessible) =
+ self.address_accessible(address);
+
+ if primary_accessible {
self.primary.borrow_mut().read_word(address)
}
- else if self.secondary.borrow().is_address_accessible(address) {
+ else if secondary_accessible {
self.secondary.borrow_mut().read_word(address)
}
else {
@@ -81,12 +93,8 @@ impl<'a> MemoryInterface for ShareableCache<'a> {
fn write_word(&mut self, address: isa::Address, value: isa::Word) -> Result<()> {
if self.secondary_enabled {
- let primary_accessible = {
- self.primary.borrow().is_address_accessible(address)
- };
- let secondary_accessible = {
- self.secondary.borrow().is_address_accessible(address)
- };
+ let (primary_accessible, secondary_accessible) =
+ self.address_accessible(address);
if primary_accessible {
self.primary.borrow_mut().write_word(address, value)