7.1创建日志输出接口
//D:\code\x86\code\start\start\source\kernel\include\tools\log.h
#ifndef LOG_H
#define LOG_H
void log_init (void); //后面课时讲
void log_printf(const char * fmt, ...);//后面的可变参数
#endif // LOG_H
我们将输出到这个串行接口(不是VGA接口)上,下面代码是这个串行接口初始化和设置一些参数,不用学这个接口这个早就淘汰了,先学一下这个接口过渡一下,后面还是要写函数输出到屏幕上面来
//D:\code\x86\code\start\start\source\kernel\tools\log.c
#include "comm/cpu_instr.h"
#include "tools/log.h"
#include "os_cfg.h"
// 目标用串口,参考资料:https://wiki.osdev.org/Serial_Ports
#define COM1_PORT 0x3F8 // RS232端口0初始化
/**
* @brief 初始化日志输出
*/
void log_init (void) {
//初始化串行接口
outb(COM1_PORT + 1, 0x00); // Disable all interrupts
outb(COM1_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(COM1_PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
outb(COM1_PORT + 1, 0x00); // (hi byte)
outb(COM1_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
outb(COM1_PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
// If serial is not faulty set it in normal operation mode
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
outb(COM1_PORT + 4, 0x0F);
}
/**
* @brief 日志打印
*/
void log_printf(const char * fmt, ...) {
const char * p = fmt;
while (*p != '\0') {
while ((inb(COM1_PORT + 5) & (1 << 6)) == 0); //看串行接口是不是处于忙的状态
outb(COM1_PORT, *p++);
}
outb(COM1_PORT, '\r'); //换行,改变了列号变为0
outb(COM1_PORT, '\n');//回车,改变了行号变为下一行行号
}
小知识点
这里面的\n和c语言里面的\n效果是不一样的
这里面\n+\r等价于c语言里面的\n
如果单独输出这个\n
如果单独输出这个\r