Browse Source

a bunch of changes

master
Mathieu Sérandour 2 years ago
parent
commit
14cd5c04c5
  1. 2
      kernel/Makefile
  2. 4
      kernel/debug/assert.c
  3. 7
      kernel/entry.c
  4. 24
      kernel/klib/string.c
  5. 2
      kernel/klib/string.h
  6. 106
      kernel/video/terminal.c
  7. 6
      kernel/video/terminal.h
  8. 32
      kernel/video/video.c
  9. 2
      kernel/video/video.h

2
kernel/Makefile

@ -3,7 +3,7 @@ CC := x86_64-elf-gcc
ASM := nasm
ASM_FLAGS := -felf64
CFLAGS = -Wall -Wextra -O2 -pipe
CFLAGS = -Wall -Wextra -O1 -pipe
INTERNALLDFLAGS := \
-fno-pic \

4
kernel/debug/assert.c

@ -8,7 +8,7 @@ void __assert(const char* __restrict__ expression,
const int line) {
char buffer[1024];
sprintf(buffer, "assertion '%s' failed at: %s:%d", expression, file, line);
sprintf(buffer, "%s:%d: assertion '%s' failed", file, line, expression);
panic("buffer");
panic(buffer);
}

7
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);
// "int" but designates a binary file
extern int _binary____resources_bmp_charmap_bmp_start;
extern uint64_t test(void);
extern int64_t test_size;
@ -156,9 +153,6 @@ void _start(struct stivale2_struct *stivale2_struct) {
initVideo(&sc);
Image* image = loadBMP(&_binary____resources_bmp_charmap_bmp_start);
PRINT_VAL(image);
// We should now be able to call the above function pointer to print out
@ -168,7 +162,6 @@ void _start(struct stivale2_struct *stivale2_struct) {
kprintf("print=0x%lx\n\n", kprintf);//0x00ea60
//0x100a60
draw(image, NULL, NULL);
init_gdt_table();
//*ptr = 0xfffa24821;
asm("hlt");

24
kernel/klib/string.c

@ -252,3 +252,27 @@ void * memset (void * _buf, int _ch, size_t n) {
return buf;
}
void * memmove (void* _dest, const void* _src, size_t n) {
uint8_t* dest=_dest;
const uint8_t* src=_src;
// check for overlapping
if((dest < src && dest + n > src)
|| (src < dest && src + n > dest)) {
dest += n;
src += n;
// backward copy
for(;n > 0; --n)
*(--dest) = *(--src);
return _dest;
}
// not overlapping: just call memcpy
else {
return memcpy(dest, src, n);
}
}

2
kernel/klib/string.h

@ -16,7 +16,7 @@ int memcmp (const void *, const void *, size_t) __ATTR_PURE__;
void * memcpy (void * __restrict__, const void * __restrict__, size_t);
void * memset (void *, int, size_t);
//void * memmem (const void *, size_t, const void *, size_t) __ATTR_PURE__;
//void * memmove (void *, const void *, size_t);
void * memmove (void *, const void *, size_t);
//void * memrchr (const void *, int, size_t) __ATTR_PURE__;

106
kernel/video/terminal.c

@ -1,9 +1,113 @@
#include <stddef.h>
#include "terminal.h"
#include "../debug/assert.h"
#include "video.h"
#include "../memory/kalloc.h"
#include "../klib/string.h"
#define TAB_SPACE 6
terminal_handler_t terminal_handler = NULL;
uint32_t terminal_foreground = 0xa0a0a0;
uint32_t terminal_background = 0x00;
struct Char {
uint32_t fg_color: 24;
char c;
uint32_t bg_color;
};
#define CHARSIZE 16
Image* charmap = NULL;
struct Char* buffer = NULL;
int ncols, nlines;
int cur_col, cur_line;
// "int" but designates a binary file
extern int _binary____resources_bmp_charmap_bmp_start;
void setup_terminal(void) {
assert(charmap != NULL);
charmap = loadBMP(&_binary____resources_bmp_charmap_bmp_start);
assert(charmap != NULL);
assert(charmap->bpp == 32);
assert(charmap->w == 256);
assert(charmap->h == 256);
const Image* screenImage = getScreenImage();
ncols = screenImage->w / CHARSIZE;
nlines = screenImage->h / CHARSIZE;
size_t buffer_size = screenImage->w * screenImage->h *
sizeof(struct Char);
buffer = kmalloc(buffer_size);
memset(buffer, 0, buffer_size);
cur_col = 0;
cur_line= 0;
}
static void scroll(int lines) {
}
static void next_line(void) {
cur_col = 0;
cur_line++;
if(cur_line >= nlines) {
cur_line = nlines;
scroll(1);
}
}
static void advance_cursor(char c) {
switch(c) {
default:
cur_col += 1;
if(cur_col >= ncols)
next_line();
break;
case '\n':
next_line();
break;
case '\t':
cur_col = (cur_col / TAB_SPACE) * TAB_SPACE;
if(cur_col >= ncols)
next_line();
break;
case '\r':
cur_col = 0;
break;
}
}
void printchar(char c) {
advance_cursor(c);
}
terminal_handler_t get_terminal_handler(void) {
return terminal_handler;
}
@ -13,7 +117,7 @@ void set_terminal_handler(terminal_handler_t h) {
terminal_handler = h;
}
void termina_set_colors(uint32_t foreground, uint32_t background) {
void terminal_set_colors(uint32_t foreground, uint32_t background) {
terminal_foreground = foreground;
terminal_background = background;
}

6
kernel/video/terminal.h

@ -3,6 +3,7 @@
#include <stddef.h>
#include <stdint.h>
struct stivale2_struct_tag_framebuffer;
typedef void (*terminal_handler_t)(const char *string, size_t length);
/**
@ -11,6 +12,9 @@ typedef void (*terminal_handler_t)(const char *string, size_t length);
terminal_handler_t get_terminal_handler(void);
void setup_terminal(void);
// no use another terminal handler
void set_terminal_handler(terminal_handler_t h);
void termina_set_colors(uint32_t foreground, uint32_t background);
void terminal_set_colors(uint32_t foreground, uint32_t background);

32
kernel/video/video.c

@ -34,8 +34,8 @@ inline void lower_blit(const Image* src, const Image* dst,
uint8_t* src_line_start = src->pix + srcy * src->pitch
+ srcx * BPP;
size_t src_skip = src->pitch;
size_t dst_skip = dst->pitch;
size_t src_skip = src->pitch;
size_t copy_size = width * BPP;
@ -44,15 +44,17 @@ inline void lower_blit(const Image* src, const Image* dst,
for(size_t i = height+1; i > 0 ; i--) {
/*
memcpy(dst_line_start,
src_line_start,
copy_size);
*/
memset(dst_line_start, 0xffffffff, copy_size);
//memset(dst_line_start, 0xffffffff, copy_size);
src_line_start += src_skip;
dst_line_start += dst_skip;
}
}
void draw(const Image* img, const Pos* srcpos, const Rect* dstrect) {
@ -182,6 +184,10 @@ inline bool check_BMP_header(const struct BMPFileHeader* header) {
(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;
@ -190,8 +196,8 @@ Image* loadBMP(const void* rawFile) {
if(check_BMP_header(header))
return NULL;
int32_t w = header->w;
int32_t h = header->h;
uint32_t w = header->w;
uint32_t h = header->h;
const uint8_t* srcpix24 = (const uint8_t*)rawFile + header->body_offset;
Image* ret = alloc_image(w,h, 32);
@ -199,13 +205,15 @@ Image* loadBMP(const void* rawFile) {
size_t bpitch = ret->pitch / 4;
uint32_t* pix32 = ret->pix;
for(size_t y = 0; y < ret->h; y++) {
for(size_t x = 0; x < ret->w; x++) {
const uint32_t* src_ptr = (const uint32_t *)srcpix24
+ 3 * x + (ret->h-1-y) * 3 * h;
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);
/// 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;
}

2
kernel/video/video.h

@ -23,7 +23,7 @@ typedef struct {
} Rect;
const Image* getScreenImage(void);
void initVideo(const Image* srceen);
void draw(const Image* img, const Pos* srcpos, const Rect* dstrect);

Loading…
Cancel
Save