Designing non-destructive marching tests
The plan is to implement a boot-time RAM diagnostic in C that tests each memory word non-destructively by saving its original content to a local variable, running marching ones and zeros patterns on each bit to detect stuck-at and coupling faults, verifying the writes by reading back, then restoring the original data. This approach tests individual bit faults within words without clearing or overwriting the entire memory region, keeping the test transparent and safe for boot-time use.
At power‑on you don't have the luxury of allocating a shadow buffer for the entire RAM image, so a transparent algorithm has to work one word at a time: take a copy of the current contents into a temporary, exercise the cell with a set of patterns and then put the original data back.
A simple pair of “marching” tests that will expose stuck–at‑0/1 faults and bit‑to‑bit coupling faults in a word looks like this:
- marching‑ones: write a single ‘1’ into each bit position while all other bits are 0, read back and compare. If a bit is stuck at 0 you will never read the ‘1’. If writing a ‘1’ into bit i disturbs bit j because of a coupling fault you will see the unexpected bit set when you read back.
- marching‑zeros: the inverse – write a single ‘0’ into each position with all others at ‘1’. This exposes stuck‑at‑1 and complementary coupling faults.
The code below implements these two passes non‑destructively over a region of 32‑bit words. Each word is saved in a register‑sized temporary, tested, and then restored:
#include <stdint.h>
#include <stddef.h>
/**
* Run a non-destructive marching ones/zeros test over [base, base+words).
* Returns 0 on success, nonzero on first failing address.
*/
int ram_march_test(volatile uint32_t *base, size_t words)
{
for (size_t i = 0; i < words; ++i) {
volatile uint32_t *p = base + i;
uint32_t original = *p; /* save current content in a register */
/* Marching ones: single '1' walking across the word */
for (unsigned bit = 0; bit < 32; ++bit) {
uint32_t pattern = 1u << bit;
*p = pattern;
uint32_t rd = *p;
if (rd != pattern) {
/* restore original before returning */
*p = original;
return (int)i;
}
}
/* Marching zeros: single '0' walking across an all-ones word */
for (unsigned bit = 0; bit < 32; ++bit) {
uint32_t pattern = ~(1u << bit);
*p = pattern;
uint32_t rd = *p;
if (rd != pattern) {
*p = original;
return (int)i;
}
}
/* restore original content */
*p = original;
}
return 0;
}
Because every write is followed by a read and the word is restored before moving on, the test is transparent to the rest of the system. You can call this routine early in your boot code with the address range you want to verify:
extern uint32_t _sram_start[];
extern uint32_t _sram_end[];
void boot_selftest(void)
{
if (ram_march_test(_sram_start, (_sram_end - _sram_start)) != 0) {
/* handle error: halt, light LED, etc. */
}
/* continue boot */
}
On a wider or narrower memory you can adjust the loop limit accordingly. If you are testing the region that contains your stack, avoid re‑using the same stack frame while you are exercising that address; on many platforms a separate minimal stack in a known‑good area is used during the self‑test.