2016-01-07 08:14:33 +00:00
|
|
|
#pragma once
|
2011-12-12 10:59:53 +00:00
|
|
|
|
2016-01-07 08:14:33 +00:00
|
|
|
namespace nall { namespace mosaic {
|
2011-12-12 10:59:53 +00:00
|
|
|
|
|
|
|
struct bitstream {
|
2015-12-14 09:41:06 +00:00
|
|
|
~bitstream() {
|
|
|
|
close();
|
|
|
|
}
|
2011-12-12 10:59:53 +00:00
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto read(uint64 addr) const -> bool {
|
2011-12-12 10:59:53 +00:00
|
|
|
if(data == nullptr || (addr >> 3) >= size) return 0;
|
2015-12-14 09:41:06 +00:00
|
|
|
uint mask = endian == 0 ? (0x01 << (addr & 7)) : (0x80 >> (addr & 7));
|
2011-12-12 10:59:53 +00:00
|
|
|
return data[addr >> 3] & mask;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto write(uint64 addr, bool value) -> void {
|
2011-12-12 10:59:53 +00:00
|
|
|
if(data == nullptr || readonly == true || (addr >> 3) >= size) return;
|
2015-12-14 09:41:06 +00:00
|
|
|
uint mask = endian == 0 ? (0x01 << (addr & 7)) : (0x80 >> (addr & 7));
|
2011-12-12 10:59:53 +00:00
|
|
|
if(value == 0) data[addr >> 3] &= ~mask;
|
|
|
|
if(value == 1) data[addr >> 3] |= mask;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto open(const string& filename) -> bool {
|
2011-12-12 10:59:53 +00:00
|
|
|
readonly = false;
|
|
|
|
if(fp.open(filename, filemap::mode::readwrite) == false) {
|
|
|
|
readonly = true;
|
|
|
|
if(fp.open(filename, filemap::mode::read) == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data = fp.data();
|
|
|
|
size = fp.size();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
auto close() -> void {
|
2011-12-12 10:59:53 +00:00
|
|
|
fp.close();
|
|
|
|
data = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-12-14 09:41:06 +00:00
|
|
|
filemap fp;
|
|
|
|
uint8* data = nullptr;
|
|
|
|
uint size = 0;
|
|
|
|
bool readonly = false;
|
|
|
|
bool endian = 1;
|
2011-12-12 10:59:53 +00:00
|
|
|
};
|
|
|
|
|
2016-01-07 08:14:33 +00:00
|
|
|
}}
|