add reversing
This commit is contained in:
54
src/IO.cpp
54
src/IO.cpp
@@ -1,8 +1,27 @@
|
|||||||
#include "IO.h"
|
#include "IO.h"
|
||||||
|
|
||||||
#include "Assembleur.h"
|
#include "Assembleur.h"
|
||||||
|
#include <algorithm>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
static std::uint32_t reverseInt(std::uint32_t a_Int) {
|
||||||
|
std::uint32_t result = 0;
|
||||||
|
for (int i = 0; i < 32; i++) {
|
||||||
|
result |= a_Int & 0x1;
|
||||||
|
result <<= 1;
|
||||||
|
a_Int >>= 1;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reverseDataByte(BinaryData& a_Data) {
|
||||||
|
for (std::uint32_t& element : a_Data) {
|
||||||
|
element = reverseInt(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BinaryData ParseFile(const std::string& fileName) {
|
BinaryData ParseFile(const std::string& fileName) {
|
||||||
std::ifstream file{fileName};
|
std::ifstream file{fileName};
|
||||||
@@ -45,22 +64,47 @@ BinaryData ParseFile(const std::string& fileName) {
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputFileBinary(const BinaryData& a_Data, const std::string& fileName) {
|
void OutputFileBinary(BinaryData& a_Data, const std::string& fileName, bool a_Reverse) {
|
||||||
std::ofstream file{fileName};
|
std::ofstream file{fileName};
|
||||||
|
if (a_Reverse)
|
||||||
|
reverseDataByte(a_Data);
|
||||||
file.write(reinterpret_cast<const char*>(a_Data.data()), a_Data.size() * sizeof(a_Data.at(0)));
|
file.write(reinterpret_cast<const char*>(a_Data.data()), a_Data.size() * sizeof(a_Data.at(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputFileIntegers(const BinaryData& a_Data, const std::string& fileName) {
|
void OutputFileIntegers(BinaryData& a_Data, const std::string& fileName, bool a_Reverse) {
|
||||||
std::ofstream file{fileName};
|
std::ofstream file{fileName};
|
||||||
|
if (a_Reverse)
|
||||||
|
reverseDataByte(a_Data);
|
||||||
for (std::uint32_t number : a_Data) {
|
for (std::uint32_t number : a_Data) {
|
||||||
file << number << "\n";
|
file << number << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputFileBinIntegers(const BinaryData& a_Data, const std::string& fileName) {
|
void OutputFileBinIntegers(BinaryData& a_Data, const std::string& fileName, bool a_Reverse) {
|
||||||
std::ofstream file{fileName};
|
std::ofstream file{fileName};
|
||||||
|
if (a_Reverse)
|
||||||
|
reverseDataByte(a_Data);
|
||||||
for (std::uint32_t number : a_Data) {
|
for (std::uint32_t number : a_Data) {
|
||||||
file << std::bitset<8>(number >> 24) << " " << std::bitset<8>(number >> 16) << " " << std::bitset<8>(number >> 8) <<
|
file << std::bitset<8>(number >> 24) << " " << std::bitset<8>(number >> 16) << " " << std::bitset<8>(number >> 8) << " "
|
||||||
" " << std::bitset<8>(number) << "\n";
|
<< std::bitset<8>(number) << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void OutputFileLogisim(BinaryData& a_Data, const std::string& fileName, const std::string& a_Header, bool a_Reverse) {
|
||||||
|
std::ofstream file{fileName};
|
||||||
|
file << a_Header << "\n";
|
||||||
|
std::uint64_t cursor = 0;
|
||||||
|
if (a_Reverse)
|
||||||
|
reverseDataByte(a_Data);
|
||||||
|
for (std::uint32_t number : a_Data) {
|
||||||
|
if (cursor % 8 == 0) {
|
||||||
|
file << std::setfill('0') << std::setw(4) << std::hex << cursor << std::dec << ": ";
|
||||||
|
file << std::bitset<8>(number >> 24) << " " << std::bitset<8>(number >> 16) << " " << std::bitset<8>(number >> 8) << " "
|
||||||
|
<< std::bitset<8>(number) << " ";
|
||||||
|
} else {
|
||||||
|
file << std::bitset<8>(number >> 24) << " " << std::bitset<8>(number >> 16) << " " << std::bitset<8>(number >> 8) << " "
|
||||||
|
<< std::bitset<8>(number) << "\n";
|
||||||
|
}
|
||||||
|
cursor += 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
7
src/IO.h
7
src/IO.h
@@ -7,6 +7,7 @@ using BinaryData = std::vector<std::uint32_t>;
|
|||||||
|
|
||||||
BinaryData ParseFile(const std::string& fileName);
|
BinaryData ParseFile(const std::string& fileName);
|
||||||
|
|
||||||
void OutputFileBinary(const BinaryData& a_Data, const std::string& fileName);
|
void OutputFileBinary(BinaryData& a_Data, const std::string& fileName, bool a_Reverse);
|
||||||
void OutputFileIntegers(const BinaryData& a_Data, const std::string& fileName);
|
void OutputFileIntegers(BinaryData& a_Data, const std::string& fileName, bool a_Reverse);
|
||||||
void OutputFileBinIntegers(const BinaryData& a_Data, const std::string& fileName);
|
void OutputFileBinIntegers(BinaryData& a_Data, const std::string& fileName, bool a_Reverse);
|
||||||
|
void OutputFileLogisim(BinaryData& a_Data, const std::string& fileName, const std::string& a_Header, bool a_Reverse);
|
||||||
33
src/main.cpp
33
src/main.cpp
@@ -8,9 +8,7 @@ int main(int argc, char** argv) {
|
|||||||
argparse::ArgumentParser program("Assembleur");
|
argparse::ArgumentParser program("Assembleur");
|
||||||
|
|
||||||
std::string inputFileName;
|
std::string inputFileName;
|
||||||
program.add_argument("file")
|
program.add_argument("file").help("The assembly file to compile").store_into(inputFileName);
|
||||||
.help("The assembly file to compile")
|
|
||||||
.store_into(inputFileName);
|
|
||||||
|
|
||||||
std::string outputFileName;
|
std::string outputFileName;
|
||||||
program.add_argument("-o", "--output")
|
program.add_argument("-o", "--output")
|
||||||
@@ -21,12 +19,25 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
std::string formatType;
|
std::string formatType;
|
||||||
program.add_argument("-f", "--format")
|
program.add_argument("-f", "--format")
|
||||||
.help("Type of the output. [bin|int|binint]")
|
.help("Type of the output. [logisim|bin|int|binint]")
|
||||||
.metavar("type")
|
.metavar("type")
|
||||||
.default_value(std::string("binint"))
|
.default_value(std::string("logisim"))
|
||||||
.choices("bin", "int", "binint")
|
.choices("logisim", "bin", "int", "binint")
|
||||||
.store_into(formatType);
|
.store_into(formatType);
|
||||||
|
|
||||||
|
std::string header;
|
||||||
|
program.add_argument("-h", "--header")
|
||||||
|
.help("Header of the file")
|
||||||
|
.metavar("header")
|
||||||
|
.default_value(std::string("v3.0 hex words addressed"))
|
||||||
|
.store_into(header);
|
||||||
|
|
||||||
|
bool reverse;
|
||||||
|
program.add_argument("-r", "--reversed")
|
||||||
|
.help("inverse bit orders")
|
||||||
|
.flag()
|
||||||
|
.store_into(reverse);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
program.parse_args(argc, argv);
|
program.parse_args(argc, argv);
|
||||||
} catch (const std::exception& err) {
|
} catch (const std::exception& err) {
|
||||||
@@ -38,12 +49,14 @@ int main(int argc, char** argv) {
|
|||||||
try {
|
try {
|
||||||
auto output = ParseFile(inputFileName);
|
auto output = ParseFile(inputFileName);
|
||||||
|
|
||||||
if (formatType == "bin") {
|
if (formatType == "logisim") {
|
||||||
OutputFileBinary(output, outputFileName);
|
OutputFileLogisim(output, outputFileName, header, reverse);
|
||||||
|
} else if (formatType == "bin") {
|
||||||
|
OutputFileBinary(output, outputFileName, reverse);
|
||||||
} else if (formatType == "int") {
|
} else if (formatType == "int") {
|
||||||
OutputFileIntegers(output, outputFileName);
|
OutputFileIntegers(output, outputFileName, reverse);
|
||||||
} else {
|
} else {
|
||||||
OutputFileBinIntegers(output, outputFileName);
|
OutputFileBinIntegers(output, outputFileName, reverse);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (std::runtime_error& e) {
|
} catch (std::runtime_error& e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user