今天能够显示字符串和变量了,不过程序结构非常的糟糕,无奈真的是时间紧迫,好在也是显示了正确的数值。
字体是直接借用的
制作方法是
makefont hankaku.txt font.bin
bin2obj font.bin font.o _myfont
然后拷贝到工作目录
Makefile中的源文件中加入font.o
默认的标准输入输出头文件中虽然含有sprintf()这个函数,但是不知怎么无法链接,无奈在网上当了一个测试用的,不过还很好用,直接把代码粘在自己的kernel.c中,发现缺少了strnlen() 函数的实现, 于是大胆的实现了一个空函数,并生命之,居然正常的运行起来了。
以下是最新的kernel.c代码,代码结构糟糕的很, 真的没有时间整理了,大家凑合着看吧
#include "multiboot2.h"
typedef char * va_list;
#ifdef __cplusplus
#define _ADDRESSOF(v) ( &reinterpret_cast<const char &>(v) )
#else
#define _ADDRESSOF(v) ( &(v) )
#endif
#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
#define _crt_va_start(ap,v) ( ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v) )
#define _crt_va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define _crt_va_end(ap) ( ap = (va_list)0 )
#define Test_va_start _crt_va_start /* windows stdarg.h */
#define Test_va_arg _crt_va_arg
#define Test_va_end _crt_va_end
#define ZEROPAD 1 /* pad with zero */
#define SIGN 2 /* unsigned/signed long */
#define PLUS 4 /* show plus */
#define SPACE 8 /* space if plus */
#define LEFT 16 /* left justified */
#define SPECIAL 32 /* 0x */
#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
int _div(long* n,unsigned base);
static inline int isdigit(int ch);
static int skip_atoi(const char **s);
static char *Test_number(char *str, long num, int base, int size, int precision, int type);
int Test_vsprintf(char *buf, const char *fmt, va_list args);
int strnlen(char * s, int precision);
int sprintf(char *buf, const char *fmt, ...);
int kernel_main (unsigned int magic, unsigned int addr) ;
static char font_A[16] = {
0x00, 0x18, 0x18, 0x18, 0x18, 0x24, 0x24, 0x24,
0x24, 0x7e, 0x42, 0x42, 0x42, 0xe7, 0x00, 0x00
};
void putfont8 (int * vram, int xsize, int x, int y, int c, char * fontt);
void putfont8_asc (int *vram, int xsize, int x0, int y0, int c, char * s);
int scr_loc = 0;
char string[256];
extern void put_char(char str);
int kernel_main (unsigned int magic, unsigned int addr) {
if (1) {
sprintf(string, "magic = %x", magic);
}
struct multiboot_tag *tag = (struct multiboot_tag *) (addr + 8);
struct multiboot_tag_framebuffer *tagfb = (struct multiboot_tag_framebuffer *) tag;
void * fb;
for (tag = (struct multiboot_tag *) (addr + 8); tag->type != MULTIBOOT_TAG_TYPE_END;tag = (struct multiboot_tag *) ((multiboot_uint8_t *) tag + ((tag->size + 7) & ~7)))
{
switch (tag->type)
{
case MULTIBOOT_TAG_TYPE_CMDLINE:
break;
case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME:
break;
case MULTIBOOT_TAG_TYPE_MODULE:
break;
case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO:
break;
case MULTIBOOT_TAG_TYPE_BOOTDEV:
break;
case MULTIBOOT_TAG_TYPE_MMAP:
{
}
break;
case MULTIBOOT_TAG_TYPE_FRAMEBUFFER:
{
unsigned int i;
struct multiboot_tag_framebuffer *tagfb
= (struct multiboot_tag_framebuffer *) tag;
fb = (void *) (unsigned long) tagfb->common.framebuffer_addr;
switch (tagfb->common.framebuffer_type)
{
case MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED:
{
unsigned best_distance, distance;
struct multiboot_color *palette;
palette = tagfb->framebuffer_palette;
best_distance = 4*256*256;
for (i = 0; i < tagfb->framebuffer_palette_num_colors; i++)
{
distance = (0xff - palette[i].blue)
* (0xff - palette[i].blue)
+ palette[i].red * palette[i].red
+ palette[i].green * palette[i].green;
if (distance < best_distance)
{
best_distance = distance;
}
}
}
break;
case MULTIBOOT_FRAMEBUFFER_TYPE_RGB:
break;
case MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT:
break;
default:
break;
}
// Paint desktop
for (i = 0 * 3; i < tagfb->common.framebuffer_width
&& i < 256 * 3; i++) {
multiboot_uint32_t *pixel = fb + tagfb->common.framebuffer_pitch * i;
for (int j= 0; j < 1024; j++)
*(pixel + j) = 0x008484;
}
//Paint taskbar
for (i = 246 * 3; i < tagfb->common.framebuffer_width
&& i < 256 * 3; i++) {
multiboot_uint32_t *pixel = fb + tagfb->common.framebuffer_pitch * i;
for (int j= 0; j < 1024; j++)
*(pixel + j) = 0xc6c6c6;
}
//title
for (i = 46 * 3; i < tagfb->common.framebuffer_width
&& i < 56 * 3; i++) {
multiboot_uint32_t *pixel = fb + tagfb->common.framebuffer_pitch * i;
for (int j= 200; j < 500; j++)
*(pixel + j) = 0xc6c6c6;
}
//left
for (i = 56 * 3; i < tagfb->common.framebuffer_width
&& i < 104 * 3; i++) {
multiboot_uint32_t *pixel = fb + tagfb->common.framebuffer_pitch * i;
for (int j= 200; j < 202; j++)
*(pixel + j) = 0xc6c6c6;
}
//bottom
for (i = 104 * 3; i < tagfb->common.framebuffer_width
&& i < 106 * 3; i++) {
multiboot_uint32_t *pixel = fb + tagfb->common.framebuffer_pitch * i;
for (int j= 200; j < 500; j++)
*(pixel + j) = 0xc6c6c6;
}
// right
for (i = 56* 3; i < tagfb->common.framebuffer_width
&& i < 104 * 3; i++) {
multiboot_uint32_t *pixel = fb + tagfb->common.framebuffer_pitch * i;
for (int j= 498; j < 500; j++)
*(pixel + j) = 0xc6c6c6;
}
//body
for (i = 56 * 3; i < tagfb->common.framebuffer_width
&& i < 104 * 3; i++) {
multiboot_uint32_t *pixel = fb + tagfb->common.framebuffer_pitch * i;
for (int j= 202; j < 498; j++)
*(pixel + j) = 0x000000;
}
int x0, y0;
x0 = 300;
y0 = 50;
multiboot_uint32_t *pixel = fb + y0 * tagfb->common.framebuffer_pitch + x0;
int k;
for (k = 0; k < 200; k++)
*(pixel+k) = 0x0000ff;
break;
}
}
}
tag = (struct multiboot_tag *) ((multiboot_uint8_t *) tag
+ ((tag->size + 7) & ~7));
putfont8_asc (fb, 1024, 0, 0, 0xffffff, string);
sprintf(string, "tagfb->common.framebuffer_pitch = %d", tagfb->common.framebuffer_pitch);
putfont8 (fb, 1024, 30, 30, 0x000000, font_A);
putfont8_asc (fb, 1024, 90, 90, 0xffffff, "Hello!, How are you!");
putfont8_asc (fb, 1024, 120, 120, 0xffffff, string);
return 1;
}
void putfont8 (int * vram, int xsize, int x, int y, int c, char * fontt) {
int i;
int *p;
unsigned char d;
for (i = 0; i < 16; i++) {
d = fontt[i];
p = vram + (y + i) * xsize + x;
if ((d & 0x80) != 0) {*(p + 0) = c;}
if ((d & 0x40) != 0) {*(p + 1) = c;}
if ((d & 0x20) != 0) {*(p + 2) = c;}
if ((d & 0x10) != 0) {*(p + 3) = c;}
if ((d & 0x08) != 0) {*(p + 4) = c;}
if ((d & 0x04) != 0) {*(p + 5) = c;}
if ((d & 0x02) != 0) {*(p + 6) = c;}
if ((d & 0x01) != 0) {*(p + 7) = c;}
}
return;
}
void putfont8_asc (int *vram, int xsize, int x0, int y0, int c, char * s) {
extern char myfont[4096];
for (; *s != 0; s++) {
putfont8(vram, xsize, x0, y0, c, myfont+ *s * 16 );
x0 +=8;
}
return;
}
int sprintf(char *buf, const char *fmt, ...)
{
//记录fmt对应的地址
va_list args;
int val;
//得到首个%对应的字符地址
Test_va_start(args, fmt);
int i = Test_vsprintf(buf, fmt, args);
Test_va_end(args);
return val;
}
int _div(long* n,unsigned base)
{
int __res;
__res = ((unsigned long) *n) % (unsigned) base;
*n = ((unsigned long) *n) / (unsigned) base;
return __res;
}
#define do_div(n,base) _div(&n,base)/*({ \
int __res; \
__res = ((unsigned long) n) % (unsigned) base; \
n = ((unsigned long) n) / (unsigned) base; \
__res; })*/
static inline int isdigit(int ch)
{
return (ch >= '0') && (ch <= '9');
}
static int skip_atoi(const char **s)
{
int i = 0;
while (isdigit(**s))
i = i * 10 + *((*s)++) - '0';
return i;
}
static char *Test_number(char *str, long num, int base, int size, int precision,
int type)
{
char c, sign, tmp[66];
const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
int i;
if (type & LARGE)
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (type & LEFT)
type &= ~ZEROPAD;
if (base < 2 || base > 36)
return 0;
c = (type & ZEROPAD) ? '0' : ' ';
sign = 0;
if (type & SIGN) {
if (num < 0) {
sign = '-';
num = -num;
size--;
} else if (type & PLUS) {
sign = '+';
size--;
} else if (type & SPACE) {
sign = ' ';
size--;
}
}
if (type & SPECIAL) {
if (base == 16)
size -= 2;
else if (base == 8)
size--;
}
i = 0;
if (num == 0)
{
tmp[i++] = '0';
}
else
{
while (num != 0)
{
tmp[i++] = digits[do_div(num, base)];
}
}
if (i > precision)
precision = i;
size -= precision;
if (!(type & (ZEROPAD + LEFT)))
while (size-- > 0)
*str++ = ' ';
if (sign)
*str++ = sign;
if (type & SPECIAL) {
if (base == 8)
*str++ = '0';
else if (base == 16) {
*str++ = '0';
*str++ = digits[33];
}
}
if (!(type & LEFT))
while (size-- > 0)
*str++ = c;
while (i < precision--)
*str++ = '0';
while (i-- > 0)
*str++ = tmp[i];
while (size-- > 0)
*str++ = ' ';
return str;
}
int Test_vsprintf(char *buf, const char *fmt, va_list args)
{
int len;
unsigned long num;
int i, base;
char *str;
const char *s;
int flags; /* flags to Test_number() */
int field_width; /* width of output field */
int precision; /* min. # of digits for integers; max
Test_number of chars for from string */
int qualifier; /* 'h', 'l', or 'L' for integer fields */
for (str = buf; *fmt; ++fmt) {
if (*fmt != '%') {
*str++ = *fmt;
continue;
}
/* process flags */
flags = 0;
repeat:
++fmt; /* this also skips first '%' */
switch (*fmt) {
case '-':
flags |= LEFT;
goto repeat;
case '+':
flags |= PLUS;
goto repeat;
case ' ':
flags |= SPACE;
goto repeat;
case '#':
flags |= SPECIAL;
goto repeat;
case '0':
flags |= ZEROPAD;
goto repeat;
}
/* get field width */
field_width = -1;
if (isdigit(*fmt))
field_width = skip_atoi(&fmt);
else if (*fmt == '*') {
++fmt;
/* it's the next argument */
field_width = Test_va_arg(args, int);
if (field_width < 0) {
field_width = -field_width;
flags |= LEFT;
}
}
/* get the precision */
precision = -1;
if (*fmt == '.') {
++fmt;
if (isdigit(*fmt))
precision = skip_atoi(&fmt);
else if (*fmt == '*') {
++fmt;
/* it's the next argument */
precision = Test_va_arg(args, int);
}
if (precision < 0)
precision = 0;
}
/* get the conversion qualifier */
qualifier = -1;
if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
qualifier = *fmt;
++fmt;
}
/* default base */
base = 10;
switch (*fmt) {
case 'c':
if (!(flags & LEFT))
while (--field_width > 0)
*str++ = ' ';
*str++ = (unsigned char)Test_va_arg(args, int);
while (--field_width > 0)
*str++ = ' ';
continue;
case 's':
s = Test_va_arg(args, char *);
len = strnlen(s, precision);
if (!(flags & LEFT))
while (len < field_width--)
*str++ = ' ';
for (i = 0; i < len; ++i)
*str++ = *s++;
while (len < field_width--)
*str++ = ' ';
continue;
case 'p':
if (field_width == -1) {
field_width = 2 * sizeof(void *);
flags |= ZEROPAD;
}
str = Test_number(str,
(unsigned long)Test_va_arg(args, void *), 16,
field_width, precision, flags);
continue;
case 'n':
if (qualifier == 'l') {
long *ip = Test_va_arg(args, long *);
*ip = (str - buf);
} else {
int *ip = Test_va_arg(args, int *);
*ip = (str - buf);
}
continue;
case '%':
*str++ = '%';
continue;
/* integer Test_number formats - set up the flags and "break" */
case 'o':
base = 8;
break;
case 'X':
flags |= LARGE;
case 'x':
base = 16;
break;
case 'd':
case 'i':
flags |= SIGN;
case 'u':
break;
default:
*str++ = '%';
if (*fmt)
*str++ = *fmt;
else
--fmt;
continue;
}
if (qualifier == 'l')
num = Test_va_arg(args, unsigned long);
else if (qualifier == 'h') {
num = (unsigned short)Test_va_arg(args, int);
if (flags & SIGN)
num = (short)num;
} else if (flags & SIGN)
num = Test_va_arg(args, int);
else
num = Test_va_arg(args, unsigned int);
str = Test_number(str, num, base, field_width, precision, flags);
}
*str = '\0';
return str - buf;
}
int strnlen(char * s, int precision) {
int len = 255;
return len;
}