Skip to content

Instantly share code, notes, and snippets.

@marethyu
Last active August 2, 2022 01:19
Show Gist options
  • Save marethyu/ad745f5e37e9b3943c62c35ea5cd8b01 to your computer and use it in GitHub Desktop.
Save marethyu/ad745f5e37e9b3943c62c35ea5cd8b01 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <bitset>
#include <cstdint>
#include <array>
using BYTE = uint8_t;
struct HALFWORD
{
union
{
struct
{
BYTE byte1; // lowest
BYTE byte2; // highest
};
uint16_t raw;
};
};
struct WORD
{
union
{
struct
{
BYTE byte1; // lowest
BYTE byte2;
BYTE byte3;
BYTE byte4; // highest
};
uint32_t raw;
};
};
struct CPSR
{
union
{
struct
{
uint32_t mode: 5;
uint32_t thumbState : 1;
uint32_t fiqDisable : 1;
uint32_t irqDisable : 1;
uint32_t unused : 20;
uint32_t V : 1;
uint32_t C : 1;
uint32_t Z : 1;
uint32_t N : 1;
};
WORD w;
};
};
struct Foo
{
CPSR cpsr;
};
struct Bar
{
std::array<BYTE*, 4> io;
Bar(Foo& foo)
{
io[0] = &foo.cpsr.w.byte1;
io[1] = &foo.cpsr.w.byte2;
io[2] = &foo.cpsr.w.byte3;
io[3] = &foo.cpsr.w.byte4;
}
BYTE get(int i)
{
return *io[i];
}
void set(int i, BYTE b)
{
*io[i] = b;
}
};
int main()
{
Foo foo;
foo.cpsr.w.raw = 0xA000001F;
std::cout << std::bitset<32>(foo.cpsr.w.raw).to_string() << '\n';
std::cout << std::bitset<5>(foo.cpsr.mode).to_string() << '\n';
std::cout << std::bitset<8>(foo.cpsr.w.byte1).to_string() << '\n';
std::cout << std::bitset<8>(foo.cpsr.w.byte2).to_string() << '\n';
std::cout << std::bitset<8>(foo.cpsr.w.byte3).to_string() << '\n';
std::cout << std::bitset<8>(foo.cpsr.w.byte4).to_string() << '\n';
foo.cpsr.w.byte3 = 43;
std::cout << std::bitset<32>(foo.cpsr.w.raw).to_string() << '\n';
Bar bar(foo);
bar.set(0, 0b000101);
bar.set(3, 0xFF);
std::cout << std::bitset<32>(foo.cpsr.w.raw).to_string() << '\n';
std::cout << std::bitset<5>(foo.cpsr.mode).to_string() << '\n';
std::cout << std::bitset<8>(foo.cpsr.w.byte1).to_string() << '\n';
std::cout << std::bitset<8>(foo.cpsr.w.byte2).to_string() << '\n';
std::cout << std::bitset<8>(foo.cpsr.w.byte3).to_string() << '\n';
std::cout << std::bitset<8>(foo.cpsr.w.byte4).to_string() << '\n';
std::cout << std::bitset<8>(bar.get(1)).to_string() << '\n';
foo.cpsr.w.byte2 = 0xFF;
std::cout << "fuck\n";
std::cout << std::bitset<8>(bar.get(0)).to_string() << '\n';
std::cout << std::bitset<8>(bar.get(1)).to_string() << '\n';
std::cout << std::bitset<8>(bar.get(2)).to_string() << '\n';
std::cout << std::bitset<8>(bar.get(3)).to_string() << '\n';
foo.cpsr.w.byte1 = foo.cpsr.w.byte3 = foo.cpsr.w.byte4 = 0xFF;
std::cout << "fuck\n";
std::cout << std::bitset<8>(bar.get(0)).to_string() << '\n';
std::cout << std::bitset<8>(bar.get(1)).to_string() << '\n';
std::cout << std::bitset<8>(bar.get(2)).to_string() << '\n';
std::cout << std::bitset<8>(bar.get(3)).to_string() << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment