A Bug in my Code Snippet, or the PE6 Simulator?

AllyCat

Senior Member
Hi,

Three months ago I posted a Code Snippet which has been "viewed" over 400 times. So I was a little surprised to discover recently that it totally "crashes" the PE6 Simulator and Editor 6.0.9.3 (at least I've found no way to recover from the "hang"). The snippet test harness works perfectly in the PE5 Simulator and a real PICaxe downloaded from PE6.

The cause is the program line for b1 = b1 to 255 step 16 which is rather unusual (and perhaps "risky") but was entirely intentional*. The potential issue is not the FOR b1 = b1 part (which is valid) but that in calculating if the "TO" value has been reached, the value might (will) overflow a single byte (which of course is all that b1 can contain). Thus the "end" condition might never be reached, which appears to be what's happening in the PE6 Simulator. However, normally the PICaxe interpreter calculates to a WORD resolution within any command (otherwise something like b1 = b1 * 250 / 251 wouldn't be calculated correctly) which appears to be happening in the real chips and PE5 Simulator.

* 255 seemed to be the only value that supports up to 16 iterations (loops) for the division routine, plus the four potential "flag" bits, within a single byte. Anybody have any better ideas? I guess the "counting" loop probably could be closed in some other way, but a FOR loop seems so much the "obvious" (self-documenting) and compact structure to use.

Cheers, Alan.
 

PhilHornby

Senior Member
Three months ago I posted a Code Snippet which has been "viewed" over 400 times. So I was a little surprised to discover recently that it totally "crashes" the PE6 Simulator and Editor 6.0.9.3 (at least I've found no way to recover from the "hang").
To my mind, there is no reason/excuse for a Software Simulator not matching exactly, that which it is simulating! Otherwise, what is the point of it?
 

hippy

Technical Support
Staff member
We have made a note of the issue and will investigate that in due course.

We do strive to make the simulator behave exactly as a physical chip does but some differences in the way a physical chip and simulation perform may arise because they are implemented using different code.

We hope to catch all those differences and resolve those during testing but some unusual constructs may fall through testing or give rise to behaviour which was not expected in particular circumstances. We will only become aware of those issues when they are reported or when we come across them during other testing.
 

newplumber

Senior Member
Hi
Well first I must say I do love the PE6 editor and all who made it did a VERY awesome job
and I do love the simulator... it saves my horrible coding hours of debugging
so I figure since PE6 may have tiny kinks which i believe are excusable since you
will never fix something you can not see is broken. Its awesome to have Alan find them
because all my simulations worked flawlessly after I fixed MY errors (of course my codes are very simple)
IMHO PE6 is one of the best programs to teach a beginner how to start in micro controllers
especially in the simulator with step by step ... I just wish more people/kids would understand
just how easy it is to use.
my two cents anyway
Mark
 

AllyCat

Senior Member
And: FOR wordvar = 0 TO 65535

Hi,

I didn't want to start yet another "Bug" thread and this issue is rather similar to that in my OP, so .....

Recently I was investigating the RANDOM function to check that it really does emulate a "16-bit Maximal Length Feedback Shift Register". This generates a repeating sequence of 65535 numbers (i.e. 2^16 - 1), or to paraphrase Eric Morecombe "All the numbers from 1 to 65535, but not in the right order". :)

So I wrote a simple program: FOR w1 = 0 TO 65535 : RANDOM w0 : NEXT with a couple of SERTXDs to check that the first and last numbers were the same.

Well, after 5 minutes I was thinking that the PICaxe was even slower than I'd thought, and after half an hour finally decided that something was "wrong". So I then wrote the following test Program to download to a real chip (or try one of the simulators if you're really patient). :

Code:
#picaxe 20m2   ; Or any M2
#no_data
#terminal 4800
for w1 = 0 to 65535
   if time > 10 then
	sertxd(#w1," ")
	time = 0
   endif
next
sertxd("  Finished")
It appears to fail with code downloaded from PE5, PE6 and AXEPad. The 65535 has some similarity with the issue in my OP, but this version appears to fail only when starting from zero (even 1 seems ok).

Cheers, Alan.
 

hippy

Technical Support
Staff member
The "For w0 = 0 To 65535" 16-bit issue I thought was documented, or at least well known, because 65535 ($FFFF) will always increment and roll-over to 0 ($0000) which is in the valid range, so it repeats forever.

The easiest way to have a loop which runs from 0 to 65535 is -

Code:
w0 = 0
Do
  SerTxd( #w0, " ")
  w0 = w0 + 1
Loop Until w0 = 0
The FOR-NEXT doesn't work because that is effectively ...

Code:
w0 = 0
Do
  SerTxd( #w0, " ")
  w0 = w0 + 1
Loop Until w0 < 0 Or w0 > 65535
As 'w0' can never be less than 0 nor greater than 65535, the loop repeats forever.

It does appear the simulator terminates when the physical chip does not so we will investigate that.

One can speed up debugging of large loops by forcing a jump in the index variable as below ...

Code:
For w0 = 0 To 65535
  [b]If w0 > 5 Then
    w0 = w0 Min 65530
  End If[/b]
  SerTxd( #w0, " " )
Next
SerTxd( "DONE")
 
Last edited:

AllyCat

Senior Member
Thanks hippy,

w0 = w0 + 1
Loop Until w0 < 0 Or w0 > 65535
The "under the hood" details are usually informative and sometimes quite unintuitive. After incrementing w0, I would have thought that the exit condition could (should?) be a <= not just a <. But I'm not sure if that would work with a negative STEP. Or, of course, test for the exit before the STEP, but that's not as tidy to program.

Usually, the main purpose of the "hood" now is to "streamline" the vehicle, but I suppose it also serves to protect the driver from the engine. Or is it to protect the engine from the driver? :)

Cheers, Alan.
 

hippy

Technical Support
Staff member
The definition of "FOR var = start TO end" is to "repaet the loop while var is >= start and <= end".

So both of these are functionally the same ...

Code:
var = 0
Do
  ...
  var = var + 1
Loop While var >= 0 Or var <= 65535
Code:
var = 0
Do
  ...
  var = var + 1
Loop Until var < 0 Or var > 65535
And in both cases the loop condition will always cause the loop to repeat.
 
Top