Hi (probably a question for Rob),
The app in question is a cpu simulator which allows the user to write programs in the simulated computer and then either a) fetch and run one instruction (single step) or run the program until a stop instruction is reached.
After advice from Rob I solved a very early problem in timing when I couldn’t get the while do loop function correctly for the obvious (now) reason that the loops whizzed through and fired off all the iterations and didn’t wait for the screen drawing, validation code etc.
The loop is now: (not all in proper lua code to save space here)
function run_program()
notfinished = true
… various bits of setup code
runloop = timer.performWithDelay(1000,singlestep,0)
various end code bits
end
function singlestep ()
if notfinished then
fetch_instruction()
timer.perfromWithDelay(500,execute_instruction)
else
tidyup code here
end
end
end
This worked great. Potentially the program could be up to 255 instructions but is usually shorter, also as some of the instructions cause loops the program could be infinite (there is an interrupt button that sets notfinished = false).
What I have discovered recently was that when the program gets long (say 50 steps) the whole app slows down and effectively freezes). With even a simple 2 instruction program that adds 1 to the accumulator (in the app) and then executes a goto instruction that takes it back to the start (ie adding 1 to the acc continually) the app works perfectly for about 40 iterations then begins to freeze.
My thoughts are that it is running out of some resource (timers?) or filling memory with uncollected garbage that eventually overflows? and then eventually impacts on the program.
Very few users would ever see the bug, but it is not elegant or even honest to leave it in.
Anyone see the flaw (and a solution)???
regards
Martin