kernel

hobby kernel
git clone git://git.evenfri.xyz/kernel.git
Log | Files | Refs | README | LICENSE

tty.c (2013B)


      1 #include <dat.h>
      2 #include <fns.h>
      3 
      4 size_t tty_row;
      5 size_t tty_column;
      6 uint8_t tty_color;
      7 uint16_t* tty_buffer = (uint16_t*)vga_start;
      8 
      9 void tty_set_color(uint8_t color) {
     10   tty_color = color;
     11 }
     12 
     13 void tty_putentryat(char c, uint8_t color, size_t x, size_t y) {
     14   const size_t index = y * vga_width + x;
     15   tty_buffer[index] = vga_entry(c, color);
     16 }
     17 
     18 void tty_clear(void) {
     19   tty_row = 0;
     20   tty_column = 0;
     21 
     22   for (size_t y = 0; y < vga_height; y++) {
     23     for (size_t x = 0; x < vga_width; x++) {
     24       const size_t index = y * vga_width + x;
     25       tty_buffer[index] = vga_entry(' ', tty_color);
     26     }
     27   }
     28   vga_cursor_update(tty_column, tty_row);
     29 }
     30 
     31 void tty_init(void) {
     32   vga_cursor_enable(14, 15);
     33   tty_color = vga_entry_color(vga_color_light_grey, vga_color_black);
     34   tty_clear();
     35 }
     36 
     37 void tty_scroll(void) {
     38   /* move lines to line-1 */
     39   for (int row = 1; row < vga_height; row++) {
     40     for (int col = 0; col < vga_width; col++) {
     41       size_t src_index = row * vga_width + col;
     42       size_t dst_index = (row - 1) * vga_width + col;
     43       tty_buffer[dst_index] = tty_buffer[src_index];
     44     }
     45   }
     46   /* clear last line*/
     47   uint16_t blank = ' ' | (tty_color << 8);
     48   for (int col = 0; col < vga_width; col++) {
     49     size_t index = (vga_height - 1) * vga_width + col;
     50     tty_buffer[index] = blank;
     51   }
     52 }
     53 
     54 void tty_putchar(char c) {
     55   if(c == '\n') {
     56     tty_row++;
     57     tty_column = 0;
     58 
     59     if (tty_row >= vga_height) {
     60       tty_scroll();
     61       tty_row = vga_height - 1;
     62     }
     63   } else if(c == '\b') {
     64     if (tty_column > 0) {
     65       tty_column--;
     66       tty_putentryat(' ', tty_color, tty_column, tty_row);
     67     }
     68   } else {
     69     tty_putentryat(c, tty_color, tty_column, tty_row);
     70     if (++tty_column == vga_width) {
     71       tty_column = 0;
     72       if (++tty_row == vga_height) {
     73         tty_scroll();
     74         tty_row = vga_height - 1;
     75       }
     76     }
     77   }
     78 
     79   vga_cursor_update(tty_column, tty_row);
     80 }
     81 
     82 void tty_write(const char* data, size_t size) {
     83   for(size_t i = 0; i < size; i++) {
     84     tty_putchar(data[i]);
     85   }
     86 }
     87