Browse Source

workflow from macro file and xpm image to working bytecode

still broken xpm converter, otherwise fully working
master
constantinfuerst 4 years ago
parent
commit
a959c1f06d
  1. BIN
      .vs/cpu_design_and_emulator/v16/Solution.VC.db-shm
  2. BIN
      .vs/cpu_design_and_emulator/v16/Solution.VC.db-wal
  3. 4
      cpu_design_and_emulator/main.cpp
  4. 2
      cpu_design_and_emulator/microprocessor/cpu/cpu.cpp
  5. 2
      cpu_design_and_emulator/microprocessor/cpu/cpu.h
  6. 2
      cpu_design_and_emulator/microprocessor/cpu/instructions.cpp
  7. 5
      custom_asm_macro_compiler/compiler/compiler.cpp
  8. 6
      custom_asm_macro_compiler/compiler/compiler.h
  9. 46
      custom_asm_macro_compiler/compiler/compiling/wrappers.cpp
  10. 10
      custom_asm_macro_compiler/compiler/macroProcessor/functions/functionHandler.cpp
  11. 1
      custom_asm_macro_compiler/compiler/macroProcessor/macroProcessor.cpp
  12. 10
      custom_asm_macro_compiler/custom_asm_macro_compiler.vcxproj
  13. 3
      custom_asm_macro_compiler/custom_asm_macro_compiler.vcxproj.filters
  14. 24
      custom_asm_macro_compiler/main.cpp
  15. 4
      custom_asm_macro_compiler/packages.config
  16. 19
      program_data/redo.bat
  17. 15
      program_data/segment_number_one.xpm
  18. 4
      program_data/test.txt
  19. 48
      program_data/testDECOMPILED.txt
  20. BIN
      program_data/testOUTPUT.bin
  21. 14
      segment_number_one.xpm
  22. 26
      testDECOMPILED.txt
  23. 1
      testOUTPUT.bin
  24. 20
      xpm_to_ppubytecode/converter/converter.cpp
  25. 4
      xpm_to_ppubytecode/main.cpp
  26. 2
      xpm_to_ppubytecode/reader/reader.cpp

BIN
.vs/cpu_design_and_emulator/v16/Solution.VC.db-shm

BIN
.vs/cpu_design_and_emulator/v16/Solution.VC.db-wal

4
cpu_design_and_emulator/main.cpp

@ -44,7 +44,7 @@ public:
}
bool OnUserUpdate(float fElapsedTime) override {
static bool cycle = false;
static bool cycle = true; cycle = true;
if (GetKey(olc::SPACE).bPressed)
cycle = true;
@ -98,7 +98,7 @@ void computer::initMembers() {
int main() {
std::cout << "STARTING" << std::endl;
auto* bin = binReader::readFile("C:\\Projects\\cpu_design_and_emulator\\testOUTPUT.bin");
auto* bin = binReader::readFile("C:\\Projects\\cpu_design_and_emulator\\program_data\\testOUTPUT.bin");
computer cpuEngine(bin);
if (cpuEngine.Construct(184, 64, 4, 4))

2
cpu_design_and_emulator/microprocessor/cpu/cpu.cpp

@ -306,7 +306,7 @@ cpu::cpu(bus* parent, bool* running_flag, const uint16_t& programCounter, const
instruction(0xee,"0xee NOP", &cpu::IMP, &cpu::NOP),
instruction(0xef,"0xef NOP", &cpu::IMP, &cpu::NOP),
//internally reserved instructions for emulator debugging
instruction(0xf0,"0xf0 OUT", &cpu::VAL, &cpu::OUT),
instruction(0xf0,"0xf0 OUT", &cpu::VAL, &cpu::OTP),
instruction(0xf1,"0xf1 END", &cpu::IMP, &cpu::END),
instruction(0xf2,"0xf2 NOP", &cpu::IMP, &cpu::NOP),
instruction(0xf3,"0xf3 NOP", &cpu::IMP, &cpu::NOP),

2
cpu_design_and_emulator/microprocessor/cpu/cpu.h

@ -58,7 +58,7 @@ private:
void PPU(); void POX(); void POY();
//emulator specific debugging instructions
void OUT(); void END();
void OTP(); void END();
bool* flagQUIT;
struct instruction {

2
cpu_design_and_emulator/microprocessor/cpu/instructions.cpp

@ -292,7 +292,7 @@ void cpu::PCC() {
//debugging
void cpu::OUT() {
void cpu::OTP() {
const uint16_t pc_temp = m_pc - 1;
const uint8_t val = m_parentBus->read(m_addrAbs);
uint16_t out = 0x0000;

5
custom_asm_macro_compiler/compiler/compiler.cpp

@ -14,11 +14,6 @@ void compiler::loadAsmFile(const std::string& asmIn, const std::string& bOffset)
baseOffset = std::stoi(bOffset, 0, 16);
}
void compiler::loadGraphicsData(const std::string& segmentIn, const std::string& paletteIn) {
segmentFname = segmentIn;
paletteFname = paletteIn;
}
void compiler::output(const std::string& outputIn) {
std::ofstream output;
output.open(outputIn);

6
custom_asm_macro_compiler/compiler/compiler.h

@ -1,6 +1,7 @@
#pragma once
#include "pch.h"
#include "compilerDEFS.h"
#include "nlohmann/json.hpp"
class compiler {
public:
@ -10,8 +11,7 @@ public:
private:
uint16_t currentAddr = 0x0000;
std::string asmFname;
std::string segmentFname;
std::string paletteFname;
std::string imageFname;
std::string outputFname;
bool useGraphics = false;
uint16_t baseOffset = 0x0000;
@ -32,6 +32,7 @@ private:
cdef::strvecptr fDecl(cdef::strvecptr input);
cdef::strvecptr fCall(cdef::strvecptr input);
cdef::strvecptr fSetReturn();
cdef::strvecptr includeImageFile(cdef::strvecptr input);
static const bool PRE = false; static const bool POST = true;
cdef::strvecptr processMacro(const std::string& str, const bool& mode);
@ -64,7 +65,6 @@ public:
compiler(); ~compiler();
void loadAsmFile(const std::string& asmIn, const std::string& baseOffset);
void loadGraphicsData(const std::string& segmentIn, const std::string& paletteIn);
void compile();
void output(const std::string& outputIn);
};

46
custom_asm_macro_compiler/compiler/compiling/wrappers.cpp

@ -115,32 +115,42 @@ void compiler::compileFunction(const unsigned int& pos) {
}
void compiler::appendGraphicsData() {
uint16_t pStart = baseOffset + currentAddr;
auto hchrtoi = [&](const char chr) { switch (chr) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case 'a': case 'A': return 10; case 'b': case 'B': return 11; case 'c': case 'C': return 12; case 'd': case 'D': return 13; case 'e': case 'E': return 14; case 'f': case 'F': return 15; default: return 0; } };
auto hstrtoi = [&](const std::string& str) { return hchrtoi(str[2]) * 16 + hchrtoi(str[3]); };
auto byteoff = [&](const unsigned int& i) { switch (i) { case 7: return 1; case 6: return 2; case 5: return 4; case 4: return 8; case 3: return 16; case 2: return 32; case 1: return 64; case 0: return 128; default: return 0; }; };
auto bstrtoi = [&](const std::string& str) { int retval = 0x00; for (auto i = 2; i < 10; i++) { if (str[i] == '1') { retval += byteoff(i); } } return retval; };
auto strtoi = [&](const std::string& str) { if (str[0] == '0' && str[1] == 'x') { return hstrtoi(str); } else if (str[0] == '0' && str[1] == 'b') { return bstrtoi(str); } else if (str[0] != '0') { return hstrtoi(str.substr(5, 4)); } else { return std::stoi(str); } };
uint16_t pStart = (baseOffset & 0x00ff) + currentAddr;
codeVEC[8] = pStart & 0x00ff;
codeVEC[11] = pStart >> 8;
std::ifstream pFile(paletteFname);
if (!pFile.is_open()) return;
std::string line;
std::ifstream imgfile(imageFname);
if (!imgfile.is_open()) return;
nlohmann::json json;
imgfile >> json;
imgfile.close();
while (getline(pFile, line)) {
codeVEC.push_back(std::stoi(line));
currentAddr++;
const unsigned int pcount = json["palette"]["count"].get<unsigned int>();
const unsigned int scount = json["segments"]["count"].get<unsigned int>();
for (auto i = 0; i < pcount; i++) {
auto ident = "color_" + std::to_string(i);
auto cs = json["palette"][ident].get<std::array<std::string, 3>>();
for (auto& c : cs) {
codeVEC.push_back(strtoi(c)); currentAddr++;
}
}
pFile.close();
pStart = baseOffset + currentAddr;
pStart = (baseOffset & 0x00ff) + currentAddr;
codeVEC[2] = pStart & 0x00ff;
codeVEC[5] = pStart >> 8;
std::ifstream sFile(paletteFname);
if (!sFile.is_open()) return;
while (getline(sFile, line)) {
codeVEC.push_back(std::stoi(line));
currentAddr++;
for (auto i = 0; i < scount; i++) {
auto ident = "segment_" + std::to_string(i);
auto ss = json["segments"][ident].get<std::array<std::string, 16>>();
for (auto& s : ss) {
codeVEC.push_back(strtoi(s)); currentAddr++;
}
}
sFile.close();
}

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

@ -53,3 +53,13 @@ cdef::strvecptr compiler::fSetReturn() {
ret->push_back(new std::string("MTH;"));
return ret;
}
cdef::strvecptr compiler::includeImageFile(cdef::strvecptr input) {
std::string ifname = *input->at(0);
std::fstream f; f.open(ifname); if (!f.is_open()) return nullptr; f.close();
useGraphics = true;
imageFname = ifname;
return nullptr;
}

1
custom_asm_macro_compiler/compiler/macroProcessor/macroProcessor.cpp

@ -55,6 +55,7 @@ cdef::strvecptr compiler::processMacro(const std::string& str, const bool& mode)
if (base[0] == "fDecl" && mode == PRE) ret = fDecl(&arguments);
else if (base[0] == "fCall") ret = fCall(&arguments);
else if (base[0] == "fSetReturn" && mode == PRE) ret = fSetReturn();
else if (base[0] == "includeImageFile" && mode == PRE) ret = includeImageFile(&arguments);
else {
ret = new std::vector<std::string*>;
auto* strPtr = new std::string(str);

10
custom_asm_macro_compiler/custom_asm_macro_compiler.vcxproj

@ -188,7 +188,17 @@
<ClInclude Include="compiler\compilerDEFS.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\nlohmann.json.3.7.3\build\native\nlohmann.json.targets" Condition="Exists('..\packages\nlohmann.json.3.7.3\build\native\nlohmann.json.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\nlohmann.json.3.7.3\build\native\nlohmann.json.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\nlohmann.json.3.7.3\build\native\nlohmann.json.targets'))" />
</Target>
</Project>

3
custom_asm_macro_compiler/custom_asm_macro_compiler.vcxproj.filters

@ -60,4 +60,7 @@
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>

24
custom_asm_macro_compiler/main.cpp

@ -30,30 +30,26 @@
* 1) declaring a function: "fDecl([fname]) { [newline][function code][newline] }"
* 2) calling a function: "fCall([fname], [[all arguments to be passed, comma separated]]);
* 3) before returning (or pushing to stack) but after the last fCall() use: call "fSetReturn()"
* 4) including a converted image data file in json format: "includeImageFile([full file name and location])", should not be called twice
*
*/
int main() {
#ifdef COMPILE
std::string asmFile = "C:\\Projects\\cpu_design_and_emulator\\test.txt",
std::string
asmFile = "C:\\Projects\\cpu_design_and_emulator\\program_data\\test.txt",
baseOffset = "0x1010",
paletteFile = "[none]",
segmentFile = "[none]",
outputFile = "C:\\Projects\\cpu_design_and_emulator\\testOUTPUT.bin";
outputFile = "C:\\Projects\\cpu_design_and_emulator\\program_data\\testOUTPUT.bin";
compiler c;
if (segmentFile != "[none]" && paletteFile != "[none]")
c.loadGraphicsData(segmentFile, paletteFile);
c.loadAsmFile(asmFile, baseOffset);
c.compile();
c.output(outputFile);
compiler c; c.loadAsmFile(asmFile, baseOffset); c.compile(); c.output(outputFile);
#endif
#ifdef DECOMPILE
std::string mcFile = "C:\\Projects\\cpu_design_and_emulator\\testOUTPUT.bin",
decoFile = "C:\\Projects\\cpu_design_and_emulator\\testDECOMPILED.txt";
decompiler dc;
dc.decompile(mcFile, decoFile);
std::string
mcFile = "C:\\Projects\\cpu_design_and_emulator\\program_data\\testOUTPUT.bin",
decoFile = "C:\\Projects\\cpu_design_and_emulator\\program_data\\testDECOMPILED.txt";
decompiler dc; dc.decompile(mcFile, decoFile);
#endif
return 0;

4
custom_asm_macro_compiler/packages.config

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="nlohmann.json" version="3.7.3" targetFramework="native" />
</packages>

19
program_data/redo.bat

@ -0,0 +1,19 @@
@echo off
C:\Projects\cpu_design_and_emulator\build\debug\x86-64\build\XPM\xpmtobyte-d64.exe
:loop1
timeout /t 1 /nobreak >nul 2>&1
tasklist | find /i "prog1.exe" >nul 2>&1
if errorlevel 1 goto cont1
goto loop1
:cont1
C:\Projects\cpu_design_and_emulator\build\debug\x86-64\build\ASM\custom_asm_macro_compiler-d64.exe
:loop2
timeout /t 1 /nobreak >nul 2>&1
tasklist | find /i "prog2.exe" >nul 2>&1
if errorlevel 1 goto cont2
goto loop2
:cont2
start "cpu" "C:\Projects\cpu_design_and_emulator\build\debug\x86-64\build\CPU\cpudesign-d64.exe"

15
program_data/segment_number_one.xpm

@ -0,0 +1,15 @@
/* XPM */
static char * C:\Projects\cpu_design_and_emulator\program_data\segment_number_one_xpm[] = {
"8 8 4 1",
" c None",
". c #000000",
"+ c #FFFFFF",
"@ c #5F5F5F",
"........",
"...++@..",
"..+@+@..",
".+@.+@..",
"....+@..",
"....+@..",
"....+@..",
"........"};

4
test.txt → program_data/test.txt

@ -1,3 +1,4 @@
includeImageFile(C:\Projects\cpu_design_and_emulator\program_data\segment_number_one.json);
fDecl(test1){
OUT 0x03;
fSetReturn();
@ -11,4 +12,5 @@ fDecl(test2){
}
OUT 0x01;
fCall(test2);
OUT 0x06;
OUT 0x06;
PPU 0x12, 0x1a, 0x00;

48
program_data/testDECOMPILED.txt

@ -0,0 +1,48 @@
4112 | 1010: MOV 0x02, 0x4d
4115 | 1013: MOV 0x03, 0x10
4118 | 1016: MOV 0x04, 0x44
4121 | 1019: MOV 0x05, 0x10
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
4159 | 103f: PPU 0x12, 0x1a, 0x00
4163 | 1043: END
4164 | 1044: NOP
4165 | 1045: NOP
4166 | 1046: NOP
4167 | 1047: BSR 0x5f
4169 | 1049: BSR 0xff
4171 | 104b: NOP
4172 | 104c: NOP
4173 | 104d: NOP
4174 | 104e: NOP
4175 | 104f: CLI
4176 | 1050: NOP
4177 | 1051: LOD 0x16
4179 | 1053: PCC 0x16
4181 | 1055: NOP
4182 | 1056: NOP
4183 | 1057: NOP
4184 | 1058: NOP
4185 | 1059: NOP
4186 | 105a: NOP
4187 | 105b: NOP

BIN
program_data/testOUTPUT.bin

14
segment_number_one.xpm

@ -1,14 +0,0 @@
/* XPM */
static char * C:\Projects\cpu_design_and_emulator\segment_number_one_xpm[] = {
"8 8 3 1",
" c #000000",
". c #FFFFFF",
"+ c #5F5F5F",
" ",
" ..+ ",
" .+.+ ",
" .+ .+ ",
" .+ ",
" .+ ",
" .+ ",
" "};

26
testDECOMPILED.txt

@ -1,26 +0,0 @@
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

@ -1 +0,0 @@
����@6� % &�@� % &��@&��

20
xpm_to_ppubytecode/converter/converter.cpp

@ -63,11 +63,21 @@ void converter::readColor(const std::string& line, const unsigned int& counter)
std::string shortened = line.substr(5, 11);
std::string zero = "0x";
bcode->bytePalette[counter].ident = line[0];
bcode->bytePalette[counter].r = zero + shortened[0] + shortened[1];
bcode->bytePalette[counter].g = zero + shortened[2] + shortened[3];
bcode->bytePalette[counter].b = zero + shortened[4] + shortened[5];
if (shortened == "one") {
auto& p = bcode->bytePalette[0];
if ((p.r == p.g && p.g == p.b && p.b == "0x00")) {
bcode->bytePalette[counter].ident = line[0];
bcode->bytePalette[counter].r = "0x00";
bcode->bytePalette[counter].g = "0x00";
bcode->bytePalette[counter].b = "0x00";
}
}
else {
bcode->bytePalette[counter].ident = line[0];
bcode->bytePalette[counter].r = zero + shortened[0] + shortened[1];
bcode->bytePalette[counter].g = zero + shortened[2] + shortened[3];
bcode->bytePalette[counter].b = zero + shortened[4] + shortened[5];
}
}
void converter::readPixel(const std::string& line, const unsigned int& counter) {

4
xpm_to_ppubytecode/main.cpp

@ -3,6 +3,6 @@
#include "reader/reader.h"
int main() {
reader xpmtobyte("C:\\Projects\\cpu_design_and_emulator\\segment_number_one.json");
xpmtobyte.readFile("C:\\Projects\\cpu_design_and_emulator\\segment_number_one.xpm");
reader xpmtobyte("C:\\Projects\\cpu_design_and_emulator\\program_data\\segment_number_one.json");
xpmtobyte.readFile("C:\\Projects\\cpu_design_and_emulator\\program_data\\segment_number_one.xpm");
}

2
xpm_to_ppubytecode/reader/reader.cpp

@ -48,7 +48,7 @@ void reader::writeOutput() {
output.open(ofileName);
if (output.is_open()) {
output << std::setw(4)<< out;
output << out;
output.close();
}
}

Loading…
Cancel
Save