Browse Source
compiler fixing
compiler fixing
added debugger, fixed faulty jumps in compiler, added cycle-stepping execution for debugging purposesmaster
19 changed files with 250 additions and 44 deletions
-
17cpu_design_and_emulator/main.cpp
-
4cpu_design_and_emulator/microprocessor/cpu/cpu.cpp
-
6cpu_design_and_emulator/microprocessor/cpu/instructions.cpp
-
14custom_asm_macro_compiler/binfileReader/binfilereader.cpp
-
7custom_asm_macro_compiler/binfileReader/binfilereader.h
-
9custom_asm_macro_compiler/compiler/compiler.cpp
-
13custom_asm_macro_compiler/compiler/compiler.h
-
5custom_asm_macro_compiler/compiler/compiling/compiling.cpp
-
17custom_asm_macro_compiler/compiler/compiling/wrappers.cpp
-
72custom_asm_macro_compiler/compiler/decompiling/decompiler.cpp
-
18custom_asm_macro_compiler/compiler/decompiling/decompiler.h
-
14custom_asm_macro_compiler/compiler/macroProcessor/functions/functionHandler.cpp
-
6custom_asm_macro_compiler/custom_asm_macro_compiler.vcxproj
-
12custom_asm_macro_compiler/custom_asm_macro_compiler.vcxproj.filters
-
38custom_asm_macro_compiler/main.cpp
-
1custom_asm_macro_compiler/pch.h
-
14test.txt
-
26testDECOMPILED.txt
-
1testOUTPUT.bin
@ -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; |
|||
} |
@ -0,0 +1,7 @@ |
|||
#pragma once |
|||
#include "pch.h" |
|||
|
|||
class binReader final { |
|||
public: |
|||
static std::vector<uint8_t>* readFile(const std::string& fname); |
|||
}; |
@ -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; |
|||
} |
@ -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); |
|||
}; |
@ -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; |
@ -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 |
@ -0,0 +1 @@ |
|||
����@6�%&�@�%&��@&�� |
Write
Preview
Loading…
Cancel
Save
Reference in new issue