Skip to content

Commit b620b28

Browse files
committed
Added port I/O and print function
1 parent a47bc55 commit b620b28

File tree

12 files changed

+122
-4
lines changed

12 files changed

+122
-4
lines changed

bootloader/printf.asm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
global _printf
1+
global _printformat
22

33
section .text:
44
_printformat:

build/OS.bin

77 Bytes
Binary file not shown.

build/full_kernel.bin

77 Bytes
Binary file not shown.

build/kernel.o

484 Bytes
Binary file not shown.

build/printk.o

576 Bytes
Binary file not shown.

kernel/drivers/port_io.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "port_io.h"
2+
3+
// Send byte
4+
void pb_out(uint16_t port, uint8_t data)
5+
{
6+
__asm__("outb %%al, %%dx" : : "a"(data), "d"(port));
7+
return;
8+
}
9+
10+
// Send word
11+
void pw_out(uint16_t port, uint16_t data)
12+
{
13+
__asm__("outw %%ax, %%dx" : : "a"(data), "d"(port));
14+
return;
15+
}
16+
17+
// Send dword
18+
void pd_out(uint16_t port, uint32_t data)
19+
{
20+
__asm__("outl %%eax, %%edx" : : "a"(data), "d"(port));
21+
return;
22+
}
23+
24+
// Receive byte
25+
uint8_t pb_in(uint16_t port)
26+
{
27+
uint8_t result = 0;
28+
__asm__("inb %%dx, %%al" : "=a"(result) : "d"(port));
29+
return result;
30+
}
31+
32+
// Receive word
33+
uint16_t pw_in(uint16_t port)
34+
{
35+
uint16_t result = 0;
36+
__asm__("inw %%dx, %%ax" : "=a"(result) : "d"(port));
37+
return result;
38+
}
39+
40+
// Receive dword
41+
uint32_t pd_in(uint16_t port)
42+
{
43+
uint32_t result = 0;
44+
__asm__("inl %%edx, %%eax" : "=a"(result) : "d"(port));
45+
return result;
46+
}

kernel/drivers/port_io.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef PORT_IO_H
2+
#define PORT_IO_H
3+
4+
#include "../lib/std/datatypes.h"
5+
6+
void pb_out(uint16_t port, uint8_t data);
7+
void pw_out(uint16_t port, uint16_t data);
8+
void pd_out(uint16_t port, uint32_t data);
9+
10+
uint8_t pb_in(uint16_t port);
11+
uint16_t pw_in(uint16_t port);
12+
uint32_t pd_in(uint16_t port);
13+
14+
#endif

kernel/kernel.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
extern void main(){
2-
*(char*)0xb8000 = 'Q';
1+
#include "lib/std/io.h"
2+
3+
extern void main()
4+
{
5+
_printk("Hey! I am writing straight to video memory", 0x0f);
36
return;
47
}

kernel/lib/std/datatypes.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef DATATYPES_H
2+
#define DATATYPES_H
3+
4+
typedef char int8_t;
5+
typedef short int int16_t;
6+
typedef int int32_t;
7+
typedef long long int int64_t;
8+
9+
typedef unsigned char uint8_t;
10+
typedef unsigned short int uint16_t;
11+
typedef unsigned int uint32_t;
12+
typedef unsigned long long int uint64_t;
13+
14+
#endif

kernel/lib/std/io.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef VGA_TEXT_MODE_H
2+
#define VGA_TEXT_MODE_H
3+
4+
#include "datatypes.h"
5+
#include "../../drivers/port_io.h"
6+
7+
void _printk(const char*, uint8_t);
8+
9+
#endif

0 commit comments

Comments
 (0)