sortix-mirror/sortix/vga.cpp

237 lines
6.1 KiB
C++
Raw Normal View History

/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of Sortix.
Sortix is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
Sortix. If not, see <http://www.gnu.org/licenses/>.
vga.cpp
A Video Graphics Array driver.
*******************************************************************************/
#include <sortix/kernel/platform.h>
#include <libmaxsi/memory.h>
2012-09-22 14:44:50 +00:00
#include <errno.h>
#include "fs/util.h"
#include "fs/devfs.h"
#include "vga.h"
#include "scheduler.h"
#include "syscall.h"
Implemented the fork() system call and what it needed to work properly. This commit got completely out of control. Added the fork(), getpid(), getppid(), sleep(), usleep() system calls, and aliases in the Maxsi:: namespace. Fixed a bug where zero-byte allocation would fail. Worked on the DescriptorTable class which now works and can fork. Got rid of some massive print-registers statements and replaced them with the portable InterruptRegisters::LogRegisters() function. Removed the SysExecuteOld function and replaced it with Process::Execute(). Rewrote the boot sequence in kernel.cpp such that it now loads the system idle process 'idle' as PID 0, and the initization process 'init' as PID 1. Rewrote the SIGINT hack. Processes now maintain a family-tree structure and keep track of their threads. PIDs are now allocated using a simple hack. Virtual memory per-process can now be allocated using a simple hack. Processes can now be forked. Fixed the Process::Execute function such that it now resets the stack pointer to where the stack actually is - not just a magic value. Removed the old and ugly Process::_endcodesection hack. Rewrote the scheduler into a much cleaner and faster version. Debug code is now moved to designated functions. The noop kernel-thread has been replaced by a simple user-space infinite-loop program 'idle'. The Thread class has been seperated from the Scheduler except in Scheduler- related code. Thread::{Save,Load}Registers has been improved and has been moved to $(CPU)/thread.cpp. Threads can now be forked. A new CreateThread function creates threads properly and portably. Added a MicrosecondsSinceBoot() function. Fixed a crucial bug in MemoryManagement::Fork(). Added an 'idle' user-space program that is a noop infinite loop, which is used by the scheduler when there is nothing to do. Rewrote the 'init' program such that it now forks off a shell, instead of becoming the shell. Added the $$ (current PID) and $PPID (parent PPID) variables to the shell.
2011-09-21 18:52:29 +00:00
#include "process.h"
2011-11-20 14:58:42 +00:00
#include "serialterminal.h"
#define TEST_VGAFONT 0
using namespace Maxsi;
namespace Sortix {
namespace VGA {
uint8_t* const VGA = (uint8_t* const) 0xB8000;
const unsigned WIDTH = 80;
const unsigned HEIGHT = 25;
const size_t VGA_SIZE = sizeof(uint16_t) * WIDTH * HEIGHT;
size_t vgafontsize;
uint8_t* vgafont;
static void WriteIndex(uint16_t port, uint8_t index, uint8_t value)
{
CPU::OutPortB(port, index);
CPU::OutPortB(port+1, value);
}
static uint8_t ReadIndex(uint16_t port, uint8_t index)
{
CPU::OutPortB(port, index);
return CPU::InPortB(port+1);
}
static uint8_t ReplaceIndex(uint16_t port, uint8_t index, uint8_t value)
{
uint8_t ret = ReadIndex(port, index);
WriteIndex(port, index, value);
return ret;
}
static void FetchVGAFont(uint8_t* font)
{
// TODO: I just got these magic constants off the net.
// Select plane 2 for reading.
uint8_t old_03ce_04 = ReplaceIndex(0x03CE, 0x04, 0x02);
// Clear even/odd mode.
uint8_t old_03ce_05 = ReplaceIndex(0x03CE, 0x05, 0x04);
// Map VGA Memory to 0xA0000 (Select 0xA0000-0xAFFFF).
uint8_t old_03ce_06 = ReplaceIndex(0x03CE, 0x06, 0x04);
// Set bitplane 2.
uint8_t old_03c4_02 = ReplaceIndex(0x03C4, 0x02, 0x04);
// Clear even/odd mode (the other way, don't ask why).
uint8_t old_03c4_04 = ReplaceIndex(0x03C4, 0x04, 0x07);
// The font data is now at 0xA0000, so fetch it. Note that there is
// reserved room for a 8x32 resolution but we are using 8x16.
const uint8_t* data = (const uint8_t*) 0xA0000UL;
for ( size_t i = 0; i < VGA_FONT_NUMCHARS; i++ )
{
const uint8_t* src = data + (32*8)/8 * i;
uint8_t* dest = font + VGA_FONT_CHARSIZE * i;
Memory::Copy(dest, src, VGA_FONT_CHARSIZE);
}
// Restore the VGA state.
WriteIndex(0x03C4, 0x02, old_03c4_02);
WriteIndex(0x03C4, 0x04, old_03c4_04);
WriteIndex(0x03CE, 0x05, old_03ce_05);
WriteIndex(0x03CE, 0x06, old_03ce_06);
WriteIndex(0x03CE, 0x04, old_03ce_04);
}
#if TEST_VGAFONT
static void PrintFontChar(const uint8_t* font, unsigned char c)
{
const uint8_t* glyph = font + VGA_FONT_CHARSIZE * (size_t) c;
for ( size_t y = 0; y < VGA_FONT_HEIGHT; y++ )
{
for ( size_t x = 0; x < VGA_FONT_WIDTH; x++ )
{
size_t bitindex = y * VGA_FONT_WIDTH + x;
uint8_t bitmap = glyph[bitindex/8UL];
uint8_t bitmod = bitindex % 8UL;
uint8_t bitmask = 1U << bitmod;
const char* toprint = (bitmap & bitmask) ? "X" : " ";
Log::Print(toprint);
}
Log::Print("\n");
}
}
#endif
const uint8_t* GetFont()
{
return vgafont;
}
void Init()
{
vgafontsize = VGA_FONT_NUMCHARS * VGA_FONT_CHARSIZE;
if ( !(vgafont = new uint8_t[vgafontsize]) )
Panic("Unable to allocate vga font buffer");
FetchVGAFont(vgafont);
#if TEST_VGAFONT
PrintFontChar(vgafont, 'A');
PrintFontChar(vgafont, 'S');
#endif
DevMemoryBuffer* vgamembuf = new DevMemoryBuffer(vgafont, vgafontsize,
false, false);
if ( !vgamembuf )
Panic("Unable to allocate vga font filesystem object");
if ( !DeviceFS::RegisterDevice("vgafont", vgamembuf) )
Panic("Unable to register vga font filesystem object");
}
// Changes the position of the hardware cursor.
void SetCursor(unsigned x, unsigned y)
{
unsigned value = x + y * WIDTH;
// This sends a command to indicies 14 and 15 in the
// CRT Control Register of the VGA controller. These
// are the high and low bytes of the index that show
// where the hardware cursor is to be 'blinking'.
CPU::OutPortB(0x3D4, 14);
CPU::OutPortB(0x3D5, (value >> 8) & 0xFF);
CPU::OutPortB(0x3D4, 15);
CPU::OutPortB(0x3D5, (value >> 0) & 0xFF);
}
} // namespace VGA
DevVGA::DevVGA()
{
offset = 0;
}
DevVGA::~DevVGA()
{
#ifdef PLATFORM_SERIAL
// TODO: HACK: This is a hack that is unrelated to this file.
// This is a hack to make the cursor a proper color after the vga buffer
// has been radically modified. The best solution would be for the VGA
// to ANSI Escape Codes converter to keep track of colors and restoring
// them, but this will do for now.
Log::PrintF("\e[m");
#endif
}
ssize_t DevVGA::Read(uint8_t* dest, size_t count)
{
if ( VGA::VGA_SIZE - offset < count ) { count = VGA::VGA_SIZE - offset; }
Maxsi::Memory::Copy(dest, VGA::VGA + offset, count);
offset += count;
return count;
}
ssize_t DevVGA::Write(const uint8_t* src, size_t count)
{
2012-09-22 14:44:50 +00:00
if ( offset == VGA::VGA_SIZE && count ) { errno = ENOSPC; return -1; }
if ( VGA::VGA_SIZE - offset < count ) { count = VGA::VGA_SIZE - offset; }
Maxsi::Memory::Copy(VGA::VGA + offset, src, count);
offset = (offset + count) % VGA::VGA_SIZE;
VGA::SetCursor(VGA::WIDTH, VGA::HEIGHT-1);
#ifdef PLATFORM_SERIAL
SerialTerminal::OnVGAModified();
#endif
return count;
}
bool DevVGA::IsReadable()
{
return true;
}
bool DevVGA::IsWritable()
{
return true;
}
size_t DevVGA::BlockSize()
{
return 1;
}
uintmax_t DevVGA::Size()
{
return VGA::VGA_SIZE;
}
uintmax_t DevVGA::Position()
{
return offset;
}
bool DevVGA::Seek(uintmax_t position)
{
2012-09-22 14:44:50 +00:00
if ( VGA::VGA_SIZE < position ) { errno = EINVAL; return false; }
offset = position;
return true;
}
bool DevVGA::Resize(uintmax_t size)
{
if ( size == VGA::VGA_SIZE ) { return false; }
2012-09-22 14:44:50 +00:00
errno = ENOSPC;
return false;
}
} // namespace Sortix