#include "HexFile.h" #include #include #include #include #include bool HexFile::load(const std::string& filename) { std::ifstream file(filename, std::ios::binary); if (!file.is_open()) { std::cerr << "Failed to open file: " << filename << std::endl; return false; } // Check file extension to determine format size_t dotPos = filename.find_last_of('.'); if (dotPos != std::string::npos) { std::string ext = filename.substr(dotPos + 1); if (ext == "hex") { return parseIntelHex(filename); } else if (ext == "s19" || ext == "s28" || ext == "s37" || ext == "srec") { return parseMotorolaSRecord(filename); } else if (ext == "bin") { return parseBinary(filename); } } // Default to binary if extension not recognized return parseBinary(filename); } bool HexFile::parseIntelHex(const std::string& filename) { std::ifstream file(filename); if (!file.is_open()) return false; data_.clear(); uint32_t baseAddress = 0; std::string line; while (std::getline(file, line)) { if (line.empty() || line[0] != ':') continue; // Parse Intel HEX record uint8_t byteCount = std::stoi(line.substr(1, 2), nullptr, 16); uint16_t address = std::stoi(line.substr(3, 4), nullptr, 16); uint8_t recordType = std::stoi(line.substr(7, 2), nullptr, 16); if (recordType == 0) { // Data record uint32_t fullAddress = baseAddress + address; if (fullAddress + byteCount > data_.size()) { data_.resize(fullAddress + byteCount, 0xFF); } for (int i = 0; i < byteCount; i++) { uint8_t byte = std::stoi(line.substr(9 + i * 2, 2), nullptr, 16); data_[fullAddress + i] = byte; } } else if (recordType == 2) { // Extended segment address baseAddress = std::stoi(line.substr(9, 4), nullptr, 16) << 4; } else if (recordType == 4) { // Extended linear address baseAddress = std::stoi(line.substr(9, 4), nullptr, 16) << 16; } } return true; } bool HexFile::parseMotorolaSRecord(const std::string& filename) { std::ifstream file(filename); if (!file.is_open()) return false; data_.clear(); std::string line; while (std::getline(file, line)) { if (line.empty() || line[0] != 'S') continue; uint8_t recordType = line[1] - '0'; uint8_t byteCount = std::stoi(line.substr(2, 2), nullptr, 16); if (recordType == 1 || recordType == 2 || recordType == 3) { // Data records uint32_t address; int dataStart; if (recordType == 1) { address = std::stoi(line.substr(4, 4), nullptr, 16); dataStart = 8; } else if (recordType == 2) { address = std::stoi(line.substr(4, 6), nullptr, 16); dataStart = 10; } else { // recordType == 3 address = std::stoi(line.substr(4, 8), nullptr, 16); dataStart = 12; } int dataBytes = byteCount - (dataStart - 2) / 2; if (address + dataBytes > data_.size()) { data_.resize(address + dataBytes, 0xFF); } for (int i = 0; i < dataBytes; i++) { uint8_t byte = std::stoi(line.substr(dataStart + i * 2, 2), nullptr, 16); data_[address + i] = byte; } } } return true; } bool HexFile::parseBinary(const std::string& filename) { std::ifstream file(filename, std::ios::binary | std::ios::ate); if (!file.is_open()) return false; std::streamsize size = file.tellg(); file.seekg(0, std::ios::beg); data_.resize(size); if (!file.read(reinterpret_cast(data_.data()), size)) { data_.clear(); return false; } return true; }