Copying a Procedure to RAM
Posted: Sun Mar 31, 2024 11:04 am
What is the best way to copy a procedure, eg. from Flash memory to RAM?
For example:
Getting the procedure's starting address is straight forward, but how to detect its ending address? Scanning for 'pop' is not safe, as any relevant constants in code memory at the end of the procedure can be missed.
Here's my current approach, which relies on the existence of 'p1', which works for my current use case (I have verified that 'p1' follows 'p0' in code memory):
Is there a better way? Scanning for 'push{... lr}'?
For example:
Code: Select all
MODULE M;
PROCEDURE p0;
END p0;
PROCEDURE p1;
END P1;
END M.
Here's my current approach, which relies on the existence of 'p1', which works for my current use case (I have verified that 'p1' follows 'p0' in code memory):
Code: Select all
IMPORT SYSTEM;
CONST RamAddr = 020040000H;
TYPE P = PROCEDURE;
VAR p0ram: P;
PROCEDURE copyProc;
VAR addrBegin, addrNext, p0RamAddr : INTEGER;
BEGIN
addrBegin := SYSTEM.ADR(p0); addrNext := SYSTEM.ADR(p1);
p0RamAddr := RamAddr;
SYSTEM.COPY(addrBegin, RamAddr, (addrNext - addrBegin) DIV 4);
p0ram := SYSTEM.VAL(P, p0RamAddr)
END copyProc;