mirror of
https://github.com/RetroDECK/Supermodel.git
synced 2025-02-16 17:35:39 +00:00
Added ToHexString() and made ostream serialization use this
This commit is contained in:
parent
e12a945ddd
commit
86631695d7
|
@ -208,6 +208,39 @@ namespace Util
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string BitRegister::ToHexString() const
|
||||||
|
{
|
||||||
|
const char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||||
|
if (Empty())
|
||||||
|
return std::string("");
|
||||||
|
size_t partial = Size() & 3;
|
||||||
|
size_t num_digits = Size() / 4 + (partial != 0 ? 1 : 0);
|
||||||
|
std::string out(num_digits + 2, '0');
|
||||||
|
out[0] = '0';
|
||||||
|
out[1] = 'x';
|
||||||
|
size_t idx = 0;
|
||||||
|
size_t digit_idx = 2;
|
||||||
|
size_t digit = 0;
|
||||||
|
if (partial != 0)
|
||||||
|
{
|
||||||
|
for (idx = 0; idx < partial; idx++)
|
||||||
|
{
|
||||||
|
digit <<= 1;
|
||||||
|
digit |= m_bits[idx];
|
||||||
|
}
|
||||||
|
out[digit_idx++] = digits[digit];
|
||||||
|
}
|
||||||
|
while (idx < Size())
|
||||||
|
{
|
||||||
|
digit = m_bits[idx++] << 3;
|
||||||
|
digit |= m_bits[idx++] << 2;
|
||||||
|
digit |= m_bits[idx++] << 1;
|
||||||
|
digit |= m_bits[idx++] << 0;
|
||||||
|
out[digit_idx++] = digits[digit];
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, const BitRegister ®)
|
std::ostream &operator<<(std::ostream &os, const BitRegister ®)
|
||||||
{
|
{
|
||||||
if (reg.Empty())
|
if (reg.Empty())
|
||||||
|
@ -215,14 +248,7 @@ namespace Util
|
||||||
os << "[ empty ]";
|
os << "[ empty ]";
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
char *buf = new char[reg.Size() + 1];
|
os << "[ " << reg.Size() << ": " << reg.ToHexString() << " ]";
|
||||||
buf[reg.Size()] = 0;
|
|
||||||
for (size_t i = 0; i < reg.Size(); i++)
|
|
||||||
{
|
|
||||||
buf[i] = reg.m_bits[i] == 0 ? '0' : '1';
|
|
||||||
}
|
|
||||||
os << "[ " << reg.Size() << ": " << buf << " ]";
|
|
||||||
delete [] buf;
|
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
} // Util
|
} // Util
|
||||||
|
|
|
@ -41,11 +41,14 @@ namespace Util
|
||||||
void Set(const std::string &value);
|
void Set(const std::string &value);
|
||||||
void SetZeros(size_t count);
|
void SetZeros(size_t count);
|
||||||
void SetOnes(size_t count);
|
void SetOnes(size_t count);
|
||||||
void SetNoBitValue(uint8_t bit);
|
|
||||||
void Reset();
|
void Reset();
|
||||||
|
|
||||||
|
// Configuration state
|
||||||
|
void SetNoBitValue(uint8_t bit);
|
||||||
|
|
||||||
// String serialization
|
// String serialization
|
||||||
std::string ToBinaryString() const;
|
std::string ToBinaryString() const;
|
||||||
|
std::string ToHexString() const;
|
||||||
friend std::ostream &operator<<(std::ostream &os, const BitRegister ®);
|
friend std::ostream &operator<<(std::ostream &os, const BitRegister ®);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -8,7 +8,6 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
Util::BitRegister reg;
|
Util::BitRegister reg;
|
||||||
|
|
||||||
|
|
||||||
// Test
|
// Test
|
||||||
reg.SetZeros(1);
|
reg.SetZeros(1);
|
||||||
expected.push_back("0");
|
expected.push_back("0");
|
||||||
|
@ -209,7 +208,52 @@ int main(int argc, char **argv)
|
||||||
reg.Insert(29, "0110");
|
reg.Insert(29, "0110");
|
||||||
expected.push_back("00000000000000101001001000110011");
|
expected.push_back("00000000000000101001001000110011");
|
||||||
results.push_back(reg.ToBinaryString());
|
results.push_back(reg.ToBinaryString());
|
||||||
|
|
||||||
|
// Test hex printing
|
||||||
|
reg.SetOnes(1);
|
||||||
|
expected.push_back("0x1");
|
||||||
|
results.push_back(reg.ToHexString());
|
||||||
|
|
||||||
|
reg.SetZeros(1);
|
||||||
|
expected.push_back("0x0");
|
||||||
|
results.push_back(reg.ToHexString());
|
||||||
|
|
||||||
|
reg.SetZeros(2);
|
||||||
|
expected.push_back("0x0");
|
||||||
|
results.push_back(reg.ToHexString());
|
||||||
|
|
||||||
|
reg.SetZeros(3);
|
||||||
|
expected.push_back("0x0");
|
||||||
|
results.push_back(reg.ToHexString());
|
||||||
|
|
||||||
|
reg.SetZeros(4);
|
||||||
|
expected.push_back("0x0");
|
||||||
|
results.push_back(reg.ToHexString());
|
||||||
|
|
||||||
|
reg.SetZeros(5);
|
||||||
|
expected.push_back("0x00");
|
||||||
|
results.push_back(reg.ToHexString());
|
||||||
|
|
||||||
|
reg.Set("11001");
|
||||||
|
expected.push_back("0x19");
|
||||||
|
results.push_back(reg.ToHexString());
|
||||||
|
|
||||||
|
reg.Set("111001");
|
||||||
|
expected.push_back("0x39");
|
||||||
|
results.push_back(reg.ToHexString());
|
||||||
|
|
||||||
|
reg.Set("1111001");
|
||||||
|
expected.push_back("0x79");
|
||||||
|
results.push_back(reg.ToHexString());
|
||||||
|
|
||||||
|
reg.Set("01111001");
|
||||||
|
expected.push_back("0x79");
|
||||||
|
results.push_back(reg.ToHexString());
|
||||||
|
|
||||||
|
reg.Set("101111001");
|
||||||
|
expected.push_back("0x179");
|
||||||
|
results.push_back(reg.ToHexString());
|
||||||
|
|
||||||
// Check results
|
// Check results
|
||||||
size_t num_failed = 0;
|
size_t num_failed = 0;
|
||||||
for (size_t i = 0; i < expected.size(); i++)
|
for (size_t i = 0; i < expected.size(); i++)
|
||||||
|
|
Loading…
Reference in a new issue