diff options
author | Siddarth Suresh <155843085+SiddarthSuresh98@users.noreply.github.com> | 2025-03-11 11:28:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-11 11:28:25 -0400 |
commit | 33c7c78b1c65c375d0291fd435e02ddc9d35681b (patch) | |
tree | 25646d98b4bfcf4b9a664eabfc2651c481984c1d /inc | |
parent | 66edce63597093cf5f3afa5b577fd9e3ecae0ef6 (diff) | |
parent | 202f9a05d449ddc1160584c4e8a87f397f248e94 (diff) |
Merge pull request #23 from bdunahu/bdunahu
Memory simulator CLI function implementation
Diffstat (limited to 'inc')
-rw-r--r-- | inc/accessor.h | 15 | ||||
-rw-r--r-- | inc/cache.h | 10 | ||||
-rw-r--r-- | inc/cli.h | 172 | ||||
-rw-r--r-- | inc/definitions.h | 19 | ||||
-rw-r--r-- | inc/dram.h | 16 | ||||
-rw-r--r-- | inc/logger.h | 26 | ||||
-rw-r--r-- | inc/response.h | 3 | ||||
-rw-r--r-- | inc/storage.h | 24 | ||||
-rw-r--r-- | inc/utils.h | 9 |
9 files changed, 181 insertions, 113 deletions
diff --git a/inc/accessor.h b/inc/accessor.h new file mode 100644 index 0000000..fb4999d --- /dev/null +++ b/inc/accessor.h @@ -0,0 +1,15 @@ +#ifndef ACCESSOR_H +#define ACCESSOR_H +#include <iostream> + +enum Accessor { + IDLE, + MEM, + FETCH, + L1CACHE, + SIDE, +}; + +std::ostream &operator<<(std::ostream &os, Accessor a); + +#endif /* ACCESSOR_H_INCLUDED */ diff --git a/inc/cache.h b/inc/cache.h index e8b7030..04f6181 100644 --- a/inc/cache.h +++ b/inc/cache.h @@ -3,6 +3,7 @@ #include "definitions.h" #include "storage.h" #include <array> +#include <ostream> class Cache : public Storage { @@ -25,6 +26,13 @@ class Cache : public Storage int address, std::array<signed int, LINE_SIZE> &data) 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_SIZE> get_meta() const; + private: /** * Fetches `address` from a lower level of storage if it is not already @@ -42,4 +50,6 @@ class Cache : public Storage std::array<std::array<int, 2>, L1_CACHE_SIZE> meta; }; +std::ostream &operator<<(std::ostream &os, const Cache &c); + #endif /* CACHE_H_INCLUDED */ @@ -1,95 +1,97 @@ #ifndef CLI_H #define CLI_H -#include <unordered_map> +#include "cache.h" #include <functional> #include <string> +#include <unordered_map> + +class Cli +{ + public: + /** + * Constructor. + * @return A newly allocated CLI object. + */ + Cli(); + ~Cli(); + + /** + * Prints all available commands to the console. + */ + void help(); + + /** + * Loads data from memory from the specified memory address. + * @param memory_address address of the memory where data needs to be loaded + * from + */ + void load(Accessor accessor, int memory_address); + + /** + * Stores data into memory at the specified address. + * @param accessor the pipline stage that is making this request + * @param data data value to be written to the memory + * @param address address of the memory where data needs to be stored + * @return the response from the storage device + */ + void store(Accessor accessor, int data, int address); + + /** + * Resets the memory configuration and cycles to their initial state. + * This function provides a side door reset interface to the memory system, + * allowing the user to reset the memory configuration directly. + */ + void reset(); -class Cli { - public: - - Cli(); - - ~Cli(); - - /** - * Prints all available commands to the console. - */ - void help(); - - /** - * Loads data from memory from the specified memory address. - * @param memory_address address of the memory where data needs to be loaded from - * @param pipeline_stage pipeline stage to be served by memory subsystem - */ - void load(int memory_address, int pipeline_stage); - - /** - * Stores data into memory at the specified address. - * @param memory_address address of the memory where data needs to be stored - * @param pipeline_stage pipeline stage to be served by memory subsystem - * @param data data value to be written to the memory - */ - void store(int memory_address, int pipeline_stage, int data); - - /** - * Loads a memory image from a file and configures memory to the image. - * This function provides a side door memory image loading interface to the memory system, - * allowing the user to load a memory image from a file and configure the memory subsystem to the image. - * @param filename name of file containing memory image - */ - void load_memory_image(const std::string& filename); - - /** - * Resets the memory configuration and cycles to their initial state. - * This function provides a side door reset interface to the memory system, - * allowing the user to reset the memory configuration directly. - */ - void reset(); - - /** - * Updates the memory at the specified address with the given data. - * This function provides a side door modification interface to the memory system, - * allowing the user to modify the memory configuration directly. - * @param memory_address address of the memory to be updated - * @param data data value to be written to the memory - */ - void update_memory(int memory_address, int data); - - /** - * Displays the current status of the entire memory subsystem. - * This function provides a side door view into the memory system, - * showing its current state and configuration. - */ - void view_memory(); - - /** - * Displays the data at the specified memory address. - * This function provides a side door view into the memory system, - * showing the data at the specified memory address. - * @param memory_address address of the memory to be viewed - */ - void view_memory_address(int memory_address); - - /** - * Updates the controls using a configuration file. - * This function provides a side door modification interface to the control system, - * allowing the user to update the controls directly. - * @param config_file name of file containing control configuration - */ - void update_controls(const std::string& config_file); + /** + * Advance the clock one cycle, refreshing the storage devices. + */ + void clock(); - /** - * Runs the command line interface - * This function is the main entry point for the command line interface. - */ - void run(); + /** + * Displays `lines` lines of the data in `level`, starting from `base`. + * + * + * This function provides a side door view into the storage system, showing + * its current state and configuration. + * @param level the level specifying the storage device. The first level + * one cache is level zero, with descending levels incrementing by a factor + * of one. + */ + void peek(int level); - private: + /** + * Runs the command line interface + * This function is the main entry point for the command line interface. + */ + void run(); - /** Map of commands and their corresponding functions. - * This map is used to store the commands and their corresponding functions. - */ - std::unordered_map<std::string, std::function<void(std::vector<std::string>)>> commands; + private: + /** + * Initializes the cache object. + */ + void initialize(); + /** + * Attempts to match string to either fetch or mem, or throws + * std::invalid_argument otherwise. + * @param the string to be converted accessor + * @return the corresponding accessor + * @throws invalid_argument if the string is not fetch or mem + */ + Accessor match_accessor_or_die(std::string s); + /** Map of commands and their corresponding functions. + * This map is used to store the commands and their corresponding functions. + */ + std::unordered_map<char, std::function<void(std::vector<std::string>)>> + commands; + /** + * The cache object to interface with. + */ + Cache *cache; + /** + * The current cycle. + */ + int cycle; }; -#endif /* CLI_H_INCLUDED */
\ No newline at end of file +#endif /* CLI_H_INCLUDED */ diff --git a/inc/definitions.h b/inc/definitions.h index 877065e..f015ce9 100644 --- a/inc/definitions.h +++ b/inc/definitions.h @@ -1,5 +1,6 @@ #ifndef DEFINITIONS_H #define DEFINITIONS_H +#include "logger.h" #include <cmath> /** @@ -9,17 +10,17 @@ /** * The total number of words in a line */ -#define LINE_SIZE (int)pow(2, 2) +#define LINE_SIZE static_cast<int>(pow(2, 2)) /** * The number of bits to specify a memory line * calculated as: (/ (expt 2 15) 4) */ -#define MEM_SPEC 13 +#define MEM_SPEC 8 /** * The total number of words in memory */ -#define MEM_SIZE (int)pow(2, MEM_SPEC) +#define MEM_SIZE static_cast<int>(pow(2, MEM_SPEC)) /** * The number of bits to specify a l1 cache line @@ -28,7 +29,17 @@ /** * The total number of words in l1 cache */ -#define L1_CACHE_SIZE (int)pow(2, L1_CACHE_SPEC) +#define L1_CACHE_SIZE static_cast<int>(pow(2, L1_CACHE_SPEC)) + +/** + * The total number of cycles a memory access takes. + */ +#define MEM_DELAY 4 + +/** + * The total number of cycles a level one cache access takes + */ +#define L1_CACHE_DELAY 1 /** * Return the N least-significant bits from integer K using a bit mask @@ -1,6 +1,8 @@ #ifndef DRAM_H #define DRAM_H -#include <storage.h> +#include "definitions.h" +#include "storage.h" +#include <ostream> class Dram : public Storage { @@ -16,9 +18,12 @@ class Dram : public Storage ~Dram(); Response write(Accessor accessor, signed int data, int address) override; - Response read(Accessor accessor, int address, std::array<signed int, LINE_SIZE>& data) override; + Response read( + Accessor accessor, + int address, + std::array<signed int, LINE_SIZE> &data) override; - private: + private: /** * Helper for `write`. */ @@ -26,8 +31,9 @@ class Dram : public Storage /** * Helper for `read`. */ - void do_read(std::array<signed int, LINE_SIZE>& data_line, int address); + void do_read(std::array<signed int, LINE_SIZE> &data_line, int address); }; -#endif /* DRAM_H_INCLUDED */ +std::ostream &operator<<(std::ostream &os, const Dram &d); +#endif /* DRAM_H_INCLUDED */ diff --git a/inc/logger.h b/inc/logger.h index 7ab3051..38527c8 100644 --- a/inc/logger.h +++ b/inc/logger.h @@ -9,13 +9,14 @@ enum LogLevel { DEBUG, INFO, WARNING, ERROR, CRITICAL }; class Logger { public: + static Logger* getInstance(); + + ~Logger(); + /** - * Constructor. - * @param The file name to log to. - * @return A new logger object. + * Do not allow copies. */ - Logger(const string &); - ~Logger(); + Logger(const Logger& obj) = delete; /** * Set the log level. @@ -31,10 +32,17 @@ class Logger void log(LogLevel, const string &); private: - LogLevel level = INFO; - ofstream logFile; - string levelToString(LogLevel); - int levelToInt(LogLevel); + /** + * Constructor. + * @param The file name to log to. + * @return A new logger object. + */ + Logger(const string &); + static Logger* logger_instance; + static LogLevel level; + static ofstream logFile; + static string level_to_string(LogLevel); + static int level_to_int(LogLevel); }; #endif /* LOGGER_H_INCLUDED */ diff --git a/inc/response.h b/inc/response.h index d945e0f..6cd6678 100644 --- a/inc/response.h +++ b/inc/response.h @@ -1,5 +1,6 @@ #ifndef RESPONSE_H #define RESPONSE_H +#include <iostream> enum Response { OK, @@ -7,4 +8,6 @@ enum Response { BLOCKED, }; +std::ostream &operator<<(std::ostream &os, Response r); + #endif /* RESPONSE_H_INCLUDED */ diff --git a/inc/storage.h b/inc/storage.h index 793b982..a30e74d 100644 --- a/inc/storage.h +++ b/inc/storage.h @@ -1,22 +1,18 @@ #ifndef STORAGE_H #define STORAGE_H +#include "accessor.h" #include "definitions.h" #include "response.h" #include <algorithm> #include <array> +#include <map> #include <vector> -enum Accessor { - IDLE, - MEM, - FETCH, - L1CACHE, - SIDE, -}; - class Storage { public: + virtual ~Storage() = default; + /** * Write `data` into `address`. * @param the source making the request. @@ -43,12 +39,20 @@ 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); + std::vector<std::array<signed int, LINE_SIZE>> + view(int base, int lines) const; /** - * Advances to the next job if the current job is completed. + * Refreshes the state of this storage device and lower. */ void resolve(); + /** + * Getter for lower attribute. + * TODO this doesn't seem like good object-oriented practice. + * @return this->lower + */ + Storage *get_lower(); + protected: /** * The data currently stored in this level of storage. diff --git a/inc/utils.h b/inc/utils.h index e258ed8..71e515b 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -1,5 +1,6 @@ #ifndef UTILS_H #define UTILS_H +#include <string> /** * Parse an address into a tag, index into the cache table, and a line @@ -11,4 +12,12 @@ */ void get_bit_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, ...); + #endif /* UTILS_H_INCLUDED */ |