DataHoarder
1566c33d58
All checks were successful
continuous-integration/drone/push Build is passing
83 lines
2.8 KiB
C++
83 lines
2.8 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <vector>
|
|
#include "Parser.h"
|
|
#include "Assembler.h"
|
|
#include "instructions/virtual/Stack.h"
|
|
#include "EmbeddedResource.h"
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc < 3) {
|
|
std::cout << "Usage: " << argv[0] << " code.asm [code2.asm ...] output.bin\n";
|
|
return 1;
|
|
}
|
|
|
|
try {
|
|
|
|
Linker linker;
|
|
|
|
std::vector<EmbeddedResource> embeddedResources = {
|
|
LOAD_RESOURCE(asm_registers_asm),
|
|
LOAD_RESOURCE(asm_api_asm),
|
|
LOAD_RESOURCE(asm_stdlib_asm),
|
|
LOAD_RESOURCE(asm_init_asm)
|
|
};
|
|
|
|
Instruction::Instruction::addDefaultOperators();
|
|
|
|
linker.addVariable("rrcc_STACK_START",
|
|
{Token(Token::Type::RegisterLocation, std::to_string(Instruction::Stack::stackRegisterStart),
|
|
Instruction::Stack::stackRegisterStart)});
|
|
linker.addVariable("rrcc_STACK_POINTER", {Token(Token::Type::RegisterLocation,
|
|
std::to_string(Instruction::Stack::stackOffsetRegisterAddress),
|
|
Instruction::Stack::stackOffsetRegisterAddress)});
|
|
|
|
for (auto &r : embeddedResources) {
|
|
std::vector<uint8_t> bytes(r.begin(), r.end());
|
|
Parser parser(bytes, linker.getVariables());
|
|
parser.parse();
|
|
linker.addFromParser(parser);
|
|
}
|
|
|
|
for (int inputIndex = 1; inputIndex < (argc - 1); ++inputIndex) {
|
|
try {
|
|
std::ifstream image(argv[inputIndex]);
|
|
if (image.is_open()) {
|
|
std::vector<uint8_t> bytes;
|
|
int c;
|
|
while (!image.eof() && (c = image.get()) != EOF) {
|
|
bytes.push_back(c);
|
|
}
|
|
Parser parser(bytes, linker.getVariables());
|
|
parser.parse();
|
|
linker.addFromParser(parser);
|
|
} else {
|
|
throw std::runtime_error("Could not open " + std::string(argv[inputIndex]));
|
|
}
|
|
}catch (const std::exception& e){
|
|
std::cerr << "Exception: " << e.what() << " on input file " << argv[inputIndex] << "\n";
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
Assembler assembler(linker);
|
|
assembler.assemble();
|
|
|
|
std::ofstream patchedImage(argv[argc - 1]);
|
|
|
|
if (patchedImage.is_open()) {
|
|
auto &result = assembler.getImage();
|
|
for (auto c : result) {
|
|
patchedImage.put(c);
|
|
}
|
|
patchedImage.close();
|
|
}
|
|
} catch (const std::exception &e) {
|
|
std::cerr << "Exception: " << e.what() << "\n";
|
|
return 1;
|
|
}
|
|
|
|
|
|
return 0;
|
|
}
|