1 REM Next BASIC Timers demonstration 1.0 2 REM Copyright Simon N Goodwin 2020, CC-NC-SA 4 REM Some procedures require the Real Time Clock 6 REM 16 bit PROC dF requires NextZXOS 2.06A or later 7 8 REM Example framework for timing BASIC 10 PROC dF(): REM delta Time 15 REM frames=0 DPOKE 23672,0 20 PROC dF() 25 REPEAT 30 FOR %j=1 TO 100: PROC dQ(): NEXT %j 35 PROC dF() 40 PRINT %t;" frames per 100 checks" 50 REPEAT UNTIL INKEY$ =" " 80 STOP 82 REM Put whatever you want to time in line 30 85 REM GO SUB 600 is faster than PROC dQ() 90 190 REM Up to 255 frames in t% 200 DEFPROC dT() 210 LET %b=% PEEK 23672 220 LET %t=%b-a: IF %b15 THEN PRINT "No such channel ";ch: STOP 945 LET %c=ch 950 LET p=% DPEEK ((c+3)*2+23568): REM Get offset from STRMS 960 IF p=0 THEN PRINT "#";ch;" is not open": STOP 965 LET %q=% DPEEK 23578 970 REM PRINT "Target was ";p,%q 980 DPOKE 23578,p 990 ENDPROC 999 1000 DEFPROC REVERT() 1010 DPOKE 23578,%q 1015 CLOSE # %c 1020 ENDPROC 1030 1250 REM Read NextREG to detect 50 or 60 Hertz 1260 DEFPROC HERTZ() 1280 ENDPROC = %50 + (5 * ( REG 5 & 4)/2) 1290 2860 REM This extracts time components from the .time command 2870 DEFPROC CLOCKWORK() 3100 DIM v$(22) 3110 OPEN # 4,"V>v$" 3120 PROC DIVERT(4) 3130 .time 3150 PROC REVERT() 3160 PRINT v$ 3170 IF v$(15)<>v$(18) THEN PRINT "Bad time ";v$: STOP 3240 PRINT v$(13 TO 14);" hours, "; 3260 PRINT v$(16 TO 17);" minutes, "; 3270 PRINT v$(19 TO 20);" seconds" 3310 LET t= VAL v$(19 TO 20)+60 * ( VAL v$(16 TO 17)+60* VAL v$(13 TO 14)) 3320 PRINT 't;" seconds have elapsed today."'' 3330 ENDPROC 3340 3380 REM Return the number of seconds the RTC has counted today 3390 DEFPROC SECONDS() 3395 LOCAL t: REM v$ 3400 DIM v$(22) 3410 OPEN # 4,"V>v$" 3420 PROC DIVERT(4) 3430 .time 3440 PROC REVERT() 3450 IF v$(15)<>v$(18) THEN PRINT "Bad time ";v$: STOP 3460 LET t= VAL v$(19 TO 20)+60 * ( VAL v$(16 TO 17)+60* VAL v$(13 TO 14)) 3470 ENDPROC =t 3480 3482 REM Returns at the start of a new real-time clock second 3483 DEFPROC AWAIT() 3486 LOCAL old,time 3490 PROC SECONDS() TO old 3500 REPEAT 3510 PROC SECONDS() TO time 3530 REM PAUSE 1: PRINT time, 3560 REPEAT UNTIL time=old+1 3570 ENDPROC = time 3590 3600 DEFPROC TFRAME() 3610 PROC AWAIT() TO t 3620 PROC HERTZ() TO h: PRINT h 3630 PROC MODES() 3670 ENDPROC 3680 3690 REM PROC REALTIME(20) times a '20 second' PAUSE 3700 DEFPROC REALTIME(w) 3702 LOCAL h,t,e 3705 PROC MODES() 3707 PROC HERTZ() TO h 3710 PROC AWAIT() TO t 3720 PAUSE h*w 3730 PROC SECONDS() TO e 3740 PRINT w;" second PAUSE took ";e-t;" seconds" 3800 ENDPROC 3990 9999 SAVE "Timers.bas"