diff options
| author | Siddarth Suresh <155843085+SiddarthSuresh98@users.noreply.github.com> | 2025-03-10 10:28:28 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-10 10:28:28 -0400 | 
| commit | 66edce63597093cf5f3afa5b577fd9e3ecae0ef6 (patch) | |
| tree | a0b0df4fff6f7d680901cd6007df67b15b3df3b6 /src | |
| parent | 92b41d1a8b970c46b0c6d2d8d538c68855a4de25 (diff) | |
| parent | 1f8b8babcfb383f1fb0281663561161061684206 (diff) | |
Merge pull request #19 from bdunahu/bdunahu
cache store, handle load requests to memory, write allocate policy
-- changes look good
Diffstat (limited to 'src')
| -rw-r--r-- | src/storage/cache.cc | 66 | ||||
| -rw-r--r-- | src/storage/dram.cc | 25 | ||||
| -rw-r--r-- | src/storage/storage.cc | 10 | ||||
| -rw-r--r-- | src/utils/utils.cc | 8 | 
4 files changed, 86 insertions, 23 deletions
| diff --git a/src/storage/cache.cc b/src/storage/cache.cc index 67cedda..2031367 100644 --- a/src/storage/cache.cc +++ b/src/storage/cache.cc @@ -1,22 +1,78 @@  #include "cache.h"  #include "definitions.h"  #include "response.h" +#include "utils.h"  #include <bits/stdc++.h> -Cache::Cache(int lines, Storage *lower, int delay) +Cache::Cache(Storage *lower, int delay)  {  	this->data = new std::vector<std::array<signed int, LINE_SIZE>>; -	this->data->resize(lines); -	this->lower = lower; +	this->data->resize(L1_CACHE_SIZE);  	this->delay = delay; -	this->lower = nullptr; +	this->is_waiting = false; +	this->lower = lower; +	this->meta.fill({-1, -1}); +	this->requester = IDLE; +	this->wait_time = this->delay;  }  Cache::~Cache() { delete this->data; }  Response Cache::write(Accessor accessor, signed int data, int address)  { +	Response r = WAIT; + +	/* Do this first--then process the first cycle immediately. */ +	if (this->requester == IDLE) +		this->requester = accessor; + +	if (this->requester == accessor) { +		fetch_resource(address); +		if (this->is_waiting) +			r = BLOCKED; +		else if (this->wait_time == 0) { +			int tag, index, offset; +			get_bit_fields(address, &tag, &index, &offset); +			this->data->at(index).at(offset) = data; +			this->meta[index].at(1) = 1; +			r = OK; +		} +	} + +	return r; +} + +Response Cache::read( +	Accessor accessor, int address, std::array<signed int, LINE_SIZE> &data) +{  	return WAIT;  } -Response Cache::read(Accessor accessor, int address, std::array<signed int, LINE_SIZE>& data) { return WAIT; } +void Cache::fetch_resource(int expected) +{ +	Response r = OK; +	int tag, index, offset; +	std::array<signed int, LINE_SIZE> actual; +	std::array<int, 2> *meta; + +	get_bit_fields(expected, &tag, &index, &offset); +	meta = &this->meta.at(index); + +	if (meta->at(0) != tag) { +		// address not in cache +		if (meta->at(1) >= 0) { +			// occupant is dirty +			// TODO +			r = WAIT; +		} else { +			actual = this->data->at(index); +			r = this->lower->read(L1CACHE, expected, actual); +			if (r == OK) { +				meta->at(0) = tag; +				meta->at(1) = -1; +			} +		} +	} + +	this->is_waiting = (r == OK) ? false : true; +} diff --git a/src/storage/dram.cc b/src/storage/dram.cc index 0db4c35..441f10b 100644 --- a/src/storage/dram.cc +++ b/src/storage/dram.cc @@ -8,13 +8,22 @@ Dram::Dram(int lines, int delay)  	this->data = new std::vector<std::array<signed int, LINE_SIZE>>;  	this->data->resize(lines);  	this->delay = delay; -	this->wait_time = this->delay; +	this->is_waiting = false;  	this->lower = nullptr;  	this->requester = IDLE; +	this->wait_time = this->delay;  }  Dram::~Dram() { delete this->data; } +void Dram::do_write(signed data, int address) +{ +	int line = address / LINE_SIZE; +	int word = address % LINE_SIZE; + +	this->data->at(line).at(word) = data; +} +  Response Dram::write(Accessor accessor, signed int data, int address)  {  	Response r = WAIT; @@ -38,20 +47,26 @@ Response Dram::write(Accessor accessor, signed int data, int address)  	return r;  } -void Dram::do_read(std::array<signed int, LINE_SIZE>& data_line, int address){ +void Dram::do_read(std::array<signed int, LINE_SIZE> &data_line, int address) +{  	int line = address / LINE_SIZE;  	data_line = this->data->at(line);  } -Response Dram::read(Accessor accessor, int address, std::array<signed int, LINE_SIZE>& data) {  +Response Dram::read( +	Accessor accessor, int address, std::array<signed int, LINE_SIZE> &data) +{  	Response r = WAIT; +  	if (this->requester == IDLE)  		this->requester = accessor; +  	if (this->requester == accessor) {  		if (this->wait_time == 0) {  			this->do_read(data, address);  			r = OK;  		}  	} -	return r;  - } + +	return r; +} diff --git a/src/storage/storage.cc b/src/storage/storage.cc index e3067a2..61531d1 100644 --- a/src/storage/storage.cc +++ b/src/storage/storage.cc @@ -13,20 +13,12 @@ Storage::view(int base, int lines)  	return ret;  } -void Storage::do_write(signed data, int address) -{ -	int line = address / LINE_SIZE; -	int word = address % LINE_SIZE; - -	this->data->at(line).at(word) = data; -} -  void Storage::resolve()  {  	if (this->wait_time == 0) {  		this->requester = IDLE;  		this->wait_time = delay; -	} else if (this->requester != IDLE) { +	} else if (this->requester != IDLE && !this->is_waiting) {  		--this->wait_time;  	}  } diff --git a/src/utils/utils.cc b/src/utils/utils.cc index b4bdb2f..dfeb2b3 100644 --- a/src/utils/utils.cc +++ b/src/utils/utils.cc @@ -1,11 +1,11 @@ -#include "definitions.h"  #include "utils.h" +#include "definitions.h"  void get_bit_fields(int address, int *tag, int *index, int *offset)  {  	*tag = -		MID(address, LINE_SPEC + L1_CACHE_SPEC, +		GET_MID_BITS(address, LINE_SPEC + L1_CACHE_SPEC,  			MEM_SPEC + LINE_SPEC + L1_CACHE_SPEC); -	*index = MID(address, LINE_SPEC, L1_CACHE_SPEC + LINE_SPEC); -	*offset = LAST(address, LINE_SPEC); +	*index = GET_MID_BITS(address, LINE_SPEC, L1_CACHE_SPEC + LINE_SPEC); +	*offset = GET_LS_BITS(address, LINE_SPEC);  } | 
