spo/simulator_SIC_XE/include/loader.h
2025-11-17 15:21:25 +01:00

68 lines
No EOL
1.3 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,
END,
UNKNOWN
};
struct HeaderMetadata {
string program_name;
int start_address;
int length;
};
struct TextRecord {
int start_address;
std::vector<uint8_t> data;
};
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;
HeaderMetadata readHeader();
TextRecord readTextRecord();
EndRecord readEndRecord();
bool load_into_memory(int start_address, const std::vector<uint8_t>& data);
};
#endif // LOADER_H