console.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Copyright (C) 2013 by John Cronin <jncronin@tysos.org>
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. * THE SOFTWARE.
  18. */
  19. #include <string.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include "fb.h"
  23. #include "console.h"
  24. #include "util.h"
  25. #ifdef ENABLE_DEFAULT_FONT
  26. extern uint8_t vgafont8[];
  27. #define CHAR_W 8
  28. #define CHAR_H 8
  29. #define FONT vgafont8
  30. #endif
  31. #ifdef ENABLE_ALTERNATIVE_FONT
  32. #ifdef ENABLE_DEFAULT_FONT
  33. #error Cannot enable both the default and alternative fonts
  34. #endif
  35. #define FONT altfont
  36. extern uint8_t FONT[];
  37. #define CHAR_W ALTERNATIVE_FONT_W
  38. #define CHAR_H ALTERNATIVE_FONT_H
  39. #ifdef ALTERNATIVE_FONT_LSB_LEFT
  40. #define FONT_LSB_LEFT
  41. #endif
  42. #endif
  43. #ifndef FONT
  44. #error No console font is defined! Please see config.h
  45. #endif
  46. // Some fonts are encoded that the least significant byte is to the left, others have it to the right
  47. #ifdef FONT_LSB_LEFT
  48. #define BIT_SHIFT (s_bit_no)
  49. #else
  50. #define BIT_SHIFT (7 - s_bit_no)
  51. #endif
  52. #define DEF_FORE 0xffffffff
  53. #define DEF_BACK 0x00000000
  54. static int cur_x = 0;
  55. static int cur_y = 0;
  56. static uint32_t cur_fore = DEF_FORE;
  57. static uint32_t cur_back = DEF_BACK;
  58. void clear()
  59. {
  60. int height = fb_get_height();
  61. int pitch = fb_get_pitch();
  62. int line_byte_width = fb_get_width() * (fb_get_bpp() >> 3);
  63. uint8_t *fb = (uint8_t *)fb_get_framebuffer();
  64. for(int line = 0; line < height; line++)
  65. memset(&fb[line * pitch], 0, line_byte_width);
  66. cur_x = 0;
  67. cur_y = 0;
  68. }
  69. #ifdef FONT
  70. void newline()
  71. {
  72. cur_y++;
  73. cur_x = 0;
  74. // Scroll up if necessary
  75. if(cur_y == fb_get_height() / CHAR_H)
  76. {
  77. uint8_t *fb = (uint8_t *)fb_get_framebuffer();
  78. int line_byte_width = fb_get_width() * (fb_get_bpp() >> 3);
  79. int pitch = fb_get_pitch();
  80. int height = fb_get_height();
  81. for(int line = 0; line < (height - CHAR_H); line++)
  82. quick_memcpy(&fb[line * pitch], &fb[(line + CHAR_H) * pitch], line_byte_width);
  83. for(int line = height - CHAR_H; line < height; line++)
  84. memset(&fb[line * pitch], 0, line_byte_width);
  85. cur_y--;
  86. }
  87. }
  88. int console_putc(int c)
  89. {
  90. int line_w = fb_get_width() / CHAR_W;
  91. if(c == '\n')
  92. newline();
  93. else
  94. {
  95. draw_char((char)c, cur_x, cur_y, cur_fore, cur_back);
  96. cur_x++;
  97. if(cur_x == line_w)
  98. newline();
  99. }
  100. return c;
  101. }
  102. void draw_char(char c, int x, int y, uint32_t fore, uint32_t back)
  103. {
  104. volatile uint8_t *fb = (uint8_t *)fb_get_framebuffer();
  105. int bpp = fb_get_bpp();
  106. int bytes_per_pixel = bpp >> 3;
  107. int d_offset = y * CHAR_H * fb_get_pitch() + x * bytes_per_pixel * CHAR_W;
  108. int line_d_offset = d_offset;
  109. int s_offset = (int)c * CHAR_W * CHAR_H;
  110. for(int c_y = 0; c_y < CHAR_H; c_y++)
  111. {
  112. d_offset = line_d_offset;
  113. for(int c_x = 0; c_x < CHAR_W; c_x++)
  114. {
  115. int s_byte_no = s_offset / 8;
  116. int s_bit_no = s_offset % 8;
  117. uint8_t s_byte = FONT[s_byte_no];
  118. uint32_t colour = back;
  119. if((s_byte >> BIT_SHIFT) & 0x1)
  120. colour = fore;
  121. for(int i = 0; i < bytes_per_pixel; i++)
  122. {
  123. fb[d_offset + i] = (uint8_t)(colour & 0xff);
  124. colour >>= 8;
  125. }
  126. d_offset += bytes_per_pixel;
  127. s_offset++;
  128. }
  129. line_d_offset += fb_get_pitch();
  130. }
  131. }
  132. #endif