Jump to content
Sign in to follow this  
pacgreg

bB Modulus?

Recommended Posts

I want to make a constant timer that controls a bunch of stuff, including activating a block of code every 5 cycles, but it needs to also go to 45, is there a way i could activate something every multiple of 5 like if timer % 5 == 0 then goto..., but in basic?

Share this post


Link to post
Share on other sites

Off the top of my head I can think of 3 options:

 

1) Use BCD for the variable that holds the cycle count. Then AND it with %00001111 and check if it is equal to 0 or 5. This option will use a few more CPU cycles than the other two, but is good if you absolutely must use mod 5 and don't have a lot of ROM space available.

 

2) Create a 45 byte lookup table with a 1 stored in every 5th byte and 0 in the rest. Then simply index into the lookup table with the cycle count variable and compare that value with 1. This option is good if you must use mod 5 and have more ROM space than CPU cycles.

 

3) Make it mod 4 or 8 (or any power of 2). Then you can simply AND the count variable with n-1 to obtain mod n quickly. This is the best option to use if you can tolerate using a value that is a power of 2. Ex: cycleCount AND %00000011 is equivalent to cycleCount % 4

Share this post


Link to post
Share on other sites

The second option seems like its useful for this and possibly more things in the future, is it called a data array in the batari page?

Share this post


Link to post
Share on other sites

what else does the timer have to do?
do you really want 45 or do you want
0..44?

 count = count + 1
 if count = 68 then count = 0
 if count & 7 = 5 then count = count + 3

will go through a cycle with a length of
45

but it doesn't count 0..44

bits 0..2 count 0..4 and
bits 3..6 (ie count/8) count 0..8


Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...