324 lines
19 KiB
C#
324 lines
19 KiB
C#
using TuneBoss.Core.Protocol;
|
||
|
||
namespace TuneBoss.Core.Models;
|
||
|
||
/// <summary>
|
||
/// Describes a single memory region on the device — its address and read length.
|
||
/// </summary>
|
||
public sealed record MemoryRegion(ushort Address, byte Length)
|
||
{
|
||
public byte AddrLo => (byte)(Address & 0xFF);
|
||
public byte AddrHi => (byte)(Address >> 8);
|
||
public byte[] AddrBytes => new[] { AddrLo, AddrHi };
|
||
|
||
/// <summary>True if this region exists (address != 0x0000 or explicit availability flag).</summary>
|
||
public bool IsAvailable { get; init; } = true;
|
||
|
||
public static MemoryRegion Unavailable => new(0x0000, 0) { IsAvailable = false };
|
||
}
|
||
|
||
/// <summary>
|
||
/// Complete memory address map for a connected TuneBoss device.
|
||
/// Constructed once after EcuInfo is parsed; replaces the static memAddress class.
|
||
///
|
||
/// ── Generation mapping ────────────────────────────────────────────────────────
|
||
/// Gen1 (AppInfoVersion=245): legacy addresses, APP_MEMRD_SEQ for all ops
|
||
/// Gen2 (AppInfoVersion=1) : dedicated read commands, single-cylinder layout
|
||
/// Gen3 (AppInfoVersion=2) : same as Gen2 + separate 2D_USER address space
|
||
///
|
||
/// ── 1D table FW gates (from populateInitTables) ───────────────────────────────
|
||
/// Tables 1–18 : always (base set)
|
||
/// Tables 19–24 : FW_Version >= 80
|
||
/// Tables 25–26 : FW_Version >= 83
|
||
/// Tables 27–34 : FW_Version >= 90
|
||
/// Tables 35–37 : FW_Version >= 130
|
||
///
|
||
/// ── Misc FW gates ─────────────────────────────────────────────────────────────
|
||
/// Core (136B) : always
|
||
/// LaunchFuel (12B) : FW_Version >= 150
|
||
/// LaunchFuel2 (12B) : FW_Version >= 150
|
||
/// AuxOut (12B) : FW_Version >= 150
|
||
/// Extended (var B) : FW_Version >= 150, size from EcuVariant
|
||
/// </summary>
|
||
public sealed record MemoryLayout
|
||
{
|
||
// ── 2D main tuning tables ─────────────────────────────────────────────────
|
||
public MemoryRegion Fuel_PulseWidth1 { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Spark_Angle1 { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion AFR_Target1 { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Fuel_Angle2D { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Secondary_Load_Conversion { get; init; } = MemoryRegion.Unavailable;
|
||
|
||
// ── Acceleration-compensation 2D tables (FW_Version >= 197) ───────────────
|
||
// 228-byte tables in the MAIN 2D address space (base 2D opcodes — NOT user tables),
|
||
// at slots 9/10/11 (index × 228): 0x0804 / 0x08E8 / 0x09CC. The firmware allocates
|
||
// the cyl2 base tables (slots 5-8) on every unit, so accel comp sits at a fixed
|
||
// 9-11 regardless of cylinder count.
|
||
public MemoryRegion Accel_Fuel_Comp { get; init; } = MemoryRegion.Unavailable; // 0x0804 (9×228)
|
||
public MemoryRegion Accel_Spark_Comp { get; init; } = MemoryRegion.Unavailable; // 0x08E8 (10×228)
|
||
public MemoryRegion Accel_FuelAngle_Comp { get; init; } = MemoryRegion.Unavailable; // 0x09CC (11×228)
|
||
|
||
// ── 2D user-adjustable tables (cyl1) ─────────────────────────────────────
|
||
public MemoryRegion Fuel_PulseWidth_Adjust1 { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Spark_Angle_Adjust1 { get; init; } = MemoryRegion.Unavailable;
|
||
|
||
// ── Subtables (cyl1) ─────────────────────────────────────────────────────
|
||
public MemoryRegion AFR_Target1_SubTable { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Fuel_PulseWidth_Adjust1_SubTable { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Spark_Angle_Adjust1_SubTable { get; init; } = MemoryRegion.Unavailable;
|
||
|
||
// ── 2D tables cyl2 (isTwoCylinder only) ──────────────────────────────────
|
||
public MemoryRegion Fuel_PulseWidth2 { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Spark_Angle2 { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion AFR_Target2 { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Fuel_Angle2D_2 { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Fuel_PulseWidth_Adjust2 { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Spark_Angle_Adjust2 { get; init; } = MemoryRegion.Unavailable;
|
||
|
||
// ── Subtables cyl2 ───────────────────────────────────────────────────────
|
||
public MemoryRegion AFR_Target2_SubTable { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Fuel_PulseWidth_Adjust2_SubTable { get; init; } = MemoryRegion.Unavailable;
|
||
public MemoryRegion Spark_Angle_Adjust2_SubTable { get; init; } = MemoryRegion.Unavailable;
|
||
|
||
// ── 1D tables (up to 37 depending on FW_Version) ─────────────────────────
|
||
// Index 0-based: OneDimTables[0] = table 1, OneDimTables[36] = table 37
|
||
// Unavailable entries have IsAvailable=false and are skipped by the service layer.
|
||
public MemoryRegion[] OneDimTables { get; init; } = new MemoryRegion[37];
|
||
|
||
// ── Misc regions ─────────────────────────────────────────────────────────
|
||
public MemoryRegion MiscCore { get; init; } = MemoryRegion.Unavailable; // 136 bytes, always
|
||
public MemoryRegion MiscLaunchFuel { get; init; } = MemoryRegion.Unavailable; // 12B, FW>=150
|
||
public MemoryRegion MiscLaunchFuel2 { get; init; } = MemoryRegion.Unavailable; // 12B, FW>=150
|
||
public MemoryRegion MiscAuxOut { get; init; } = MemoryRegion.Unavailable; // 12B, FW>=150
|
||
public MemoryRegion MiscExtended { get; init; } = MemoryRegion.Unavailable; // var, FW>=150
|
||
|
||
// ── RPM limit misc address ────────────────────────────────────────────────
|
||
public MemoryRegion RpmLimitMisc { get; init; } = MemoryRegion.Unavailable; // 0x63 length
|
||
|
||
// ── Read lengths (fixed from populateInitTables) ──────────────────────────
|
||
public const byte Len2DTable = 0xE3; // 227 — split read
|
||
public const byte LenAFRTable = 0xEF; // 239 — AFR split read
|
||
public const byte Len1DTable = 0x17; // 23 — single read
|
||
public const byte LenSubTable = 0x17; // 23 — single read
|
||
public const byte LenMiscCore = 0x88; // 136
|
||
public const byte LenMiscExt12 = 0x0B; // 11 (12 bytes incl [0])
|
||
public const byte LenRpmCheck = 0x63; // 99
|
||
|
||
// ════════════════════════════════════════════════════════════════════════
|
||
// FACTORY
|
||
// ════════════════════════════════════════════════════════════════════════
|
||
|
||
/// <summary>
|
||
/// Constructs the complete memory layout from parsed EcuInfo.
|
||
/// This is the single entry point — call once after ReadEcuInfoAsync().
|
||
/// </summary>
|
||
public static MemoryLayout For(EcuInfo info, EcuVariant variant)
|
||
{
|
||
var gen = GenerationConfig.ResolveGeneration(info.AppInfoVersion);
|
||
byte fw = info.FwVersion;
|
||
byte fw2sub = info.FwSubVersion2;
|
||
|
||
return gen switch
|
||
{
|
||
GenerationType.Gen1 => BuildGen1(fw, variant),
|
||
GenerationType.Gen2 => BuildGen2(fw, variant, fw2sub),
|
||
GenerationType.Gen3 => BuildGen3(fw, variant, fw2sub),
|
||
_ => throw new InvalidOperationException($"Unknown generation for AppInfoVersion {info.AppInfoVersion}")
|
||
};
|
||
}
|
||
|
||
// ════════════════════════════════════════════════════════════════════════
|
||
// GEN1 — legacy, APP_MEMRD_SEQ for all
|
||
// ════════════════════════════════════════════════════════════════════════
|
||
private static MemoryLayout BuildGen1(byte fw, EcuVariant variant)
|
||
{
|
||
static MemoryRegion R(ushort addr, byte len) => new(addr, len);
|
||
var layout = new MemoryLayout
|
||
{
|
||
// 2D tables at fixed GEN1 addresses
|
||
Fuel_PulseWidth1 = R(0x0000, Len2DTable),
|
||
Spark_Angle1 = R(0x00E4, Len2DTable),
|
||
AFR_Target1 = R(0x01C8, LenAFRTable),
|
||
Fuel_Angle2D = R(0x02AC, Len2DTable),
|
||
Secondary_Load_Conversion = R(0x0390, Len2DTable),
|
||
Fuel_PulseWidth_Adjust1 = R(0x048C, Len2DTable),
|
||
Spark_Angle_Adjust1 = R(0x0588, Len2DTable),
|
||
|
||
// Subtables
|
||
AFR_Target1_SubTable = R(0x02AC, LenSubTable),
|
||
Fuel_PulseWidth_Adjust1_SubTable = R(0x0570, LenSubTable),
|
||
Spark_Angle_Adjust1_SubTable = R(0x066C, LenSubTable),
|
||
|
||
// Misc
|
||
MiscCore = R(0x0834, LenMiscCore),
|
||
RpmLimitMisc = R(0x0840, LenRpmCheck),
|
||
|
||
// GEN1 has no extended misc regions
|
||
MiscLaunchFuel = MemoryRegion.Unavailable,
|
||
MiscLaunchFuel2 = MemoryRegion.Unavailable,
|
||
MiscAuxOut = MemoryRegion.Unavailable,
|
||
MiscExtended = MemoryRegion.Unavailable,
|
||
|
||
OneDimTables = Build1DTables_Gen1(fw),
|
||
};
|
||
return layout;
|
||
}
|
||
|
||
// ════════════════════════════════════════════════════════════════════════
|
||
// GEN2 — dedicated read commands, single/dual cylinder
|
||
// ════════════════════════════════════════════════════════════════════════
|
||
private static MemoryLayout BuildGen2(byte fw, EcuVariant variant, byte fw2sub = 0)
|
||
{
|
||
static MemoryRegion R(ushort addr, byte len) => new(addr, len);
|
||
bool isTwoCyl = variant != EcuVariant.SingleCylinder;
|
||
|
||
return new MemoryLayout
|
||
{
|
||
// Cyl1 2D
|
||
Fuel_PulseWidth1 = R(0x0000, Len2DTable),
|
||
Spark_Angle1 = R(0x00E4, Len2DTable),
|
||
AFR_Target1 = R(0x01C8, LenAFRTable),
|
||
Fuel_Angle2D = R(0x02AC, Len2DTable),
|
||
Secondary_Load_Conversion = R(0x0390, Len2DTable),
|
||
Fuel_PulseWidth_Adjust1 = R(0x0474, Len2DTable),
|
||
Spark_Angle_Adjust1 = R(0x0558, Len2DTable),
|
||
|
||
// Cyl1 subtables
|
||
AFR_Target1_SubTable = R(0x0000, LenSubTable),
|
||
Fuel_PulseWidth_Adjust1_SubTable = R(0x0018, LenSubTable),
|
||
Spark_Angle_Adjust1_SubTable = R(0x0030, LenSubTable),
|
||
|
||
// Cyl2 2D — MAIN space slots 5/6/7/8, matching the firmware's contiguous 2D
|
||
// declaration (0x0474/0x0558/0x063C/0x0720). The memory is allocated on EVERY
|
||
// unit; single-cylinder just doesn't access it (gated isTwoCyl for the nav).
|
||
// (Previously mis-mapped to slots 7-10, which then clashed with accel at 9-11.)
|
||
Fuel_PulseWidth2 = isTwoCyl ? R(0x0474, Len2DTable) : MemoryRegion.Unavailable, // idx 5
|
||
AFR_Target2 = isTwoCyl ? R(0x0558, LenAFRTable) : MemoryRegion.Unavailable, // idx 6
|
||
Fuel_Angle2D_2 = isTwoCyl ? R(0x063C, Len2DTable) : MemoryRegion.Unavailable, // idx 7
|
||
Spark_Angle2 = isTwoCyl ? R(0x0720, Len2DTable) : MemoryRegion.Unavailable, // idx 8
|
||
Fuel_PulseWidth_Adjust2 = isTwoCyl ? R(0x09CC, Len2DTable) : MemoryRegion.Unavailable,
|
||
Spark_Angle_Adjust2 = isTwoCyl ? R(0x0AB0, Len2DTable) : MemoryRegion.Unavailable,
|
||
|
||
// Cyl2 subtables
|
||
AFR_Target2_SubTable = isTwoCyl ? R(0x0048, LenSubTable) : MemoryRegion.Unavailable,
|
||
Fuel_PulseWidth_Adjust2_SubTable = isTwoCyl ? R(0x0060, LenSubTable) : MemoryRegion.Unavailable,
|
||
Spark_Angle_Adjust2_SubTable = isTwoCyl ? R(0x0078, LenSubTable) : MemoryRegion.Unavailable,
|
||
// Acceleration-compensation tables — FW >= 197, MAIN 2D space, slots 9/10/11
|
||
// (index × 228): 0x0804 / 0x08E8 / 0x09CC, right after cyl2 Spark_Angle2 (idx 8).
|
||
// Present on EVERY unit regardless of cylinder count — the cyl2 slots 5-8 are
|
||
// always allocated in firmware, so accel sits at a fixed 9-11 (fw-gate only).
|
||
Accel_Fuel_Comp = fw >= 197 ? R(0x0804, Len2DTable) : MemoryRegion.Unavailable,
|
||
Accel_Spark_Comp = fw >= 197 ? R(0x08E8, Len2DTable) : MemoryRegion.Unavailable,
|
||
Accel_FuelAngle_Comp = fw >= 197 ? R(0x09CC, Len2DTable) : MemoryRegion.Unavailable,
|
||
|
||
// Misc core
|
||
MiscCore = R(0x0000, LenMiscCore),
|
||
RpmLimitMisc = R(0x000C, LenRpmCheck),
|
||
|
||
// Extended misc — FW >= 150
|
||
MiscLaunchFuel = fw >= 150 ? R(0x0088, LenMiscExt12) : MemoryRegion.Unavailable,
|
||
MiscLaunchFuel2 = fw >= 150 ? R(0x0094, LenMiscExt12) : MemoryRegion.Unavailable,
|
||
MiscAuxOut = fw >= 150 ? R(0x00A0, LenMiscExt12) : MemoryRegion.Unavailable,
|
||
MiscExtended = fw >= 150
|
||
? R(0x00AC, MiscValues.ExtendedLengthFor(fw2sub))
|
||
: MemoryRegion.Unavailable,
|
||
|
||
OneDimTables = Build1DTables_Gen2(fw),
|
||
};
|
||
}
|
||
|
||
// ════════════════════════════════════════════════════════════════════════
|
||
// GEN3 — same as Gen2 but 2D_USER in separate address space
|
||
// ════════════════════════════════════════════════════════════════════════
|
||
private static MemoryLayout BuildGen3(byte fw, EcuVariant variant, byte fw2sub = 0)
|
||
{
|
||
// GEN3 user-adjust tables live in a separate address space:
|
||
// Fuel_PulseWidth_Adjust1 = 0x0000, Spark_Angle_Adjust1 = 0x00E4
|
||
// etc. (addressed via APP_MEMRD_2D_USER not APP_MEMRD_2D)
|
||
var base2 = BuildGen2(fw, variant, fw2sub);
|
||
bool isTwoCyl = variant != EcuVariant.SingleCylinder;
|
||
|
||
static MemoryRegion R(ushort addr, byte len) => new(addr, len);
|
||
|
||
return base2 with
|
||
{
|
||
// Override user-adjust addresses for GEN3
|
||
Fuel_PulseWidth_Adjust1 = R(0x0000, Len2DTable),
|
||
Spark_Angle_Adjust1 = R(0x00E4, Len2DTable),
|
||
Fuel_PulseWidth_Adjust2 = isTwoCyl ? R(0x01C8, Len2DTable) : MemoryRegion.Unavailable,
|
||
Spark_Angle_Adjust2 = isTwoCyl ? R(0x02AC, Len2DTable) : MemoryRegion.Unavailable,
|
||
// Acceleration-compensation tables are inherited from BuildGen2 (MAIN 2D space,
|
||
// slots 9/10/11, single-cylinder only) — no GEN3 override needed.
|
||
};
|
||
}
|
||
|
||
// ════════════════════════════════════════════════════════════════════════
|
||
// 1D TABLE ADDRESS BUILDERS
|
||
// Note the deliberate addr17→panel19, addr18→panel20 swap observed in
|
||
// populateInitTables — preserved here in array order.
|
||
// ════════════════════════════════════════════════════════════════════════
|
||
private static MemoryRegion[] Build1DTables_Gen1(byte fw)
|
||
{
|
||
var t = Enumerable.Repeat(MemoryRegion.Unavailable, 37).ToArray();
|
||
static MemoryRegion R(ushort a) => new(a, Len1DTable);
|
||
|
||
// Base 18 (addr 0x0684 onwards, 0x18 = 24 byte stride)
|
||
t[0] = R(0x0684); t[1] = R(0x069C); t[2] = R(0x06B4);
|
||
t[3] = R(0x06CC); t[4] = R(0x06E4); t[5] = R(0x06FC);
|
||
t[6] = R(0x0714); t[7] = R(0x072C); t[8] = R(0x0744);
|
||
t[9] = R(0x075C); t[10] = R(0x0774); t[11] = R(0x078C);
|
||
t[12] = R(0x07A4); t[13] = R(0x07BC); t[14] = R(0x07D4);
|
||
t[15] = R(0x07EC); t[16] = R(0x0804); t[17] = R(0x081C);
|
||
return t;
|
||
}
|
||
|
||
private static MemoryRegion[] Build1DTables_Gen2(byte fw)
|
||
{
|
||
var t = Enumerable.Repeat(MemoryRegion.Unavailable, 37).ToArray();
|
||
static MemoryRegion R(ushort a) => new(a, Len1DTable);
|
||
|
||
// Base 18 — always
|
||
t[0] = R(0x0000); t[1] = R(0x0018); t[2] = R(0x0030);
|
||
t[3] = R(0x0048); t[4] = R(0x0060); t[5] = R(0x0078);
|
||
t[6] = R(0x0090); t[7] = R(0x00A8); t[8] = R(0x00C0);
|
||
t[9] = R(0x00D8); t[10] = R(0x00F0); t[11] = R(0x0108);
|
||
t[12] = R(0x0120); t[13] = R(0x0138); t[14] = R(0x0150);
|
||
t[15] = R(0x0168); t[16] = R(0x0180); t[17] = R(0x0198);
|
||
|
||
if (fw >= 80)
|
||
{
|
||
t[18] = R(0x01B0); t[19] = R(0x01C8); t[20] = R(0x01E0);
|
||
t[21] = R(0x01F8); t[22] = R(0x0210); t[23] = R(0x0228);
|
||
}
|
||
if (fw >= 83)
|
||
{
|
||
// Slots 25-26 (Speed Fuel/Spark Comp) ship in FW 83, not 80 —
|
||
// populateInitTables gates these in a nested FW>=83 check.
|
||
t[24] = R(0x0240); t[25] = R(0x0258);
|
||
}
|
||
if (fw >= 90)
|
||
{
|
||
t[26] = R(0x0270); t[27] = R(0x0288); t[28] = R(0x02A0);
|
||
t[29] = R(0x02B8); t[30] = R(0x02D0); t[31] = R(0x02E8);
|
||
t[32] = R(0x0300); t[33] = R(0x0318);
|
||
}
|
||
if (fw >= 130)
|
||
{
|
||
t[34] = R(0x0330); t[35] = R(0x0348); t[36] = R(0x0360);
|
||
}
|
||
|
||
return t;
|
||
}
|
||
|
||
// ── Convenience ──────────────────────────────────────────────────────────
|
||
|
||
/// <summary>
|
||
/// Returns all available 1D table regions in index order.
|
||
/// Skips Unavailable entries (FW-gated tables not present on this device).
|
||
/// </summary>
|
||
public IEnumerable<(int Index, MemoryRegion Region)> Available1DTables =>
|
||
OneDimTables
|
||
.Select((r, i) => (i, r))
|
||
.Where(x => x.r.IsAvailable);
|
||
}
|