Permalink
Browse files

Files for part 4, https://www.youtube.com/watch?v=XGSOl1QZVKI

  • Loading branch information...
Viktor Engelmann Viktor Engelmann
Viktor Engelmann authored and Viktor Engelmann committed Jul 4, 2016
1 parent fd95a89 commit 8745b3cc495107eceacf98c9d9be9b426070f8ba
Showing with 245 additions and 2 deletions.
  1. +30 −1 kernel.cpp
  2. +4 −1 makefile
  3. +100 −0 port.cpp
  4. +111 −0 port.h
View
@@ -6,8 +6,37 @@ void printf(char* str)
{
static uint16_t* VideoMemory = (uint16_t*)0xb8000;
static uint8_t x=0,y=0;
for(int i = 0; str[i] != '\0'; ++i)
VideoMemory[i] = (VideoMemory[i] & 0xFF00) | str[i];
{
switch(str[i])
{
case '\n':
x = 0;
y++;
break;
default:
VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | str[i];
x++;
break;
}
if(x >= 80)
{
x = 0;
y++;
}
if(y >= 25)
{
for(y = 0; y < 25; y++)
for(x = 0; x < 80; x++)
VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | ' ';
x = 0;
y = 0;
}
}
}
View
@@ -6,7 +6,7 @@ GCCPARAMS = -m32 -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-excep
ASPARAMS = --32
LDPARAMS = -melf_i386
objects = loader.o gdt.o kernel.o
objects = loader.o gdt.o port.o kernel.o
@@ -41,3 +41,6 @@ run: mykernel.iso
install: mykernel.bin
sudo cp $< /boot/mykernel.bin
.PHONY: clean
clean:
rm -f $(objects) mykernel.bin mykernel.iso
View
100 port.cpp
@@ -0,0 +1,100 @@
#include "port.h"
Port::Port(uint16_t portnumber)
{
this->portnumber = portnumber;
}
Port::~Port()
{
}
Port8Bit::Port8Bit(uint16_t portnumber)
: Port(portnumber)
{
}
Port8Bit::~Port8Bit()
{
}
void Port8Bit::Write(uint8_t data)
{
Write8(portnumber, data);
}
uint8_t Port8Bit::Read()
{
return Read8(portnumber);
}
Port8BitSlow::Port8BitSlow(uint16_t portnumber)
: Port8Bit(portnumber)
{
}
Port8BitSlow::~Port8BitSlow()
{
}
void Port8BitSlow::Write(uint8_t data)
{
Write8Slow(portnumber, data);
}
Port16Bit::Port16Bit(uint16_t portnumber)
: Port(portnumber)
{
}
Port16Bit::~Port16Bit()
{
}
void Port16Bit::Write(uint16_t data)
{
Write16(portnumber, data);
}
uint16_t Port16Bit::Read()
{
return Read16(portnumber);
}
Port32Bit::Port32Bit(uint16_t portnumber)
: Port(portnumber)
{
}
Port32Bit::~Port32Bit()
{
}
void Port32Bit::Write(uint32_t data)
{
Write32(portnumber, data);
}
uint32_t Port32Bit::Read()
{
return Read32(portnumber);
}
View
111 port.h
@@ -0,0 +1,111 @@
#ifndef __PORT_H
#define __PORT_H
#include "types.h"
class Port
{
protected:
Port(uint16_t portnumber);
// FIXME: Must be virtual (currently isnt because the kernel has no memory management yet)
~Port();
uint16_t portnumber;
};
class Port8Bit : public Port
{
public:
Port8Bit(uint16_t portnumber);
~Port8Bit();
virtual uint8_t Read();
virtual void Write(uint8_t data);
protected:
static inline uint8_t Read8(uint16_t _port)
{
uint8_t result;
__asm__ volatile("inb %1, %0" : "=a" (result) : "Nd" (_port));
return result;
}
static inline void Write8(uint16_t _port, uint8_t _data)
{
__asm__ volatile("outb %0, %1" : : "a" (_data), "Nd" (_port));
}
};
class Port8BitSlow : public Port8Bit
{
public:
Port8BitSlow(uint16_t portnumber);
~Port8BitSlow();
virtual void Write(uint8_t data);
protected:
static inline void Write8Slow(uint16_t _port, uint8_t _data)
{
__asm__ volatile("outb %0, %1\njmp 1f\n1: jmp 1f\n1:" : : "a" (_data), "Nd" (_port));
}
};
class Port16Bit : public Port
{
public:
Port16Bit(uint16_t portnumber);
~Port16Bit();
virtual uint16_t Read();
virtual void Write(uint16_t data);
protected:
static inline uint16_t Read16(uint16_t _port)
{
uint16_t result;
__asm__ volatile("inw %1, %0" : "=a" (result) : "Nd" (_port));
return result;
}
static inline void Write16(uint16_t _port, uint16_t _data)
{
__asm__ volatile("outw %0, %1" : : "a" (_data), "Nd" (_port));
}
};
class Port32Bit : public Port
{
public:
Port32Bit(uint16_t portnumber);
~Port32Bit();
virtual uint32_t Read();
virtual void Write(uint32_t data);
protected:
static inline uint32_t Read32(uint16_t _port)
{
uint32_t result;
__asm__ volatile("inl %1, %0" : "=a" (result) : "Nd" (_port));
return result;
}
static inline void Write32(uint16_t _port, uint32_t _data)
{
__asm__ volatile("outl %0, %1" : : "a"(_data), "Nd" (_port));
}
};
#endif

0 comments on commit 8745b3c

Please sign in to comment.