From 3db897baa99c2e82ccfefdf704beea5db2447ac2 Mon Sep 17 00:00:00 2001 From: Arno Rehn Date: Wed, 18 Mar 2026 14:55:23 +0100 Subject: [PATCH] SAM_SPI: Fix IndexOutOfRangeException in SetDmaAccessWidth When the SPI Mode Register PCS field is set to 0xF (no chip select active), ChipSelect() computes selectedSlaveAddr=4 via GetLeastSignificantZero/Logarithm2. SetDmaAccessWidth() then accesses txLengths[4] which is out of bounds for the 4-element array (indices 0-3), causing an unhandled IndexOutOfRangeException that crashes the emulation. Guard the array access in SetDmaAccessWidth() and fall back to byte-width DMA when the computed slave register number exceeds the array bounds. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/Emulator/Peripherals/Peripherals/SPI/SAM_SPI.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Emulator/Peripherals/Peripherals/SPI/SAM_SPI.cs b/src/Emulator/Peripherals/Peripherals/SPI/SAM_SPI.cs index cf58be659..178ca1c07 100644 --- a/src/Emulator/Peripherals/Peripherals/SPI/SAM_SPI.cs +++ b/src/Emulator/Peripherals/Peripherals/SPI/SAM_SPI.cs @@ -357,7 +357,15 @@ private void SetDmaAccessWidth() } else { - DmaWriteAccessWidth = txLengths[SelectedSlaveRegisterNumber] > 0 ? TransferType.Word : TransferType.Byte; + var slaveRegNum = SelectedSlaveRegisterNumber; + if(slaveRegNum >= txLengths.Length) + { + DmaWriteAccessWidth = TransferType.Byte; + } + else + { + DmaWriteAccessWidth = txLengths[slaveRegNum] > 0 ? TransferType.Word : TransferType.Byte; + } } }