2011-12-12 10:59:53 +00:00
|
|
|
#ifdef NALL_MOSAIC_INTERNAL_HPP
|
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
namespace mosaic {
|
|
|
|
|
|
|
|
struct bitstream {
|
|
|
|
filemap fp;
|
2013-05-02 11:25:45 +00:00
|
|
|
uint8_t* data = nullptr;
|
|
|
|
unsigned size = 0;
|
|
|
|
bool readonly = false;
|
|
|
|
bool endian = 1;
|
2011-12-12 10:59:53 +00:00
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
bool read(uint64_t addr) const {
|
2011-12-12 10:59:53 +00:00
|
|
|
if(data == nullptr || (addr >> 3) >= size) return 0;
|
|
|
|
unsigned mask = endian == 0 ? (0x01 << (addr & 7)) : (0x80 >> (addr & 7));
|
|
|
|
return data[addr >> 3] & mask;
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
void write(uint64_t addr, bool value) {
|
2011-12-12 10:59:53 +00:00
|
|
|
if(data == nullptr || readonly == true || (addr >> 3) >= size) return;
|
|
|
|
unsigned mask = endian == 0 ? (0x01 << (addr & 7)) : (0x80 >> (addr & 7));
|
|
|
|
if(value == 0) data[addr >> 3] &= ~mask;
|
|
|
|
if(value == 1) data[addr >> 3] |= mask;
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
bool open(const string& filename) {
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
void close() {
|
2011-12-12 10:59:53 +00:00
|
|
|
fp.close();
|
|
|
|
data = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
bitstream() {
|
2011-12-12 10:59:53 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
~bitstream() {
|
2011-12-12 10:59:53 +00:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|