⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coreutils.c

📁 ucos在x86上的移植代码。吐血推荐啊
💻 C
字号:
/* coreutils.c :
 *   内核系统核心函数, C语言函数部分.
 *
 * Copyright(c) 2007, Alex P.Wonder
 * phoenixwonder@gmail.com
 *
 */

#include "coreutils.h"

static unsigned long int next=1;

/* 随机数 */
int random(void)
{
	next=next*1103515245+12345;
	return (unsigned int)(next/65536)%32768;
}

/* 字符串输出 */
void os_putstring(int x, int y, int color, char *str)
{
	while(*str)
	{
		os_putchar(*str, x, y, color);
		x++;
		str++;
	}
}

/* --os_sprintf()实现-- */
#define putchar(c) //outbyte(c)

static void printchar(char **str, int c)
{
	if (str) {
		**str = c;
		++(*str);
	}
	else /* 如果不需要保存到*str中,就直接写向屏幕. */
		putchar(c);
}

#define PAD_RIGHT 1
#define PAD_ZERO 2

static int prints(char **out, const char *string, int width, int pad)
{
	register int pc = 0, padchar = 0;

	if (width > 0) {
		register int len = 0;
		register const char *ptr;
		for (ptr = string; *ptr; ++ptr) ++len;
		if (len >= width) width = 0;
		else width -= len;
		if (pad & PAD_ZERO) padchar = '0';
	}

	if (!(pad & PAD_RIGHT)) {
		for ( ; width > 0; --width) {
			printchar (out, padchar);
			++pc;
		}
	}

	for ( ; *string ; ++string) {
		printchar (out, *string);
		++pc;
	}
	for ( ; width > 0; --width) {
		printchar (out, padchar);
		++pc;
	}

	return pc;
}

/* the following should be enough for 32 bit int */
#define PRINT_BUF_LEN 12

static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
{
	char print_buf[PRINT_BUF_LEN];
	register char *s;
	register int t, neg = 0, pc = 0;
	register unsigned int u = i;

	if (i == 0) {
		print_buf[0] = '0';
		print_buf[1] = '\0';
		return prints (out, print_buf, width, pad);
	}

	if (sg && b == 10 && i < 0) {
		neg = 1;
		u = -i;
	}

	s = print_buf + PRINT_BUF_LEN-1;
	*s = '\0';

	while (u) {
		t = u % b;
		if( t >= 10 )
		t += letbase - '0' - 10;
		*--s = t + '0';
		u /= b;
	}

	if (neg) {
		if( width && (pad & PAD_ZERO) ) {
			printchar (out, '-');
			++pc;
			--width;
		}
		else {
			*--s = '-';
		}
	}

	return pc + prints (out, s, width, pad);
}

static int print(char **out, int *varg)
{
	register int width, pad;
	register int pc = 0;
	register char *format = (char *)(*varg++);
	char scr[2];

	for(; *format != 0; ++format) {
		if (*format == '%') {
			++format;
			width = pad = 0;
			if (*format == '\0') break;
			if (*format == '%') goto out;
			if (*format == '-') {
				++format;
				pad = PAD_RIGHT;
			}
			while (*format == '0') {
				++format;
				pad |= PAD_ZERO;
			}
			for ( ; *format >= '0' && *format <= '9'; ++format) {
				width *= 10;
				width += *format - '0';
			}
			if( *format == 's' ) {
				register char *s = *((char **)varg++);
				pc += prints (out, s?s:"(null)", width, pad);
				continue;
			}
			if( *format == 'd' ) {
				pc += printi (out, *varg++, 10, 1, width, pad, 'a');
				continue;
			}
			if( *format == 'x' ) {
				pc += printi (out, *varg++, 16, 0, width, pad, 'a');
				continue;
			}
			if( *format == 'X' ) {
				pc += printi (out, *varg++, 16, 0, width, pad, 'A');
				continue;
			}
			if( *format == 'u' ) {
				pc += printi (out, *varg++, 10, 0, width, pad, 'a');
				continue;
			}
			if( *format == 'c' ) {
				/* char are converted to int then pushed on the stack */
				scr[0] = *varg++;
				scr[1] = '\0';
				pc += prints (out, scr, width, pad);
				continue;
			}
		}
		else {
out:
			printchar (out, *format);
			++pc;
		}
	}
	if (out) **out = '\0';
	return pc;
}

/* assuming sizeof(void *) == sizeof(int) */
//
//int os_printf(const char *format, ...)
//{
//	register int *varg = (int *)(&format);
//	return print(0, varg);
//}

int os_sprintf(char *out, const char *format, ...)
{
	register int *varg = (int *)(&format);
	return print(&out, varg);
}

void os_set_tick(unsigned short freq)
{
    int count;
		if (freq > 0) {                        
			/* Compute 8254 counts for desired frequency and ...  */
			/* ... round to nearest count                         */
      count = (int)((2386360L / freq + 1) >> 1); 
    } else {
        count = 0;
    }
    
    OS_ENTER_CRITICAL();
    outp(TICK_T0_8254_CWR,  TICK_T0_8254_CTR0_MODE3); /* Load the 8254 with desired frequency          */  
    //outp(TICK_T0_8254_CWR,  0x34); 			/* Load the 8254 with desired frequency          */  
    outp(TICK_T0_8254_CTR0, count & 0xFF);            /* Low  byte                                     */
    outp(TICK_T0_8254_CTR0, (count >> 8) & 0xFF);     /* High byte                                     */
    OS_EXIT_CRITICAL();
}

void os_clrscr(unsigned char color)
{
	int i;
	char str[80];
	os_memset(str, ' ', 80);
	for(i = 0; i < 25; i++)
		os_putstring(0, i, color, str);
}

unsigned char bcd2hex(unsigned char bcdval)
{
	return ((((bcdval&0xf0)>>4)*10) + (bcdval&0x0f));
}


/* 读日期 */
void os_getdate( struct date *dp)
{
	if(!dp) return;
	
	dp->da_year = bcd2hex(read_rtc_regs(rrYear)) + 2000;
	dp->da_mon  = bcd2hex(read_rtc_regs(rrMonth));
	dp->da_day  = bcd2hex(read_rtc_regs(rrMDay));
}

/* 读时间 */
void os_gettime( struct time *tp)
{
	if(!tp) return;
	
	tp->ti_hour = bcd2hex(read_rtc_regs(rrHour));
	tp->ti_min  = bcd2hex(read_rtc_regs(rrMin));
	tp->ti_sec  = bcd2hex(read_rtc_regs(rrSec));	
}

void os_gotoxy(int x, int y)
{
	unsigned short offset;
	offset = y*80 + x;
	outp(IO_KB_CUR_IDX, 14);
	outp(IO_KB_CUR_DATA, 0xff & (offset>>8));
	outp(IO_KB_CUR_IDX, 15);
	outp(IO_KB_CUR_DATA, 0xff & offset);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -