1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-11-29 10:50:28 +00:00

Add address translate helper function

This makes it a little simpler for plugins to read data from an
arbitrary address when the file data is discontiguous.
This commit is contained in:
Andy McFadden 2020-03-13 15:34:34 -07:00
parent c0de0a8844
commit 662dda106f

View File

@ -35,6 +35,15 @@ namespace PluginCommon {
mAddrMap = addrMap; mAddrMap = addrMap;
} }
/// <summary>
/// Converts a file offset to an address.
/// </summary>
/// <param name="offset">File offset.</param>
/// <returns>24-bit address.</returns>
public int OffsetToAddress(int offset) {
return mAddrMap.OffsetToAddress(offset);
}
/// <summary> /// <summary>
/// Determines the file offset that best contains the specified target address. /// Determines the file offset that best contains the specified target address.
/// </summary> /// </summary>
@ -47,12 +56,37 @@ namespace PluginCommon {
} }
/// <summary> /// <summary>
/// Converts a file offset to an address. /// Returns the data found at the specified address. If the address is out
/// of bounds this throws an AddressException.
/// </summary> /// </summary>
/// <param name="offset">File offset.</param> /// <param name="data">Data array.</param>
/// <returns>24-bit address.</returns> /// <param name="srcOffset">Offset of the address reference. Only matters when
public int OffsetToAddress(int offset) { /// multiple file offsets map to the same address.</param>
return mAddrMap.OffsetToAddress(offset); /// <param name="address">Data address.</param>
/// <returns>Data found.</returns>
public byte GetDataAtAddress(byte[] data, int srcOffset, int address) {
int offset = AddressToOffset(srcOffset, address);
if (offset == -1) {
Exception ex = new AddressTranslateException("Address $" + address.ToString("X4") +
" is outside the file bounds");
ex.Data.Add("Address", address);
throw ex;
}
try {
byte foo = data[offset];
} catch (Exception ex) {
throw new AddressTranslateException("FAILED at srcOff=$" + srcOffset.ToString("x4") +
" addr=$" + address.ToString("x4"));
}
return data[offset];
} }
} }
/// <summary>
/// Exception thrown by AddressTranslate's GetDataAtAddress().
/// </summary>
public class AddressTranslateException : Exception {
public AddressTranslateException() : base() { }
public AddressTranslateException(string msg) : base(msg) { }
}
} }