printk.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. //
  2. // Created by longjin on 2022/1/22.
  3. //
  4. #include "printk.h"
  5. #include "kprint.h"
  6. #include <driver/multiboot2/multiboot2.h>
  7. #include <mm/mm.h>
  8. #include <common/spinlock.h>
  9. #include <driver/uart/uart.h>
  10. #include <driver/video/video.h>
  11. #include "math.h"
  12. #pragma GCC push_options
  13. #pragma GCC optimize("O0")
  14. struct printk_screen_info pos;
  15. extern ul VBE_FB_phys_addr; // 由bootloader传来的帧缓存区的物理地址
  16. static spinlock_t printk_lock;
  17. static bool sw_show_scroll_animation = false; // 显示换行动画的开关
  18. /**
  19. * @brief Set the printk pos object
  20. *
  21. * @param x 列坐标
  22. * @param y 行坐标
  23. */
  24. static int set_printk_pos(const int x, const int y);
  25. /**
  26. * @brief 在屏幕上指定位置打印字符
  27. *
  28. * @param fb 帧缓存线性地址
  29. * @param Xsize 行分辨率
  30. * @param x 左上角列像素点位置
  31. * @param y 左上角行像素点位置
  32. * @param FRcolor 字体颜色
  33. * @param BKcolor 背景颜色
  34. * @param font 字符的bitmap
  35. */
  36. static void putchar(uint *fb, int Xsize, int x, int y, unsigned int FRcolor, unsigned int BKcolor, unsigned char font);
  37. static uint *get_pos_VBE_FB_addr();
  38. /**
  39. * @brief 清屏
  40. *
  41. */
  42. static int cls();
  43. /**
  44. * @brief 滚动窗口(尚不支持向下滚动)
  45. *
  46. * @param direction 方向,向上滑动为true,否则为false
  47. * @param pixels 要滑动的像素数量
  48. * @param animation 是否包含滑动动画
  49. */
  50. static int scroll(bool direction, int pixels, bool animation);
  51. /**
  52. * @brief 将数字按照指定的要求转换成对应的字符串(2~36进制)
  53. *
  54. * @param str 要返回的字符串
  55. * @param num 要打印的数值
  56. * @param base 基数
  57. * @param field_width 区域宽度
  58. * @param precision 精度
  59. * @param flags 标志位
  60. */
  61. static char *write_num(char *str, ul num, int base, int field_width, int precision, int flags);
  62. static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags);
  63. static int calculate_max_charNum(int len, int size)
  64. {
  65. /**
  66. * @brief 计算屏幕上能有多少行
  67. * @param len 屏幕长/宽
  68. * @param size 字符长/宽
  69. */
  70. return len / size - 1;
  71. }
  72. int printk_init(const int char_size_x, const int char_size_y)
  73. {
  74. struct multiboot_tag_framebuffer_info_t info;
  75. int reserved;
  76. multiboot2_iter(multiboot2_get_Framebuffer_info, &info, &reserved);
  77. pos.width = info.framebuffer_width;
  78. pos.height = info.framebuffer_height;
  79. pos.char_size_x = char_size_x;
  80. pos.char_size_y = char_size_y;
  81. pos.max_x = calculate_max_charNum(pos.width, char_size_x);
  82. pos.max_y = calculate_max_charNum(pos.height, char_size_y);
  83. VBE_FB_phys_addr = (ul)info.framebuffer_addr;
  84. pos.FB_address = (uint *)0xffff800003000000;
  85. pos.FB_length = 1UL * pos.width * pos.height;
  86. // 初始化自旋锁
  87. spin_init(&printk_lock);
  88. // ======== 临时的将物理地址填写到0x0000000003000000处 之后会在mm内将帧缓存区重新映射=====
  89. ul global_CR3 = (ul)get_CR3();
  90. ul fb_virt_addr = (ul)pos.FB_address;
  91. ul fb_phys_addr = VBE_FB_phys_addr;
  92. // 计算帧缓冲区的线性地址对应的pml4页表项的地址
  93. ul *tmp = phys_2_virt((ul *)((ul)global_CR3 & (~0xfffUL)) + ((fb_virt_addr >> PAGE_GDT_SHIFT) & 0x1ff));
  94. tmp = phys_2_virt((ul *)(*tmp & (~0xfffUL)) + ((fb_virt_addr >> PAGE_1G_SHIFT) & 0x1ff));
  95. ul *tmp1;
  96. // 初始化2M物理页
  97. for (ul i = 0; i < (pos.FB_length << 2); i += PAGE_2M_SIZE)
  98. {
  99. // 计算当前2M物理页对应的pdt的页表项的物理地址
  100. tmp1 = phys_2_virt((ul *)(*tmp & (~0xfffUL)) + (((fb_virt_addr + i) >> PAGE_2M_SHIFT) & 0x1ff));
  101. // 页面写穿,禁止缓存
  102. set_pdt(tmp1, mk_pdt((ul)fb_phys_addr + i, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD));
  103. }
  104. flush_tlb();
  105. pos.x = 0;
  106. pos.y = 0;
  107. cls();
  108. kdebug("width=%d\theight=%d", pos.width, pos.height);
  109. // 由于此时系统并未启用双缓冲,因此关闭滚动动画
  110. printk_disable_animation();
  111. return 0;
  112. }
  113. static int set_printk_pos(const int x, const int y)
  114. {
  115. // 指定的坐标不在屏幕范围内
  116. if (!((x >= 0 && x <= pos.max_x) && (y >= 0 && y <= pos.max_y)))
  117. return EPOS_OVERFLOW;
  118. pos.x = x;
  119. pos.y = y;
  120. return 0;
  121. }
  122. static int skip_and_atoi(const char **s)
  123. {
  124. /**
  125. * @brief 获取连续的一段字符对应整数的值
  126. * @param:**s 指向 指向字符串的指针 的指针
  127. */
  128. int ans = 0;
  129. while (is_digit(**s))
  130. {
  131. ans = ans * 10 + (**s) - '0';
  132. ++(*s);
  133. }
  134. return ans;
  135. }
  136. static void auto_newline()
  137. {
  138. /**
  139. * @brief 超过每行最大字符数,自动换行
  140. *
  141. */
  142. if (pos.x > pos.max_x)
  143. {
  144. #ifdef DEBUG
  145. uart_send(COM1, '\r');
  146. uart_send(COM1, '\n');
  147. #endif
  148. pos.x = 0;
  149. ++pos.y;
  150. }
  151. if (pos.y > pos.max_y)
  152. {
  153. #ifdef DEBUG
  154. uart_send(COM1, '\r');
  155. uart_send(COM1, '\n');
  156. #endif
  157. pos.y = pos.max_y;
  158. int lines_to_scroll = 1;
  159. barrier();
  160. scroll(true, lines_to_scroll * pos.char_size_y, sw_show_scroll_animation);
  161. barrier();
  162. pos.y -= (lines_to_scroll - 1);
  163. }
  164. }
  165. int vsprintf(char *buf, const char *fmt, va_list args)
  166. {
  167. /**
  168. * 将字符串按照fmt和args中的内容进行格式化,然后保存到buf中
  169. * @param buf 结果缓冲区
  170. * @param fmt 格式化字符串
  171. * @param args 内容
  172. * @return 最终字符串的长度
  173. */
  174. char *str, *s;
  175. str = buf;
  176. int flags; // 用来存储格式信息的bitmap
  177. int field_width; //区域宽度
  178. int precision; //精度
  179. int qualifier; //数据显示的类型
  180. int len;
  181. //开始解析字符串
  182. for (; *fmt; ++fmt)
  183. {
  184. //内容不涉及到格式化,直接输出
  185. if (*fmt != '%')
  186. {
  187. *str = *fmt;
  188. ++str;
  189. continue;
  190. }
  191. //开始格式化字符串
  192. //清空标志位和field宽度
  193. field_width = flags = 0;
  194. bool flag_tmp = true;
  195. bool flag_break = false;
  196. ++fmt;
  197. while (flag_tmp)
  198. {
  199. switch (*fmt)
  200. {
  201. case '\0':
  202. //结束解析
  203. flag_break = true;
  204. flag_tmp = false;
  205. break;
  206. case '-':
  207. // 左对齐
  208. flags |= LEFT;
  209. ++fmt;
  210. break;
  211. case '+':
  212. //在正数前面显示加号
  213. flags |= PLUS;
  214. ++fmt;
  215. break;
  216. case ' ':
  217. flags |= SPACE;
  218. ++fmt;
  219. break;
  220. case '#':
  221. //在八进制数前面显示 '0o',在十六进制数前面显示 '0x' 或 '0X'
  222. flags |= SPECIAL;
  223. ++fmt;
  224. break;
  225. case '0':
  226. //显示的数字之前填充‘0’来取代空格
  227. flags |= PAD_ZERO;
  228. ++fmt;
  229. break;
  230. default:
  231. flag_tmp = false;
  232. break;
  233. }
  234. }
  235. if (flag_break)
  236. break;
  237. //获取区域宽度
  238. field_width = -1;
  239. if (*fmt == '*')
  240. {
  241. field_width = va_arg(args, int);
  242. ++fmt;
  243. }
  244. else if (is_digit(*fmt))
  245. {
  246. field_width = skip_and_atoi(&fmt);
  247. if (field_width < 0)
  248. {
  249. field_width = -field_width;
  250. flags |= LEFT;
  251. }
  252. }
  253. //获取小数精度
  254. precision = -1;
  255. if (*fmt == '.')
  256. {
  257. ++fmt;
  258. if (*fmt == '*')
  259. {
  260. precision = va_arg(args, int);
  261. ++fmt;
  262. }
  263. else if is_digit (*fmt)
  264. {
  265. precision = skip_and_atoi(&fmt);
  266. }
  267. }
  268. //获取要显示的数据的类型
  269. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'Z')
  270. {
  271. qualifier = *fmt;
  272. ++fmt;
  273. }
  274. //为了支持lld
  275. if (qualifier == 'l' && *fmt == 'l', *(fmt + 1) == 'd')
  276. ++fmt;
  277. //转化成字符串
  278. long long *ip;
  279. switch (*fmt)
  280. {
  281. //输出 %
  282. case '%':
  283. *str++ = '%';
  284. break;
  285. // 显示一个字符
  286. case 'c':
  287. //靠右对齐
  288. if (!(flags & LEFT))
  289. {
  290. while (--field_width > 0)
  291. {
  292. *str = ' ';
  293. ++str;
  294. }
  295. }
  296. *str++ = (unsigned char)va_arg(args, int);
  297. while (--field_width > 0)
  298. {
  299. *str = ' ';
  300. ++str;
  301. }
  302. break;
  303. //显示一个字符串
  304. case 's':
  305. s = va_arg(args, char *);
  306. if (!s)
  307. s = '\0';
  308. len = strlen(s);
  309. if (precision < 0)
  310. {
  311. //未指定精度
  312. precision = len;
  313. }
  314. else if (len > precision)
  315. {
  316. len = precision;
  317. }
  318. //靠右对齐
  319. if (!(flags & LEFT))
  320. while (len < field_width--)
  321. {
  322. *str = ' ';
  323. ++str;
  324. }
  325. for (int i = 0; i < len; i++)
  326. {
  327. *str = *s;
  328. ++s;
  329. ++str;
  330. }
  331. while (len < field_width--)
  332. {
  333. *str = ' ';
  334. ++str;
  335. }
  336. break;
  337. //以八进制显示字符串
  338. case 'o':
  339. flags |= SMALL;
  340. case 'O':
  341. flags |= SPECIAL;
  342. if (qualifier == 'l')
  343. str = write_num(str, va_arg(args, long long), 8, field_width, precision, flags);
  344. else
  345. str = write_num(str, va_arg(args, int), 8, field_width, precision, flags);
  346. break;
  347. //打印指针指向的地址
  348. case 'p':
  349. if (field_width == 0)
  350. {
  351. field_width = 2 * sizeof(void *);
  352. flags |= PAD_ZERO;
  353. }
  354. str = write_num(str, (unsigned long)va_arg(args, void *), 16, field_width, precision, flags);
  355. break;
  356. //打印十六进制
  357. case 'x':
  358. flags |= SMALL;
  359. case 'X':
  360. // flags |= SPECIAL;
  361. if (qualifier == 'l')
  362. str = write_num(str, va_arg(args, ll), 16, field_width, precision, flags);
  363. else
  364. str = write_num(str, va_arg(args, int), 16, field_width, precision, flags);
  365. break;
  366. //打印十进制有符号整数
  367. case 'i':
  368. case 'd':
  369. flags |= SIGN;
  370. if (qualifier == 'l')
  371. str = write_num(str, va_arg(args, long long), 10, field_width, precision, flags);
  372. else
  373. str = write_num(str, va_arg(args, int), 10, field_width, precision, flags);
  374. break;
  375. //打印十进制无符号整数
  376. case 'u':
  377. if (qualifier == 'l')
  378. str = write_num(str, va_arg(args, unsigned long long), 10, field_width, precision, flags);
  379. else
  380. str = write_num(str, va_arg(args, unsigned int), 10, field_width, precision, flags);
  381. break;
  382. //输出有效字符数量到*ip对应的变量
  383. case 'n':
  384. if (qualifier == 'l')
  385. ip = va_arg(args, long long *);
  386. else
  387. ip = (ll *)va_arg(args, int *);
  388. *ip = str - buf;
  389. break;
  390. case 'f':
  391. // 默认精度为3
  392. // printk("1111\n");
  393. // va_arg(args, double);
  394. // printk("222\n");
  395. if (precision < 0)
  396. precision = 3;
  397. str = write_float_point_num(str, va_arg(args, double), field_width, precision, flags);
  398. break;
  399. //对于不识别的控制符,直接输出
  400. default:
  401. *str++ = '%';
  402. if (*fmt)
  403. *str++ = *fmt;
  404. else
  405. --fmt;
  406. break;
  407. }
  408. }
  409. *str = '\0';
  410. //返回缓冲区已有字符串的长度。
  411. return str - buf;
  412. }
  413. static char *write_num(char *str, ul num, int base, int field_width, int precision, int flags)
  414. {
  415. /**
  416. * @brief 将数字按照指定的要求转换成对应的字符串
  417. *
  418. * @param str 要返回的字符串
  419. * @param num 要打印的数值
  420. * @param base 基数
  421. * @param field_width 区域宽度
  422. * @param precision 精度
  423. * @param flags 标志位
  424. */
  425. // 首先判断是否支持该进制
  426. if (base < 2 || base > 36)
  427. return 0;
  428. char pad, sign, tmp_num[100];
  429. const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  430. // 显示小写字母
  431. if (flags & SMALL)
  432. digits = "0123456789abcdefghijklmnopqrstuvwxyz";
  433. if (flags & LEFT)
  434. flags &= ~PAD_ZERO;
  435. // 设置填充元素
  436. pad = (flags & PAD_ZERO) ? '0' : ' ';
  437. sign = 0;
  438. if (flags & SIGN)
  439. {
  440. int64_t signed_num = (int64_t)num;
  441. if (signed_num < 0)
  442. {
  443. sign = '-';
  444. num = -signed_num;
  445. }
  446. else
  447. num = signed_num;
  448. }
  449. else
  450. {
  451. // 设置符号
  452. sign = (flags & PLUS) ? '+' : ((flags & SPACE) ? ' ' : 0);
  453. }
  454. // sign占用了一个宽度
  455. if (sign)
  456. --field_width;
  457. if (flags & SPECIAL)
  458. if (base == 16) // 0x占用2个位置
  459. field_width -= 2;
  460. else if (base == 8) // O占用一个位置
  461. --field_width;
  462. int js_num = 0; // 临时数字字符串tmp_num的长度
  463. if (num == 0)
  464. tmp_num[js_num++] = '0';
  465. else
  466. {
  467. num = ABS(num);
  468. //进制转换
  469. while (num > 0)
  470. {
  471. tmp_num[js_num++] = digits[num % base]; // 注意这里,输出的数字,是小端对齐的。低位存低位
  472. num /= base;
  473. }
  474. }
  475. if (js_num > precision)
  476. precision = js_num;
  477. field_width -= precision;
  478. // 靠右对齐
  479. if (!(flags & (LEFT + PAD_ZERO)))
  480. while (field_width-- > 0)
  481. *str++ = ' ';
  482. if (sign)
  483. *str++ = sign;
  484. if (flags & SPECIAL)
  485. if (base == 16)
  486. {
  487. *str++ = '0';
  488. *str++ = digits[33];
  489. }
  490. else if (base == 8)
  491. *str++ = digits[24]; //注意这里是英文字母O或者o
  492. if (!(flags & LEFT))
  493. while (field_width-- > 0)
  494. *str++ = pad;
  495. while (js_num < precision)
  496. {
  497. --precision;
  498. *str++ = '0';
  499. }
  500. while (js_num-- > 0)
  501. *str++ = tmp_num[js_num];
  502. while (field_width-- > 0)
  503. *str++ = ' ';
  504. return str;
  505. }
  506. static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags)
  507. {
  508. /**
  509. * @brief 将浮点数按照指定的要求转换成对应的字符串
  510. *
  511. * @param str 要返回的字符串
  512. * @param num 要打印的数值
  513. * @param field_width 区域宽度
  514. * @param precision 精度
  515. * @param flags 标志位
  516. */
  517. char pad, sign, tmp_num_z[100], tmp_num_d[350];
  518. const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  519. // 显示小写字母
  520. if (flags & SMALL)
  521. digits = "0123456789abcdefghijklmnopqrstuvwxyz";
  522. // 设置填充元素
  523. pad = (flags & PAD_ZERO) ? '0' : ' ';
  524. sign = 0;
  525. if (flags & SIGN && num < 0)
  526. {
  527. sign = '-';
  528. num = -num;
  529. }
  530. else
  531. {
  532. // 设置符号
  533. sign = (flags & PLUS) ? '+' : ((flags & SPACE) ? ' ' : 0);
  534. }
  535. // sign占用了一个宽度
  536. if (sign)
  537. --field_width;
  538. int js_num_z = 0, js_num_d = 0; // 临时数字字符串tmp_num_z tmp_num_d的长度
  539. uint64_t num_z = (uint64_t)(num); // 获取整数部分
  540. uint64_t num_decimal = (uint64_t)(round(1.0 * (num - num_z) * pow(10, precision))); // 获取小数部分
  541. if (num == 0 || num_z == 0)
  542. tmp_num_z[js_num_z++] = '0';
  543. else
  544. {
  545. //存储整数部分
  546. while (num_z > 0)
  547. {
  548. tmp_num_z[js_num_z++] = digits[num_z % 10]; // 注意这里,输出的数字,是小端对齐的。低位存低位
  549. num_z /= 10;
  550. }
  551. }
  552. while (num_decimal > 0)
  553. {
  554. tmp_num_d[js_num_d++] = digits[num_decimal % 10];
  555. num_decimal /= 10;
  556. }
  557. field_width -= (precision + 1 + js_num_z);
  558. // 靠右对齐
  559. if (!(flags & LEFT))
  560. while (field_width-- > 0)
  561. *str++ = pad;
  562. if (sign)
  563. *str++ = sign;
  564. // 输出整数部分
  565. // while (js_num_z-- > 0)
  566. // *str++ = tmp_num_z[js_num_z];
  567. while (js_num_z > 0)
  568. {
  569. *str++ = tmp_num_z[js_num_z - 1];
  570. --js_num_z;
  571. }
  572. *str++ = '.';
  573. // 输出小数部分
  574. int total_dec_count = js_num_d;
  575. for (int i = 0; i < precision && js_num_d-- > 0; ++i)
  576. *str++ = tmp_num_d[js_num_d];
  577. while (total_dec_count < precision)
  578. {
  579. ++total_dec_count;
  580. *str++ = '0';
  581. }
  582. while (field_width-- > 0)
  583. *str++ = ' ';
  584. return str;
  585. }
  586. static void putchar(uint *fb, int Xsize, int x, int y, unsigned int FRcolor, unsigned int BKcolor, unsigned char font)
  587. {
  588. /**
  589. * @brief 在屏幕上指定位置打印字符
  590. *
  591. * @param fb 帧缓存线性地址
  592. * @param Xsize 行分辨率
  593. * @param x 左上角列像素点位置
  594. * @param y 左上角行像素点位置
  595. * @param FRcolor 字体颜色
  596. * @param BKcolor 背景颜色
  597. * @param font 字符的bitmap
  598. */
  599. //#if DEBUG
  600. uart_send(COM1, font);
  601. //#endif
  602. unsigned char *font_ptr = font_ascii[font];
  603. unsigned int *addr;
  604. int testbit; // 用来测试某位是背景还是字体本身
  605. for (int i = 0; i < pos.char_size_y; ++i)
  606. {
  607. // 计算出帧缓冲区的地址
  608. addr = fb + Xsize * (y + i) + x;
  609. testbit = (1 << (pos.char_size_x + 1));
  610. for (int j = 0; j < pos.char_size_x; ++j)
  611. {
  612. //从左往右逐个测试相应位
  613. testbit >>= 1;
  614. if (*font_ptr & testbit)
  615. *addr = FRcolor; // 字,显示前景色
  616. else
  617. *addr = BKcolor; // 背景色
  618. ++addr;
  619. }
  620. ++font_ptr;
  621. }
  622. }
  623. /**
  624. * @brief 格式化打印字符串
  625. *
  626. * @param FRcolor 前景色
  627. * @param BKcolor 背景色
  628. * @param ... 格式化字符串
  629. */
  630. int printk_color(unsigned int FRcolor, unsigned int BKcolor, const char *fmt, ...)
  631. {
  632. uint64_t rflags = 0; // 加锁后rflags存储到这里
  633. spin_lock_irqsave(&printk_lock, rflags);
  634. va_list args;
  635. va_start(args, fmt);
  636. char buf[4096]; // vsprintf()的缓冲区
  637. int len = vsprintf(buf, fmt, args);
  638. va_end(args);
  639. unsigned char current;
  640. int i; // 总共输出的字符数
  641. for (i = 0; i < len; ++i)
  642. {
  643. current = *(buf + i);
  644. //输出换行
  645. if (current == '\n')
  646. {
  647. pos.x = 0;
  648. ++pos.y;
  649. auto_newline();
  650. }
  651. else if (current == '\t') // 输出制表符
  652. {
  653. int space_to_print = 8 - pos.x % 8;
  654. while (space_to_print--)
  655. {
  656. putchar(pos.FB_address, pos.width, pos.x * pos.char_size_x, pos.y * pos.char_size_y, FRcolor, BKcolor, ' ');
  657. ++pos.x;
  658. auto_newline();
  659. }
  660. }
  661. else if (current == '\b') // 退格
  662. {
  663. --pos.x;
  664. if (pos.x < 0)
  665. {
  666. --pos.y;
  667. if (pos.y <= 0)
  668. pos.x = pos.y = 0;
  669. else
  670. pos.x = pos.max_x;
  671. }
  672. putchar(pos.FB_address, pos.width, pos.x * pos.char_size_x, pos.y * pos.char_size_y, FRcolor, BKcolor, ' ');
  673. auto_newline();
  674. }
  675. else
  676. {
  677. putchar(pos.FB_address, pos.width, pos.x * pos.char_size_x, pos.y * pos.char_size_y, FRcolor, BKcolor, current);
  678. ++pos.x;
  679. auto_newline();
  680. }
  681. }
  682. spin_unlock_irqrestore(&printk_lock, rflags);
  683. return i;
  684. }
  685. int do_scroll(bool direction, int pixels)
  686. {
  687. if (direction == true) // 向上滚动
  688. {
  689. pixels = pixels;
  690. if (pixels > pos.height)
  691. return EPOS_OVERFLOW;
  692. // 无需滚动
  693. if (pixels == 0)
  694. return 0;
  695. unsigned int src = pixels * pos.width;
  696. unsigned int count = pos.FB_length - src;
  697. memcpy(pos.FB_address, (pos.FB_address + src), sizeof(unsigned int) * (pos.FB_length - src));
  698. memset(pos.FB_address + (pos.FB_length - src), 0, sizeof(unsigned int) * (src));
  699. return 0;
  700. }
  701. else
  702. return EUNSUPPORTED;
  703. return 0;
  704. }
  705. /**
  706. * @brief 滚动窗口(尚不支持向下滚动)
  707. *
  708. * @param direction 方向,向上滑动为true,否则为false
  709. * @param pixels 要滑动的像素数量
  710. * @param animation 是否包含滑动动画
  711. */
  712. static int scroll(bool direction, int pixels, bool animation)
  713. {
  714. // 暂时不支持反方向滚动
  715. if (direction == false)
  716. return EUNSUPPORTED;
  717. // 为了保证打印字符正确,需要对pixel按照字体高度对齐
  718. int md = pixels % pos.char_size_y;
  719. if (md)
  720. pixels = pixels + pos.char_size_y - md;
  721. if (animation == false)
  722. return do_scroll(direction, pixels);
  723. else
  724. {
  725. int steps;
  726. if (pixels > 10)
  727. steps = 5;
  728. else
  729. steps = pixels % 10;
  730. int half_steps = steps / 2;
  731. // 计算加速度
  732. double accelerate = 0.5 * pixels / (half_steps * half_steps);
  733. int current_pixels = 0;
  734. double delta_x;
  735. int trace[13] = {0};
  736. int js_trace = 0;
  737. // 加速阶段
  738. for (int i = 1; i <= half_steps; ++i)
  739. {
  740. trace[js_trace] = (int)(accelerate * i + 0.5);
  741. current_pixels += trace[js_trace];
  742. do_scroll(direction, trace[js_trace]);
  743. ++js_trace;
  744. }
  745. // 强制使得位置位于1/2*pixels
  746. if (current_pixels < pixels / 2)
  747. {
  748. delta_x = pixels / 2 - current_pixels;
  749. current_pixels += delta_x;
  750. do_scroll(direction, delta_x);
  751. }
  752. // 减速阶段,是加速阶段的重放
  753. for (int i = js_trace - 1; i >= 0; --i)
  754. {
  755. current_pixels += trace[i];
  756. do_scroll(direction, trace[i]);
  757. }
  758. if (current_pixels > pixels)
  759. kerror("During scrolling: scrolled pixels over bound!");
  760. // 强制使得位置位于pixels
  761. if (current_pixels < pixels)
  762. {
  763. delta_x = pixels - current_pixels;
  764. current_pixels += delta_x;
  765. do_scroll(direction, delta_x);
  766. }
  767. }
  768. return 0;
  769. }
  770. /**
  771. * @brief 清屏
  772. *
  773. */
  774. static int cls()
  775. {
  776. memset(pos.FB_address, BLACK, pos.FB_length * sizeof(unsigned int));
  777. pos.x = 0;
  778. pos.y = 0;
  779. return 0;
  780. }
  781. /**
  782. * @brief 获取VBE帧缓冲区长度
  783. */
  784. ul get_VBE_FB_length()
  785. {
  786. return pos.FB_length;
  787. }
  788. /**
  789. * @brief 设置pos变量中的VBE帧缓存区的线性地址
  790. * @param virt_addr VBE帧缓存区线性地址
  791. */
  792. void set_pos_VBE_FB_addr(uint *virt_addr)
  793. {
  794. pos.FB_address = (uint *)virt_addr;
  795. }
  796. static uint *get_pos_VBE_FB_addr()
  797. {
  798. return pos.FB_address;
  799. }
  800. /**
  801. * @brief 使能滚动动画
  802. *
  803. */
  804. void printk_enable_animation()
  805. {
  806. sw_show_scroll_animation = true;
  807. }
  808. /**
  809. * @brief 禁用滚动动画
  810. *
  811. */
  812. void printk_disable_animation()
  813. {
  814. sw_show_scroll_animation = false;
  815. }
  816. int sprintk(char *buf, const char *fmt, ...)
  817. {
  818. int count = 0;
  819. va_list args;
  820. va_start(args, fmt);
  821. count = vsprintf(buf, fmt, args);
  822. va_end(args);
  823. return count;
  824. }
  825. #pragma GCC pop_options