English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. Blitz Basic

 
 
Thread Tools
Old 03 November 2022, 18:57   #1
Amiga1992
Registered User
 
Join Date: May 2001
Location: ?
Posts: 19,645
Timed events?

I am trying to repeat some routine X times per time unit and I would like to know what the best approach is.

Let's say I have a routine that increases a number +1 every time it is called, and I want to call it 60 times per minute. With this I mean, take a minute, divide it by 60 evenly and execute this function each time. It should add +12 to the variable once every second.

Any ideas? Is there a timer I can work with, do I create my own>? I never created timed events like this in any other language let alone on the Amiga, any help appreciated.
Amiga1992 is offline  
Old 03 November 2022, 21:24   #2
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
There are a few approaches you can take, depending on the precision you need. One of the simplest ways of doing it is to set up an interrupt to count video frames. This will give you a variable that increases every frame, so either 50 or 60 times per second, and you can read that variable to know when a given number of frames have passed. You can use the NTSC function to determine whether the program is running on a pal or NTSC screenmode. The downside of this approach is that it won't work well when you run a non-standard screenmode, or have other screens open with different vertical frequencies and switch between them.

After that, there are OS calls you can use for timing or you can bang the CIA hardware if you don't care about being system-friendly. AmiBlitz 3 provides a couple of very nice includes that use these timing functions and provide an easy interface for Blitz programs to use - eclock.include allows timing via the OS timer.device and ciatimer.include allows hardware-banging direct access to the CIAs for timing. Blitz 2 doesn't include such luxuries unfortunately, so you're kinda limited to the first option or manually invoking the timer.device functions. It might be possible to backport the AB3 timer includes to suit if you really needed.

Edit: Here's a quick little demonstration of the first method:

Code:
framecount.w = 0

If NTSC
  framemax.w = 60
Else
  framemax.w = 50
End If

SetInt 5
  framecount + 1
End SetInt


mycounter.w = 0

Repeat

  Delay_ 1 ; Do whatever you need to do here. Delay_ stops it from busy-looping

  If framecount >= framemax
    framecount - framemax
    mycounter + 12
    NPrint mycounter
  End If
Until Joyb(0)
End

Last edited by Daedalus; 03 November 2022 at 21:38. Reason: Added code
Daedalus is offline  
Old 05 November 2022, 00:58   #3
Amiga1992
Registered User
 
Join Date: May 2001
Location: ?
Posts: 19,645
Quote:
Originally Posted by Daedalus View Post
Blitz 2 doesn't include such luxuries unfortunately, so you're kinda limited to the first option or manually invoking the timer.device functions. It might be possible to backport the AB3 timer includes to suit if you really needed.
Ah I always forget Amiblitz 3 exists.. Yeah I am using Blitz 2 so I am shit out of options.

Banging the hardware is OK, I already am doing it. But it probably is better to poke at the CIA because I don't want to deal with 50/60 Hz shenanigans, although I am pretty sure (untested) that my program runs only in PAL.

So what should I do with the CIA then?
Amiga1992 is offline  
Old 05 November 2022, 14:07   #4
aNdy/AL/COS
Registered User
 
aNdy/AL/COS's Avatar
 
Join Date: Jan 2022
Location: Wales
Posts: 91
I'm using Blitz2 for my game which has a timer that counts down to zero on which you lose a life. Kind of the reverse of you adding 1 every time a routine is called and then adding 12 to a variable.

To be honest, I'm not even checking for NTSC...

As Daedalus pointed out, your best bet is to setup an interrupt (SetInt 5) to do the timing for counting every second that goes by - that command runs on interrupt and stops for no man. In your main program loop, you then check to see if the interrupt has reached 0 and if so, call your routine to what you need.

In my setup code if have something like this to count every second that goes by...

Code:
SetInt 5
tminus=tminus-1
  If tminus=0
    tminus=59
  EndIf
EndInt
Then in my main code loop, I just check to see if 'tminus' is zero and if it is, my main countdown clock decreases by 1 and the onscreen display updated.

In your case, your main loop would check for 'tminus' being zero, call your routine and add 12.

Unless I've misunderstood your original question and I'm making an idiot of myself by trying to help out. If so, I'll shut-up!

Last edited by aNdy/AL/COS; 05 November 2022 at 14:08. Reason: odd formatting
aNdy/AL/COS is offline  
Old 05 November 2022, 17:07   #5
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
Quote:
Originally Posted by Akira View Post
Ah I always forget Amiblitz 3 exists.. Yeah I am using Blitz 2 so I am shit out of options.

Banging the hardware is OK, I already am doing it. But it probably is better to poke at the CIA because I don't want to deal with 50/60 Hz shenanigans, although I am pretty sure (untested) that my program runs only in PAL.

So what should I do with the CIA then?
Using the CIA can be a little bit more involved than the code I posted, but you can skip most of the boilerplate stuff by assuming that you have full control of the hardware. Accessing them is a matter of peeking and poking the relevant registers, and a description of how the timer registers are used can be found here. Bear in mind that there will be slight differences between PAL and NTSC machines as the timings are based off the main motherboard clock, though the difference is around 1%, so far less than the 20% error that you get by counting PAL frames on an NTSC screenmode.

For a more fuller-featured and easy to use set of functions, have a look at the AmiBlitz 3 CIA Timer include. It uses some AmiBlitz 3 features, but it looks like it could be relatively simple to adapt for Blitz 2 use.
Daedalus is offline  
Old 08 November 2022, 09:43   #6
E-Penguin
Banana
 
E-Penguin's Avatar
 
Join Date: Jul 2016
Location: Darmstadt
Posts: 1,213
You could also use the 'ticks' command to get a system timer and subtract a previous value. It's 50Hz on PAL. Then add that many counts to your counter.

Something like:
lastTicks = Ticks
(do a thing)
tickDiff=Ticks - lastTicks
myCounter += tickDiff * countPerTick
lastTicks += ticksDiff

Basically similar to the interrupt approach but without needing an interrupt. Set countPerTick as needed to scale 50Hz to whatever you're counting
E-Penguin is offline  
Old 08 November 2022, 17:29   #7
Amiga1992
Registered User
 
Join Date: May 2001
Location: ?
Posts: 19,645
OK I thank you all for your input, seems like method 1 is the easiest.

Why set an interrupt though, I have this main loop with VWait which happens every frame, wouldn't changing the variable at every frame yield the same results?
Amiga1992 is offline  
Old 08 November 2022, 18:25   #8
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
Quote:
Originally Posted by Akira View Post
OK I thank you all for your input, seems like method 1 is the easiest.

Why set an interrupt though, I have this main loop with VWait which happens every frame, wouldn't changing the variable at every frame yield the same results?
Yes, unless something happens that takes more than a frame to execute, in which case your timing will start to slow down. If you know your main loop will never take as long as a frame, even on the slowest machine or with some heavy multitasking going on, then you don't need the interrupts.
Daedalus is offline  
Old 09 November 2022, 16:30   #9
Amiga1992
Registered User
 
Join Date: May 2001
Location: ?
Posts: 19,645
Quote:
Originally Posted by Daedalus View Post
Yes, unless something happens that takes more than a frame to execute, in which case your timing will start to slow down. If you know your main loop will never take as long as a frame, even on the slowest machine or with some heavy multitasking going on, then you don't need the interrupts.
Ah great to know. I might be slowing down a bit. Good point!
Amiga1992 is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
mouse wheel events meynaf Coders. System 1 26 August 2018 16:06
Adelaide Events! amigaman101 News 0 15 October 2015 11:07
Custom input events CFou! support.WinUAE 15 20 May 2012 17:54
timed wait using CIAs jotd support.Other 3 23 March 2008 14:55
Events: ComputerParty @ SyntaxSociety Paul News 0 15 October 2004 16:56

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 19:58.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.11361 seconds with 15 queries