User:Lweb20
Optimized Functions
#define inline __attribute__((always_inline)) inline
inline void* memset(void* ptr, int value, size_t num)
{
asm("rep stosb": : "a"(value), "c"(num), "D"(ptr));
return ptr;
}
inline void* memsetw(void* ptr, int value, size_t num)
{
asm("rep stosw": : "a"(value), "c"(num), "D"(ptr));
return ptr;
}
inline void* memsetd(void* ptr, int value, size_t num)
{
asm("rep stosd": : "a"(value), "c"(num), "D"(ptr));
return ptr;
}
inline void* memcpy(void* destination, const void* source, size_t num)
{
asm("rep movsb": : "D"(destination), "c"(num), "S"(source));
return destination;
}
VGA
VGA controllers
The VGA has controllers which in turn have registers to manipulate.
// attribute controller
#define VGA_ATTRIBUTE_CONTROLLER 0x3C0
// sequencer
#define VGA_SEQUENCER 0x3C4
// graphics controller
#define VGA_GRAPHICS_CONTROLLER 0x3CE
// crt controller
#define VGA_CRT_CONTROLLER 0x3D4
Some VGA ports are not associated with a specific controller. They contain a single register.
#define VGA_FEATURE_CONTROL 0x3DA
#define VGA_MISC_REG_WRITE 0x3C2
#define VGA_MISC_REG_READ 0x3CC
Useful functions
Write the registers
inline void vga_attrib_write(unsigned char index, unsigned char value)
{
inb(VGA_FEATURE_CONTROL);
outb(VGA_ATTRIBUTE_CONTROLLER, index);
outb(VGA_ATTRIBUTE_CONTROLLER, value);
}
inline void vga_seq_write(unsigned char index, unsigned char value)
{
outw(VGA_SEQUENCER, ((unsigned short)value << 8) | index);
}
inline void vga_graphics_write(unsigned char index, unsigned char value)
{
outw(VGA_GRAPHICS_CONTROLLER, ((unsigned short)value << 8) | index);
}
inline void vga_crtc_write(unsigned char index, unsigned char value)
{
outw(VGA_CRT_CONTROLLER, ((unsigned short)value << 8) | index);
}
inline void vga_misc_write(unsigned char value)
{
outb(VGA_MISC_REG_WRITE, value);
}
inline void vga_feature_write(unsigned char value)
{
outb(VGA_FEATURE_CONTROL, value);
}
To write registers 0 through 7 of the CRT is needed to unlock.
inline void vga_crtc_unlock()
{
unsigned short data;
outb(VGA_CRT_CONTROLLER, 0x11);
data = inb(VGA_CRT_CONTROLLER + 1);
outw(VGA_CRT_CONTROLLER, (ClearBit(data, 7) << 8) | 0x11);
}
inline void vga_crtc_lock()
{
unsigned short data;
outb(VGA_CRT_CONTROLLER, 0x11);
data = inb(VGA_CRT_CONTROLLER + 1);
outw(VGA_CRT_CONTROLLER, (SetBit(data, 7) << 8) | 0x11);
}
Reactivate the screen refresh disabled by writing to the controller attributes.
inline void vga_attrib_active_palette()
{
outb(VGA_ATTRIBUTE_CONTROLLER, (1 << 5));
}
Read the registers
inline unsigned char vga_attrib_read(unsigned char index)
{
unsigned char data;
inb(VGA_FEATURE_CONTROL);
outb(VGA_ATTRIBUTE_CONTROLLER, index);
data = inb(VGA_ATTRIBUTE_CONTROLLER + 1);
return data;
}
inline unsigned char vga_seq_read(unsigned char index)
{
outb(VGA_SEQUENCER, index);
return inb(VGA_SEQUENCER + 1);
}
inline unsigned char vga_graphics_read(unsigned char index)
{
outb(VGA_GRAPHICS_CONTROLLER, index);
return inb(VGA_GRAPHICS_CONTROLLER + 1);
}
inline unsigned char vga_crtc_read(unsigned char index)
{
outb(VGA_CRT_CONTROLLER, index);
return inb(VGA_CRT_CONTROLLER + 1);
}
inline unsigned char vga_misc_read()
{
return inb(VGA_MISC_REG_READ);
}
inline unsigned char vga_feature_read()
{
return inb(VGA_FEATURE_CONTROL);
}
Select plane
inline void vga_select_plane(unsigned char plane)
{
vga_seq_write(0x02, plane);
}
Wiki Links
Wiki:
http://wiki.osdev.org/OS_Specific_Toolchain
http://wiki.osdev.org/Hosted_GCC_Cross-Compiler
http://wiki.osdev.org/Porting_Newlib
Interrupt Controllers
8259 Programmable Interrupt Controller (PIC)
82489DX Advanced Programmable Interrupt Controller (APIC)
82093AA I/O Advanced Programmable Interrupt Controller (IOAPIC)
Cross Compiling:
http://f.osdev.org/viewtopic.php?f=13&t=18894
IOAPIC:
http://forum.osdev.org/viewtopic.php?t=23774&p=193091
http://forum.osdev.org/viewtopic.php?f=1&t=21745&start=0
SMP:
http://forum.osdev.org/viewtopic.php?f=1&t=23018 (Problems activating SMP on newer (core i5/i7) machines)
http://forum.osdev.org/viewtopic.php?t=28674 (IPI Question)
http://forum.osdev.org/viewtopic.php?f=1&t=23445 (CPUID - how to determine number of physical cores?)
External Links
GZIP file format specification version 4.3
https://tools.ietf.org/html/rfc1952
Qemu Documentation
http://wiki.qemu.org/download/qemu-doc.html
Qemu Monitor
https://en.wikibooks.org/wiki/QEMU/Monitor
Bochs User Manual
http://bochs.sourceforge.net/doc/docbook/user/bochsrc.html
X.org Docs
The little book about OS development
https://littleosbook.github.io/
Linux System Calls
https://sourceware.org/newlib/libc.html#Syscalls
http://man7.org/linux/man-pages/man2/syscalls.2.html
http://syscalls.kernelgrok.com/
SMP
http://www.cheesecake.org/sac/smp.html
X86 architecture
https://en.wikipedia.org/wiki/X86
List of Intel CPU microarchitectures
https://en.wikipedia.org/wiki/List_of_Intel_CPU_microarchitectures
List of AMD CPU microarchitectures
https://en.wikipedia.org/wiki/List_of_AMD_CPU_microarchitectures