The core CHDK code now include some changes I made for EXMEM (changeset 1124), including the code to build and load CHDK itself into the EXMEM memory space.
I thought I'd share some of what I've learned about using EXMEM on the G12 & SX30 and some instructions on how to go about using it on other recent cameras. This is complex and not for the faint hearted - you've been warned
First here's a simplified memory map for recent dryos cameras like the G12 & SX30 (not to scale). The numbers are in HEX, the question marks are values that change from camera to camera:
Without CHDK
============
variables heap/stack buffers (e.g. RAW) exmem pool vid buffers
|----|-----------|------------------|----------------------|---------------------|-----------|
0 1900 ? 4000000 ? ? MAXRAMADDR
With CHDK (default memory)
==========================
variables CHDK heap/stack buffers (e.g. RAW) exmem pool vid buffers
|----|-----------|----|-------------|----------------------|---------------------|-----------|
0 1900 ? ? 4000000 ? ? MAXRAMADDR
With CHDK (exmem memory)
==========================
variables heap/stack buffers (e.g. RAW) exmem pool CHDK vid buffers
|----|-----------|------------------|----------------------|-------------|-------|-----------|
0 1900 ? 4000000 ? ? ? MAXRAMADDR
Moving CHDK into the EXMEM area means that the Canon heap is left at its original size, while still making more memory available for CHDK - everyone wins!
In order to get this working on a camera there are a few steps involved:
1. Find MAXRAMADDR and add to PLATFORM/PLATFORMSUB/makefile.inc if not already there.
2. Find the exmem_alloc routine in the firmware, and enable EXMEM in the build (OPT_EXMEM_MALLOC).
3. Define a value for EXMEM_HEAP_SIZE.
4. Enable OPT_EXMEM_TESTING to determine if the allocated memory is safe to use.
5. If necessary, determine the value for EXMEM_HEAP_SKIP.
6. Turn off EXMEM testing and let CHDK use the EXMEM memory.
7. Enable CHDK to load into EXMEM.
Step 1.MAXRAMADDR defines the upper limit of the cameras RAM. If it has not been set for your camera you need to determine what the value is and add it to the PLATFORM/PLATFORMSUB/makefile.inc file. For example cameras with 128MB of RAM will have MAXRAMADDR=0x07FFFFFF, cameras with 64MB of RAM will have MAXRAMADDR=0x03FFFFFF.
Search in the firmware for a function that starts like the below (the addresses and values will be different):
ROM:FF890270 EXPORT _sub_FF890270__ExMemMan.c__0
ROM:FF890270 _sub_FF890270__ExMemMan.c__0 ; CODE XREF: sub_FF88FD1C+AC
ROM:FF890270 STMFD SP!, {R4-R8,LR}
ROM:FF890274 LDR R6, =0x3BFC8
ROM:FF890278 MOV R4, R0
ROM:FF89027C MOV R0, #0
ROM:FF890280 MOV R5, #0x48000000 <<<<<<<<<<<<<<< want this value
ROM:FF890284
ROM:FF890284 loc_FF890284 ; CODE XREF: _sub_FF890270__ExMemMan.c__0+38
ROM:FF890284 LDR R1, [R6,R0,LSL#3]
We need the value marked in the code above and it will probably be either 0x48000000 (128MB) or 0x44000000 (64MB). The value will have the 'uncached' memory bit set (0x40000000), subtract that and the result is the amount of RAM on the camera. Set the MAXRAMADDR value in makefile.inc to be one less than this value. In the example above MAXRAMADDR will be 0x7ffffff (0x8000000 - 1).
Step 2.Is covered in reyalp's first post.
Step 3.The default value for EXMEM_HEAP_SIZE in the code is 2MB if you don't define a value.
You should add a 4MB value into PLATFORM/PLATFORMSUB/makefile.inc.
Starting with 4MB is needed in case the camera has video buffers that need to be skipped (see later).
PLATFORM/PLATFORMSUB/makefile.inc:
EXMEM_BUFFER_SIZE=0x400000 # Amount of EXMEM memory to allocate for CHDK = 4MB
Step 4.Enable OPT_EXMEM_TESTING in buildconf.inc. Rebuild CHDK and load it onto your camera. This enables exmem testing, which allocates the memory; but does not use it. instead it checks the memory for corruption.
You should now have 'OK" displayed at the bottom left of the LCD. Test the camera & CHDK functions as much as possible. If the OK disappears and is replaced by a bunch of numbers then this means that the EXMEM memory has been corrupted by some camera function (most likely recording a video and using the video buffers shown in the memory map above).
The info displayed on two lines:
s:XXXXXXXX e:YYYYYYYY
f:AAAAAAAA l:BBBBBBBB c:NN
XXXXXXXX = start address of the EXMEM allocated memory buffer
YYYYYYYY = end address of the EXMEM allocated memory buffer
AAAAAAAA = first corrupt memory location
BBBBBBBB = last corrupt memory location
NN = number of corrupt memory locations found
If the first corrupt location is at the start of the EXMEM allocated memory buffer then reduce the size of EXMEM_HEAP_SIZE to 2MB and try again. If that still fails then you probably won't be able to use EXMEM.
If the first corrupt location is not at the start of the memory then it is most likely a problem with the video buffers, use step 5 below to try and fix this.
If everything works and no corruption occurs you should be good to go to step 6.
Continued in next post.