-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added implementation for Decode Stage
- Loading branch information
1 parent
5c17f7f
commit 25dc906
Showing
5 changed files
with
182 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "DecodeStage.hpp" | ||
#include <stdexcept> | ||
#include <cstdint> | ||
|
||
// Constructor | ||
DecodeStage::DecodeStage(RegisterBank& register_bank) | ||
: fetched_instruction(0), register_bank(register_bank), decoded_instruction(DecodedInstruction<InstructionFormat::INIVALID_TYPE>(0)){} | ||
|
||
// Process the fetched instruction and decode it | ||
void DecodeStage::process() { | ||
// Extract the opcode from the fetched instruction | ||
uint32_t opcode = DecodedInstructionBase(fetched_instruction).get_opcode(); | ||
|
||
InstructionFormat format = static_cast<InstructionFormat>(opcode); | ||
// Decode the instruction based on the opcode | ||
using enum InstructionFormat; | ||
|
||
switch (format) { | ||
case R_TYPE: | ||
decoded_instruction = DecodedInstruction<R_TYPE>(fetched_instruction); | ||
break; | ||
case I_TYPE: | ||
decoded_instruction = DecodedInstruction<I_TYPE>(fetched_instruction); | ||
break; | ||
case S_TYPE: | ||
decoded_instruction = DecodedInstruction<S_TYPE>(fetched_instruction); | ||
break; | ||
case B_TYPE: | ||
decoded_instruction = DecodedInstruction<B_TYPE>(fetched_instruction); | ||
break; | ||
case U_TYPE: | ||
decoded_instruction = DecodedInstruction<U_TYPE>(fetched_instruction); | ||
break; | ||
case J_TYPE: | ||
decoded_instruction = DecodedInstruction<J_TYPE>(fetched_instruction); | ||
break; | ||
default: | ||
throw std::invalid_argument("Unsupported instruction format"); | ||
} | ||
} | ||
|
||
// Get the decoded instruction (This is the output) | ||
const DecodedInstructionVariant& DecodeStage::get_decoded_instruction() const { | ||
return decoded_instruction; | ||
} | ||
|
||
// Set the fetched instruction (from FetchStage output) | ||
void DecodeStage::set_fetched_instruction(uint32_t instruction) { | ||
fetched_instruction = instruction; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <variant> | ||
#include "core/cpu/pipeline/PipelineStage.hpp" | ||
#include "core/cpu/register_bank/RegisterBank.hpp" | ||
#include "core/cpu/isa/Instruction.hpp" | ||
|
||
// Define the variant type to hold all possible instruction formats | ||
using DecodedInstructionVariant = std::variant< | ||
DecodedInstruction<InstructionFormat::INIVALID_TYPE>, | ||
DecodedInstruction<InstructionFormat::R_TYPE>, | ||
DecodedInstruction<InstructionFormat::I_TYPE>, | ||
DecodedInstruction<InstructionFormat::S_TYPE>, | ||
DecodedInstruction<InstructionFormat::B_TYPE>, | ||
DecodedInstruction<InstructionFormat::U_TYPE>, | ||
DecodedInstruction<InstructionFormat::J_TYPE> | ||
>; | ||
|
||
class DecodeStage : public PipelineStage { | ||
private: | ||
uint32_t fetched_instruction; // Instruction fetched in FetchStage | ||
RegisterBank& register_bank; // Reference to the register bank | ||
DecodedInstructionVariant decoded_instruction; // Decoded instruction | ||
|
||
public: | ||
DecodeStage(RegisterBank& register_bank); | ||
|
||
void process() override; | ||
|
||
const DecodedInstructionVariant& get_decoded_instruction() const; | ||
|
||
void set_fetched_instruction(uint32_t fetched_instruction); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "FetchStage.hpp" | ||
|
||
FetchStage::FetchStage(Memory& memory, uint32_t& program_counter) | ||
: memory(memory), program_counter(program_counter), fetched_instruction(0) {} | ||
|
||
void FetchStage::process() { | ||
// Fetch the instruction from memory at the current program counter | ||
fetched_instruction = memory.read(program_counter); | ||
|
||
// Increment the program counter to point to the next instruction | ||
program_counter += 4; | ||
} | ||
|
||
uint32_t FetchStage::get_fetched_instruction() { | ||
return fetched_instruction; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
#pragma once | ||
#include <cstdint> | ||
#include "core/cpu/pipeline/PipelineStage.hpp" | ||
#include "core/memory/Memory.hpp" | ||
#include "core/cpu/register_bank/RegisterBank.hpp" | ||
|
||
class FetchStage : public PipelineStage { | ||
private: | ||
Memory* memory; | ||
RegisterBank* registers; | ||
Memory& memory; | ||
uint32_t& program_counter; | ||
uint32_t fetched_instruction; | ||
|
||
public: | ||
FetchStage(Memory* memory, RegisterBank* registers); | ||
FetchStage(Memory& memory, uint32_t& program_counter); | ||
void process() override; | ||
uint32_t get_fetched_instruction(); | ||
}; |