Skip to content

Commit

Permalink
added LLVM, ready to build a C Compiler for Sysdarft (SDCC - SysDarft…
Browse files Browse the repository at this point in the history
…CCompiler)
  • Loading branch information
Anivice committed Feb 3, 2025
1 parent 50550a6 commit cd99256
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Sysdarft
![Lines of Code](https://img.shields.io/badge/ProjectLines-44649-cyan)
![Size of Code](https://img.shields.io/badge/ProjectSize-2044%20K-yellow)
![Lines of Code](https://img.shields.io/badge/ProjectLines-34055919-cyan)
![Size of Code](https://img.shields.io/badge/ProjectSize-1944572%20K-yellow)
[![Automated CMake Test Workflow](https://github.com/Anivice/Sysdarft/actions/workflows/CMake_GitHub_Action.yml/badge.svg)](https://github.com/Anivice/Sysdarft/actions/workflows/CMake_GitHub_Action.yml)

<!--
Expand Down
33 changes: 25 additions & 8 deletions src/SysdarftConsole/debugger_operand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,23 @@ int64_t convert_to_64bit_signed(const Type data)
return result;
}

template <typename Type, unsigned BitWidth = sizeof(Type) * 8>
bool check_msb(const Type data)
{
// Ensure the data is treated as unsigned to avoid sign extension issues
using UnsignedType = std::make_unsigned_t<Type>;
auto udata = static_cast<UnsignedType>(data);

// Ensure that the BitWidth is not greater than the size of the type
static_assert(BitWidth <= sizeof(UnsignedType) * 8, "BitWidth exceeds the size of the type.");

// Get the most significant bit (MSB) position
auto msb_off = BitWidth - 1; // MSB position based on BitWidth
auto msb = (udata >> msb_off) & 1; // Isolate the MSB

return msb == 1; // Return true if MSB is 1, false otherwise
}

void debugger_operand_type::do_decode_memory_without_prefix()
{
const auto WidthBCD = pop_code8();
Expand Down Expand Up @@ -227,10 +244,10 @@ void debugger_operand_type::do_decode_memory_without_prefix()
literal3 = OperandReferenceTable.literal;
const auto val = do_access_register_based_on_table();
switch (OperandReferenceTable.OperandInfo.RegisterValue.RegisterWidthBCD) {
case _8bit_prefix: off2 = convert_to_64bit_signed(*(uint8_t*)&val); break;
case _16bit_prefix: off2 = convert_to_64bit_signed(*(uint16_t*)&val); break;
case _32bit_prefix: off2 = convert_to_64bit_signed(*(uint32_t*)&val); break;
case _64bit_prefix: off2 = convert_to_64bit_signed(*(uint64_t*)&val); break;
case _8bit_prefix: off2 = check_msb(*(uint8_t*)&val) ? convert_to_64bit_signed(*(uint8_t*)&val) : static_cast<int64_t>(val); break;
case _16bit_prefix: off2 = check_msb(*(uint16_t*)&val) ? convert_to_64bit_signed(*(uint16_t*)&val) : static_cast<int64_t>(val); break;
case _32bit_prefix: off2 = check_msb(*(uint32_t*)&val) ? convert_to_64bit_signed(*(uint32_t*)&val) : static_cast<int64_t>(val); break;
case _64bit_prefix: off2 = check_msb(*(uint64_t*)&val) ? convert_to_64bit_signed(*(uint64_t*)&val) : static_cast<int64_t>(val); break;
default: throw IllegalInstruction("Unknown register width");
}
break;
Expand All @@ -239,10 +256,10 @@ void debugger_operand_type::do_decode_memory_without_prefix()
do_decode_constant_without_prefix();
const auto val = OperandReferenceTable.OperandInfo.ConstantValue;
switch (OperandReferenceTable.OperandInfo.ConstantWidth) {
case _8bit_prefix: off2 = convert_to_64bit_signed(*(uint8_t*)&val); break;
case _16bit_prefix: off2 = convert_to_64bit_signed(*(uint16_t*)&val); break;
case _32bit_prefix: off2 = convert_to_64bit_signed(*(uint32_t*)&val); break;
case _64bit_prefix: off2 = convert_to_64bit_signed(*(uint64_t*)&val); break;
case _8bit_prefix: off2 = check_msb(*(uint8_t*)&val) ? convert_to_64bit_signed(*(uint8_t*)&val) : static_cast<int64_t>(val); break;
case _16bit_prefix: off2 = check_msb(*(uint16_t*)&val) ? convert_to_64bit_signed(*(uint16_t*)&val) : static_cast<int64_t>(val); break;
case _32bit_prefix: off2 = check_msb(*(uint32_t*)&val) ? convert_to_64bit_signed(*(uint32_t*)&val) : static_cast<int64_t>(val); break;
case _64bit_prefix: off2 = check_msb(*(uint64_t*)&val) ? convert_to_64bit_signed(*(uint64_t*)&val) : static_cast<int64_t>(val); break;
default: throw IllegalInstruction("Unknown register width");
}
literal3 = std::to_string(off2);
Expand Down
33 changes: 25 additions & 8 deletions src/cpu/SysdarftCPUDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ void OperandType::do_decode_constant_without_prefix()
#endif // __DEBUG__
}

template <typename Type, unsigned BitWidth = sizeof(Type) * 8>
bool check_msb(const Type data)
{
// Ensure the data is treated as unsigned to avoid sign extension issues
using UnsignedType = std::make_unsigned_t<Type>;
auto udata = static_cast<UnsignedType>(data);

// Ensure that the BitWidth is not greater than the size of the type
static_assert(BitWidth <= sizeof(UnsignedType) * 8, "BitWidth exceeds the size of the type.");

// Get the most significant bit (MSB) position
auto msb_off = BitWidth - 1; // MSB position based on BitWidth
auto msb = (udata >> msb_off) & 1; // Isolate the MSB

return msb == 1; // Return true if MSB is 1, false otherwise
}

template < typename Type, unsigned BitWidth = sizeof(Type) * 8 >
int64_t convert_to_64bit_signed(const Type data)
{
Expand Down Expand Up @@ -234,10 +251,10 @@ void OperandType::do_decode_memory_without_prefix()
#endif
const auto val = do_access_register_based_on_table();
switch (OperandReferenceTable.OperandInfo.RegisterValue.RegisterWidthBCD) {
case _8bit_prefix: off2 = convert_to_64bit_signed(*(uint8_t*)&val); break;
case _16bit_prefix: off2 = convert_to_64bit_signed(*(uint16_t*)&val); break;
case _32bit_prefix: off2 = convert_to_64bit_signed(*(uint32_t*)&val); break;
case _64bit_prefix: off2 = convert_to_64bit_signed(*(uint64_t*)&val); break;
case _8bit_prefix: off2 = check_msb(*(uint8_t*)&val) ? convert_to_64bit_signed(*(uint8_t*)&val) : static_cast<int64_t>(val); break;
case _16bit_prefix: off2 = check_msb(*(uint16_t*)&val) ? convert_to_64bit_signed(*(uint16_t*)&val) : static_cast<int64_t>(val); break;
case _32bit_prefix: off2 = check_msb(*(uint32_t*)&val) ? convert_to_64bit_signed(*(uint32_t*)&val) : static_cast<int64_t>(val); break;
case _64bit_prefix: off2 = check_msb(*(uint64_t*)&val) ? convert_to_64bit_signed(*(uint64_t*)&val) : static_cast<int64_t>(val); break;
default: throw IllegalInstruction("Unknown register width");
}
break;
Expand All @@ -246,10 +263,10 @@ void OperandType::do_decode_memory_without_prefix()
do_decode_constant_without_prefix();
const auto val = OperandReferenceTable.OperandInfo.ConstantValue;
switch (OperandReferenceTable.OperandInfo.ConstantWidth) {
case _8bit_prefix: off2 = convert_to_64bit_signed(*(uint8_t*)&val); break;
case _16bit_prefix: off2 = convert_to_64bit_signed(*(uint16_t*)&val); break;
case _32bit_prefix: off2 = convert_to_64bit_signed(*(uint32_t*)&val); break;
case _64bit_prefix: off2 = convert_to_64bit_signed(*(uint64_t*)&val); break;
case _8bit_prefix: off2 = check_msb(*(uint8_t*)&val) ? convert_to_64bit_signed(*(uint8_t*)&val) : static_cast<int64_t>(val); break;
case _16bit_prefix: off2 = check_msb(*(uint16_t*)&val) ? convert_to_64bit_signed(*(uint16_t*)&val) : static_cast<int64_t>(val); break;
case _32bit_prefix: off2 = check_msb(*(uint32_t*)&val) ? convert_to_64bit_signed(*(uint32_t*)&val) : static_cast<int64_t>(val); break;
case _64bit_prefix: off2 = check_msb(*(uint64_t*)&val) ? convert_to_64bit_signed(*(uint64_t*)&val) : static_cast<int64_t>(val); break;
default: throw IllegalInstruction("Unknown register width");
}
#ifdef __DEBUG__
Expand Down

0 comments on commit cd99256

Please sign in to comment.