bsnes/nall/bitvector.hpp

119 lines
2.9 KiB
C++
Raw Normal View History

Update to v094r05 release. byuu says: Commands can be prefixed with: (cpu|smp|ppu|dsp|apu|vram|oam|cgram)/ to set their source. Eg "vram/hex 0800" or "smp/breakpoints.append execute ffc0"; default is cpu. These overlap a little bit in odd ways, but that's just the way the SNES works: it's not a very orthogonal system. CPU is both a processor and the main bus (ROM, RAM, WRAM, etc), APU is the shared memory by the SMP+DSP (eg use it to catch writes from either chip); PPU probably won't ever be used since it's broken down into three separate buses (VRAM, OAM, CGRAM), but DSP could be useful for tracking bugs like we found in Koushien 2 with the DSP echo buffer corrupting SMP opcodes. Technically the PPU memory pools are only ever tripped by the CPU poking at them, as the PPU doesn't ever write. I now have run.for, run.to, step.for, step.to. The difference is that run only prints the next instruction after running, whereas step prints all of the instructions along the way as well. run.to acts the same as "step over" here. Although it's not quite as nice, since you have to specify the address of the next instruction. Logging the Field/Vcounter/Hcounter on instruction listings now, good for timing information. Added in the tracer mask, as well as memory export, as well as VRAM/OAM/CGRAM/SMP read/write/execute breakpoints, as well as an APU usage map (it tracks DSP reads/writes separately, although I don't currently have debugger callbacks on DSP accesses just yet.) Have not hooked up actual SMP debugging just yet, but I plan to soon. Still thinking about how I want to allow / block interleaving of instructions (terminal output and tracing.) So ... remaining tasks at this point: - full SMP debugging - CPU+SMP interleave support - aliases - hotkeys - save states (will be kind of tricky ... will have to suppress breakpoints during synchronization, or abort a save in a break event.) - keep track of window geometry between runs
2014-02-05 11:30:08 +00:00
#ifndef NALL_BITVECTOR_HPP
#define NALL_BITVECTOR_HPP
namespace nall {
struct bitvector {
protected:
uint8_t* pool = nullptr;
unsigned bits = 0;
public:
bitvector() {}
bitvector(unsigned size) { resize(size); }
bitvector(const bitvector& source) { operator=(source); }
bitvector(bitvector&& source) { operator=(std::move(source)); }
~bitvector() { reset(); }
bitvector& operator=(const bitvector& source) {
bits = source.bits;
pool = (uint8_t*)realloc(pool, bytes());
memcpy(pool, source.pool, bytes());
return *this;
}
bitvector& operator=(bitvector&& source) {
pool = source.pool;
bits = source.bits;
source.pool = nullptr;
source.bits = 0;
return *this;
}
explicit operator bool() const { return bits > 0; }
bool empty() const { return bits == 0; }
unsigned size() const { return bits; }
unsigned bytes() const { return (bits + 7) / 8; }
uint8_t* data() { return pool; }
const uint8_t* data() const { return pool; }
void reset() {
if(pool) free(pool);
pool = nullptr;
bits = 0;
}
void resize(unsigned size) {
unsigned from = bits;
bits = size;
for(unsigned n = size; n < from; n++) clear(n); //on reduce
pool = (uint8_t*)realloc(pool, bytes());
for(unsigned n = from; n < size; n++) clear(n); //on expand
}
bool get(unsigned position) const {
return pool[position >> 3] & (0x80 >> (position & 7));
}
void clear() {
memset(pool, 0x00, bytes());
}
void set() {
memset(pool, 0xff, bytes());
for(unsigned n = bits; n < bytes() * 8; n++) clear(n);
}
void clear(unsigned position) {
pool[position >> 3] &= ~(0x80 >> (position & 7));
}
void set(unsigned position) {
pool[position >> 3] |= (0x80 >> (position & 7));
}
void invert(unsigned position) {
get(position) ? clear(position) : set(position);
}
void set(unsigned position, bool value) {
value ? set(position) : clear(position);
}
struct reference {
reference(bitvector& self, unsigned position) : self(self), position(position) {}
operator bool() const { return self.get(position); }
void operator=(bool value) { self.set(position, value); }
protected:
bitvector& self;
unsigned position;
};
reference operator[](unsigned position) {
return reference(*this, position);
}
bool operator[](unsigned position) const {
return get(position);
}
struct iterator {
iterator(bitvector& self, unsigned position) : self(self), position(position) {}
bool operator!=(const iterator& source) const { return position != source.position; }
iterator& operator++() { position++; return *this; }
reference operator*() { return self.operator[](position); }
protected:
bitvector& self;
unsigned position;
};
iterator begin() { return iterator(*this, 0); }
iterator end() { return iterator(*this, bits); }
};
}
#endif