summaryrefslogtreecommitdiff
path: root/inc
diff options
context:
space:
mode:
Diffstat (limited to 'inc')
-rw-r--r--inc/cache.h80
-rw-r--r--inc/definitions.h48
-rw-r--r--inc/dram.h48
-rw-r--r--inc/storage.h40
-rw-r--r--inc/utils.h39
5 files changed, 90 insertions, 165 deletions
diff --git a/inc/cache.h b/inc/cache.h
index 0f15536..6f06466 100644
--- a/inc/cache.h
+++ b/inc/cache.h
@@ -3,77 +3,75 @@
#include "definitions.h"
#include "storage.h"
#include <array>
+#include <cmath>
#include <functional>
#include <ostream>
+/**
+ * Parse an address into a tag, index into the cache table, and a line
+ * offset.
+ * @param the address to be parsed
+ * @param the resulting tag
+ * @param the resulting index
+ * @param the resulting offset
+ */
+// clang-format off
+#define GET_FIELDS(a, t, i, o) \
+ *(t) = GET_MID_BITS(a, this->size + LINE_SPEC - this->ways, MEM_WORD_SPEC); \
+ *(i) = GET_MID_BITS(a, LINE_SPEC, this->size + LINE_SPEC - this->ways); \
+ *(o) = GET_LS_BITS(a, LINE_SPEC)
+// clang-format on
+
class Cache : public Storage
{
public:
/**
- * Constructor.
+nn * Constructor.
* @param The number of `lines` contained in memory. The total number of
* words is this number multiplied by LINE_SIZE.
* @param The next lowest level in storage. Methods from this object are
* called in case of a cache miss.
+ * @param The number of bits required to specify a line in this level of cache.
+ * @param The number of ways this line of cache uses, or the number of data addresses stored for
+ * certain address index.
* @param The number of clock cycles each access takes.
* @return A new cache object.
*/
- Cache(Storage *lower, int delay);
+ Cache(Storage *lower, unsigned int size, unsigned int ways, int delay);
~Cache();
- int
- write_word(void *, signed int, int) override;
- int
- write_line(void *, std::array<signed int, LINE_SIZE>, int) override;
- int
- read_line(void *, int, std::array<signed int, LINE_SIZE> &) override;
- int
- read_word(void *, int, signed int &) override;
-
- /**
- * Getter for the meta attribute.
- * TODO this doesn't seem like good object-oriented practice.
- * @return this->meta
- */
- std::array<std::array<int, 2>, L1_CACHE_LINES>
- get_meta() const;
+ int write_word(void *, signed int, int) override;
+ int write_line(void *, std::array<signed int, LINE_SIZE>, int) override;
+ int read_line(void *, int, std::array<signed int, LINE_SIZE> &) override;
+ int read_word(void *, int, signed int &) override;
private:
+ int process(
+ void *id, int address, std::function<void(int index, int offset)> request_handler) override;
/**
- * Helper for all access methods.
- * Calls `request_handler` when `id` is allowed to complete its
- * request cycle.
- * @param the source making the request
- * @param the address to write to
- * @param the function to call when an access should be completed
- */
- int
- process(void *id, int address, std::function<void(int index, int offset)> request_handler);
- /**
- * Returns OK if `id` is allowed to complete its request this cycle.
- * Handles cache misses, wait times, and setting the current id this
- * storage is serving.
- * @param the id asking for a resource
- * @return 1 if the access can be carried out this function call, 0 otherwise.
- */
- int
- is_access_cleared(void *id, int address);
- /**
- * Helper for is_access_cleared.
+ * Helper for process.
* Fetches `address` from a lower level of storage if it is not already
* present. The victim line is chosen/written back.
* @param the address that must be present in cache.
* @param 0 if the address is currently in cache, 1 if it is being fetched.
*/
- int
- is_address_missing(int address);
+ int is_address_missing(int address);
+ /**
+ * The number of bits required to specify a line in this level of cache.
+ */
+ unsigned int size;
+ /**
+ * The number of bits required to specify a way, or the number of data addresses stored for
+ * certain address index.
+ */
+ unsigned int ways;
/**
* An array of metadata about elements in `data`.
* If the first value of an element is negative, the corresponding
* element in `data` is invalid. If the most second value of an element
* is nonzero, the corresponding element in `data` is dirty.
*/
- std::array<std::array<int, 2>, L1_CACHE_LINES> meta;
+ std::vector<std::array<signed int, 3>> meta;
};
#endif /* CACHE_H_INCLUDED */
diff --git a/inc/definitions.h b/inc/definitions.h
index 6fa29ee..8513361 100644
--- a/inc/definitions.h
+++ b/inc/definitions.h
@@ -20,22 +20,12 @@
* The number of bits to specify a memory line
* The total number of lines in memory
*/
-#define MEM_WORD_SPEC 10
+#define MEM_WORD_SPEC 16
#define MEM_LINE_SPEC static_cast<unsigned int>(MEM_WORD_SPEC - LINE_SPEC)
#define MEM_WORDS static_cast<int>(pow(2, MEM_WORD_SPEC))
#define MEM_LINES static_cast<int>(pow(2, MEM_LINE_SPEC))
/**
- * The number of bits to specify a l1 cache word
- * The number of bits to specify a l1 cache line
- * The total number of lines in l1 cache
- */
-#define L1_CACHE_WORD_SPEC 7
-#define L1_CACHE_LINE_SPEC \
- static_cast<unsigned int>(L1_CACHE_WORD_SPEC - LINE_SPEC)
-#define L1_CACHE_LINES static_cast<int>(pow(2, L1_CACHE_LINE_SPEC))
-
-/**
* The total number of cycles a memory access takes
*/
#define MEM_DELAY 3
@@ -46,42 +36,6 @@
#define L1_CACHE_DELAY 0
/**
- * The number of general purpose registers
- */
-#define GPR_NUM 16
-
-/**
- * The number of vector registers
- */
-#define V_NUM 8
-
-/**
- * The number of bits to specify an instruction type
- */
-#define TYPE_SIZE 2
-
-/**
- * The number of bits to specify a register
- */
-#define REG_SIZE 5
-
-/**
- * The number of bits to specify an R-Type opcode.
- */
-#define R_OPCODE_SIZE 5
-
-/**
- * The number of bits to specify an opcode.
- */
-#define OPCODE_SIZE 4
-
-/**
- * The maximum value an integer can hold.
- * The minimum is always this number plus one negated.
- */
-#define MAX_INT 2147483647
-
-/**
* Return the N least-significant bits from integer K using a bit mask
* @param the integer to be parsed
* @param the number of bits to be parsed
diff --git a/inc/dram.h b/inc/dram.h
index c6c3159..2a4e358 100644
--- a/inc/dram.h
+++ b/inc/dram.h
@@ -5,6 +5,15 @@
#include <functional>
#include <ostream>
+// clang-format off
+/**
+ * Ensures address is within the current memory size using a clean wrap.
+ * @param an address
+ */
+#define WRAP_ADDRESS(a) \
+ ((a < 0) ? ((a % MEM_WORDS) + MEM_WORDS) % MEM_WORDS : a % MEM_WORDS)
+// clang-format on
+
class Dram : public Storage
{
public:
@@ -16,42 +25,25 @@ class Dram : public Storage
Dram(int delay);
~Dram();
- int
- write_word(void *, signed int, int) override;
- int
- write_line(void *, std::array<signed int, LINE_SIZE>, int) override;
- int
- read_word(void *, int, signed int &) override;
- int
- read_line(void *, int, std::array<signed int, LINE_SIZE> &) override;
+ int write_word(void *, signed int, int) override;
+ int write_line(void *, std::array<signed int, LINE_SIZE>, int) override;
+ int read_word(void *, int, signed int &) override;
+ int read_line(void *, int, std::array<signed int, LINE_SIZE> &) override;
/**
* TODO This will accept a file at a later date.
*/
- void
- load(std::vector<signed int> program);
+ void load(std::vector<signed int> program);
private:
+ int process(void *id, int address, std::function<void(int line, int word)> request_handler) override;
/**
- * Helper for all access methods.
- * Calls `request_handler` when `id` is allowed to complete its
- * request cycle.
- * @param the source making the request
- * @param the address to write to
- * @param the function to call when an access should be completed
- * @return 1 if the access completed successfully, 0 otherwise
- */
- int
- process(void *id, int address, std::function<void(int line, int word)> request_handler);
- /**
- * Returns OK if `id` is allowed to complete its request this cycle.
- * Handles wait times, side door, and setting the current id this
- * storage is serving.
- * @param the source making the request
- * @return 1 if the access can be completed this function call, 0 otherwise
+ * Given `address`, returns the line and word it is in.
+ * @param an address
+ * @param the line (row) `address` is in
+ * @param the word (column) `address` corresponds to
*/
- int
- is_access_cleared(void *id);
+ void get_memory_index(int address, int &line, int &word);
};
#endif /* DRAM_H_INCLUDED */
diff --git a/inc/storage.h b/inc/storage.h
index 81194da..1bf5805 100644
--- a/inc/storage.h
+++ b/inc/storage.h
@@ -3,6 +3,7 @@
#include "definitions.h"
#include <algorithm>
#include <array>
+#include <functional>
#include <map>
#include <vector>
@@ -24,10 +25,8 @@ class Storage
* @param the address to write to.
* @return 1 if the request was completed, 0 otherwise.
*/
- virtual int
- write_word(void *id, signed int data, int address) = 0;
- virtual int
- write_line(void *id, std::array<signed int, LINE_SIZE> data_line, int address) = 0;
+ virtual int write_word(void *id, signed int data, int address) = 0;
+ virtual int write_line(void *id, std::array<signed int, LINE_SIZE> data_line, int address) = 0;
/**
* Get the data line at `address`.
@@ -36,10 +35,8 @@ class Storage
* @param the data being returned
* @return 1 if the request was completed, 0 otherwise
*/
- virtual int
- read_line(void *id, int address, std::array<signed int, LINE_SIZE> &data) = 0;
- virtual int
- read_word(void *id, int address, signed int &data) = 0;
+ virtual int read_line(void *id, int address, std::array<signed int, LINE_SIZE> &data) = 0;
+ virtual int read_word(void *id, int address, signed int &data) = 0;
/**
* Sidedoor view of `lines` of memory starting at `base`.
@@ -48,11 +45,34 @@ class Storage
* @return A matrix of data values, where each row is a line and each column
* is a word.
*/
- std::vector<std::array<signed int, LINE_SIZE>>
- view(int base, int lines) const;
+ std::vector<std::array<signed int, LINE_SIZE>> view(int base, int lines) const;
protected:
/**
+ * Helper for all access methods.
+ * Calls `request_handler` when `id` is allowed to complete its
+ * request cycle.
+ * May include extra checks depending on storage device.
+ * @param the source making the request
+ * @param the address to write to
+ * @param the function to call when an access should be completed
+ */
+ virtual int
+ process(void *id, int address, std::function<void(int index, int offset)> request_handler) = 0;
+ /**
+ * Helper for process. Given `id`, returns 0 if the request should trivially be ignored.
+ * @param the source making the request
+ * @return 0 if the request should not be completed, 1 if it should be evaluated further.
+ */
+ int preprocess(void *id);
+ /**
+ * Returns OK if `id` should complete its request this cycle. In the case it can, automatically
+ * clears the current requester.
+ * @param the id asking for a resource
+ * @return 1 if the access can be carried out this function call, 0 otherwise.
+ */
+ int is_data_ready();
+ /**
* The data currently stored in this level of storage.
*/
std::vector<std::array<signed int, LINE_SIZE>> *data;
diff --git a/inc/utils.h b/inc/utils.h
deleted file mode 100644
index a375b68..0000000
--- a/inc/utils.h
+++ /dev/null
@@ -1,39 +0,0 @@
-#ifndef UTILS_H
-#define UTILS_H
-#include <string>
-
-/**
- * Parse an address into a tag, index into the cache table, and a line
- * offset.
- * @param the address to be parsed
- * @param the resulting tag
- * @param the resulting index
- * @param the resulting offset
- */
-void get_cache_fields(int address, int *tag, int *index, int *offset);
-
-/**
- * Formats a string using snprintf.
- * @param an object that represents the format string
- * @param arguments to be formatted
- * @return a string object holding the formatted result
- */
-const std::string string_format(const char *const zcFormat, ...);
-
-/**
- * Given `address`, returns an address that is within the current memory size
- * using a clean wrap.
- * @param an address
- * @return an address guaranteed to be within range.
- */
-int wrap_address(int address);
-
-/**
- * Given `address`, returns the line and word it is in.
- * @param an address
- * @param the line (row) `address` is in
- * @param the word (column) `address` corresponds to
- */
-void get_memory_index(int address, int &line, int &word);
-
-#endif /* UTILS_H_INCLUDED */