Index: lab3/i8254.h
===================================================================
--- lab3/i8254.h	(nonexistent)
+++ lab3/i8254.h	(revision 47)
@@ -0,0 +1,65 @@
+#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: lab3/i8254.h
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab3/kbc_func.c
===================================================================
--- lab3/kbc_func.c	(nonexistent)
+++ lab3/kbc_func.c	(revision 47)
@@ -0,0 +1,112 @@
+#include "kbc_func.h"
+#include "kbc.h"
+
+#include <lcom/lcf.h>
+
+int (subscribe_kbc_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
+    if (interrupt_id == NULL) return 1;
+    *interrupt_id = interrupt_bit;
+    return (sys_irqsetpolicy(KBC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id));
+}
+
+int (unsubscribe_interrupt)(int *interrupt_id) {
+    if (interrupt_id == NULL) return 1;
+    return sys_irqrmpolicy(interrupt_id);
+}
+
+uint8_t scancode[2];
+int two_byte_scancode = 0;
+int got_error = 0;
+
+void (kbc_ih)(void) {
+    uint8_t status = 0;
+    got_error = 0;
+
+    if (util_sys_inb(STATUS_REG, &status)) {
+        got_error = 1;
+        return;
+    }
+
+    if (status & (TIME_OUT_REC | PARITY_ERROR)) {
+        got_error = 1;
+        return;
+    }
+
+    uint8_t byte = 0;
+
+    if (util_sys_inb(OUTPUT_BUF, &byte)) {
+        got_error = 1;
+        return;
+    }
+
+    if (two_byte_scancode) {
+        scancode[1] = byte;
+        two_byte_scancode = 0;
+    } else {
+        scancode[0] = byte;
+        two_byte_scancode = (byte == TWO_BYTE_CODE);
+    }
+
+}
+
+int (kbd_poll)(uint8_t bytes[], uint8_t *size){
+    if(bytes == NULL || size == NULL) return 1;
+    uint8_t c;
+    if(kbc_read_byte(&c)) return 1;
+    if(c == TWO_BYTE_CODE){
+        if(kbc_read_byte(&bytes[1])) return 1;
+        bytes[0] = c;
+        *size = 2;
+    }else{
+        bytes[1] = 0;
+        bytes[0] = c;
+        *size = 1;
+    }
+    return 0;
+}
+
+int (kbc_read_cmd)(uint8_t *cmd){
+    if(kbc_issue_cmd(READ_KBC_CMD)) return 1;
+    if(kbc_read_byte(cmd)) return 1;
+    return 0;
+}
+
+int (kbc_change_cmd)(uint8_t cmd){
+    if(kbc_issue_cmd(WRITE_KBC_CMD)) return 1;
+    if(sys_outb(KBC_CMD_ARG, cmd)) return 1;
+    return 0;
+}
+
+int (kbc_restore_kbd)(){
+    uint8_t cmd = 0;
+    if(kbc_read_cmd(&cmd)) return 1;
+    cmd = (cmd | INT_KBD) & (~DIS_KBD);
+    if(kbc_change_cmd(cmd)) return 1;
+    return 0;
+}
+
+int (kbc_issue_cmd)(uint8_t cmd){
+    uint8_t stat;
+    for(int i = 0; i < KBC_NUM_TRIES; ++i){
+        if(util_sys_inb(STATUS_REG, &stat)) return 1;
+        if((stat&IN_BUF_FULL) == 0){
+            if(sys_outb(KBC_CMD, cmd)) return 1;
+            return 0;
+        }
+        tickdelay(micros_to_ticks(DELAY));
+    }
+    return 1;
+}
+
+int (kbc_read_byte)(uint8_t *byte){
+    uint8_t stat;
+    while(true){
+        if(util_sys_inb(STATUS_REG, &stat)) return 1;
+        if((stat&OUT_BUF_FUL) && (stat&AUX_MOUSE)==0){
+            if(stat & (PARITY_ERROR | TIME_OUT_REC)) return 1;
+            if(util_sys_inb(OUTPUT_BUF, byte)) return 1;
+            else return 0;
+        }
+        tickdelay(micros_to_ticks(DELAY));
+    }
+}

Property changes on: lab3/kbc_func.c
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab3/timer_func.c
===================================================================
--- lab3/timer_func.c	(nonexistent)
+++ lab3/timer_func.c	(revision 47)
@@ -0,0 +1,15 @@
+#include "timer_func.h"
+
+#include <lcom/lcf.h>
+#include "i8254.h"
+
+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));
+}
+
+int no_interrupts = 0;
+void (timer_int_handler)() {
+    no_interrupts++;
+}

Property changes on: lab3/timer_func.c
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: lab3/timer_func.h
===================================================================
--- lab3/timer_func.h	(nonexistent)
+++ lab3/timer_func.h	(revision 47)
@@ -0,0 +1,8 @@
+#ifndef TIMER_FUNC_H_INCLUDED
+#define TIMER_FUNC_H_INCLUDED
+
+#include <stdint.h>
+
+int (subscribe_timer_interrupt)(uint8_t interrupt_bit, int *interrupt_id);
+
+#endif //TIMER_FUNC_H_INCLUDED

Property changes on: lab3/timer_func.h
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
