Index: lab3/Makefile
===================================================================
--- lab3/Makefile	(revision 8)
+++ lab3/Makefile	(revision 9)
@@ -1,17 +1,16 @@
-# name of the program (Minix service)
-PROG=lab3
-
-# source code files to be compiled
-SRCS = lab3.c #timer.c utils.c
-
-# additional compilation flags
-# "-Wall -Wextra -Werror -I . -std=c11 -Wno-unused-parameter" are already set
-CFLAGS += -pedantic
-CPPFLAGS += -D LAB3
-
-# list of library dependencies (for Lab 2, only LCF library)
-DPADD += ${LIBLCF}
-LDADD += -llcf
-
-# include LCOM's makefile that does all the "heavy lifting"
-.include <minix.lcom.mk>
+# name of the program (Minix service)
+PROG=lab3
+
+# source code files to be compiled
+SRCS = lab3.c keyboard.c 
+
+# additional compilation flags
+# "-Wall -Wextra -Werror -I . -std=c11 -Wno-unused-parameter" are already set
+CPPFLAGS += -pedantic -DLAB3
+
+# list of library dependencies (for Lab 2, only LCF library)
+DPADD += ${LIBLCF}
+LDADD += -llcf
+
+# include LCOM's makefile that does all the "heavy lifting"
+.include <minix.lcom.mk>
Index: lab3/lab3
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/x-executable
Index: lab3/lab3.c
===================================================================
--- lab3/lab3.c	(revision 8)
+++ lab3/lab3.c	(revision 9)
@@ -6,15 +6,19 @@
 #include <stdbool.h>
 #include "i8254.h"
 #include <stdint.h>
+#include "i8042.h"
+#include "keyboard.h"
 //#include <timer.c>
 
 
-#define IRQ_KB 1
+/* #define IRQ_KB 1
 #define DELAY_US 20000
 #define EXIT_CODE 0x81
-#define I_O_BUFFER 0x60
+#define I_O_BUFFER 0x60 */
 
-static uint32_t scancode;
+uint32_t cnt=0;
+ uint32_t scancode;
+int flag=0;
 
 int main(int argc, char *argv[]) {
   // sets the language of LCF messages (can be either EN-US or PT-PT)
@@ -40,8 +44,8 @@
   return 0;
 }
 
-void(kbc_ih)() {}
 
+
 int(kbd_test_scan)() {
   /* To be completed by the students */
   int ipc_status;
@@ -48,10 +52,9 @@
   uint8_t r, bit_no = 0;
   message msg;
   int hook_id = bit_no;
-  uint8_t scan_arr[2] = {0, 0};
+/*   uint8_t scan_arr[2] = {0, 0};
 
-  int flag = 0;
-  uint32_t cnt=0;
+  int flag = 0; */
 
   printf("%d\n",
          sys_irqsetpolicy(IRQ_KB, IRQ_REENABLE | IRQ_EXCLUSIVE, &hook_id));
@@ -84,34 +87,10 @@
       case HARDWARE: /* hardware interrupt notification */
         if (msg.m_notify.interrupts & BIT(0)) { /* subscribed interrupt */
 
-          #ifdef LAB3 
-          cnt++; 
-          #endif
-          sys_inb(0x64, &scancode);
-          if (scancode & 0x01) {
-            #ifdef LAB3 
-            cnt++; 
-            #endif
-            sys_inb(0x60, &scancode);
-          if (scancode != 0xe0 && flag == 0) {
-            // tickdelay(micros_to_ticks(DELAY_US));
-            scan_arr[0] = scancode;
-            kbd_print_scancode(!(scancode & 0x80), 1, scan_arr);
+          kbc_ih();
           }
 
-          else if (scancode == 0xe0) {
-            scan_arr[0] = scancode;
-            flag = 1;
-          }
-
-          else if (flag == 1) {
-            scan_arr[1] = scancode;
-            kbd_print_scancode(!(scancode & 0x80), 2, scan_arr);
-            flag = 0;
-          }
-          }
-
-        }
+        
         // printf("msg:%x\n",msg.m_notify.interrupts);
         break;
       default:
@@ -134,12 +113,9 @@
   /* To be completed by the students */
   uint32_t key=0;
   uint8_t scan_arr[2] = {0, 0};
-  int flag = 0;
-  uint32_t cnt=0;
-  sys_outb(0x64,0x20);        //enable interrupts again
-  
-  sys_outb(0x64, 0x60);
-  sys_inb(0x60,&key);
+  //int flag = 0;
+  //uint32_t cnt=0;
+
   while(scancode != EXIT_CODE)
   {
     tickdelay(micros_to_ticks(DELAY_US));
@@ -146,18 +122,18 @@
     #ifdef LAB3 
     cnt++; 
     #endif
-    sys_inb(0x64, &scancode);
-          if (scancode & 0x01) {
+    sys_inb(IN_BUF, &scancode);
+          if (scancode & O_BUFFER_FULL) {
             #ifdef LAB3 
             cnt++;
              #endif
-            sys_inb(0x60, &scancode);
-          if (scancode != 0xe0 && flag == 0) {
+            sys_inb(I_O_BUFFER, &scancode);
+          if (scancode != TWO_BYTES && flag == 0) {
             scan_arr[0] = scancode;
-            kbd_print_scancode(!(scancode & 0x80), 1, scan_arr);
+            kbd_print_scancode(!(scancode & PAR_ERR), 1, scan_arr);
           }
 
-          else if (scancode == 0xe0) {
+          else if (scancode == TWO_BYTES) {
             scan_arr[0] = scancode;
             flag = 1;
           }
@@ -164,27 +140,37 @@
 
           else if (flag == 1) {
             scan_arr[1] = scancode;
-            kbd_print_scancode(!(scancode & 0x80), 2, scan_arr);
+            kbd_print_scancode(!(scancode & PAR_ERR), 2, scan_arr);
             flag = 0;
           }
         }
   }
+  sys_outb(IN_BUF, READ_CMD);        
+  sys_outb(IN_BUF, I_O_BUFFER);
+
+  #ifdef LAB3 
+    cnt++; 
+  #endif
+
+  sys_inb(I_O_BUFFER,&key); 
+
+  sys_outb(IN_BUF,I_O_BUFFER);        //enable interrupts again
+  sys_outb(I_O_BUFFER,  key |  BIT(0));  
+
   kbd_print_no_sysinb(cnt);
-  sys_outb(0x64,0x60);        //enable interrupts again
-  sys_outb(0x60,  key |  BIT(0));      //enable interrupts again
   return 0;
 }
 
 int(kbd_test_timed_scan)(uint8_t n) {
   /* To be completed by the students */
-  uint8_t scan_arr[2] = {0, 0};
-  int flag=0;
+/*   uint8_t scan_arr[2] = {0, 0};
+  int flag=0; */
    int ipc_status;
   message msg;
   uint8_t r;
   uint8_t bit_no_timer = 0, bit_no_kbd = 1;
   int hook_id_kbd=bit_no_kbd, hook_id_timer=bit_no_timer;
-  int cont=0,cnt=0;
+  int cont=0;//cnt=0;
   
   sys_irqsetpolicy(IRQ_TIMER,IRQ_REENABLE,&hook_id_timer);
   sys_irqsetpolicy(IRQ_KB, IRQ_REENABLE | IRQ_EXCLUSIVE, &hook_id_kbd);
@@ -212,34 +198,7 @@
                if (msg.m_notify.interrupts  & BIT(bit_no_kbd)) 
                { 
                     
-                  #ifdef LAB3 
-                  cnt++; 
-                 #endif
-                 sys_inb(0x64, &scancode); 
-                 if (scancode & 0x01) { 
-                  #ifdef LAB3 
-                   cnt++; 
-                    #endif
-                   sys_inb(0x60, &scancode); 
-                 if (scancode != 0xe0 && flag == 0) 
-                 {
-                    
-                   scan_arr[0] = scancode;
-                   kbd_print_scancode(!(scancode & 0x80), 1, scan_arr);
-                 }
-
-                 else if (scancode == 0xe0)
-                  {
-                   scan_arr[0] = scancode;
-                    flag = 1;
-                 }
-
-                 else if (flag == 1) 
-                 {
-                    scan_arr[1] = scancode;
-                     kbd_print_scancode(!(scancode & 0x80), 2, scan_arr);
-                   flag = 0;
-                   }
+                  kbc_ih();
                    cont=0;
                }
                 //printf("msg:%x\n",msg.m_notify.interrupts);
@@ -252,8 +211,8 @@
      // printf("3\n"); /* received a standard message, not a notification */
         /* no standard messages expected: do nothing */
     
+ 
  }
- }
   kbd_print_no_sysinb(cnt);
   
   if(sys_irqrmpolicy(&hook_id_timer)!=0) return 1;
