/ src / impl / x86_64 / print.c
print.c
 1  #include "print.h"
 2  
 3  const static size_t NUM_COLS = 80;
 4  const static size_t NUM_ROWS = 25;
 5  
 6  struct Char {
 7    uint8_t character;
 8    uint8_t color;
 9  };
10  
11  struct Char *buffer = (struct Char *)0xb8000;
12  size_t col = 0;
13  size_t row = 0;
14  uint8_t color = PRINT_COLOR_WHITE | PRINT_COLOR_BLACK << 4;
15  
16  void clear_row(size_t row) {
17    struct Char empty = (struct Char){
18      character : ' ',
19      color : color,
20    };
21  
22    for (size_t col = 0; col < NUM_COLS; col++) {
23      buffer[col + NUM_COLS * row] = empty;
24    }
25  }
26  
27  void print_clear() {
28    for (size_t i = 0; i < NUM_ROWS; i++) {
29      clear_row(i);
30    }
31  }
32  
33  void print_newline();
34  
35  void print_char(char character) {
36    if (character == '\n') {
37      print_newline();
38      return;
39    }
40  
41    if (col > NUM_COLS) {
42      print_newline();
43    }
44  
45    buffer[col + NUM_COLS] = (struct Char){
46      character : (uint8_t)character,
47      color : color,
48    };
49  
50    col++;
51  }
52  
53  void print_newline() {
54    col = 0;
55  
56    if (row < NUM_ROWS - 1) {
57      row++;
58      return;
59    }
60  
61    for (size_t row = 1; row < NUM_ROWS; row++) {
62      for (size_t col = 0; col < NUM_COLS; col++) {
63        struct Char character = buffer[col + NUM_COLS * row];
64        buffer[col + NUM_COLS * (row - 1)] = character;
65      }
66    }
67  
68    clear_row(NUM_COLS - 1);
69  }
70  
71  void print_str(char *str) {
72    for (size_t i = 0; 1; i++) {
73      char character = (uint8_t)str[i];
74  
75      if (character == '\0') {
76        return;
77      }
78  
79      print_char(character);
80    }
81  }
82  
83  void print_set_color(uint8_t foreground, uint8_t background) {
84    color = foreground + (background << 4);
85  }