1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wslで、最小のカーネルをコンパイル その10

Last updated at Posted at 2024-03-15

概要

wslで、kernel.elfのコンパイル、やってみた。
cで、書いたカーネルで、俺言語を組み込んだ。

写真

九九を、打ち込んで、実行した。

image.png

サンプルコード

#include "kernel3.h"

#define true 		1
#define false		0
#define ESC 		(0x1B)
#define BS  		(0x08)
#define EOT 		(0x04)

const uint8 lower_ascii_codes_jp[256] = {
	0,	ESC,	'1',	'2',	'3',	'4',	'5',	'6',	'7',	'8',	'9',	'0',	'-',	'^',	BS, '\t',
	'q',	'w',	'e',	'r',	't',	'y',	'u',	'i',	'o',	'p',	'[',	']',	'\n',	0,
	'a',	's',	'd',	'f',	'g',	'h',	'j',	'k',	'l',	';',	':',	'`',	0, '\\',
	'z',	'x',	'c',	'v',	'b',	'n',	'm',	',',	'.',	'/',	0,	'*',
	0,	' ',	0,	0,	0,	0,	0,	0,		0,	0,	0,	0,		0,	0,	0,	'7',	/* 0x44 */
	'8',	'9',	'-',	'4',	/* 0x48 */
	'5',	'6',	'+',	'1',	/* 0x4C */
	'2',	'3',	'0',	'.',	/* 0x50 */
	0,	0,	0,	0,	/* 0x54 */
	0,	0,	0,	0		/* 0x58 */
};
const uint8 upper_ascii_codes_jp[256] = {
	0,	ESC,	'!',	'"',	'#',	'$',	'%',	'&',	'&',	'(',	')',	')',	'=',	'~',	BS, '\t',
	'Q',	'W',	'E',	'R',	'T',	'Y',	'U',	'I',	'O',	'P',	'{',	'}',	'\n',	0,
	'A',	'S',	'D',	'F',	'G',	'H',	'J',	'K',	'L',	'+',	'*',	'~',	0,	'|',
	'Z',	'X',	'C',	'V',	'B',	'N',	'M',	'<',	'>',	'?',	0,	'*',
	0,	' ',	0,	0,	0,	0,	0,	0,	0,	0,	0,	0,	0,	0,	0,	'7',	/* 0x44 */
	'8',	'9',	'-',	'4',	/* 0x48 */
	'5',	'6',	'+',	'1',	/* 0x4C */
	'2',	'3',	'0',	'.',	/* 0x50 */
	0,	0,	0,	0,	/* 0x54 */
	0,	0,	0,	0		/* 0x58 */
};

uint32 vga_index;
static uint32 next_line_index = 1;
uint8 g_fore_color = WHITE,
	g_back_color = BLUE;
int digit_ascii_codes[10] = {
	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39
};
uint32 strlen(const char * str) {
	uint32 length = 0;
	while (str[length])
		length++;
	return length;
}
uint32 digit_count(int num) {
	uint32 count = 0;
	if (num == 0)
		return 1;
	while (num > 0)
	{
		count++;
		num = num / 10;
	}
	return count;
}
void itoa(int num, char * number) {
	int dgcount = digit_count(num);
	int index = dgcount - 1;
	char x;
	if (num == 0 && dgcount == 1)
	{
		number[0] = '0';
		number[1] = '\0';
	}
	else
	{
		while (num != 0)
		{
			x = num % 10;
			number[index] = x + '0';
			index--;
			num = num / 10;
		}
		number[dgcount] = '\0';
	}
}

uint16 vga_entry(unsigned char ch, uint8 fore_color, uint8 back_color) {
	uint16 ax = 0;
	uint8 ah = 0,
		al = 0;
	ah = back_color;
	ah <<= 4;
	ah |= fore_color;
	ax = ah;
	ax <<= 8;
	al = ch;
	ax |= al;
	return ax;
}
void clear_vga_buffer(uint16 ** buffer, uint8 fore_color, uint8 back_color) {
	uint32 i;
	for (i = 0; i < BUFSIZE; i++)
	{
		(* buffer) [i] = vga_entry(NULL, fore_color, back_color);
	}
	next_line_index = 1;
	vga_index = 0;
}
void init_vga(uint8 fore_color, uint8 back_color) {
	vga_buffer = (uint16 *) VGA_ADDRESS;
	clear_vga_buffer(&vga_buffer, fore_color, back_color);
	g_fore_color = fore_color;
	g_back_color = back_color;
}
void print_new_line() {
	if (next_line_index >= 55)
	{
		next_line_index = 0;
		clear_vga_buffer(&vga_buffer, g_fore_color, g_back_color);
	}
	vga_index = 80 * next_line_index;
	next_line_index++;
}
void print_char(char ch) {
	vga_buffer[vga_index] = vga_entry(ch, g_fore_color, g_back_color);
	vga_index++;
}
void print_string(char * str) {
	uint32 index = 0;
	while (str[index])
	{
		print_char(str[index]);
		index++;
	}
}
void print_int(int num) {
	char str_num[digit_count(num) + 1];
	itoa(num, str_num);
	print_string(str_num);
}
uint8 inb(uint16 port) {
	uint8 ret;
	asm volatile("inb %1, %0": "=a"(ret): "d"(port));
	return ret;
}
void outb(uint16 port, uint8 data) {
	asm volatile("outb %0, %1": "=a"(data): "d"(port));
}
char get_input_keycode() {
	char ch = 0;
	while ((ch = inb(KEYBOARD_PORT)) != 0)
	{
		if (ch > 0)
			return ch;
	}
	return ch;
}
void wait_for_io(uint32 timer_count) {
	while (1)
	{
		asm volatile("nop");
		timer_count--;
		if (timer_count <= 0)
			break;
	}
}
void sleep(uint32 timer_count) {
	wait_for_io(timer_count);
}

char ram[300];
int Pcc = 257;
int Rmd = 258;
int Bnd = 259;
int Lbf = 270;
int Obj = 0;
int Ptr = 270;
int Cur;
int Vc;
int Adr;
int Adr2;
char Val;
int i;

char RD(int ado) {
	char v = ram[ado];
	return v;
}
void WR(int ado, char data) {
	ram[ado] = data;
}
void putchr(char c) {
	print_char(c);
}
void putint(int c) {
	print_int(c);
}
void putstr(const char * s) {
	while (* s)
		putchr(* s++);
}
void crlf() {
	print_new_line();
}
int putl(int pt, char d) {
	while (RD(pt) != d)
	{
		putchr(RD(pt++));
	}
	return pt;
}
void putlp(int d) {
	while (RD(Ptr) != d)
	{
		putchr(RD(Ptr++));
	}
}
int num() {
	if (48 <= RD(Ptr) && RD(Ptr) <= 57)
		return true;
	else
		return false;
}
int getnm2() {
	char ch;
	if (!num())
		return false;
	int n = 0;
	do
	{
		n *= 10;
		ch = RD(Ptr++);
		n += ch - 48;
	} while (num());
	Val = n;
	return true;
}
int nxtln(int pt) {
	for (pt += 1; RD(pt++) != '\0';)
		;
	return pt;
}
int fndln() {
	for (Cur = Obj; Cur != RD(Bnd); Cur = nxtln(Cur))
	{
		if (RD(Cur) == RD(Pcc))
			return true;
	}
	return false;
}
int nxline() {
	for (Cur = Obj; Cur != RD(Bnd); Cur = nxtln(Cur))
	{
		if (RD(Cur) > RD(Pcc))
			return true;
	}
	return false;
}
void getvr() {
	Vc = RD(Ptr);
	if (Vc == 'a')
		Adr = 260;
	else if (Vc == '#')
		Adr = Pcc;
	else if (Vc == 'b')
		Adr = 261;
	else if (Vc == 'c')
		Adr = 262;
	else if (Vc == 'd')
		Adr = 263;
	else if (Vc == 'e')
		Adr = 264;
	else if (Vc == 'f')
		Adr = 265;
	else if (Vc == '%')
		Adr = Rmd;
	else
		Adr = 260;
}
void getvr2() {
	char cc = RD(Ptr);
	if (cc == 'a')
		Adr2 = 260;
	else if (cc == '#')
		Adr2 = Pcc;
	else if (cc == 'b')
		Adr2 = 261;
	else if (cc == 'c')
		Adr2 = 262;
	else if (cc == 'd')
		Adr2 = 263;
	else if (cc == 'e')
		Adr2 = 264;
	else if (cc == 'f')
		Adr2 = 265;
	else if (cc == '%')
		Adr2 = Rmd;
	else
		Adr2 = 260;
}
void facto() {
	char cc;
	if (RD(Ptr) == '\0')
	{
		Val = 0;
		return;
	}
	if (getnm2())
		return;
	cc = RD(Ptr++);
	if (cc == '?')
	{
	}
	else if (cc == '$')
	{
	}
	else if (cc == '(')
	{
	}
	else
	{
		Ptr--;
		getvr2();
		Val = RD(Adr2);
		Ptr++;
	}
}
void term() {
	char cc;
	cc = RD(Ptr++);
	char Val2 = Val;
	facto();
	if (cc == '*')
	{
		Val = Val2 * Val;
	}
	else if (cc == '+')
	{
		Val = Val2 + Val;
	}
	else if (cc == '-')
	{
		Val = Val2 - Val;
	}
	else if (cc == '/')
	{
		WR(Rmd, Val2 % Val);
		Val = Val2 / Val;
	}
	else if (cc == '=')
	{
		if (Val2 == Val)
			Val = 1;
		else
			Val = 0;
	}
	else if (cc == '>')
	{
		if (Val2 >= Val)
			Val = 1;
		else
			Val = 0;
	}
	else
	{
		if (Val2 < Val)
			Val = 1;
		else
			Val = 0;
	}
}
void expr() {
	char cc;
	facto();
	while ((cc = RD(Ptr)) != '\0' && cc != ')')
	{
		term();
	}
	Ptr++;
}
void ordr() {//siki hyouka
	getvr();//hidari
	Ptr++;
	Ptr++;
	if (RD(Ptr) == '"')
	{
		Ptr++;
		putlp('"');
	}
	else
	{
		expr();//migi
		if (Vc == '$')
		{
		}
		else if (Vc == 's')
		{
			init_vga(WHITE, BLUE);
		}
		else if (Vc == 'r')
		{
			print_new_line();
		}
		else if (Vc == '?')
		{
			putint(Val);
			putchr(' ');
		}
		else
		{
			WR(Adr, Val);
		}
	}
}
void run() {
	int ima = 0;
	Ptr = Lbf;
	if (!getnm2())
	{
		Ptr = Lbf;
		WR(Pcc, 0);
		for ( ; ; )
		{
			ima = RD(Pcc);
			ordr();
			if (RD(Pcc) == 0 && ima == 0)
			{
				break;
			}
			else
			{
				if (RD(Pcc) == 0)
					WR(Pcc, ima);
				if (RD(Pcc) == 1)
					ima = 1;
				if (RD(Pcc) == ima)
				{
					if (nxline())
					{
						WR(Pcc, RD(Cur));
						Ptr = Cur + 2;
					}
					else
					{
						break;
					}
				}
				else
				{
					if (fndln())
					{
						WR(Pcc, RD(Cur));
						Ptr = Cur + 2;
					}
					else
					{
						break;
					}
				}
			}
		}
	}
	else
	{
		if (Val == 0) //list
		{
			int pt;
			for (pt = Obj; pt < RD(Bnd); pt++)
			{
				putint(RD(pt));
				pt += 1;
				pt = putl(pt, '\0');
				crlf();
			}
		}
		else
		{
			int src;
			int dst;
			int m;
			int h;
			int f = 0;
			WR(Pcc, Val);
			if (fndln())
			{
				src = nxtln(Cur);
				for (dst = Cur; src != RD(Bnd); dst++, src++)
				{
					WR(dst, RD(src));
				}
				WR(Bnd, dst);
			}
			if (nxline())
			{
				f = 1;
			}
			src = RD(Bnd);
			for (m = 2, h = Ptr; RD(h) != '\0'; m++)
			{
				h++;
			}
			if (m > 2)
			{
				WR(Bnd, src + m);
				if (f == 1)
				{
					Cur--;
					for (dst = RD(Bnd); src != Cur; dst--, src--)
					{
						WR(dst, RD(src));
					}
					src++;
				}
				WR(src, Val);
				src += 1;
				for (i = 1; i < m; i++)
				{
					WR(src++, RD(Ptr++));
				}
			}
		}
	}
	for (i = 0; i < 129; i++)
	{
		WR(i + Lbf, '\0');
	}
	Ptr = Lbf;
	putstr("OK> ");
}
uint8 scan(void) {
	static uint8 key = 0;
	uint8 scan = inb(0x60);
	if (scan != key)
		return key = scan;
	else
		return 0;
}
void kernel_entry() {
	init_vga(WHITE, BLUE);
	print_string("ore gengo v1.0");
	print_new_line();
	char ch = 0;
	char md = 0;
	char sc = 0;
	while (1)
	{
		sleep(0x00FFFFFF);
		sc = scan();
		if (sc == 0x2a)
		{
			md = 1;
		}
		else if (sc == 0x36)
		{
			md = 1;
		}
		else
		{
			if (sc > 0)
			{
				if (md == 1)
				{
					ch = upper_ascii_codes_jp[sc];
					md = 0;
				}
				else
				{
					ch = lower_ascii_codes_jp[sc];
				}
				if (ch == 10)
				{
					print_new_line();
					WR(Ptr++, '\0');
					run();
				}
				else
				{
					putchr(ch);
					WR(Ptr++, ch);
				}
			}
		}
	}
}





以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?