Page 1 of 1

Force a Fixed Memory location for Variable

Posted: Sun Mar 10, 2019 12:06 pm
by Jay Dee
Hi,
Is there a way to force FC to store a variable in a specific memory location?

Why
I'm looking to simplify how an external program accesses variables from a PIC via USB serial.
If I could force a variable to save to a specific address location, I could then always use this location in future FC programs. The external program should then always be able to just read out that specific address. This might all sound a bit screwy but it quite specific to a setup I've been playing around with.

Yep, I have also been using the EEPROM for saving static values, works OK. But for highly dynamic variable, I dontt want to keep writing them to EE mem every few milliseconds.

Any ideas? J.

Re: Force a Fixed Memory location for Variable

Posted: Sun Mar 10, 2019 12:36 pm
by kersing
Variables are stored in RAM. Starting any external program would very likely wipe the contents of that RAM or at least initialize part of it. Also, how do you want to run two programs one one controller?

There might be ways to force FC to use a specific RAM location to store a variable, however what is possible depends on the controller you are using (and the compiler FC uses to generate the code).

Re: Force a Fixed Memory location for Variable

Posted: Sun Mar 10, 2019 1:04 pm
by Jay Dee
Ah yeah.. the compiler. Forgot that step.. could be kinda hard to work around that.

Currently I have a tool that allows me to read values stored at a specific address and that works in my testing so far. So the tricky bit of reading the data back from the PIC over USB Serial is basically sorted. The tool is only for the PIC ECIO units... so my bench testing is currently on a 18F2455

However the current method requires extracting the memory location of each variable from a flowcode debug file, with this information each variable can be individually queried. This does however add steps to the process.
So I thought... If I could force my FC or supplementary C to write specific variables to known memory locations, i could skip the extraction step.
All a work in progress. :)

Re: Force a Fixed Memory location for Variable

Posted: Sun Mar 10, 2019 7:21 pm
by kersing
Forcing the PIC compiler to use specific memory locations should be doable, however this will have a huge impact on the optimizations the compiler does to fit variables and arrays into the available RAM. PIC RAM is not exactly the easiest to work with due to the way memory banks are designed and by fixing certain variables at certain positions you are severely hampering the compiler in choosing the optimal layout.

Wouldn’t be easier to use a small program (script) to extract the required information from the compiler output?

Re: Force a Fixed Memory location for Variable

Posted: Mon Mar 11, 2019 4:09 pm
by Jay Dee
Hi Kersing,
I agree it may well be less than optimal and yes currently the extraction is currently perfomed by a PHP script. This chain works but I'm just trying to speed up some aspects of the process. I have found during deveolopement, that sometimes I need to re-run through the process, this can be a pain when tyring to push a project forward. During dev, I would happliy exchange some inefficiency in compiled code over speed of turnaround.

It is really about looking at the optioins, trying them and drawing conclusions on the results.

After a bit more reading the XC8 compiler does allow 'absolute addressing' , not knowing the details of what is being described I'm unsure of the method to try. It is also suggested that a 'sensible' locatin in memory is chosen. However not having an extensive knowloedge of the memory map any suggestions are appreciated.
As to the method employed, ref;
http://microchipdeveloper.com/faq:38

Which of these options do people this would be most suitable to try;
Using __section specifier with CCI syntax enabled
Using __at specifier with CCI syntax enabled
Absolute variables in Data Space using '@' qualifier:
Absolute variables in Program Space using '@' qualifier:
Absolute functions using '@' qualifier:

Thanks, J.

Re: Force a Fixed Memory location for Variable

Posted: Mon Mar 11, 2019 10:39 pm
by kersing
If you want to use this for Flowcode variables you will need some magic to include the right modifiers when it’s generating code. Ben will probably know what magic to apply as well as the right directive to use.

Re: Force a Fixed Memory location for Variable

Posted: Tue Mar 12, 2019 12:17 pm
by Benj
Here's an example of the magic in practise :wink:

I have created a Compile -> AddVar Event macro that is called repeatedly when we are generating the C code for variables.

I simply check for a specific variable name and when I find it I slightly change the code generation to add a location for the variable.
VarLocations.fcfx
(9.33 KiB) Downloaded 174 times
The project generates the following code.

Code: Select all

MX_GLOBAL MX_UINT8 FCV_TESTC;
MX_GLOBAL MX_UINT8 FCV_TESTB @ 0x101;
MX_GLOBAL MX_UINT8 FCV_TESTA @ 0x100;
Hope this helps.

Re: Force a Fixed Memory location for Variable

Posted: Thu Mar 14, 2019 11:21 am
by Jay Dee
Great stuff Ben, I'll have a play next week. Currently trying to climb out from under the bulk of work that built up whilst away from the office! J. :)