Browse Source

compiler fixing

added debugger, fixed faulty jumps in compiler, added cycle-stepping execution for debugging purposes
master
constantinfuerst 4 years ago
parent
commit
b32718024a
  1. 17
      cpu_design_and_emulator/main.cpp
  2. 4
      cpu_design_and_emulator/microprocessor/cpu/cpu.cpp
  3. 6
      cpu_design_and_emulator/microprocessor/cpu/instructions.cpp
  4. 14
      custom_asm_macro_compiler/binfileReader/binfilereader.cpp
  5. 7
      custom_asm_macro_compiler/binfileReader/binfilereader.h
  6. 9
      custom_asm_macro_compiler/compiler/compiler.cpp
  7. 13
      custom_asm_macro_compiler/compiler/compiler.h
  8. 5
      custom_asm_macro_compiler/compiler/compiling/compiling.cpp
  9. 17
      custom_asm_macro_compiler/compiler/compiling/wrappers.cpp
  10. 72
      custom_asm_macro_compiler/compiler/decompiling/decompiler.cpp
  11. 18
      custom_asm_macro_compiler/compiler/decompiling/decompiler.h
  12. 14
      custom_asm_macro_compiler/compiler/macroProcessor/functions/functionHandler.cpp
  13. 6
      custom_asm_macro_compiler/custom_asm_macro_compiler.vcxproj
  14. 12
      custom_asm_macro_compiler/custom_asm_macro_compiler.vcxproj.filters
  15. 38
      custom_asm_macro_compiler/main.cpp
  16. 1
      custom_asm_macro_compiler/pch.h
  17. 14
      test.txt
  18. 26
      testDECOMPILED.txt
  19. 1
      testOUTPUT.bin

17
cpu_design_and_emulator/main.cpp

@ -44,14 +44,23 @@ public:
}
bool OnUserUpdate(float fElapsedTime) override {
if (*quit == false)
for (auto& chip : mChips)
chip->onCycle();
static bool cycle = false;
if (GetKey(olc::SPACE).bPressed)
cycle = true;
if (*quit == false) {
if (cycle == true) {
cycle = false;
for (auto& chip : mChips)
chip->onCycle();
}
}
else {
std::cin.get();
std::cout << "QUIT" << std::endl;
exit(0);
}
return true;
}
@ -89,7 +98,7 @@ void computer::initMembers() {
int main() {
std::cout << "STARTING" << std::endl;
auto* bin = binReader::readFile("C:\\Users\\Admin\\Desktop\\testOUTPUT.bin");
auto* bin = binReader::readFile("C:\\Projects\\cpu_design_and_emulator\\testOUTPUT.bin");
computer cpuEngine(bin);
if (cpuEngine.Construct(184, 64, 4, 4))

4
cpu_design_and_emulator/microprocessor/cpu/cpu.cpp

@ -4,7 +4,7 @@
void cpu::onCycle() {
opcode = m_parentBus->read(m_pc);
auto& instr = lookupTable[opcode];
if (opcode != 0x00) std::cout << "cpu opcode: " << instr.ostr << " at: " << m_pc << std::endl;
if (opcode != 0x00) std::cout << std::dec << "cpu opcode: " << instr.ostr << " at: " << m_pc << " | " << std::hex << m_pc << std::endl;
m_pc++;
(this->*instr.addr)();
(this->*instr.instr)();
@ -76,7 +76,7 @@ cpu::cpu(bus* parent, bool* running_flag, const uint16_t& programCounter, const
instruction(0x15,"0x15 MSL", &cpu::VAL, &cpu::MSL),
instruction(0x16,"0x16 MSH", &cpu::VAL, &cpu::MSH),
instruction(0x17,"0x17 MSR", &cpu::ABS, &cpu::MSR),
instruction(0x18,"0x18 PCC", &cpu::IMP, &cpu::PCC),
instruction(0x18,"0x18 PCC", &cpu::VAL, &cpu::PCC),
instruction(0x19,"0x19 NOP", &cpu::IMP, &cpu::NOP),
instruction(0x1a,"0x1a NOP", &cpu::IMP, &cpu::NOP),
instruction(0x1b,"0x1b NOP", &cpu::IMP, &cpu::NOP),

6
cpu_design_and_emulator/microprocessor/cpu/instructions.cpp

@ -61,6 +61,7 @@ void cpu::MSR() {
void cpu::JJR() {
m_pc = m_regJoined[0] | (m_regJoined[1] << 8);
std::cout << "Jumping to: " << std::hex << m_pc << std::dec << std::endl;
}
void cpu::MJH() {
@ -282,9 +283,10 @@ void cpu::MSH() {
}
void cpu::PCC() {
m_parentBus->write(stackPtr, m_pc >> 8);
const uint16_t advanced_pc = m_pc + m_parentBus->read(m_addrAbs);
m_parentBus->write(stackPtr, advanced_pc >> 8);
stackPtr++;
m_parentBus->write(stackPtr, m_pc & 0x00ff);
m_parentBus->write(stackPtr, advanced_pc & 0x00ff);
stackPtr++;
}

14
custom_asm_macro_compiler/binfileReader/binfilereader.cpp

@ -0,0 +1,14 @@
#include "pch.h"
#include "binfilereader.h"
std::vector<uint8_t>* binReader::readFile(const std::string& fname) {
std::ifstream binaryFile;
binaryFile.open(fname, std::ios::binary);
if (!binaryFile.is_open()) return nullptr;
// copies all data into buffer
auto* vec = new std::vector<uint8_t> (std::istreambuf_iterator<char>(binaryFile), {});
binaryFile.close();
return vec;
}

7
custom_asm_macro_compiler/binfileReader/binfilereader.h

@ -0,0 +1,7 @@
#pragma once
#include "pch.h"
class binReader final {
public:
static std::vector<uint8_t>* readFile(const std::string& fname);
};

9
custom_asm_macro_compiler/compiler/compiler.cpp

@ -1,16 +1,17 @@
#include "pch.h"
#include "compiler.h"
void compiler::compile(const std::string& baseOffset) {
void compiler::compile() {
readProgram(asmFname);
currentAddr = std::stoi(baseOffset, 0 , 16);
currentAddr = baseOffset;
compileProgram();
assembleProgram();
if (useGraphics == true) appendGraphicsData();
}
void compiler::loadAsmFile(const std::string& asmIn) {
void compiler::loadAsmFile(const std::string& asmIn, const std::string& bOffset) {
asmFname = asmIn;
baseOffset = std::stoi(bOffset, 0, 16);
}
void compiler::loadGraphicsData(const std::string& segmentIn, const std::string& paletteIn) {
@ -89,7 +90,7 @@ compiler::compiler() {
instruction(0x15,"NOP", dataType::imp),
instruction(0x16,"NOP", dataType::imp),
instruction(0x17,"MSR", dataType::abs),
instruction(0x18,"PCC", dataType::imp),
instruction(0x18,"PCC", dataType::val),
instruction(0x19,"NOP", dataType::imp),
instruction(0x1a,"NOP", dataType::imp),
instruction(0x1b,"NOP", dataType::imp),

13
custom_asm_macro_compiler/compiler/compiler.h

@ -2,7 +2,11 @@
#include "pch.h"
#include "compilerDEFS.h"
class compiler final {
class compiler {
public:
std::array<cdef::dataTranslate, 8>* lookupTableDATA;
std::array<cdef::instruction, 256>* lookupTableINSTR;
private:
uint16_t currentAddr = 0x0000;
std::string asmFname;
@ -10,9 +14,8 @@ private:
std::string paletteFname;
std::string outputFname;
bool useGraphics = false;
uint16_t baseOffset = 0x0000;
std::array<cdef::dataTranslate, 8>* lookupTableDATA;
std::array<cdef::instruction, 256>* lookupTableINSTR;
cdef::collection segments;
cdef::collection palettes;
std::vector <cdef::function*> functions;
@ -60,8 +63,8 @@ private:
public:
compiler(); ~compiler();
void loadAsmFile(const std::string& asmIn);
void loadAsmFile(const std::string& asmIn, const std::string& baseOffset);
void loadGraphicsData(const std::string& segmentIn, const std::string& paletteIn);
void compile(const std::string& baseOffset);
void compile();
void output(const std::string& outputIn);
};

5
custom_asm_macro_compiler/compiler/compiling/compiling.cpp

@ -44,7 +44,7 @@ cdef::asmLine* compiler::captureInstrLine(const std::string& instrLine) {
dataType dtype = dataType::imp;
bool opcodefound = false;
std::string instrTXT = instrLine.substr(0, 3);
for (auto& instr : *lookupTableINSTR) {
if (instr.opString != instrTXT) continue;
for (auto& data : *lookupTableDATA) {
@ -59,7 +59,8 @@ cdef::asmLine* compiler::captureInstrLine(const std::string& instrLine) {
if (opcodefound == true) break;
}
if (opcode == 0) return notfound();
if (opcode == 0)
return notfound();
asml->push_back(new asmPart(opcode));
//helper lambdas for conversions and pushing

17
custom_asm_macro_compiler/compiler/compiling/wrappers.cpp

@ -12,8 +12,9 @@ void compiler::assembleProgram() {
}
}
codeVEC[13] = currentAddr & 0x00ff;
codeVEC[14] = currentAddr >> 8;
uint16_t pStart = (baseOffset & 0x00ff) + currentAddr - 1;
codeVEC[13] = pStart & 0x00ff;
codeVEC[14] = pStart >> 8;
for (size_t i = 0; i < compiledVEC.size(); i++) {
uint8_t d = compiledVEC[i]->data;
@ -114,8 +115,9 @@ void compiler::compileFunction(const unsigned int& pos) {
}
void compiler::appendGraphicsData() {
codeVEC[8] = currentAddr & 0x00ff;
codeVEC[11] = currentAddr >> 8;
uint16_t pStart = baseOffset + currentAddr;
codeVEC[8] = pStart & 0x00ff;
codeVEC[11] = pStart >> 8;
std::ifstream pFile(paletteFname);
if (!pFile.is_open()) return;
@ -127,9 +129,10 @@ void compiler::appendGraphicsData() {
}
pFile.close();
codeVEC[2] = currentAddr & 0x00ff;
codeVEC[5] = currentAddr >> 8;
pStart = baseOffset + currentAddr;
codeVEC[2] = pStart & 0x00ff;
codeVEC[5] = pStart >> 8;
std::ifstream sFile(paletteFname);
if (!sFile.is_open()) return;

72
custom_asm_macro_compiler/compiler/decompiling/decompiler.cpp

@ -0,0 +1,72 @@
#include "pch.h"
#include "decompiler.h"
void decompiler::decompile(const std::string& binFile, const std::string& instrFile) {
readFile(binFile);
processFile();
outputInstr(instrFile);
}
void decompiler::readFile(const std::string& binFile) {
binptr = binReader::readFile(binFile);
}
void decompiler::processFile() {
instrText = new std::vector<std::string>;
uint8_t instr;
std::queue<uint8_t> data;
std::stringstream current;
unsigned int instrLength = 0;
auto int_to_hex = [&](int i) {
std::stringstream stream;
stream << "0x" << std::setfill('0') << std::setw(2) << std::hex << i;
return stream.str();
};
auto processQueue = [&]() {
while (!data.empty()) {
current << int_to_hex(static_cast<int>(data.front())); data.pop();
if (data.size() > 0) current << ", ";
lineCounter++;
}
};
auto setLength = [&](const uint8_t op) {
for (auto& e : *lookupTableINSTR)
if (e.opCode == op) {
instr = op;
current << std::dec << (lineCounter - 16 + 4112) << " | " << std::hex << (lineCounter - 16 + 4112) << std::dec << ":\t\t" << e.opString << " ";
auto dtype = e.expected;
for (auto& f : *lookupTableDATA)
if (f.dt == dtype)
instrLength = f.bcount;
}
lineCounter++;
};
auto pushString = [&]() {
instrText->push_back(current.str()); current.str("");
};
for (auto& byte : *binptr) {
if (instrLength == 0) {
processQueue(); pushString(); setLength(byte);
}
else {
data.push(byte); instrLength--;
}
}
delete binptr;
}
void decompiler::outputInstr(const std::string& instrFile) {
std::ofstream file;
file.open(instrFile);
if (!file.is_open()) return;
for (auto& str : *instrText)
file << str << std::endl;
file.close();
delete instrText;
}

18
custom_asm_macro_compiler/compiler/decompiling/decompiler.h

@ -0,0 +1,18 @@
#pragma once
#include "pch.h"
#include "../compiler.h"
#include "../../binfileReader/binfilereader.h"
class decompiler final : public compiler {
private:
std::vector<uint8_t>* binptr = nullptr;
std::vector<std::string>* instrText = nullptr;
unsigned int lineCounter = 16;
void readFile(const std::string& binFile);
void processFile();
void outputInstr(const std::string& instrFile);
public:
void decompile(const std::string& binFile, const std::string& instrFile);
};

14
custom_asm_macro_compiler/compiler/macroProcessor/functions/functionHandler.cpp

@ -5,23 +5,25 @@ cdef::strvecptr compiler::fCall(cdef::strvecptr input) {
using namespace cdef;
auto* ret = new std::vector<std::string*>;
uint8_t fAddrLO = 0x00;
uint8_t fAddrHI = 0x00;
uint16_t fAddr = baseOffset;
std::string reqFname = *input->at(0);
for (auto& f : functions)
if (f->fname == reqFname) {
delete input->at(0); input->erase(input->begin() + 0);
fAddrLO = f->fPos & 0x00ff;
fAddrHI = f->fPos >> 8;
fAddr += f->fPos;
}
ret->push_back(new std::string("PCC;")); //push current program counter to stack
ret->push_back(new std::string("PCC 0x03;")); //push current program counter to stack
auto itochar = [&](const uint8_t& i) { switch (i) { case 0x00: return '0'; case 0x01: return '1'; case 0x02: return '2'; case 0x03: return '3'; case 0x04: return '4'; case 0x05: return '5'; case 0x06: return '6'; case 0x07: return '7'; case 0x08: return '8'; case 0x09: return '9'; case 0x0a: return 'a'; case 0x0b: return 'b'; case 0x0c: return 'c'; case 0x0d: return 'd'; case 0x0e: return 'e'; case 0x0f: return 'f'; default: return '0'; } };
auto itohstr = [&](const uint8_t& i) { return "0x" + std::string(1, itochar(i >> 4)) + std::string(1, itochar(i & 0x0f)); };
ret->push_back(new std::string("JMP " + itohstr(fAddrLO) + ", " + itohstr(fAddrHI) + ";")); //call to jump with the function location
uint8_t fAddrLO = fAddr & 0x00ff;
uint8_t fAddrHI = fAddr >> 8;
auto* str = new std::string("JMP " + itohstr(fAddrLO) + ", " + itohstr(fAddrHI) + ";");
ret->push_back(str); //call to jump with the function location
return ret;
}

6
custom_asm_macro_compiler/custom_asm_macro_compiler.vcxproj

@ -36,7 +36,7 @@
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
@ -166,11 +166,14 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="binfileReader\binfilereader.cpp" />
<ClCompile Include="compiler\compiler.cpp" />
<ClCompile Include="compiler\compiling\compiling.cpp" />
<ClCompile Include="compiler\compiling\wrappers.cpp" />
<ClCompile Include="compiler\decompiling\decompiler.cpp" />
<ClCompile Include="compiler\macroProcessor\functions\functionHandler.cpp" />
<ClCompile Include="compiler\macroProcessor\macroProcessor.cpp" />
<ClCompile Include="compiler\decompiling\decompiler.h" />
<ClCompile Include="main.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
@ -180,6 +183,7 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="binfileReader\binfilereader.h" />
<ClInclude Include="compiler\compiler.h" />
<ClInclude Include="compiler\compilerDEFS.h" />
<ClInclude Include="pch.h" />

12
custom_asm_macro_compiler/custom_asm_macro_compiler.vcxproj.filters

@ -36,6 +36,15 @@
<ClCompile Include="compiler\compiling\wrappers.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="binfileReader\binfilereader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="compiler\decompiling\decompiler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="compiler\decompiling\decompiler.h">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
@ -47,5 +56,8 @@
<ClInclude Include="compiler\compilerDEFS.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="binfileReader\binfilereader.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

38
custom_asm_macro_compiler/main.cpp

@ -1,5 +1,12 @@
#include "pch.h"
#define COMPILE
#define DECOMPILE
#include "compiler/compiler.h"
#ifdef DECOMPILE
#include "compiler/decompiling/decompiler.h"
#endif
/*
*
@ -28,35 +35,44 @@
*/
int main() {
std::string asmFile = "C:\\Users\\Admin\\Desktop\\test.txt",
baseOffset = "0x1010",
paletteFile = "[none]",
segmentFile = "[none]",
outputFile = "C:\\Users\\Admin\\Desktop\\testOUTPUT.bin";
#ifdef COMPILE
std::string asmFile = "C:\\Projects\\cpu_design_and_emulator\\test.txt",
baseOffset = "0x1010",
paletteFile = "[none]",
segmentFile = "[none]",
outputFile = "C:\\Projects\\cpu_design_and_emulator\\testOUTPUT.bin";
/*
std::cout << "Please enter the full path to a assembly file" << std::endl;
std::cin >> asmFile;
std::cout << "Please enter the base address offset this program is supposed to utilize (standard is 0x1010)" << std::endl;
std::cin >> baseOffset;
std::cout << "Please enter the full directory to a palette file, enter [none] if none" << std::endl;
std::cin >> paletteFile;
std::cout << "Please enter the full directory to a segment file, enter [none] if none" << std::endl;
std::cin >> segmentFile;
std::cout << "Please enter a file descriptor for the output files" << std::endl;
std::cin >> outputFile;
*/
compiler c;
if (segmentFile != "[none]" && paletteFile != "[none]")
c.loadGraphicsData(segmentFile, paletteFile);
c.loadAsmFile(asmFile);
c.compile(baseOffset);
c.loadAsmFile(asmFile, baseOffset);
c.compile();
c.output(outputFile);
#endif
#ifdef DECOMPILE
std::string mcFile = "C:\\Projects\\cpu_design_and_emulator\\testOUTPUT.bin";
std::string decoFile = "C:\\Projects\\cpu_design_and_emulator\\testDECOMPILED.txt";
decompiler dc;
dc.decompile(mcFile, decoFile);
#endif
return 0;
}

1
custom_asm_macro_compiler/pch.h

@ -1,5 +1,6 @@
#pragma once
#include <iomanip>
#include <iostream>
#include <vector>
#include <cstdint>

14
test.txt

@ -0,0 +1,14 @@
fDecl(test1){
OUT 0x03;
fSetReturn();
}
fDecl(test2){
OUT 0x02;
fCall(test1);
OUT 0x04;
fSetReturn();
OUT 0x05;
}
OUT 0x01;
fCall(test2);
OUT 0x06;

26
testDECOMPILED.txt

@ -0,0 +1,26 @@
4112 | 1010: MOV 0x02, 0xcc
4115 | 1013: MOV 0x03, 0xcc
4118 | 1016: MOV 0x04, 0xcc
4121 | 1019: MOV 0x05, 0xcc
4124 | 101c: JMP 0x36, 0x10
4127 | 101f: OUT 0x03
4129 | 1021: POP
4130 | 1022: MTL
4131 | 1023: POP
4132 | 1024: MTH
4133 | 1025: JJR
4134 | 1026: OUT 0x02
4136 | 1028: PCC 0x03
4138 | 102a: JMP 0x1f, 0x10
4141 | 102d: OUT 0x04
4143 | 102f: POP
4144 | 1030: MTL
4145 | 1031: POP
4146 | 1032: MTH
4147 | 1033: OUT 0x05
4149 | 1035: JJR
4150 | 1036: OUT 0x01
4152 | 1038: PCC 0x03
4154 | 103a: JMP 0x26, 0x10
4157 | 103d: OUT 0x06

1
testOUTPUT.bin

@ -0,0 +1 @@
����@6� % &�@� % &��@&��
Loading…
Cancel
Save