Advertisement
lylythechosenone

screen.c

Nov 13th, 2020
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include "screen.h"
  2. #include "ports.h"
  3.  
  4. int CURRENT_CURSOR_POSITION = 0;
  5.  
  6. void set_cursor_position(int offset);
  7. int get_cursor_position();
  8.  
  9. void clear() {
  10.     char* video_memory = (char*) VIDEO_ADDRESS;
  11.     int row = 0;
  12.     for(row = 0; row < TOTAL_COLS * TOTAL_ROWS; row++)
  13.     {
  14.         video_memory[row * 2] = ' ';
  15.         video_memory[row * 2 + 1] = 0x0f;
  16.     }
  17.     CURRENT_CURSOR_POSITION = 0x00;
  18.     set_cursor_position(CURRENT_CURSOR_POSITION);
  19. }
  20.  
  21. void printf(char *str) {
  22.     char* video_memory = (char*) VIDEO_ADDRESS;
  23.     int pos = 0;
  24.     int cursorPosition = get_cursor_position();
  25.     while(str[pos] != 0)
  26.     {
  27.         video_memory[cursorPosition] = str[pos++];
  28.         video_memory[cursorPosition + 1] = 0x0f;
  29.         cursorPosition = cursorPosition + 2;
  30.     }
  31.     set_cursor_position(cursorPosition);
  32. }
  33.  
  34. int get_cursor_position() {
  35.     port_byte_out(REG_SCREEN_CTRL, 14);
  36.     int position = port_byte_in(REG_SCREEN_DATA) << 8;
  37.  
  38.     port_byte_out(REG_SCREEN_CTRL, 15);
  39.     position += port_byte_in(REG_SCREEN_DATA);
  40.  
  41.     return position * 2;
  42. }
  43.  
  44. void set_cursor_position(int offset)
  45. {
  46.     offset /= 2;
  47.     port_byte_out(REG_SCREEN_CTRL, 14);
  48.     port_byte_out(REG_SCREEN_DATA, offset >> 8);
  49.  
  50.     port_byte_out(REG_SCREEN_CTRL, 15);
  51.     port_byte_out(REG_SCREEN_DATA, offset & 0xff);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement