MonkOS  v0.1
A simple 64-bit operating system (x86_64)
tty.c File Reference

Teletype (console) screen text manipulation routines. More...

#include <core.h>
#include <libc/stdio.h>
#include <libc/string.h>
#include <kernel/x86/cpu.h>
#include <kernel/device/tty.h>

Go to the source code of this file.

Data Structures

struct  tty_t
 Virtual console state. More...
 

Macros

#define CRTC_PORT_CMD   0x03d4
 Command port for CRT controller. More...
 
#define CRTC_PORT_DATA   0x03d5
 Data port for CRT controller. More...
 
#define CRTC_CMD_STARTADDR_HI   0x0c
 Hi-byte of buffer start address. More...
 
#define CRTC_CMD_STARTADDR_LO   0x0d
 Lo-byte of buffer start address. More...
 
#define CRTC_CMD_CURSORADDR_HI   0x0e
 Hi-byte of cursor start address. More...
 
#define CRTC_CMD_CURSORADDR_LO   0x0f
 Lo-byte of cursor start address. More...
 
#define SCREEN_ROWS   25
 
#define SCREEN_COLS   80
 
#define SCREEN_SIZE   (SCREEN_ROWS * SCREEN_COLS)
 
#define SCREEN_BUFFER   0x000b8000
 

Functions

static uint16_t color (textcolor_t fg, textcolor_t bg)
 
static void update_buffer_offset ()
 
static void update_cursor ()
 
void tty_init ()
 Initialize all virtual consoles. More...
 
void tty_activate (int id)
 Activate the requested virtual console. More...
 
void tty_set_textcolor (int id, textcolor_t fg, textcolor_t bg)
 Set the foreground and background colors used to display text on the virtual console. More...
 
void tty_set_textcolor_fg (int id, textcolor_t fg)
 Set the foreground color used to display text on the virtual console. More...
 
void tty_set_textcolor_bg (int id, textcolor_t bg)
 Set the background color used to display text on the virtual console. More...
 
textcolor_t tty_get_textcolor_fg (int id)
 Get the foreground color used to display text on the virtual console. More...
 
textcolor_t tty_get_textcolor_bg (int id)
 Get the background color used to display text on the virtual console. More...
 
void tty_clear (int id)
 Clear the virtual console screen's contents using the current text background color. More...
 
void tty_setpos (int id, screenpos_t pos)
 Set the position of the cursor on the virtual console. More...
 
void tty_getpos (int id, screenpos_t *pos)
 Get the current position of the cursor on the virtual console. More...
 
static int colorcode (char x, int orig)
 
static void tty_printchar (tty_t *cons, const char **strptr)
 
void tty_print (int id, const char *str)
 Output a null-terminated string to the virtual console using the console's current text color and screen position. More...
 
void tty_printc (int id, char ch)
 Output a single character to the virtual console using the console's current text color and screen position. More...
 
int tty_printf (int id, const char *format,...)
 Output a printf-formatted string to the virtual console using the console's current text color and screen position. More...
 

Variables

static tty_t tty [MAX_TTYS]
 All virtual consoles. More...
 
static tty_t * active_tty
 The currently visible console. More...
 

Detailed Description

Teletype (console) screen text manipulation routines.

Definition in file tty.c.


Data Structure Documentation

struct tty

Virtual console state.

Definition at line 33 of file tty.c.

Data Fields
uint16_t textcolor Current fg/bg color (shifted).
uint16_t textcolor_orig Original, non-override text color.
screenpos_t pos Current screen position.
uint8_t ybuf Virtual buffer y position.
uint16_t * screen Virtual screen buffer for 50 rows.
uint16_t * tlcorner Points to char in top-left corner.

Macro Definition Documentation

#define CRTC_PORT_CMD   0x03d4

Command port for CRT controller.

Definition at line 17 of file tty.c.

Referenced by update_buffer_offset(), and update_cursor().

#define CRTC_PORT_DATA   0x03d5

Data port for CRT controller.

Definition at line 18 of file tty.c.

Referenced by update_buffer_offset(), and update_cursor().

#define CRTC_CMD_STARTADDR_HI   0x0c

Hi-byte of buffer start address.

Definition at line 21 of file tty.c.

Referenced by update_buffer_offset().

#define CRTC_CMD_STARTADDR_LO   0x0d

Lo-byte of buffer start address.

Definition at line 22 of file tty.c.

Referenced by update_buffer_offset().

#define CRTC_CMD_CURSORADDR_HI   0x0e

Hi-byte of cursor start address.

Definition at line 23 of file tty.c.

Referenced by update_cursor().

#define CRTC_CMD_CURSORADDR_LO   0x0f

Lo-byte of cursor start address.

Definition at line 24 of file tty.c.

Referenced by update_cursor().

#define SCREEN_ROWS   25

Definition at line 27 of file tty.c.

Referenced by tty_printchar().

#define SCREEN_COLS   80

Definition at line 28 of file tty.c.

Referenced by tty_printchar(), and update_cursor().

#define SCREEN_SIZE   (SCREEN_ROWS * SCREEN_COLS)

Definition at line 29 of file tty.c.

Referenced by tty_clear(), and tty_printchar().

#define SCREEN_BUFFER   0x000b8000

Definition at line 30 of file tty.c.

Referenced by tty_init(), update_buffer_offset(), and update_cursor().

Function Documentation

static uint16_t color ( textcolor_t  fg,
textcolor_t  bg 
)
inlinestatic

Definition at line 49 of file tty.c.

Referenced by tty_init(), tty_set_textcolor(), tty_set_textcolor_bg(), and tty_set_textcolor_fg().

static void update_buffer_offset ( )
static

Definition at line 55 of file tty.c.

Referenced by tty_activate(), tty_clear(), and tty_printchar().

static void update_cursor ( )
static

Definition at line 72 of file tty.c.

Referenced by tty_activate(), tty_clear(), tty_print(), tty_printc(), and tty_setpos().

void tty_init ( )

Initialize all virtual consoles.

This function must be called before any other console functions can be used.

Definition at line 89 of file tty.c.

Referenced by kmain().

void tty_activate ( int  id)

Activate the requested virtual console.

The virtual console's buffer is immediately displayed on the screen.

Parameters
[in]idVirtual tty id (0-3).

Definition at line 107 of file tty.c.

Referenced by isr_fatal().

void tty_set_textcolor ( int  id,
textcolor_t  fg,
textcolor_t  bg 
)

Set the foreground and background colors used to display text on the virtual console.

Parameters
[in]idVirtual tty id (0-3).
[in]fgForeground color.
[in]bgBackground color.

Definition at line 122 of file tty.c.

Referenced by isr_fatal(), and kmain().

void tty_set_textcolor_fg ( int  id,
textcolor_t  fg 
)

Set the foreground color used to display text on the virtual console.

Parameters
[in]idVirtual tty id (0-3).
[in]fgForeground color.

Definition at line 132 of file tty.c.

void tty_set_textcolor_bg ( int  id,
textcolor_t  bg 
)

Set the background color used to display text on the virtual console.

Parameters
[in]idVirtual tty id (0-3).
[in]bgBackground color.

Definition at line 143 of file tty.c.

textcolor_t tty_get_textcolor_fg ( int  id)

Get the foreground color used to display text on the virtual console.

Parameters
[in]idVirtual tty id (0-3).
Returns
Foreground color.

Definition at line 154 of file tty.c.

Referenced by tty_set_textcolor_bg().

textcolor_t tty_get_textcolor_bg ( int  id)

Get the background color used to display text on the virtual console.

Parameters
[in]idVirtual tty id (0-3).
Returns
Background color.

Definition at line 164 of file tty.c.

Referenced by tty_set_textcolor_fg().

void tty_clear ( int  id)

Clear the virtual console screen's contents using the current text background color.

Parameters
[in]idVirtual tty id (0-3).

Definition at line 174 of file tty.c.

Referenced by isr_fatal(), and kmain().

void tty_setpos ( int  id,
screenpos_t  pos 
)

Set the position of the cursor on the virtual console.

Text written to the console after this function will be located at the requested screen position.

Parameters
[in]idVirtual tty id (0-3).
[in]posThe screen position of the cursor.

Definition at line 194 of file tty.c.

void tty_getpos ( int  id,
screenpos_t *  pos 
)

Get the current position of the cursor on the virtual console.

Parameters
[in]idVirtual tty id (0-3).
[out]posA pointer to a screenpos_t to receive the position.

Definition at line 209 of file tty.c.

static int colorcode ( char  x,
int  orig 
)
static

Definition at line 218 of file tty.c.

Referenced by tty_printchar().

static void tty_printchar ( tty_t *  cons,
const char **  strptr 
)
static

Definition at line 240 of file tty.c.

Referenced by tty_print(), and tty_printc().

void tty_print ( int  id,
const char *  str 
)

Output a null-terminated string to the virtual console using the console's current text color and screen position.

A newline character (\n) causes the screen position to be updated as though a carriage return and line feed were performed.

To change the foreground color on the fly without having to call a console function, you may use the escape sequence \033[x], where x is a single character representing the foreground color to use for all following text. If x is a hexadecimal digit, then it represents one of the 16 textcolor codes. If x is '-', then it represents the console's original foreground color setting.

To change the background color on the fly, use the escape sequence \033{x}. The meaning of x is the same as with the foreground color escape sequence.

Parameters
[in]idVirtual tty id (0-3).
[in]strThe null-terminated string to be printed.

Definition at line 343 of file tty.c.

Referenced by cmd_display_apic(), cmd_display_help(), cmd_display_pcie(), cmd_switch_to_keycodes(), command_prompt(), dump_context(), isr_breakpoint(), kmain(), and tty_printf().

void tty_printc ( int  id,
char  ch 
)

Output a single character to the virtual console using the console's current text color and screen position.

See tty_print for further details.

Parameters
[in]idVirtual tty id (0-3).
[in]chThe character to be printed.

Definition at line 357 of file tty.c.

Referenced by command_run().

int tty_printf ( int  id,
const char *  format,
  ... 
)

Output a printf-formatted string to the virtual console using the console's current text color and screen position.

See tty_print for further details.

Parameters
[in]idVirtual tty id (0-3).
[in]formatThe null-terminated format string used to format the text to be printed.
[in]...Variable arguments list to be initialized with va_start.
Returns
The number of characters written to the console.

Definition at line 372 of file tty.c.

Referenced by cmd_display_apic(), cmd_display_help(), cmd_display_pcie(), command_exec(), dump_context(), isr_fatal(), keycode_run(), and probe_function().

Variable Documentation

tty_t tty[MAX_TTYS]
static

All virtual consoles.

Definition at line 45 of file tty.c.

tty_t* active_tty
static

The currently visible console.

Definition at line 46 of file tty.c.

Referenced by tty_activate(), tty_clear(), tty_init(), tty_print(), tty_printc(), tty_printchar(), tty_setpos(), update_buffer_offset(), and update_cursor().