Make 6502 symbols code a little more like normal C++

Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2018-11-04 13:27:43 +00:00
parent 95968cb000
commit 003cea0d64
4 changed files with 42 additions and 43 deletions

View File

@ -10,28 +10,27 @@ namespace EightBit {
public: public:
Symbols(std::string path = "") noexcept; Symbols(std::string path = "") noexcept;
const std::map<uint16_t, std::string>& getLabels() const { return labels; } const std::map<uint16_t, std::string>& labels() const { return m_labels; }
const std::map<uint16_t, std::string>& getConstants() const { return constants; } const std::map<uint16_t, std::string>& constants() const { return m_constants; }
const std::map<std::string, uint16_t>& getScopes() const { return scopes; } const std::map<std::string, uint16_t>& scopes() const { return m_scopes; }
const std::map<std::string, uint64_t>& getAddresses() const { return addresses; } const std::map<std::string, uint64_t>& addresses() const { return m_addresses; }
private: private:
void AssignScopes(); static std::vector<std::string> split(const std::string& input, const std::string& regex);
void AssignSymbols();
void Parse(std::string path); void assignScopes();
void assignSymbols();
std::map<uint16_t, std::string> labels; void parse(std::string path);
std::map<uint16_t, std::string> constants;
std::map<std::string, uint16_t> scopes; std::map<uint16_t, std::string> m_labels;
std::map<std::string, uint64_t> addresses; std::map<uint16_t, std::string> m_constants;
std::map<std::string, uint16_t> m_scopes;
std::map<std::string, uint64_t> m_addresses;
struct kv_pair_t { struct kv_pair_t {
std::map<std::string, std::string> element; std::map<std::string, std::string> element;
}; };
std::map<std::string, std::map<std::string, kv_pair_t>> m_parsed;
static std::vector<std::string> split(const std::string& input, const std::string& regex);
std::map<std::string, std::map<std::string, kv_pair_t>> parsed;
}; };
} }

View File

@ -495,8 +495,8 @@ std::string EightBit::Disassembly::dump_Word(uint16_t address) {
//// ////
std::string EightBit::Disassembly::convertAddress(uint16_t address) const { std::string EightBit::Disassembly::convertAddress(uint16_t address) const {
auto label = symbols.getLabels().find(address); auto label = symbols.labels().find(address);
if (label != symbols.getLabels().end()) if (label != symbols.labels().end())
return label->second; return label->second;
std::ostringstream output; std::ostringstream output;
output << "$" << dump_WordValue(address); output << "$" << dump_WordValue(address);
@ -504,8 +504,8 @@ std::string EightBit::Disassembly::convertAddress(uint16_t address) const {
} }
std::string EightBit::Disassembly::convertAddress(uint8_t address) const { std::string EightBit::Disassembly::convertAddress(uint8_t address) const {
auto label = symbols.getLabels().find(address); auto label = symbols.labels().find(address);
if (label != symbols.getLabels().end()) if (label != symbols.labels().end())
return label->second; return label->second;
std::ostringstream output; std::ostringstream output;
output << "$" << dump_ByteValue(address); output << "$" << dump_ByteValue(address);
@ -513,15 +513,15 @@ std::string EightBit::Disassembly::convertAddress(uint8_t address) const {
} }
std::string EightBit::Disassembly::convertConstant(uint16_t constant) { std::string EightBit::Disassembly::convertConstant(uint16_t constant) {
auto label = symbols.getConstants().find(constant); auto label = symbols.constants().find(constant);
if (label != symbols.getConstants().end()) if (label != symbols.constants().end())
return label->second; return label->second;
return dump_DByte(constant); return dump_DByte(constant);
} }
std::string EightBit::Disassembly::convertConstant(uint8_t constant) const { std::string EightBit::Disassembly::convertConstant(uint8_t constant) const {
auto label = symbols.getConstants().find(constant); auto label = symbols.constants().find(constant);
if (label != symbols.getConstants().end()) if (label != symbols.constants().end())
return label->second; return label->second;
return dump_ByteValue(constant); return dump_ByteValue(constant);
} }

View File

@ -44,7 +44,7 @@ void EightBit::Profiler::EmitProfileInformation() {
for (auto& scopeCycle : scopeCycles) { for (auto& scopeCycle : scopeCycles) {
auto name = scopeCycle.first; auto name = scopeCycle.first;
auto cycles = scopeCycle.second; auto cycles = scopeCycle.second;
auto namedAddress = (size_t)symbols.getAddresses().find(name)->second; auto namedAddress = (size_t)symbols.addresses().find(name)->second;
auto count = addressCounts[namedAddress]; auto count = addressCounts[namedAddress];
EmitScope.fire(ProfileScopeEventArgs(name, cycles, count)); EmitScope.fire(ProfileScopeEventArgs(name, cycles, count));
} }
@ -70,11 +70,11 @@ void EightBit::Profiler::addAddress(uint16_t address, int cycles) {
} }
void EightBit::Profiler::BuildAddressScopes() { void EightBit::Profiler::BuildAddressScopes() {
for (auto& label : symbols.getLabels()) { for (auto& label : symbols.labels()) {
auto address = label.first; auto address = label.first;
auto key = label.second; auto key = label.second;
auto scope = symbols.getScopes().find(key); auto scope = symbols.scopes().find(key);
if (scope != symbols.getScopes().end()) { if (scope != symbols.scopes().end()) {
for (uint16_t i = address; i < address + scope->second; ++i) { for (uint16_t i = address; i < address + scope->second; ++i) {
addressScopes[i] = key; addressScopes[i] = key;
} }

View File

@ -7,25 +7,25 @@
EightBit::Symbols::Symbols(std::string path) noexcept { EightBit::Symbols::Symbols(std::string path) noexcept {
if (!path.empty()) { if (!path.empty()) {
Parse(path); parse(path);
AssignSymbols(); assignSymbols();
AssignScopes(); assignScopes();
} }
} }
void EightBit::Symbols::AssignScopes() { void EightBit::Symbols::assignScopes() {
auto parsedScopes = parsed["scope"]; auto parsedScopes = m_parsed["scope"];
for(auto& parsedScopeElement : parsedScopes) { for(auto& parsedScopeElement : parsedScopes) {
auto& parsedScope = parsedScopeElement.second.element; auto& parsedScope = parsedScopeElement.second.element;
auto name = parsedScope["name"]; auto name = parsedScope["name"];
auto trimmedName = name.substr(1, name.length() - 2); auto trimmedName = name.substr(1, name.length() - 2);
auto size = parsedScope["size"]; auto size = parsedScope["size"];
scopes[trimmedName] = (uint16_t)std::stoi(size); m_scopes[trimmedName] = (uint16_t)std::stoi(size);
} }
} }
void EightBit::Symbols::AssignSymbols() { void EightBit::Symbols::assignSymbols() {
auto symbols = parsed["sym"]; auto symbols = m_parsed["sym"];
for(auto& symbolElement : symbols) { for(auto& symbolElement : symbols) {
auto& symbol = symbolElement.second.element; auto& symbol = symbolElement.second.element;
auto name = symbol["name"]; auto name = symbol["name"];
@ -34,15 +34,15 @@ void EightBit::Symbols::AssignSymbols() {
auto number = (uint16_t)std::stoi(value, nullptr, 16); auto number = (uint16_t)std::stoi(value, nullptr, 16);
auto symbolType = symbol["type"]; auto symbolType = symbol["type"];
if (symbolType == "lab") { if (symbolType == "lab") {
labels[number] = trimmedName; m_labels[number] = trimmedName;
addresses[trimmedName] = number; m_addresses[trimmedName] = number;
} else if (symbolType == "equ") { } else if (symbolType == "equ") {
constants[number] = trimmedName; m_constants[number] = trimmedName;
} }
} }
} }
void EightBit::Symbols::Parse(std::string path) { void EightBit::Symbols::parse(std::string path) {
std::string line; std::string line;
std::ifstream reader(path); std::ifstream reader(path);
while (std::getline(reader, line)) { while (std::getline(reader, line)) {
@ -58,11 +58,11 @@ void EightBit::Symbols::Parse(std::string path) {
} }
if (data.element.find("id") != data.element.end()) { if (data.element.find("id") != data.element.end()) {
if (parsed.find(type) == parsed.end()) if (m_parsed.find(type) == m_parsed.end())
parsed[type] = std::map<std::string, kv_pair_t>(); m_parsed[type] = std::map<std::string, kv_pair_t>();
auto id = data.element["id"]; auto id = data.element["id"];
data.element.erase("id"); data.element.erase("id");
parsed[type][id] = data; m_parsed[type][id] = data;
} }
} }
} }