Browse Source

!Compile Me!

compile-me
garritfra 5 years ago
parent
commit
435d696639
  1. 6
      .vscode/settings.json
  2. 2
      Makefile
  3. 24
      io.c
  4. 36
      io.h
  5. 11
      io.s
  6. 40
      kmain.c
  7. 1
      kmain.h
  8. 2
      loader.s

6
.vscode/settings.json vendored

@ -0,0 +1,6 @@
{
"files.associations": {
"kmain.h": "c",
"io.h": "c"
}
}

2
Makefile

@ -1,4 +1,4 @@
OBJECTS = loader.o kmain.o
OBJECTS = io.o loader.o kmain.o
CC = gcc
CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \
-nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c

24
io.c

@ -0,0 +1,24 @@
#include "kmain.h"
#include "io.h"
void fb_write_cell(unsigned int i, char c, unsigned char fg, unsigned char bg) {
char *fb = (char *) 0x000B8000;
fb[i] = c;
fb[i + 1] = ((fg & 0x0F) << 4) | (bg & 0x0F);
}
void fb_move_cursor(unsigned short pos)
{
outb(FB_COMMAND_PORT, FB_HIGH_BYTE_COMMAND);
outb(FB_DATA_PORT, ((pos >> 8) & 0x00FF));
outb(FB_COMMAND_PORT, FB_LOW_BYTE_COMMAND);
outb(FB_DATA_PORT, pos & 0x00FF);
}
int write(char *buf, unsigned int len) {
for(int i = 0; i <= (int)len; i++) {
fb_write_cell((int)(i * 2), buf[i], 0, 15);
fb_move_cursor(i);
}
return 0;
}

36
io.h

@ -0,0 +1,36 @@
#ifndef INCLUDE_IO_H
#define INCLUDE_IO_H
/* The I/O ports */
#define FB_COMMAND_PORT 0x3D4
#define FB_DATA_PORT 0x3D5
/* The I/O port commands */
#define FB_HIGH_BYTE_COMMAND 14
#define FB_LOW_BYTE_COMMAND 15
int write(char *buf, unsigned int len);
void outb(unsigned short port, unsigned char data);
/** fb_write_cell:
* Writes a character with the given foreground and background to position i
* in the framebuffer.
*
* @param i The location in the framebuffer
* @param c The character
* @param fg The foreground color
* @param bg The background color
*
* @example fb_write_cell(0, 'A', 2, 8);
*/
void fb_write_cell(unsigned int i, char c, unsigned char fg, unsigned char bg);
/** fb_move_cursor:
* Moves the cursor of the framebuffer to the given position
*
* @param pos The new position of the cursor
*/
void fb_move_cursor(unsigned short pos);
#endif /* INCLUDE_IO_H */

11
io.s

@ -0,0 +1,11 @@
global outb ; make the label outb visible outside this file
; outb - send a byte to an I/O port
; stack: [esp + 8] the data byte
; [esp + 4] the I/O port
; [esp ] return address
outb:
mov al, [esp + 8] ; move the data to be sent into the al register
mov dx, [esp + 4] ; move the address of the I/O port into the dx register
out dx, al ; send the data to the I/O port
ret ; return to the calling function

40
kmain.c

@ -1,31 +1,11 @@
void fb_write_cell(unsigned int i, char c, unsigned char fg, unsigned char bg);
int main() {
fb_write_cell(0, 'A', 2, 8);
#include "kmain.h"
#include "io.h"
int main()
{
fb_write_cell(0, 'A', 0, 15);
fb_write_cell(2, 'B', 0, 15);
fb_write_cell(4, 'C', 0, 15);
fb_move_cursor(6);
return 0;
}
/** fb_write_cell:
* Writes a character with the given foreground and background to position i
* in the framebuffer.
*
* @param i The location in the framebuffer
* @param c The character
* @param fg The foreground color
* @param bg The background color
*
* @example fb_write_cell(0, 'A', 2, 8);
*/
void fb_write_cell(unsigned int i, char c, unsigned char fg, unsigned char bg) {
char *fb = (char *) 0x000B8000;
fb[i] = c;
fb[i + 1] = ((fg & 0x0F) << 4) | (bg & 0x0F);
}
struct example {
unsigned char config; /* bit 0 - 7 */
unsigned short address; /* bit 8 - 23 */
unsigned char index; /* bit 24 - 31 */
} __attribute__((packed));
}

1
kmain.h

@ -0,0 +1 @@
int main();

2
loader.s

@ -25,5 +25,7 @@ loader: ; the loader label (defined as entry point in li
mov esp, kernel_stack + KERNEL_STACK_SIZE ; point esp to the start of the
; stack (end of memory area)
call main
.loop:
jmp .loop ; loop forever
Loading…
Cancel
Save