From c9ffa12af29ecb40ed6cde680139a28dbf5322cd Mon Sep 17 00:00:00 2001
From: Divyam Khandelwal <divyam@Divyams-MacBook-Pro.local>
Date: Sat, 28 Apr 2018 21:49:38 -0500
Subject: [PATCH] RTC virtualised

---
 student-distrib/rtc.c | 131 ++++++++++++++++++++++++++----------------
 student-distrib/rtc.h |   9 ++-
 2 files changed, 91 insertions(+), 49 deletions(-)

diff --git a/student-distrib/rtc.c b/student-distrib/rtc.c
index 0de20a0..9e92193 100644
--- a/student-distrib/rtc.c
+++ b/student-distrib/rtc.c
@@ -1,8 +1,13 @@
 #include "rtc.h"
 
 /* Set interrupt flag */
-//interrupt_flag = 1;
+interrupt_flag = 1;
 
+/* For virtualisation */
+rtc_active[MAX_TERMINAL]       = {0,0,0};
+rtc_flag[MAX_TERMINAL]         = {1,1,1};
+rtc_counter[MAX_TERMINAL]      = {0,0,0};
+rtc_init_counter[MAX_TERMINAL] = {0,0,0};
 
 /* rtc_init()
  * Inputs: void
@@ -14,7 +19,7 @@ void rtc_init(void)
 {
     uint8_t prev;
 
-    //int32_t freq;
+    // int32_t freq;
 
     /* select register B, and disable NMI */
     outb(DISABLE_NMI_B, RTC_PORT);
@@ -26,6 +31,9 @@ void rtc_init(void)
     outb(prev | BIT_SIX, RTC_DATA);	
     /* Enable interrupts */
 
+    // freq = 2;
+    // rtc_write(NULL, &freq, 4);
+
     enable_irq(RTC_IRQ);
 
 }
@@ -39,12 +47,26 @@ void rtc_init(void)
  */
 void rtc_interrupt(void)
 {
+    int i;
+
     /* logic to enable more than one RTC interrupt */
     outb(0x0C, 0x70); /* 0x0C = DATA to write, 0x70 = port */
     inb(0x71);        /* read from port 0x71 (RTC Data port) */
 
     /* Clear interrupt flag */
-    interrupt_flag = 0;
+    for (i = 0; i < MAX_TERMINAL; i++)
+    {
+        rtc_counter[i]--;
+        /* Check if counter has reached zero */
+        if(rtc_counter[i] == 0)
+        {
+            /* Clear interrupt flag */
+            rtc_flag[i] = 0;
+            /* Reset counter */
+            rtc_counter[i] = rtc_init_counter[i];
+        }
+    }
+    // interrupt_flag = 0;
 
     return;
 }
@@ -58,11 +80,15 @@ void rtc_interrupt(void)
  */
 int32_t rtc_read(int32_t fd, void* buf, int32_t nbytes)
 {
+
     /* Set flag */
-    interrupt_flag = 1;
+    rtc_flag[running_terminal] = 1;
 
+    //interrupt_flag = 1;
+    sti();
     /* Wait until interrupt handler clears flag */
-    while (interrupt_flag);
+    while (rtc_flag[running_terminal]);
+    cli();
 
     return 0;
 }
@@ -103,37 +129,41 @@ int32_t rtc_write(int32_t fd, const void* buf, int32_t nbytes)
     if (valid_freq != 1)
       return -1;
 
-    /* Translate frequency to rate */
+    rtc_counter[running_terminal] = INIT_FREQ/freq;
 
-    temp_freq = freq;
+    rtc_init_counter[running_terminal] = rtc_counter[running_terminal];
 
-    for (i = 1; i <=  MAX_FREQ_POW; i++)
-    {
-        temp_freq = temp_freq >> 1;
+    // /* Translate frequency to rate */
 
-        if ((temp_freq & bit_mask) == 1)
-        {
-            rate = RATE_OFFSET - i;
-            break;
-        }
-        //temp_freq = temp_freq >> 1;
-    }
+    // temp_freq = freq;
 
-    /* Set the rate */
-    rate &= 0x0F;
+    // for (i = 1; i <=  MAX_FREQ_POW; i++)
+    // {
+    //     temp_freq = temp_freq >> 1;
 
-    /* Disable interrupts */
-    cli();
-    /* set index to register A, disable NMI */
-    outb(DISABLE_NMI_A, RTC_PORT);
-    /* get initial value of register A */
-    prev = inb(RTC_DATA);
-    /* reset index to A */
-    outb(DISABLE_NMI_A, RTC_PORT);
-    /* write only our rate to A. Note, rate is the bottom 4 bits. */
-    outb((prev & 0xF0) | rate, RTC_DATA);
-    /* Enable interrupts */
-    sti();
+    //     if ((temp_freq & bit_mask) == 1)
+    //     {
+    //         rate = RATE_OFFSET - i;
+    //         break;
+    //     }
+    //     //temp_freq = temp_freq >> 1;
+    // }
+
+    // /* Set the rate */
+    // rate &= 0x0F;
+
+    // /* Disable interrupts */
+    // cli();
+    // /* set index to register A, disable NMI */
+    // outb(DISABLE_NMI_A, RTC_PORT);
+    // /* get initial value of register A */
+    // prev = inb(RTC_DATA);
+    // /* reset index to A */
+    // outb(DISABLE_NMI_A, RTC_PORT);
+    // /* write only our rate to A. Note, rate is the bottom 4 bits. */
+    // outb((prev & 0xF0) | rate, RTC_DATA);
+    // /* Enable interrupts */
+    // sti();
 
     /* Return number of bytes written */
     return FOUR_BYTES;
@@ -146,26 +176,31 @@ int32_t rtc_write(int32_t fd, const void* buf, int32_t nbytes)
  */
 int32_t rtc_open(const uint8_t* filename)
 {
-    uint8_t rate, prev;
+    /* Set RTC to active for the running terminal */
+    rtc_active[running_terminal] = 1;
+    return 0;
 
-    /* Set the frequency to 2 Hz */
-    rate = TWO_HZ; 
-    rate &= 0x0F;
 
-    /* Disable interrupts */
-    cli();
-    /* set index to register A, disable NMI */
-    outb(DISABLE_NMI_A, RTC_PORT);
-    /* get initial value of register A */
-    prev = inb(RTC_DATA);
-    /* reset index to A */
-    outb(DISABLE_NMI_A, RTC_PORT);
-    /* write only our rate to A. Note, rate is the bottom 4 bits. */
-    outb((prev & 0xF0) | rate, RTC_DATA);
-    /* Enable interrupts */
-    sti();
+    // uint8_t rate, prev;
+
+    // /* Set the frequency to 2 Hz */
+    // rate = TWO_HZ; 
+    // rate &= 0x0F;
+
+    // /* Disable interrupts */
+    // cli();
+    // /* set index to register A, disable NMI */
+    // outb(DISABLE_NMI_A, RTC_PORT);
+    // /* get initial value of register A */
+    // prev = inb(RTC_DATA);
+    // /* reset index to A */
+    // outb(DISABLE_NMI_A, RTC_PORT);
+    // /* write only our rate to A. Note, rate is the bottom 4 bits. */
+    // outb((prev & 0xF0) | rate, RTC_DATA);
+    // /* Enable interrupts */
+    // sti();
     
-    return 0;
+    // return 0;
 }
 
 /* rtc_close()
diff --git a/student-distrib/rtc.h b/student-distrib/rtc.h
index 13b55a7..1fc3020 100644
--- a/student-distrib/rtc.h
+++ b/student-distrib/rtc.h
@@ -16,6 +16,7 @@
 #define BIT_SIX         0x40
 #define TWO_HZ          15  
 #define FREQ_MAX        8192
+#define INIT_FREQ       1024
 #define MAX_FREQ_POW    13
 #define RATE_OFFSET     16
 #define FOUR_BYTES 		4
@@ -28,7 +29,13 @@
 #define REGISTER_C		0x0C
 
 /* Interrupt flag for read() */
-volatile int interrupt_flag;
+extern int interrupt_flag;
+
+/* Global Variables for Virtualization */
+extern int rtc_active[MAX_TERMINAL];
+extern int rtc_flag[MAX_TERMINAL];
+extern int rtc_counter[MAX_TERMINAL];
+
 
 /*
  * rtc_init()
-- 
GitLab