fix bit padding

This commit is contained in:
2024-12-13 16:35:05 +01:00
parent 9f1d80cd9e
commit ada82368d0

View File

@@ -51,6 +51,17 @@ static std::map<std::string, Instruction> INSTRUCTION_KEYS = {
{"ret", {SautControle, Ret}}, {"ret", {SautControle, Ret}},
}; };
constexpr int LINE_LENGTH = 32;
constexpr int INSTRUCTION_BITS_COUNT = 2;
constexpr int OPERATION_BITS_COUNT = 2;
constexpr int IMMEDIATE_BITS_COUNT = 1;
constexpr int INSTRUCTION_BLOCK_SIZE = INSTRUCTION_BITS_COUNT + OPERATION_BITS_COUNT + IMMEDIATE_BITS_COUNT;
constexpr int REGISTRY_BITS_COUNT = 3;
static constexpr int GetOffset(int a_Start, int a_BlockSize) {
return LINE_LENGTH - a_Start - a_BlockSize;
}
std::uint32_t Assembleur::ParseLabel(const std::string& a_Label) { std::uint32_t Assembleur::ParseLabel(const std::string& a_Label) {
auto it = m_Labels.find(a_Label); auto it = m_Labels.find(a_Label);
@@ -65,34 +76,52 @@ void Assembleur::AddLabel(const std::string& a_Label, std::uint32_t a_Line) {
m_Labels.insert({a_Label, a_Line}); m_Labels.insert({a_Label, a_Line});
} }
// 2 bits type instruction | 3 bits sous-type opération
std::uint32_t Assembleur::IToInt(Instruction a_Instruction) { std::uint32_t Assembleur::IToInt(Instruction a_Instruction) {
return static_cast<std::uint32_t>(a_Instruction.m_Instruction) << 30 | static_cast<std::uint32_t>(a_Instruction.m_SubInstruction) return static_cast<std::uint32_t>(a_Instruction.m_Instruction) << GetOffset(0, INSTRUCTION_BITS_COUNT) |
<< 28; static_cast<std::uint32_t>(a_Instruction.m_SubInstruction) << GetOffset(INSTRUCTION_BITS_COUNT, OPERATION_BITS_COUNT);
} }
// 5 bits instruction | 1 bit immédiat (non utilisé) | 3 bits R1 | 3 bits R2 | 3 bits R3
std::uint32_t Assembleur::ParseOperation(Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2, std::uint32_t a_R3) { std::uint32_t Assembleur::ParseOperation(Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2, std::uint32_t a_R3) {
return IToInt(a_Instruction) | a_R1 << 23 | a_R2 << 20 | a_R3 << 17; return IToInt(a_Instruction) |
a_R1 << GetOffset(INSTRUCTION_BLOCK_SIZE, REGISTRY_BITS_COUNT) |
a_R2 << GetOffset(INSTRUCTION_BLOCK_SIZE + REGISTRY_BITS_COUNT, REGISTRY_BITS_COUNT) |
a_R3 << GetOffset(INSTRUCTION_BLOCK_SIZE + 2 * REGISTRY_BITS_COUNT, REGISTRY_BITS_COUNT);
} }
// 5 bits instruction | 1 bit immédiat | 3 bits R1 | 3 bits R2 | 20 bits constante
std::uint32_t Assembleur::ParseOperationImmediate( std::uint32_t Assembleur::ParseOperationImmediate(
Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2, std::uint32_t a_C1) { Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2, std::uint32_t a_C1) {
return IToInt(a_Instruction) | 1 << 26 | a_R1 << 23 | a_R2 << 20 | a_C1; return IToInt(a_Instruction) | 1 << GetOffset(INSTRUCTION_BITS_COUNT + OPERATION_BITS_COUNT, IMMEDIATE_BITS_COUNT) |
a_R1 << GetOffset(INSTRUCTION_BLOCK_SIZE, REGISTRY_BITS_COUNT) |
a_R2 << GetOffset(INSTRUCTION_BLOCK_SIZE + REGISTRY_BITS_COUNT, REGISTRY_BITS_COUNT) |
a_C1;
} }
// 5 bits instruction | 1 bit immédiat (non utilisé) | 26 bits adresse du label
std::uint32_t Assembleur::ParseJump(Instruction a_Instruction, const std::string& a_Label) { std::uint32_t Assembleur::ParseJump(Instruction a_Instruction, const std::string& a_Label) {
return IToInt(a_Instruction) | ParseLabel(a_Label) & 0x3FFFFFF; return IToInt(a_Instruction) | ParseLabel(a_Label) & 0x3FFFFFF;
} }
// 5 bits instruction | 1 bit immédiat (non utilisé) | 3 bits R1 | 3 bits R2 | 20 bits adresse du label
std::uint32_t Assembleur::ParseJump(Instruction a_Instruction, std::uint8_t a_R1, std::uint8_t a_R2, const std::string& a_Label) { std::uint32_t Assembleur::ParseJump(Instruction a_Instruction, std::uint8_t a_R1, std::uint8_t a_R2, const std::string& a_Label) {
return IToInt(a_Instruction) | a_R1 << 23 | a_R2 << 20 | ParseLabel(a_Label) & 0xFFFFF; return IToInt(a_Instruction) |
a_R1 << GetOffset(INSTRUCTION_BLOCK_SIZE, REGISTRY_BITS_COUNT) |
a_R2 << GetOffset(INSTRUCTION_BLOCK_SIZE + REGISTRY_BITS_COUNT, REGISTRY_BITS_COUNT) |
ParseLabel(a_Label) & 0xFFFFF;
} }
// 5 bits instruction | 1 bit immédiat (non utilisé)
std::uint32_t Assembleur::ParseJump(Instruction a_Instruction) { std::uint32_t Assembleur::ParseJump(Instruction a_Instruction) {
return IToInt(a_Instruction); return IToInt(a_Instruction);
} }
// 5 bits instruction | 1 bit immédiat (non utilisé) | 3 bits R1 | 3 bits R2
std::uint32_t Assembleur::ParseIO(Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2) { std::uint32_t Assembleur::ParseIO(Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2) {
return IToInt(a_Instruction) | a_R1 << 23 | a_R2 << 20; return IToInt(a_Instruction) |
a_R1 << GetOffset(INSTRUCTION_BLOCK_SIZE, REGISTRY_BITS_COUNT) |
a_R2 << GetOffset(INSTRUCTION_BLOCK_SIZE + REGISTRY_BITS_COUNT, REGISTRY_BITS_COUNT);
} }