9
|
1 #include "kernel.h"
|
|
2
|
|
3 static int shiftstate = 0;
|
17
|
4 static volatile uint8_t charAvail = 0;
|
9
|
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
|
17
|
31 // IO port helpers:
|
|
32 void outb(uint16_t port, uint8_t value)
|
|
33 {
|
|
34 asm volatile ("outb %1, %0" : : "dN" (port), "a" (value));
|
|
35 }
|
|
36
|
|
37 uint8_t inb(uint16_t port)
|
|
38 {
|
|
39 uint8_t ret;
|
|
40 asm volatile ("inb %1, %0" : "=a" (ret) : "dN" (port));
|
|
41 return ret;
|
|
42 }
|
|
43
|
|
44 uint16_t inw(uint16_t port)
|
|
45 {
|
|
46 uint16_t ret;
|
|
47 asm volatile ("inw %1, %0" : "=a" (ret) : "dN" (port));
|
|
48 return ret;
|
|
49 }
|
|
50
|
|
51
|
9
|
52 static uint64_t ticks = 0;
|
|
53 void timerDriverUpdate()
|
|
54 {
|
|
55 ticks++;
|
|
56 }
|
|
57
|
|
58 uint64_t getTimeMS()
|
|
59 {
|
|
60 return 55*ticks;
|
|
61 }
|
|
62
|
|
63 int strncmp(const char* s1, const char* s2, int size) {
|
|
64 int i;
|
|
65 for (i=0; i<size; i++) {
|
|
66 if (s1[i] != s2[i]) return 0;
|
|
67 }
|
|
68 return 1;
|
|
69 }
|
|
70
|
|
71 void keyboardDriverUpdate(unsigned char scancode)
|
|
72 {
|
|
73 switch(scancode) {
|
|
74 case 0x2a:
|
|
75 shiftstate = 1;
|
|
76 break;
|
|
77 case 0xaa:
|
|
78 shiftstate = 0;
|
|
79 break;
|
|
80 default:
|
|
81 if (scancode < 128) {
|
|
82 if (charAvail == 0) {
|
|
83 if (shiftstate == 0) {
|
|
84 kbdchar = keymap[scancode];
|
|
85 } else {
|
|
86 kbdchar = keymapUPPER[scancode];
|
|
87 }
|
|
88
|
|
89 charAvail = 1;
|
|
90 }
|
|
91 } else {
|
|
92 // Key release
|
|
93 //printf("Unhandled scancode: 0x%x\n", scancode);
|
|
94 }
|
|
95 break;
|
|
96 }
|
|
97 }
|
|
98
|
|
99 char getChar() {
|
|
100 while (charAvail == 0);
|
|
101 char c = kbdchar;
|
|
102 charAvail = 0;
|
|
103 return c;
|
|
104 }
|
|
105
|
|
106 void getline(char *buffer, int len) {
|
|
107 char c;
|
|
108 int i = 0;
|
|
109 while (i < len-1) {
|
|
110 c = getChar();
|
|
111 if (c == '\n') {
|
|
112 // Enter
|
|
113 break;
|
|
114 }
|
|
115 if (c == 0x0e) {
|
|
116 if (i>0) {
|
|
117 printf(" ");
|
|
118 i--;
|
|
119 }
|
|
120 continue;
|
|
121 }
|
|
122 buffer[i] = c;
|
|
123 printf("%c", c);
|
|
124 i++;
|
|
125 }
|
|
126 buffer[i] = 0;
|
|
127 }
|
|
128
|
|
129 /*
|
|
130 malloc and free divide the chunks of memory present at the heap
|
|
131 of the kernel into smaller parts.
|
|
132 The heap is located at: 0x
|
|
133 */
|
|
134 static void* kernel_heap = (void*) 0xD0000000;
|
|
135 /* Allocates 'size' bytes and returns the pointer if succesfull.
|
|
136 Kernelpanic in case of failure..
|
|
137 */
|
17
|
138 void* malloc(uint64_t size) {
|
9
|
139 printf("Malloc %d bytes\n", size);
|
|
140 return kernel_heap;
|
|
141 }
|
|
142
|
|
143 void free(void* ptr) {
|
|
144 printf("Free address %x\n", ptr);
|
|
145 }
|
|
146
|
|
147 void kmain()
|
|
148 {
|
|
149 init_screen();
|
17
|
150 printf("Welcome! .. ");
|
14
|
151
|
17
|
152 printf("Enabling interrupts .. ");
|
14
|
153 setupIDT();
|
9
|
154 printf("Entering mainloop!\n");
|
|
155
|
|
156 while (1==1)
|
|
157 {
|
|
158 char buffer[70];
|
|
159 printf(">>>");
|
|
160 getline(buffer, 70);
|
|
161 // TODO: interpret this line with python :)
|
|
162 printf("\n");
|
|
163 printf("Got line: '%s'\n", buffer);
|
|
164 if (buffer[0] == 'x')
|
|
165 {
|
|
166 printf("System time in ms: %d\n", getTimeMS());
|
|
167 }
|
|
168 if ( strncmp(buffer, "help", 4))
|
|
169 {
|
|
170 printf("Help\n Try one of these commands:\n");
|
|
171 printf(" x: print system time in ms\n");
|
|
172 }
|
|
173 }
|
|
174 }
|
|
175
|
|
176
|