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