added M records
This commit is contained in:
parent
e0ce2fb3d0
commit
a711223abf
11 changed files with 297 additions and 20 deletions
|
|
@ -17,6 +17,7 @@ Loader::~Loader()
|
|||
void Loader::load()
|
||||
{
|
||||
HeaderMetadata header = readHeader();
|
||||
_relocation_address = header.start_address;
|
||||
|
||||
while(true) {
|
||||
RecordType type = parseRecordType(static_cast<char>(_file_reader->readByte()));
|
||||
|
|
@ -28,6 +29,11 @@ void Loader::load()
|
|||
}
|
||||
break;
|
||||
}
|
||||
case RecordType::MODIFICATION: {
|
||||
ModificationRecord modRecord = readModificationRecord();
|
||||
applyModification(modRecord);
|
||||
break;
|
||||
}
|
||||
case RecordType::END: {
|
||||
EndRecord endRecord = readEndRecord();
|
||||
_machine->setPC(endRecord.execution_start_address);
|
||||
|
|
@ -45,6 +51,7 @@ Loader::RecordType Loader::parseRecordType(char c)
|
|||
switch (c) {
|
||||
case 'H': return RecordType::HEADER;
|
||||
case 'T': return RecordType::TEXT;
|
||||
case 'M': return RecordType::MODIFICATION;
|
||||
case 'E': return RecordType::END;
|
||||
default: return RecordType::UNKNOWN; // fallback; adjust as needed
|
||||
}
|
||||
|
|
@ -105,6 +112,29 @@ Loader::TextRecord Loader::readTextRecord()
|
|||
return record;
|
||||
}
|
||||
|
||||
Loader::ModificationRecord Loader::readModificationRecord()
|
||||
{
|
||||
ModificationRecord record;
|
||||
if(FILE_CONTAINS_WHITE_SPACES) _file_reader->readByte();
|
||||
|
||||
record.address = std::stoi(_file_reader->readString(6), nullptr, 16);
|
||||
if(FILE_CONTAINS_WHITE_SPACES) _file_reader->readByte();
|
||||
|
||||
record.length = std::stoi(_file_reader->readString(2), nullptr, 16);
|
||||
|
||||
record.add = true;
|
||||
std::string rest = _file_reader->readLine();
|
||||
// Remove whitespace
|
||||
rest.erase(std::remove_if(rest.begin(), rest.end(), ::isspace), rest.end());
|
||||
if (!rest.empty()) {
|
||||
if (rest[0] == '-') {
|
||||
record.add = false;
|
||||
}
|
||||
}
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
Loader::EndRecord Loader::readEndRecord()
|
||||
{
|
||||
EndRecord record;
|
||||
|
|
@ -132,3 +162,54 @@ bool Loader::load_into_memory(int start_address, const std::vector<uint8_t> &dat
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Loader::applyModification(const ModificationRecord& mod)
|
||||
{
|
||||
// M record specifies address and length in half-bytes (nibbles)
|
||||
// We need to modify the value at that address by adding or subtracting
|
||||
// the relocation address
|
||||
|
||||
int address = mod.address;
|
||||
int halfBytes = mod.length;
|
||||
|
||||
// Calculate how many full bytes we need to read
|
||||
// halfBytes can be odd or even
|
||||
int numBytes = (halfBytes + 1) / 2;
|
||||
|
||||
if (address < 0 || address + numBytes > MEMORY_SIZE) {
|
||||
throw std::runtime_error("Modification address out of bounds");
|
||||
}
|
||||
|
||||
// Read the current value from memory
|
||||
int currentValue = 0;
|
||||
for (int i = 0; i < numBytes; ++i) {
|
||||
currentValue = (currentValue << 8) | _machine->getByte(address + i);
|
||||
}
|
||||
|
||||
// If odd number of half-bytes, we only modify the relevant nibbles
|
||||
// For simplicity, we'll work with the full bytes and mask appropriately
|
||||
int mask = 0;
|
||||
for (int i = 0; i < halfBytes; ++i) {
|
||||
mask = (mask << 4) | 0xF;
|
||||
}
|
||||
|
||||
// Extract the value to modify
|
||||
int shift = (numBytes * 2 - halfBytes) * 4;
|
||||
int valueToModify = (currentValue >> shift) & mask;
|
||||
|
||||
// Apply modification
|
||||
int newValue = mod.add ? (valueToModify + _relocation_address)
|
||||
: (valueToModify - _relocation_address);
|
||||
|
||||
// Mask to keep only the relevant bits
|
||||
newValue &= mask;
|
||||
|
||||
// Reconstruct the full value
|
||||
int preservedBits = currentValue & ~(mask << shift);
|
||||
int finalValue = preservedBits | (newValue << shift);
|
||||
|
||||
// Write back to memory (big-endian)
|
||||
for (int i = 0; i < numBytes; ++i) {
|
||||
_machine->setByte(address + i, (finalValue >> ((numBytes - 1 - i) * 8)) & 0xFF);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue