Преглед изворни кода

2.156u-modules: support colouring printf

rain пре 5 година
родитељ
комит
6ad029cd20
11 измењених фајлова са 67 додато и 44 уклоњено
  1. 1 1
      LINES
  2. 3 2
      Makefile
  3. 5 0
      README.md
  4. 0 4
      TODO.md
  5. 19 4
      include/modules/tty.h
  6. 7 2
      init/head.S
  7. 1 1
      init/main.c
  8. 3 3
      kernel/int.c
  9. 7 2
      kernel/tty.S
  10. 0 1
      lines
  11. 21 24
      modules/tty.c

+ 1 - 1
LINES

@@ -8,5 +8,5 @@
 2021-5-30 1.706: 1706 lines, 18 files, rain
 2021-6-02 1.854: 1854 lines, 20 files, rain
 2021-6-02 2.015: 2015 lines, 21 files, rain
-
+2021-6-15 2.156: 2156 lines, 21 files, rain
 

+ 3 - 2
Makefile

@@ -15,15 +15,16 @@ kbinary: $(KSRCS) boot/bootsect Makefile
 
 vmxnix: vmxnix
 	gcc kernel.o -e init vmxnix
+
+
 xnix.img: boot/bootsect $(KSRCS) Makefile kbinary
 	dd if=/dev/zero of=xnix.img bs=1024 count=1440
 	dd if=boot/bootsect of=xnix.img conv=notrunc seek=0
 	dd if=kbinary of=xnix.img conv=notrunc bs=512 seek=2
 
 
-lines: Makefile
+lines: Makefile $(KSRCS)
 	wc -l `find ./ -name *.asm -o -name *.c -o -name *.h -o -name *.S -o -name *.s`
-	wc -l `find ./ -name *.asm -o -name *.c -o -name *.h -o -name *.S -o -name *.s`|grep total|awk '{print $1}'>lines
 
 
 clear: Makefile

+ 5 - 0
README.md

@@ -47,3 +47,8 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;add handler for divide-error (always divide by zero). `do_divide_error` write in C, it pucs a message and return. 
 &nbsp;&nbsp;&nbsp;&nbsp;`divide_error` is an assembly function. it saves all registers, and call `do_divide_error`. then it recover all registers and iretq. 
 
+
+2.156u-modules:  
+&nbsp;&nbsp;&nbsp;&nbsp;now we can support colouring printf, and I think it's beautifuller than VGA (well...). 
+&nbsp;&nbsp;&nbsp;&nbsp;...and, loader loaded kernel to 0x100000 (1MB). 
+

+ 0 - 4
TODO.md

@@ -1,4 +0,0 @@
-v0.1功能
-# 基础的进程调度(calvinlin)
-# 2个内核进程(calvinlin)
-# 简单的fs实现(calvinlin,rain)

+ 19 - 4
include/modules/tty.h

@@ -48,20 +48,20 @@
 
 void tty_init(void);
 
-void putc(char c);
+void putc(char c, char color);
 
 /* kernel/modules/tty.c */
 
 /* return value: length of string printed */
 
 /* we use number formats (1B per format), not % */
-__uint64_t printf(const char *fmt, ...);
+__uint64_t printf(char color, const char *fmt, ...);
 
-__uint64_t printint(long, int, int);
+__uint64_t printint(long, int, int, char);
 
 __uint16_t read_cursor(void);
 
-__uint64_t printptr(__uint64_t, int);
+__uint64_t printptr(__uint64_t, int, char);
 
 void write_cursor(__uint16_t pos);
 
@@ -71,4 +71,19 @@ void clear_line(int line);
 #endif
 
 
+#define BLACK   0
+#define BLUE    1
+#define GREEN   2
+#define CYAN    3
+#define RED     4
+#define MAGENTA 5
+#define BROWN   6
+#define GRAY    7
+
+#define COLOR_BRIGHT 8
+
+#define WHITE   (COLOR_BRIGHT + GRAY)
+#define YELLOW  (COLOR_BRIGHT + BROWN)
+
+
 #endif

+ 7 - 2
init/head.S

@@ -23,6 +23,9 @@
 
 
 
+/* it is BRIGHT+RED (well...) */
+#define RED $0x0c
+
 .text
 
 .align 8
@@ -106,7 +109,8 @@ init_die:
 
 
 init_panic:
-	leaq panic_msg(%rip), %rdi
+	movq RED, %rdi
+	leaq panic_msg(%rip), %rsi
 	callq printf
 
 	jmp init_die
@@ -114,7 +118,8 @@ init_panic:
 
 default_int_handler:
 	/* put a message on screen, and die */
-	leaq int_msg(%rip), %rdi
+	movq RED, %rdi
+	leaq int_msg(%rip), %rsi
 	call printf
 
 dih_die:

+ 1 - 1
init/main.c

@@ -42,7 +42,7 @@
  * */
 
 int init(void) {
-	printf("XNIX kernel init, 1 + 1 = %d\n", 1 + 1);
+	printf(WHITE, "XNIX kernel init, 1 + 1 = %d\n", 1 + 1);
 
 	init_int();
 

+ 3 - 3
kernel/int.c

@@ -85,18 +85,18 @@ void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
 
 
 void syscall(void) {
-	printf("int #0x80: system call. \n");
+	printf(COLOR_BRIGHT + RED, "int #0x80: system call. \n");
 	for (;;);
 }
 
 
 void die(char *s, __uint64_t *rsp, char nr) {
-	printf("int #%b: %s. \n");
+	printf(COLOR_BRIGHT + RED, "int #%b: %s. \n");
 
 	int i;
 
 	for (i = 0; i < 50; i++) 
-		printf("stack: %q\n", rsp[i]);
+		printf(COLOR_BRIGHT + RED, "stack: %q\n", rsp[i]);
 
 	for (;;);
 }

+ 7 - 2
kernel/tty.S

@@ -54,13 +54,17 @@ tty_init:
 
 putc:
 	pushq %rdi
+	pushq %rsi
 
 	/* character is in EDI */
-	movl %edi, %eax
+	shlw $8, %si
+	movw %si, %ax
+
+	andw $0xff, %di
+	addw %di, %ax
 
 	/* rdi = *framebuffer */
 	movq framebuffer(%rip), %rdi
-	movb $0x0f, %ah
 
 	cld
 	stosw
@@ -70,6 +74,7 @@ putc:
 	/* return 0 */
 	xorq %rax, %rax
 
+	popq %rsi
 	popq %rdi
 	ret
 

+ 0 - 1
lines

@@ -1 +0,0 @@
- 2134 total

+ 21 - 24
modules/tty.c

@@ -81,7 +81,7 @@ void update_cursor(void) {
 
 
 
-__uint64_t printf(const char *s, ...) {
+__uint64_t printf(char color, const char *s, ...) {
 	/* l: character puts (no '%') */
 	__uint64_t i, l;
 	/* if prev character is '%', state = '%' */
@@ -96,8 +96,8 @@ __uint64_t printf(const char *s, ...) {
 	state = 0;
 	newlines = 0;
 
-	/* argno = 2 because first argument is 's' */
-	for (i = 0, l = 0, argno = 2; s[i]; i++) {
+	/* argno = 3 because first argument is 's' */
+	for (i = 0, l = 0, argno = 3; s[i]; i++) {
 		c = s[i];
 		/* check state */
 		if (!state) {
@@ -115,7 +115,7 @@ __uint64_t printf(const char *s, ...) {
 				}
 
 				else {
-					putc(c);
+					putc(c, color);
 					l++;
 				}
 
@@ -126,42 +126,42 @@ __uint64_t printf(const char *s, ...) {
 		/* it's a formater */
 		else {
 			if (c == 'd') {
-				l += printint((int) get_arg(int, argno), 10, 1);
+				l += printint((int) get_arg(int, argno), 10, 1, color);
 				argno++;
 			}
 
 			else if (c == 'x') {
 				/* `x` is 64-bits, I think it's */
-				l += printint((long) get_arg(long, argno), 16, 0);
+				l += printint((long) get_arg(long, argno), 16, 0, color);
 				argno++;
 			}
 
 			else if (c == 'p') {
 				/* zeros at high bits should display, not like `%x` or `%d` */
-				l += printptr((__uint64_t) get_arg(__uint64_t, argno), 16);
+				l += printptr((__uint64_t) get_arg(__uint64_t, argno), 16, color);
 				argno++;
 			}
 			
 
 			/* put a byte */
 			else if (c == 'b') {
-				l += printptr((__uint64_t) get_arg(__uint32_t, argno), 2);
+				l += printptr((__uint64_t) get_arg(__uint32_t, argno), 2, color);
 				argno++;
 			}
 
 			/* put a word */
 			else if (c == 'w') {
-				l += printptr((__uint64_t) get_arg(__uint32_t, argno), 4);
+				l += printptr((__uint64_t) get_arg(__uint32_t, argno), 4, color);
 				argno++;
 			}
 
 			else if (c == 'l') {
-				l += printptr((__uint64_t) get_arg(__uint32_t, argno), 8);
+				l += printptr((__uint64_t) get_arg(__uint32_t, argno), 8, color);
 				argno++;
 			}
 
 			else if (c == 'q') {
-				l += printptr((__uint64_t) get_arg(__uint64_t, argno), 16);
+				l += printptr((__uint64_t) get_arg(__uint64_t, argno), 16, color);
 				argno++;
 			}
 			
@@ -176,7 +176,7 @@ __uint64_t printf(const char *s, ...) {
 						
 				/* put while end of fmt_s */
 				while (*fmt_s) {
-					putc(*fmt_s);
+					putc(*fmt_s, color);
 					fmt_s++;
 					l++;
 				}
@@ -184,21 +184,21 @@ __uint64_t printf(const char *s, ...) {
 			
 			else if (c == 'c') {
 				/* `char` aligned to `int` in GCC */
-				putc((char) get_arg(int, argno));
+				putc((char) get_arg(int, argno), color);
 				argno++;
 				l++;
 			}
 
 			else if (c == '%') {
 				/* put % on screen */
-				putc(c);
+				putc(c, color);
 				l++;
 			}
 
 			else {
 				/* don't forget to put % */
-				putc('%');
-				putc(c);
+				putc('%', color);
+				putc(c, color);
 				l++;
 			}
 
@@ -232,11 +232,8 @@ __uint64_t printf(const char *s, ...) {
 
 static char digits[] = {"0123456789abcdef"};
 
-int printk(char txt){
-	printint(txt);
-	return 0
-}
-__uint64_t printint(long n, int base, int sign) {
+
+__uint64_t printint(long n, int base, int sign, char color) {
 	char buf[32];
 	/* neg: x < 0 && signed interger */
 	int i, neg, j;
@@ -265,7 +262,7 @@ __uint64_t printint(long n, int base, int sign) {
 
 	j = i;
 	while (--j >= 0) 
-		putc(buf[j]);
+		putc(buf[j], color);
 
 	return i;
 }
@@ -273,7 +270,7 @@ __uint64_t printint(long n, int base, int sign) {
 
 
 
-__uint64_t printptr(__uint64_t p, int width) {
+__uint64_t printptr(__uint64_t p, int width, char color) {
 	int i;
 	
 	for (i = width - 1; i >= 0; i--) {
@@ -285,7 +282,7 @@ __uint64_t printptr(__uint64_t p, int width) {
 		else 
 			c += '0';
 
-		putc(c);
+		putc(c, color);
 	}
 
 	return width;