Просмотр исходного кода

2.040s-modules: display PSF font on VGA 320x200 screen

rain 5 лет назад
Родитель
Сommit
10994f1678
23 измененных файлов с 900 добавлено и 553 удалено
  1. 1 0
      LINES
  2. 17 10
      Makefile
  3. 3 0
      README.md
  4. 55 76
      boot/boot.asm
  5. 153 0
      boot/boot.lst
  6. 2 44
      boot/loader.asm
  7. 285 0
      boot/loader.lst
  8. BIN
      font.psf
  9. 94 0
      include/kernel/graphic.h
  10. 0 59
      include/modules/tty.h
  11. 2 2
      init/head.S
  12. 3 4
      init/main.c
  13. 1 1
      kernel.lds
  14. 1 1
      kernel/Makefile
  15. 118 0
      kernel/graphic.c
  16. 3 3
      kernel/int.c
  17. 0 1
      kernel/trap.S
  18. 0 88
      kernel/tty.S
  19. 1 1
      modules/Makefile
  20. 26 263
      modules/tty.c
  21. 14 0
      tools/Makefile
  22. 59 0
      tools/bin2s.c
  23. 62 0
      tools/psf.c

+ 1 - 0
LINES

@@ -9,4 +9,5 @@
 2021-6-02 1.854: 1854 lines, 20 files, rain
 2021-6-02 1.854: 1854 lines, 20 files, rain
 2021-6-02 2.015: 2015 lines, 21 files, rain
 2021-6-02 2.015: 2015 lines, 21 files, rain
 2021-6-15 2.156: 2156 lines, 21 files, rain
 2021-6-15 2.156: 2156 lines, 21 files, rain
+2021-6-20 2.040: 2040 lines, 25 files, rain
 
 

+ 17 - 10
Makefile

@@ -1,23 +1,31 @@
 OBJS = boot/bootsect 
 OBJS = boot/bootsect 
-KOBJS = init/head.o init/main.o modules/tty.o kernel/tty.o kernel/int.o kernel/trap.o
-KSRCS = init/head.S init/main.c modules/tty.c kernel/tty.S kernel/int.c kernel/trap.S
+KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/graphic.o font.o
+KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/graphic.c 
 
 
 all: Makefile
 all: Makefile
+	make -C tools/
 	make -C boot/
 	make -C boot/
 	make -C init/
 	make -C init/
 	make -C modules/
 	make -C modules/
 	make -C kernel/ 
 	make -C kernel/ 
 	make xnix.img
 	make xnix.img
+	make vmxnix
 
 
-kbinary: $(KSRCS) boot/bootsect Makefile
+
+font.o: tools/bin2s font.psf 
+	tools/bin2s font font.psf > font.asm
+	nasm font.asm -o font.o -f elf64
+
+
+kbinary: $(KSRCS) $(KOBJS) boot/bootsect Makefile
 	ld -b elf64-x86-64 -o kernel.o $(KOBJS) -T kernel.lds
 	ld -b elf64-x86-64 -o kernel.o $(KOBJS) -T kernel.lds
 	objcopy -I elf64-x86-64 -S -R ".comment" -R ".eh_frame" -O binary kernel.o kbinary
 	objcopy -I elf64-x86-64 -S -R ".comment" -R ".eh_frame" -O binary kernel.o kbinary
 
 
-vmxnix: vmxnix
-	gcc kernel.o -e init vmxnix
+vmxnix: $(KSRCS) $(KOBJS) Makefile 
+	ld -b elf64-x86-64 $(KOBJS) -e init -T kernel.lds -o vmxnix
 
 
 
 
-xnix.img: boot/bootsect $(KSRCS) Makefile kbinary
+xnix.img: boot/bootsect $(KSRCS) $(KOBJS) Makefile kbinary
 	dd if=/dev/zero of=xnix.img bs=1024 count=1440
 	dd if=/dev/zero of=xnix.img bs=1024 count=1440
 	dd if=boot/bootsect of=xnix.img conv=notrunc seek=0
 	dd if=boot/bootsect of=xnix.img conv=notrunc seek=0
 	dd if=kbinary of=xnix.img conv=notrunc bs=512 seek=2
 	dd if=kbinary of=xnix.img conv=notrunc bs=512 seek=2
@@ -30,10 +38,9 @@ lines: Makefile $(KSRCS)
 clear: Makefile
 clear: Makefile
 	make -C boot/ clear
 	make -C boot/ clear
 	make -C init/ clear
 	make -C init/ clear
-	make -C modules/ clear
 	make -C kernel/ clear
 	make -C kernel/ clear
+	make -C tools/ clear
 	rm boot/bootsect
 	rm boot/bootsect
-	rm xnix.img kbinary kernel.o
-
-
+	rm xnix.img kbinary kernel.o vmxnix font.o font.asm 
+	make -C modules/ clear
 
 

+ 3 - 0
README.md

@@ -52,3 +52,6 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;now we can support colouring printf, and I think it's beautifuller than VGA (well...). 
 &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). 
 &nbsp;&nbsp;&nbsp;&nbsp;...and, loader loaded kernel to 0x100000 (1MB). 
 
 
+
+2.040u-modules:  
+&nbsp;&nbsp;&nbsp;&nbsp;now we use graphic.c to display PSF font on VGA 320x200 screen. (but it just can display upper characters, can't display some lower character)

+ 55 - 76
boot/boot.asm

@@ -19,6 +19,8 @@
 
 
 org 0x7c00
 org 0x7c00
 
 
+cyldsneed equ 10
+
 
 
 ; yeah, it's really 0x7c00 because I don't want to use other memory. 
 ; yeah, it's really 0x7c00 because I don't want to use other memory. 
 
 
@@ -41,27 +43,11 @@ _start:
 	mov si, boot_msg
 	mov si, boot_msg
 	call print
 	call print
 
 
+	call read_it
 
 
-	xor ax, ax
-	mov bx, 0x7e00
-
-.read_it:
-	inc ax
-	mov cl, 1
-
-	push ax
-	push bx
-
-	call read_sector
-
-	pop bx
-	pop ax
-
-	add bx, 512
-
-	cmp ax, 64
-
-	jne .read_it
+.set_vga:
+	mov ax, 0x0013
+	int 0x10
 
 
 	jmp 0x0000:0x7e00
 	jmp 0x0000:0x7e00
 
 
@@ -84,88 +70,81 @@ print:
 	ret
 	ret
 
 
 
 
+; NOTE: QEMU's HD only supports LBA mode. So this MBR can't load it successfully. :-)
 
 
-read_sector:
-	; LBA address in AX, and sectors count in CL, buffer in ES:BX
-	
-	; LBA to CHS: 
-	; TEMP     = LBA / SECTORS_PER_TRACK
-	; SECTOR   = (LBA % SECTORS_PER_TRACK) + 1
-	; CYLINDER = TEMP >> 1
-	; HEAD     = TEMP & 1
-
-	; DIV: unsigned divide
-	; 12345 / 123: 
-	; mov ax, 12345		: dividend
-	; mov bl, 123		: divisor
-	; AL: quotient
-	; AH: remainder
-
-	; 18: sectors per track for 1.44M floppy (and for my u-disk)
-
-	; save bx first
-	push bx
-	
-	mov bl, 18
-	div bl
-
-	; BIOS standard interrupt: 0x13 AH = 0x02: read disk in CHS mode
-	; AL: sectors count
-	; CH: cylinder number low
-	; CL: 0-5b: sector number, 6-7b: cylinder number (disk only)
+read_it:
+	; BIOS 0x13, AH = 0x02: read disk in CHS mode
+	; AL: sectors will read
+	; CH: cylinder
+	; CL: sector start
 	; DH: head
 	; DH: head
 	; DL: driver
 	; DL: driver
-	; ES:BX: buffer
+	; BX: buffer address (segment register ES)
 
 
-	; SECTOR = AH + 1
-	; but CL used to save sectors count, so we save it first. 
-	push cx
-
-	; we inc ah because it's shorter than inc cl
-
-	inc ah
-	mov cl, ah
-
-	; CH: CYLINDER = (LBA / SECTORS_PER_TRACK) >> 1
+	; segment address
+	mov ax, 0x07e0
+	mov es, ax
 
 
+	mov ch, 0
+	mov dh, 0
 
 
-	mov dh, al
-	and dh, 1
+	; sector 1 is MBR (this code)
+	mov cl, 2
 
 
-	shr al, 1
-	mov ch, al
+.retry:
+	mov ah, 0x02
 
 
-	; for 'push cx'
-	pop ax
+	; only read one sectors
+	mov al, 1
+	mov bx, 0x0000
 
 
-	; for 'push bx'
-	pop bx
-	
 	mov dl, [BOOTDRIVER]
 	mov dl, [BOOTDRIVER]
 	
 	
-	; okay, now we read it
-
-	mov ah, 2
 	int 0x13
 	int 0x13
 
 
 	jc read_failed
 	jc read_failed
 
 
+.next_sector:
+	mov ax, es
+	; add 512 for memory address
+	add ax, 512 / 16
+	mov es, ax
+
+	; sector++
+	add cl, 1
+	; if sector > 18
+	cmp cl, 18
+	jbe .retry
+
+	; next head
+	mov cl, 1
+	; head++
+	add dh, 1
+	; if head == 2 (head can only be 0 or 1)
+	cmp dh, 2
+	jb .retry
+
+	; next cylinder
+	mov dh, 0
+	add ch, 1
+	
+	; if cylinder == CYLINDERS_NEED
+	cmp ch, cyldsneed
+	jb .retry
+
 	ret
 	ret
 
 
 
 
 read_failed:
 read_failed:
-	mov si, readdisk_fail_msg
+	mov si, read_fail_msg
 	call print
 	call print
 
 
 .die:	hlt
 .die:	hlt
 	jmp .die
 	jmp .die
 
 
-	; it never go here
-
 
 
 boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
 boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
-readdisk_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
-
+read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
 
 
 
 
 times 510 - ($ - $$) db 0x00
 times 510 - ($ - $$) db 0x00

+ 153 - 0
boot/boot.lst

@@ -0,0 +1,153 @@
+     1                                  ; Copyright (C) 2021 Rain
+     2                                  
+     3                                  ; This file is part of XNIX. 
+     4                                  ; XNIX is free software: you can redistribute it and/or modify 
+     5                                  ; it under the terms of the GNU General Public License as published by 
+     6                                  ; the Free Software Foundation, either version 3 of the License, or 
+     7                                  ; (at your option) and later version. 
+     8                                  
+     9                                  ; XNIX is distributed in the hope that it will be useful, 
+    10                                  ; but WITHOUT ANY WARRANTY; without even the implied warranty of 
+    11                                  ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+    12                                  ; GNU General Public License for more details. 
+    13                                  
+    14                                  ; You should have received a copy of the GNU General Public License 
+    15                                  ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+    16                                  
+    17                                  
+    18                                  
+    19                                  
+    20                                  org 0x7c00
+    21                                  
+    22                                  cyldsneed equ 10
+    23                                  
+    24                                  
+    25                                  ; yeah, it's really 0x7c00 because I don't want to use other memory. 
+    26                                  
+    27                                  BOOTDRIVER equ 0x7c00
+    28                                  
+    29                                  _start:
+    30 00000000 31C0                    	xor ax, ax
+    31                                  
+    32 00000002 8ED8                    	mov ds, ax
+    33 00000004 8EC0                    	mov es, ax
+    34                                  
+    35 00000006 8ED0                    	mov ss, ax
+    36 00000008 BC007C                  	mov sp, 0x7c00
+    37                                  
+    38                                  	; save startup driver
+    39 0000000B 8816007C                	mov [BOOTDRIVER], dl
+    40                                  
+    41                                  	; display message 'Loading System'
+    42                                  
+    43 0000000F BE[7400]                	mov si, boot_msg
+    44 00000012 E80800                  	call print
+    45                                  
+    46 00000015 E81500                  	call read_it
+    47                                  
+    48 00000018 EA007E0000              	jmp 0x0000:0x7e00
+    49                                  
+    50                                  
+    51                                  
+    52                                  print:
+    53 0000001D FC                      	cld
+    54 0000001E AC                      	lodsb
+    55                                  
+    56 0000001F 84C0                    	test al,al
+    57 00000021 7409                    	jz .ret
+    58                                  
+    59 00000023 B40E                    	mov ah, 0x0e
+    60 00000025 BB0F00                  	mov bx, 0x0f
+    61 00000028 CD10                    	int 0x10
+    62                                  
+    63 0000002A EBF1                    	jmp print
+    64                                  
+    65                                  .ret:
+    66 0000002C C3                      	ret
+    67                                  
+    68                                  
+    69                                  ; NOTE: QEMU's HD only supports LBA mode. So this MBR can't load it successfully. :-)
+    70                                  
+    71                                  read_it:
+    72                                  	; BIOS 0x13, AH = 0x02: read disk in CHS mode
+    73                                  	; AL: sectors will read
+    74                                  	; CH: cylinder
+    75                                  	; CL: sector start
+    76                                  	; DH: head
+    77                                  	; DL: driver
+    78                                  	; BX: buffer address (segment register ES)
+    79                                  
+    80                                  	; segment address
+    81 0000002D B8E007                  	mov ax, 0x07e0
+    82 00000030 8EC0                    	mov es, ax
+    83                                  
+    84 00000032 B500                    	mov ch, 0
+    85 00000034 B600                    	mov dh, 0
+    86                                  
+    87                                  	; sector 1 is MBR (this code)
+    88 00000036 B102                    	mov cl, 2
+    89                                  
+    90                                  .retry:
+    91 00000038 B402                    	mov ah, 0x02
+    92                                  
+    93                                  	; only read one sectors
+    94 0000003A B001                    	mov al, 1
+    95 0000003C BB0000                  	mov bx, 0x0000
+    96                                  
+    97 0000003F 8A16007C                	mov dl, [BOOTDRIVER]
+    98                                  	
+    99 00000043 CD13                    	int 0x13
+   100                                  
+   101 00000045 7224                    	jc read_failed
+   102                                  
+   103                                  .next_sector:
+   104 00000047 8CC0                    	mov ax, es
+   105                                  	; add 512 for memory address
+   106 00000049 83C020                  	add ax, 512 / 16
+   107 0000004C 8EC0                    	mov es, ax
+   108                                  
+   109                                  	; sector++
+   110 0000004E 80C101                  	add cl, 1
+   111                                  	; if sector > 18
+   112 00000051 80F912                  	cmp cl, 18
+   113 00000054 76E2                    	jbe .retry
+   114                                  
+   115                                  	; next head
+   116 00000056 B101                    	mov cl, 1
+   117                                  	; head++
+   118 00000058 80C601                  	add dh, 1
+   119                                  	; if head == 2 (head can only be 0 or 1)
+   120 0000005B 80FE02                  	cmp dh, 2
+   121 0000005E 72D8                    	jb .retry
+   122                                  
+   123                                  	; next cylinder
+   124 00000060 B600                    	mov dh, 0
+   125 00000062 80C501                  	add ch, 1
+   126                                  	
+   127                                  	; if cylinder == CYLINDERS_NEED
+   128 00000065 80FD0A                  	cmp ch, cyldsneed
+   129 00000068 72CE                    	jb .retry
+   130                                  
+   131 0000006A C3                      	ret
+   132                                  
+   133                                  
+   134                                  read_failed:
+   135 0000006B BE[8800]                	mov si, read_fail_msg
+   136 0000006E E8ACFF                  	call print
+   137                                  
+   138 00000071 F4                      .die:	hlt
+   139 00000072 EBFD                    	jmp .die
+   140                                  
+   141                                  
+   142 00000074 4C6F6164696E672053-     boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
+   142 0000007D 797374656D2E2E2E0D-
+   142 00000086 0A00               
+   143 00000088 70616E69633A207265-     read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
+   143 00000091 6164206469736B2066-
+   143 0000009A 61696C65642E200D0A-
+   143 000000A3 00                 
+   144                                  
+   145                                  
+   146 000000A4 00<rep 15Ah>            times 510 - ($ - $$) db 0x00
+   147 000001FE 55AA                    db 0x55, 0xaa
+   148                                  

+ 2 - 44
boot/loader.asm

@@ -32,11 +32,6 @@ _start:
 	mov ss, ax
 	mov ss, ax
 	mov sp, 0x7c00
 	mov sp, 0x7c00
 
 
-	; display message 'Starting Loader...'
-
-	mov si, message
-	call print
-
 .open_a20:
 .open_a20:
 	; disable interrupts before we initial done (enable in kernel)
 	; disable interrupts before we initial done (enable in kernel)
 
 
@@ -66,23 +61,6 @@ _start:
 	jmp dword code32:init_prot
 	jmp dword code32:init_prot
 
 
 
 
-print:
-	cld
-	lodsb
-
-	test al,al
-	jz .ret
-
-	mov ah, 0x0e
-	mov bx, 0x0f
-	int 0x10
-
-	jmp print
-
-.ret:
-	ret
-
-
 bits 32
 bits 32
 
 
 init_prot:
 init_prot:
@@ -98,15 +76,6 @@ init_prot:
 
 
 	mov sp, 0x00007c00
 	mov sp, 0x00007c00
 
 
-	; copy kernel to 0x100000
-
-	mov esi, 0x8000
-	mov edi, 0x100000
-
-	mov ecx, 64 * 512
-
-	call memcpy
-
 	; now we switch to long-mode
 	; now we switch to long-mode
 
 
 .to_long_mode:
 .to_long_mode:
@@ -185,17 +154,7 @@ init_prot:
 
 
 	; okay! now set segment registers and init long mode
 	; okay! now set segment registers and init long mode
 	
 	
-	jmp code64:0x100000
-
-memcpy:
-	cld
-	rep movsb
-
-	ret
-
-
-
-message: db "Starting Loader", 0x0d, 0x0a, 0x00
+	jmp code64:0x8000
 
 
 
 
 alignb 16
 alignb 16
@@ -239,6 +198,5 @@ code64 equ gdt64_code - gdt64
 data64 equ gdt64_data - gdt64
 data64 equ gdt64_data - gdt64
 
 
 
 
-times 510 - ($ - $$) db 0x00
-db 0x55, 0xaa
+times 512 - ($ - $$) db 0x00
 
 

+ 285 - 0
boot/loader.lst

@@ -0,0 +1,285 @@
+     1                                  ; Copyright (C) 2021 Rain
+     2                                  
+     3                                  ; This file is part of XNIX. 
+     4                                  ; XNIX is free software: you can redistribute it and/or modify 
+     5                                  ; it under the terms of the GNU General Public License as published by 
+     6                                  ; the Free Software Foundation, either version 3 of the License, or 
+     7                                  ; (at your option) and later version. 
+     8                                  
+     9                                  ; XNIX is distributed in the hope that it will be useful, 
+    10                                  ; but WITHOUT ANY WARRANTY; without even the implied warranty of 
+    11                                  ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+    12                                  ; GNU General Public License for more details. 
+    13                                  
+    14                                  ; You should have received a copy of the GNU General Public License 
+    15                                  ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+    16                                  
+    17                                  
+    18                                  
+    19                                  
+    20                                  org 0x7e00
+    21                                  
+    22                                  
+    23                                  %include "boot/paging.asm"
+    24                              <1> ; Copyright (C) 2021 Rain
+    25                              <1> 
+    26                              <1> ; This file is part of XNIX. 
+    27                              <1> ; XNIX is free software: you can redistribute it and/or modify 
+    28                              <1> ; it under the terms of the GNU General Public License as published by 
+    29                              <1> ; the Free Software Foundation, either version 3 of the License, or 
+    30                              <1> ; (at your option) and later version. 
+    31                              <1> 
+    32                              <1> ; XNIX is distributed in the hope that it will be useful, 
+    33                              <1> ; but WITHOUT ANY WARRANTY; without even the implied warranty of 
+    34                              <1> ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+    35                              <1> ; GNU General Public License for more details. 
+    36                              <1> 
+    37                              <1> ; You should have received a copy of the GNU General Public License 
+    38                              <1> ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+    39                              <1> 
+    40                              <1> 
+    41                              <1> 
+    42                              <1> 
+    43                              <1> PATT_PRESENT 	equ 0x0001
+    44                              <1> PATT_RDWR	equ 0x0002
+    45                              <1> PATT_USER	equ 0x0004
+    46                              <1> PATT_CACHEMEM	equ 0x0008
+    47                              <1> PATT_NOCACHE	equ 0x0010
+    48                              <1> PATT_ACCESSED	equ 0x0020
+    49                              <1> PATT_DIRTY	equ 0x0040
+    50                              <1> PATT_PAT	equ 0x0080
+    51                              <1> PATT_GLOBAL	equ 0x0100
+    52                              <1> 
+    53                              <1> ; 0x83 it is. 
+    54                              <1> PAGE_KERNEL	equ PATT_PRESENT + PATT_RDWR + PATT_PAT
+    55                              <1> 
+    56                              <1> ; this is linux's 007. (8-based 007)
+    57                              <1> PAGE_USER	equ PATT_PRESENT + PATT_RDWR + PATT_USER
+    58                              <1> 
+    24                                  
+    25                                  
+    26                                  _start:
+    27 00000000 31C0                    	xor ax, ax
+    28                                  
+    29 00000002 8ED8                    	mov ds, ax
+    30 00000004 8EC0                    	mov es, ax
+    31                                  
+    32 00000006 8ED0                    	mov ss, ax
+    33 00000008 BC007C                  	mov sp, 0x7c00
+    34                                  
+    35                                  	; display message 'Starting Loader...'
+    36                                  
+    37 0000000B BE[EC00]                	mov si, message
+    38 0000000E E81E00                  	call print
+    39                                  
+    40                                  .open_a20:
+    41                                  	; disable interrupts before we initial done (enable in kernel)
+    42                                  
+    43                                  	; A20 line uses to control memory higher than 1MB. we can't access mem > 1MB when it's closing. 
+    44                                  	; So we must open it before switching to protect-mode. 
+    45                                  
+    46 00000011 FA                      	cli
+    47                                  
+    48 00000012 E492                    	in al, 0x92
+    49 00000014 0C02                    	or al, 0x02
+    50 00000016 E692                    	out 0x92, al
+    51                                  
+    52                                  .to_prot:
+    53                                  	; switch to protect-mode. 
+    54                                  	
+    55 00000018 0F0116[1801]            	lgdt [gdt32_desc]
+    56                                  	
+    57                                  	; CR0 (control register 0) uses to control CPU. It's first bit PE controls Protect-mode Enable. 
+    58 0000001D 0F20C0                  	mov eax, cr0
+    59 00000020 6683C801                	or eax, 1
+    60 00000024 0F22C0                  	mov cr0, eax
+    61                                  
+    62                                  	; CPU is in protect-mode now, but segment registers didn't initialze. 
+    63                                  	
+    64                                  	; We cannot 'mov cx, xxx'
+    65                                  
+    66 00000027 66EA[3F000000]0800      	jmp dword code32:init_prot
+    67                                  
+    68                                  
+    69                                  print:
+    70 0000002F FC                      	cld
+    71 00000030 AC                      	lodsb
+    72                                  
+    73 00000031 84C0                    	test al,al
+    74 00000033 7409                    	jz .ret
+    75                                  
+    76 00000035 B40E                    	mov ah, 0x0e
+    77 00000037 BB0F00                  	mov bx, 0x0f
+    78 0000003A CD10                    	int 0x10
+    79                                  
+    80 0000003C EBF1                    	jmp print
+    81                                  
+    82                                  .ret:
+    83 0000003E C3                      	ret
+    84                                  
+    85                                  
+    86                                  bits 32
+    87                                  
+    88                                  init_prot:
+    89                                  	; we set segment selectors for segment register (CS was set by far-jmp)
+    90 0000003F 66B81000                	mov ax, data32
+    91                                  
+    92 00000043 8ED8                    	mov ds, ax
+    93 00000045 8EC0                    	mov es, ax
+    94 00000047 8EE0                    	mov fs, ax
+    95 00000049 8EE8                    	mov gs, ax
+    96                                  
+    97 0000004B 8ED0                    	mov ss, ax
+    98                                  
+    99 0000004D 66BC007C                	mov sp, 0x00007c00
+   100                                  
+   101                                  	; copy kernel to 0x100000
+   102                                  
+   103 00000051 BE00800000              	mov esi, 0x8000
+   104 00000056 BF00001000              	mov edi, 0x100000
+   105                                  
+   106 0000005B B900800000              	mov ecx, 64 * 512
+   107                                  
+   108 00000060 E883000000              	call memcpy
+   109                                  
+   110                                  	; now we switch to long-mode
+   111                                  
+   112                                  .to_long_mode:
+   113                                  	; clear page map 0 - 0x4000 (0x3000-0x4000 maybe not used)
+   114 00000065 31FF                    	xor edi, edi
+   115                                  	; mov edi, 0x00000000
+   116                                  
+   117 00000067 31C0                    	xor eax, eax
+   118                                  	; store for 4096 times
+   119 00000069 B900100000              	mov ecx, 4096
+   120                                  
+   121 0000006E FC                      	cld
+   122 0000006F F3AB                    	rep stosd
+   123                                  
+   124                                  .init_paging:
+   125                                  	; we set page map at 0x00000000 (a good address)
+   126                                  
+   127 00000071 C70500000000071000-     	mov dword [0x00000000], 0x00001000 | PAGE_USER
+   127 0000007A 00                 
+   128                                  ;	mov dword [0x00000004], 0x00000000
+   129                                  
+   130 0000007B C70500080000071000-     	mov dword [0x00000800], 0x00001000 | PAGE_USER
+   130 00000084 00                 
+   131                                  ;	mov dword [0x00000804], 0x00000000
+   132                                  
+   133                                  	; and page directory pointer table
+   134                                  
+   135 00000085 C70500100000072000-     	mov dword [0x00001000], 0x00002000 | PAGE_USER
+   135 0000008E 00                 
+   136                                  ;	mov dword [0x00001004], 0x00000000
+   137                                  
+   138                                  	; page directory entry (2 MB)
+   139                                  
+   140 0000008F C70500200000830000-     	mov dword [0x00002000], 0x00000000 | PAGE_KERNEL
+   140 00000098 00                 
+   141                                  ;	mov dword [0x00002004], 0x00000000
+   142                                  
+   143 00000099 C70508200000830020-     	mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
+   143 000000A2 00                 
+   144                                  ;	mov dword [0x0000200c], 0x00000000
+   145                                  
+   146                                  .enable_long_mode:
+   147 000000A3 0F0115[36010000]        	lgdt [gdt64_desc]
+   148                                  
+   149 000000AA 66B81000                	mov ax, data64
+   150                                  
+   151 000000AE 8ED8                    	mov ds, ax
+   152 000000B0 8EC0                    	mov es, ax
+   153 000000B2 8EE0                    	mov fs, ax
+   154 000000B4 8EE8                    	mov gs, ax
+   155 000000B6 8ED0                    	mov ss, ax
+   156                                  
+   157                                  	; open PAE (page map level 3)
+   158 000000B8 0F20E0                  	mov eax, cr4
+   159 000000BB 0FBAE805                	bts eax, 5
+   160 000000BF 0F22E0                  	mov cr4, eax
+   161                                  
+   162                                  	; set Page Table Base Register
+   163                                  
+   164                                  	; mov eax, 0x00000000
+   165 000000C2 31C0                    	xor eax, eax
+   166 000000C4 0F22D8                  	mov cr3, eax
+   167                                  
+   168                                  	; enable long mode (must before paging)
+   169                                  
+   170 000000C7 B9800000C0              	mov ecx, 0xc0000080
+   171 000000CC 0F32                    	rdmsr
+   172                                  
+   173 000000CE 0FBAE808                	bts eax, 8
+   174 000000D2 0F30                    	wrmsr
+   175                                  
+   176                                  	; enable paging
+   177                                  
+   178 000000D4 0F20C0                  	mov eax, cr0
+   179                                  
+   180                                  	; PE again
+   181 000000D7 83C801                  	or eax, 1
+   182 000000DA 0FBAE81F                	bts eax, 31
+   183                                  	
+   184 000000DE 0F22C0                  	mov cr0, eax
+   185                                  
+   186                                  	; okay! now set segment registers and init long mode
+   187                                  
+   188 000000E1 EA000010000800          	jmp code64:0x100000
+   189                                  
+   190                                  memcpy:
+   191 000000E8 FC                      	cld
+   192 000000E9 F3A4                    	rep movsb
+   193                                  
+   194 000000EB C3                      	ret
+   195                                  
+   196                                  
+   197                                  
+   198 000000EC 5374617274696E6720-     message: db "Starting Loader", 0x0d, 0x0a, 0x00
+   198 000000F5 4C6F616465720D0A00 
+   199                                  
+   200                                  
+   201 000000FE ????                    alignb 16
+   202                                  
+   203                                  gdt32:
+   204 00000100 0000000000000000        	dd 0x00000000, 0x00000000
+   205                                  
+   206                                  gdt32_code:
+   207 00000108 FFFF0000009ACF00        	dd 0x0000ffff, 0x00cf9a00
+   208                                  
+   209                                  gdt32_data:
+   210 00000110 FFFF00000092CF00        	dd 0x0000ffff, 0x00cf9200
+   211                                  
+   212                                  gdt32_len equ $ - gdt32
+   213                                  
+   214                                  gdt32_desc:
+   215 00000118 1700                    	dw gdt32_len - 1
+   216 0000011A [00010000]              	dd gdt32
+   217                                  
+   218                                  code32 equ gdt32_code - gdt32
+   219                                  data32 equ gdt32_data - gdt32
+   220                                  
+   221                                  
+   222                                  
+   223                                  gdt64:
+   224 0000011E 0000000000000000        	dq 0x0000000000000000
+   225                                  
+   226                                  gdt64_code:
+   227 00000126 0000000000982000        	dq 0x0020980000000000
+   228                                  
+   229                                  gdt64_data:
+   230 0000012E 0000000000920000        	dq 0x0000920000000000
+   231                                  
+   232                                  gdt64_len equ $ - gdt64
+   233                                  
+   234                                  gdt64_desc:
+   235 00000136 1700                    	dw gdt64_len - 1
+   236 00000138 [1E010000]              	dd gdt64
+   237                                  
+   238                                  code64 equ gdt64_code - gdt64
+   239                                  data64 equ gdt64_data - gdt64
+   240                                  
+   241                                  
+   242 0000013C 00<rep C2h>             times 510 - ($ - $$) db 0x00
+   243 000001FE 55AA                    db 0x55, 0xaa
+   244                                  


+ 94 - 0
include/kernel/graphic.h

@@ -0,0 +1,94 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#ifndef INCLUDED_GRAPHIC_H
+#define INCLUDED_GRAPHIC_H
+
+
+#include <kernel/types.h>
+
+
+void putblock(__uint8_t, int, int, int, int);
+void putc(char, __uint8_t, __uint8_t);
+void print(__uint8_t, __uint8_t, char *);
+
+
+/* 
+ * we supports PSF format in XNIX. 
+ * in linux, PSF fonts are in /usr/share/kbd/consolefonts, and they are always compressed. 
+ * */
+
+
+#define PSF_MAGIC 0x864ab572
+
+struct psf_header {
+	__uint32_t magic;
+
+	/* zero */
+	__uint32_t version;
+
+	__uint32_t header_size;
+
+	/* zero if there's no unicode table */
+	__uint32_t flags;
+
+	/* in psf, a font calls glyph */
+	__uint32_t glyphs_count;
+
+	/* size of a glyph */
+	__uint32_t glyph_size;
+
+	__uint32_t height;
+	__uint32_t width;
+};
+
+
+/* '__uint64_t * ' can change to other types */
+/* because they are all just lables with an address */
+
+extern __uint8_t *font_start;
+extern __uint8_t *font_end;
+
+/* but 'font_size' must be __uint64_t because it's a value */
+extern __uint64_t font_size;
+
+
+
+#define FB_ADDR ((char *) 0xa0000)
+
+/* x and y in this vga mode */
+#define VGA_X   320
+#define VGA_Y   200
+
+/* this is default value */
+
+#define CHAR_HEIGHT 8
+#define CHAR_WIDTH  16
+
+#define X_CHARS (VGA_X / CHAR_WIDTH)
+#define Y_LINES (VGA_Y / CHAR_HEIGHT)
+
+#endif

+ 0 - 59
include/modules/tty.h

@@ -27,63 +27,4 @@
 #ifndef INCLUDED_TTY_H
 #ifndef INCLUDED_TTY_H
 #define INCLUDED_TTY_H
 #define INCLUDED_TTY_H
 
 
-/* index register of cursor */
-#define CURSOR_INDEX 0x03d4
-
-/* data register of cursor */
-#define CURSOR_DATA  0x03d5
-
-/* write to CURSOR_INDEX */
-#define CURSORCTL_POSHI 0x0e
-#define CURSORCTL_POSLO 0x0f
-
-#define TEXT_VGA_X 80
-#define TEXT_VGA_Y 25
-
-#ifndef ASSEMBLY
-
-#include <stdarg.h>
-
-/* kernel/asm/tty.c */
-
-void tty_init(void);
-
-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(char color, const char *fmt, ...);
-
-__uint64_t printint(long, int, int, char);
-
-__uint16_t read_cursor(void);
-
-__uint64_t printptr(__uint64_t, int, char);
-
-void write_cursor(__uint16_t pos);
-
-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
 #endif

+ 2 - 2
init/head.S

@@ -111,7 +111,7 @@ init_die:
 init_panic:
 init_panic:
 	movq RED, %rdi
 	movq RED, %rdi
 	leaq panic_msg(%rip), %rsi
 	leaq panic_msg(%rip), %rsi
-	callq printf
+//	callq printf
 
 
 	jmp init_die
 	jmp init_die
 
 
@@ -120,7 +120,7 @@ default_int_handler:
 	/* put a message on screen, and die */
 	/* put a message on screen, and die */
 	movq RED, %rdi
 	movq RED, %rdi
 	leaq int_msg(%rip), %rsi
 	leaq int_msg(%rip), %rsi
-	call printf
+//	call printf
 
 
 dih_die:
 dih_die:
 	hlt
 	hlt

+ 3 - 4
init/main.c

@@ -28,6 +28,7 @@
 #include <kernel/types.h>
 #include <kernel/types.h>
 
 
 #include <modules/tty.h>
 #include <modules/tty.h>
+#include <kernel/graphic.h>
 
 
 #include <arch/io.h>
 #include <arch/io.h>
 #include <arch/sys.h>
 #include <arch/sys.h>
@@ -42,12 +43,10 @@
  * */
  * */
 
 
 int init(void) {
 int init(void) {
-	printf(WHITE, "XNIX kernel init, 1 + 1 = %d\n", 1 + 1);
-
 	init_int();
 	init_int();
 
 
+	print(0x0f, 0x00, "XNIX KERNEL IS STARTING...\nHELLO, WORLD");
+
 	return 0;
 	return 0;
 }
 }
 
 
-
-

+ 1 - 1
kernel.lds

@@ -3,7 +3,7 @@ OUTPUT_ARCH(i386:x86-64)
 ENTRY(_start)
 ENTRY(_start)
 
 
 SECTIONS {
 SECTIONS {
-	. = 0x100000;
+	. = 0x8000;
 	.text : {
 	.text : {
 		_text = .;
 		_text = .;
 		*(.text)
 		*(.text)

+ 1 - 1
kernel/Makefile

@@ -1,5 +1,5 @@
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror -g
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror -g
-all: tty.o int.o trap.o
+all: int.o trap.o graphic.o 
 
 
 
 
 %.o: %.S
 %.o: %.S

+ 118 - 0
kernel/graphic.c

@@ -0,0 +1,118 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+#include <kernel/init.h>
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+#include <kernel/graphic.h>
+
+
+void putblock(__uint8_t color, int xs, int xe, int ys, int ye) {
+	int x, y;
+
+	char *fb = FB_ADDR;
+
+	for (y = ys; y <= ye; y++) {
+		for (x = xs; x <= xe; x++) 
+			fb[y * VGA_X + x] = color;
+	}
+	
+	return;
+}
+
+
+static int x = 0, y = 0;
+
+
+void putc(char c, __uint8_t fg, __uint8_t bg) {
+	if (c == '\n') {
+		/* it's '\n', but we use '\r\n' */
+		x = 0;
+		y += 16;
+
+		return;
+	}
+
+	if (c == '\r') {
+		x = 0;
+		return;
+	}
+
+	if (c == ' ') {
+		x += 8;
+		return;
+	}
+
+
+	struct psf_header *psf = (struct psf_header *) &font_start;
+
+	/* 
+	 * yeah, it's very ugly. but it just get address of this glyph, and check 
+	 * 'c' is really in this font. 
+	 * */
+
+	__uint8_t *glyph = (__uint8_t *) &font_start + psf->header_size + 
+		(c > 0 && c < psf->glyphs_count ? c : 0) * psf->glyph_size;
+
+	/* okay, now we put it on the screen */
+
+	int i, j;
+	char *p, d, mask;
+
+	for (i = 0; i < psf->height; i++) {
+		p = FB_ADDR + (y + i) * VGA_X + x;
+		d = glyph[i];
+
+		for (j = 0; j < 8; j++) {
+			mask = 0x80 >> j;
+
+			if (d & mask) 
+				p[j] = fg;
+			else 
+				p[j] = bg;
+		}
+	}
+
+	x += 8;
+
+	if (x >= VGA_X) {
+		x = 0;
+		y += 16;
+
+		/* but y will > Y_LINES */
+	}
+
+	return;
+}
+
+
+void print(__uint8_t fg, __uint8_t bg, char *s) {
+	for (; *s; s++) 
+		putc(*s, fg, bg);
+
+	return;
+}
+

+ 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) {
 void syscall(void) {
-	printf(COLOR_BRIGHT + RED, "int #0x80: system call. \n");
+//	printf(COLOR_BRIGHT + RED, "int #0x80: system call. \n");
 	for (;;);
 	for (;;);
 }
 }
 
 
 
 
 void die(char *s, __uint64_t *rsp, char nr) {
 void die(char *s, __uint64_t *rsp, char nr) {
-	printf(COLOR_BRIGHT + RED, "int #%b: %s. \n");
+//	printf(COLOR_BRIGHT + RED, "int #%b: %s. \n");
 
 
 	int i;
 	int i;
 
 
 	for (i = 0; i < 50; i++) 
 	for (i = 0; i < 50; i++) 
-		printf(COLOR_BRIGHT + RED, "stack: %q\n", rsp[i]);
+	//	printf(COLOR_BRIGHT + RED, "stack: %q\n", rsp[i]);
 
 
 	for (;;);
 	for (;;);
 }
 }

+ 0 - 1
kernel/trap.S

@@ -100,7 +100,6 @@ restore_all:
 
 
 __stack_chk_fail:
 __stack_chk_fail:
 	movq schkfail_msg(%rip), %rdi
 	movq schkfail_msg(%rip), %rdi
-	callq printf
 
 
 scf_die:
 scf_die:
 	hlt
 	hlt

+ 0 - 88
kernel/tty.S

@@ -1,88 +0,0 @@
-/* Copyright (C) 2021 Rain */
-
-/* This file is part of XNIX. */
-
-/* 
-  XNIX is free software: you can redistribute it and/or modify 
-  it under the terms of the GNU General Public License as published by 
-  the Free Software Foundation, either version 3 of the License, or 
-  (at your option) and later version. 
-*/
-
-/*
-  XNIX is distributed in the hope that it will be useful, 
-  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-  GNU General Public License for more details. 
-*/
-
-/*
-  You should have received a copy of the GNU General Public License 
-   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
-*/
-
-
-
-
-#define ASSEMBLY
-#include <modules/tty.h>
-
-
-
-.text
-.align 8
-
-
-
-.globl tty_init
-
-tty_init:
-	pushq %rsi
-	pushq %rdi
-
-	/* set cursor position */
-	movw $0x0000, %di
-	call write_cursor
-
-	popq %rdi
-	popq %rsi
-
-	ret
-	
-
-.globl putc
-
-putc:
-	pushq %rdi
-	pushq %rsi
-
-	/* character is in EDI */
-	shlw $8, %si
-	movw %si, %ax
-
-	andw $0xff, %di
-	addw %di, %ax
-
-	/* rdi = *framebuffer */
-	movq framebuffer(%rip), %rdi
-
-	cld
-	stosw
-
-	movq %rdi, framebuffer(%rip)
-	
-	/* return 0 */
-	xorq %rax, %rax
-
-	popq %rsi
-	popq %rdi
-	ret
-
-
-.data
-.align 8
-
-.globl framebuffer
-framebuffer: .quad 0x00000000000b8000
-
-

+ 1 - 1
modules/Makefile

@@ -1,4 +1,4 @@
-OBJS = tty.o
+OBJS = 
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -g
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -g
 
 
 all: $(OBJS)
 all: $(OBJS)

+ 26 - 263
modules/tty.c

@@ -1,16 +1,16 @@
 /* Copyright (C) 2021 Rain */
 /* Copyright (C) 2021 Rain */
 
 
-/* This file is part of Cunix. */
+/* This file is part of XNIX. */
 
 
 /* 
 /* 
-  Cunix is free software: you can redistribute it and/or modify 
+  XNIX is free software: you can redistribute it and/or modify 
   it under the terms of the GNU General Public License as published by 
   it under the terms of the GNU General Public License as published by 
   the Free Software Foundation, either version 3 of the License, or 
   the Free Software Foundation, either version 3 of the License, or 
   (at your option) and later version. 
   (at your option) and later version. 
 */
 */
 
 
 /*
 /*
-  Cunix is distributed in the hope that it will be useful, 
+  XNIX is distributed in the hope that it will be useful, 
   but WITHOUT ANY WARRANTY; without even the implied warranty of 
   but WITHOUT ANY WARRANTY; without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
   GNU General Public License for more details. 
   GNU General Public License for more details. 
@@ -18,286 +18,49 @@
 
 
 /*
 /*
   You should have received a copy of the GNU General Public License 
   You should have received a copy of the GNU General Public License 
-   along with Cunix. If not, see <https://www.gnu.org/licenses/>.  
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
 */
 */
 
 
 
 
 
 
-
 #include <kernel/init.h>
 #include <kernel/init.h>
-#include <kernel/types.h>
 #include <kernel/errno.h>
 #include <kernel/errno.h>
+#include <kernel/types.h>
 
 
 #include <modules/tty.h>
 #include <modules/tty.h>
+
 #include <arch/io.h>
 #include <arch/io.h>
 #include <arch/sys.h>
 #include <arch/sys.h>
+#include <arch/x86/int.h>
 
 
-#include <lib/string.h>
-
-#include <kernel/args.h>
-
-
-
-
-/* variables in .data will initialize by value 0 */
-int x, y;
-
-extern __uint64_t framebuffer;
-
-__uint16_t read_cursor(void) {
-	__uint16_t pos = 0;
-
-	outb(CURSORCTL_POSLO, CURSOR_INDEX);
-	pos |= inb(CURSOR_DATA);
-
-	outb(CURSORCTL_POSHI, CURSOR_INDEX);
-	pos |= ((__uint16_t) inb(CURSOR_DATA)) << 8;
-
-	return pos;
-}
-
-
-
-void write_cursor(__uint16_t pos) {
-	outb(CURSORCTL_POSLO, CURSOR_INDEX);
-	outb((__uint8_t) (pos & 0xff), CURSOR_DATA);
-
-	outb(CURSORCTL_POSHI, CURSOR_INDEX);
-	outb((__uint8_t) ((pos >> 8) & 0xff), CURSOR_DATA);
-
-	return;
-}
-
-
-
-void update_cursor(void) {
-	__uint16_t pos = y * TEXT_VGA_X + x;
-	write_cursor(pos);
-
-	framebuffer = 0xb8000 + pos * 2;
-
-	return;
-}
-
-
-
-__uint64_t printf(char color, const char *s, ...) {
-	/* l: character puts (no '%') */
-	__uint64_t i, l;
-	/* if prev character is '%', state = '%' */
-	char state, c;
-	/* fmt_s only use in %s, but we cannot define variables in switch-case */
-	char *fmt_s;
-
-	int argno;
-
-	int newlines;
-
-	state = 0;
-	newlines = 0;
-
-	/* argno = 3 because first argument is 's' */
-	for (i = 0, l = 0, argno = 3; s[i]; i++) {
-		c = s[i];
-		/* check state */
-		if (!state) {
-			if (c == '%') {
-				state = '%';
-				continue;
-			}
-
-			/* c != '%' (it's not a formater), put it */
-			else {
-				/* if it's \n */
-				if (c == '\n') {
-					/* new line, and reset x */
-					newlines++;
-				}
-
-				else {
-					putc(c, color);
-					l++;
-				}
 
 
-				continue;
-			}
-		}
 
 
-		/* it's a formater */
-		else {
-			if (c == 'd') {
-				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, 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, color);
-				argno++;
-			}
-			
-
-			/* put a byte */
-			else if (c == 'b') {
-				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, color);
-				argno++;
-			}
-
-			else if (c == 'l') {
-				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, color);
-				argno++;
-			}
-			
-			
-
-			else if (c == 's') {
-				fmt_s = (char *) get_arg(char *, argno);
-				argno++;
-
-				if (!fmt_s) 
-					fmt_s = "(null)";
-						
-				/* put while end of fmt_s */
-				while (*fmt_s) {
-					putc(*fmt_s, color);
-					fmt_s++;
-					l++;
-				}
-			}
-			
-			else if (c == 'c') {
-				/* `char` aligned to `int` in GCC */
-				putc((char) get_arg(int, argno), color);
-				argno++;
-				l++;
-			}
-
-			else if (c == '%') {
-				/* put % on screen */
-				putc(c, color);
-				l++;
-			}
-
-			else {
-				/* don't forget to put % */
-				putc('%', color);
-				putc(c, color);
-				l++;
-			}
-
-			state = 0;
-		}
-	}
-
-
-	/* update cursor position */
-	x += l;
-	if (x > TEXT_VGA_X) {
-		x = TEXT_VGA_X - x;
-		y++;
-	}
-
-	y += newlines;
-
-	if (newlines) 
-		x = 0;
-
-	if (y > TEXT_VGA_Y) {
-		y = 0;
-		clear_line(0);
-	}
-
-	update_cursor();
-
-	return i;
-}
- 
-
-static char digits[] = {"0123456789abcdef"};
-
-
-__uint64_t printint(long n, int base, int sign, char color) {
-	char buf[32];
-	/* neg: x < 0 && signed interger */
-	int i, neg, j;
-	/* x: real number should put */
-	__uint64_t x;
-
-	if (n < 0 && sign) {
-		neg = 1;
-		x = -n;
-	}
-
-	else {
-		neg = 0;
-		x = n;
-	}
-
-	i = 0;
-	do {
-		/* get string of based-number */
-		/* the first bit of 10-based digit is at buf[15] */
-		buf[i++] = digits[x % base];
-	} while (x /= base);
-
-	if (neg) 
-		buf[i++] = '-';
-
-	j = i;
-	while (--j >= 0) 
-		putc(buf[j], color);
-
-	return i;
-}
-
-
-
-
-__uint64_t printptr(__uint64_t p, int width, char color) {
-	int i;
-	
-	for (i = width - 1; i >= 0; i--) {
-		char c = (p >> (i * 4)) & 0x0f;
-
-		if (c > 9) 
-			c = c - 10 + 'a';
-		
-		else 
-			c += '0';
+/* 
+ * we supports PSF format in XNIX. 
+ * in linux, PSF fonts are in /usr/share/kbd/consolefonts, and they are always compressed. 
+ * */
 
 
-		putc(c, color);
-	}
 
 
-	return width;
-}
+#define PSF_MAGIC 0x864ab572
 
 
+struct psf_header {
+	__uint32_t magic;
 
 
-#define LINE_ADDR(l) ((char *) (__uint64_t) (0xb8000 + (l) * TEXT_VGA_X * 2))
+	/* zero */
+	__uint32_t version;
 
 
+	__uint32_t header_size;
 
 
-void clear_line(int line) {
-	char *p = LINE_ADDR(line);
+	/* zero if there's no unicode table */
+	__uint32_t flags;
 
 
-	memsetw(p, 0x0720, TEXT_VGA_X * 2);
+	/* in psf, a font calls glyph */
+	__uint32_t glyphs_count;
 
 
-	return;
-}
+	/* size of a glyph */
+	__uint32_t glyph_size;
 
 
+	__uint32_t height;
+	__uint32_t width;
+};
 
 

+ 14 - 0
tools/Makefile

@@ -0,0 +1,14 @@
+OBJS =  readpsf bin2s
+
+all: $(OBJS)
+
+clear:
+	rm $(OBJS)
+
+readpsf: psf.c
+	gcc psf.c -Wall -Werror -o readpsf
+
+bin2s: bin2s.c
+	gcc bin2s.c -Wall -Werror -o bin2s
+
+

+ 59 - 0
tools/bin2s.c

@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/types.h>
+#include <string.h>
+#include <stdlib.h>
+
+
+
+int main(int argc, const char **argv) {
+	/* 
+	 * argv[1]: name (like 'font')
+	 * argv[2]: binary file
+	 * */
+
+	const char *name = argv[1];
+	const char *filename = argv[2];
+
+	if (!name || !filename) {
+		printf("fatal: no enough arguments. \n");
+		printf("usage: bin2s name filename\n");
+
+		return 1;
+	}
+
+	int fd = open(filename, O_RDONLY);
+
+	printf("section data\nglobal %s_start\n%s_start:", name, name);
+
+	/* read one byte from file, and put it in hex mode. */
+
+	char c;
+
+	for (;;) {
+		int i;
+
+		printf("\n\tdb ");
+
+		for (i = 0; i < 5; i++) {
+			if (!read(fd, &c, 1)) 
+				goto done;
+
+			if (i) 
+				printf(", ");
+			
+			printf("0x%02x", c & 0xff);
+		}
+	}
+
+done:
+
+	printf("\nglobal %s_end\n%s_end:\n", name, name);
+	printf("global %s_size\n%s_size: dq $ - %s_start\n", name, name, name);
+
+	close(fd);
+
+	return 0;
+}

+ 62 - 0
tools/psf.c

@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/types.h>
+#include <string.h>
+#include <stdlib.h>
+
+
+/* 
+ * we supports PSF format in XNIX. 
+ * in linux, PSF fonts are in /usr/share/kbd/consolefonts, and they are always compressed. 
+ * */
+
+
+#define PSF_MAGIC 0x864ab572
+
+struct psf_header {
+	__uint32_t magic;
+
+	/* zero */
+	__uint32_t version;
+
+	__uint32_t header_size;
+
+	/* zero if there's no unicode table */
+	__uint32_t flags;
+
+	/* in psf, a font calls glyph */
+	__uint32_t glyphs_count;
+
+	/* size of a glyph */
+	__uint32_t glyph_size;
+
+	__uint32_t height;
+	__uint32_t width;
+} psf;
+
+
+int main(int argc, const char **argv) {
+	if (!argv[1]) {
+		printf("fatal error: no input file. \n");
+		return 1;
+	}
+
+	int fd = open(argv[1], O_RDONLY);
+
+	read(fd, (void *) &psf, sizeof(struct psf_header));
+
+	printf("magic: %x\n", psf.magic);
+	printf("version: %d\n", psf.version);
+	printf("header size: %d\n", psf.header_size);
+	printf("flags (zero if no unicode table): %d\n", psf.flags);
+	printf("glyphs count: %d\n", psf.glyphs_count);
+	printf("glyph size (byte): %d\n", psf.glyph_size);
+
+	printf("height: %d, width: %d\n", psf.height, psf.width);
+
+	close(fd);
+
+	return 0;
+}