From eaa87e9fcd90c00d6261cbdb854efb7a09467f1d Mon Sep 17 00:00:00 2001 From: bd Date: Thu, 27 Mar 2025 20:30:18 -0400 Subject: Instr, InstrDTO gets/sets, other structures required for decode --- src/utils/accessor.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/utils') diff --git a/src/utils/accessor.cc b/src/utils/accessor.cc index 86484c5..99347ed 100644 --- a/src/utils/accessor.cc +++ b/src/utils/accessor.cc @@ -3,6 +3,8 @@ std::ostream &operator<<(std::ostream &os, Accessor a) { - const std::string nameA[] = {"IDLE", "MEM", "FETCH", "L1CACHE", "SIDE"}; + const std::string nameA[] = { + "IDLE", "WRITE", "MEM", "EXEC", "DCDE", "FETCH", "L1CACHE", "SIDE", + }; return os << nameA[a]; } -- cgit v1.2.3 From 9cd1f287f0c86b36788cd8ede812b847c584f711 Mon Sep 17 00:00:00 2001 From: bd Date: Fri, 28 Mar 2025 18:10:14 -0400 Subject: add get_instr_fields func to parse instruction fields from raw bits --- gui/main.cc | 2 + inc/definitions.h | 27 +++++++++++- inc/utils.h | 20 ++++++++- src/storage/cache.cc | 4 +- src/utils/utils.cc | 59 +++++++++++++++++++++++--- tests/utils.cc | 116 +++++++++++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 215 insertions(+), 13 deletions(-) (limited to 'src/utils') diff --git a/gui/main.cc b/gui/main.cc index 4c14fbe..8f9d06b 100644 --- a/gui/main.cc +++ b/gui/main.cc @@ -9,6 +9,7 @@ static Logger *global_log = Logger::getInstance(); static std::string version_number = "v0.1"; +// clang-format off static std::string banner = " _/_/_/ _/_/_/ _/_/_/ _/_/_/ \n" " _/ _/ _/ _/ _/ \n" @@ -24,6 +25,7 @@ static std::string banner = " _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ \n" " _/ _/ _/_/_/_/ _/_/_/ _/ _/_/ _/ _/ _/ \n" " _/_/ _/_/ "; +// clang-format on static void print_version_number() { std::cout << banner << version_number << '\n'; diff --git a/inc/definitions.h b/inc/definitions.h index ff2f7c6..c9367ff 100644 --- a/inc/definitions.h +++ b/inc/definitions.h @@ -11,6 +11,10 @@ * The total number of words in a line */ #define LINE_SIZE static_cast(pow(2, 2)) +/** + * Number of bits in a word + */ +#define WORD_SPEC 32 /** * The number of bits to specify a memory word @@ -28,7 +32,8 @@ * The total number of lines in l1 cache */ #define L1_CACHE_WORD_SPEC 7 -#define L1_CACHE_LINE_SPEC static_cast(L1_CACHE_WORD_SPEC - LINE_SPEC) +#define L1_CACHE_LINE_SPEC \ + static_cast(L1_CACHE_WORD_SPEC - LINE_SPEC) #define L1_CACHE_LINES static_cast(pow(2, L1_CACHE_LINE_SPEC)) /** @@ -46,6 +51,26 @@ */ #define GPR_NUM 16 +/** + * 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 + /** * Return the N least-significant bits from integer K using a bit mask * @param the integer to be parsed diff --git a/inc/utils.h b/inc/utils.h index df8d374..7fed157 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -10,7 +10,25 @@ * @param the resulting index * @param the resulting offset */ -void get_bit_fields(int address, int *tag, int *index, int *offset); +void get_cache_fields(int address, int *tag, int *index, int *offset); + +/** + * Parse an instruction into a type, opcode, and fields. If the type is invalid, + * only the type field will be set. + * @param the resulting first field, which varies per type. To call this + * function properly, this field must contain the full instruction bytes on + * function entry. + * @param the resulting second field, which varies per type. + * @param the resulting third field, which varies per type. + * @param the resulting type. + * @param the resulting opcode. + */ +void get_instr_fields( + signed int &s1, + signed int &s2, + signed int &s3, + unsigned int &type, + unsigned int &opcode); /** * Formats a string using snprintf. diff --git a/src/storage/cache.cc b/src/storage/cache.cc index dccab47..80f59ef 100644 --- a/src/storage/cache.cc +++ b/src/storage/cache.cc @@ -69,7 +69,7 @@ Response Cache::process( Response r = this->is_access_cleared(accessor, address); if (r == OK) { int tag, index, offset; - get_bit_fields(address, &tag, &index, &offset); + get_cache_fields(address, &tag, &index, &offset); request_handler(index, offset); } return r; @@ -104,7 +104,7 @@ void Cache::handle_miss(int expected) std::array *actual; std::array *meta; - get_bit_fields(expected, &tag, &index, &offset); + get_cache_fields(expected, &tag, &index, &offset); r = OK; meta = &this->meta.at(index); actual = &this->data->at(index); diff --git a/src/utils/utils.cc b/src/utils/utils.cc index 3a11c2c..3b140e0 100644 --- a/src/utils/utils.cc +++ b/src/utils/utils.cc @@ -4,14 +4,62 @@ #include #include -void get_bit_fields(int address, int *tag, int *index, int *offset) +static Logger *global_log = Logger::getInstance(); + +void get_cache_fields(int address, int *tag, int *index, int *offset) { - *tag = GET_MID_BITS( - address, L1_CACHE_LINE_SPEC + LINE_SPEC, MEM_WORD_SPEC); + *tag = GET_MID_BITS(address, L1_CACHE_LINE_SPEC + LINE_SPEC, MEM_WORD_SPEC); *index = GET_MID_BITS(address, LINE_SPEC, L1_CACHE_LINE_SPEC + LINE_SPEC); *offset = GET_LS_BITS(address, LINE_SPEC); } +void get_instr_fields( + signed int &s1, + signed int &s2, + signed int &s3, + unsigned int &type, + unsigned int &opcode) +{ + type = GET_LS_BITS(s1, TYPE_SIZE); + switch (type) { + case 0: + // R-TYPE + opcode = GET_MID_BITS(s1, TYPE_SIZE, TYPE_SIZE + R_OPCODE_SIZE); + s3 = GET_MID_BITS( + s1, TYPE_SIZE + R_OPCODE_SIZE + (REG_SIZE * 2), + TYPE_SIZE + R_OPCODE_SIZE + (REG_SIZE * 3)); + s2 = GET_MID_BITS( + s1, TYPE_SIZE + R_OPCODE_SIZE + REG_SIZE, + TYPE_SIZE + R_OPCODE_SIZE + (REG_SIZE * 2)); + s1 = GET_MID_BITS( + s1, TYPE_SIZE + R_OPCODE_SIZE, + TYPE_SIZE + R_OPCODE_SIZE + REG_SIZE); + break; + case 1: + // I-TYPE + opcode = GET_MID_BITS(s1, TYPE_SIZE, TYPE_SIZE + OPCODE_SIZE); + s3 = GET_MID_BITS( + s1, TYPE_SIZE + OPCODE_SIZE + (REG_SIZE * 2), WORD_SPEC); + s2 = GET_MID_BITS( + s1, TYPE_SIZE + OPCODE_SIZE + REG_SIZE, + TYPE_SIZE + OPCODE_SIZE + (REG_SIZE * 2)); + s1 = GET_MID_BITS( + s1, TYPE_SIZE + OPCODE_SIZE, TYPE_SIZE + OPCODE_SIZE + REG_SIZE); + break; + case 2: + // J-TYPE + opcode = GET_MID_BITS(s1, TYPE_SIZE, TYPE_SIZE + OPCODE_SIZE); + s2 = GET_MID_BITS(s1, TYPE_SIZE + OPCODE_SIZE + REG_SIZE, WORD_SPEC); + s1 = GET_MID_BITS( + s1, TYPE_SIZE + OPCODE_SIZE, TYPE_SIZE + OPCODE_SIZE + REG_SIZE); + break; + default: + global_log->log( + DEBUG, + string_format("%s returning invalid type: %d", __FUNCTION__, type)); + } +} + const std::string string_format(const char *const zcFormat, ...) { va_list vaArgs; @@ -28,8 +76,9 @@ const std::string string_format(const char *const zcFormat, ...) return std::string(zc.data(), iLen); } -int wrap_address(int address) { - if (address < 0){ +int wrap_address(int address) +{ + if (address < 0) { return ((address % MEM_WORDS) + MEM_WORDS) % MEM_WORDS; } return address % MEM_WORDS; diff --git a/tests/utils.cc b/tests/utils.cc index ea1a1ed..6c72694 100644 --- a/tests/utils.cc +++ b/tests/utils.cc @@ -2,26 +2,134 @@ #include "definitions.h" #include -TEST_CASE("Parse arbitrary fields # one", "[cache]") +TEST_CASE("Parse arbitrary fields # one", "[utils]") { int tag, index, offset; int address = 0b0001010101; - get_bit_fields(address, &tag, &index, &offset); + get_cache_fields(address, &tag, &index, &offset); CHECK(tag == 0b000); CHECK(index == 0b10101); CHECK(offset == 0b01); } -TEST_CASE("Parse arbitrary fields # two", "[cache]") +TEST_CASE("Parse arbitrary fields # two", "[utils]") { int tag, index, offset; int address = 0b0100111011; - get_bit_fields(address, &tag, &index, &offset); + get_cache_fields(address, &tag, &index, &offset); CHECK(tag == 0b010); CHECK(index == 0b01110); CHECK(offset == 0b11); } +TEST_CASE("Parse invalid type", "[utils]") +{ + signed int s1 = 0, s2 = 0, s3 = 0; + unsigned int type = 0, opcode = 0; + + s1 = 0x00FF00FF; + get_instr_fields(s1, s2, s3, type, opcode); + + REQUIRE(type == 0b11); + // behavior does nothing + CHECK(s1 == 0x00FF00FF); + CHECK(s2 == 0b0); + CHECK(s3 == 0b0); + CHECK(opcode == 0b0); +} + +TEST_CASE("Parse arbitrary r-type # one", "[utils]") +{ + signed int s1 = 0, s2 = 0, s3 = 0; + unsigned int type = 0, opcode = 0; + + s1 = 0xCCCCCCCC; + get_instr_fields(s1, s2, s3, type, opcode); + + REQUIRE(type == 0b00); + CHECK(s1 == 0b11001); + CHECK(s2 == 0b01100); + CHECK(s3 == 0b00110); + CHECK(opcode == 0b10011); +} + +TEST_CASE("Parse arbitrary r-type # two", "[utils]") +{ + signed int s1 = 0, s2 = 0, s3 = 0; + unsigned int type = 0, opcode = 0; + + s1 = 0x99AABBCC; + get_instr_fields(s1, s2, s3, type, opcode); + + REQUIRE(type == 0b00); + CHECK(s1 == 0b10111); + CHECK(s2 == 0b01011); + CHECK(s3 == 0b10101); + CHECK(opcode == 0b10011); +} + +TEST_CASE("Parse arbitrary i-type # one", "[utils]") +{ + signed int s1 = 0, s2 = 0, s3 = 0; + unsigned int type = 0, opcode = 0; + + s1 = 0xDDDDDDDD; + get_instr_fields(s1, s2, s3, type, opcode); + + REQUIRE(type == 0b01); + CHECK(s1 == 0b10111); + CHECK(s2 == 0b11011); + CHECK(s3 == 0xDDDD); + CHECK(opcode == 0b0111); +} + +TEST_CASE("Parse arbitrary i-type # two", "[utils]") +{ + signed int s1 = 0, s2 = 0, s3 = 0; + unsigned int type = 0, opcode = 0; + + s1 = 0xAABBCCDD; + get_instr_fields(s1, s2, s3, type, opcode); + + REQUIRE(type == 0b01); + CHECK(s1 == 0b10011); + CHECK(s2 == 0b11001); + CHECK(s3 == 0xAABB); + CHECK(opcode == 0b0111); +} + +TEST_CASE("Parse arbitrary j-type # one", "[utils]") +{ + signed int s1 = 0, s2 = 0, s3 = 0; + unsigned int type = 0, opcode = 0; + + s1 = 0xEEEEEEEE; + get_instr_fields(s1, s2, s3, type, opcode); + + REQUIRE(type == 0b10); + CHECK(s1 == 0b11011); + CHECK(s2 == 0b111011101110111011101); + CHECK(opcode == 0b1011); + // behavior does nothing + CHECK(s3 == 0b0); +} + +TEST_CASE("Parse arbitrary j-type # two", "[utils]") +{ + signed int s1 = 0, s2 = 0, s3 = 0; + unsigned int type = 0, opcode = 0; + + s1 = 0xBBCCDDEE; + get_instr_fields(s1, s2, s3, type, opcode); + + REQUIRE(type == 0b10); + CHECK(s1 == 0b10111); + CHECK(s2 == 0b101110111100110011011); + CHECK(opcode == 0b1011); + // behavior does nothing + CHECK(s3 == 0b0); +} + TEST_CASE("wrap address outside upper bound", "[utils]") { int address = MEM_WORDS + 25; -- cgit v1.2.3 From 7b3abbd51c91b51725a12d17fad6ecbfcdb19975 Mon Sep 17 00:00:00 2001 From: bd Date: Fri, 28 Mar 2025 19:55:04 -0400 Subject: Move get_instr_fields, add all instruction mnemonics --- inc/id.h | 18 +++++++++ inc/instr.h | 47 ++++++++++++++++++++++- inc/storage.h | 2 +- inc/utils.h | 18 --------- src/sim/id.cc | 58 +++++++++++++++++++++++++++- src/sim/instr.cc | 27 +++++++++++--- src/utils/utils.cc | 49 ------------------------ tests/utils.cc | 108 ----------------------------------------------------- 8 files changed, 142 insertions(+), 185 deletions(-) (limited to 'src/utils') diff --git a/inc/id.h b/inc/id.h index eafe274..1503457 100644 --- a/inc/id.h +++ b/inc/id.h @@ -15,6 +15,24 @@ class ID : public Stage ID(Stage *next); Response advance(InstrDTO &i) override; + + /** + * Parse an instruction into a type, opcode, and fields. If the type is + * invalid, only the type field will be set. + * @param the resulting first field, which varies per type. To call this + * function properly, this field must contain the full instruction bytes on + * function entry. + * @param the resulting second field, which varies per type. + * @param the resulting third field, which varies per type. + * @param the resulting type. + * @param the resulting opcode. + */ + void get_instr_fields( + signed int &s1, + signed int &s2, + signed int &s3, + unsigned int &type, + unsigned int &opcode); }; #endif /* ID_D_INCLUDED */ diff --git a/inc/instr.h b/inc/instr.h index ce08685..98ecf1d 100644 --- a/inc/instr.h +++ b/inc/instr.h @@ -1,13 +1,56 @@ #ifndef INSTR_H #define INSTR_H - #include +#include #include +enum Mnemonic { + ADD, + SUB, + MUL, + QUOT, + REM, + SFTR, + SFTL, + AND, + OR, + NOT, + XOR, + ADDV, + SUBV, + MULV, + DIVV, + CMP, + CEV, + LOAD, + LOADV, + ADDI, + SUBI, + SFTRI, + SFTLI, + ANDI, + ORI, + XORI, + STORE, + STOREV, + JMP, + JRL, + JAL, + BEQ, + BGT, + BUF, + BOF, + PUSH, + POP, +}; + +std::ostream &operator<<(std::ostream &os, Mnemonic a); + namespace instr { // clang-format off -extern const std::map>> instr_map; + extern const std::map mnemonic_map; + extern const std::map> instr_map; // clang-format on } // namespace instr diff --git a/inc/storage.h b/inc/storage.h index ff1fbcb..d6fa094 100644 --- a/inc/storage.h +++ b/inc/storage.h @@ -80,7 +80,7 @@ class Storage /** * The accessor currently being serviced. */ - enum Accessor requester; + Accessor requester; /** * The number of cycles until the current request is completed. */ diff --git a/inc/utils.h b/inc/utils.h index 7fed157..a375b68 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -12,24 +12,6 @@ */ void get_cache_fields(int address, int *tag, int *index, int *offset); -/** - * Parse an instruction into a type, opcode, and fields. If the type is invalid, - * only the type field will be set. - * @param the resulting first field, which varies per type. To call this - * function properly, this field must contain the full instruction bytes on - * function entry. - * @param the resulting second field, which varies per type. - * @param the resulting third field, which varies per type. - * @param the resulting type. - * @param the resulting opcode. - */ -void get_instr_fields( - signed int &s1, - signed int &s2, - signed int &s3, - unsigned int &type, - unsigned int &opcode); - /** * Formats a string using snprintf. * @param an object that represents the format string diff --git a/src/sim/id.cc b/src/sim/id.cc index 87dce0c..65acef5 100644 --- a/src/sim/id.cc +++ b/src/sim/id.cc @@ -1,9 +1,65 @@ #include "id.h" #include "accessor.h" #include "instrDTO.h" +#include "logger.h" #include "response.h" #include "stage.h" +#include "utils.h" + +static Logger *global_log = Logger::getInstance(); ID::ID(Stage *stage) : Stage(stage) { this->id = DCDE; } -Response ID::advance(InstrDTO &i) { return OK; } +Response ID::advance(InstrDTO &i) { Response r; } + +void ID::get_instr_fields( + signed int &s1, + signed int &s2, + signed int &s3, + unsigned int &type, + unsigned int &opcode) +{ + // unsigned int &opcode; + int opcode_bits; + + type = GET_LS_BITS(s1, TYPE_SIZE); + opcode_bits = (type == 0b0) ? R_OPCODE_SIZE : OPCODE_SIZE; + + switch (type) { + case 0: + // R-TYPE + opcode += GET_MID_BITS(s1, TYPE_SIZE, TYPE_SIZE + opcode_bits); + s3 = GET_MID_BITS( + s1, TYPE_SIZE + opcode_bits + (REG_SIZE * 2), + TYPE_SIZE + opcode_bits + (REG_SIZE * 3)); + s2 = GET_MID_BITS( + s1, TYPE_SIZE + opcode_bits + REG_SIZE, + TYPE_SIZE + opcode_bits + (REG_SIZE * 2)); + s1 = GET_MID_BITS( + s1, TYPE_SIZE + opcode_bits, + TYPE_SIZE + opcode_bits + REG_SIZE); + break; + case 1: + // I-TYPE + opcode = GET_MID_BITS(s1, TYPE_SIZE, TYPE_SIZE + opcode_bits); + s3 = GET_MID_BITS( + s1, TYPE_SIZE + opcode_bits + (REG_SIZE * 2), WORD_SPEC); + s2 = GET_MID_BITS( + s1, TYPE_SIZE + opcode_bits + REG_SIZE, + TYPE_SIZE + opcode_bits + (REG_SIZE * 2)); + s1 = GET_MID_BITS( + s1, TYPE_SIZE + opcode_bits, TYPE_SIZE + opcode_bits + REG_SIZE); + break; + case 2: + // J-TYPE + opcode = GET_MID_BITS(s1, TYPE_SIZE, TYPE_SIZE + opcode_bits); + s2 = GET_MID_BITS(s1, TYPE_SIZE + OPCODE_SIZE + REG_SIZE, WORD_SPEC); + s1 = GET_MID_BITS( + s1, TYPE_SIZE + OPCODE_SIZE, TYPE_SIZE + OPCODE_SIZE + REG_SIZE); + break; + default: + global_log->log( + DEBUG, + string_format("%s returning invalid type: %d", __FUNCTION__, type)); + } +} diff --git a/src/sim/instr.cc b/src/sim/instr.cc index 608b871..50aa71d 100644 --- a/src/sim/instr.cc +++ b/src/sim/instr.cc @@ -3,16 +3,16 @@ #include // clang-format off -#define INIT_INSTRUCTION(type, opcode, body) \ - {type, {{opcode, [](signed int &s1, signed int &s2, signed int &s3) { \ +#define INIT_INSTRUCTION(mnemonic, body) \ + {mnemonic, [](signed int &s1, signed int &s2, signed int &s3) { \ body; \ - }}}} + }} // clang-format on namespace instr { // clang-format off -const std::map>> +const std::map> // clang-format on instr_map = { @@ -20,11 +20,26 @@ const std::map mnemonic_map = { + {0b0000100, ADD}, {0b0001000, SUB}, {0b0001100, MUL}, {0b0010000, QUOT}, + {0b0010100, REM}, {0b0011000, SFTR}, {0b0011100, SFTL}, {0b0100000, AND}, + {0b0100100, OR}, {0b0101000, NOT}, {0b0101100, XOR}, {0b0110000, ADDV}, + {0b0110100, SUBV}, {0b0111000, MULV}, {0b0111100, DIVV}, {0b1000000, CMP}, + {0b1000100, CEV}, {0b000101, LOAD}, {0b001001, LOADV}, {0b001101, ADDI}, + {0b010001, SUBI}, {0b010101, SFTRI}, {0b011101, SFTLI}, {0b100001, ANDI}, + {0b100101, ORI}, {0b101001, XORI}, {0b101101, STORE}, {0b110001, STOREV}, + {0b000101, CEV}, {0b000101, LOAD}, {0b001001, LOADV}, {0b001001, LOADV}, + {0b000110, JMP}, {0b001010, JRL}, {0b001110, JAL}, {0b010010, BEQ}, + {0b010110, BGT}, {0b011010, BUF}, {0b011110, BOF}, {0b100010, PUSH}, + {0b100110, POP}, + +}; } // namespace instr diff --git a/src/utils/utils.cc b/src/utils/utils.cc index 3b140e0..e12a0e0 100644 --- a/src/utils/utils.cc +++ b/src/utils/utils.cc @@ -4,8 +4,6 @@ #include #include -static Logger *global_log = Logger::getInstance(); - void get_cache_fields(int address, int *tag, int *index, int *offset) { *tag = GET_MID_BITS(address, L1_CACHE_LINE_SPEC + LINE_SPEC, MEM_WORD_SPEC); @@ -13,53 +11,6 @@ void get_cache_fields(int address, int *tag, int *index, int *offset) *offset = GET_LS_BITS(address, LINE_SPEC); } -void get_instr_fields( - signed int &s1, - signed int &s2, - signed int &s3, - unsigned int &type, - unsigned int &opcode) -{ - type = GET_LS_BITS(s1, TYPE_SIZE); - switch (type) { - case 0: - // R-TYPE - opcode = GET_MID_BITS(s1, TYPE_SIZE, TYPE_SIZE + R_OPCODE_SIZE); - s3 = GET_MID_BITS( - s1, TYPE_SIZE + R_OPCODE_SIZE + (REG_SIZE * 2), - TYPE_SIZE + R_OPCODE_SIZE + (REG_SIZE * 3)); - s2 = GET_MID_BITS( - s1, TYPE_SIZE + R_OPCODE_SIZE + REG_SIZE, - TYPE_SIZE + R_OPCODE_SIZE + (REG_SIZE * 2)); - s1 = GET_MID_BITS( - s1, TYPE_SIZE + R_OPCODE_SIZE, - TYPE_SIZE + R_OPCODE_SIZE + REG_SIZE); - break; - case 1: - // I-TYPE - opcode = GET_MID_BITS(s1, TYPE_SIZE, TYPE_SIZE + OPCODE_SIZE); - s3 = GET_MID_BITS( - s1, TYPE_SIZE + OPCODE_SIZE + (REG_SIZE * 2), WORD_SPEC); - s2 = GET_MID_BITS( - s1, TYPE_SIZE + OPCODE_SIZE + REG_SIZE, - TYPE_SIZE + OPCODE_SIZE + (REG_SIZE * 2)); - s1 = GET_MID_BITS( - s1, TYPE_SIZE + OPCODE_SIZE, TYPE_SIZE + OPCODE_SIZE + REG_SIZE); - break; - case 2: - // J-TYPE - opcode = GET_MID_BITS(s1, TYPE_SIZE, TYPE_SIZE + OPCODE_SIZE); - s2 = GET_MID_BITS(s1, TYPE_SIZE + OPCODE_SIZE + REG_SIZE, WORD_SPEC); - s1 = GET_MID_BITS( - s1, TYPE_SIZE + OPCODE_SIZE, TYPE_SIZE + OPCODE_SIZE + REG_SIZE); - break; - default: - global_log->log( - DEBUG, - string_format("%s returning invalid type: %d", __FUNCTION__, type)); - } -} - const std::string string_format(const char *const zcFormat, ...) { va_list vaArgs; diff --git a/tests/utils.cc b/tests/utils.cc index 6c72694..2e0e934 100644 --- a/tests/utils.cc +++ b/tests/utils.cc @@ -22,114 +22,6 @@ TEST_CASE("Parse arbitrary fields # two", "[utils]") CHECK(offset == 0b11); } -TEST_CASE("Parse invalid type", "[utils]") -{ - signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; - - s1 = 0x00FF00FF; - get_instr_fields(s1, s2, s3, type, opcode); - - REQUIRE(type == 0b11); - // behavior does nothing - CHECK(s1 == 0x00FF00FF); - CHECK(s2 == 0b0); - CHECK(s3 == 0b0); - CHECK(opcode == 0b0); -} - -TEST_CASE("Parse arbitrary r-type # one", "[utils]") -{ - signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; - - s1 = 0xCCCCCCCC; - get_instr_fields(s1, s2, s3, type, opcode); - - REQUIRE(type == 0b00); - CHECK(s1 == 0b11001); - CHECK(s2 == 0b01100); - CHECK(s3 == 0b00110); - CHECK(opcode == 0b10011); -} - -TEST_CASE("Parse arbitrary r-type # two", "[utils]") -{ - signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; - - s1 = 0x99AABBCC; - get_instr_fields(s1, s2, s3, type, opcode); - - REQUIRE(type == 0b00); - CHECK(s1 == 0b10111); - CHECK(s2 == 0b01011); - CHECK(s3 == 0b10101); - CHECK(opcode == 0b10011); -} - -TEST_CASE("Parse arbitrary i-type # one", "[utils]") -{ - signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; - - s1 = 0xDDDDDDDD; - get_instr_fields(s1, s2, s3, type, opcode); - - REQUIRE(type == 0b01); - CHECK(s1 == 0b10111); - CHECK(s2 == 0b11011); - CHECK(s3 == 0xDDDD); - CHECK(opcode == 0b0111); -} - -TEST_CASE("Parse arbitrary i-type # two", "[utils]") -{ - signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; - - s1 = 0xAABBCCDD; - get_instr_fields(s1, s2, s3, type, opcode); - - REQUIRE(type == 0b01); - CHECK(s1 == 0b10011); - CHECK(s2 == 0b11001); - CHECK(s3 == 0xAABB); - CHECK(opcode == 0b0111); -} - -TEST_CASE("Parse arbitrary j-type # one", "[utils]") -{ - signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; - - s1 = 0xEEEEEEEE; - get_instr_fields(s1, s2, s3, type, opcode); - - REQUIRE(type == 0b10); - CHECK(s1 == 0b11011); - CHECK(s2 == 0b111011101110111011101); - CHECK(opcode == 0b1011); - // behavior does nothing - CHECK(s3 == 0b0); -} - -TEST_CASE("Parse arbitrary j-type # two", "[utils]") -{ - signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; - - s1 = 0xBBCCDDEE; - get_instr_fields(s1, s2, s3, type, opcode); - - REQUIRE(type == 0b10); - CHECK(s1 == 0b10111); - CHECK(s2 == 0b101110111100110011011); - CHECK(opcode == 0b1011); - // behavior does nothing - CHECK(s3 == 0b0); -} - TEST_CASE("wrap address outside upper bound", "[utils]") { int address = MEM_WORDS + 25; -- cgit v1.2.3 From 9793bf119cc6314e264bdfc9e98bc27c81db0adb Mon Sep 17 00:00:00 2001 From: bd Date: Sat, 29 Mar 2025 12:30:54 -0400 Subject: Add implementation functions for checking out a register. --- inc/instrDTO.h | 21 ++++++++++++-------- inc/response.h | 1 + inc/stage.h | 40 +++++++++++++++++++++++++++++++++++++- src/sim/id.cc | 37 ++++++++++++----------------------- src/sim/instrDTO.cc | 4 ++++ src/sim/stage.cc | 53 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/utils/response.cc | 2 +- 7 files changed, 120 insertions(+), 38 deletions(-) (limited to 'src/utils') diff --git a/inc/instrDTO.h b/inc/instrDTO.h index 2a6ab1f..77a223e 100644 --- a/inc/instrDTO.h +++ b/inc/instrDTO.h @@ -1,9 +1,10 @@ #ifndef INSTRDTO_H #define INSTRDTO_H -#include +#include "accessor.h" +#include "instr.h" #include +#include #include -#include "accessor.h" class InstrDTO { @@ -39,9 +40,9 @@ class InstrDTO */ signed int get_s3(); /** - * @return the string representation of oper. + * @return the mnemonic of the instruction */ - std::string get_oper_name(); + Mnemonic get_mnemonic(); /** * @param set hist key @@ -63,6 +64,10 @@ class InstrDTO * @param s3 */ void set_s3(signed int); + /** + * @param the mnemonic of the instruction + */ + void set_mnemonic(Mnemonic); private: /** @@ -82,10 +87,10 @@ class InstrDTO signed int s1; signed int s2; signed int s3; - /** - * The operation to be conducted during the execute phase. - */ - std::function instr; + /** + * The mnemonic of the operation. + */ + Mnemonic mnemonic; }; #endif /* INSTRDTO_H_INCLUDED */ diff --git a/inc/response.h b/inc/response.h index 6cd6678..05e9352 100644 --- a/inc/response.h +++ b/inc/response.h @@ -6,6 +6,7 @@ enum Response { OK, WAIT, BLOCKED, + STALLED, }; std::ostream &operator<<(std::ostream &os, Response r); diff --git a/inc/stage.h b/inc/stage.h index f6bad41..0348263 100644 --- a/inc/stage.h +++ b/inc/stage.h @@ -1,11 +1,12 @@ #ifndef STAGE_H #define STAGE_H +#include "accessor.h" #include "definitions.h" #include "instrDTO.h" #include "response.h" #include "storage.h" -#include "accessor.h" #include +#include class Stage { @@ -27,6 +28,16 @@ class Stage virtual Response advance(InstrDTO &i) = 0; protected: + /** + * Facilitates register checkout. + * It does this by checking that the register passed in is not currently + * checked out. If true, then replaces r with the value of the register and + * returns OK. If false, returns STALLED. + * + * @param the registers number, to be dereferenced. + * @return OK if `r` is not checked out, STALLED otherwise. + */ + Response check_out(unsigned int &r); /** * The name of the pipeline stage. */ @@ -59,6 +70,33 @@ class Stage * The current clock cycle. */ static int clock_cycle; + + private: + /** + * Helper for `check_out`. + * Returns true if r are not checked out, false otherwise. + * @param a list of register numbers. + * @return true if registers are not in checked_out, false otherwise. + */ + bool is_checked_out(unsigned int r); + /** + * Helper for `check_out`. + * Checks out a single register, and places it in checked_out. + * @param a register number. + * @return the value in the register. + */ + signed int check_out_register(unsigned int r); + // TODO fix this comment after writeback stage + /** + * Helper for `check_out_register` + * @param the register number. + * @return the value in the associated register. + */ + signed int dereference_register(unsigned int r); + /** + * The set of registers currently checked out. + */ + static std::set checked_out; }; #endif /* STAGE_H_INCLUDED */ diff --git a/src/sim/id.cc b/src/sim/id.cc index c6e42d5..e9c48df 100644 --- a/src/sim/id.cc +++ b/src/sim/id.cc @@ -5,13 +5,20 @@ #include "logger.h" #include "response.h" #include "stage.h" -#include "utils.h" - -static Logger *global_log = Logger::getInstance(); ID::ID(Stage *stage) : Stage(stage) { this->id = DCDE; } -Response ID::advance(InstrDTO &i) { Response r; } +Response ID::advance(InstrDTO &i) +{ + Response r; + signed int s1, s2, s3; + Mnemonic m; + + s1 = i.get_instr_bits(); + + get_instr_fields(s1, s2, s3, m); + return r; +} // TODO this function is ugly void ID::get_instr_fields( @@ -53,30 +60,10 @@ void ID::split_instr(signed int &raw, unsigned int &type, Mnemonic &m) opcode_size = (type == 0b0) ? R_OPCODE_SIZE : OPCODE_SIZE; opcode = GET_MID_BITS(raw, TYPE_SIZE, TYPE_SIZE + opcode_size); try { - m = instr::mnemonic_map.at((opcode << 2) + type); + m = instr::mnemonic_map.at((opcode << TYPE_SIZE) + type); } catch (std::out_of_range const &) { m = NOP; } raw = (unsigned int)raw >> (TYPE_SIZE + opcode_size); } - -Response ID::dereference_register(signed int &v) -{ - Response r; - r = OK; - - if (v < 0 || v >= GPR_NUM + V_NUM) { - global_log->log( - ERROR, string_format( - "instruction tried to access register %d, which does " - "not exist", - v)); - exit(EXIT_FAILURE); - } else if (v >= GPR_NUM) - v = this->vrs[v % GPR_NUM]; - else - v = this->gprs[v]; - - return r; -} diff --git a/src/sim/instrDTO.cc b/src/sim/instrDTO.cc index 7864eb4..7418033 100644 --- a/src/sim/instrDTO.cc +++ b/src/sim/instrDTO.cc @@ -19,6 +19,8 @@ signed int InstrDTO::get_s2() { return this->s2; } signed int InstrDTO::get_s3() { return this->s3; } +Mnemonic InstrDTO::get_mnemonic() { return this->mnemonic; } + void InstrDTO::set_time_of(Accessor a, int i) { this->hist[a] = i; } void InstrDTO::set_instr_bits(signed int instr) { this->instr_bits = instr; } @@ -28,3 +30,5 @@ void InstrDTO::set_s1(signed int s) { this->s1 = s; } void InstrDTO::set_s2(signed int s) { this->s2 = s; } void InstrDTO::set_s3(signed int s) { this->s3 = s; } + +void InstrDTO::set_mnemonic(Mnemonic m) { this->mnemonic = m; } diff --git a/src/sim/stage.cc b/src/sim/stage.cc index 560cc41..8d9dfc0 100644 --- a/src/sim/stage.cc +++ b/src/sim/stage.cc @@ -1,12 +1,59 @@ #include "stage.h" +#include "utils.h" +#include +#include -Stage::Stage(Stage *next) { - this->next = next; -} +static Logger *global_log = Logger::getInstance(); + +Stage::Stage(Stage *next) { this->next = next; } std::array Stage::gprs; std::array Stage::vrs; +std::set Stage::checked_out; unsigned int Stage::pc; Storage *Stage::storage; bool Stage::is_pipelined; int Stage::clock_cycle; + +Response Stage::check_out(unsigned int &v) +{ + Response r; + if (this->is_checked_out(v)) + r = STALLED; + else { + r = OK; + v = this->check_out_register(v); + } + return r; +} + +bool Stage::is_checked_out(unsigned int r) +{ + return this->checked_out.find(r) != this->checked_out.end(); +} + +signed int Stage::check_out_register(unsigned int v) +{ + signed int r; + this->checked_out.insert(v); + + r = this->dereference_register(v); + return r; +} + +signed int Stage::dereference_register(unsigned int v) +{ + signed int r; + + if (v >= GPR_NUM + V_NUM) { + global_log->log( + ERROR, string_format( + "instruction tried to access register %d, which does " + "not exist", + v)); + exit(EXIT_FAILURE); + } + + r = (v >= GPR_NUM) ? this->vrs[v % GPR_NUM] : this->gprs[v]; + return r; +} diff --git a/src/utils/response.cc b/src/utils/response.cc index def6578..3d6e439 100644 --- a/src/utils/response.cc +++ b/src/utils/response.cc @@ -3,6 +3,6 @@ std::ostream &operator<<(std::ostream &os, Response r) { - const std::string nameR[] = {"OK", "WAIT", "BLOCKED"}; + const std::string nameR[] = {"OK", "WAIT", "BLOCKED", "STALLED"}; return os << nameR[r]; } -- cgit v1.2.3