[SeaBIOS] [PATCH 3/4] Generalize timer based delay code.

Kevin O'Connor kevin at koconnor.net
Sun May 23 22:16:46 CEST 2010


Move the timer based counting code in serial.c to clock.c.

Rework the interface to make it similar to the tsc based timers.
---
 src/clock.c  |   26 +++++++++++++++++++++++++-
 src/serial.c |   37 ++++++-------------------------------
 src/util.h   |    3 +++
 3 files changed, 34 insertions(+), 32 deletions(-)

diff --git a/src/clock.c b/src/clock.c
index 0567955..062658c 100644
--- a/src/clock.c
+++ b/src/clock.c
@@ -54,7 +54,6 @@
  * TSC timer
  ****************************************************************/
 
-#define TICKS_PER_DAY (u32)((u64)60*60*24*PIT_TICK_RATE / PIT_TICK_INTERVAL)
 #define CALIBRATE_COUNT 0x800   // Approx 1.7ms
 
 u32 cpu_khz VAR16VISIBLE;
@@ -223,6 +222,31 @@ timer_setup(void)
  * Standard clock functions
  ****************************************************************/
 
+#define TICKS_PER_DAY (u32)((u64)60*60*24*PIT_TICK_RATE / PIT_TICK_INTERVAL)
+
+// Calculate the timer value at 'count' number of full timer ticks in
+// the future.
+u32
+calc_future_timer_ticks(u32 count)
+{
+    return (GET_BDA(timer_counter) + count + 1) % TICKS_PER_DAY;
+}
+// Return the timer value that is 'msecs' time in the future.
+u32
+calc_future_timer(u32 msecs)
+{
+    u32 kticks = DIV_ROUND_UP((u64)(msecs * PIT_TICK_RATE), PIT_TICK_INTERVAL);
+    u32 ticks = DIV_ROUND_UP(kticks, 1000);
+    return calc_future_timer_ticks(ticks);
+}
+// Check if the given timer value has passed.
+int
+check_timer(u32 end)
+{
+    return (((GET_BDA(timer_counter) + TICKS_PER_DAY - end) % TICKS_PER_DAY)
+            < (TICKS_PER_DAY/2));
+}
+
 // get current clock count
 static void
 handle_1a00(struct bregs *regs)
diff --git a/src/serial.c b/src/serial.c
index 3f68bc4..21b4bd0 100644
--- a/src/serial.c
+++ b/src/serial.c
@@ -9,31 +9,6 @@
 #include "util.h" // debug_enter
 #include "bregs.h" // struct bregs
 
-// Timers based on 18.2Hz clock irq.
-struct tick_timer_s {
-    u16 last_tick, remaining;
-};
-
-struct tick_timer_s
-initTickTimer(u16 count)
-{
-    struct tick_timer_s tt = {GET_BDA(timer_counter), count};
-    return tt;
-}
-
-int
-checkTickTimer(struct tick_timer_s *tt)
-{
-    u16 timer = GET_BDA(timer_counter);
-    if (tt->last_tick != timer) {
-        tt->last_tick = timer;
-        tt->last_tick--;
-        if (!tt->last_tick)
-            return 1;
-    }
-    return 0;
-}
-
 
 /****************************************************************
  * COM ports
@@ -117,7 +92,7 @@ handle_1401(struct bregs *regs)
     u16 addr = getComAddr(regs);
     if (!addr)
         return;
-    struct tick_timer_s tt = initTickTimer(GET_BDA(com_timeout[regs->dx]));
+    u32 end = calc_future_timer_ticks(GET_BDA(com_timeout[regs->dx]));
     for (;;) {
         u8 lsr = inb(addr+SEROFF_LSR);
         if ((lsr & 0x60) == 0x60) {
@@ -127,7 +102,7 @@ handle_1401(struct bregs *regs)
             regs->ah = lsr;
             break;
         }
-        if (checkTickTimer(&tt)) {
+        if (check_timer(end)) {
             // Timed out - can't write data.
             regs->ah = lsr | 0x80;
             break;
@@ -144,7 +119,7 @@ handle_1402(struct bregs *regs)
     u16 addr = getComAddr(regs);
     if (!addr)
         return;
-    struct tick_timer_s tt = initTickTimer(GET_BDA(com_timeout[regs->dx]));
+    u32 end = calc_future_timer_ticks(GET_BDA(com_timeout[regs->dx]));
     for (;;) {
         u8 lsr = inb(addr+SEROFF_LSR);
         if (lsr & 0x01) {
@@ -153,7 +128,7 @@ handle_1402(struct bregs *regs)
             regs->ah = lsr;
             break;
         }
-        if (checkTickTimer(&tt)) {
+        if (check_timer(end)) {
             // Timed out - can't read data.
             regs->ah = lsr | 0x80;
             break;
@@ -261,7 +236,7 @@ handle_1700(struct bregs *regs)
     if (!addr)
         return;
 
-    struct tick_timer_s tt = initTickTimer(GET_BDA(lpt_timeout[regs->dx]));
+    u32 end = calc_future_timer_ticks(GET_BDA(lpt_timeout[regs->dx]));
 
     outb(regs->al, addr);
     u8 val8 = inb(addr+2);
@@ -276,7 +251,7 @@ handle_1700(struct bregs *regs)
             regs->ah = v ^ 0x48;
             break;
         }
-        if (checkTickTimer(&tt)) {
+        if (check_timer(end)) {
             // Timeout
             regs->ah = (v ^ 0x48) | 0x01;
             break;
diff --git a/src/util.h b/src/util.h
index 7902e83..09d9e40 100644
--- a/src/util.h
+++ b/src/util.h
@@ -304,6 +304,9 @@ void usleep(u32 count);
 void msleep(u32 count);
 u64 calc_future_tsc(u32 msecs);
 u64 calc_future_tsc_usec(u32 usecs);
+u32 calc_future_timer_ticks(u32 count);
+u32 calc_future_timer(u32 msecs);
+int check_timer(u32 end);
 void handle_1583(struct bregs *regs);
 void handle_1586(struct bregs *regs);
 void useRTC(void);
-- 
1.6.6.1




More information about the SeaBIOS mailing list