But...however, basic code, not extra algorithms uneeded!
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip> // For formatting hex output
using namespace std;
// Function to display a portion of the ROM file in hexadecimal
void displayHex(const vector<unsigned char>& romData, size_t start, size_t length) {
for (size_t i = start; i < start + length && i < romData.size(); ++i) {
cout << hex << setw(2) << setfill('0') << (int)romData[i] << " ";
if ((i - start + 1) % 16 == 0) {
cout << endl;
}
}
cout << endl;
}
// Function to load the ROM file into memory
vector<unsigned char> loadROM(const string& filename) {
ifstream file(filename, ios::binary | ios::ate);
if (!file.is_open()) {
cerr << "Error opening file!" << endl;
exit(1);
}
size_t size = file.tellg();
file.seekg(0, ios::beg);
vector<unsigned char> romData(size);
file.read(reinterpret_cast<char*>(romData.data()), size);
file.close();
return romData;
}
// Function to save the modified ROM data back to the file
void saveROM(const string& filename, const vector<unsigned char>& romData) {
ofstream file(filename, ios::binary);
if (!file.is_open()) {
cerr << "Error saving file!" << endl;
exit(1);
}
file.write(reinterpret_cast<const char*>(romData.data()), romData.size());
file.close();
}
// Function to edit a byte at a specific location in the ROM data
void editByte(vector<unsigned char>& romData, size_t index, unsigned char newValue) {
if (index >= romData.size()) {
cerr << "Invalid index!" << endl;
exit(1);
}
romData[index] = newValue;
}
int main() {
string romFile = "yourfile.rom"; // Replace with your ROM file path
vector<unsigned char> romData = loadROM(romFile);
size_t startAddress = 0x1000; // Example starting address for display
size_t length = 64; // Display length in bytes
cout << "Displaying ROM data starting from address 0x" << hex << startAddress << ":" << endl;
displayHex(romData, startAddress, length);
// Ask user for byte to modify
size_t indexToEdit;
cout << "Enter the index (in decimal) of the byte you want to edit: ";
cin >> indexToEdit;
unsigned char newHexValue;
cout << "Enter the new value (in hex, e.g., 0xAA): ";
cin >> hex >> newHexValue; // Input in hexadecimal format
// Edit the byte at the specified index
editByte(romData, indexToEdit, newHexValue);
// Save the modified ROM back to the file
saveROM(romFile, romData);
cout << "ROM modified and saved successfully!" << endl;
return 0;
}
~Z