Hi All, I am having major “bug” issues within an app I am developing which simulates the operation of a cpu for educational purposes. The bugs are all around asynchronous delays in performWithDelay (and also to a certain extent transition.to.
The core of the app is a loop which fetches instructions, decodes them and executes them (just like a CPU!!). To be usable the loop needs to be quite a lot slower than Corona lua is capable of, otherwise the program flies by in a few microseconds and the user sees the final result but not any of the intermediate stages, negating the point of the app.
What I would like to do (tried and failed) is a loop like this (pseudocode)
repeat
get instruction
decode instruction
execute instruction
until instruction is stop
there would/could be up to 100 iterations of the loop as the “memory” has 100 locations.
to slow it down I need to add some delays (using performWithDelay) for every section
so
repeat
performWithDelay(300,get instruction)
performWithDelay (300,decode instruction)
performWithDelay (300, execute instruction)
until instruction = stop
this doesn’t work for two reasons:
-
as the delays are asynchronous they get fired much faster than it looks, in fact almost instantaneously and all finishing at about the same time too.
-
also because of this the final condition seems to never get triggered and it ends up in an infinite loop.
I ended up with the following
for i= 0,99 do
timer=performWithDelay (200*i,singlestep)
end
then singlestep is a routine that looks like this
function singlestep()
if notfinished then
fetch instruction
execute instruction
end
end
This all works ok, but there are up to 100 simultaneous timers running and when i gets to about 50 the timers have delays up to 50*200 = 5 seconds. Again this worked fine. but the time for the program run was either too slow or too quick depending on what the user wanted to see and I needed to add a variable called runspeed, which could be altered with a widget between 1-5
the code became:
for i = 0,99 do
timer = performWithDelay(100*(6-runspeed)*i, singlestep)
end
this gives variable results, sometimes the loop goes slowly, then with the same runspeed setting it will go fast, then sometimes somewhere in the middle.
Any ideas, either a) How to tidy the whole thing up to fire of sequential timers without my workaround or b) how to make my runspeed work.
thanks
Martin