AddressMap / ToolMap class

This commit is contained in:
Kelvin Sherlock 2013-07-05 13:54:15 -04:00
parent 82f8770a5e
commit 0a729dcacf
4 changed files with 133 additions and 85 deletions

View File

@ -6,7 +6,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-framework Carbon")
add_definitions(-I ${CMAKE_SOURCE_DIR}/)
add_executable(mpw loader.cpp debugger.cpp)
add_executable(mpw loader.cpp debugger.cpp address_map.cpp)
target_link_libraries(mpw CPU_LIB)
target_link_libraries(mpw DEBUGGER_LIB)
target_link_libraries(mpw TOOLBOX_LIB)

79
bin/address_map.cpp Normal file
View File

@ -0,0 +1,79 @@
#include "address_map.h"
AddressMap::AddressMap()
{
}
AddressMap::~AddressMap()
{
}
void AddressMap::add(uint32_t address)
{
unsigned page = address >> 12;
if (page >= pageMap.size()) return;
pageMap[page]++;
map.emplace(address, 0);
}
void AddressMap::remove(uint32_t address)
{
unsigned page = address >> 12;
if (page >= pageMap.size()) return;
// erase(key) returns # of elements erased.
pageMap[page] -= map.erase(address);
}
bool AddressMap::lookup(uint32_t address)
{
unsigned page = address >> 12;
if (page >= pageMap.size()) return false;
if (!pageMap[page]) return false;
return map.find(address) != map.end();
}
void AddressMap::clear()
{
pageMap.fill(0);
map.clear();
}
ToolMap::ToolMap()
{
}
ToolMap::~ToolMap()
{
}
void ToolMap::clear()
{
map.clear();
}
bool ToolMap::lookup(uint16_t tool)
{
if ((tool & 0xf000) != 0xa000) return false;
return map.find(tool) != map.end();
}
void ToolMap::add(uint16_t tool)
{
if ((tool & 0xf000) != 0xa000) return;
// return std::pair<iter, bool>
map.emplace(tool, 0);
}
void ToolMap::remove(uint16_t tool)
{
if ((tool & 0xf000) != 0xa000) return;
map.erase(tool);
}

42
bin/address_map.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef __address_map__
#define __address_map__
#include <unordered_map>
#include <array>
class AddressMap
{
public:
AddressMap();
~AddressMap();
void add(uint32_t address);
void remove(uint32_t address);
bool lookup(uint32_t address);
void clear();
private:
std::array<unsigned, 4096> pageMap;
std::unordered_map<uint32_t, unsigned> map;
};
class ToolMap
{
public:
ToolMap();
~ToolMap();
void add(uint16_t tool);
void remove(uint16_t tool);
bool lookup(uint16_t tool);
void clear();
private:
std::unordered_map<uint32_t, unsigned> map;
};
#endif

View File

@ -18,6 +18,7 @@
#include "loader.h"
#include "address_map.h"
#include <debugger/commands.h>
@ -74,85 +75,11 @@ namespace {
sigInt = true;
}
// any reason to have enable/disable flag
// vs deleting it?
std::unordered_map<uint16_t, bool> tbrkMap;
std::unordered_map<uint16_t, unsigned> brkMap;
std::array<uint32_t, 4096> brkPageMap; // bloom filter on a page-level.
AddressMap brkMap; // address breaks
AddressMap rbrkMap; // read breaks.
AddressMap wbrkMap; // write breaks.
ToolMap tbrkMap; // tool breaks.
void tbrkAdd(uint16_t tool)
{
auto iter = tbrkMap.find(tool);
if (iter == tbrkMap.end())
{
tbrkMap.emplace(tool, true);
}
}
void tbrkRemove(uint16_t tool)
{
auto iter = tbrkMap.find(tool);
if (iter == tbrkMap.end()) return;
tbrkMap.erase(iter);
}
void tbrkRemoveAll()
{
tbrkMap.clear();
}
bool tbrkLookup(uint16_t tool)
{
if ((tool & 0xf000) != 0xa000) return false;
return tbrkMap.find(tool) != tbrkMap.end();
}
void brkRemoveAll()
{
brkPageMap.fill(0);
brkMap.clear();
}
void brkRemove(uint32_t address)
{
uint32_t page = address >> 12;
if (page >= brkPageMap.size()) return;
auto iter = brkMap.find(address);
if (iter == brkMap.end())
return;
brkPageMap[page]--;
brkMap.erase(iter);
}
void brkAdd(uint32_t address)
{
uint32_t page = address >> 12;
if (page >= brkPageMap.size()) return;
auto iter = brkMap.find(address);
if (iter == brkMap.end())
{
brkPageMap[page]++;
brkMap.emplace(address, 0);
}
}
bool brkLookup(uint32_t address)
{
uint32_t page = address >> 12;
if (page >= brkPageMap.size()) return false;
if (!brkPageMap[page]) return false;
return brkMap.find(address) != brkMap.end();
}
void hexdump(const uint8_t *data, ssize_t size, uint32_t address = 0)
@ -273,7 +200,7 @@ namespace {
// check for pc breaks
if (brkLookup(pc))
if (brkMap.lookup(pc))
{
if (!trace) disasm(pc);
printf("Address break: $%08x\n", pc);
@ -285,7 +212,7 @@ namespace {
// check for toolbreaks.
if ((op & 0xf000) == 0xa000)
{
if (tbrkLookup(op))
if (tbrkMap.lookup(op))
{
if (!trace) disasm(pc);
printf("Tool break: $%04x\n", op);
@ -451,8 +378,8 @@ void DebugToolBreak(Command &cmd)
if (tool >= 0xa000 && tool <= 0xafff)
{
if (remove) tbrkRemove(tool);
else tbrkAdd(tool);
if (remove) tbrkMap.remove(tool);
else tbrkMap.add(tool);
}
else
{
@ -477,8 +404,8 @@ void DebugBreak(Command &cmd)
if ((address & 0xff000000) == 0)
{
if (remove) brkRemove(address);
else brkAdd(address);
if (remove) brkMap.remove(address);
else brkMap.add(address);
}
else
{