31 bit timer

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
User avatar
Ruben
Member
Posts: 15
Joined: Sun Mar 07, 2010 9:58 pm
Contact:

31 bit timer

Post by Ruben »

As you might know I'm working on running interpreted languages on XMOS devices.
I dropped the previous interpreter (which can be found in projects) for a much better (bytecode based) one. Now it comes with a problem which is that integers are 31 bit (one bit is used as a flag). For most applications it would be no problem, however there are the timers ;). They really need the full 32 bit range.

Is there a way I can have timers only using 31 bits instead of the full 32 (or some workaround). Somehow resetting it or so?


User avatar
paul
XCore Addict
Posts: 169
Joined: Fri Jan 08, 2010 12:13 am
Contact:

Post by paul »

Not sure I quite understand your problem - but surely if you mask the MSB then it shouldn't make any difference. The timer would just appear to wrap round at 2^31, not 2^32.
Paul

On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
User avatar
Ruben
Member
Posts: 15
Joined: Sun Mar 07, 2010 9:58 pm
Contact:

Post by Ruben »

That will AFAIK only work as long as you don't want to do something like this:

Code: Select all

timer t;
t when timerafter ( time ) :> void ;
The problem is that the value of time will always be in the range of 0x0000.0000 & 0x8FFF.FFFF (31 bit).
However, half of the time the above code will not work properly because timer will return something in the range of 0x1000.0000 & 0xFFFF.FFFF . This results in timeafter() being true immediately even if time is like Tnow+100000.
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

For each 31 bit time there are two possible 32 bit times it could map to. When using timerafter you want to map to the time closest to the current time. I think the following code should do the job (untested).

Code: Select all

void f(timer t, unsigned time_31) {
  unsigned now;
  int diff;
  unsigned time;
  // Get current time
  t :> now;
  // Calculate the difference between the target and now.
  diff = time_31 - now;
  // Truncate to 31 bits and sign extend.
  diff = (diff << 1) >> 1;
  // Calculate 32 bit target time. Of the two possible 32 bit
  // target times this calculation yields the one closet to the current time
  // since diff is in the range [-2^30, 2^30 - 1].
  time = now + diff;
  // Do the actual input
  t when timerafter(time) :> void;
}
User avatar
Ruben
Member
Posts: 15
Joined: Sun Mar 07, 2010 9:58 pm
Contact:

Post by Ruben »

Hmm, very interesting solution and it seems to work fine :)

Thanks a lot!
Post Reply