From 3c121e97f4f1eb85a41ab4059a53b31d49a0d129 Mon Sep 17 00:00:00 2001
From: Jarkko Toivanen <jt@jakest.us>
Date: Wed, 2 Aug 2023 20:16:43 +0300
Subject: [PATCH] Framebuffer putpixel LEGGOOO! (disabled paging lol)

---
 src/framebuffer.asm | 74 +++++++++++++++++++++++++++++++++++++++++
 src/mbootinfo.asm   |  7 ++++
 src/start32.asm     | 81 ++++++++++++++++++++++-----------------------
 3 files changed, 121 insertions(+), 41 deletions(-)
 create mode 100644 src/framebuffer.asm

diff --git a/src/framebuffer.asm b/src/framebuffer.asm
new file mode 100644
index 0000000..ad97385
--- /dev/null
+++ b/src/framebuffer.asm
@@ -0,0 +1,74 @@
+;; x, y, r, g, b
+putpixel:
+	push ebp
+	mov ebp, esp
+
+	push eax
+	push ebx
+	push ecx
+	push edi
+
+
+
+	; Destination
+	std
+	mov edi, [mbootinfo.fb_addr]
+	mov eax, [mbootinfo.fb_pitch]
+	mul dword [ebp+12]   ;y
+	add edi, eax
+
+	mov eax, [ebp+8]   ;x
+	mul byte [mbootinfo.fb_bytespp]
+	dec eax
+	add edi, eax
+
+	movzx eax, [mbootinfo.fb_bytespp]
+	dec eax
+	add edi, eax
+
+	; Blue
+	mov ebx, [ebp + 24]
+	mov cl, [mbootinfo.fb_bpos]
+	shl ebx, cl
+	or eax, ebx
+	; Green
+	mov ebx, [ebp + 20]
+	mov cl, [mbootinfo.fb_gpos]
+	shl ebx, cl
+	or eax, ebx
+	; Red
+	mov ebx, [ebp + 16]
+	mov cl, [mbootinfo.fb_rpos]
+	shl ebx, cl
+	or eax, ebx
+
+	cmp [mbootinfo.fb_bpp], 24
+	je .24bpp
+	jne .32bpp
+.done:
+	pop edi
+	pop ecx
+	pop ebx
+	pop eax
+
+	pop ebp
+	ret
+.32bpp:
+	stosb
+	shr eax, 8
+	stosb
+	shr eax, 8
+	stosb
+	shr eax, 8
+	stosb
+	jmp .done
+.24bpp:
+	;shr eax, 8
+	stosb
+	shr eax, 8
+	stosb
+	shr eax, 8
+	stosb
+	jmp .done
+
+.msg db 10, ":D", 10, 0
diff --git a/src/mbootinfo.asm b/src/mbootinfo.asm
index a12504a..1a6f559 100644
--- a/src/mbootinfo.asm
+++ b/src/mbootinfo.asm
@@ -65,6 +65,12 @@ mbootgetinfo:
 	mov edi, mbootinfo.fb_rpos
 	rep movsb
 
+.bytesppcalc:
+	xor eax, eax
+	mov al, [mbootinfo.fb_bpp]
+	mov ebx, 8
+	div bl
+	mov [mbootinfo.fb_bytespp], al
 .done:
 	pop edi
 	pop esi
@@ -116,3 +122,4 @@ mbootinfo:
 	.fb_gmasksize db 0
 	.fb_bpos db 0
 	.fb_bmasksize db 0
+	.fb_bytespp db 0
diff --git a/src/start32.asm b/src/start32.asm
index 55f5261..5d48f85 100644
--- a/src/start32.asm
+++ b/src/start32.asm
@@ -11,6 +11,9 @@ MB_HEADER_FLAGS      equ MB_PAGE_ALIGN or MB_MEMORY_INFO or MB_VIDEO_REQUEST
 MB_HEADER_FLAGS      equ MB_PAGE_ALIGN or MB_MEMORY_INFO or MB_VIDEO_REQUEST or MB_AOUT_KLUDGE
 MB_CHECKSUM          equ -(MB_HEADER_MAGIC + MB_HEADER_FLAGS)
 
+VWIDTH  equ 1024
+VHEIGHT equ 768
+
 multiboot:
 	dd MB_HEADER_MAGIC
 	dd MB_HEADER_FLAGS
@@ -21,8 +24,8 @@ multiboot:
 	dd bss_end	; bss end address
 	dd start	; entry address
 	dd 0	; video mode_type (0:fb, 1:txt) (set flags[2]!)
-	dd 1024	; video width
-	dd 768	; video height
+	dd VWIDTH	; video width
+	dd VHEIGHT	; video height
 	dd 32	; video depth
 
 start:
@@ -41,10 +44,6 @@ start:
 	call mbootgetinfo
 	add esp, 2*4
 
-	mov eax, [mbootinfo.fb_addr]
-	mov dword [eax], 0xffffffff
-
-
 	push stuff.fbaddrmsgpfx
 	call serialwrite
 	add esp, 4
@@ -84,8 +83,8 @@ start:
 	add esp, 4
 	push stuff.x
 	call serialwrite
-
 	add esp, 4
+
 	push dword esp	;destination address
 	push dword 10	;base
 	xor eax, eax
@@ -100,37 +99,35 @@ start:
 	add esp, 36	;Clean reserved uitoa return string from stack
 
 
-	;; Setup paging
-	;; Clear pagedir
-	cld
-	mov eax, 0x00000002	;Supervisor only, Write enabled, Not present
-	mov ecx, 1024
-	mov edi, pagedir
-	rep stosd
 
-	;; Clear pagetable1
-	mov edi, pagetable1
-	mov cx, 0
-.clearpt1loop:
-	mov eax, 0x00001000
-	mul ecx
-	or eax, 011b	;supervisor, rw, present
-	stosd
-	inc cx
-	cmp cx, 1024
-	jne .clearpt1loop
+	mov ebx, 1
+	mov eax, 1
+
+	push  0x00	;B
+	push  0x00	;G
+	push  0xff	;R
+	push  ebx		;y
+	push  eax		;x
+	call putpixel
+	add esp, 20	;clean stack
+inc eax
+	push  0x00	;B
+	push  0x00	;G
+	push  0xff	;R
+	push  ebx		;y
+	push  eax		;x
+	call putpixel
+	add esp, 20	;clean stack
+inc ebx
+	push  0x00	;B
+	push  0x00	;G
+	push  0xff	;R
+	push  ebx		;y
+	push  eax		;x
+	call putpixel
+	add esp, 20	;clean stack
 
-	;; put table1 in dir
-	mov eax, pagetable1
-	or eax, 011b	;supervisor, rw, present
-	mov [pagedir], eax
 
-	;; Enable paging
-	mov eax, pagedir
-	mov cr3, eax
-	mov eax, cr0
-	or eax, 0x80000000
-	mov cr0, eax
 
 	jmp hang
 
@@ -146,6 +143,7 @@ hang:
 include "src/serial.asm"
 include "src/mbootinfo.asm"
 include "src/itoa.asm"
+include "src/framebuffer.asm"
 
 stuff:
 	.bootmsg db 10, "=== rOSka ===", 10, 0
@@ -153,13 +151,14 @@ stuff:
 	.fbdimensionsmsgpfx db 10, "Framebuffer dimensions: ", 0
 	.x db "x", 0
 
+align 4096
 bss_start:
-align 4096
-pagedir:
-	rb 4096
-align 4096
-pagetable1:
-	rb 4096
+;align 4096
+;pagedir:
+;	rb 4096
+;align 4096
+;pagetable1:
+;	rb 4096
 align 4096
 stack_bottom:
 rb 16384