summaryrefslogtreecommitdiff
path: root/viengoos/output-vga.c
blob: 1e9b853c0019c46f6fd7419b623b185504a73724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* output-vga.c - A VGA output driver.
   Copyright (C) 2003 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   This file is part of the GNU Hurd.

   The GNU Hurd 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 2, or (at
   your option) any later version.

   The GNU Hurd 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 this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/io.h>

#include "output.h"

#define VGA_VIDEO_MEM_BASE_ADDR	0x0B8000
#define VGA_VIDEO_MEM_LENGTH	0x004000

/* The default attribute is light grey on black.  */
#define VGA_DEF_ATTRIBUTE	7

#define VGA_COLUMNS	       	80
#define VGA_ROWS	       	25

/* The CRTC Registers.  XXX Depends on the I/O Address Select field.
   However, the only need to use the other values is for compatibility
   with monochrome adapters.  */
#define VGA_CRT_ADDR_REG	0x3d4
#define VGA_CRT_DATA_REG	0x3d5

/* The cursor position subregisters.  */
#define VGA_CRT_CURSOR_HIGH	0x0e
#define VGA_CRT_CURSOR_LOW	0x0f


/* Set the cursor position to POS, which is (x_pos + y_pos * width).  */
static void
vga_set_cursor_pos (unsigned int pos)
{
  outb (VGA_CRT_CURSOR_HIGH, VGA_CRT_ADDR_REG);
  outb (pos >> 8, VGA_CRT_DATA_REG);
  outb (VGA_CRT_CURSOR_LOW, VGA_CRT_ADDR_REG);
  outb (pos & 0xff, VGA_CRT_DATA_REG);
}


/* Get the cursor position, which is (x_pos + y_pos * width).  */
static unsigned int
vga_get_cursor_pos (void)
{
  unsigned int pos;

  outb (VGA_CRT_CURSOR_HIGH, VGA_CRT_ADDR_REG);
  pos = inb (VGA_CRT_DATA_REG) << 8;
  outb (VGA_CRT_CURSOR_LOW, VGA_CRT_ADDR_REG);
  pos |= inb (VGA_CRT_DATA_REG) & 0xff;

  return pos;
}


/* Global variables.  */

static int col;
static int row;
static char *video;


static void
vga_init (struct output_driver *device, const char *cfg)
{
  unsigned int pos = vga_get_cursor_pos ();
  col = pos % VGA_COLUMNS;
  row = pos / VGA_COLUMNS;

  /* FIXME: We are faulting in the video memory here.  We must have a
     way to give it back to the system eventually, for example to the
     physical memory server.  */
  video = (char *) VGA_VIDEO_MEM_BASE_ADDR;
}


static void
vga_putchar (struct output_driver *device, int chr)
{
  unsigned int pos;

  if (chr == '\n')
    {
      col = 0;
      row++;
    }
  else
    {
      pos = row * VGA_COLUMNS + col;
      video[2 * pos] = chr & 0xff;
      video[2 * pos + 1] = VGA_DEF_ATTRIBUTE;
      col++;
      if (col == VGA_COLUMNS)
	{
	  col = 0;
	  row++;
	}
    }

  if (row == VGA_ROWS)
    {
      int i;

      row--;
      for (i = 0; i < VGA_COLUMNS * row; i++)
	{
	  video[2 * i] = video[2 * (i + VGA_COLUMNS)];
	  video[2 * i + 1] = video[2 * (i + VGA_COLUMNS) + 1];
	}
      for (i = VGA_COLUMNS * row; i < VGA_COLUMNS * VGA_ROWS; i++)
	{
	  video[2 * i] = ' ';
	  video[2 * i + 1] = VGA_DEF_ATTRIBUTE;
	}
    }

  pos = row * VGA_COLUMNS + col;
  vga_set_cursor_pos (pos);
}


struct output_driver vga_output =
  {
    "vga",
    vga_init,
    0,		/* deinit */
    vga_putchar
  };