![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
frothbeast
Joined: 08 Jul 2010 Posts: 4
|
Ram array, rolling pointer, offsets, pointer wrap-around |
Posted: Thu Jul 08, 2010 9:44 am |
|
|
I would like to create a variable, int8 ram[128].
Then I will make a loop to roll through these values and 'wrap around' or repeat indefinitely.
In each loop I plan to write a value to ram[pointer].
I also will read location ram[pointer-10], ram[pointer-30], etc. for 7 different "stations"
if my pointer is at 0, is there an easy way to define the array or pointer so that ram[-10] is ram[117]?
after writing this out I think my problem would be solved if I used 0-255 instead of 0-128 locations, and -10 in an 8 bit pointer would be 246.
I will try that..... |
|
![](templates/subSilver/images/spacer.gif) |
newguy
Joined: 24 Jun 2004 Posts: 1903
|
|
Posted: Thu Jul 08, 2010 12:01 pm |
|
|
Because you plan an array of 128 values, this will work. Any "power of 2" value will actually work. When you access your array, make sure you logical AND "pointer" with a mask. For 128, the mask would be 0x7f. This approach means you'll never have to worry about "pointer" properly wrapping - just keep incrementing it. Also, the element math (ie pointer - 10, pointer - 30, etc) also must be ANDed with the mask. You don't have to worry about them wrapping either.
Code: | #define MASK 0x7f
pointer++;
ram[pointer & MASK] = whatever;
old_value = ram[(pointer - 10) & MASK];
really_old_value = ram[(pointer - 30) & MASK]; |
|
|
![](templates/subSilver/images/spacer.gif) |
frothbeast
Joined: 08 Jul 2010 Posts: 4
|
Thanks |
Posted: Thu Jul 08, 2010 7:54 pm |
|
|
That is exactly what I was looking for. Thanks.
I can use 256 for this task, but as ram gets scarce, I will want to know how to implement that mask. And you have illustrated it perfectly.
Froth. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|