Advertisement
Guest User

Test-case kernel

a guest
Nov 18th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1.  
  2. #include <stddef.h>
  3. #include <stdint.h>
  4.  
  5. // kernel.cpp
  6.  
  7. char *vidptr = (char*)0xb8000; //video mem begins here.
  8. unsigned int current_loc = 0;
  9.  
  10. void print(const char* str)
  11. {
  12. unsigned int j = 0;
  13.  
  14. /* this loop writes the string to video memory */
  15. while(str[j] != '\0') {
  16. /* the character's ascii */
  17. vidptr[current_loc] = str[j];
  18. /* attribute-byte: give character black bg and light grey fg */
  19. vidptr[current_loc+1] = 0x07;
  20. ++j;
  21. current_loc += 2;
  22. }
  23. }
  24.  
  25. void port_out8(uint16_t _port, uint8_t _value)
  26. {
  27. asm volatile ("outb %0, %1" : : "a"(_value), "Nd"(_port));
  28. }
  29.  
  30. void port_out16(uint16_t _port, uint16_t _value)
  31. {
  32. asm volatile ("outw %0, %1" : : "a"(_value), "Nd"(_port));
  33. }
  34.  
  35. void port_out32(uint16_t _port, uint32_t _value)
  36. {
  37. asm volatile ("outl %0, %1" : : "a"(_value), "Nd"(_port));
  38. }
  39.  
  40. uint8_t port_in8(uint16_t _port)
  41. {
  42. uint8_t ret;
  43. asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(_port));
  44. return ret;
  45. }
  46.  
  47. uint16_t port_in16(uint16_t _port)
  48. {
  49. uint16_t ret;
  50. asm volatile ("inw %1, %0" : "=a"(ret) : "Nd"(_port));
  51. return ret;
  52. }
  53.  
  54. uint32_t port_in32(uint16_t _port)
  55. {
  56. uint32_t ret;
  57. asm volatile ("inl %1, %0" : "=a"(ret) : "Nd"(_port));
  58. return ret;
  59. }
  60.  
  61. #define write_port port_out8
  62. #define read_port port_in8
  63.  
  64. void kb_init(void)
  65. {
  66. /* 0xFD is 11111101 - enables only IRQ1 (keyboard)*/
  67. write_port(0x21 , 0xFD);
  68. print("Keyboard enabled");
  69. }
  70.  
  71.  
  72. struct IDT_entry
  73. {
  74. unsigned short int offset_lowerbits;
  75. unsigned short int selector;
  76. unsigned char zero;
  77. unsigned char type_attr;
  78. unsigned short int offset_higherbits;
  79. };
  80.  
  81. #define IDT_SIZE 256
  82. IDT_entry IDT[IDT_SIZE];
  83.  
  84. extern "C" void keyboard_handler();
  85.  
  86. asm volatile("keyboard_handler: \n call keyboard_handler_main \n iret");
  87.  
  88. unsigned long idt_ptr[2];
  89.  
  90. void idt_init(void)
  91. {
  92. unsigned long keyboard_address;
  93. unsigned long idt_address;
  94.  
  95. /* populate IDT entry of keyboard's interrupt */
  96. keyboard_address = (unsigned long)keyboard_handler;
  97. IDT[0x21].offset_lowerbits = keyboard_address & 0xffff;
  98. IDT[0x21].selector = 0x08; /* KERNEL_CODE_SEGMENT_OFFSET */
  99. IDT[0x21].zero = 0;
  100. IDT[0x21].type_attr = 0x8e; /* INTERRUPT_GATE */
  101. IDT[0x21].offset_higherbits = (keyboard_address & 0xffff0000) >> 16;
  102.  
  103.  
  104. /* Ports
  105. * PIC1 PIC2
  106. *Command 0x20 0xA0
  107. *Data 0x21 0xA1
  108. */
  109.  
  110. /* ICW1 - begin initialization */
  111. write_port(0x20 , 0x11);
  112. write_port(0xA0 , 0x11);
  113.  
  114. /* ICW2 - remap offset address of IDT */
  115. /*
  116. * In x86 protected mode, we have to remap the PICs beyond 0x20 because
  117. * Intel have designated the first 32 interrupts as "reserved" for cpu exceptions
  118. */
  119. write_port(0x21 , 0x20);
  120. write_port(0xA1 , 0x28);
  121.  
  122. /* ICW3 - setup cascading */
  123. write_port(0x21 , 0x00);
  124. write_port(0xA1 , 0x00);
  125.  
  126. /* ICW4 - environment info */
  127. write_port(0x21 , 0x01);
  128. write_port(0xA1 , 0x01);
  129. /* Initialization finished */
  130.  
  131. /* mask interrupts */
  132. write_port(0x21 , 0xff);
  133. write_port(0xA1 , 0xff);
  134.  
  135. /* fill the IDT descriptor */
  136. idt_address = (unsigned long)IDT ;
  137. idt_ptr[0] = (sizeof (struct IDT_entry) * IDT_SIZE) + ((idt_address & 0xffff) << 16);
  138. idt_ptr[1] = idt_address >> 16 ;
  139.  
  140. //load_idt(idt_ptr);
  141. asm volatile ("lidt ((idt_ptr)) \n sti");
  142. print("Initiated IDT");
  143. }
  144.  
  145. extern "C" void kmain(void)
  146. {
  147. const char *str = "Welcome to Luna!";
  148.  
  149. /* this loops clears the screen
  150. * there are 25 lines each of 80 columns; each element takes 2 bytes */
  151. int j = 0;
  152. while(j < 80 * 25 * 2) {
  153. /* blank character */
  154. vidptr[j] = ' ';
  155. /* attribute-byte - light grey on black screen */
  156. vidptr[j+1] = 0x07;
  157. j = j + 2;
  158. }
  159.  
  160. print(str);
  161.  
  162. idt_init();
  163. kb_init();
  164.  
  165. while (true) { asm volatile ("hlt"); }
  166.  
  167. return;
  168. }
  169.  
  170.  
  171. extern "C" void keyboard_handler_main(void) {
  172. unsigned char status;
  173. char keycode;
  174.  
  175. /* write EOI */
  176. write_port(0x20, 0x20);
  177.  
  178. //print("K");
  179.  
  180. status = read_port(0x60);//KEYBOARD_STATUS_PORT);
  181. /* Lowest bit of status will be set if buffer is not empty */
  182. if (status & 0x01 || true) {
  183. keycode = read_port(0x60);//KEYBOARD_DATA_PORT);
  184. if(keycode < 0)
  185. return;
  186. vidptr[current_loc++] = "!!1234567890-=!!qwertyuiop[]!!asdfghjkl;'#!\\zxcvbnm,./!!! !FFFFFFFFFF!"[keycode];//'a';// + keycode;//keyboard_map[keycode];
  187. vidptr[current_loc++] = 0x07;
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement