From d1532c8483b6c6b30127ba9759733ce8cf692387 Mon Sep 17 00:00:00 2001
From: Bah Thierno-Souleymane <thiernosouleymane.bah.etu@univ-lille.fr>
Date: Tue, 20 Apr 2021 01:14:01 +0200
Subject: [PATCH] feat(Two contexts demonstration done)

---
 src/main.c | 106 ++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 81 insertions(+), 25 deletions(-)

diff --git a/src/main.c b/src/main.c
index bda70a6..448477b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,16 +2,61 @@
 #include "gdt.h"
 #include "idt.h"
 #include "keyboard.h"
+#include "utils.h"
+#include "context.h"
 
-void clear_screen();      /* clear screen */
-void putc(char aChar);    /* print a single char on screen */
-void puts(char *aString); /* print a string on the screen */
-void puthex(int aNumber); /* print an Hex number on screen */
+/* print a char on the screen */
+int cursor_x = 0; /* here is the cursor position on X [0..79] */
+int cursor_y = 0; /* here is the cursor position on Y [0..24] */
+
+int counter = 0;
 
 void empty_irq(int_regs_t *r)
 {
 }
 
+void timer_it(int_regs_t *r)
+{
+  yield();
+}
+
+void counter_handler(void *args)
+{
+  while (1)
+  {
+    irq_disable();
+    int tmp_cursor[2] = {cursor_x, cursor_y};
+    cursor_x = 70;
+    cursor_y = 0;
+
+    puthex(counter++);
+
+    cursor_x = tmp_cursor[0];
+    cursor_y = tmp_cursor[1];
+
+    irq_enable();
+  }
+}
+
+void keyboard_handler(void *args)
+{
+  init_keymapping();
+  while (1)
+  {
+    irq_disable();
+    if (_inb(0x64) == (0x1D))
+    {
+      _outb(0x64, 0x1C);
+      unsigned char s = _inb(0x60);
+      char c = keyboard_map(s);
+      if (c)
+        putc(c);
+      // puthex(s);
+    }
+    irq_enable();
+  }
+}
+
 /* multiboot entry-point with datastructure as arg. */
 void main(unsigned int *mboot_info)
 {
@@ -28,8 +73,8 @@ void main(unsigned int *mboot_info)
 
   puts("\n\n");
 
-  idt_setup_handler(0, empty_irq);
-  idt_setup_handler(0, empty_irq);
+  idt_setup_handler(0, timer_it);
+  idt_setup_handler(1, empty_irq);
 
   __asm volatile("sti");
 
@@ -37,24 +82,39 @@ void main(unsigned int *mboot_info)
 
   puts("Hello world\n\n");
 
-  init_keymapping();
-  while (1)
-  {
-    if (_inb(0x64) == (0x1D))
-    {
-      _outb(0x64, 0x1C);
-      unsigned char s = _inb(0x60);
-      char c = keyboard_map(s);
-      if (c)
-        putc(c);
-      // puthex(s);
-    }
-  }
+  ctx_t *ctx1 = create_ctx(counter_handler, NULL);
+  ctx_t *ctx2 = create_ctx(keyboard_handler, NULL);
 
   for (;;)
     ; /* nothing more to do... really nothing ! */
 }
 
+void assert0(char b, const char *filename, const char *fct_name, const int line)
+{
+  if (!b)
+  {
+    puts("Assertion error in ");
+    puts(filename);
+    puts(" in ");
+    puts(fct_name);
+    puts(" at 0x");
+    puthex(line);
+    puts("\n");
+  }
+}
+
+void irq_disable()
+{
+  __asm volatile("cli");
+}
+
+void irq_enable()
+{
+  __asm volatile("sti");
+  _outb(0xA0, 0x20);
+  _outb(0x20, 0x20);
+}
+
 /* base address for the video output assume to be set as character oriented by the multiboot */
 unsigned char *video_memory = (unsigned char *)0xB8000;
 
@@ -70,9 +130,9 @@ void clear_screen()
 }
 
 /* print a string on the screen */
-void puts(char *aString)
+void puts(const char *aString)
 {
-  char *current_char = aString;
+  const char *current_char = aString;
   while (*current_char != 0)
   {
     putc(*current_char++);
@@ -96,10 +156,6 @@ void puthex(int aNumber)
   }
 }
 
-/* print a char on the screen */
-int cursor_x = 0; /* here is the cursor position on X [0..79] */
-int cursor_y = 0; /* here is the cursor position on Y [0..24] */
-
 void setCursor()
 {
   int cursor_offset = cursor_x + cursor_y * 80;
-- 
GitLab