CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Ram array, rolling pointer, offsets, pointer wrap-around

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
frothbeast



Joined: 08 Jul 2010
Posts: 4

View user's profile Send private message

Ram array, rolling pointer, offsets, pointer wrap-around
PostPosted: Thu Jul 08, 2010 9:44 am     Reply with quote

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.....
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Thu Jul 08, 2010 12:01 pm     Reply with quote

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];
frothbeast



Joined: 08 Jul 2010
Posts: 4

View user's profile Send private message

Thanks
PostPosted: Thu Jul 08, 2010 7:54 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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