[UNTESTED] refactor how instructions are parsed

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2023-09-14 10:12:30 +05:30
parent 3cf5cbd024
commit 0b674c7c64
11 changed files with 742 additions and 389 deletions

View File

@@ -1,4 +1,6 @@
#include "utility.hh"
#include "util/bits.hh"
#include <bit>
std::ostream&
operator<<(std::ostream& os, const Condition cond) {
@@ -33,6 +35,47 @@ operator<<(std::ostream& os, const Condition cond) {
return os;
}
uint32_t
eval_shift(ShiftType shift_type, uint32_t value, uint8_t amount, bool& carry) {
switch (shift_type) {
case ShiftType::LSL:
if (amount > 0 && amount <= 32)
carry = get_nth_bit(value, 32 - amount);
else if (amount > 32)
carry = 0;
return value << amount;
case ShiftType::LSR:
if (amount > 0 && amount <= 32)
carry = get_nth_bit(value, amount - 1);
else if (amount > 32)
carry = 0;
else
carry = get_nth_bit(value, 31);
return value >> amount;
case ShiftType::ASR:
if (amount > 0 && amount <= 32)
carry = get_nth_bit(value, amount - 1);
else
carry = get_nth_bit(value, 31);
return static_cast<int32_t>(value) >> amount;
case ShiftType::ROR:
if (amount == 0) {
bool old_carry = carry;
carry = get_nth_bit(value, 0);
return (value >> 1) | (old_carry << 31);
} else {
carry = get_nth_bit(value, (amount % 32 + 31) % 32);
return std::rotr(value, amount);
}
}
}
std::ostream&
operator<<(std::ostream& os, const ShiftType shift_type) {