feat(kernel): cga text mode driver

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-07-14 15:11:28 +02:00
parent baaeda937f
commit 7fcb200475
15 changed files with 405 additions and 189 deletions

View file

@ -12,7 +12,11 @@ SRCS = kernel.asm \
lock.inc \
gdt.inc \
isr.inc \
pic.inc
pic.inc \
idt.inc \
dev/at/pit.inc \
dev/at/cga.inc \
dev/at/kbd.inc
.PHONY: all
all: $(KERNEL)

131
kernel/dev/at/cga.inc Normal file
View file

@ -0,0 +1,131 @@
;; File: cga.inc
;;
;; Usefull links:
;; - <http://www.techhelpmanual.com/901-color_graphics_adapter_i_o_ports.html>
;; - <http://www.osdever.net/FreeVGA/vga/crtcreg.htm>
CGA_MEMORY = 0xC00B8000
CGA_COMMAND = 0x3d4
CGA_DATA = 0x3d5
CGA_CURSOR_HIGH = 0xE
CGA_CURSOR_LOW = 0xF
CGA_COLUMNS = 80
CGA_LINES = 24
;; Function: cga_getpos
;;
;; Out:
;; AX - cursor position
cga_getpos:
; read cursor position
xor ax, ax
xchg bx, bx
; get high
mov dx, CGA_COMMAND
mov al, CGA_CURSOR_HIGH
out dx, al
mov dx, CGA_DATA
in al, dx
xchg al, ah
; get low
mov dx, CGA_COMMAND
mov al, CGA_CURSOR_LOW
out dx, al
mov dx, CGA_DATA
in al, dx
ret
;; Function: cga_setpos
;;
;; In:
;; AX - cursor position
cga_setpos:
xchg bx, bx
mov dx, CGA_COMMAND
push ax
mov al, CGA_CURSOR_HIGH
out dx, al
pop ax
xchg al, ah
mov dx, CGA_DATA
out dx, al
mov dx, CGA_COMMAND
mov al, CGA_CURSOR_LOW
out dx, al
xchg al, ah
mov dx, CGA_DATA
out dx, al
ret
;; Function: cga_putc
;;
;; In:
;; AL - charactere to print
cga_putc:
push ebp
mov ebp, esp
sub esp, 0x2
push eax
call cga_getpos
mov [ebp-0x2], ax
pop eax
cmp al, LF
jne @f
mov ax, [ebp-0x2]
mov dl, CGA_COLUMNS
div dl
mov al, CGA_COLUMNS
sub al, ah
xor ah, ah
mov cx, [ebp-0x2]
add cx, ax
jmp .end
@@:
mov edx, CGA_MEMORY
xor ecx, ecx
mov cx, [ebp-0x2]
shl cx, 1
add edx, ecx
mov ah, 0x07
mov word [edx], ax
mov cx, [ebp-0x2]
inc cx
mov ax, cx
mov dl, CGA_COLUMNS
div dl
cmp al, CGA_LINES
jbe .end
mov [ebp-0x2], cx
; scroll up
push esi
push edi
mov ecx, CGA_COLUMNS*(CGA_LINES-1)
mov edi, CGA_MEMORY
mov esi, CGA_MEMORY+80*2
rep movsw
mov ecx, CGA_COLUMNS
xor ax, ax
mov edi, CGA_MEMORY+(CGA_COLUMNS*(CGA_LINES-1)*2)
rep stosw
pop esi
pop edi
mov cx, CGA_COLUMNS*(CGA_LINES-1)
.end:
mov ax, cx
call cga_setpos
leave
ret

18
kernel/dev/at/kbd.inc Normal file
View file

@ -0,0 +1,18 @@
;; File: kbd.inc
kbd_getc:
ret
kbd_irq:
pusha
mov esi, szMsgKbdIRQ
call klog
mov al, PIC_EOI
out PIC1_COMMAND, al
popa
iret
szMsgKbdIRQ db "KBD: IRQ", 0

29
kernel/dev/at/pit.inc Normal file
View file

@ -0,0 +1,29 @@
;; File: pit.inc
;;
;; Usefull links:
;; - <http://www.osdever.net/bkerndev/Docs/pit.htm>
PIT_CHANNEL0 = 0x40
PIT_CHANNEL1 = 0x40
PIT_CHANNEL2 = 0x42
PIT_COMMAND = 0x43
;; Function: pit_init
pit_init:
ret
;; Function: pit_irq
pit_irq:
pusha
;mov esi, szMsgPitIRQ
;call klog
mov al, PIC_EOI
out PIC1_COMMAND, al
popa
iret
szMsgPitIRQ db "PIT: IRQ", 0

2
kernel/dev/console.inc Normal file
View file

@ -0,0 +1,2 @@
;; File: console.inc

9
kernel/dev/dev.inc Normal file
View file

@ -0,0 +1,9 @@
;; File: dev.inc
struc Device {
.write dd ?
.read dd ?
}

View file

@ -1,3 +1,4 @@
;; File: gdt.inc
gdt:
; null descriptor
@ -30,3 +31,7 @@ gdt:
pGDT:
dw gdt.end - gdt - 1
dd gdt
gdt_set_tss:
ret

View file

@ -1,10 +1,51 @@
;; File: idt.inc
idt_set_table:
idt_set_gate:
mov [aInterruptGate + (ecx * 8)], ax
mov [aInterruptGate + (ecx * 8) + 2], word 0x08
mov [aInterruptGate + (ecx * 8) + 5], word 0x8E
shr eax, 16
mov [aInterruptGate + (ecx * 8) + 6], ax
ret
idt_setup:
xor ecx, ecx
@@:
mov eax, [aISRTable + (ecx * 4)]
call idt_set_gate
inc ecx
cmp ecx, 32
jb @b
mov eax, pit_irq
call idt_set_gate
inc ecx
mov eax, kbd_irq
call idt_set_gate
inc ecx
@@:
mov eax, irq_dummy
call idt_set_gate
inc ecx
cmp ecx, 0x30
jb @b
mov ecx, 0x42
mov eax, isr_syscall
call idt_set_gate
lidt [pIDT]
sti
ret
pIdt:
dw ?
dd ?
pIDT:
dw aInterruptGate.end-aInterruptGate-1
dd aInterruptGate
; variable: idt_entries
aInterruptGate:
dd 256*2 dup(0)
.end:

View file

@ -0,0 +1,2 @@
irq_dummy:
iret

View file

@ -18,54 +18,6 @@ forward
}
idt_setup:
xor ecx, ecx
@@:
mov eax, [aISRTable + (ecx * 4)]
mov [aInterruptGate + (ecx * 8)], ax
mov [aInterruptGate + (ecx * 8) + 2], word 0x08
mov [aInterruptGate + (ecx * 8) + 5], word 0x8E
shr eax, 16
mov [aInterruptGate + (ecx * 8) + 6], ax
inc ecx
cmp ecx, 32
jb @b
@@:
mov eax, irq_dummy
mov [aInterruptGate + (ecx * 8)], ax
mov [aInterruptGate + (ecx * 8) + 2], word 0x08
mov [aInterruptGate + (ecx * 8) + 5], word 0x8E
shr eax, 16
mov [aInterruptGate + (ecx * 8) + 6], ax
inc ecx
cmp ecx, 0x30
jb @b
mov ecx, 0x42
mov eax, isr_syscall
mov [aInterruptGate + (ecx * 8)], ax
mov [aInterruptGate + (ecx * 8) + 2], word 0x08
mov [aInterruptGate + (ecx * 8) + 5], word 0x8E
shr eax, 16
mov [aInterruptGate + (ecx * 8) + 6], ax
lidt [pIDT]
sti
ret
pIDT:
dw aInterruptGate.end-aInterruptGate-1
dd aInterruptGate
; variable: idt_entries
aInterruptGate:
dd 256*2 dup(0)
.end:
irq_dummy:
iret

View file

@ -60,10 +60,15 @@ kmain:
lgdt [pGDT]
; I don't think i need to reload segment cuz their value are already correct
call idt_setup
;int 0x42
call pit_init
mov eax, SYSCALL_EXIT
int 0x42
mov al, 'X'
call cga_putc
.halt:
hlt
@ -81,7 +86,11 @@ kmain:
include 'gdt.inc'
include 'syscall.inc'
include 'isr.inc'
include 'idt.inc'
include 'pic.inc'
include 'dev/at/pit.inc'
include 'dev/at/kbd.inc'
include 'dev/at/cga.inc'
szMsgKernelAlive db "Kernel (", VERSION_FULL , ") is alive", 0

View file

@ -34,7 +34,7 @@ klog_print:
or al, al
jz @f
out dx, al
;call klog_kputc
;call klog_kput
jmp @b
@@:
ret

View file

@ -1,3 +1,6 @@
;; File: coff.inc
;; Struc: COFFFileHdr
struc COFFFileHdr {
.f_magic dw ?
.f_nscns dw ?

View file

@ -16,9 +16,20 @@ rc4_init:
mov ebp, esp
mov eax, [ebp+8]
xor ecx, ecx
xor cx, cx
@@:
mov byte [eax], byte cx
inc cx
inc eax
cmp cx, 255
jne @b
xor cx, cx
mov eax, [ebp+8]
@@:
.loop:
.end:
leave