Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libsnes/bsnes/nall/mosaic/bitstream.hpp
2 views
1
#ifdef NALL_MOSAIC_INTERNAL_HPP
2
3
namespace nall {
4
namespace mosaic {
5
6
struct bitstream {
7
filemap fp;
8
uint8_t *data;
9
unsigned size;
10
bool readonly;
11
bool endian;
12
13
inline bool read(uint64_t addr) const {
14
if(data == nullptr || (addr >> 3) >= size) return 0;
15
unsigned mask = endian == 0 ? (0x01 << (addr & 7)) : (0x80 >> (addr & 7));
16
return data[addr >> 3] & mask;
17
}
18
19
inline void write(uint64_t addr, bool value) {
20
if(data == nullptr || readonly == true || (addr >> 3) >= size) return;
21
unsigned mask = endian == 0 ? (0x01 << (addr & 7)) : (0x80 >> (addr & 7));
22
if(value == 0) data[addr >> 3] &= ~mask;
23
if(value == 1) data[addr >> 3] |= mask;
24
}
25
26
inline bool open(const string &filename) {
27
readonly = false;
28
if(fp.open(filename, filemap::mode::readwrite) == false) {
29
readonly = true;
30
if(fp.open(filename, filemap::mode::read) == false) {
31
return false;
32
}
33
}
34
data = fp.data();
35
size = fp.size();
36
return true;
37
}
38
39
inline void close() {
40
fp.close();
41
data = nullptr;
42
}
43
44
inline bitstream() : data(nullptr), endian(1) {
45
}
46
47
inline ~bitstream() {
48
close();
49
}
50
};
51
52
}
53
}
54
55
#endif
56
57