FOR.. NEXT to Seconds converter?

on-topic Acorn-related news and discussions not covered by the other forums
Commie_User
Posts: 1066
Joined: Wed Jan 27, 2016 12:50 am
Contact:

FOR.. NEXT to Seconds converter?

Post by Commie_User » Mon Mar 13, 2017 10:26 am

What's the BASIC command to have a FOR.. NEXT loop return its duration in seconds?

For example, I'm sure FOR T=1 TO 1000: NEXT T takes a bit less than an actual second to run out. I'm using a base issue BBC B.

Thanks.

User avatar
danielj
Posts: 6026
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by danielj » Mon Mar 13, 2017 10:30 am

The TIME variable is constantly updated and measures time in milliseconds. Page 73 of the user guide. Just set it to zero at the start of the loop and see what it is after it finishes!

d.

Commie_User
Posts: 1066
Joined: Wed Jan 27, 2016 12:50 am
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by Commie_User » Mon Mar 13, 2017 11:21 am

Oh, I'm sorry, I had no idea that would even have been a thing to just find as easy as that!

I'm still used to the Commodore, with practically nothing in its BASIC. The default position was not to read the book - it was often wrong as well as lacking - and just ask someone! :lol:

User avatar
danielj
Posts: 6026
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by danielj » Mon Mar 13, 2017 11:35 am

The BBC Manual is excellent (actually, the Speccy, ZX81 and Amstrad manuals were very good too). I really don't know what commodore were thinking with their BASIC... A proper pig's ear! :D

d.

User avatar
Richard Russell
Posts: 216
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by Richard Russell » Mon Mar 13, 2017 11:42 am

danielj wrote:The TIME variable is constantly updated and measures time in milliseconds.
Centiseconds, actually.

Richard.

User avatar
danielj
Posts: 6026
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by danielj » Mon Mar 13, 2017 11:46 am

Richard Russell wrote:
danielj wrote:The TIME variable is constantly updated and measures time in milliseconds.
Centiseconds, actually.

Richard.
:oops: Oh for shame! You are, of course, completely correct :D

Commie_User
Posts: 1066
Joined: Wed Jan 27, 2016 12:50 am
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by Commie_User » Mon Mar 13, 2017 11:48 am

danielj wrote:The BBC Manual is excellent (actually, the Speccy, ZX81 and Amstrad manuals were very good too). I really don't know what commodore were thinking with their BASIC... A proper pig's ear! :D

d.
I think it had a lot to do with Jack Tramiel ordering production of the 64 at such short notice that the engineers had too little time to adapt the PET BASIC properly, let alone have a kid-friendly book done which could flaunt the 64. That was version 4, with proper disk handling commands and such, or so I'm told. We didn't use it.

We had version 2, which was solid and compact, yet empty of dedicated commands. Utility cartridges plugged the gap but to do anything like play a simple envelope, you had to type maybe ten lines to set up the chip and then enter your data statement. And when you ran it, it was all over in two or three seconds.

But most kids on the Commodore, I think, just wanted to get straight to the games anyway.

User avatar
danielj
Posts: 6026
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by danielj » Mon Mar 13, 2017 12:49 pm

Have you seen http://mdfs.net/Software/BBCBasic/C64/ ? I don't think Jonathan's put sound into it though :(

d.

Commie_User
Posts: 1066
Joined: Wed Jan 27, 2016 12:50 am
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by Commie_User » Mon Mar 13, 2017 1:01 pm

Ooooooooh, yes. I've seen them. In fact, the Aztec one was looked at and screenshotted by my good self. The link to Commodore Remix for my view is in there.

They are so POOR! Of the non-Beeb platforms, Richard Russell gets the usual thumbs up for the stratospherically excellent BBC BASIC for RM NIMBUS machines. It was so slick and good that I assumed it was on a chip.

User avatar
danielj
Posts: 6026
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by danielj » Mon Mar 13, 2017 1:03 pm

The top one should be perfect apart from the lack of sound - to all intents and purposes it's the acorn code running natively on the C64?

d.

Commie_User
Posts: 1066
Joined: Wed Jan 27, 2016 12:50 am
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by Commie_User » Mon Mar 13, 2017 1:40 pm

Screen modes
------------
Currently MODE n just clears the screen and sets white on black text. All
Commodore screen modes are 40 characters by 25 lines, just like the BBC's
MODE 6, but with 16 colours. It can be thought of as a cross between the
BBC's MODE 7 and MODE 1.
>RUN
>=

No FN
>LIST
5PROCmusic
10DEFPROCpoke(A%,N%)
20DIM X% -1:Y%=X%DIV256
30!X%=A%:X%?4=N%:A%=6:CALL&FFF1
40ENDPROC
50DEFFNpeek(A%)
60DIM X% -1:Y%=X%DIV256
70!X%=A%:A%=5:CALL &FFF1
80=X%?4
90:
100DEFPROCmusic
101PROCpoke(&64D405,9)
102PROCpoke(&64D406,0)
110PROCpoke(&64D418,15):REM VOLUME
120PROCpoke(&64D400,177)
130PROCpoke(&64D401,25)
140PROCpoke(&64D404,33)
150T%=TIME+100:REPEATUNTILTIME>T%
160PROCpoke(&64D404,32)
>

That only played one note.

So very much no, I'm afraid. I can't even *CAT and the SOUND command is alien to it.

Commie_User
Posts: 1066
Joined: Wed Jan 27, 2016 12:50 am
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by Commie_User » Mon Mar 13, 2017 7:40 pm

Though come to think of it..


**** commodore 64 basic v2 ****

64k ram system 38911 basic bytes free

ready.
10 let ti$="000000"
20 for t=1 to 8200
30 next t
40 print ti$
run
000011



The C64 does tell me how long a FOR.. NEXT loop lasts in seconds and I recall that the time clock TI$ is in the book.

So on reflection, it's a brilliant BASIC and peerless manual!

User avatar
PitfallJones
Posts: 431
Joined: Fri Feb 22, 2008 3:44 pm
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by PitfallJones » Mon Mar 13, 2017 7:44 pm

Wow! - this is the same (almost) question I have at the moment - only in machine code

How do you do a wait( some_ms_amount ) call?

It's for some serial communications code I'm writting that needs an absolute delay.

PJ

RobC
Posts: 2152
Joined: Sat Sep 01, 2007 9:41 pm
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by RobC » Mon Mar 13, 2017 8:53 pm

Commie_User wrote: 10 let ti$="000000"
20 for t=1 to 8200
30 next t
40 print ti$
run
000011
So does the C64 really execute that loop in 11 centiseconds or does it know that it's doing nothing and so skips it?
It takes about 120 centiseconds on my M128.

User avatar
BigEd
Posts: 1742
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by BigEd » Mon Mar 13, 2017 9:11 pm

AFAICT C64 counts in jiffies, so that's 11/60ths, or 183ms. (Or, possibly, 11/50ths in PAL land, which is 220ms)

User avatar
danielj
Posts: 6026
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by danielj » Mon Mar 13, 2017 9:13 pm

PitfallJones wrote:Wow! - this is the same (almost) question I have at the moment - only in machine code

How do you do a wait( some_ms_amount ) call?

It's for some serial communications code I'm writting that needs an absolute delay.

PJ
MartinB is a good person to ask - but as I understand it, the best way is to work out a loop of the desired size using the cycle count of instructions and the speed of the processor?

d.

User avatar
danielj
Posts: 6026
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by danielj » Mon Mar 13, 2017 9:14 pm

BigEd wrote:AFAICT C64 counts in jiffies, so that's 11/60ths, or 183ms. (Or, possibly, 11/50ths in PAL land, which is 220ms)
Weren't they a brand of prophylactic?

d.

User avatar
MartinB
Posts: 4673
Joined: Mon Mar 31, 2008 9:04 pm
Location: Obscurity
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by MartinB » Mon Mar 13, 2017 10:00 pm

Daniel wrote:MartinB is a good person to ask....
There's a first... :shock: :lol:

Precise waits to build an accurate copy of a specific speed asynchronous (no clock) serial protocol is indeed something I've done and is at the heart of UPURS. As Daniel has suggested, the best way is to exploit the fact that we know the exact time expended by one instruction cycle because we know the clock speed of the host CPU. If that clock speed is variable or the system is heavily interrupt driven and we can't inhibit the latter then you can't use a cycle-counting method. This being the case, it would need the system to have some form of accessible hardware timer but this is unlikely to be of any use for very short time intervals.

So, considering a Beeb for example which we know runs at a nominal 2MHz, a single cycle takes 1/2E6s or 0.5us to execute. Thus, an example instruction, let's say a NOP which consumes 2 cycles (6502 Instruction Set), expends 1us of time at 2MHz. So, if I wanted to expend 100us and memory space isn't an issue, I could simply roll-out 100 consecutive NOPS in my code. I would precede the wait with an SEI to prevent an IRQ occurring and trashing my timing and I would restore IRQ's afterwards with a CLI. For longer times you can use loops but you then need to be careful to also include the cycle count of iterations of the loop control instructions but it's not terribly difficult to produce simple contiguous waits. It gets a lot trickier when specific code has to execute in specific times and the code may have conditional flow paths (e.g. UPURS!) but you get the idea.

If what you are doing is for an Archimedes or a Pi etc.then I'm afraid I'm stuffed... :lol:

User avatar
1024MAK
Posts: 7309
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by 1024MAK » Mon Mar 13, 2017 10:23 pm

MartinB wrote:
Daniel wrote:MartinB is a good person to ask....
There's a first... :shock: :lol:
You do know that he just pulls names out of hats...* don't you :?:

Mark

* via a special admin forum function...
For a "Complete BBC Games Archive" visit www.bbcmicro.co.uk NOW!
BeebWiki‬ - for answers to many questions...

RobC
Posts: 2152
Joined: Sat Sep 01, 2007 9:41 pm
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by RobC » Mon Mar 13, 2017 10:39 pm

BigEd wrote:AFAICT C64 counts in jiffies, so that's 11/60ths, or 183ms. (Or, possibly, 11/50ths in PAL land, which is 220ms)
Just had a play on my C64. It seems that TI$ is updated in seconds and a Google search seems to confirm that it's designed to be set to "HHMMSS".

So, the loop is taking 11 seconds rather than the 11 centiseconds that I assumed. I should have guessed that BASIC on a C64 isn't 10 times as fast as a Beeb! :oops:

User avatar
jgharston
Posts: 3034
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by jgharston » Tue Mar 14, 2017 12:35 am

Commie_User wrote:What's the BASIC command to have a FOR.. NEXT loop return its duration in seconds?
There is no way to convert a FOR/NEXT loop to seconds as it will run as fast as it happens to run whenever it happens to run. It will never run at exactly the same speed every time on every platform as split second differences between pressing RETURN when running the program, and split second differences between how long background tasks take, what screen mode you happen to be in, will all add up to change how long a piece of foreground code takes. If you want to know how much time has elapsed, then do exactly that - ask the system how much time has elapsed.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
BigEd
Posts: 1742
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by BigEd » Tue Mar 14, 2017 5:39 am

RobC wrote:
BigEd wrote:AFAICT C64 counts in jiffies, so that's 11/60ths, or 183ms. (Or, possibly, 11/50ths in PAL land, which is 220ms)
Just had a play on my C64. It seems that TI$ is updated in seconds and a Google search seems to confirm that it's designed to be set to "HHMMSS".

So, the loop is taking 11 seconds rather than the 11 centiseconds that I assumed. I should have guessed that BASIC on a C64 isn't 10 times as fast as a Beeb! :oops:
Ah - good to know!

Commie_User
Posts: 1066
Joined: Wed Jan 27, 2016 12:50 am
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by Commie_User » Tue Mar 14, 2017 10:27 am

jgharston wrote:
Commie_User wrote:What's the BASIC command to have a FOR.. NEXT loop return its duration in seconds?
If you want to know how much time has elapsed, then do exactly that - ask the system how much time has elapsed.
That's what I thought I asked.

User avatar
jgharston
Posts: 3034
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by jgharston » Tue Mar 14, 2017 3:29 pm

Commie_User wrote:
jgharston wrote:
Commie_User wrote:What's the BASIC command to have a FOR.. NEXT loop return its duration in seconds?
If you want to know how much time has elapsed, then do exactly that - ask the system how much time has elapsed.
That's what I thought I asked.
No you didn't, you asked how long a FOR/NEXT loop takes.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
danielj
Posts: 6026
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by danielj » Tue Mar 14, 2017 3:47 pm

And relax!
:)

d.

Commie_User
Posts: 1066
Joined: Wed Jan 27, 2016 12:50 am
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by Commie_User » Tue Mar 14, 2017 5:19 pm

Incidentally, I should have just Googled ONLINE STOPWATCH and sorted it out that way. I should have thought of that earlier. (I thought microseconds only want up to 25 anyway, or 30 depending on the project.)
Last edited by Commie_User on Tue Mar 14, 2017 6:53 pm, edited 1 time in total.

User avatar
1024MAK
Posts: 7309
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by 1024MAK » Tue Mar 14, 2017 6:44 pm

Commie_User wrote: Incidentally, I should have just Googled ONLINE STOPWATCH and sorted it out that way. I should have thought of that earlier. (I thought microseconds only want up to 25 anyway, or 30 depending on the project.)
And in other news, it was revealed today that in fact ms can go on forever...
How many your computer can count is of course a different matter...

See also...

:lol: :lol: :lol:

Mark

PS 'said the person wearing a "Time Lord" T-shirt :P '
For a "Complete BBC Games Archive" visit www.bbcmicro.co.uk NOW!
BeebWiki‬ - for answers to many questions...

User avatar
Richard Russell
Posts: 216
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by Richard Russell » Tue Mar 14, 2017 10:26 pm

jgharston wrote:Ask the system how much time has elapsed... That's what I thought I asked... No you didn't, you asked how long a FOR/NEXT loop takes.
Commie_User wrote:I thought microseconds only want up to 25 anyway, or 30 depending on the project.
I'm relatively new to this forum. Is the gibberish usually up to this exceptionally high standard? :roll:

Richard.

User avatar
danielj
Posts: 6026
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by danielj » Tue Mar 14, 2017 10:30 pm

Fortunately not :) As you were :D

d.

User avatar
1024MAK
Posts: 7309
Joined: Mon Apr 18, 2011 4:46 pm
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: FOR.. NEXT to Seconds converter?

Post by 1024MAK » Tue Mar 14, 2017 11:46 pm

For more, er, gibberish, try the WoS chit-chat sub-forum :lol:
For a "Complete BBC Games Archive" visit www.bbcmicro.co.uk NOW!
BeebWiki‬ - for answers to many questions...

Post Reply