summaryrefslogtreecommitdiff
path: root/kernel/kernel.c
blob: bdf1ec182aa09edb3d34865bfaf045e262ece465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

#include <stdint.h>
#include "kernel.h"	// general kernel config
#include "console.h" // this will allow us to write to screen

// garbage
int unhandled=0;

char col=100;
char col2=0;

int cursor=0;
char last_code=0;

uint8_t kb_in;

//// interrupt stuff /////
//
#define INT_MAX 255
//

// interrupt descriptor table
static struct int_desc
{
    uint16_t addrLo;
    uint16_t sel;
    uint8_t zeros;
    uint8_t flags;
    uint16_t addrHi;

} idt[INT_MAX];

// interrupt descriptor table descriptor
static struct idt_desc
{
    uint16_t size;
    uint16_t baseLo;
    uint16_t baseHi;

} idtd;

void int_def_handler()
{
    __asm__("pusha");

    unhandled++;

    // todo also the other pic!// TODO
    __asm__("mov $0x20, %al");
    __asm__("out %al, $0x20");

    __asm__("popa");
    __asm__("leave");
    __asm__("iret");

}

void int_kb_handler()
{
    __asm__("pusha");

    __asm__("in $0x60, %al");
    __asm__("mov %%al, %0"::"m" (kb_in));

    int0(kb_in); //TODO!!

    __asm__("mov $0x20, %al");
    __asm__("out %al, $0x20");

    __asm__("popa");
    __asm__("leave");
    __asm__("iret");

}

void int_init(uint16_t sel)
{
    int i;

    for(i=0; i<INT_MAX; i++)
    {
	if(i==33)int_install_ir(i, 0b10001110, sel,&int_kb_handler);
	else int_install_ir(i, 0b10001110, sel,&int_def_handler);
    }

}

void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr)
{

    uint64_t base=(uint64_t)&(*addr);

    idt[irq].addrLo = base & 0xffff;
    idt[irq].addrHi = (base >> 16) & 0xffff;
    idt[irq].addrHi =  0x0;
    idt[irq].zeros=0;
    idt[irq].flags=flags;
    idt[irq].sel=sel;

}


void int_install()
{

    idtd.size=8*34;
    idtd.baseHi=0x0000;
    idtd.baseLo=&idt[0];

    __asm__("lidt %0"::"m" (idtd));

}
/*
void int_show()
{
    uint16_t *ptr3=&idt[0];

    print_string("idt located: ");
    print_hex(ptr3);
    print_nextline();


    // first two bytes as chars
    int offset;
    for(offset=32;offset<40;offset++)
    {
	print_hex(offset-32); //irq
	print_string(" -> ");
	print_hex(*(ptr3+offset*4)); //addrLo
	print_hex(*(ptr3+1+offset*4)); //sel
	print_hex(*(ptr3+2+offset*4)); //zeros & flags
	print_hex(*(ptr3+3+offset*4)); //addrHi
	print_nextline();
    }
}
*/

void int_disable()
{
    __asm__("cli");
}

void int_enable()
{
    __asm__("sti");
}

////////// KERNEL MAIN///// /////
//
void kernel_main()
{

    // clear console
    scr_clear();

    // hello message
    scr_put_string(KERNEL_HELLO_MESSAGE);
    scr_nextline();
    scr_nextline();
    
    // init interrupt decriptor table 
    // install and enable!
    int_init(0x08);
    int_install();
    int_enable();

    scr_put_string("Interrupts Up and Running");
    scr_nextline();

    // kernel main loop
    while(1)
    {
	
//	print_nextline();

//	print_str_col(0,0,"ABC",col);
	print_str_col(1,SCR_HEIGHT,"(*)",col2++);

	//int0();
    }


 
}



/// keyboard driver ////

void int0(uint8_t in)
{
    int i=0;
    //
    //print_hex(int_count);

    uint8_t make_codes[]={
	0x1e,	// A
	0x30,	// B
	0x2e,	// C
	0x20,	// D
	0x12,	// E
	0x21,	// F
	0x22,	// G
	0x23,	// H
	0x17,	// I
	0x24,	// J
	0x25,	// K
	0x26,	// L
	0x32,	// M
	0x31,	// N
	0x18,	// O
	0x19,	// P
	0x10,	// Q
	0x13,	// R
	0x1F,	// S
	0x14,	// T
	0x16,	// U
	0x2F,	// V
	0x11,	// W
	0x2D,	// X
	0x15,	// Y
	0x2c,	// Z
	};

    uint8_t break_codes[]={ 
	0x9e,	// A
	0xb0,	// B
	0xae,	// C
	0xa0,	// D
	0x92,	// E
	0xa1,	// F
	0xa2,	// G
	0xa3,	// H
	0x97,	// I
	0xa4,	// J
	0xa5,	// K
	0xa6,	// L
	0xb2,	// M
	0xb1,	// N
	0x98,	// O
	0x99,	// P
	0x90,	// Q
	0x93,	// R
	0x9F,	// S
	0x94,	// T
	0x96,	// U
	0xaF,	// V
	0x91,	// W
	0xaD,	// X
	0x95,	// Y
	0xac,	// Z
	};

    //if(last_code==*int_count)return;

    for(i=0;i<26;i++)
    {
  	if(make_codes[i]==in)
	{
	    print_char_col(cursor,20,'A'+i,0xf);
	}
    }

    for(i=0;i<26;i++)
    {
  	if(break_codes[i]==in)
	{
	    print_char_col(cursor++,20,'a'+i,SCR_RED);
	}
    }

    last_code=in;

}