Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
8:edd70006d3e4 | 9:92ace1ca50a8 |
---|---|
1 #include "kernel.h" | |
2 | |
3 static int shiftstate = 0; | |
4 static volatile unsigned char charAvail = 0; | |
5 static volatile char kbdchar = ' '; | |
6 | |
7 static char keymap[128] = { | |
8 '?','?','1','2', '3', '4', '5','6', '7', '8','9', '0', '-','=', 0xe, '?', | |
9 'q','w','e','r', 't', 'y', 'u','i', 'o', 'p','[', ']', '\n','?', 'a', 's', | |
10 'd','f','g','h', 'j', 'k', 'l',';', '\'', '?','?', '?', 'z','x', 'c', 'v', | |
11 'b','n','m',',', '.', '/', '?','?', '?', ' ','?', '?', '?','?', '?', '?', | |
12 | |
13 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?', | |
14 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?', | |
15 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?', | |
16 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?' | |
17 }; | |
18 | |
19 static char keymapUPPER[128] = { | |
20 '?','?','!','@', '#', '$', '%','^', '&', '*','(', ')', '_','+', '?', '?', | |
21 'Q','W','E','R', 'T', 'Y', 'U','I', 'O', 'P','{', '}', '|','?', 'A', 'S', | |
22 'D','F','G','H', 'J', 'K', 'L',':', '"', '?','?', '?', 'Z','X', 'C', 'V', | |
23 'B','N','M','<', '>', '?', '?','?', '?', ' ','?', '?', '?','?', '?', '?', | |
24 | |
25 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?', | |
26 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?', | |
27 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?', | |
28 '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?' | |
29 }; | |
30 | |
31 static uint64_t ticks = 0; | |
32 void timerDriverUpdate() | |
33 { | |
34 ticks++; | |
35 } | |
36 | |
37 uint64_t getTimeMS() | |
38 { | |
39 return 55*ticks; | |
40 } | |
41 | |
42 int strncmp(const char* s1, const char* s2, int size) { | |
43 int i; | |
44 for (i=0; i<size; i++) { | |
45 if (s1[i] != s2[i]) return 0; | |
46 } | |
47 return 1; | |
48 } | |
49 | |
50 void keyboardDriverUpdate(unsigned char scancode) | |
51 { | |
52 switch(scancode) { | |
53 case 0x2a: | |
54 shiftstate = 1; | |
55 break; | |
56 case 0xaa: | |
57 shiftstate = 0; | |
58 break; | |
59 default: | |
60 if (scancode < 128) { | |
61 if (charAvail == 0) { | |
62 if (shiftstate == 0) { | |
63 kbdchar = keymap[scancode]; | |
64 } else { | |
65 kbdchar = keymapUPPER[scancode]; | |
66 } | |
67 | |
68 charAvail = 1; | |
69 } | |
70 } else { | |
71 // Key release | |
72 //printf("Unhandled scancode: 0x%x\n", scancode); | |
73 } | |
74 break; | |
75 } | |
76 } | |
77 | |
78 char getChar() { | |
79 while (charAvail == 0); | |
80 char c = kbdchar; | |
81 charAvail = 0; | |
82 return c; | |
83 } | |
84 | |
85 void getline(char *buffer, int len) { | |
86 char c; | |
87 int i = 0; | |
88 while (i < len-1) { | |
89 c = getChar(); | |
90 if (c == '\n') { | |
91 // Enter | |
92 break; | |
93 } | |
94 if (c == 0x0e) { | |
95 if (i>0) { | |
96 printf(" "); | |
97 i--; | |
98 } | |
99 continue; | |
100 } | |
101 buffer[i] = c; | |
102 printf("%c", c); | |
103 i++; | |
104 } | |
105 buffer[i] = 0; | |
106 } | |
107 | |
108 /* | |
109 malloc and free divide the chunks of memory present at the heap | |
110 of the kernel into smaller parts. | |
111 The heap is located at: 0x | |
112 */ | |
113 static void* kernel_heap = (void*) 0xD0000000; | |
114 /* Allocates 'size' bytes and returns the pointer if succesfull. | |
115 Kernelpanic in case of failure.. | |
116 */ | |
117 void* malloc(size_t size) { | |
118 printf("Malloc %d bytes\n", size); | |
119 return kernel_heap; | |
120 } | |
121 | |
122 void free(void* ptr) { | |
123 printf("Free address %x\n", ptr); | |
124 } | |
125 | |
126 void kmain() | |
127 { | |
128 init_screen(); | |
129 clear_screen(); | |
130 printf("Welcome!\n"); | |
131 printf("sizeof(uint32_t)=%u, sizeof(uint64_t)=%u\n", sizeof(uint32_t), sizeof(uint64_t)); | |
132 printf("Entering mainloop!\n"); | |
133 | |
134 while (1==1) | |
135 { | |
136 char buffer[70]; | |
137 printf(">>>"); | |
138 getline(buffer, 70); | |
139 // TODO: interpret this line with python :) | |
140 printf("\n"); | |
141 printf("Got line: '%s'\n", buffer); | |
142 if (buffer[0] == 'x') | |
143 { | |
144 printf("System time in ms: %d\n", getTimeMS()); | |
145 } | |
146 if ( strncmp(buffer, "help", 4)) | |
147 { | |
148 printf("Help\n Try one of these commands:\n"); | |
149 printf(" x: print system time in ms\n"); | |
150 } | |
151 } | |
152 } | |
153 | |
154 |