MentOS  0.8.0
The Mentoring Operating System
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Classes | Macros | Functions | Variables
vga.c File Reference

Implementation of Video Graphics Array (VGA) drivers. More...

Classes

struct  vga_ops_t
 VGA pointers for drawing operations. More...
 
struct  vga_font_t
 VGA font details. More...
 
struct  vga_driver_t
 VGA driver details. More...
 

Macros

#define __DEBUG_HEADER__   "[VGA ]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 
#define AC_INDEX   0x03C0
 Attribute Controller index port.
 
#define AC_WRITE   0x03C0
 Attribute Controller write port.
 
#define AC_READ   0x03C1
 Attribute Controller data port.
 
#define MISC_WRITE   0x03C2
 Miscellaneous output register.
 
#define MISC_READ   0x03CC
 Miscellaneous input register.
 
#define SC_INDEX   0x03C4
 Sequence controller index.
 
#define SC_DATA   0x03C5
 Sequence controller data.
 
#define PALETTE_MASK   0x03C6
 DAC Mask Register.
 
#define PALETTE_READ   0x03C7
 Controls the DAC.
 
#define PALETTE_INDEX   0x03C8
 Controls the DAC index.
 
#define PALETTE_DATA   0x03C9
 Controls the DAC data.
 
#define GC_INDEX   0x03CE
 Graphics controller index.
 
#define GC_DATA   0x03CF
 Graphics controller data.
 
#define CRTC_INDEX   0x03D4
 CRT controller index.
 
#define CRTC_DATA   0x03D5
 CRT controller data.
 
#define INPUT_STATUS_READ   0x03DA
 By reading this port it'll go to the index state.
 
#define MODE_NUM_SEQ_REGS   5
 Number of sequencer registers.
 
#define MODE_NUM_CRTC_REGS   25
 Number of CRTC registers.
 
#define MODE_NUM_GC_REGS   9
 Number of Graphics Controller (GC) registers.
 
#define MODE_NUM_AC_REGS   (16 + 5)
 Number of Attribute Controller (AC) registers.
 
#define MODE_NUM_REGS   (1 + MODE_NUM_SEQ_REGS + MODE_NUM_CRTC_REGS + MODE_NUM_GC_REGS + MODE_NUM_AC_REGS)
 Total number of registers.
 

Functions

static char * __get_seg (void)
 Returns the video address. More...
 
void __vga_set_color_map (unsigned int index, unsigned char r, unsigned char g, unsigned char b)
 Sets the color at the given index. More...
 
void __vga_get_color_map (unsigned int index, unsigned char *r, unsigned char *g, unsigned char *b)
 Gets the color at the given index. More...
 
static void __save_palette (palette_entry_t *p, size_t size)
 Saves the current palette in p. More...
 
static void __load_palette (palette_entry_t *p, size_t size)
 Loads the palette p. More...
 
static void __set_plane (unsigned int plane)
 Sets the current plane. More...
 
static unsigned __get_plane (void)
 Returns the current plane. More...
 
static unsigned char __read_byte (unsigned int offset)
 Reads from the video memory. More...
 
static void __write_byte (unsigned int offset, unsigned char value)
 Writes onto the video memory. More...
 
static void __set_mode (vga_mode_t *vga_mode)
 Sets the given mode. More...
 
static void __read_registers (vga_mode_t *vga_mode)
 Reads the VGA registers. More...
 
static void __write_font (unsigned char *buf, unsigned font_height)
 Writes the font. More...
 
static unsigned int __reverse_bits (char num)
 Reverses the bits of the given number. More...
 
static void __write_pixel_1 (int x, int y, unsigned char c)
 Writes a pixel. More...
 
static unsigned __read_pixel_1 (unsigned x, unsigned y)
 Reads a pixel. More...
 
static void __write_pixel_2 (int x, int y, unsigned char c)
 Writes a pixel. More...
 
static unsigned __read_pixel_2 (unsigned x, unsigned y)
 Reads a pixel. More...
 
static void __write_pixel_4 (int x, int y, unsigned char color)
 Writes a pixel. More...
 
static unsigned __read_pixel_4 (int x, int y)
 Reads a pixel. More...
 
static void __write_pixel_8 (int x, int y, unsigned char color)
 Writes a pixel. More...
 
static unsigned __read_pixel_8 (int x, int y)
 Reads a pixel. More...
 
int vga_is_enabled (void)
 Checks if the VGA is enabled. More...
 
int vga_width (void)
 Returns the width of the screen. More...
 
int vga_height (void)
 Returns the height of the screen. More...
 
void vga_draw_pixel (int x, int y, unsigned char color)
 Draws a pixel at the given position. More...
 
unsigned int vga_read_pixel (int x, int y)
 Reads a pixel at the given position. More...
 
void vga_draw_char (int x, int y, unsigned char c, unsigned char color)
 Draws a character at the given position. More...
 
void vga_draw_string (int x, int y, const char *str, unsigned char color)
 Draws a string at the given position. More...
 
void vga_draw_line (int x0, int y0, int x1, int y1, unsigned char color)
 Draws a line from point 1 to point 2. More...
 
void vga_draw_rectangle (int sx, int sy, int w, int h, unsigned char color)
 Draws a rectangle provided the position of the starting corner and the ending corner. More...
 
void vga_draw_circle (int xc, int yc, int r, unsigned char color)
 Draws a circle provided the position of the center and the radius. More...
 
void vga_draw_triangle (int x1, int y1, int x2, int y2, int x3, int y3, unsigned char color)
 Draws a triangle. More...
 
void vga_run_test (void)
 Run some VGA tests.
 
void vga_initialize (void)
 Initializes the VGA.
 
void vga_finalize (void)
 Finalizes the VGA.
 
static void __vga_clear_cursor (void)
 Clears the character at the cursor.
 
static void __vga_draw_cursor (void)
 Draws the cursor.
 
void vga_putc (int c)
 Print the given character on the screen. More...
 
void vga_puts (const char *str)
 Prints the given string on the screen. More...
 
void vga_move_cursor (unsigned int x, unsigned int y)
 Move the cursor at the position x, y on the screen. More...
 
void vga_get_cursor_position (unsigned int *x, unsigned int *y)
 Returns cursor's position on the screen. More...
 
void vga_get_screen_size (unsigned int *width, unsigned int *height)
 Returns screen size. More...
 
void vga_clear_screen (void)
 Clears the screen.
 
void vga_new_line (void)
 Move to the following line (the effect of
character).
 
void vga_update (void)
 Updates the graphic elements.
 
void vga_set_color (unsigned int color)
 Change the color. More...
 

Variables

static bool_t vga_enable = false
 Is VGA enabled.
 
palette_entry_t stored_palette [256]
 The stored palette.
 
char vidmem [262144]
 A buffer for storing a copy of the video memory.
 
static vga_driver_tdriver = NULL
 Current driver.
 
static vga_ops_t ops_720_480_16
 Operations for 720*480, and 16-bit color video. More...
 
static vga_ops_t ops_640_480_16
 Operations for 640*480, and 16-bit color video. More...
 
static vga_ops_t ops_320_200_256
 Operations for 320*200, and 256-bit color video. More...
 
static vga_font_t font_4x6
 4x6 font. More...
 
static vga_font_t font_5x6
 5x6 font. More...
 
static vga_font_t font_8x8
 8x8 font. More...
 
static vga_font_t font_8x14
 8x14 font. More...
 
static vga_font_t font_8x16
 8x16 font. More...
 
static vga_driver_t driver_720_480_16
 Drivers for 720*480, and 16-bit color video. More...
 
static vga_driver_t driver_640_480_16
 Drivers for 640*480, and 16-bit color video. More...
 
static vga_driver_t driver_320_200_256
 Drivers for 320*200, and 16-bit color video. More...
 
static int _x = 0
 Current x coordinate of the cursor.
 
static int _y = 0
 Current y coordinate of the cursor.
 
static unsigned char _color = 7
 Current color.
 
static int _cursor_state = 0
 Current state of the cursor.
 

Detailed Description

Implementation of Video Graphics Array (VGA) drivers.

Function Documentation

◆ __get_plane()

static unsigned __get_plane ( void  )
inlinestatic

Returns the current plane.

Returns
the current plane.

◆ __get_seg()

static char* __get_seg ( void  )
inlinestatic

Returns the video address.

Returns
pointer to the video.

◆ __load_palette()

static void __load_palette ( palette_entry_t p,
size_t  size 
)
static

Loads the palette p.

Parameters
ppalette we are going to load.
sizethe size of the palette.

◆ __read_byte()

static unsigned char __read_byte ( unsigned int  offset)
static

Reads from the video memory.

Parameters
offsetwhere we are going to read.
Returns
the value we read.

◆ __read_pixel_1()

static unsigned __read_pixel_1 ( unsigned  x,
unsigned  y 
)
inlinestatic

Reads a pixel.

Parameters
xx coordinates.
yy coordinates.
Returns
the pixel value.

◆ __read_pixel_2()

static unsigned __read_pixel_2 ( unsigned  x,
unsigned  y 
)
inlinestatic

Reads a pixel.

Parameters
xx coordinates.
yy coordinates.
Returns
the pixel value.

◆ __read_pixel_4()

static unsigned __read_pixel_4 ( int  x,
int  y 
)
inlinestatic

Reads a pixel.

Parameters
xx coordinates.
yy coordinates.
Returns
the pixel value.

◆ __read_pixel_8()

static unsigned __read_pixel_8 ( int  x,
int  y 
)
inlinestatic

Reads a pixel.

Parameters
xx coordinates.
yy coordinates.
Returns
the pixel value.

◆ __read_registers()

static void __read_registers ( vga_mode_t vga_mode)
static

Reads the VGA registers.

Parameters
vga_modethe current VGA mode.

◆ __reverse_bits()

static unsigned int __reverse_bits ( char  num)
inlinestatic

Reverses the bits of the given number.

Parameters
numthe number of which we want to reverse the bits.
Returns
reversed bits.

◆ __save_palette()

static void __save_palette ( palette_entry_t p,
size_t  size 
)
static

Saves the current palette in p.

Parameters
poutput variable where we save the palette.
sizethe size of the palette.

◆ __set_mode()

static void __set_mode ( vga_mode_t vga_mode)
static

Sets the given mode.

Parameters
vga_modethe new mode we set.

◆ __set_plane()

static void __set_plane ( unsigned int  plane)
inlinestatic

Sets the current plane.

Parameters
planethe plane to set.

◆ __vga_get_color_map()

void __vga_get_color_map ( unsigned int  index,
unsigned char *  r,
unsigned char *  g,
unsigned char *  b 
)

Gets the color at the given index.

Parameters
indexindex of the palette we want to read.
routput value for red.
goutput value for green.
boutput value for blue.

◆ __vga_set_color_map()

void __vga_set_color_map ( unsigned int  index,
unsigned char  r,
unsigned char  g,
unsigned char  b 
)

Sets the color at the given index.

Parameters
indexindex of the palette we want to change.
rred.
ggreen.
bblue.

◆ __write_byte()

static void __write_byte ( unsigned int  offset,
unsigned char  value 
)
static

Writes onto the video memory.

Parameters
offsetwhere we are going to write.
valuethe value to write.

◆ __write_font()

static void __write_font ( unsigned char *  buf,
unsigned  font_height 
)
static

Writes the font.

Parameters
bufbuffer where the font resides.
font_heightthe height of the font.

◆ __write_pixel_1()

static void __write_pixel_1 ( int  x,
int  y,
unsigned char  c 
)
inlinestatic

Writes a pixel.

Parameters
xx coordinates.
yy coordinates.
ccolor.

◆ __write_pixel_2()

static void __write_pixel_2 ( int  x,
int  y,
unsigned char  c 
)
inlinestatic

Writes a pixel.

Parameters
xx coordinates.
yy coordinates.
ccolor.

◆ __write_pixel_4()

static void __write_pixel_4 ( int  x,
int  y,
unsigned char  color 
)
inlinestatic

Writes a pixel.

Parameters
xx coordinates.
yy coordinates.
colorthe color.

◆ __write_pixel_8()

static void __write_pixel_8 ( int  x,
int  y,
unsigned char  color 
)
inlinestatic

Writes a pixel.

Parameters
xx coordinates.
yy coordinates.
colorthe color.

◆ vga_draw_char()

void vga_draw_char ( int  x,
int  y,
unsigned char  c,
unsigned char  color 
)

Draws a character at the given position.

Parameters
xx-axis position.
yy-axis position.
ccharacter to draw.
colorcolor of the character.

◆ vga_draw_circle()

void vga_draw_circle ( int  xc,
int  yc,
int  r,
unsigned char  color 
)

Draws a circle provided the position of the center and the radius.

Parameters
xcx-axis position.
ycy-axis position.
rradius.
colorused to draw the circle.

◆ vga_draw_line()

void vga_draw_line ( int  x0,
int  y0,
int  x1,
int  y1,
unsigned char  color 
)

Draws a line from point 1 to point 2.

Parameters
x0point 1 x-axis position.
y0point 1 y-axis position.
x1point 2 x-axis position.
y1point 2 y-axis position.
colorcolor of the line.

◆ vga_draw_pixel()

void vga_draw_pixel ( int  x,
int  y,
unsigned char  color 
)

Draws a pixel at the given position.

Parameters
xx-axis position.
yy-axis position.
colorcolor of the character.

◆ vga_draw_rectangle()

void vga_draw_rectangle ( int  sx,
int  sy,
int  w,
int  h,
unsigned char  color 
)

Draws a rectangle provided the position of the starting corner and the ending corner.

Parameters
sxtop-left corner x-axis position.
sytop-left corner y-axis position.
wwidth.
hheight.
colorcolor of the rectangle.

◆ vga_draw_string()

void vga_draw_string ( int  x,
int  y,
const char *  str,
unsigned char  color 
)

Draws a string at the given position.

Parameters
xx-axis position.
yy-axis position.
strstring to draw.
colorcolor of the character.

◆ vga_draw_triangle()

void vga_draw_triangle ( int  x1,
int  y1,
int  x2,
int  y2,
int  x3,
int  y3,
unsigned char  color 
)

Draws a triangle.

Parameters
x11st point x-axis position.
y11st point y-axis position.
x22nd point x-axis position.
y22nd point y-axis position.
x33rd point x-axis position.
y33rd point y-axis position.
colorused to draw the triangle.

◆ vga_get_cursor_position()

void vga_get_cursor_position ( unsigned int *  x,
unsigned int *  y 
)

Returns cursor's position on the screen.

Parameters
xThe output x coordinate.
yThe output y coordinate.

◆ vga_get_screen_size()

void vga_get_screen_size ( unsigned int *  width,
unsigned int *  height 
)

Returns screen size.

Parameters
widthThe screen width.
heightThe screen height.

◆ vga_height()

int vga_height ( void  )

Returns the height of the screen.

Returns
the height of the screen.

◆ vga_is_enabled()

int vga_is_enabled ( void  )

Checks if the VGA is enabled.

Returns
1 if enabled, 0 otherwise.

◆ vga_move_cursor()

void vga_move_cursor ( unsigned int  x,
unsigned int  y 
)

Move the cursor at the position x, y on the screen.

Parameters
xThe x coordinate.
yThe y coordinate.

◆ vga_putc()

void vga_putc ( int  c)

Print the given character on the screen.

Parameters
cThe character to print.

◆ vga_puts()

void vga_puts ( const char *  str)

Prints the given string on the screen.

Parameters
strThe string to print.

◆ vga_read_pixel()

unsigned int vga_read_pixel ( int  x,
int  y 
)

Reads a pixel at the given position.

Parameters
xx-axis position.
yy-axis position.
Returns
the pixel we read.

◆ vga_set_color()

void vga_set_color ( unsigned int  color)

Change the color.

Parameters
colorthe color to set.

◆ vga_width()

int vga_width ( void  )

Returns the width of the screen.

Returns
the width of the screen.

Variable Documentation

◆ driver_320_200_256

vga_driver_t driver_320_200_256
static
Initial value:
= {
.width = 320,
.height = 200,
.bpp = 256,
.address = (char *)NULL,
.ops = &ops_320_200_256,
}
#define NULL
Define NULL.
Definition: stddef.h:10
static vga_ops_t ops_320_200_256
Operations for 320*200, and 256-bit color video.
Definition: vga.c:685

Drivers for 320*200, and 16-bit color video.

◆ driver_640_480_16

vga_driver_t driver_640_480_16
static
Initial value:
= {
.width = 640,
.height = 480,
.bpp = 16,
.address = (char *)NULL,
.ops = &ops_640_480_16,
}
static vga_ops_t ops_640_480_16
Operations for 640*480, and 16-bit color video.
Definition: vga.c:677

Drivers for 640*480, and 16-bit color video.

◆ driver_720_480_16

vga_driver_t driver_720_480_16
static
Initial value:
= {
.width = 720,
.height = 480,
.bpp = 16,
.address = (char *)NULL,
.ops = &ops_720_480_16,
}
static vga_ops_t ops_720_480_16
Operations for 720*480, and 16-bit color video.
Definition: vga.c:669

Drivers for 720*480, and 16-bit color video.

◆ font_4x6

vga_font_t font_4x6
static
Initial value:
= {
.font = arr_4x6_font,
.width = 4,
.height = 6,
}
const unsigned char arr_4x6_font[]
Font 4x6.
Definition: vga_font.h:1047

4x6 font.

◆ font_5x6

vga_font_t font_5x6
static
Initial value:
= {
.font = arr_5x6_font,
.width = 5,
.height = 6,
}
const unsigned char arr_5x6_font[]
Font 5x6.
Definition: vga_font.h:787

5x6 font.

◆ font_8x14

vga_font_t font_8x14
static
Initial value:
= {
.font = arr_8x14_font,
.width = 8,
.height = 14,
}
static const unsigned char arr_8x14_font[]
Font 8x14.
Definition: vga_font.h:267

8x14 font.

◆ font_8x16

vga_font_t font_8x16
static
Initial value:
= {
.font = arr_8x16_font,
.width = 8,
.height = 16,
}
static const unsigned char arr_8x16_font[]
Font 8x16.
Definition: vga_font.h:7

8x16 font.

◆ font_8x8

vga_font_t font_8x8
static
Initial value:
= {
.font = arr_8x8_font,
.width = 8,
.height = 8,
}
static const unsigned char arr_8x8_font[]
Font 8x8.
Definition: vga_font.h:527

8x8 font.

◆ ops_320_200_256

vga_ops_t ops_320_200_256
static
Initial value:
= {
.write_pixel = __write_pixel_8,
.read_pixel = __read_pixel_8,
.draw_rect = NULL,
.fill_rect = NULL,
}
static void __write_pixel_8(int x, int y, unsigned char color)
Writes a pixel.
Definition: vga.c:494
static unsigned __read_pixel_8(int x, int y)
Reads a pixel.
Definition: vga.c:504

Operations for 320*200, and 256-bit color video.

◆ ops_640_480_16

vga_ops_t ops_640_480_16
static
Initial value:
= {
.write_pixel = __write_pixel_4,
.read_pixel = __read_pixel_4,
.draw_rect = NULL,
.fill_rect = NULL,
}
static unsigned __read_pixel_4(int x, int y)
Reads a pixel.
Definition: vga.c:483
static void __write_pixel_4(int x, int y, unsigned char color)
Writes a pixel.
Definition: vga.c:461

Operations for 640*480, and 16-bit color video.

◆ ops_720_480_16

vga_ops_t ops_720_480_16
static
Initial value:
= {
.write_pixel = __write_pixel_4,
.read_pixel = __read_pixel_4,
.draw_rect = NULL,
.fill_rect = NULL,
}

Operations for 720*480, and 16-bit color video.