![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-10-31 23:25 |
47,815 topics / 349,930 posts / today 0 new / 48,290 members |
| DOS开发编程 & 发展交流 (开发室) » A question that has always existed: What is the exact frequency of the 8253 timer on a PC? |
| Printable Version 1,851 / 11 |
| Floor1 zyl910 | Posted 2006-06-03 12:40 |
| 中级用户 Posts 126 Credits 282 | |
|
What is the exact input frequency of the 8253 timer chip on a PC?
Many books say that the output frequency of timer #0 of the 8253 on a PC is 18.2 times per second (triggering once every 55ms), but all say this is only an approximate value, and the exact value has a long string of decimals. Computers should rely on integer operations, and those decimal values should just be the result of converting to real time. So I wanted to know the exact frequency, so I looked up a lot of materials, and it turned out that there were some differences: 1. The exact value (counter #0) as said in many books: F: 18.2064819336 times per second T: once every 54.925ms. 2. Terrv Dettmann's "DOS Programmer's Reference": H: The system clock is called about 18.2 times per second (65536 times per hour). D: The 00h function of int 1Ah: This interrupt obtains the system clock counter, which starts from zero and counts 18.2065 times per second. The total number of counts in a full day (from midnight) is 1573040 counts, and the elapsed time is 86399.9129 seconds. 3. Mazid's "Assembly Language, Design and Interface Technology": The 8284 chip is connected to a 14.31818MHz crystal oscillator source. The Clr pin output is the main frequency of the CPU, memory (system bus) in 8088, divided by 3, 4.772776MHz. PClk is connected to the 8253/54 timer, divided by 6, 2.386383MHz. There is a 72LS175 D flip-flop between the 8284 and the 8253, so the frequency is divided by 2 again, 1.1931817MHz. 4. Some interface technology books say that the frequency period of the crystal oscillator source is 70ns, matching the memory speed. 5. On many computers, the return value of QueryPerformanceFrequency (Win32 API) is 3579545. 3579545 * 4 = 14318180, which matches 3. Constant Analysis ~~~~~~~~ Now analyze these constants: The first type, according to the frequency (18.2064819336 times per second) Crystal oscillator source frequency: 18.2064819336Hz * 65536 * 12 = 14,318,160.0000049152Hz Period: 1s / 14,318,160.0000049152Hz = 0.000000069841376266200176256402804053989s = 69.841376266200176256402804053989ns 8253 input frequency: 18.2064819336Hz * 65536 = 1,193,180.0000004096Hz Period: 1s / 1,193,180.0000004096Hz = 0.00000083809651519440211507683364864787s = 838.09651519440211507683364864787ns Timer #0 output frequency: 18.2064819336Hz Period: 1s / 18.2064819336Hz = 0.054925493219780337013675369997787s = 54.925493219780337013675369997787ms Timer #0 triggers 65536 times: (1s / 18.2064819336Hz) * 65536 = 65536s / 18.2064819336Hz = 3599.597123651524166528229048175s Timer #0 triggers 1573040 times: (1s / 18.2064819336Hz) * 1573040 = 1573040s / 18.2064819336Hz = 86399.997854443261335991904021318s The first type, according to the period (once every 54.925ms) Crystal oscillator source frequency: (1s / 54.925ms) * 65536 * 12 = (65536 * 12)s / 0.054925s = 14,318,288.575329995448338643604916Hz Period: 54.925ms / (65536 * 12) = 0.000000069840749104817708333333333333333s = 69.840749104817708333333333333333ns 8253 input frequency: (1s / 54.925ms) * 65536 = 65536s / 0.054925s = 1,193,190.7146108329540282203004096Hz Period: 54.925ms / 65536 = 0.0000008380889892578125s = 838.0889892578125ns Timer #0 output frequency: 1s / 54.925ms = 18.206645425580336822940373236231Hz Period: 54.925ms Timer #0 triggers 65536 times: 54.925ms * 65536 = 3599.5648s Timer #0 triggers 1573040 times: 54.925ms * 1573040 = 86399.222s The second type, according to the hour (65536 times per hour) Crystal oscillator source frequency: 1s / (3600s / 65536 / 65536 / 12) = (65536 * 65536 * 12)s / 3600s = 14,316,557.653333333333333333333333Hz Period: 3600s / 65536 / 65536 / 12 = 3600s / (65536 * 65536 * 12) = 0.000000069849193096160888671875s = 69.849193096160888671875ns 8253 input frequency: 1s / (3600s / 65536 / 65536) = (65536*65536)s / 3600s = 1,193,046.4711111111111111111111111Hz Period: 3600s / 65536 / 65536 = 3600s / (65536 * 65536) = 0.0000008381903171539306640625s = 838.1903171539306640625ns Timer #0 output frequency: 1s / (3600s / 65536) = 65536s / 3600s = 18.204444444444444444444444444444Hz Period: 3600s / 65536 = 0.054931640625s = 54.931640625ms Timer #0 triggers 65536 times: 3600s Timer #0 triggers 1573040 times: (3600s / 65536) * 1573040 = (3600s * 1573040) / 65536 = 86409.66796875s The second type, according to the day (clock counts 1573040 times, elapsed time is 86399.9129 seconds) Crystal oscillator source frequency: 1s / (86399.9129s / 1573040 / 65536 / 12) = (1573040 * 65536 * 12)s / 86399.9129s = 14,318,174.078622248240715529702808Hz Period: 86399.9129s / 1573040 / 65536 / 12 = 86399.9129s / (1573040 * 65536 * 12) = 0.000000069841307593336928084579328349353s = 69.841307593336928084579328349353ns 8253 input frequency: 1s / (86399.9129s / 1573040 / 65536) = (1573040 * 65536)s / 86399.9129s = 1,193,181.1732185206867262941419007Hz Period: 86399.9129s / 1573040 / 65536 = 86399.9129s / (1573040 * 65536) = 0.00000083809569112004313701495194019224s = 838.09569112004313701495194019224ns Timer #0 output frequency: 1s / (86399.9129s / 1573040) = 1573040s / 86399.9129s = 18.206499835487681377049165983592Hz Period: 86399.9129s / 1573040 = 0.054925439213243147027411890352439s = 54.925439213243147027411890352439ms Timer #0 triggers 65536 times: (86399.9129s / 1573040) * 65536Hz = (86399.9129s * 65536) / 1573040 = 3599.5935842791028835884656461374s Timer #0 triggers 1573040 times: 86399.9129s The third type (8284 chip connected to a 14.31818MHz crystal oscillator source) Crystal oscillator source frequency: 14.31818MHz = 14,318,180Hz Period: 1s / 14,318,180Hz = 0.000000069841278710003645714748662190306s = 69.841278710003645714748662190306ns 8253 input frequency: 14,318,180Hz / 12 = 1,193,181.6666666666666666666666667Hz Period: 1s / (14,318,180Hz / 12) = 12s / 14,318,180Hz = 0.00000083809534452004374857698394628368s = 838.09534452004374857698394628368ns Timer #0 output frequency: 14,318,180Hz / 12 / 65536 = 14,318,180Hz / (12 * 65536) = 18.206507364908854166666666666667Hz Period: 1s / (14,318,180Hz / 12 / 65536) = (12 * 65536)s / 14,318,180Hz = 0.054925416498465587106741219903647s = 54.925416498465587106741219903647ms Timer #0 triggers 65536 times: (1s / (14,318,180Hz / 12 / 65536)) * 65536 = (12 * 65536 * 65536)s / 14,318,180Hz = 3599.5920956434407166273925876054s Timer #0 triggers 1573040 times: (1s / (14,318,180Hz / 12 / 65536)) * 1573040 = (12 * 65536 * 1573040)s / 14,318,180Hz = 86399.877168746307142388208557233s The fourth type (the frequency period of the crystal oscillator source is 70ns, matching the memory speed) Crystal oscillator source frequency: 1s / 0.00000007s = 14,285,714.285714285714285714285714Hz Period: 70ns = 0.00000007s 8253 input frequency: 1s / (0.00000007s * 12) = 1,190,476.1904761904761904761904762Hz Period: 0.00000007s * 12 = 0.00000084s = 840ns Timer #0 output frequency: 1s / (0.00000007s * 12 * 65536) = 18.165225074404761904761904761905Hz Period: 0.00000007s * 12 * 65536 = 0.05505024s = 55.05024ms Timer #0 triggers 65536 times: (0.00000007s * 12 * 65536) * 65536 = 3607.77252864s Timer #0 triggers 1573040 times: (0.00000007s * 12 * 65536) * 1573040 = 86596.2295296s Total Table ~~~~ Crystal oscillator source frequency (Hz): 1F: 14,318,160.0000049152 1T: 14,318,288.575329995448338643604916 2H: 14,316,557.653333333333333333333333 2D: 14,318,174.078622248240715529702808 3 : 14,318,180 4 : 14,285,714.285714285714285714285714 Crystal oscillator source period (ns): 1F: 69.841376266200176256402804053989 1T: 69.840749104817708333333333333333 2H: 69.849193096160888671875 2D: 69.841307593336928084579328349353 3 : 69.841278710003645714748662190306 4 : 70 8253 input frequency (Hz): 1F: 1,193,180.0000004096 1T: 1,193,190.7146108329540282203004096 2H: 1,193,046.4711111111111111111111111 2D: 1,193,181.1732185206867262941419007 3 : 1,193,181.6666666666666666666666667 4 : 1,190,476.1904761904761904761904762 8253 input period (ns): 1F: 838.09651519440211507683364864787 1T: 838.0889892578125 2H: 838.1903171539306640625 2D: 838.09569112004313701495194019224 3 : 838.09534452004374857698394628368 4 : 840 Timer #0 output frequency (Hz): 1F: 18.2064819336 1T: 18.206645425580336822940373236231 2H: 18.204444444444444444444444444444 2D: 18.206499835487681377049165983592 3 : 18.206507364908854166666666666667 4 : 18.165225074404761904761904761905 Timer #0 output period (ms): 1F: 54.925493219780337013675369997787 1T: 54.925 2H: 54.931640625 2D: 54.925439213243147027411890352439 3 : 54.925416498465587106741219903647 4 : 55.05024 Timer #0 triggers 65536 times (s): 1F: 3599.597123651524166528229048175 1T: 3599.5648 2H: 3600 2D: 3599.5935842791028835884656461374 3 : 3599.5920956434407166273925876054 4 : 3607.77252864 Timer #0 triggers 1573040 times (s): 1F: 86399.997854443261335991904021318 1T: 86399.222 2H: 86409.66796875 2D: 86399.9129 3 : 86399.877168746307142388208557233 4 : 86596.2295296 Conclusion ~~~~ Personally, I think the statement in "Assembly Language, Design and Interface Technology" is the most credible, with a crystal oscillator source frequency of 14,318,180Hz, because it matches many numbers. PS: I have always had this question, but I never studied it. Until a few days ago, I saw an article by Fengyun "Not Very Accurate Clock" (http://blog.codingnow.com/2006/05/iaeeoeo.html) and then realized the seriousness of this problem, which caught my attention. [ Last edited by zyl910 on 2006-6-3 at 12:44 ] |
|
| Floor2 Scott0902 | Posted 2006-06-03 14:03 |
| 中级用户 Posts 237 Credits 466 | |
| Floor3 zyl910 | Posted 2006-06-03 14:27 |
| 中级用户 Posts 126 Credits 282 | |
|
Just now I flipped through "Graphics Programmer's Guide (Michael Abrash's Graphics Programming Black Book Special Editior)" again, and found that the code uses an (input frequency period) of 0.8381ms (ZTimerReport function):
The following is the description of 8253 in the "Graphics Programmer's Guide", which is consistent with "Assembly Language, Design and Interface Technology":
|
|
| Floor4 jawbin | Posted 2006-06-08 19:29 |
| 高级用户 Posts 444 Credits 994 | |
|
In normal use, I just take it as 18 times, and I haven't required such precision. It can be set to a higher frequency (UCOS/II seems to have changed it), but it seems to affect performance.
|
|
| Floor5 fqljwdyq | Posted 2006-06-09 08:15 |
| 新手上路 Posts 7 Credits 14 From 江苏 | |
|
In actual applications, precise timing never uses the system-provided 8253. Generally, timing from expansion cards is used, and this 18.2 seconds of the system is not of much concern.
|
|
| Floor6 zyl910 | Posted 2006-06-09 09:02 |
| 中级用户 Posts 126 Credits 282 | |
|
But I want to achieve millisecond-level timing under DOS!
It's for making animation and game programs There's no hope of others installing expansion cards |
|
| Floor7 zyl910 | Posted 2006-06-09 09:08 |
| 中级用户 Posts 126 Credits 282 | |
|
My purpose is: to write functions similar to the Win32 API - QueryPerformanceFrequency, QueryPerformanceCounter - to achieve high-precision timing
|
|
| Floor8 zyl910 | Posted 2006-06-09 09:14 |
| 中级用户 Posts 126 Credits 282 | |
|
Cai cai's question:
What are TSC and ACPI? How to use them to time? ACPI seems to be that advanced power management, does it also have a timing function? Oh, it's really hard to find hardware interface information now
|
|
| Floor9 asbai | Posted 2006-06-09 13:05 |
| 高级用户 Posts 252 Credits 653 | |
|
TSC is a newly added 64-bit register in Pentium and above processors. This counter increments by one automatically at each CPU base clock (the base clock refers to the stage of the pipeline). TSC can be read using the RDTSC instruction.
The QueryPerformanceCounter in the Win32 API indeed should use TSC. First, its precision is very high. Second, MSDN says that this API uses some hardware-related features, and it may not be supported on some CPUs. The main design intention of TSC is profiling. In most cases, it is not suitable to use it for timing, mainly because the overhead is relatively high. In most cases, the 8253 itself can work at a precision of the nanosecond level at most. This precision should be sufficient for most applications, but any operating system including Windows usually cannot keep its own working granularity at this frequency, which would make the maintenance overhead of the operating system itself too large. Plus, when Windows handles interrupts, in order to further reduce the overhead, it specially uses the DPC mechanism. So the Timer granularity of NT-series Windows client products is generally between 10ms and 15ms, and the granularity of server products is generally between 15ms and 30ms. Through the Windows Native API (not the Win32 API): NtSetTimerResolution, the working granularity of the system can be changed. However, this change will affect all processes in the current system, and improper settings may increase the management burden of the system. What is even more frustrating is that there are signs that this setting may also affect the working state of the system kernel, such as thread scheduling, blocked event and asynchronous IO state query, etc. Adjusting this value also correspondingly increases the frequency of these tasks, leading to further performance degradation of the system. |
|
| Floor10 asbai | Posted 2006-06-09 13:14 |
| 高级用户 Posts 252 Credits 653 | |
|
ACPI?
Brother zyl910 is referring to the Advanced Programmable Interrupt Controller (APIC) Timer? As for the commonly said ACPI Timer, it is the periodic timing interrupt triggered by the RTC in an ACPI-compliant system. There is a special article on various hardware Timers and timing issues introduced by Microsoft's Hardware Development Center: http://www.microsoft.com/whdc/system/CEC/mm-timer.mspx |
|
| Floor11 zyl910 | Posted 2006-06-10 12:56 |
| 中级用户 Posts 126 Credits 282 | |
|
昏
Original TSC refers to RDTSC. I didn't pay attention to its abbreviation usually. Thanks a lot, brother asbai. Now it's really hard to find hardware interface programming materials. It's quite troublesome to write a more accurate timing function under DOS. |
|
| Floor12 asbai | Posted 2006-06-10 14:16 |
| 高级用户 Posts 252 Credits 653 | |
Originally posted by zyl910 at 2006-6-10 12: Brother zyl910, you're too polite. Now, hardware materials are really hard to find. Intel and AMD released x86 developer manuals are both authoritative and detailed (especially AMD), which should be the preferred source. There is also a website: http://www.powernet.co.za/info/index.htm, although the speed is a bit slow, the materials are quite complete. I only know so much. Brothers, do you have any good places, take them out and share them. ![]() |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |