printk.c 23 KB

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