Advertisement
lfed255-dev

console.c

Oct 30th, 2019
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #include "console.h"
  2.  
  3. unsigned short* videoMemory = (unsigned short*)0xb8000;
  4.  
  5. int strs_count = 25;
  6. int strs_len = 80;
  7.  
  8. int dbcolor = 0;
  9. int dfcolor = WHITE_COLOR;
  10.  
  11. int cursorx = 0, cursory = 0;
  12.  
  13.  
  14. void __console_mdl_init(int s,int l,int b, int f) {
  15. if ( s > 0 )
  16. strs_count = s;
  17. if ( l > 0 )
  18. strs_len = l;
  19. if ( b > 0 )
  20. dbcolor = b;
  21. if ( f > 0 )
  22. dfcolor = f;
  23. __console_clear();
  24. //print_mod("console initialized",MOD_OK);
  25. }
  26.  
  27. void __console_movecto(int x,int y) {
  28. cursorx = x;
  29. cursory = y;
  30. __console_flushc();
  31. }
  32. void __console_movec(int x,int y) {
  33. cursorx += x;
  34. cursory += y;
  35. __console_flushc();
  36. }
  37.  
  38.  
  39. void __console_flushc() {
  40. uint16_t pos = ( cursory * strs_len ) + cursorx;
  41. outb(0x3D4, 14);
  42. outb(0x3D5, pos >> 8);
  43. outb(0x3D4, 15);
  44. outb(0x3D5, pos);
  45. }
  46.  
  47. void __console_clear() {
  48. uint8_t ab = (0 << 4) | (15 & 0x0F);
  49. uint16_t c = 0x20 | (ab << 8);
  50. for(int i = 0;i < strs_len * strs_count; i++)
  51. videoMemory[i] = c;
  52. cursorx = 0;
  53. cursory = 0;
  54. __console_flushc();
  55. }
  56.  
  57. void __console_flushs() {
  58. uint8_t at = ( dbcolor << 4 ) | ( dfcolor & 0x0F );
  59. uint16_t b = 0x20 | ( at << 8 );
  60.  
  61. if ( cursory >= strs_count ) {
  62. for ( int i = 0; i < ( strs_count-1 ) * strs_len; i++ ) {
  63. videoMemory[i] = videoMemory[i+strs_len];
  64. }
  65. for ( int i = ( strs_count - 1 ) * strs_len; i < strs_count * strs_len; i++ ) {
  66. videoMemory[i] = b;
  67. }
  68. cursory = strs_count - 1;
  69. }
  70. }
  71.  
  72. void __console_putchara(unsigned char c,uint8_t a) {
  73. uint16_t attr = a << 8;
  74. uint16_t *loc;
  75.  
  76.  
  77. if ( c == 0x08 && cursorx ) {
  78. --cursorx;
  79. }
  80. else if ( c == 0x09 ) {
  81. cursorx = (cursorx + 8) & ~(8-1);
  82. }
  83. else if ( c == '\r' ) {
  84. cursorx = 0;
  85. }
  86. else if ( c == '\n' ) {
  87. cursorx = 0;
  88. ++cursory;
  89. }
  90. else if ( c >= ' ') {
  91. loc = videoMemory + ( cursory*strs_len + cursorx );
  92. *loc = c | attr;
  93. ++cursorx;
  94. }
  95. if ( cursorx >= strs_len || cursorx == strs_len - 1) { // 79 80
  96. __console_putchara('\n',( dbcolor << 4 ) | ( dfcolor & 0x0F));
  97. // cursorx = 0;
  98. // ++cursory;
  99. }
  100. __console_flushs();
  101. __console_flushc();
  102. }
  103.  
  104.  
  105. void __console_putchar(unsigned char c) {
  106. __console_putchara(c, ( dbcolor << 4 ) | ( dfcolor & 0x0F));
  107. }
  108.  
  109. void __console_writea(const char* buf,uint8_t a) {
  110. int i = 0;
  111. while(buf[i]) {
  112. __console_putchara(buf[i],a);
  113. i++;
  114. }
  115. }
  116.  
  117. void __console_write(const char* buf) {
  118. __console_writea(buf,( dbcolor << 4 ) | ( dfcolor & 0x0F));
  119. }
  120.  
  121.  
  122.  
  123. void __console_write_hex(uint32_t v) {
  124. __console_write("0x");
  125. __console_write_int(v,16);
  126. }
  127.  
  128. void __console_write_int(uint32_t v, unsigned base)
  129. {
  130. int acc = v;
  131. char c[33], c2[33];
  132. int i = 0, j = 0;
  133.  
  134. if (v == 0 || base > 16)
  135. {
  136. __console_putchar('0');
  137. return;
  138. }
  139.  
  140. while (acc > 0)
  141. {
  142. c[i] = "0123456789abcdef"[acc % base];
  143. acc /= base;
  144. ++i;
  145. }
  146. c[i] = 0;
  147. c2[i--] = 0;
  148. while(i >= 0)
  149. c2[i--] = c[j++];
  150.  
  151. __console_write(c2);
  152. }
  153.  
  154. void __console_write_dec(uint32_t v) {
  155. __console_write_int(v, 10);
  156. }
  157.  
  158. void __console_writec(const char* buf,int8_t b,int8_t f) {
  159. if ( b < 0 ) {
  160. b = dbcolor;
  161. }
  162. if ( f < 0 ) {
  163. f = dfcolor;
  164. }
  165. __console_writea(buf,( b << 4 ) | (f & 0x0F ));
  166. }
  167.  
  168. void __console_putcharc(unsigned char c,int8_t b,int8_t f) {
  169. if ( b < 0 ) {
  170. b = dbcolor;
  171. }
  172. if ( f < 0 ) {
  173. f = dfcolor;
  174. }
  175. __console_putchara(c, ( b << 4 ) | ( f & 0x0F ));
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement