2017-06-04 20:38:34 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Disassembler.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <bitset>
|
|
|
|
|
|
|
|
#include "Memory.h"
|
|
|
|
#include "Intel8080.h"
|
|
|
|
|
|
|
|
EightBit::Disassembler::Disassembler() {
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string EightBit::Disassembler::state(Intel8080& cpu) {
|
|
|
|
|
2017-06-19 12:53:00 +00:00
|
|
|
auto pc = cpu.PC();
|
|
|
|
auto sp = cpu.SP();
|
2017-06-04 20:38:34 +00:00
|
|
|
|
|
|
|
auto a = cpu.A();
|
|
|
|
auto f = cpu.F();
|
|
|
|
|
|
|
|
auto b = cpu.B();
|
|
|
|
auto c = cpu.C();
|
|
|
|
|
|
|
|
auto d = cpu.D();
|
|
|
|
auto e = cpu.E();
|
|
|
|
|
|
|
|
auto h = cpu.H();
|
|
|
|
auto l = cpu.L();
|
|
|
|
|
|
|
|
std::ostringstream output;
|
|
|
|
|
|
|
|
output
|
|
|
|
<< "PC=" << hex(pc.word)
|
|
|
|
<< " "
|
|
|
|
<< "SP=" << hex(sp.word)
|
2017-06-16 00:58:12 +00:00
|
|
|
<< " " << "A=" << hex(a) << " " << "F=" << flags(f)
|
2017-06-04 20:38:34 +00:00
|
|
|
<< " " << "B=" << hex(b) << " " << "C=" << hex(c)
|
|
|
|
<< " " << "D=" << hex(d) << " " << "E=" << hex(e)
|
|
|
|
<< " " << "H=" << hex(h) << " " << "L=" << hex(l);
|
|
|
|
|
|
|
|
return output.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string EightBit::Disassembler::disassemble(Intel8080& cpu) {
|
|
|
|
|
|
|
|
const auto& memory = cpu.getMemory();
|
2017-06-19 12:53:00 +00:00
|
|
|
auto pc = cpu.PC();
|
2017-06-04 20:38:34 +00:00
|
|
|
auto opcode = memory.peek(pc.word);
|
|
|
|
const auto& instruction = cpu.getInstructions()[opcode];
|
|
|
|
|
|
|
|
std::ostringstream output;
|
|
|
|
|
|
|
|
// hex opcode
|
|
|
|
output << hex(opcode);
|
|
|
|
|
|
|
|
// hex raw operand
|
|
|
|
switch (instruction.mode) {
|
|
|
|
case Intel8080::Immediate:
|
|
|
|
output << hex(memory.peek(pc.word + 1));
|
|
|
|
break;
|
|
|
|
case Intel8080::Absolute:
|
|
|
|
output << hex(memory.peek(pc.word + 1));
|
|
|
|
output << hex(memory.peek(pc.word + 2));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
output << "\t";
|
|
|
|
|
|
|
|
// base disassembly
|
|
|
|
output << instruction.disassembly;
|
|
|
|
|
|
|
|
// disassembly operand
|
|
|
|
switch (instruction.mode) {
|
|
|
|
case Intel8080::Immediate:
|
|
|
|
output << hex(memory.peek(pc.word + 1));
|
|
|
|
break;
|
|
|
|
case Intel8080::Absolute:
|
2017-06-05 21:39:15 +00:00
|
|
|
output << hex(memory.peekWord(pc.word + 1));
|
2017-06-04 20:38:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.str();
|
|
|
|
}
|
|
|
|
|
2017-06-16 00:58:12 +00:00
|
|
|
std::string EightBit::Disassembler::flag(uint8_t value, int flag, const std::string& represents) {
|
|
|
|
std::ostringstream output;
|
|
|
|
output << (value & flag ? represents : "-");
|
|
|
|
return output.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string EightBit::Disassembler::flags(uint8_t value) {
|
|
|
|
std::ostringstream output;
|
|
|
|
output
|
|
|
|
<< flag(value, Intel8080::SF, "S")
|
|
|
|
<< flag(value, Intel8080::ZF, "Z")
|
|
|
|
<< "0"
|
|
|
|
<< flag(value, Intel8080::AC, "A")
|
|
|
|
<< "0"
|
|
|
|
<< flag(value, Intel8080::PF, "P")
|
|
|
|
<< "1"
|
|
|
|
<< flag(value, Intel8080::CF, "C");
|
|
|
|
return output.str();
|
|
|
|
}
|
|
|
|
|
2017-06-04 20:38:34 +00:00
|
|
|
std::string EightBit::Disassembler::hex(uint8_t value) {
|
|
|
|
std::ostringstream output;
|
|
|
|
output << std::hex << std::setw(2) << std::setfill('0') << (int)value;
|
|
|
|
return output.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string EightBit::Disassembler::hex(uint16_t value) {
|
|
|
|
std::ostringstream output;
|
|
|
|
output << std::hex << std::setw(4) << std::setfill('0') << (int)value;
|
|
|
|
return output.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string EightBit::Disassembler::binary(uint8_t value) {
|
|
|
|
std::ostringstream output;
|
|
|
|
output << std::bitset<8>(value);
|
|
|
|
return output.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string EightBit::Disassembler::invalid(uint8_t value) {
|
|
|
|
std::ostringstream output;
|
|
|
|
output << "Invalid instruction: " << hex(value) << "(" << binary(value) << ")";
|
|
|
|
return output.str();
|
|
|
|
}
|