spo/ass3/simulator_SIC_XE/include/loader.h
2025-12-21 17:23:05 +01:00

77 lines
No EOL
1.6 KiB
C++

#ifndef LOADER_H
#define LOADER_H
#include <memory>
#include <string>
#include <vector>
#include "file_reader.h"
#include <stdexcept>
class Machine;
using std::shared_ptr;
using std::string;
class Loader {
public:
Loader( shared_ptr<Machine> machine, string filename) : _machine(machine), _filename(filename) {
_file_reader = std::make_shared<FileReader>(filename, std::ios::in);
if (!_file_reader->good()) {
throw std::runtime_error("Loader: failed to open file: " + filename);
}
}
~Loader();
enum class RecordType {
HEADER,
TEXT,
MODIFICATION,
END,
UNKNOWN
};
struct HeaderMetadata {
string program_name;
int start_address;
int length;
};
struct TextRecord {
int start_address;
std::vector<uint8_t> data;
};
struct ModificationRecord {
int address; // Address to be modified
int length; // Length in nibbles
bool add; // true for +, false for -
};
struct EndRecord {
int execution_start_address;
};
void load();
private :
static RecordType parseRecordType(char c);
shared_ptr<Machine> _machine;
string _filename;
shared_ptr<FileReader> _file_reader;
int _relocation_address;
HeaderMetadata readHeader();
TextRecord readTextRecord();
ModificationRecord readModificationRecord();
EndRecord readEndRecord();
bool load_into_memory(int start_address, const std::vector<uint8_t>& data);
void applyModification(const ModificationRecord& mod);
};
#endif // LOADER_H