From 18efe15647ef6815a6b6695b4b900ce1052f33bf Mon Sep 17 00:00:00 2001
From: Bah Thierno-Souleymane <thiernosouleymane.bah.etu@univ-lille.fr>
Date: Thu, 29 Apr 2021 19:15:47 +0200
Subject: [PATCH] feat(semanphores implemented and tested with ping pong
 scenario)

---
 include/context.h |   2 +
 include/utils.h   |   1 +
 src/main.c        | 165 +++++++++++++---------------------------------
 src/sem.c         |   2 +-
 4 files changed, 48 insertions(+), 122 deletions(-)

diff --git a/include/context.h b/include/context.h
index f482268..f922730 100644
--- a/include/context.h
+++ b/include/context.h
@@ -4,6 +4,8 @@
 #define STACK_SIZE 16384
 #define CTX_MAX 16
 
+#define SEM_MAGIC 0xCAFEBABE
+
 typedef int(func_t)(int);
 typedef void(funct_t)(void *);
 
diff --git a/include/utils.h b/include/utils.h
index d3fd720..af50f15 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -10,6 +10,7 @@ void putc(char aChar);          /* print a single char on screen */
 void puts(const char *aString); /* print a string on the screen */
 void puthex(int aNumber);       /* print an Hex number on screen */
 void assert0(char b, const char *filename, const char *fct_name, const int line);
+
 void irq_disable();
 void irq_enable();
 
diff --git a/src/main.c b/src/main.c
index 448477b..f597cf8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,16 +1,22 @@
+#include "idt.h"
 #include "ioport.h"
 #include "gdt.h"
-#include "idt.h"
-#include "keyboard.h"
 #include "utils.h"
 #include "context.h"
+#include "sem.h"
+#include "keyboard.h"
 
-/* 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] */
+extern int cursor_x;
+extern int cursor_y;
 
 int counter = 0;
 
+ctx_t *ping_ctx = NULL;
+ctx_t *pong_ctx = NULL;
+
+sem_t sping;
+sem_t spong;
+
 void empty_irq(int_regs_t *r)
 {
 }
@@ -57,6 +63,30 @@ void keyboard_handler(void *args)
   }
 }
 
+void ping(void *arg)
+{
+  while (1)
+  {
+    puts("PING\n");
+    for (int i = 0; i < 100000000; i++)
+      ;
+    sem_up(&spong);
+    sem_down(&sping);
+  }
+}
+
+void pong(void *arg)
+{
+  while (1)
+  {
+    puts("PONG\n");
+    for (int i = 0; i < 100000000; i++)
+      ;
+    sem_up(&sping);
+    sem_down(&spong);
+  }
+}
+
 /* multiboot entry-point with datastructure as arg. */
 void main(unsigned int *mboot_info)
 {
@@ -73,7 +103,7 @@ void main(unsigned int *mboot_info)
 
   puts("\n\n");
 
-  idt_setup_handler(0, timer_it);
+  idt_setup_handler(0, empty_irq);
   idt_setup_handler(1, empty_irq);
 
   __asm volatile("sti");
@@ -82,122 +112,15 @@ void main(unsigned int *mboot_info)
 
   puts("Hello world\n\n");
 
-  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;
-
-/* clear screen */
-void clear_screen()
-{
-  int i;
-  for (i = 0; i < 80 * 25; i++)
-  {                                           /* for each one of the 80 char by 25 lines */
-    video_memory[i * 2 + 1] = 0x0F;           /* color is set to black background and white char */
-    video_memory[i * 2] = (unsigned char)' '; /* character shown is the space char */
-  }
-}
+  sem_init(&sping, -1);
+  sem_init(&spong, 0);
 
-/* print a string on the screen */
-void puts(const char *aString)
-{
-  const char *current_char = aString;
-  while (*current_char != 0)
-  {
-    putc(*current_char++);
-  }
-}
+  ctx_t *ping_ctx = create_ctx(ping, NULL);
+  ctx_t *pong_ctx = create_ctx(pong, NULL);
 
-/* print an number in hexa */
-char *hex_digit = "0123456789ABCDEF";
-void puthex(int aNumber)
-{
-  int i;
-  int started = 0;
-  for (i = 28; i >= 0; i -= 4)
-  {
-    int k = (aNumber >> i) & 0xF;
-    if (k != 0 || started)
-    {
-      putc(hex_digit[k]);
-      started = 1;
-    }
-  }
-}
-
-void setCursor()
-{
-  int cursor_offset = cursor_x + cursor_y * 80;
-  _outb(0x3d4, 14);
-  _outb(0x3d5, ((cursor_offset >> 8) & 0xFF));
-  _outb(0x3d4, 15);
-  _outb(0x3d5, (cursor_offset & 0xFF));
-}
+  //sem_down(&sping);
+  yield();
 
-void putc(char c)
-{
-  if (cursor_x > 79)
-  {
-    cursor_x = 0;
-    cursor_y++;
-  }
-  if (cursor_y > 24)
-  {
-    cursor_y = 0;
-    clear_screen();
-  }
-  switch (c)
-  { /* deal with a special char */
-  case '\r':
-    cursor_x = 0;
-    break; /* carriage return */
-  case '\n':
-    cursor_x = 0;
-    cursor_y++;
-    break; /* new ligne */
-  case 0x8:
-    if (cursor_x > 0)
-      cursor_x--;
-    break; /* backspace */
-  case 0x9:
-    cursor_x = (cursor_x + 8) & ~7;
-    break; /* tabulation */
-    /* or print a simple character */
-  default:
-    video_memory[(cursor_x + 80 * cursor_y) * 2] = c;
-    cursor_x++;
-    break;
-  }
-  setCursor();
+  for (;;)
+    ; /* nothing more to do... really nothing ! */
 }
diff --git a/src/sem.c b/src/sem.c
index 402aae8..327cd58 100644
--- a/src/sem.c
+++ b/src/sem.c
@@ -42,4 +42,4 @@ void sem_down(sem_t *sem)
         yield();
     }
     irq_enable();
-}
+}
\ No newline at end of file
-- 
GitLab