Browse Source

bunch of added stuff

master
Mathieu Serandour 1 year ago
parent
commit
1e4a679f8c
  1. 3
      disk_root/boot/limine.cfg
  2. 50
      kernel/entry.c
  3. 6
      kernel/extern_files.s
  4. 0
      kernel/irq/idt.c
  5. 7
      kernel/irq/idt.s
  6. 0
      kernel/irq/irq.c
  7. 0
      kernel/irq/irq.h
  8. 4
      kernel/klib/string.c
  9. 46
      kernel/memory/gdt.s
  10. 16
      kernel/memory/kalloc.c
  11. 14
      kernel/regs.s
  12. 1354
      kernel/video.asm
  13. 95
      kernel/video/terminal.c
  14. 4
      kernel/video/terminal.h
  15. 230
      kernel/video/video.c
  16. 45
      kernel/video/video.h
  17. BIN
      resources/bmp/charmap.bmp

3
disk_root/boot/limine.cfg

@ -5,7 +5,7 @@ VERBOSE=yes
THEME_MARGIN=64
#RESOLUTION=800x600
#BACKGROUND_PATH=boot:///boot/bg.bmp
BACKGROUND_PATH=boot:///boot/bg.bmp
#THEME_BACKGROUND=8f000000
BACKGROUND_STYLE=centered
@ -13,5 +13,6 @@ BACKGROUND_STYLE=centered
PROTOCOL=stivale2
#RESOLUTION=800x600
BACKGROUND_PATH=boot:///boot/bg.bmp
KERNEL_PATH=boot:///boot/kernel.elf
BACKGROUND_STYLE=centered

50
kernel/entry.c

@ -100,9 +100,6 @@ void *stivale2_get_tag(struct stivale2_struct *stivale2_struct, uint64_t id) {
#define PRINT_VAL(v) kprintf(#v "=%ld\n", v);
#define PRINT_HEX(v) kprintf(#v "=%lx\n", v);
extern uint64_t test(void);
extern int64_t test_size;
// const char but represents a big string
extern const char _binary_bootmessage_txt;
@ -132,7 +129,31 @@ void _start(struct stivale2_struct *stivale2_struct) {
struct stivale2_struct_tag_framebuffer* _fbtag = stivale2_get_tag(stivale2_struct,0x506461d2950408fa);
memcpy(&fbtag, _fbtag, sizeof(fbtag));
Image sc = {.w = fbtag.framebuffer_width,
.h = fbtag.framebuffer_height,
.pitch= fbtag.framebuffer_pitch,
.bpp = fbtag.framebuffer_bpp,
.pix = (void*)fbtag.framebuffer_addr};
initVideo(&sc);
setup_terminal();
set_terminal_fgcolor(0x00ff00);
set_terminal_bgcolor(0xff0000);
kputs(&_binary_bootmessage_txt);
set_terminal_fgcolor(0xff0000);
set_terminal_bgcolor(0x00ff00);
PRINT_VAL(fbtag.memory_model);
PRINT_VAL(fbtag.framebuffer_pitch);
PRINT_VAL(fbtag.framebuffer_width);
@ -145,32 +166,19 @@ void _start(struct stivale2_struct *stivale2_struct) {
PRINT_VAL(fbtag. red_mask_size);
PRINT_VAL(fbtag.green_mask_size);
PRINT_VAL(fbtag. blue_mask_size);
PRINT_VAL(test_size);
Image sc = {.w = fbtag.framebuffer_width,
.h = fbtag.framebuffer_height,
.pitch= fbtag.framebuffer_pitch,
.bpp = fbtag.framebuffer_bpp,
.pix = (void*)fbtag.framebuffer_addr};
initVideo(&sc);
// We should now be able to call the above function pointer to print out
// a simple "Hello World" to screen.
kprintf("ds=0x%x\nss=0x%x\ncs=%x\nes=%x\nfs=%x\ngs=%x\n\n",
_ds(), _ss(), _cs(),_es(),_fs(),_gs());
kprintf("print=0x%lx\n\n", kprintf);//0x00ea60
//0x100a60
setup_terminal();
init_gdt_table();
kputs(&_binary_bootmessage_txt);
//*ptr = 0xfffa24821;
asm("hlt");
asm volatile ("sti");
asm volatile ("hlt");
for(size_t i = 0; i < fbtag.framebuffer_height; i++) {
uint8_t* base = (uint8_t*)fbtag.framebuffer_addr + fbtag.framebuffer_pitch*i;

6
kernel/extern_files.s

@ -1,6 +1,6 @@
section .rodata
global _binary_charmap_bmp
global _binary_bootmessage_txt
[section .rodata]
[global _binary_charmap_bmp]
[global _binary_bootmessage_txt]
_binary_charmap_bmp:
incbin "../resources/bmp/charmap.bmp"

0
kernel/irq.c → kernel/irq/idt.c

7
kernel/irq/idt.s

@ -0,0 +1,7 @@
[section .text]
[global _lidt]
lidt:
lidt [rdi]
ret

0
kernel/irq.h → kernel/irq/irq.c

0
kernel/irq/irq.h

4
kernel/klib/string.c

@ -242,10 +242,6 @@ void * memset (void * _buf, int _ch, size_t n) {
}
}
// last unaligned bytes
for(;n > 0 && (uint64_t)buf % 8 != 0; --n)
*(buf++) = ch;

46
kernel/memory/gdt.s

@ -1,34 +1,10 @@
[bits 64]
[section .text]
[global _lgdt]
[global _cr3]
section .text
global _lgdt
global _cr3
extern gdt_descriptor
; argument in RDI
_lgdt:
call _lgdt2
;pop rax
push qword 1234
pop rax
;cmp rax, 1234
;a: je a
ret
_lgdt2:
cli
;sgdt [gdt_descriptor]
;sub dword [gdt_descriptor], 4*8
;add qword [gdt_descriptor+2], 4*8
;mov rax, qword [gdt_descriptor+2]
;mov qword [rax], 0
lgdt [gdt_descriptor]
;ret
lgdt [rdi]
mov rax, rsp
push qword 0x30
@ -40,7 +16,6 @@ _lgdt2:
iretq
far_ret:
;mov ax, ds
mov ax, 0x30
mov ds, ax
mov ss, ax
@ -53,15 +28,4 @@ far_ret:
_cr3:
mov rax, rdi
mov cr3, rax
ret
global test
test:
mov rax, 0xff00ff
ret
dq 0
endtest: TIMES 64 dq 0
section .data
global test_size
test_size: dq endtest - test
ret

16
kernel/memory/kalloc.c

@ -1,16 +1,28 @@
#include <stdint.h>
#include <stddef.h>
#include "kalloc.h"
#include "../debug/assert.h"
#include "../common.h"
static void* brk = (void *) 0x00800000;
#define HEAP_SIZE_MB 16
#define HEAP_SIZE HEAP_SIZE_MB * 1024 * 1024
static uint8_t heap[HEAP_SIZE] __attribute__((section(".bss")));
static void* brk = (void *) heap;
void* kmalloc(size_t size) {
void* ptr = brk;
brk = mallign16(brk+size);
assert((size_t)brk - (size_t)heap < HEAP_SIZE);
return ptr;
}
void kfree(void* ptr) {
}
}

14
kernel/regs.s

@ -1,11 +1,9 @@
bits 64
global _ss
global _cs
global _ds
global _es
global _fs
global _gs
[global _ss]
[global _cs]
[global _ds]
[global _es]
[global _fs]
[global _gs]
section .text

1354
kernel/video.asm

File diff suppressed because it is too large

95
kernel/video/terminal.c

@ -32,14 +32,18 @@ struct Char {
#define INTERLINE 4
#define LINE_HEIGHT (FONTHEIGHT + INTERLINE)
#define N_PAGES 4
static Image* charmap = NULL;
static struct Char* buffer = NULL;
static uint16_t ncols, nlines;
static uint16_t term_nlines;
static uint16_t first_line = 0;
static uint16_t cur_col, cur_line;
static uint32_t current_fgcolor = 0xffffff;
static uint32_t current_fgcolor = 0xa0a0a0;
static uint32_t current_bgcolor = 0;
@ -62,25 +66,40 @@ void setup_terminal(void) {
// dynamicly create the terminal
// with right size
ncols = screenImage->w / FONTWIDTH;
nlines = screenImage->h / LINE_HEIGHT;
size_t buffer_len = screenImage->w * screenImage->h;
ncols = screenImage->w / FONTWIDTH;
term_nlines = (screenImage->h / LINE_HEIGHT);
nlines = N_PAGES * term_nlines;
buffer = kmalloc(buffer_len * sizeof(struct Char));
buffer = kmalloc(nlines * ncols * sizeof(struct Char));
struct Char* ptr = buffer;
for(;buffer_len > 0; --buffer_len)
*(ptr++) = make_Char(0);
clear_terminal();
cur_col = 0;
cur_line= 0;
set_terminal_handler(write_string);
}
set_terminal_handler(write_string);
void clear_terminal(void) {
cur_col = 0;
cur_line = 0;
first_line = 0;
size_t buffer_len = nlines * ncols;
struct Char* ptr = buffer;
for(;buffer_len > 0; --buffer_len)
*(ptr++) = make_Char(0);
}
void set_terminal_fgcolor(uint32_t c) {
current_fgcolor = c;
}
void set_terminal_bgcolor(uint32_t c) {
current_bgcolor = c;
}
/*
@ -90,8 +109,13 @@ static struct Char* get_Char_at(int l, int c) {
}
*/
static void scroll(int lines) {
static void move_buffer(int lines) {
need_refresh = true;
if(lines > 0) {// scroll backward
size_t bytes = ncols * lines;
memmove(buffer, buffer + bytes, bytes);
}
}
static void next_line(void) {
@ -100,16 +124,20 @@ static void next_line(void) {
if(cur_line >= nlines) {
cur_line = nlines;
scroll(1);
move_buffer(1);
}
else if(cur_line > first_line + term_nlines) {
first_line++;
need_refresh = true;
}
}
// create the char struct
static struct Char make_Char(char c) {
return (struct Char) {
current_fgcolor,
c,
current_bgcolor
.fg_color = current_fgcolor,
.c = c,
.bg_color = current_bgcolor
};
}
@ -147,12 +175,12 @@ static void emplace_char(char c) {
advance_cursor(c);
}
static void print_char(const struct Char* c, int line, int col) {
static void print_char(const struct Char* restrict c, int line, int col) {
uint16_t c_x = c->c % 16,
c_y = c->c / 16;
/*
Pos srcpos = {
FONTWIDTH * c_x ,
FONTWIDTH * c_x,
FONTHEIGHT * c_y,
};
Rect dstrect = {
@ -162,12 +190,19 @@ static void print_char(const struct Char* c, int line, int col) {
.w = FONTWIDTH,
.h = FONTHEIGHT,
};
imageDraw(charmap, &srcpos, &dstrect);
*/
//imageDrawMask(charmap, &srcpos, &dstrect, c->fg_color, c->bg_color);
//imageLower_draw(charmap, &srcpos, &dstrect);
imageLower_blitBinaryMask(
charmap,
FONTWIDTH * c_x, FONTHEIGHT * c_y,
col * FONTWIDTH, line * LINE_HEIGHT,
FONTWIDTH, FONTHEIGHT,
c->bg_color, c->fg_color);
Rect interlineRect = {
.x = dstrect.x,
.y = dstrect.y + FONTHEIGHT,
.x = col * FONTWIDTH,
.y = line * LINE_HEIGHT + FONTHEIGHT,
.w = FONTWIDTH,
.h = INTERLINE,
@ -178,12 +213,10 @@ static void print_char(const struct Char* c, int line, int col) {
}
static void flush_screen(void) {
// if(need_refresh)
// imageDraw(charmap, NULL, NULL);
// begins at the terminal's first line
const struct Char* curr = buffer + first_line * ncols;
const struct Char* curr = buffer;
for(size_t l = 0; l < nlines; l++) {
for(size_t l = 0; l < term_nlines; l++) {
for(size_t c = 0; c < ncols; c++) {
print_char(curr++, l, c);
}
@ -203,14 +236,9 @@ static void write_string(const char *string, size_t length) {
}
flush_screen();
//imageDraw(charmap, NULL, NULL);
}
terminal_handler_t get_terminal_handler(void) {
return terminal_handler;
}
@ -224,4 +252,3 @@ void terminal_set_colors(uint32_t foreground, uint32_t background) {
terminal_foreground = foreground;
terminal_background = background;
}

4
kernel/video/terminal.h

@ -11,8 +11,12 @@ typedef void (*terminal_handler_t)(const char *string, size_t length);
**/
terminal_handler_t get_terminal_handler(void);
void set_terminal_bgcolor(uint32_t c);
void set_terminal_fgcolor(uint32_t c);
void setup_terminal(void);
void clear_terminal(void);
// no use another terminal handler
void set_terminal_handler(terminal_handler_t h);

230
kernel/video/video.c

@ -12,6 +12,11 @@
static Image screen;
inline void lower_blit(const Image* src, const Image* dst,
uint16_t srcx, uint16_t srcy,
uint16_t dstx, uint16_t dsty,
uint16_t width, uint16_t height);
void initVideo(const Image* s) {
assert(s != NULL);
assert(s->bpp == 32);
@ -19,6 +24,80 @@ void initVideo(const Image* s) {
memcpy(&screen,s, sizeof(Image));
}
void imageLower_blit(const Image* img,
uint16_t srcx, uint16_t srcy,
uint16_t dstx, uint16_t dsty,
uint16_t width, uint16_t height) {
lower_blit(img, &screen, srcx,srcy,dstx,dsty,width,height);
}
void imageLower_blitBinaryMask(
const Image* img,
uint16_t srcx, uint16_t srcy,
uint16_t dstx, uint16_t dsty,
uint16_t width, uint16_t height,
uint32_t black, uint32_t white) {
assert(img->bpp == 32);
// begining of the first line
uint8_t* dst_ptr = screen.pix + dsty * screen.pitch
+ dstx * BPP;
uint8_t* src_ptr = img->pix + srcy * img->pitch
+ srcx * BPP;
size_t copy_size = width * BPP;
size_t dst_skip = screen.pitch - copy_size;
size_t src_skip = img -> pitch - copy_size;
// assert that everything are 2-aligned
// so that we can process faster
assert((size_t)dst_ptr % 8 == 0);
assert((size_t)src_ptr % 8 == 0);
assert((size_t)copy_size % 8 == 0);
assert((size_t)dst_skip % 8 == 0);
assert((size_t)src_skip % 8 == 0);
uint32_t* src_ptr32 = (uint32_t *)src_ptr;
uint32_t* dst_ptr32 = (uint32_t *)dst_ptr;
for(size_t i = height+1; i > 0 ; i--) {
// allow GCC to perform eco+ vectorization
src_ptr32 = __builtin_assume_aligned(src_ptr32, 8);
dst_ptr32 = __builtin_assume_aligned(dst_ptr32, 8);
for(int i = copy_size / 4; i > 0; i--) {
if((uint8_t) *(src_ptr32++) != 0) {
*(dst_ptr32++) = white;
}
else
*(dst_ptr32++) = black;
}
//memcpy(dst_ptr, src_ptr, copy_size);
src_ptr32 += src_skip / 4;
dst_ptr32 += dst_skip / 4;
//src_ptr += img->pitch;
//dst_ptr += screen.pitch;
}
}
/**
* blit the image without any verification
* (faster than blit but riskier)
@ -38,9 +117,6 @@ inline void lower_blit(const Image* src, const Image* dst,
size_t src_skip = src->pitch;
size_t copy_size = width * BPP;
//kprintf("copy_size: %lu\n"
// "dst_skip: %lu", copy_size, dst_skip);
for(size_t i = height+1; i > 0 ; i--) {
@ -88,6 +164,7 @@ void imageDraw(const Image* img, const Pos* srcpos, const Rect* dstrect) {
imageBlit(img, &screen, srcpos, dstrect);
}
void imageBlit(const Image* restrict src, Image* restrict dst,
const Pos* srcpos, const Rect* dstrect) {
uint32_t sxbegin,
@ -161,9 +238,17 @@ Image* alloc_image(uint32_t width, uint32_t height, uint32_t bpp) {
ret->w = width;
ret->h = height;
ret->pitch = (uint32_t)allign16(width * (bpp/8));
ret->bpp = bpp;
assert(bpp % 8 == 0 || bpp == 1);
if(bpp == 1) {
// align on 8 byte
ret->pitch = ((width * bpp +7) / 8) * 8;
}
else
ret->pitch = (uint32_t)allign16(width * (bpp/8));
ret->pix = kmalloc(ret->pitch * height);
return ret;
@ -206,28 +291,22 @@ inline bool check_BMP_header(const struct BMPFileHeader* header) {
}
#define PRINT_VAL(v) kprintf(__FILE__ ":%d:%s::" \
#v "=%4lx\n", __LINE__,__func__,v);
#define PRINT_struct(v,s) kprintf("%x:\t" #v "=%ld\n", \
(size_t)&v-(size_t)s, v);
const Image* getScreenImage(void) {
return &screen;
}
Image* loadBMP(const void* rawFile) {
// the header should be at the beginning
const struct BMPFileHeader* header = rawFile;
// assert that the checks are already performed
static Image* loadBMP_24(const struct BMPFileHeader* restrict header, const void* restrict body) {
assert(header->bpp == 24);
if(check_BMP_header(header))
return NULL;
uint32_t w = header->w;
uint32_t h = header->h;
const uint8_t* srcpix24 = (const uint8_t*)rawFile + header->body_offset;
const uint8_t* srcpix = body;
const size_t bpp = 3;
Image* ret = alloc_image(w,h, 32);
@ -236,15 +315,124 @@ Image* loadBMP(const void* rawFile) {
for(size_t y = 0; y < h; y++) {
for(size_t x = 0; x < w; x++) {
const uint32_t* src_ptr = (const uint32_t *)(srcpix24
+ x * 3
+ (h-1 - y) * 3 * w);
const uint32_t* src_ptr = (const uint32_t *)(srcpix
+ x * bpp
+ (h-1 - y) * bpp * w);
/// the image is reversed along
/// the y axis
pix32[x + y * bpitch] = (*(const uint32_t *)src_ptr) & 0x00ffffff;
pix32[x + y * bpitch] = (*(const uint32_t *)src_ptr) & 0x00ffffff;
}
}
return ret;
}
// assert that the checks are already performed
static Image* loadBMP_8(const struct BMPFileHeader* restrict header, const void* restrict body) {
assert(header->bpp == 8);
uint32_t w = header->w;
uint32_t h = header->h;
const uint8_t* srcpix = body;
Image* ret = alloc_image(w,h, 8);
size_t bpitch = ret->pitch;
uint32_t* pix32 = ret->pix;
for(size_t y = 0; y < h; y++) {
const uint8_t* src_ptr = (srcpix + (h-1 - y) * w);
for(size_t x = 0; x < w; x++) {
/// the image is reversed along
/// the y axis
pix32[x + y * bpitch] = *(src_ptr++);
}
}
return ret;
}
Image* loadBMP(const void* restrict rawFile) {
// the header should be at the beginning
const struct BMPFileHeader* header = rawFile;
if(check_BMP_header(header))
return NULL;
// only handle 32bit and 8bit image
assert(header->bpp == 8 || header->bpp == 24);
const void* body = (uint8_t *)header + header->body_offset;
switch(header->bpp) {
case 24:
return loadBMP_24(header, body);
break;
case 8:
assert(0);
return loadBMP_8 (header, body);
break;
default:
assert(0);
__builtin_unreachable();
}
}
/*
(00): black black
(01): black white
(10): white black
(11): white white
*/
Image* loadBMP_24b_1b(const void* rawFile) {
const struct BMPFileHeader* header = rawFile;
if(check_BMP_header(header))
return NULL;
assert(header->bpp == 24);
uint32_t w = header->w;
uint32_t h = header->h;
const uint8_t* srcpix = (const uint8_t *)header + header->body_offset;
Image* ret = alloc_image(w,h, 1);
size_t bpitch = ret->pitch;
uint32_t* pix32 = ret->pix;
for(size_t y = 0; y < h; y++) {
const uint8_t* src_ptr = (srcpix + (h-1 - y) * w);
uint8_t byte = 0;
for(size_t x = 0; x < w; x++) {
byte <<= 1;
// put 1 iif r channel > 128
byte |= *(src_ptr++) >> 7;
if((x % 8 == 0 && x != 0) || x == w-1) {
pix32[x/8 + y * bpitch] = byte;
byte = 0;
}
}
}
}

45
kernel/video/video.h

@ -27,12 +27,51 @@ const Image* getScreenImage(void);
void initVideo(const Image* srceen);
void imageDraw(const Image* img, const Pos* srcpos, const Rect* dstrect);
void imageBlit(const Image* __restrict__ src,
Image* __restrict__ dst,
const Pos* srcpos, const Rect* dstrect);
void imageLower_blit(
const Image* img,
uint16_t srcx, uint16_t srcy,
uint16_t dstx, uint16_t dsty,
uint16_t width,
uint16_t height
);
// for 32-bit images
// (@TODO) for now it is for 32-bit images
//
// maps the black src pixels to the black color argument
// and all the others colors to the white px
// _ white
// |
// |---------------- black
// 0 1 2 ... 255
// 1D color
void imageLower_blitBinaryMask(
const Image* img,
uint16_t srcx, uint16_t srcy,
uint16_t dstx, uint16_t dsty,
uint16_t width, uint16_t height,
uint32_t black,
uint32_t white
);
void imageBlit(
const Image* __restrict__ src,
Image* __restrict__ dst,
const Pos* srcpos,
const Rect* dstrect
);
// only handle 32bit
void imageFillRect(uint32_t color, const Rect* rect);
// only handle 32bit and 8bit image
Image* loadBMP(const void* rawFile);
// load 1 BIT/px image
// from a 24 bit/px image
Image* loadBMP_24b_1b(const void* rawFile);
#endif

BIN
resources/bmp/charmap.bmp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 144 KiB

After

Width:  |  Height:  |  Size: 144 KiB

Loading…
Cancel
Save