Index: proj/src/data/scoreboard.txt
===================================================================
--- proj/src/data/scoreboard.txt	(revision 379)
+++ proj/src/data/scoreboard.txt	(revision 380)
@@ -1,3 +1,3 @@
-06/01/20 18:01:51 - 8 - 45
-06/01/20 15:01:27 - 6 - 38
-06/01/20 19:01:08 - 5 - 25
+06/01/20 20:43:27 - 1 - 2
+06/01/20 20:01:58 - 0 - 15
+06/01/20 34:01:31 - 0 - 11
Index: proj/src/libs/rtc/include/rtc.h
===================================================================
--- proj/src/libs/rtc/include/rtc.h	(revision 379)
+++ proj/src/libs/rtc/include/rtc.h	(revision 380)
@@ -11,6 +11,10 @@
 
 #include <stdint.h>
 
+// RTC IRQ Line
+
+#define RTC_IRQ   8  /* @brief RTC IRQ Line */
+
 /**
  * @brief Subscribes RTC Interrupts
  * @param interrupt_bit Bit of Interrupt Vector that will be set when RTC Interrupt is pending
@@ -38,6 +42,65 @@
 int (rtc_write_register)(uint32_t reg, uint8_t data);
 
 /**
+ * @brief Reads seconds register.
+ *
+ * @param sec Address to store value
+ * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
+ * @see {_ERRORS_H_::errors}
+ */
+int rtc_read_sec(uint8_t *sec);
+
+/**
+ * @brief Reads minutes register.
+ *
+ * @param min Address to store value
+ * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
+ * @see {_ERRORS_H_::errors}
+ */
+int rtc_read_min(uint8_t *min);
+
+/**
+ * @brief Reads hours register.
+ *
+ * @param hour Address to store value
+ * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
+ * @see {_ERRORS_H_::errors}
+ */
+int rtc_read_hour(uint8_t *hour);
+/**
+ * @brief Reads weekday register.
+ *
+ * @param hour Address to store value
+ * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
+ * @see {_ERRORS_H_::errors}
+ */
+int (rtc_read_weekday)(uint8_t *weekday);
+/**
+ * @brief Reads day register.
+ *
+ * @param hour Address to store value
+ * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
+ * @see {_ERRORS_H_::errors}
+ */
+int (rtc_read_day)(uint8_t *day);
+/**
+ * @brief Reads month register.
+ *
+ * @param hour Address to store value
+ * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
+ * @see {_ERRORS_H_::errors}
+ */
+int (rtc_read_month)(uint8_t *month);
+/**
+ * @brief Reads year register.
+ *
+ * @param hour Address to store value
+ * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
+ * @see {_ERRORS_H_::errors}
+ */
+int (rtc_read_year)(uint8_t *year);
+
+/**
  * @brief Checks if there's an update in progress.
  * @return  The value of the flag UIP, or ERROR_CODE if error occurs.
  */
@@ -44,11 +107,11 @@
 int (rtc_check_update)(void);
 
 /**
- * @brief Enables/Disables updates of time/date registers.
+ * @brief Enables/Disables interrupts on update.
  * @param   on  zero to disable, any other value to enable
  * @return  ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
  */
-int (rtc_set_updates)(int on);
+int (rtc_set_updates_int)(int on);
 
 /**
  * @brief Reads time from RTC.
@@ -63,6 +126,8 @@
  * @return  ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
  */
 int (rtc_read_date)(uint8_t *date);
+/// @brief RTC Interrupt Handler
+void rtc_ih();
 
 /**
  * @}
Index: proj/src/libs/rtc/src/rtc.c
===================================================================
--- proj/src/libs/rtc/src/rtc.c	(revision 379)
+++ proj/src/libs/rtc/src/rtc.c	(revision 380)
@@ -5,10 +5,6 @@
 #include "utils.h"
 #include "errors.h"
 
-// RTC IRQ Line
-
-#define KBC_IRQ   8  /* @brief KBC IRQ Line */
-
 // RTC Ports
 
 #define RTC_ADDR_REG        0x70
@@ -61,10 +57,12 @@
 
 #define VRT     BIT(7)  /** @brief Valid RAM/time - If set to 0 RTC reading aren't valid */
 
+static uint8_t s = 0, m = 0, h = 0;
+
 int (subscribe_rtc_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
     if (interrupt_id == NULL) return NULL_PTR;
     *interrupt_id = interrupt_bit;
-    if (sys_irqsetpolicy(KBC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR;
+    if (sys_irqsetpolicy(RTC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR;
     return SUCCESS;
 }
 
@@ -93,22 +91,20 @@
     return (data & UIP) != 0;
 }
 
-int (rtc_set_updates)(int on) {
+int (rtc_set_updates_int)(int on) {
     uint8_t data;
     int r;
     if (on) {
         if ((r = rtc_read_register(RTC_REG_B, &data))) return r;
 
-        data &= ~SET;
+        data |= UIE;
 
         if ((r = rtc_write_register(RTC_REG_B, data))) return r;
 
     } else {
-        while (rtc_check_update());
-
         if ((r = rtc_read_register(RTC_REG_B, &data))) return r;
 
-        data |= SET;
+        data &= ~UIE;
 
         if ((r = rtc_write_register(RTC_REG_B, data))) return r;
     }
@@ -115,41 +111,62 @@
     return SUCCESS;
 }
 
-int (rtc_read_time)(uint8_t *time) {
+int (rtc_read_sec)(uint8_t *sec) {
+    return rtc_read_register(RTC_SEC, sec);
+}
 
-    int r;
-    //if ((r = rtc_set_updates(false))) return r;
-    while (rtc_check_update());
+int (rtc_read_min)(uint8_t *min) {
+    return rtc_read_register(RTC_MIN, min);
+}
 
-    uint8_t hour, min, sec;
+int (rtc_read_hour)(uint8_t *hour) {
+    return rtc_read_register(RTC_HOUR, hour);
+}
 
-    if ((r = rtc_read_register(RTC_SEC, &sec)))    return r;
-    if ((r = rtc_read_register(RTC_MIN, &min)))    return r;
-    if ((r = rtc_read_register(RTC_HOUR, &hour)))  return r;
+int (rtc_read_weekday)(uint8_t *weekday) {
+    return rtc_read_register(RTC_WEEK_DAY, weekday);
+}
 
-    //if ((r = rtc_set_updates(true))) return r;
+int (rtc_read_day)(uint8_t *day) {
+    return rtc_read_register(RTC_MONTH_DAY, day);
+}
 
-    time[0] = BCD_FIRST(sec)*10   + BCD_SECOND(sec);
-    time[1] = BCD_FIRST(min)*10   + BCD_SECOND(min);
-    time[2] = BCD_FIRST(hour)*10  + BCD_SECOND(hour);
+int (rtc_read_month)(uint8_t *month) {
+    return rtc_read_register(RTC_MONTH, month);
+}
 
+int (rtc_read_year)(uint8_t *year) {
+    return rtc_read_register(RTC_YEAR, year);
+}
+
+int (rtc_read_time)(uint8_t *time) {
+
+    time[0] = BCD_FIRST(s)*10   + BCD_SECOND(s);
+    time[1] = BCD_FIRST(m)*10   + BCD_SECOND(m);
+    time[2] = BCD_FIRST(h)*10  + BCD_SECOND(h);
+
     return SUCCESS;
 }
 
 int (rtc_read_date)(uint8_t *date) {
     int r;
-    //if ((r = rtc_set_updates(false))) return r;
-    while (rtc_check_update());
 
-    uint8_t year, month, day, weekday;
+    uint8_t year=0, month=0, day=0, weekday=0;
+    uint8_t year2=0, month2=0, day2=0, weekday2=0;
 
-    if ((r = rtc_read_register(RTC_WEEK_DAY,    &weekday)))     return r;
-    if ((r = rtc_read_register(RTC_MONTH_DAY,   &day)))         return r;
-    if ((r = rtc_read_register(RTC_MONTH,       &month)))       return r;
-    if ((r = rtc_read_register(RTC_YEAR,        &year)))        return r;
+    do {
+        year2 = year;
+        month2 = month;
+        day2 = day;
+        weekday2 = weekday;
 
-    //if ((r = rtc_set_updates(true))) return r;
+        if ((r = rtc_read_weekday(&weekday))) return r;
+        if ((r = rtc_read_day(&day))) return r;
+        if ((r = rtc_read_month(&month))) return r;
+        if ((r = rtc_read_year(&year))) return r;
 
+    } while (year != year2 || month != month2 || day != day2 || weekday != weekday2);
+
     date[0] = weekday;
     date[1] = BCD_FIRST(day)*10     + BCD_SECOND(day);
     date[2] = BCD_FIRST(month)*10   + BCD_SECOND(month);
@@ -157,3 +174,17 @@
 
     return SUCCESS;
 }
+
+void rtc_ih() {
+    uint8_t data;
+
+    if (rtc_read_register(RTC_REG_C, &data)) return;
+
+    if (data & UF) {
+        printf("REACH\n");
+        if (rtc_read_sec(&s)) return;
+        if (rtc_read_min(&m)) return;
+        if (rtc_read_hour(&h)) return;
+        printf("%x %x %x", h, m, s);
+    }
+}
Index: proj/src/project/src/interrupts_func.c
===================================================================
--- proj/src/project/src/interrupts_func.c	(revision 379)
+++ proj/src/project/src/interrupts_func.c	(revision 380)
@@ -7,6 +7,7 @@
 #include "hltp.h"
 #include "utils.h"
 #include "errors.h"
+#include "rtc.h"
 
 static int timer_subscribed = 0, timer_id;
 
@@ -16,6 +17,8 @@
 
 static int uart_subscribed = 0, uart_id;
 
+static int rtc_subscribed = 0, rtc_id;
+
 static void (*const ih[])(void) =   {    timer_int_handler,
                                          kbc_ih,
                                          NULL,
@@ -24,10 +27,10 @@
                                          NULL,
                                          NULL,
                                          NULL,
+                                         rtc_ih,
                                          NULL,
                                          NULL,
                                          NULL,
-                                         NULL,
                                          mouse_ih,
                                          NULL,
                                          NULL,
@@ -110,6 +113,17 @@
         return SBCR_ERROR;
     }
 
+    if (subscribe_rtc_interrupt(RTC_IRQ, &rtc_id)){
+        printf("%s: failed to enable rtc interrupts.\n", __func__);
+        if (unsubscribe_all())
+            printf("%s: failed to unsubcribe driver, unexpected behaviour is expected.\n", __func__);
+        return SBCR_ERROR;
+    }
+    rtc_subscribed = 1;
+    rtc_set_updates_int(1);
+
+
+
     /// UART interrupt handling
     uart_id = 0;
     uart_set_bits_per_character(COM1_ADDR, 8);
@@ -170,6 +184,14 @@
         mouse_subscribed = 0;
     }
 
+    if (rtc_subscribed) {
+        if (unsubscribe_interrupt(&rtc_id)) {
+            printf("%s: failed to unsubcribe RTC interrupts.\n", __func__);
+            r = UNSBCR_ERROR;
+        }
+        rtc_subscribed = 0;
+    }
+
     // Unsubscribe UART interrupts
     if (uart_subscribed) {
         if (unsubscribe_interrupt(&uart_id)) {
Index: proj/src/project/src/scoreboards.c
===================================================================
--- proj/src/project/src/scoreboards.c	(revision 379)
+++ proj/src/project/src/scoreboards.c	(revision 380)
@@ -184,7 +184,7 @@
         score_info_t *score = p->highscores[i];
         sprintf(buffer, "%02d/%02d/%02d %02d:%02d:%02d",
                     (score->day), (score->month), (score->year),
-                    (score->hour), (score->month), (score->sec));
+                    (score->hour), (score->min), (score->sec));
 
 
         text_set_string(p->t[MAX_HIGHSCORES + i*3], buffer);
@@ -254,7 +254,7 @@
         score_info_t *score = p->highscores[i];
         fprintf(f, "%02d/%02d/%02d %02d:%02d:%02d - %d - %d\n",
                     (score->day), (score->month), (score->year),
-                    (score->hour), (score->month), (score->sec),
+                    (score->hour), (score->min), (score->sec),
                     (score->score), (score->time_played));
     }
     fclose(f);
