瀏覽代碼

Rework timer driver

Streetwalrus Einstein 10 年之前
父節點
當前提交
2840454083
共有 5 個文件被更改,包括 92 次插入74 次删除
  1. 17 0
      include/Timers.h
  2. 0 20
      include/timers.h
  3. 67 0
      src/Timers.cpp
  4. 8 8
      src/main.cpp
  5. 0 46
      src/timers.c

+ 17 - 0
include/Timers.h

@@ -0,0 +1,17 @@
+#ifndef INCLUDE_TIMERS_H
+#define INCLUDE_TIMERS_H
+
+namespace WalrusRPG
+{
+    namespace Timers
+    {
+        void init(unsigned timer);
+        void restore(unsigned timer);
+        void mode(unsigned timer, bool free_run, bool oneshot, bool interrupt,
+                  unsigned div, bool full_width_counter);
+        void load(unsigned timer, unsigned value);
+        unsigned read(unsigned timer);
+    }
+}
+
+#endif

+ 0 - 20
include/timers.h

@@ -1,20 +0,0 @@
-#ifndef INCLUDE_TIMERS_H
-#define INCLUDE_TIMERS_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-// TODO : Move these functions to C++ namespace
-// TODO : add defines/const values to make the modes more clearer to read
-
-void timer_init(unsigned timer);
-void timer_restore(unsigned timer);
-void timer_mode(unsigned timer, unsigned mode);
-void timer_load(unsigned timer, unsigned value);
-unsigned timer_read(unsigned timer);
-
-#ifdef __cplusplus
-}
-#endif
-#endif

+ 67 - 0
src/Timers.cpp

@@ -0,0 +1,67 @@
+#include <os.h>
+#include <Timers.h>
+
+#define TIMER 0x900D0000
+volatile unsigned *timer_ctl = (unsigned *) (TIMER + 0x08);
+volatile unsigned *timer_load = (unsigned *) (TIMER);
+volatile unsigned *timer_value = (unsigned *) (TIMER + 0x04);
+unsigned timer_ctl_bkp[2], timer_load_bkp[2];
+
+#define TIMERS WalrusRPG::Timers
+
+void TIMERS::init(unsigned timer)
+{
+    timer_ctl_bkp[timer] = timer_ctl[8 * timer];
+    timer_load_bkp[timer] = timer_load[8 * timer];
+}
+
+void TIMERS::restore(unsigned timer)
+{
+    timer_ctl[8 * timer] &= ~(1 << 7);
+    timer_ctl[8 * timer] = timer_ctl_bkp[timer] & ~(1 << 7);
+    timer_load[8 * timer] = timer_load_bkp[timer];
+    timer_ctl[8 * timer] = timer_ctl_bkp[timer];
+}
+
+void TIMERS::mode(unsigned timer, bool free_run, bool oneshot, bool interrupt,
+                  unsigned div, bool full_width_counter)
+{
+    unsigned mode = 0;
+
+    if (!free_run)
+        mode |= (1 << 6);
+
+    if (oneshot)
+        mode |= (1 << 0);
+
+    if (interrupt)
+        mode |= (1 << 5);
+
+    if (full_width_counter)
+        mode |= (1 << 1);
+
+    switch (div)
+    {
+        case 256:
+            mode |= (1 << 3);
+            break;
+
+        case 16:
+            mode |= (1 << 2);
+            break;
+    }
+
+    timer_ctl[8 * timer] &= ~(1 << 7);
+    timer_ctl[8 * timer] = mode;
+    timer_ctl[8 * timer] |= (1 << 7);
+}
+
+void TIMERS::load(unsigned timer, unsigned value)
+{
+    timer_load[8 * timer] = value;
+}
+
+unsigned TIMERS::read(unsigned timer)
+{
+    return timer_value[8 * timer];
+}

+ 8 - 8
src/main.cpp

@@ -1,7 +1,7 @@
 #include <cstdio>
 #include <cstdarg>
 #include <os.h>
-#include "timers.h"
+#include "Timers.h"
 #include "Graphics.h"
 #include "Pixel.h"
 #include "Map.h"
@@ -29,8 +29,8 @@ void print_debug_map_data(const Map &map)
 void map_loop(unsigned x, unsigned y, Map &map)
 {
     // Free-running, no interrupts, divider = 1, 32 bit, wrapping
-    timer_mode(0, 0b0000010);
-    timer_load(0, 0);
+    WalrusRPG::Timers::mode(0, true, false, false, 1, true);
+    WalrusRPG::Timers::load(0, 0);
     unsigned loop_time = 546; // 32768Hz/60ups
     unsigned loop_next = -loop_time;
     unsigned last_frame = 0;
@@ -50,7 +50,7 @@ void map_loop(unsigned x, unsigned y, Map &map)
         camera.update(0);
 
         // Frameskip
-        if (timer_read(0) > loop_next)
+        if (WalrusRPG::Timers::read(0) > loop_next)
         {
             Pixel pix(Green);
             // TODO?: Preset color macros/consts?
@@ -61,14 +61,14 @@ void map_loop(unsigned x, unsigned y, Map &map)
 
             print_debug_camera_data(camera);
             print_debug_map_data(map);
-            unsigned frame_stamp = timer_read(0);
+            unsigned frame_stamp = WalrusRPG::Timers::read(0);
             print_format(0, 240 - 8, "%u fps", 32768 / (last_frame - frame_stamp));
             last_frame = frame_stamp;
             buffer_swap_render();
         }
 
         // Frame limiting
-        while (timer_read(0) > loop_next)
+        while (WalrusRPG::Timers::read(0) > loop_next)
             ;
         loop_next -= loop_time;
     }
@@ -80,7 +80,7 @@ int main(int argc, char *argv[])
     UNUSED(argv);
 
     buffer_allocate();
-    timer_init(0);
+    WalrusRPG::Timers::init(0);
     WalrusRPG::Interrupts::init();
 
     unsigned dungeonTest[] = {
@@ -144,7 +144,7 @@ int main(int argc, char *argv[])
     map_loop(0, 0, map);
 
     WalrusRPG::Interrupts::off();
-    timer_restore(0);
+    WalrusRPG::Timers::restore(0);
     buffer_free();
     return 0;
 }

+ 0 - 46
src/timers.c

@@ -1,46 +0,0 @@
-#include <os.h>
-#include <timers.h>
-
-#define TIMER 0x900D0000
-unsigned timer_ctl_bkp[2], timer_load_bkp[2];
-
-void timer_init(unsigned timer)
-{
-    volatile unsigned *timer_ctl = (unsigned *) (TIMER + 0x08 + 0x20 * timer);
-    volatile unsigned *timer_load = (unsigned *) (TIMER + 0x20 * timer);
-
-    timer_ctl_bkp[timer] = *timer_ctl;
-    timer_load_bkp[timer] = *timer_load;
-}
-
-void timer_restore(unsigned timer)
-{
-    volatile unsigned *timer_ctl = (unsigned *) (TIMER + 0x08 + 0x20 * timer);
-    volatile unsigned *timer_load = (unsigned *) (TIMER + 0x20 * timer);
-
-    *timer_ctl &= ~(1 << 7);
-    *timer_ctl = timer_ctl_bkp[timer] & ~(1 << 7);
-    *timer_load = timer_load_bkp[timer];
-    *timer_ctl = timer_ctl_bkp[timer];
-}
-
-void timer_mode(unsigned timer, unsigned mode)
-{
-    volatile unsigned *timer_ctl = (unsigned *) (TIMER + 0x08 + 0x20 * timer);
-
-    *timer_ctl &= ~(1 << 7);
-    *timer_ctl = mode;
-    *timer_ctl |= (1 << 7);
-}
-
-void timer_load(unsigned timer, unsigned value)
-{
-    volatile unsigned *timer_load = (unsigned *) (TIMER + 0x20 * timer);
-    *timer_load = value;
-}
-
-unsigned timer_read(unsigned timer)
-{
-    volatile unsigned *timer_value = (unsigned *) (TIMER + 0x04 + 0x20 * timer);
-    return *timer_value;
-}