Initial test bootstrap.

This commit is contained in:
jmsgrogan 2023-12-18 10:16:31 +00:00
parent 6c618749f1
commit 4b308f6c32
94 changed files with 2543 additions and 681 deletions

View file

@ -2,15 +2,12 @@
#include "ByteUtils.h"
#include <sstream>
#include <iostream>
BitStream::~BitStream()
{
}
unsigned char BitStream::getCurrentByte()
Byte BitStream::getCurrentByte()
{
if (mByteOffset < 0)
{
@ -19,19 +16,18 @@ unsigned char BitStream::getCurrentByte()
return mCurrentByte;
}
void BitStream::write(uint32_t data)
void BitStream::write(DWord data)
{
unsigned num_bytes = sizeof(uint32_t);
for(unsigned idx=0; idx<num_bytes;idx++)
for(std::size_t idx=0; idx<sizeof(DWord); idx++)
{
writeByte(ByteUtils::getByteN(data, idx));
}
}
void BitStream::writeWord(uint16_t data)
void BitStream::writeWord(Word data)
{
const auto byte0 = static_cast<unsigned char>(data >> 8);
const auto byte1 = static_cast<unsigned char>((data << 8) >> 8);
const auto byte0 = static_cast<Byte>(data >> 8);
const auto byte1 = static_cast<Byte>((data << 8) >> 8);
writeByte(byte0);
writeByte(byte1);
}
@ -41,24 +37,26 @@ int BitStream::getCurrentByteOffset() const
return mByteOffset;
}
unsigned BitStream::getCurrentBitOffset() const
std::size_t BitStream::getCurrentBitOffset() const
{
return mBitOffset;
}
std::string BitStream::logLocation()
String BitStream::logLocation()
{
std::stringstream sstr;
sstr << "Byte offset " << mByteOffset<< " | Bit offset " << mBitOffset;
sstr << " | Working byte " << ByteUtils::toString(getCurrentByte()) << '\n';
return sstr.str();
String ret;
ret << "Byte offset " << mByteOffset<< " | Bit offset " << mBitOffset;
ret << " | Working byte " << ByteUtils::toString(getCurrentByte()) << '\n';
return ret;
}
std::string BitStream::logNextNBytes(unsigned n) const
String BitStream::logNextNBytes(std::size_t n) const
{
std::stringstream sstr;
unsigned count{0};
for(auto byte : peekNextNBytes(n))
std::size_t count{0};
VecBytes bytes;
peekNextNBytes(n, bytes);
for(auto byte : bytes)
{
sstr << mByteOffset + count << " | " << ByteUtils::toString(byte) + '\n';
count++;
@ -66,28 +64,28 @@ std::string BitStream::logNextNBytes(unsigned n) const
return sstr.str();
}
void BitStream::writeNBits(uint32_t data, unsigned length)
void BitStream::writeNBits(DWord data, std::size_t length)
{
const auto num_left = 8 - mBitOffset;
const int overshoot = length - num_left;
if (overshoot > 0)
{
unsigned char lower_bits = ByteUtils::getLowerNBits(data, num_left);
Byte lower_bits = ByteUtils::getLowerNBits(data, num_left);
mCurrentByte |= lower_bits << mBitOffset;
writeByte(mCurrentByte, false);
unsigned num_bytes = overshoot / 8;
for (unsigned idx=0; idx< num_bytes; idx++)
std::size_t num_bytes = overshoot / 8;
for (std::size_t idx=0; idx< num_bytes; idx++)
{
mCurrentByte = ByteUtils::getMBitsAtN(static_cast<unsigned char>(data), overshoot, idx*8 + num_left);
mCurrentByte = ByteUtils::getMBitsAtN(static_cast<Byte>(data), overshoot, idx*8 + num_left);
writeByte(mCurrentByte, false);
}
if (const auto remainder = overshoot % 8; remainder > 0)
{
mCurrentByte = ByteUtils::getMBitsAtN(static_cast<unsigned char>(data), remainder, num_bytes*8 + num_left);
mCurrentByte = ByteUtils::getMBitsAtN(static_cast<Byte>(data), remainder, num_bytes*8 + num_left);
mBitOffset = remainder;
}
else
@ -98,7 +96,7 @@ void BitStream::writeNBits(uint32_t data, unsigned length)
}
else
{
mCurrentByte |= (static_cast<unsigned char>(data) << mBitOffset);
mCurrentByte |= (static_cast<Byte>(data) << mBitOffset);
mBitOffset += length;
if (mBitOffset == 8)
{
@ -107,10 +105,9 @@ void BitStream::writeNBits(uint32_t data, unsigned length)
mBitOffset = 0;
}
}
}
bool BitStream::readNextNBits(unsigned n, unsigned char& buffer)
bool BitStream::readNextNBits(std::size_t n, Byte& buffer)
{
if (mByteOffset < 0)
{
@ -123,15 +120,15 @@ bool BitStream::readNextNBits(unsigned n, unsigned char& buffer)
int overshoot = n + mBitOffset - 8;
if (overshoot > 0)
{
unsigned char last_byte = mCurrentByte;
const auto last_byte = mCurrentByte;
if (!readNextByte())
{
return false;
}
auto num_lower = 8 - mBitOffset;
char lower_bits = ByteUtils::getHigherNBits(last_byte, num_lower);
char higher_bits = ByteUtils::getLowerNBits(mCurrentByte, overshoot);
const auto lower_bits = ByteUtils::getHigherNBits(last_byte, num_lower);
const auto higher_bits = ByteUtils::getLowerNBits(mCurrentByte, overshoot);
buffer = (higher_bits << num_lower) | lower_bits;
@ -145,3 +142,44 @@ bool BitStream::readNextNBits(unsigned n, unsigned char& buffer)
return true;
}
}
void BitStream::resetOffsets()
{
mEndByteOffset = mByteOffset;
mEndBitOffset = mBitOffset;
mEndByte = mCurrentByte;
mCurrentByte = 0;
mByteOffset = -1;
mBitOffset = 0;
}
void BitStream::reset()
{
resetOffsets();
mCurrentByte = 0;
}
void BitStream::flushRemainingBits()
{
if (mBitOffset > 0)
{
writeByte(mCurrentByte, false);
mBitOffset = 0;
}
}
std::pair<Byte, std::size_t> BitStream::getRemainingBits() const
{
return {mEndByte, mEndBitOffset};
}
void BitStream::setChecksumCalculator(AbstractChecksumCalculator* calc)
{
mChecksumCalculator = calc;
}
void BitStream::clearChecksumCalculator()
{
mChecksumCalculator = nullptr;
}