My crappy terminal OS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

92 lines
2.6 KiB

  1. #include "screen.h"
  2. #include "../cpu/ports.h"
  3. #include "../libc/mem.h"
  4. #include "../util/font.h"
  5. u32 VIDEO_ADDRESS = 0;
  6. u8 COLOR_BITS = 0;
  7. u16 SCN_WIDTH;
  8. u16 SCN_HEIGHT;
  9. u16 SCN_PITCH;
  10. /* Declaration of private functions */
  11. int get_cursor_offset();
  12. void set_cursor_offset(int offset);
  13. int print_char(char c, int col, int row, char attr);
  14. int get_offset(int col, int row);
  15. int get_offset_row(int offset);
  16. int get_offset_col(int offset);
  17. /**********************************************************
  18. * Public Kernel API functions *
  19. **********************************************************/
  20. void putpixel(int x, int y, u32 fg) {
  21. unsigned where = x*COLOR_BITS/8 + y*SCN_PITCH; //TODO: Should come from above function
  22. u8 *screen = (u8*) VIDEO_ADDRESS;
  23. //TODO: Is there a way to set these all at once?
  24. for (int j = 0; j < COLOR_BITS/8; j++) {
  25. screen[where + j] = (fg >> j*8) & 255;
  26. }
  27. }
  28. void fillrect(int x, int y, u32 fg, int w, int h) {
  29. unsigned where = x*COLOR_BITS/8 + y*SCN_PITCH;
  30. u8 *screen = (u8*) VIDEO_ADDRESS;
  31. int i, j;
  32. for (i = 0; i < h; i++) {
  33. for (j = 0; j < w; j++) {
  34. //TODO: Is there a way to set these all at once?
  35. for (int k = 0; k < COLOR_BITS/8; k++) {
  36. screen[where + (int)(j*COLOR_BITS/8) + k] = (fg >> k*8) & 255;
  37. }
  38. }
  39. where += SCN_PITCH;
  40. }
  41. }
  42. void scroll_up(u8 lines) { //NOTE: Does not clear the bottom lines!
  43. memory_copy((u8 *)(VIDEO_ADDRESS + lines * SCN_PITCH), (u8 *)VIDEO_ADDRESS, (SCN_HEIGHT - lines) * SCN_PITCH);
  44. }
  45. u32 color_to_pixel(Color_t color)
  46. {
  47. //32bpp max
  48. //Scale the values properly
  49. u8 r = color.r * ((1 << ADV_VBE_INFO->red_mask) - 1) / 255;
  50. u8 g = color.g * ((1 << ADV_VBE_INFO->green_mask) - 1) / 255;
  51. u8 b = color.b * ((1 << ADV_VBE_INFO->blue_mask) - 1) / 255;
  52. return (b << ADV_VBE_INFO->blue_position) +
  53. (g << ADV_VBE_INFO->green_position) +
  54. (r << ADV_VBE_INFO->red_position);
  55. }
  56. /**********************************************************
  57. * Private kernel functions *
  58. **********************************************************/
  59. /**
  60. * Innermost print function for our kernel, directly accesses the video memory
  61. *
  62. * If 'col' and 'row' are negative, we will print at current cursor location
  63. * If 'attr' is zero it will use 'white on black' as default
  64. * Returns the offset of the next character
  65. * Sets the video cursor to the returned offset
  66. */
  67. void setup_video() {
  68. struct vbe_mode_info_structure *vbe_info = (struct vbe_mode_info_structure *)0x7e00;
  69. ADV_VBE_INFO = vbe_info;
  70. VIDEO_ADDRESS = vbe_info->framebuffer;
  71. COLOR_BITS = vbe_info->bpp;
  72. SCN_WIDTH = vbe_info->width;
  73. SCN_HEIGHT = vbe_info->height;
  74. SCN_PITCH = vbe_info->pitch;
  75. }