time command of 20M2 in simulator

bobatkins

New Member
I'm trying to use the "time" command on a 20M2 with the Picaxe Editor version 6.0.8.11 but not having any luck. I used the example code from the manual

Code:
let w0 = time + 10

do
  if time > w0 then 
    high B.0
  end if
loop
Monitoring the value of w0 I see 10 but B.0 in the simulator never goes high.

Running

Code:
do
	let w0=time
	let w1=w1+1
	pause 100
loop
shows W1 increasing as it should, but w0 is always 0. I've tried adding "setfreq", "disabletime" and "enabletime" comments, but still I don't see the value of "time" changing at all.

What don't I understand about the "time" comment? Why don't I see the value changing. Is it something at only works in hardware and that the simulator doesn't simulate? I know it's supposed to start when power is applied to the 20M2, but the simulator could start simulation of the counter when a program was run. But does it in fact do that? The simulator doesn't list "time" as one of the values it can display.
 

The bear

Senior Member
@bobatkins,
Welcome to the forum.
I have recently learnt that 'time' doesn't simulate. Try it on a real picaxe.
Good luck.
Bear..
 

hippy

Technical Support
Staff member
When simulating 'time' is not real time but a calculated elapsed time determined roughly from the commands being simulated.

To more closely simulate real time you can add a PAUSE command which will only be executed when simulating but not in a real chip, though it often doesn't do any harm if included in the downloaded PICAXE program -

Code:
let w0 = time + 10

do
  if time > w0 then 
    high B.0
  end if
  #ifdef SIMULATING then
    pause 1000
  #endif
loop
 

Buzby

Senior Member
... calculated elapsed time determined roughly from the commands being simulated.
Hi hippy,

I've never bothered using 'time' in the simulator, 'cos I'm old school and don't expect any code that depends on specific CPU clock cycles to execute properly.


Are you saying that 'one second of real chip time' is simulated as close to 'one second in simulator time', or that 'one second is simulated as millions of slow virtual clock cycles.' ?.

In effect, does 'time' work *very slowly* in the simulator ?

Cheers,

Buzby
 

bobatkins

New Member
@bobatkins,
Welcome to the forum.
I have recently learnt that 'time' doesn't simulate. Try it on a real picaxe.
Good luck.
Bear..
Thanks for the info. I'll now give up on the simulation!

I guess on something like the 28X2 you could use the "settimer" function to do much the same thing as "time" (i.e. count elapsed seconds)
 

marks

Senior Member
Hi bobatkins,
don't give up lol
that does look like a bad example there
try this !
Code:
let w0 = 4

sertxd (#time,#outpinB.0,cr,lf)
do
  if time > w0 then 
    high B.0
  end if

sertxd (#time,"  ",#outpinB.0,cr,lf)
loop
 

bobatkins

New Member
HI and welcome to the forum.
Recently I wrote some code to try on the simulator
http://www.picaxeforum.co.uk/showthread.php?29386-Spa-Controller-help-needed-as-I-m-out-of-ideas/page2
the time does simulate very slowly
you can try the example at #18
operate switch C.0 to start pump and timer results will be displayed on the terminal.
you may have to tick simulation terminal {to stay on top}
I can confirm that "time" is running, but it's slow by a factor of at least 1000. I see a tick maybe every 15-20 minutes instead of every second. So slow as to be a useless simulation, but it is there if you wait long enough.

This code:

Code:
do
w0=time
w1=w1+1
loop
gave a value of w0=8 for ~17200 loops (w1=17230) in the simulator. I didn't time it exactly, but it took over 2 hours. I guess it's counting virtual machine clock cycles.
 

bobatkins

New Member
Hi bobatkins,
don't give up lol
that does look like a bad example there
try this !
Code:
let w0 = 4

sertxd (#time,#outpinB.0,cr,lf)
do
  if time > w0 then 
    high B.0
  end if

sertxd (#time,"  ",#outpinB.0,cr,lf)
loop
The "sertxt" command does seem to affect "time". I'm guessing that both commands must be using the same internal timer? The manual warns about "servo" (Note that the background internal timer used to count seconds is also used by several other commands such as servo) but doesn't give a list of which other commands interfere with "time". So although "time" appears to be running faster with this code, I'm guessing it's an artifact of using "sertxt".
 

Technical

Technical Support
Staff member
Time simulation is working as intended. It simulates the program as in a real chip, for instance this program

Code:
let w0 = time + 10

do
  if time > w0 then 
      high B.0
      serxtd (#w1)
      end
  end if

  inc w1
loop

on a real chip would process the loop thousands of times before finishing. That is exactly what the simulation has to do as well, remembering that each simulation line has an extra 'simulation delay' so that you can physically see it highlighting. If it didn't process all the loops then the incrementing value in w1 would not be anywhere near a realistic value at the end of the simulation. So you have to wait thousands of loops before it finishes.

As Hippy says the way to overcome this, in both reality and in simulation, is to add a pause into the loop.
 
Last edited:

hippy

Technical Support
Staff member
Are you saying that 'one second of real chip time' is simulated as close to 'one second in simulator time', or that 'one second is simulated as millions of slow virtual clock cycles.' ?
As a general concept rather exact implementation; the simulator counts commands simulated and estimates what the elapsed time would be. I don't know what actual value is used but it's a nominal value, say 100us per command, so after 10,000 commands the 'time' variable will be incremented.

For PAUSE, the simulator knows that the nominal one command time will likely wrong so will add the length of the actual pause to the internal elapsed time count which then causes 'time' to increment faster.

From my observations, it seems something similar is done when serial data is output via SERTXD and SEROUT.
 
Top