How to calculate elapsed time

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
nbla000
Salmon Run
Posts: 2582
Joined: Thu Oct 13, 2005 8:58 am
Location: Italy

How to calculate elapsed time

Post by nbla000 »

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?
Mega-Cart: the cartridge you plug in once and for all.
nippur72
de Lagash
Posts: 574
Joined: Thu Sep 07, 2006 8:35 am

Re: How to calculate elapsed time

Post by nippur72 »

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:

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
(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:

Code: Select all

   LDA #H
   LDX #M
   LDY #L
   JSR $f767
LOOP:
   LDA $A0
   BNE LOOP
(13 bytes)
User avatar
nbla000
Salmon Run
Posts: 2582
Joined: Thu Oct 13, 2005 8:58 am
Location: Italy

Post by nbla000 »

wow the second method is very short, i've used it but inverting Y with A :

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)
According with the code below Y is the higher value and A the lower.

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
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.
Mega-Cart: the cartridge you plug in once and for all.
nippur72
de Lagash
Posts: 574
Joined: Thu Sep 07, 2006 8:35 am

Post by nippur72 »

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.
User avatar
nbla000
Salmon Run
Posts: 2582
Joined: Thu Oct 13, 2005 8:58 am
Location: Italy

Post by nbla000 »

$f734 UDTIM - Increment Real-Time Clock (Called by IRQ $FEAD)

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
...
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.
Mega-Cart: the cartridge you plug in once and for all.
nippur72
de Lagash
Posts: 574
Joined: Thu Sep 07, 2006 8:35 am

Post by nippur72 »

ok, but I meant where is located the routine that loads the proper timer stop values into the VIA Chip 6522.
User avatar
Mike
Herr VC
Posts: 4850
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

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
[...]
BTW, $4F1A01 is just 1 jiffy too much.

Try this:

Code: Select all

1 TI$="235959"
2 PRINT TI$:GOTO 2
If you're lucky, you'll watch "240000" once just before the TI$ clock returns to "000000". :) It loses one jiffy each day.

@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
On NTSC VIC's:

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
Greetings,

Michael
User avatar
nbla000
Salmon Run
Posts: 2582
Joined: Thu Oct 13, 2005 8:58 am
Location: Italy

Post by nbla000 »

Thanks Mike, no more secrets for Vic Time :wink:
Mega-Cart: the cartridge you plug in once and for all.
nippur72
de Lagash
Posts: 574
Joined: Thu Sep 07, 2006 8:35 am

Post by nippur72 »

By the way, according to this tutorial on the CMP istruction you can replace

Code: Select all

...
LOOP:
   SEC
   LDA $A2
   SBC #L 
...
with

Code: Select all

...
LOOP:
   CMP $A2
   SBC #L
... 
and just save one byte.
User avatar
Mike
Herr VC
Posts: 4850
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

The C flag will be set at random by the CMP $A2 instruction, depending on the value of A before. This is likely to break this short-cut.

Michael
vicassembly
Vic 20 Devotee
Posts: 253
Joined: Fri Mar 19, 2010 1:40 pm

Post by vicassembly »

This forum is so helpful. I found this thread and it is exactly what I needed to help with my ML game.

Jonathan
User avatar
nbla000
Salmon Run
Posts: 2582
Joined: Thu Oct 13, 2005 8:58 am
Location: Italy

Post by nbla000 »

vicassembly wrote:This forum is so helpful. I found this thread and it is exactly what I needed to help with my ML game.
indeed :wink:

I used this information for the final hi-score saving time-out of Tetris+
Mega-Cart: the cartridge you plug in once and for all.
Post Reply