luxos/include/console.h

152 lines
3.7 KiB
C

#include <system.h>
#ifndef __CONSOLE_H
#define __CONSOLE_H
#define _ATTRIB 0x0F
byte default_background, default_foreground;
// Change cursor position
void text_mode_cursor(int x, int y)
{
unsigned temp = y*current_mode_width + x;
outportb (0x3D4, 14);
outportb (0x3D5, temp >> 8);
outportb (0x3D4, 15);
outportb (0x3D5, temp);
}
// Set the default colors; max is 0x0F
void set_default_colors(byte back, byte fore)
{
if (back < 0x10) default_background = back;
if (fore < 0x10) default_foreground = fore;
}
// Clear screen, and set font to default font
void clrscr()
{
byte font = default_foreground | (default_background<<4);
int i = 0;
for (i = 0; i < current_mode_width*current_mode_height; i++)
{ TextVideoRam[2*i] = 0;
TextVideoRam[2*i+1] = font;
}
cursor_x = 0; cursor_y = 0;
}
void scroll(int n)
{
memcpy(TextVideoRam,
TextVideoRam+(current_mode_width*n*2),
2*current_mode_width*(current_mode_height - n));
byte blank = default_foreground | (default_background<<4);
int i;
for (i = current_mode_width*(current_mode_height-n);
i < current_mode_width*current_mode_height; i++){
TextVideoRam[2*i] = 0;
TextVideoRam[2*i+1] = blank;
}
}
void _endl()
{
cursor_x = 0;
if (++cursor_y >=25) {
cursor_y = 24; scroll(1);
}
}
// Put character on screen in specified position; can use different font colors
void putc_pos_font(int x, int y, char c, byte back, byte fore)
{
TextVideoRam[2*(y*current_mode_width+x)] = c;
TextVideoRam[2*(y*current_mode_width+x)+1] = fore|(back<<4);
}
// Put character on screen in specified position; use default font colors
void putc_pos(int x, int y, char c)
{
TextVideoRam[2*(y*current_mode_width+x)] = c;
}
// Put character on screen in the current cursor position; different font colors
void putc_font(char c, byte back, byte fore)
{
if (cursor_x > current_mode_width) _endl();
if (c == '\n') {_endl(); return;};
TextVideoRam[2*(cursor_y*current_mode_width+cursor_x)] = c;
TextVideoRam[2*(cursor_y*current_mode_width+cursor_x)+1] = fore|(back<<4);
cursor_x++;
}
// Put character on screen in the current cursor position; default font colors
void putc(char c)
{
if (cursor_x > current_mode_width) _endl();
if (c == '\n') {_endl(); return;};
TextVideoRam[2*(cursor_y*current_mode_width+cursor_x)] = c;
cursor_x++;
}
// Put string on screen in specified position; can use different font colors
void puts_pos_font(int x, int y, char *str, byte back, byte fore)
{
int i;
for (i = 0; str[i] != 0; i++)
putc_pos_font(x+i, y, str[i], back, fore);
}
// Put string on screen in specified position; use default font colors
void puts_pos(int x, int y, char *str)
{
int i;
for (i = 0; str[i] != 0; i++)
putc_pos(x+i, y, str[i]);
}
void puts(char *str)
{
int i;
for (i = 0; str[i] != 0; i++)
putc(str[i]);
}
void puts_font(char *str, byte back, byte fore)
{
int i;
for (i = 0; str[i] != 0; i++)
putc_font(str[i], back, fore);
}
void put_hex(int x, int y, unsigned int alpha)
{
char hex[] = "0123456789ABCDEF";
char nr[9];
int i;
for (i = 7; i >= 0; i--) {
nr[i] = hex[alpha%16];
alpha /= 16;
}
nr[8] = 0;
puts_pos(x, y, nr);
}
void put_bin (int x, int y, byte xz)
{
int i;
char arr[9] = {0,0,0,0,0,0,0,0,0};
for(i=7; i>=0; i--) {
arr[i] = (xz%2) + '0'; xz/=2;
}
puts_pos (x, y, arr);
}
#endif