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/sim/instr.cc | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/sim/instr.cc (limited to 'src/sim/instr.cc') diff --git a/src/sim/instr.cc b/src/sim/instr.cc new file mode 100644 index 0000000..608b871 --- /dev/null +++ b/src/sim/instr.cc @@ -0,0 +1,30 @@ +#include "instr.h" +#include +#include + +// clang-format off +#define INIT_INSTRUCTION(type, opcode, body) \ + {type, {{opcode, [](signed int &s1, signed int &s2, signed int &s3) { \ + body; \ + }}}} +// clang-format on + +namespace instr +{ +// clang-format off +const std::map>> + // clang-format on + instr_map = { + + /* R type instructions */ + // TODO these need to be WRAPPED with a function that sets overflow. + // future note to self, if these are more than 2 lines each, you're + // doing it wrong + INIT_INSTRUCTION(0b00, 0b00001, s3 = s1 + s2), + INIT_INSTRUCTION(0b00, 0b00010, s3 = s1 - s2), + + /* I type instructions */ + + /* J type instructions */ +}; +} // namespace instr -- 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/sim/instr.cc') 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 bc47d9131869b1f072e21d9cb61746d14bf30751 Mon Sep 17 00:00:00 2001 From: bd Date: Sat, 29 Mar 2025 02:36:38 -0400 Subject: get_instr_fields return mnemonic rather than opcode and type --- inc/definitions.h | 5 +++ inc/id.h | 33 +++++++++++++++---- inc/instr.h | 9 +++--- inc/stage.h | 4 +++ src/cli/cli.cc | 3 ++ src/sim/id.cc | 97 ++++++++++++++++++++++++++++++++----------------------- src/sim/instr.cc | 38 +++++++++++++--------- src/sim/stage.cc | 1 + tests/id.cc | 85 +++++++++++++++++++++--------------------------- 9 files changed, 160 insertions(+), 115 deletions(-) (limited to 'src/sim/instr.cc') diff --git a/inc/definitions.h b/inc/definitions.h index c9367ff..3238a8b 100644 --- a/inc/definitions.h +++ b/inc/definitions.h @@ -51,6 +51,11 @@ */ #define GPR_NUM 16 +/** + * The number of vector registers + */ +#define V_NUM 8 + /** * The number of bits to specify an instruction type */ diff --git a/inc/id.h b/inc/id.h index 1503457..2411402 100644 --- a/inc/id.h +++ b/inc/id.h @@ -1,5 +1,6 @@ #ifndef ID_H #define ID_H +#include "instr.h" #include "instrDTO.h" #include "response.h" #include "stage.h" @@ -19,20 +20,38 @@ class ID : public Stage /** * Parse an instruction into a type, opcode, and fields. If the type is * invalid, only the type field will be set. + * + * This method is marked public so it may be tested, and is not used outside + * of this class during normal execution. + * * @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. + * @param the resulting mnemonic. */ void get_instr_fields( - signed int &s1, - signed int &s2, - signed int &s3, - unsigned int &type, - unsigned int &opcode); + signed int &s1, signed int &s2, signed int &s3, Mnemonic &m); + + private: + /** + * Helper for `get_instr_fields`. + * Given a raw instruction, returns the mnemonic and type. + * This operation will destroy the original arguments. + * @param the raw bits to parse. + * @param the resulting mnemonic. + * @param the resulting type. + */ + void split_instr(signed int &raw, unsigned int &type, Mnemonic &m); + /** + * Facilitates checking out a register by dereferencing the register + specified by the first parameter. Registers that are checked out cannot be + checked out until they are checked in. + * @param + * @return the contents of the register. + */ + Response dereference_register(signed int &); }; #endif /* ID_D_INCLUDED */ diff --git a/inc/instr.h b/inc/instr.h index 98ecf1d..08b4fd0 100644 --- a/inc/instr.h +++ b/inc/instr.h @@ -2,7 +2,7 @@ #define INSTR_H #include #include -#include +#include enum Mnemonic { ADD, @@ -42,15 +42,14 @@ enum Mnemonic { BOF, PUSH, POP, + NOP, }; -std::ostream &operator<<(std::ostream &os, Mnemonic a); - namespace instr { // clang-format off - extern const std::map mnemonic_map; - extern const std::map> instr_map; + extern const std::unordered_map mnemonic_map; + extern const std::unordered_map> instr_map; // clang-format on } // namespace instr diff --git a/inc/stage.h b/inc/stage.h index 04de475..f6bad41 100644 --- a/inc/stage.h +++ b/inc/stage.h @@ -35,6 +35,10 @@ class Stage * The shared pool of general-purpose integer registers. */ static std::array gprs; + /** + * The shared pool of general-purpose vector registers. + */ + static std::array vrs; /** * The address of the currently executing instruction. */ diff --git a/src/cli/cli.cc b/src/cli/cli.cc index 022b266..58575cb 100644 --- a/src/cli/cli.cc +++ b/src/cli/cli.cc @@ -3,6 +3,7 @@ #include "definitions.h" #include "dram.h" #include "response.h" +#include "accessor.h" #include "utils.h" #include @@ -43,6 +44,7 @@ Cli::Cli() }; commands['r'] = [this](std::vector args) { + (void)args; reset(); return; }; @@ -61,6 +63,7 @@ Cli::Cli() }; commands['h'] = [this](std::vector args) { + (void)args; help(); return; }; diff --git a/src/sim/id.cc b/src/sim/id.cc index 65acef5..c6e42d5 100644 --- a/src/sim/id.cc +++ b/src/sim/id.cc @@ -1,5 +1,6 @@ #include "id.h" #include "accessor.h" +#include "instr.h" #include "instrDTO.h" #include "logger.h" #include "response.h" @@ -12,54 +13,70 @@ ID::ID(Stage *stage) : Stage(stage) { this->id = DCDE; } Response ID::advance(InstrDTO &i) { Response r; } +// TODO this function is ugly void ID::get_instr_fields( - signed int &s1, - signed int &s2, - signed int &s3, - unsigned int &type, - unsigned int &opcode) + signed int &s1, signed int &s2, signed int &s3, Mnemonic &m) { - // unsigned int &opcode; - int opcode_bits; - - type = GET_LS_BITS(s1, TYPE_SIZE); - opcode_bits = (type == 0b0) ? R_OPCODE_SIZE : OPCODE_SIZE; + unsigned int type, s0b, s1b, s2b; + this->split_instr(s1, type, m); + // define the parsing bounds + s0b = REG_SIZE; switch (type) { - case 0: + case 0b00: // 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); + s1b = s0b + REG_SIZE; + s2b = s1b + REG_SIZE; break; - case 1: + case 0b01: // 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); + s1b = s0b + REG_SIZE; + s2b = WORD_SPEC; break; - case 2: + case 0b10: // 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)); + s1b = WORD_SPEC; } + + if (type != 0b10) + s3 = GET_MID_BITS(s1, s1b, s2b); + + s2 = GET_MID_BITS(s1, s0b, s1b); + s1 = GET_LS_BITS(s1, s0b); +} + +void ID::split_instr(signed int &raw, unsigned int &type, Mnemonic &m) +{ + unsigned int opcode, opcode_size; + + type = GET_LS_BITS(raw, TYPE_SIZE); + 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); + } 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/instr.cc b/src/sim/instr.cc index 50aa71d..08edf5e 100644 --- a/src/sim/instr.cc +++ b/src/sim/instr.cc @@ -1,6 +1,7 @@ #include "instr.h" #include #include +#include // clang-format off #define INIT_INSTRUCTION(mnemonic, body) \ @@ -12,7 +13,7 @@ namespace instr { // clang-format off -const std::map> +const std::unordered_map> // clang-format on instr_map = { @@ -20,26 +21,33 @@ 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}, +const std::unordered_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}, {0b0001101, ADDI}, {0b0010001, SUBI}, + {0b0010101, SFTRI}, {0b0011101, SFTLI}, {0b0100001, ANDI}, + {0b0100101, ORI}, {0b0101001, XORI}, {0b0101101, STORE}, + {0b0110001, STOREV}, {0b0000101, CEV}, {0b0000101, LOAD}, + {0b0001001, LOADV}, {0b0001001, LOADV}, {0b0000110, JMP}, + {0b0001010, JRL}, {0b0001110, JAL}, {0b0010010, BEQ}, + {0b0010110, BGT}, {0b0011010, BUF}, {0b0011110, BOF}, + {0b0100010, PUSH}, {0b0100110, POP}, }; } // namespace instr diff --git a/src/sim/stage.cc b/src/sim/stage.cc index 6599298..560cc41 100644 --- a/src/sim/stage.cc +++ b/src/sim/stage.cc @@ -5,6 +5,7 @@ Stage::Stage(Stage *next) { } std::array Stage::gprs; +std::array Stage::vrs; unsigned int Stage::pc; Storage *Stage::storage; bool Stage::is_pipelined; diff --git a/tests/id.cc b/tests/id.cc index e09276f..b847026 100644 --- a/tests/id.cc +++ b/tests/id.cc @@ -4,6 +4,7 @@ #include "dram.h" #include "if.h" #include "instrDTO.h" +#include "instr.h" #include class IDPipeFixture @@ -49,110 +50,98 @@ class IDPipeFixture Controller *ct; }; -TEST_CASE_METHOD(IDPipeFixture, "Parse invalid type", "[utils]") +TEST_CASE_METHOD(IDPipeFixture, "Parse invalid type", "[id]") { signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; + Mnemonic m; s1 = 0x00FF00FF; - this->d->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); + this->d->get_instr_fields(s1, s2, s3, m); + CHECK(m == NOP); } -TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary r-type # one", "[utils]") +TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary r-type # one", "[id]") { signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; + Mnemonic m; - s1 = 0xCCCCCCCC; - this->d->get_instr_fields(s1, s2, s3, type, opcode); + s1 = 0xCCCCCC0C; + this->d->get_instr_fields(s1, s2, s3, m); - REQUIRE(type == 0b00); - CHECK(s1 == 0b11001); + CHECK(s1 == 0b11000); CHECK(s2 == 0b01100); CHECK(s3 == 0b00110); - CHECK(opcode == 0b10011); + CHECK(m == MUL); } -TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary r-type # two", "[utils]") +TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary r-type # two", "[id]") { signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; + Mnemonic m; - s1 = 0x99AABBCC; - this->d->get_instr_fields(s1, s2, s3, type, opcode); + s1 = 0x99AABB0C; + this->d->get_instr_fields(s1, s2, s3, m); - REQUIRE(type == 0b00); - CHECK(s1 == 0b10111); + CHECK(s1 == 0b10110); CHECK(s2 == 0b01011); CHECK(s3 == 0b10101); - CHECK(opcode == 0b10011); + CHECK(m == MUL); } -TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary i-type # one", "[utils]") +TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary i-type # one", "[id]") { signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; + Mnemonic m; s1 = 0xDDDDDDDD; - this->d->get_instr_fields(s1, s2, s3, type, opcode); + this->d->get_instr_fields(s1, s2, s3, m); - REQUIRE(type == 0b01); CHECK(s1 == 0b10111); CHECK(s2 == 0b11011); CHECK(s3 == 0xDDDD); - CHECK(opcode == 0b0111); + CHECK(m == SFTLI); } -TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary i-type # two", "[utils]") +TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary i-type # two", "[id]") { signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; + Mnemonic m; s1 = 0xAABBCCDD; - this->d->get_instr_fields(s1, s2, s3, type, opcode); + this->d->get_instr_fields(s1, s2, s3, m); - REQUIRE(type == 0b01); CHECK(s1 == 0b10011); CHECK(s2 == 0b11001); CHECK(s3 == 0xAABB); - CHECK(opcode == 0b0111); + CHECK(m == SFTLI); } -TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary j-type # one", "[utils]") +TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary j-type # one", "[id]") { signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; + Mnemonic m; - s1 = 0xEEEEEEEE; - this->d->get_instr_fields(s1, s2, s3, type, opcode); + s1 = 0xEEEEEE1E; + this->d->get_instr_fields(s1, s2, s3, m); - REQUIRE(type == 0b10); - CHECK(s1 == 0b11011); + CHECK(s1 == 0b11000); CHECK(s2 == 0b111011101110111011101); - CHECK(opcode == 0b1011); + CHECK(m == BOF); // behavior does nothing CHECK(s3 == 0b0); } -TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary j-type # two", "[utils]") +TEST_CASE_METHOD(IDPipeFixture, "Parse arbitrary j-type # two", "[id]") { signed int s1 = 0, s2 = 0, s3 = 0; - unsigned int type = 0, opcode = 0; + Mnemonic m; - s1 = 0xBBCCDDEE; - this->d->get_instr_fields(s1, s2, s3, type, opcode); + s1 = 0xBBCCDD0E; + this->d->get_instr_fields(s1, s2, s3, m); - REQUIRE(type == 0b10); - CHECK(s1 == 0b10111); + CHECK(s1 == 0b10100); CHECK(s2 == 0b101110111100110011011); - CHECK(opcode == 0b1011); + CHECK(m == JAL); // behavior does nothing CHECK(s3 == 0b0); } -- cgit v1.2.3