Index: lab2/utils.h
===================================================================
--- lab2/utils.h	(revision 195)
+++ lab2/utils.h	(nonexistent)
@@ -1,14 +0,0 @@
-#ifndef UTILS_H_INCLUDED
-#define UTILS_H_INCLUDED
-
-int(util_get_LSB)(uint16_t val, uint8_t *lsb);
-
-int(util_get_MSB)(uint16_t val, uint8_t *msb);
-
-#ifdef LAB3
-    uint32_t sys_inb_counter;
-#endif
-
-int (util_sys_inb)(int port, uint8_t *value);
-
-#endif //UTILS_H_INCLUDED

Property changes on: lab2/utils.h
___________________________________________________________________
Deleted: svn:executable
## -1 +0,0 ##
-*
\ No newline at end of property
Index: lab2/i8254.h
===================================================================
--- lab2/i8254.h	(revision 195)
+++ lab2/i8254.h	(nonexistent)
@@ -1,65 +0,0 @@
-#ifndef _LCOM_I8254_H_
-#define _LCOM_I8254_H_
-
-#include <lcom/lcf.h>
-
-/** @defgroup i8254 i8254
- * @{
- *
- * Constants for programming the i8254 Timer. Needs to be completed.
- */
-
-#define TIMER_FREQ     1193182 /**< @brief clock frequency for timer in PC and AT */
-#define TIMER_MIN_FREQ (TIMER_FREQ/UINT16_MAX) + ((TIMER_FREQ % UINT16_MAX) ? 1 : 0) /**< @brief mininum frequency for timer */
-#define TIMER0_IRQ     0 /**< @brief Timer 0 IRQ line */
-
-/* I/O port addresses */
-
-#define TIMER_0    0x40 /**< @brief Timer 0 count register */
-#define TIMER_1    0x41 /**< @brief Timer 1 count register */
-#define TIMER_2    0x42 /**< @brief Timer 2 count register */
-#define TIMER_CTRL 0x43 /**< @brief Control register */
-
-#define SPEAKER_CTRL 0x61 /**< @brief Register for speaker control  */
-
-/* Timer control */
-
-/* Timer selection: bits 7 and 6 */
-
-#define TIMER_SEL0   0x00              /**< @brief Control Word for Timer 0 */
-#define TIMER_SEL1   BIT(6)            /**< @brief Control Word for Timer 1 */
-#define TIMER_SEL2   BIT(7)            /**< @brief Control Word for Timer 2 */
-#define TIMER_RB_CMD (BIT(7) | BIT(6)) /**< @brief Read Back Command */
-
-/* Register selection: bits 5 and 4 */
-
-#define TIMER_LSB     BIT(4)                  /**< @brief Initialize Counter LSB only */
-#define TIMER_MSB     BIT(5)                  /**< @brief Initialize Counter MSB only */
-#define TIMER_LSB_MSB (TIMER_LSB | TIMER_MSB) /**< @brief Initialize LSB first and MSB afterwards */
-#define TIMER_INMODE_MASK 0x30      //0011 0000
-#define TIMER_INMODE_POS  4
-
-/* Operating mode: bits 3, 2 and 1 */
-
-#define TIMER_SQR_WAVE (BIT(2) | BIT(1)) /**< @brief Mode 3: square wave generator */
-#define TIMER_RATE_GEN BIT(2)            /**< @brief Mode 2: rate generator */
-#define TIMER_MODE_MASK 0x0E             /**< @brief Mask for mode */
-#define TIMER_MODE_POS  1                /**< @brief Position of smallest bit from mode */
-#define TIMER_MODE_2ALT 0x6              /**< @brief Alternative notation for mode 2 */
-#define TIMER_MODE_3ALT 0x7              /**< @brief Alternative notation for mode 3 */
-#define TIMER_MODE_RED2 0x03             /**< @brief Reduce 3-bit mode to 2-bit mode */
-
-/* Counting mode: bit 0 */
-
-#define TIMER_BCD 0x01 /**< @brief Count in BCD */
-#define TIMER_BIN 0x00 /**< @brief Count in binary */
-
-/* READ-BACK COMMAND FORMAT */
-
-#define TIMER_RB_COUNT_  BIT(5)
-#define TIMER_RB_STATUS_ BIT(4)
-#define TIMER_RB_SEL(n)  BIT((n) + 1)
-
-/**@}*/
-
-#endif /* _LCOM_I8254_H */

Property changes on: lab2/i8254.h
___________________________________________________________________
Deleted: svn:executable
Index: lab2/timer.c
===================================================================
--- lab2/timer.c	(revision 195)
+++ lab2/timer.c	(nonexistent)
@@ -1,126 +0,0 @@
-#include <lcom/lcf.h>
-#include <lcom/timer.h>
-
-#include <stdint.h>
-
-#include "i8254.h"
-
-int (timer_set_frequency)(uint8_t timer, uint32_t freq) {
-
-    // Frequencies out this range are not supported (by limitation of hardware)
-    if (freq > TIMER_FREQ || freq < TIMER_MIN_FREQ) {
-        printf("%s: Frequency out of range, must be between %d and %d.\n", __func__, TIMER_MIN_FREQ, TIMER_FREQ);
-        return 1;
-    }
-
-    uint8_t status = 0;
-    if (timer_get_conf(timer, &status)) return 1;
-    //Make command
-    uint8_t write_cmd = 0;
-    //Select timer
-    switch(timer) {
-        case 0: write_cmd |= TIMER_SEL0; break;
-        case 1: write_cmd |= TIMER_SEL1; break;
-        case 2: write_cmd |= TIMER_SEL2; break;
-        default: return 1;
-    }
-    //Change both LSB and MSB
-    write_cmd |= TIMER_LSB_MSB;
-    //Keep 4 least significant bits
-    write_cmd |= (status & (TIMER_MODE_MASK | TIMER_BCD));
-    //Write cmd
-    if (sys_outb(TIMER_CTRL, write_cmd)) return 1;
-
-    // counter_init = clock/freq
-    uint16_t counter_init = (uint16_t)(TIMER_FREQ / freq);
-
-    int timer_port = 0;
-    switch(timer) {
-        case 0: timer_port = TIMER_0; break;
-        case 1: timer_port = TIMER_1; break;
-        case 2: timer_port = TIMER_2; break;
-        default: return 1;
-    }
-    uint8_t lsb = 0, msb = 0;
-    /* Split the 16 bits word in two bytes */
-    if (util_get_LSB(counter_init, &lsb)) return 1;
-    if (util_get_MSB(counter_init, &msb)) return 1;
-
-    /* Write the 8 LSB of the counter */
-    if (sys_outb(timer_port, lsb)) return 1;
-    /* Write the 8 MSB of the counter */
-    if (sys_outb(timer_port, msb)) return 1;
-
-    return 0;
-}
-
-int hook_id;
-
-int (timer_subscribe_int)(uint8_t *bit_no) {
-    if(bit_no == NULL) return 1;
-    hook_id = 2;
-    *bit_no = hook_id;
-    /* Subscribe Timer 0 Interrupts */
-    return sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE, &hook_id);
-}
-
-int (timer_unsubscribe_int)() {
-    /* Unsubscribe Timer 0 Interrupts */
-    if(sys_irqrmpolicy(&hook_id)) return 1;
-    return 0;
-}
-
-int no_interrupts = 0;
-void (timer_int_handler)() {
-    no_interrupts++;
-}
-
-int (timer_get_conf)(uint8_t timer, uint8_t *st) {
-    if(st == NULL) return 1;
-    // Write read-back command to TIMER_CTRL
-    u32_t cmd = TIMER_RB_CMD | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer);
-    if(sys_outb(TIMER_CTRL, cmd)) return 1;
-    int read_port;
-    switch(timer) {
-        case 0: read_port = TIMER_0; break;
-        case 1: read_port = TIMER_1; break;
-        case 2: read_port = TIMER_2; break;
-        default: return 1;
-    }
-    if(util_sys_inb(read_port, st)) return 1;
-    return 0;
-}
-
-int (timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field) {
-    union timer_status_field_val conf;
-    uint8_t in_mode;
-    switch(field){
-        case tsf_all:
-            conf.byte = st; /* Full Status Byte */
-            break;
-        case tsf_initial:
-            /* Counter Initial Value Loading Mode */
-            in_mode = (st & TIMER_INMODE_MASK) >> TIMER_INMODE_POS;
-            switch(in_mode){
-                case 0: conf.in_mode = INVAL_val    ; break; //000
-                case 1: conf.in_mode = LSB_only     ; break; //001
-                case 2: conf.in_mode = MSB_only     ; break; //010
-                case 3: conf.in_mode = MSB_after_LSB; break; //011
-                default: return 1;
-            }
-            break;
-        case tsf_mode:
-            /* Counting Mode */
-            conf.count_mode = (st & TIMER_MODE_MASK)>>TIMER_MODE_POS;
-            if(conf.count_mode == TIMER_MODE_2ALT || conf.count_mode == TIMER_MODE_3ALT)
-                conf.count_mode &= TIMER_MODE_RED2;
-            break;
-        case tsf_base:
-            /* Representation of Counter Initial Value */
-            conf.bcd = st & TIMER_BCD;
-            break;
-        default: return 1;
-    }
-    if(timer_print_config(timer, field, conf)) return 1;
-    return 0;
-}

Property changes on: lab2/timer.c
___________________________________________________________________
Deleted: svn:executable
Index: lab2/utils.c
===================================================================
--- lab2/utils.c	(revision 195)
+++ lab2/utils.c	(nonexistent)
@@ -1,30 +0,0 @@
-#include <lcom/lcf.h>
-
-#include <stdint.h>
-
-int(util_get_LSB)(uint16_t val, uint8_t *lsb) {
-    if (lsb == NULL) return 1;
-    *lsb = val;
-    return 0;
-}
-
-int(util_get_MSB)(uint16_t val, uint8_t *msb) {
-    if (msb == NULL) return 1;
-    *msb = (val >> 8);
-    return 0;
-}
-
-#ifdef LAB3
-    uint32_t sys_inb_counter = 0;
-#endif
-
-int (util_sys_inb)(int port, uint8_t *value) {
-    if(value == NULL) return 1;
-    uint32_t n = 0;
-    if(sys_inb(port, &n)) return 1;
-    *value = n;
-    #ifdef LAB3
-        ++sys_inb_counter;
-    #endif
-    return 0;
-}

Property changes on: lab2/utils.c
___________________________________________________________________
Deleted: svn:executable
Index: lab2/lab2.c
===================================================================
--- lab2/lab2.c	(revision 195)
+++ lab2/lab2.c	(nonexistent)
@@ -1,79 +0,0 @@
-#include <lcom/lcf.h>
-#include <lcom/lab2.h>
-
-#include <stdbool.h>
-#include <stdint.h>
-
-int main(int argc, char *argv[]) {
-  // sets the language of LCF messages (can be either EN-US or PT-PT)
-  lcf_set_language("EN-US");
-
-  // enables to log function invocations that are being "wrapped" by LCF
-  // [comment this out if you don't want/need it]
-  lcf_trace_calls("/home/lcom/labs/lab2/trace.txt");
-
-  // enables to save the output of printf function calls on a file
-  // [comment this out if you don't want/need it]
-  lcf_log_output("/home/lcom/labs/lab2/output.txt");
-
-  // handles control over to LCF
-  // [LCF handles command line arguments and invokes the right function]
-  if (lcf_start(argc, argv))
-    return 1;
-
-  // LCF clean up tasks
-  // [must be the last statement before return]
-  lcf_cleanup();
-
-  return 0;
-}
-
-int(timer_test_read_config)(uint8_t timer, enum timer_status_field field){
-    uint8_t state = 0;
-    if(timer_get_conf(timer, &state)) return 1;
-    if(timer_display_conf(timer, state, field)) return 1;
-    return 0;
-}
-
-int(timer_test_time_base)(uint8_t timer, uint32_t freq) {
-    if (timer_set_frequency(timer, freq)) return 1;
-    return 0;
-}
-
-extern int no_interrupts;
-
-int(timer_test_int)(uint8_t time) {
-    const int frequency = 60; // Frequency asummed at 60Hz
-    int ipc_status, r;
-    message msg;
-    uint8_t timer_id = 0;
-    no_interrupts = 0;
-    if (timer_subscribe_int(&timer_id)) return 1;
-    int irq_set = BIT(timer_id);
-    while (time) {
-        /* Get a request message. */
-        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
-            printf("driver_receive failed with %d", r);
-            continue;
-        }
-        if (is_ipc_notify(ipc_status)) { /* received notification */
-            switch (_ENDPOINT_P(msg.m_source)) {
-                case HARDWARE: /* hardware interrupt notification */
-                    if (msg.m_notify.interrupts & irq_set) { /* subscribed interrupt */
-                        timer_int_handler();
-                        if (!(no_interrupts % frequency)) { /* second elapsed */
-                            timer_print_elapsed_time();
-                            time--;
-                        }
-                    }
-                    break;
-                default:
-                    break; /* no other notifications expected: do nothing */
-            }
-        } else { /* received standart message, not a notification */
-            /* no standart message expected: do nothing */
-        }
-    }
-    if (timer_unsubscribe_int()) return 1;
-    return 0;
-}

Property changes on: lab2/lab2.c
___________________________________________________________________
Deleted: svn:executable
Index: lab2/include/errors.h
===================================================================
--- lab2/include/errors.h	(nonexistent)
+++ lab2/include/errors.h	(revision 196)
@@ -0,0 +1,23 @@
+#ifndef ERRORS_H_INCLUDED
+#define ERRORS_H_INCLUDED
+
+/** @brief Error Codes */
+enum errors {
+    SUCCESS = 0,        /** @brief Sucessful */
+    NULL_PTR,           /** @brief Null Pointer Error */
+    LCF_ERROR,          /** @brief Error originated on LCF */
+    SBCR_ERROR,         /** @brief Error on Subscribing Interrupt */
+    UNSBCR_ERROR,       /** @brief Error on Unsubscring Interrupt*/
+    READ_ERROR,         /** @brief Error on Reading from Port */
+    WRITE_ERROR,        /** @brief Error on Writing to Port */
+    TIMEOUT_ERROR,      /** @brief Timeout error */
+    INVALID_COMMAND,    /** @brief Invalid Command issued */
+    INVALID_STATE,      /** @brief State machine reached an invalid state */
+    BIOS_CALL_ERROR,    /** @brief Error upon BIOS call */
+    OUT_OF_RANGE,       /** @brief Accessing area out of range of memory */
+    ALLOC_ERROR,        /** @brief Memory allocation error */
+    LOGIC_ERROR,        /** @brief Logic error */
+    OTHER_ERROR         /** @brief Unspecified error */
+};
+
+#endif //ERRORS_H_INCLUDED

Property changes on: lab2/include/errors.h
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab2/include/i8254.h
===================================================================
--- lab2/include/i8254.h	(nonexistent)
+++ lab2/include/i8254.h	(revision 196)
@@ -0,0 +1,62 @@
+#ifndef _LCOM_I8254_H_
+#define _LCOM_I8254_H_
+
+/** @defgroup i8254 i8254
+ *
+ * Constants for programming the i8254 Timer. Needs to be completed.
+ */
+
+#define TIMER_FREQ     1193182                                                          /**< @brief clock frequency for timer in PC and AT */
+#define TIMER_MIN_FREQ (TIMER_FREQ/UINT16_MAX) + ((TIMER_FREQ % UINT16_MAX) ? 1 : 0)    /**< @brief mininum frequency for timer */
+#define TIMER0_IRQ     0                                                                /**< @brief Timer 0 IRQ line */
+
+/* I/O port addresses */
+
+#define TIMER_0    0x40         /**< @brief Timer 0 count register */
+#define TIMER_1    0x41         /**< @brief Timer 1 count register */
+#define TIMER_2    0x42         /**< @brief Timer 2 count register */
+#define TIMER_CTRL 0x43         /**< @brief Control register */
+
+#define SPEAKER_CTRL 0x61       /**< @brief Register for speaker control  */
+
+/* Timer control */
+
+/* Timer selection: bits 7 and 6 */
+
+#define TIMER_SEL0   0x00              /**< @brief Control Word for Timer 0 */
+#define TIMER_SEL1   BIT(6)            /**< @brief Control Word for Timer 1 */
+#define TIMER_SEL2   BIT(7)            /**< @brief Control Word for Timer 2 */
+#define TIMER_RB_CMD (BIT(7) | BIT(6)) /**< @brief Read Back Command */
+
+/* Register selection: bits 5 and 4 */
+
+#define TIMER_LSB     BIT(4)                    /**< @brief Initialize Counter LSB only */
+#define TIMER_MSB     BIT(5)                    /**< @brief Initialize Counter MSB only */
+#define TIMER_LSB_MSB (TIMER_LSB | TIMER_MSB)   /**< @brief Initialize LSB first and MSB afterwards */
+#define TIMER_INMODE_MASK 0x30                  /**< @brief Mask for Timer Counter Mode */
+#define TIMER_INMODE_POS  4                     /**< @brief Bit position of Timer Counter Mode */
+
+/* Operating mode: bits 3, 2 and 1 */
+
+#define TIMER_SQR_WAVE (BIT(2) | BIT(1)) /**< @brief Mode 3: square wave generator */
+#define TIMER_RATE_GEN BIT(2)            /**< @brief Mode 2: rate generator */
+#define TIMER_MODE_MASK 0x0E             /**< @brief Mask for mode */
+#define TIMER_MODE_POS  1                /**< @brief Position of smallest bit from mode */
+#define TIMER_MODE_2ALT 0x6              /**< @brief Alternative notation for mode 2 */
+#define TIMER_MODE_3ALT 0x7              /**< @brief Alternative notation for mode 3 */
+#define TIMER_MODE_RED2 0x03             /**< @brief Reduce 3-bit mode to 2-bit mode */
+
+/* Counting mode: bit 0 */
+
+#define TIMER_BCD 0x01 /**< @brief Count in BCD */
+#define TIMER_BIN 0x00 /**< @brief Count in binary */
+
+/* READ-BACK COMMAND FORMAT */
+
+#define TIMER_RB_COUNT_  BIT(5)         /**< @brief Read counter value on read-back (0 to activate) */
+#define TIMER_RB_STATUS_ BIT(4)         /**< @brief Read status value on read-back (0 to activate) */
+#define TIMER_RB_SEL(n)  BIT((n) + 1)   /**< @brief Select timer to read information from */
+
+/**@}*/
+
+#endif /* _LCOM_I8254_H */

Property changes on: lab2/include/i8254.h
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab2/include/timer.h
===================================================================
--- lab2/include/timer.h	(nonexistent)
+++ lab2/include/timer.h	(revision 196)
@@ -0,0 +1,18 @@
+/**
+ * This file concerns everything related to the timer
+ */
+
+#ifndef TIMER_H_INCLUDED
+#define TIMER_H_INCLUDED
+
+//7.1 timer_test_read_config()
+int (timer_get_conf)(uint8_t timer, uint8_t *st);
+int (timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field);
+//7.2 timer_test_time_base()
+int (timer_set_frequency)(uint8_t timer, uint32_t freq);
+//7.3 timer_test_int()
+void (timer_int_handler)(void);
+int (subscribe_timer_interrupt)(uint8_t interrupt_bit, int *interrupt_id);
+uint32_t no_interrupts;
+
+#endif //TIMER_H_INCLUDED

Property changes on: lab2/include/timer.h
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab2/include/utils.h
===================================================================
--- lab2/include/utils.h	(nonexistent)
+++ lab2/include/utils.h	(revision 196)
@@ -0,0 +1,53 @@
+#ifndef UTILS_H_INCLUDED
+#define UTILS_H_INCLUDED
+
+/**
+ * @brief Gets the least significant byte of a 16-bit variable
+ * @param val 16-bit variable
+ * @param lsb Pointer to a 8-bit variable to store the value of the LSB
+ * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
+ */
+int(util_get_LSB)(uint16_t val, uint8_t *lsb);
+
+/**
+ * @brief Gets the most significant byte of a 16-bit variable
+ * @param val 16-bit variable
+ * @param lsb Pointer to a 8-bit variable to store the value of the MSB
+ * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
+ */
+int(util_get_MSB)(uint16_t val, uint8_t *msb);
+
+/**
+ * @brief sys_inb wrapper
+ * @param port Port to read from
+ * @param value Pointer to byte to store value read
+ * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
+ */
+int (util_sys_inb)(int port, uint8_t *value);
+
+/**
+ * @brief Unsubcribes Interrupts
+ * @param interrupt_id Interrupt ID, value via arguments on subscription of the interrupt_id
+ * @see subscribe_kbc_interrupt, subscribe_timer_interrupt
+ * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
+ */
+int (unsubscribe_interrupt)(int *interrupt_id);
+
+/**
+ * @brief Gets the minimum value out of two values.
+ * @param a     First value
+ * @param b     Second value
+ * @return  The minimum of the two values
+ */
+int32_t min(int32_t a, int32_t b);
+
+/**
+ * @brief Gets the maximum value out of two values.
+ * @param a     First value
+ * @param b     Second value
+ * @return  The maximum of the two values
+ */
+int32_t max(int32_t a, int32_t b);
+
+
+#endif //UTILS_H_INCLUDED

Property changes on: lab2/include/utils.h
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab2/src/lab2.c
===================================================================
--- lab2/src/lab2.c	(nonexistent)
+++ lab2/src/lab2.c	(revision 196)
@@ -0,0 +1,79 @@
+#include <lcom/lcf.h>
+#include <lcom/lab2.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "timer.h"
+
+int main(int argc, char *argv[]) {
+  // sets the language of LCF messages (can be either EN-US or PT-PT)
+  lcf_set_language("EN-US");
+
+  // enables to log function invocations that are being "wrapped" by LCF
+  // [comment this out if you don't want/need it]
+  lcf_trace_calls("/home/lcom/labs/lab2/trace.txt");
+
+  // enables to save the output of printf function calls on a file
+  // [comment this out if you don't want/need it]
+  lcf_log_output("/home/lcom/labs/lab2/output.txt");
+
+  // handles control over to LCF
+  // [LCF handles command line arguments and invokes the right function]
+  if (lcf_start(argc, argv))
+    return 1;
+
+  // LCF clean up tasks
+  // [must be the last statement before return]
+  lcf_cleanup();
+
+  return 0;
+}
+
+int(timer_test_read_config)(uint8_t timer, enum timer_status_field field){
+    uint8_t state = 0;
+    if(timer_get_conf(timer, &state)) return 1;
+    if(timer_display_conf(timer, state, field)) return 1;
+    return 0;
+}
+
+int(timer_test_time_base)(uint8_t timer, uint32_t freq) {
+    if (timer_set_frequency(timer, freq)) return 1;
+    return 0;
+}
+
+int(timer_test_int)(uint8_t time) {
+    const int frequency = 60; // Frequency asummed at 60Hz
+    int ipc_status, r;
+    message msg;
+    uint8_t timer_id = 0;
+    no_interrupts = 0;
+    if (timer_subscribe_int(&timer_id)) return 1;
+    int irq_set = BIT(timer_id);
+    while (time) {
+        /* Get a request message. */
+        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
+            printf("driver_receive failed with %d", r);
+            continue;
+        }
+        if (is_ipc_notify(ipc_status)) { /* received notification */
+            switch (_ENDPOINT_P(msg.m_source)) {
+                case HARDWARE: /* hardware interrupt notification */
+                    if (msg.m_notify.interrupts & irq_set) { /* subscribed interrupt */
+                        timer_int_handler();
+                        if (!(no_interrupts % frequency)) { /* second elapsed */
+                            timer_print_elapsed_time();
+                            time--;
+                        }
+                    }
+                    break;
+                default:
+                    break; /* no other notifications expected: do nothing */
+            }
+        } else { /* received standart message, not a notification */
+            /* no standart message expected: do nothing */
+        }
+    }
+    if (timer_unsubscribe_int()) return 1;
+    return 0;
+}

Property changes on: lab2/src/lab2.c
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab2/src/timer.c
===================================================================
--- lab2/src/timer.c	(nonexistent)
+++ lab2/src/timer.c	(revision 196)
@@ -0,0 +1,128 @@
+#include <lcom/lcf.h>
+
+#include "timer.h"
+
+#include "i8254.h"
+#include "utils.h"
+#include "errors.h"
+
+//7.1 timer_test_read_config()
+int (timer_get_conf)(uint8_t timer, uint8_t *st) {
+    if(st == NULL) return 1;
+    // Write read-back command to TIMER_CTRL
+    u32_t cmd = TIMER_RB_CMD | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer);
+    if(sys_outb(TIMER_CTRL, cmd)) return 1;
+    int read_port;
+    switch(timer) {
+        case 0: read_port = TIMER_0; break;
+        case 1: read_port = TIMER_1; break;
+        case 2: read_port = TIMER_2; break;
+        default: return 1;
+    }
+    if(util_sys_inb(read_port, st)) return 1;
+    return 0;
+}
+int (timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field) {
+    union timer_status_field_val conf;
+    uint8_t in_mode;
+    switch(field){
+        case tsf_all:
+            conf.byte = st; /* Full Status Byte */
+            break;
+        case tsf_initial:
+            /* Counter Initial Value Loading Mode */
+            in_mode = (st & TIMER_INMODE_MASK) >> TIMER_INMODE_POS;
+            switch(in_mode){
+                case 0: conf.in_mode = INVAL_val    ; break; //000
+                case 1: conf.in_mode = LSB_only     ; break; //001
+                case 2: conf.in_mode = MSB_only     ; break; //010
+                case 3: conf.in_mode = MSB_after_LSB; break; //011
+                default: return 1;
+            }
+            break;
+        case tsf_mode:
+            /* Counting Mode */
+            conf.count_mode = (st & TIMER_MODE_MASK)>>TIMER_MODE_POS;
+            if(conf.count_mode == TIMER_MODE_2ALT || conf.count_mode == TIMER_MODE_3ALT)
+                conf.count_mode &= TIMER_MODE_RED2;
+            break;
+        case tsf_base:
+            /* Representation of Counter Initial Value */
+            conf.bcd = st & TIMER_BCD;
+            break;
+        default: return 1;
+    }
+    if(timer_print_config(timer, field, conf)) return 1;
+    return 0;
+}
+//7.2 timer_test_time_base()
+int (timer_set_frequency)(uint8_t timer, uint32_t freq) {
+
+    // Frequencies out this range are not supported (by limitation of hardware)
+    if (freq > TIMER_FREQ || freq < TIMER_MIN_FREQ) {
+        printf("%s: Frequency out of range, must be between %d and %d.\n", __func__, TIMER_MIN_FREQ, TIMER_FREQ);
+        return 1;
+    }
+
+    uint8_t status = 0;
+    if (timer_get_conf(timer, &status)) return 1;
+    //Make command
+    uint8_t write_cmd = 0;
+    //Select timer
+    switch(timer) {
+        case 0: write_cmd |= TIMER_SEL0; break;
+        case 1: write_cmd |= TIMER_SEL1; break;
+        case 2: write_cmd |= TIMER_SEL2; break;
+        default: return 1;
+    }
+    //Change both LSB and MSB
+    write_cmd |= TIMER_LSB_MSB;
+    //Keep 4 least significant bits
+    write_cmd |= (status & (TIMER_MODE_MASK | TIMER_BCD));
+    //Write cmd
+    if (sys_outb(TIMER_CTRL, write_cmd)) return 1;
+
+    // counter_init = clock/freq
+    uint16_t counter_init = (uint16_t)(TIMER_FREQ / freq);
+
+    int timer_port = 0;
+    switch(timer) {
+        case 0: timer_port = TIMER_0; break;
+        case 1: timer_port = TIMER_1; break;
+        case 2: timer_port = TIMER_2; break;
+        default: return 1;
+    }
+    uint8_t lsb = 0, msb = 0;
+    /* Split the 16 bits word in two bytes */
+    if (util_get_LSB(counter_init, &lsb)) return 1;
+    if (util_get_MSB(counter_init, &msb)) return 1;
+
+    /* Write the 8 LSB of the counter */
+    if (sys_outb(timer_port, lsb)) return 1;
+    /* Write the 8 MSB of the counter */
+    if (sys_outb(timer_port, msb)) return 1;
+
+    return 0;
+}
+//7.3 timer_test_int()
+void (timer_int_handler)(void) {
+    no_interrupts++;
+}
+#ifdef LAB2
+    static int hook_id;
+    int (timer_subscribe_int)(uint8_t *bit_no) {
+        if(bit_no == NULL) return NULL_PTR;
+        hook_id = 2;
+        *bit_no = hook_id;
+        return sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE, &hook_id);
+    }
+    int (timer_unsubscribe_int)() {
+        return unsubscribe_interrupt(&hook_id);
+    }
+#endif
+int (subscribe_timer_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
+    if (interrupt_id == NULL) return 1;
+    *interrupt_id = interrupt_bit;
+    return (sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE, interrupt_id));
+}
+uint32_t no_interrupts = 0;

Property changes on: lab2/src/timer.c
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab2/src/utils.c
===================================================================
--- lab2/src/utils.c	(nonexistent)
+++ lab2/src/utils.c	(revision 196)
@@ -0,0 +1,35 @@
+#include <lcom/lcf.h>
+
+#include "utils.h"
+
+#include <stdint.h>
+#include "errors.h"
+
+int(util_get_LSB)(uint16_t val, uint8_t *lsb) {
+    if (lsb == NULL) return NULL_PTR;
+    *lsb = val;
+    return SUCCESS;
+}
+
+int(util_get_MSB)(uint16_t val, uint8_t *msb) {
+    if (msb == NULL) return NULL_PTR;
+    *msb = (val >> 8);
+    return SUCCESS;
+}
+
+int (util_sys_inb)(int port, uint8_t *value) {
+    if(value == NULL) return NULL_PTR;
+    uint32_t n = 0;
+    if(sys_inb(port, &n)) return READ_ERROR;
+    *value = n;
+    return SUCCESS;
+}
+
+int (unsubscribe_interrupt)(int *interrupt_id) {
+    if (interrupt_id == NULL) return NULL_PTR;
+    if(sys_irqrmpolicy(interrupt_id)) return UNSBCR_ERROR;
+    return SUCCESS;
+}
+
+int32_t min(int32_t a, int32_t b){ return (b < a ? b : a); }
+int32_t max(int32_t a, int32_t b){ return (a < b ? b : a); }

Property changes on: lab2/src/utils.c
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab2/Makefile
===================================================================
--- lab2/Makefile	(revision 195)
+++ lab2/Makefile	(revision 196)
@@ -1,12 +1,14 @@
 # name of the program (Minix service)
-PROG=lab2 
+PROG=lab2
 
+.PATH: ${.CURDIR}/src
+
 # source code files to be compiled
 SRCS = lab2.c timer.c utils.c
 
 # additional compilation flags
 # "-Wall -Wextra -Werror -I . -std=c11 -Wno-unused-parameter" are already set
-CFLAGS += -pedantic
+CFLAGS += -pedantic -I./include -D LAB2
 
 # list of library dependencies (for Lab 2, only LCF library)
 DPADD += ${LIBLCF}
