diff cos/kernel/kernel.c @ 9:92ace1ca50a8

64 bits kernel without interrupts but with printf in C
author windel
date Sun, 13 Nov 2011 12:47:47 +0100
parents
children a58904747019
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cos/kernel/kernel.c	Sun Nov 13 12:47:47 2011 +0100
@@ -0,0 +1,154 @@
+#include "kernel.h"
+
+static int shiftstate = 0;
+static volatile unsigned char charAvail = 0;
+static volatile char kbdchar = ' ';
+
+static char keymap[128] = {
+  '?','?','1','2', '3', '4', '5','6', '7', '8','9', '0', '-','=', 0xe, '?',
+  'q','w','e','r', 't', 'y', 'u','i', 'o', 'p','[', ']', '\n','?', 'a', 's',
+  'd','f','g','h', 'j', 'k', 'l',';', '\'', '?','?', '?', 'z','x', 'c', 'v',
+  'b','n','m',',', '.', '/', '?','?', '?', ' ','?', '?', '?','?', '?', '?',
+
+  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
+  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
+  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
+  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?'
+};
+
+static char keymapUPPER[128] = {
+  '?','?','!','@', '#', '$', '%','^', '&', '*','(', ')', '_','+', '?', '?',
+  'Q','W','E','R', 'T', 'Y', 'U','I', 'O', 'P','{', '}', '|','?', 'A', 'S',
+  'D','F','G','H', 'J', 'K', 'L',':', '"', '?','?', '?', 'Z','X', 'C', 'V',
+  'B','N','M','<', '>', '?', '?','?', '?', ' ','?', '?', '?','?', '?', '?',
+
+  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
+  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
+  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
+  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?'
+};
+
+static uint64_t ticks = 0;
+void timerDriverUpdate()
+{
+  ticks++;
+}
+
+uint64_t getTimeMS()
+{
+  return 55*ticks;
+}
+
+int strncmp(const char* s1, const char* s2, int size) {
+  int i;
+  for (i=0; i<size; i++) {
+    if (s1[i] != s2[i]) return 0;
+  }
+  return 1;
+}
+
+void keyboardDriverUpdate(unsigned char scancode)
+{
+  switch(scancode) {
+    case 0x2a:
+      shiftstate = 1;
+      break;
+    case 0xaa:
+      shiftstate = 0;
+      break;
+    default:
+      if (scancode < 128) {
+        if (charAvail == 0) {
+          if (shiftstate == 0) {
+            kbdchar = keymap[scancode];
+          } else {
+            kbdchar = keymapUPPER[scancode];
+          }
+          
+          charAvail = 1;
+        }
+      } else {
+        // Key release
+        //printf("Unhandled scancode: 0x%x\n", scancode);
+      }
+      break;
+  }
+}
+
+char getChar() {
+  while (charAvail == 0);
+  char c = kbdchar;
+  charAvail = 0;
+  return c;
+}
+
+void getline(char *buffer, int len) {
+  char c;
+  int i = 0;
+  while (i < len-1) {
+    c = getChar();
+    if (c == '\n') {
+      // Enter
+      break;
+    }
+    if (c == 0x0e) {
+      if (i>0) {
+         printf(" ");
+         i--;
+      }
+      continue;
+    }
+    buffer[i] = c;
+    printf("%c", c);
+    i++;
+  }
+  buffer[i] = 0;
+}
+
+/*
+  malloc and free divide the chunks of memory present at the heap
+  of the kernel into smaller parts.
+  The heap is located at: 0x
+*/
+static void* kernel_heap = (void*) 0xD0000000;
+/* Allocates 'size' bytes and returns the pointer if succesfull.
+   Kernelpanic in case of failure..
+*/
+void* malloc(size_t size) {
+  printf("Malloc %d bytes\n", size);
+  return kernel_heap;
+}
+
+void free(void* ptr) {
+  printf("Free address %x\n", ptr);
+}
+
+void kmain()
+{
+  init_screen();
+  clear_screen();
+  printf("Welcome!\n");
+  printf("sizeof(uint32_t)=%u, sizeof(uint64_t)=%u\n", sizeof(uint32_t), sizeof(uint64_t));
+  printf("Entering mainloop!\n");
+
+  while (1==1) 
+  {
+    char buffer[70];
+    printf(">>>");
+    getline(buffer, 70);
+    // TODO: interpret this line with python :)
+    printf("\n");
+    printf("Got line: '%s'\n", buffer);
+    if (buffer[0] == 'x') 
+    {
+      printf("System time in ms: %d\n", getTimeMS());
+    }
+    if ( strncmp(buffer, "help", 4)) 
+    {
+      printf("Help\n Try one of these commands:\n");
+      printf(" x: print system time in ms\n");
+    }
+  }
+}
+
+