Main Page Data Structures File List Data Fields Globals Related Pages

# ztimer.cpp

Go to the documentation of this file.

    00001 /*  ztimer.cpp
    00002     Inspired by Michael Abrash's legendary Zen Timer.
    00003
    00004     12.08.2001: tk, initial implementation.
    00005
    00006     Copyright (c)2001 by Thomas Kindler, thomas.kindler@gmx.de
    00007
    00008     This program is free software; you can redistribute it and/or
    00009     modify it under the terms of the GNU General Public License as
    00010     published by the Free Software Foundation; either version 2 of
    00011     the License, or (at your option) any later version. Read the
    00012     full License at http://www.gnu.org/copyleft for more details.
    00013 */
    00014 #include <stdio.h>
    00015 #include <dos.h>
    00016 #include "amd186.h"
    00017
    00018 static unsigned old_t2con  = TCON_INT | TCON_CONT;
    00019 static unsigned old_t2cmpa = 5000;
    00020
    00021 static unsigned last_t2cnt;
    00022 static bool     isActive = false;
    00023
    00044 void ZTimer_Start()
    00045 {
    00046   isActive = true;
    00047   disable();
    00048   old_t2con = inpw(PCB_T2CON);
    00049   old_t2cmpa = inpw(PCB_T2CMPA);
    00050   outpw(PCB_T2CON, TCON_STOP);
    00051   outpw(PCB_T2CMPA, 0);
    00052   outpw(PCB_T2CNT, 0);
    00053   outpw(PCB_T2CON, TCON_START);
    00054 }
    00055
    00056
    00066 int ZTimer_Stop()
    00067 {
    00068   last_t2cnt = inpw(PCB_T2CNT);
    00069   outpw(PCB_T2CMPA, old_t2cmpa);
    00070   outpw(PCB_T2CON, old_t2con | TCON_START);
    00071   enable();
    00072   isActive = false;
    00073   return  last_t2cnt/5;
    00074 }
    00075
    00076
    00085 void ZTimer_Report()
    00086 {
    00087   if (isActive)
    00088     ZTimer_Stop();
    00089   if (last_t2cnt) {
    00090     // measure overhead
    00091     //
    00092     unsigned  time     = last_t2cnt;
    00093     unsigned  overhead = 65535u;
    00094     for (int i=0; i<16; i++) {
    00095       ZTimer_Start();
    00096       ZTimer_Stop();
    00097       if (last_t2cnt < overhead)
    00098         overhead = last_t2cnt;
    00099     }
    00100     printf("%u us\n", (time - overhead)/5);
    00101   } else {
    00102     printf(">13.1072 ms\n");
    00103   }
    00104 }