Advertisement
irvan_herz

Screen code

Jun 25th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.46 KB | None | 0 0
  1. /* Ivion Kernel - The Future Free Kernel
  2.  * Copyright(C)2013 Muhammad Irvan H.
  3.  * Software ini boleh dimiliki, disebarkan, diubah dengan bebas.
  4.  * Dengan catatan:
  5.  * > Boleh menambah, namun dilarang mengubah komentar ini.
  6.  * > Hanya dipakai untuk edukasi dan non-profit(non-komersil).
  7.  * > Dilarang mendokumentasikan kode dalam bentuk buku atau semacamnya,
  8.  *   sebelum izin kepada kru Ivion. (Izin GRATIS)
  9.  */
  10.  
  11. #include "struct.h"
  12. #include "type.h"
  13.  
  14. char* current_video_mem = (char*)0xB8000;
  15. uchar current_col;
  16. uchar current_row;
  17. const uchar MAX_COL = 80;
  18. const uchar MAX_ROW = 25;
  19. const uint VIDEO_MEM_BASE = 0xB8000;
  20. const uchar BYTES_PER_CHAR = 2;
  21. ushort cursor_pos;
  22.  
  23. void move_cursor(short, short);
  24. void new_line();
  25.  
  26. /* *
  27.  * Print String from char * strings with attributes
  28.  * */
  29. void print_str(char* strings, uchar attributes)
  30. {
  31.     int i = 0;
  32.    
  33.     while(strings[i] != 0)
  34.     {
  35.         if (strings[i] == '\n')
  36.         {
  37.             new_line();
  38.             i++;
  39.         }
  40.         else
  41.         {
  42.             *current_video_mem = strings[i];
  43.             current_video_mem++;
  44.             *current_video_mem = attributes;
  45.             current_video_mem++;
  46.             i++;
  47.            
  48.             if (current_col == 79)
  49.             {
  50.                 new_line();
  51.             }
  52.             else
  53.             {
  54.                 current_col++;
  55.             }
  56.         }
  57.     }
  58.     move_cursor(current_col,current_row);
  59. }
  60.  
  61. /* *
  62.  * Print Single Character from char character, with attributes
  63.  * */
  64. void putch(char character, char attributes)
  65. {
  66.     char the_char[2];
  67.     the_char[0] = character;
  68.     the_char[1] = 0;
  69.     print_str(&the_char[0],attributes);
  70. }
  71.  
  72. /* *
  73.  * New Line with auto scrolling
  74.  * */  
  75. void new_line()
  76. {
  77.     int i;
  78.     char* location=(char*)VIDEO_MEM_BASE;
  79.    
  80.     if (current_row == 24)
  81.     {
  82.         for (i = 0; i <= 80*25*2; i++)
  83.         {
  84.             location[i] = location[i+80*2];
  85.         }
  86.         current_row = 24;
  87.         current_col = 0;
  88.        
  89.     }
  90.     else
  91.     {
  92.         current_row++;
  93.         current_col=0;
  94.     }
  95.     current_video_mem = (char*)VIDEO_MEM_BASE + ((current_row * 80 + current_col) * 2);
  96.     move_cursor(current_col,current_row);
  97. }
  98.  
  99. /* *
  100.  * Clear screen
  101.  * */
  102. void clear_scr()
  103. {
  104.     int i;
  105.     short* location = (short*)VIDEO_MEM_BASE;
  106.    
  107.     for(i=0; i<MAX_COL*MAX_ROW; i++)
  108.     {
  109.         *location = 0x0f00;
  110.         location++;
  111.        
  112.     }
  113.     move_cursor(0,0);
  114.        
  115. }
  116.  
  117. /* *
  118.  * Move caret/cursor to X, Y
  119.  * */
  120. void move_cursor(short x, short y)
  121. {
  122.     cursor_pos = (y * MAX_COL) + x;
  123.     asm (".intel_syntax noprefix");
  124.     asm ("mov al,14");
  125.     asm ("mov dx,0x3d4");
  126.     asm ("out dx,al");
  127.     asm ("mov ax,[cursor_pos]");
  128.     asm ("shr ax,8");
  129.     asm ("mov dx,0x3d5");
  130.     asm ("out dx,al");
  131.          
  132.     asm ("mov al,15");
  133.     asm ("mov dx,0x3d4");
  134.     asm ("out dx,al");
  135.     asm ("mov ax,[cursor_pos]");
  136.     asm ("mov dx, 0x3d5");
  137.     asm ("out dx,al");
  138.  
  139. }
  140.  
  141. void get_cursor_pos(cursor_position_struct* cursor_pos_struct)
  142. {
  143.     cursor_pos_struct->x = current_col;
  144.     cursor_pos_struct->y = current_row;
  145. }
  146.  
  147. /* *
  148.  * Convert Integer Number to Hexadecimal Number
  149.  * Number saved to char* buffer
  150.  * */
  151. char hexadecimal[9];
  152. void int_to_hex_str(uint number, char* buffer)
  153. {
  154.     int i,j=0;
  155.    
  156.     //process
  157.     for (i=0;number / 0x10 != 0; i++)
  158.     {
  159.         if(number % 0x10 > 9)
  160.         {
  161.             hexadecimal[i] = (number % 0x10) + 0x37;
  162.         }
  163.         else
  164.         {
  165.             hexadecimal[i] = (number % 0x10) + 0x30;
  166.         }
  167.         number /= 0x10;
  168.     }
  169.     if(number % 0x10 > 9)
  170.     {
  171.         hexadecimal[i] = (number % 0x10) + 0x37;
  172.     }
  173.     else
  174.     {
  175.         hexadecimal[i] = (number % 0x10) + 0x30;
  176.     }
  177.  
  178.     //save
  179.     while(i != 0)
  180.     {
  181.         buffer[j] = hexadecimal[i];
  182.         i--;
  183.         j++;
  184.     }
  185.     buffer[j] = hexadecimal[i];
  186.     buffer[j+1] = 0;
  187. }
  188.  
  189. /* *
  190.  * Convert Unsigned Integer Number to Decimal String
  191.  * Number saved to char* buffer
  192.  * */
  193.  char decimal[15];
  194. void uint_to_decimal_str(uint number, char* buffer)
  195. {
  196.     int i,j=0;
  197.    
  198.     //process
  199.     for (i=0;number / 10 != 0; i++)
  200.     {
  201.         decimal[i] = (number % 10) + 0x30;
  202.         number /= 10;
  203.     }
  204.     decimal[i] = number + 0x30;
  205.     //save
  206.     while(i != 0)
  207.     {
  208.         buffer[j] = decimal[i];
  209.         i--;
  210.         j++;
  211.     }
  212.     buffer[j] = decimal[i];
  213.     buffer[j+1] = 0;
  214. }  
  215.  
  216. /* *
  217.  * Convert Signed Integer Number to Decimal String
  218.  * Number saved to char* buffer
  219.  * */
  220. void sint_to_decimal_str(sint snumber, char* buffer)
  221. {
  222.     int i,j=0,number;
  223.     //compare
  224.     if(snumber < 0)
  225.     {
  226.         number = (snumber * -1);
  227.     }
  228.     else
  229.     {
  230.         number = snumber;
  231.     }
  232.    
  233.     //process
  234.     for (i=0;number / 10 != 0; i++)
  235.     {
  236.         decimal[i] = (number % 10) + 0x30;
  237.         number /= 10;
  238.     }
  239.     decimal[i] = number + 0x30;
  240.    
  241.     //set sign
  242.     if (snumber < 0)
  243.     {
  244.         buffer[j] = 0x2d;   //strip (-)
  245.         j++;
  246.     }
  247.     //save
  248.     while(i != 0)
  249.     {
  250.         buffer[j] = decimal[i];
  251.         i--;
  252.         j++;
  253.     }
  254.     buffer[j] = decimal[i];
  255.     buffer[j+1] = 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement