Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stddef.h>
- #include <stdint.h>
- // kernel.cpp
- char *vidptr = (char*)0xb8000; //video mem begins here.
- unsigned int current_loc = 0;
- void print(const char* str)
- {
- unsigned int j = 0;
- /* this loop writes the string to video memory */
- while(str[j] != '\0') {
- /* the character's ascii */
- vidptr[current_loc] = str[j];
- /* attribute-byte: give character black bg and light grey fg */
- vidptr[current_loc+1] = 0x07;
- ++j;
- current_loc += 2;
- }
- }
- void port_out8(uint16_t _port, uint8_t _value)
- {
- asm volatile ("outb %0, %1" : : "a"(_value), "Nd"(_port));
- }
- void port_out16(uint16_t _port, uint16_t _value)
- {
- asm volatile ("outw %0, %1" : : "a"(_value), "Nd"(_port));
- }
- void port_out32(uint16_t _port, uint32_t _value)
- {
- asm volatile ("outl %0, %1" : : "a"(_value), "Nd"(_port));
- }
- uint8_t port_in8(uint16_t _port)
- {
- uint8_t ret;
- asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(_port));
- return ret;
- }
- uint16_t port_in16(uint16_t _port)
- {
- uint16_t ret;
- asm volatile ("inw %1, %0" : "=a"(ret) : "Nd"(_port));
- return ret;
- }
- uint32_t port_in32(uint16_t _port)
- {
- uint32_t ret;
- asm volatile ("inl %1, %0" : "=a"(ret) : "Nd"(_port));
- return ret;
- }
- #define write_port port_out8
- #define read_port port_in8
- void kb_init(void)
- {
- /* 0xFD is 11111101 - enables only IRQ1 (keyboard)*/
- write_port(0x21 , 0xFD);
- print("Keyboard enabled");
- }
- struct IDT_entry
- {
- unsigned short int offset_lowerbits;
- unsigned short int selector;
- unsigned char zero;
- unsigned char type_attr;
- unsigned short int offset_higherbits;
- };
- #define IDT_SIZE 256
- IDT_entry IDT[IDT_SIZE];
- extern "C" void keyboard_handler();
- asm volatile("keyboard_handler: \n call keyboard_handler_main \n iret");
- unsigned long idt_ptr[2];
- void idt_init(void)
- {
- unsigned long keyboard_address;
- unsigned long idt_address;
- /* populate IDT entry of keyboard's interrupt */
- keyboard_address = (unsigned long)keyboard_handler;
- IDT[0x21].offset_lowerbits = keyboard_address & 0xffff;
- IDT[0x21].selector = 0x08; /* KERNEL_CODE_SEGMENT_OFFSET */
- IDT[0x21].zero = 0;
- IDT[0x21].type_attr = 0x8e; /* INTERRUPT_GATE */
- IDT[0x21].offset_higherbits = (keyboard_address & 0xffff0000) >> 16;
- /* Ports
- * PIC1 PIC2
- *Command 0x20 0xA0
- *Data 0x21 0xA1
- */
- /* ICW1 - begin initialization */
- write_port(0x20 , 0x11);
- write_port(0xA0 , 0x11);
- /* ICW2 - remap offset address of IDT */
- /*
- * In x86 protected mode, we have to remap the PICs beyond 0x20 because
- * Intel have designated the first 32 interrupts as "reserved" for cpu exceptions
- */
- write_port(0x21 , 0x20);
- write_port(0xA1 , 0x28);
- /* ICW3 - setup cascading */
- write_port(0x21 , 0x00);
- write_port(0xA1 , 0x00);
- /* ICW4 - environment info */
- write_port(0x21 , 0x01);
- write_port(0xA1 , 0x01);
- /* Initialization finished */
- /* mask interrupts */
- write_port(0x21 , 0xff);
- write_port(0xA1 , 0xff);
- /* fill the IDT descriptor */
- idt_address = (unsigned long)IDT ;
- idt_ptr[0] = (sizeof (struct IDT_entry) * IDT_SIZE) + ((idt_address & 0xffff) << 16);
- idt_ptr[1] = idt_address >> 16 ;
- //load_idt(idt_ptr);
- asm volatile ("lidt ((idt_ptr)) \n sti");
- print("Initiated IDT");
- }
- extern "C" void kmain(void)
- {
- const char *str = "Welcome to Luna!";
- /* this loops clears the screen
- * there are 25 lines each of 80 columns; each element takes 2 bytes */
- int j = 0;
- while(j < 80 * 25 * 2) {
- /* blank character */
- vidptr[j] = ' ';
- /* attribute-byte - light grey on black screen */
- vidptr[j+1] = 0x07;
- j = j + 2;
- }
- print(str);
- idt_init();
- kb_init();
- while (true) { asm volatile ("hlt"); }
- return;
- }
- extern "C" void keyboard_handler_main(void) {
- unsigned char status;
- char keycode;
- /* write EOI */
- write_port(0x20, 0x20);
- //print("K");
- status = read_port(0x60);//KEYBOARD_STATUS_PORT);
- /* Lowest bit of status will be set if buffer is not empty */
- if (status & 0x01 || true) {
- keycode = read_port(0x60);//KEYBOARD_DATA_PORT);
- if(keycode < 0)
- return;
- vidptr[current_loc++] = "!!1234567890-=!!qwertyuiop[]!!asdfghjkl;'#!\\zxcvbnm,./!!! !FFFFFFFFFF!"[keycode];//'a';// + keycode;//keyboard_map[keycode];
- vidptr[current_loc++] = 0x07;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement