the code following illustrates three different timers.
peektimer is modified from an earlier pbdos message
by hans ruegg, exact timing. it counts 256 * 18 ticks/sec.
although it works in the test code below, it doesn't work
in a practical program.
newtimer is an attempt to convert the above to assembler.
mstimer was found on the pbxtra cd, here slightly
modified. i don't understand how it works, but it
works. it counts approximately 1000 ticks/sec
(more like the high 900's). part of the complexity
of the code is due to counting 1000 ticks/sec instead
of 4608, but only part.
main question: what's wrong with peektimer?
minor question: how to convert peektimer to newtimer,
that is, assembler? the test code works most of the
time but sometimes gives a negative number. using
cli/sti doesn't help.
minor question: in mstimer, are the lines marked with
a question necessary?
'test code
[this message has been edited by mark hunter (edited march 29, 2001).]
peektimer is modified from an earlier pbdos message
by hans ruegg, exact timing. it counts 256 * 18 ticks/sec.
although it works in the test code below, it doesn't work
in a practical program.
newtimer is an attempt to convert the above to assembler.
mstimer was found on the pbxtra cd, here slightly
modified. i don't understand how it works, but it
works. it counts approximately 1000 ticks/sec
(more like the high 900's). part of the complexity
of the code is due to counting 1000 ticks/sec instead
of 4608, but only part.
main question: what's wrong with peektimer?
minor question: how to convert peektimer to newtimer,
that is, assembler? the test code works most of the
time but sometimes gives a negative number. using
cli/sti doesn't help.
minor question: in mstimer, are the lines marked with
a question necessary?
'test code
Code:
dim x as long print "peektimer "; def seg = 0 x = peektimer delay 1 'how many ticks in one second print peektimer - x '4608 ticks, which is 18 * 256 print "newtimer "; x = newtimer delay 1 print newtimer - x 'usually, not always, equals above print "mstimer "; x = mstimer delay 1 print mstimer - x 'approximately 1000 (a little less) '-------------------------------------------------- 'by hans ruegg -- see link above function peektimer as long 'assumes def seg = 0 out &h43, &h34 'set timer to "mode 2" low? = inp(&h40) 'discard first time low? = inp(&h40) a& = peekl(&h46c) shift left a&, 8 function = a& + 256 - low? '+ 256 is not necessary end function 'the above converted to assembler, except for + 256 function newtimer as long ! mov al,&h34 'set timer to "mode 2" ! out &h43,al ! in al,&h40 'discard first time ! in al,&h40 'low ! xor ah,ah ! mov dx,ax 'save low in dx ! mov bx,&h0040 'want address &h46c ! mov es,bx ! mov bx,&h006c ! mov ax,es:[bx] ! mov cl,8 'shift left by 8 ! shl ax,cl ! sub ax,dx 'subtract off low ! mov function,ax end function 'from pbxtra, more or less function mstimer as long dim saveax as word, savedx as word dim k1 as byte, k2 as byte ! mov bx,&h0040 ! mov es,bx ! mov bx,&h006c s0101: ! mov al,&hc2 ! cli ! out &h43,al ! jmp s0108 ;? s0108: ! in al,&h40 ! jmp s010c ;? s010c: ! shl al,1 ;? shl al doesnt affect ah ! in al,&h40 ! jmp s0112 ;? s0112: ! mov cl,al ! in al,&h40 ! jmp s0118 ;? s0118: ! mov ch,al ! jcxz s0190 ;necessary? ! rcr cx,1 ! not cx ! mov ax,&heccc ! mul word ptr es:[bx] ! mov k1,ah ;temporary store ! mov k2,al ! mov si,dx ! mov ax,&heccc ! mul word ptr es:[bx+02] ! mov di,dx ! add si,ax ! adc di,+00 ! mov ax,&h0036 ! mul word ptr es:[bx] ! add si,ax ! adc di,dx ! mov ax,&h0036 ! mul word ptr es:[bx+02] ! add di,ax ! mov ax,&hdbb3 ! shr cx,1 ! shr cx,1 ! mul cx ! add ah,k1 ;use temp store ! adc dl,k2 ! mov ax,si ! adc al,dh ! adc ah,00 ! mov dx,di ! adc dx,+00 ;below happens every 32000 ticks ! cmp dx,savedx ;hi byte increased ? ! ja s017d ! cmp ax,saveax ;lo byte increased ? ! jb s0189 s017d: ! mov saveax,ax ! mov savedx,dx ! sti ! jmp short endmstimer s0189: ! cmp savedx,1318 ;almost midnight ? ! je s017d ;ignore decrease in tick& if is s0190: 'otherwise hangs for several secs ! sti ! jmp s0101 endmstimer: function = saveax + savedx * 65536 end function
[this message has been edited by mark hunter (edited march 29, 2001).]
Comment