added M records

This commit is contained in:
zanostro 2025-12-21 17:17:52 +01:00
parent e0ce2fb3d0
commit a711223abf
11 changed files with 297 additions and 20 deletions

View file

@ -312,6 +312,24 @@ std::vector<uint8_t> Code::generateInstruction(const InstructionNode* inst, int
bytes.push_back(byte2);
bytes.push_back((displacement >> 8) & 0xFF);
bytes.push_back(displacement & 0xFF);
// Format 4 instructions with symbol references (not immediate values) need M records
bool needsRelocation = false;
if (!operands.empty() && std::holds_alternative<SymbolRef>(operands[0])) {
auto& sym = std::get<SymbolRef>(operands[0]);
// If it's not an immediate mode with a constant, it needs relocation
if (!sym.immediate || _symbolTable.find(sym.name) != _symbolTable.end()) {
needsRelocation = true;
}
}
// Record modification if needed
if (needsRelocation) {
ModificationRecord mod;
mod.address = address + 1; // Skip the opcode+ni byte, start at xbpe+addr
mod.halfBytes = 5; // 5 half-bytes (20 bits) for format 4 address field
_modificationRecords.push_back(mod);
}
} else {
// Format 3: 12-bit displacement
byte2 |= (displacement >> 8) & 0x0F;
@ -443,6 +461,9 @@ std::string Code::emitText() {
oss << std::setfill('0') << std::setw(6) << std::hex << std::uppercase << _programLength;
oss << "\n";
// Clear and rebuild modification records
_modificationRecords.clear();
// T records: text (code/data)
std::vector<uint8_t> code = emitCode();
int textStart = 0;
@ -462,6 +483,14 @@ std::string Code::emitText() {
textStart += textLength;
}
// M records: modifications for format 4 instructions
for (const auto& mod : _modificationRecords) {
oss << "M ";
oss << std::setfill('0') << std::setw(6) << std::hex << std::uppercase << mod.address << " ";
oss << std::setfill('0') << std::setw(2) << std::hex << std::uppercase << mod.halfBytes;
oss << "\n";
}
// E record: execution start address
oss << "E ";
oss << std::setfill('0') << std::setw(6) << std::hex << std::uppercase << _startAddress;