How to calculate elapsed time
Moderator: Moderators
How to calculate elapsed time
Hi,
In a ML loop routine i need to know how many time is elapsed from the start of the loop so for example after 10 seconds i need to print a message.
There are some vic functions:
$f760 rdtim Read Real-Time Clock
$f767 settim Set Real-Time Clock
And relative TI$ bytes
00A0-00A2 160-162 3 byte jiffy clock
but before to reinvent the wheel has someone a ready-made code for this matter for both PAL/NTSC machines?
In a ML loop routine i need to know how many time is elapsed from the start of the loop so for example after 10 seconds i need to print a message.
There are some vic functions:
$f760 rdtim Read Real-Time Clock
$f767 settim Set Real-Time Clock
And relative TI$ bytes
00A0-00A2 160-162 3 byte jiffy clock
but before to reinvent the wheel has someone a ready-made code for this matter for both PAL/NTSC machines?
Mega-Cart: the cartridge you plug in once and for all.
Re: How to calculate elapsed time
my take:
1) calculate the exact amount of time to wait in jiffies (60th seconds) and split into 3 bytes value (H,M,L)
2) set time to 0 with kernel function SETTIM $f767
3) wait for time
in code:
(21 bytes)
A shorter one is:
1) calculate the exact amount of time to wait in jiffies (60th seconds) and split into 3 bytes value (H,M,L)
2) subract this value from $4F1A01 (which is 23:59:59 in jiffies) and get a new 3 bytes H,M,L
3) set this time and wait for zero in the MSB:
(13 bytes)
1) calculate the exact amount of time to wait in jiffies (60th seconds) and split into 3 bytes value (H,M,L)
2) set time to 0 with kernel function SETTIM $f767
3) wait for time
in code:
Code: Select all
LDA #$00
TXA
TYA
JSR $f767
LOOP:
SEC
LDA $A2
SBC #L
LDA $A1
SBC #M
LDA $A0
SBC #H
BCC LOOP
A shorter one is:
1) calculate the exact amount of time to wait in jiffies (60th seconds) and split into 3 bytes value (H,M,L)
2) subract this value from $4F1A01 (which is 23:59:59 in jiffies) and get a new 3 bytes H,M,L
3) set this time and wait for zero in the MSB:
Code: Select all
LDA #H
LDX #M
LDY #L
JSR $f767
LOOP:
LDA $A0
BNE LOOP
wow the second method is very short, i've used it but inverting Y with A :
According with the code below Y is the higher value and A the lower.
The routine seems that works in NTSC too (at least on Vice) but for NTSC aren't 50 jiffies per second ?
Btw thank you for help Nino.
Code: Select all
LDA #$a8
LDX #$17
LDY #$4f
JSR $f767 ;$4F17A8 = 5183400 / 60 (jiffies per second) = 86390 seconds = 23:59:50 time (so i'm waiting 10 seconds)
Code: Select all
f767 78 SEI
f768 85 A2 STA $A2
f76a 86 A1 STX $A1
f76c 84 A0 STY $A0
f76e 58 CLI
f76f 60 RTS
Btw thank you for help Nino.
Mega-Cart: the cartridge you plug in once and for all.
it should work for NTSC too, as the time routine is made to count real time (TI$ variable).
I guess the small clock difference between NTSC and PAL machines is adjusted by having different timer stop values (in the ROM kernal routine that adjusts the timers).
BTW where is located this routine? I have the C64 kernal but it is different from VIC20.
I guess the small clock difference between NTSC and PAL machines is adjusted by having different timer stop values (in the ROM kernal routine that adjusts the timers).
BTW where is located this routine? I have the C64 kernal but it is different from VIC20.
$f734 UDTIM - Increment Real-Time Clock (Called by IRQ $FEAD)
$FEAD NMI routine
Considering that the routine from f741 to f753 is the same for both PAL/NTSC system (on Vice) i guess that your routine works in both PAL/NTSC machines, thanks.
Code: Select all
f734 A2 00 LDX #$00
f736 E6 A2 INC $A2
f738 D0 06 BNE $F740
f73a E6 A1 INC $A1
f73c D0 02 BNE $F740
f73e E6 A0 INC $A0
f740 38 SEC
f741 A5 A2 LDA $A2
f743 E9 01 SBC #$01
f745 A5 A1 LDA $A1
f747 E9 1A SBC #$1A
f749 A5 A0 LDA $A0
f74b E9 4F SBC #$4F
f74d 90 06 BCC $F755
f74f 86 A0 STX $A0
f751 86 A1 STX $A1
f753 86 A2 STX $A2
f755 AD 2F 91 LDA $912F
f758 CD 2F 91 CMP $912F
f75b D0 F8 BNE $F755
f75d 85 91 STA $91
f75f 60 RTS
$FEAD NMI routine
Code: Select all
fead 48 PHA
feae 8A TXA
feaf 48 PHA
feb0 98 TYA
feb1 48 PHA
feb2 AD 1D 91 LDA $911D
feb5 10 48 BPL $FEFF
feb7 2D 1E 91 AND $911E
feba AA TAX
febb 29 02 AND #$02
febd F0 1F BEQ $FEDE
febf 20 3F FD JSR $FD3F
fec2 D0 03 BNE $FEC7
fec4 6C 02 A0 JMP ($A002)
fec7 2C 11 91 BIT $9111
feca 20 34 F7 JSR $F734 <<=== call to UDTIM
fecd 20 E1 FF JSR $FFE1
fed0 D0 2D BNE $FEFF
fed2 20 52 FD JSR $FD52
fed5 20 F9 FD JSR $FDF9
...
Mega-Cart: the cartridge you plug in once and for all.
- Mike
- Herr VC
- Posts: 4888
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
BTW, $4F1A01 is just 1 jiffy too much.nbla000 wrote:$f734 UDTIM - Increment Real-Time Clock (Called by IRQ $FEAD)
Code: Select all
[...]f740 38 SEC f741 A5 A2 LDA $A2 f743 E9 01 SBC #$01 ; <-- Bzzt... a bug! f745 A5 A1 LDA $A1 f747 E9 1A SBC #$1A f749 A5 A0 LDA $A0 f74b E9 4F SBC #$4F f74d 90 06 BCC $F755 [...]
Try this:
Code: Select all
1 TI$="235959"
2 PRINT TI$:GOTO 2
@nippur72:
On PAL VIC's:
Code: Select all
.C:fe3e A9 26 LDA #$26
.C:fe40 8D 24 91 STA $9124
.C:fe43 A9 48 LDA #$48
.C:fe45 8D 25 91 STA $9125
.C:fe48 60 RTS
Code: Select all
.C:fe3e A9 89 LDA #$89
.C:fe40 8D 24 91 STA $9124
.C:fe43 A9 42 LDA #$42
.C:fe45 8D 25 91 STA $9125
.C:fe48 60 RTS
Michael
By the way, according to this tutorial on the CMP istruction you can replace with
and just save one byte.
Code: Select all
...
LOOP:
SEC
LDA $A2
SBC #L
...
Code: Select all
...
LOOP:
CMP $A2
SBC #L
...
-
- Vic 20 Devotee
- Posts: 253
- Joined: Fri Mar 19, 2010 1:40 pm