summaryrefslogtreecommitdiff
path: root/kernel/kernel.c
blob: 7c5f982787f68f83948de6e7fbf89391dc1eebd6 (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
#include "kernel.h"	// general kernel config & includes
#include "console.h"	// this will allow us to write to screen

// TODO: cleanup . how can i compile it without the includes!??

///////

void int_kb_handler();
void int_clock_handler();
////////// KERNEL MAIN///// /////
//

// test handler
void int_test_handler()
{
    __asm__("pusha");

    scr_put_string("inside software interrupt handler 88");

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

}

void kernel_main()
{
    
    // clear console
    scr_clear();

    // hello message
    scr_put_string_nl(KERNEL_HELLO_MESSAGE);
    
    //pit config
    timer_init();
    scr_put_string_nl("Configured PIT Channel 0 : Mode 2 : 1/25 s.");

    /*
    //probe memory
    scr_put_string_nl("Probing core memory...");
    char *prober=0xf000;

    for(;prober<0xfffffff;prober+=0xf00)
    {
	*prober='x';
	if(*prober!='x')break;
    
    }

    
    //0xA5000
    scr_put_hex((uint32_t)prober>>16);
    scr_put_hex((uint32_t)prober&0xffff);
    scr_put_string_nl(" finished!");
    //

    */

    uint16_t *memmap=0x7c00+0x120;
    uint32_t avail_mem=0;

    //print memory map:
    
    while(1)
    {
	if(memmap[8]==0)break;
	if(memmap[8]==1)
	{
	    avail_mem+=memmap[4]+(memmap[5]<<16); 
	}
	//bytes: 8 8 4 4// 
	//scr_put_hex(memmap[3]);
	//scr_put_string(" ");
	//scr_put_hex(memmap[2]);
	//scr_put_string(" ");
	scr_put_hex(memmap[1]);
	scr_put_string(" ");
	scr_put_hex(memmap[0]);
	scr_put_string(" ");
//	scr_put_hex(memmap[4]);
//	scr_put_string(" ");
//	scr_put_hex(memmap[5]);
//	scr_put_string(" ");
//	scr_put_hex(memmap[6]);
//	scr_put_string(" ");
//	scr_put_hex(memmap[7]);
	

	scr_put_string(" - ");
//	scr_put_hex(memmap[8]);
//	scr_put_string(" ");
//	scr_put_hex(memmap[9]);
//	scr_put_string(" ");
//	scr_put_hex(memmap[10]);
//	scr_put_string(" ");
//	scr_put_hex(memmap[11]);
//	scr_put_string(" ");
	//scr_put_hex(memmap[7]);
	//scr_put_string(" ");
	//scr_put_hex(memmap[6]);
	//scr_put_string(" ");
	scr_put_hex(memmap[5]);
	scr_put_string(" ");
	scr_put_hex(memmap[4]);

	scr_put_string(" : ");
	scr_put_hex(memmap[8]);
//	scr_put_string(" ");
//	scr_put_hex(memmap[9]);
	

	scr_put_string_nl("");

	memmap+=12;
    }

    
    scr_put_string("Total Available Mem: ");
    scr_put_hex(avail_mem>>16);
    scr_put_string(" ");
    scr_put_hex(avail_mem&0xffff);
    scr_put_string_nl(" byte");
    scr_put_string_nl("");





    // init and interrupt decriptor table 
    int_init(0x08);
    int_install();

    // setup custom interrupts
    // remember that we shifted all interrupts with the pic by 32
    // so clock = 32 (irq 0)
    // keyboard = 22 (irq 1)
    // etc..
    
    // install PIT interrupt handler
    int_install_ir(32, 0b10001110, 0x08,&int_clock_handler);

    // install keyboard interrupt handler
    int_install_ir(33, 0b10001110, 0x08,&int_kb_handler);

    // install test software interrupt handler
    int_install_ir(88, 0b10001110, 0x08,&int_test_handler);

    // now we can enable interrupts back again
    int_enable();
    scr_put_string_nl("Interrupts are up and running");

    //init shell
    shell_init();

    // kernel main loop
    while(1)
    {

	
    }
 
}