From 9ada072d5337a582e987ed57dc4816189208dea7 Mon Sep 17 00:00:00 2001
From: root <root@no.no.james.local>
Date: Tue, 26 Feb 2019 15:38:49 +0000
Subject: fix bcd encoder

---
 main.c       |  4 ++++
 msf.c        | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 prototypes.h |  2 ++
 util.c       | 34 +++++++++++++++++++++++++++++++++-
 4 files changed, 91 insertions(+), 1 deletion(-)

diff --git a/main.c b/main.c
index 5b18a72..576b42f 100644
--- a/main.c
+++ b/main.c
@@ -16,13 +16,17 @@ int main (int argc, char *argv[])
 
   audio_init();
 
+
   audio_start();
   sync_to_minute (&tv);
+  //  sync_to_second (&tv);
 
   e.s = tv.tv_sec;
   e.ns = tv.tv_usec * 1000;
 
 
+  e.s -= 3600;
+
   for (;;) {
     e.s += 60;
     u = time_epoch_to_utc (e);
diff --git a/msf.c b/msf.c
index 411b601..38e0896 100644
--- a/msf.c
+++ b/msf.c
@@ -1,6 +1,28 @@
 #include "project.h"
 
 
+static int check_min_ident (uint8_t *i)
+{
+  if (i[0]) return 1;
+
+  if (!i[1]) return 1;
+
+  if (!i[2]) return 1;
+
+  if (!i[3]) return 1;
+
+  if (!i[4]) return 1;
+
+  if (!i[5]) return 1;
+
+  if (!i[6]) return 1;
+
+  if (i[7]) return 1;
+
+  return 0;
+}
+
+
 
 
 static void msf_set_ident (uint8_t *i)
@@ -51,6 +73,35 @@ static void msf_make_bits (UTC u, uint8_t *a, uint8_t *b)
 
 }
 
+static void process_bits (uint8_t *bitsa, uint8_t *bitsb)
+{
+  UTC u;
+
+
+  if (check_min_ident (&bitsa[52])) return;
+
+  if (check_parity (bitsa, 17, 24, bitsb[54])) return;
+
+  if (check_parity (bitsa, 25, 35, bitsb[55])) return;
+
+  if (check_parity (bitsa, 36, 38, bitsb[56])) return;
+
+  if (check_parity (bitsa, 39, 51, bitsb[57])) return;
+
+  u.jday = 0;
+  u.year = bcd (bitsa, 17, 24);
+  u.month = bcd (bitsa, 25, 29);
+  u.mday = bcd (bitsa, 30, 35);
+  u.hour = bcd (bitsa, 39, 44);
+  u.minute = bcd (bitsa, 45, 51);
+  u.second = 0;
+  u.nanosecond = 0;
+
+
+
+  printf ("MSF: decoded %02d-%02d-%02d %02d:%02d\r\n", u.year, u.month, u.mday, u.hour, u.minute);
+}
+
 
 
 void msf_make_stream (uint8_t *v, UTC u)
@@ -60,6 +111,7 @@ void msf_make_stream (uint8_t *v, UTC u)
   uint8_t *pa, *pb;
 
   msf_make_bits (u, a, b);
+  process_bits (a, b);
 
   bzero (v, 600);
 
diff --git a/prototypes.h b/prototypes.h
index e6c95a4..ef0a6b3 100644
--- a/prototypes.h
+++ b/prototypes.h
@@ -9,6 +9,8 @@ extern void msf_make_stream(uint8_t *v, UTC u);
 /* util.c */
 extern int set_parity(uint8_t *d, unsigned s, unsigned e);
 extern void bcd_set(uint8_t *d, unsigned s, unsigned e, unsigned v);
+extern int check_parity(uint8_t *d, unsigned s, unsigned e, uint8_t p);
+extern unsigned bcd(uint8_t *d, unsigned s, unsigned e);
 /* main.c */
 extern int main(int argc, char *argv[]);
 /* signal.c */
diff --git a/util.c b/util.c
index 8be15a4..d8632c5 100644
--- a/util.c
+++ b/util.c
@@ -27,7 +27,6 @@ void  bcd_set (uint8_t *d, unsigned s, unsigned e, unsigned v)
       c <<= 1;
     else {
       c >>= 3;
-      c *= 10;
       v = v / 10;
       w = v % 10;
     }
@@ -35,4 +34,37 @@ void  bcd_set (uint8_t *d, unsigned s, unsigned e, unsigned v)
 }
 
 
+int check_parity (uint8_t *d, unsigned s, unsigned e, uint8_t p)
+{
+  unsigned i;
+
+  for (i = s; i <= e; ++i)
+    p ^= d[i];
+
+  return !p;
+}
+
+
+unsigned bcd (uint8_t *d, unsigned s, unsigned e)
+{
+  unsigned ret = 0, c, i;
+
+  for (i = e, c = 1 ; i >= s; --i) {
+
+    if (d[i]) ret += c;
+
+
+    if (c & 0x77777777)
+      c <<= 1;
+    else {
+      c >>= 3;
+      c *= 10;
+    }
+  }
+
+  return ret;
+}
+
+
+
 
-- 
cgit v1.2.3