~ubuntu-branches/ubuntu/utopic/grub/utopic

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* hercules.c - hercules console interface */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2001,2002  Free Software Foundation, Inc.
 *
 *  This program 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 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifdef SUPPORT_HERCULES

#include <shared.h>
#include <hercules.h>
#include <term.h>

/* The position of the cursor.  */
static int herc_x;
static int herc_y;

static int herc_standard_color = A_NORMAL;
static int herc_normal_color = A_NORMAL;
static int herc_highlight_color = A_REVERSE;
static int herc_current_color = A_NORMAL;
static color_state herc_color_state = COLOR_STATE_STANDARD;
static int herc_cursor_state = 1;

/* Write a byte to a port.  */
static inline void
outb (unsigned short port, unsigned char value)
{
  asm volatile ("outb	%b0, %w1" : : "a" (value), "Nd" (port));
}

static void
herc_set_cursor (void)
{
  unsigned offset = herc_y * HERCULES_WIDTH + herc_x;
  
  outb (HERCULES_INDEX_REG, 0x0f);
  outb (0x80, 0);
  outb (HERCULES_DATA_REG, offset & 0xFF);
  outb (0x80, 0);
  
  outb (HERCULES_INDEX_REG, 0x0e);
  outb (0x80, 0);
  outb (HERCULES_DATA_REG, offset >> 8);
  outb (0x80, 0);
}

void
hercules_putchar (int c)
{
  switch (c)
    {
    case '\b':
      if (herc_x > 0)
	herc_x--;
      break;
      
    case '\n':
      herc_y++;
      break;
      
    case '\r':
      herc_x = 0;
      break;

    case '\a':
      break;

    default:
      {
	volatile unsigned short *video
	  = (unsigned short *) HERCULES_VIDEO_ADDR;
	
	video[herc_y * HERCULES_WIDTH + herc_x]
	  = (herc_current_color << 8) | c;
	herc_x++;
	if (herc_x >= HERCULES_WIDTH)
	  {
	    herc_x = 0;
	    herc_y++;
	  }
      }
      break;
    }

  if (herc_y >= HERCULES_HEIGHT)
    {
      volatile unsigned long *video = (unsigned long *) HERCULES_VIDEO_ADDR;
      int i;
      
      herc_y = HERCULES_HEIGHT - 1;
      grub_memmove ((char *) HERCULES_VIDEO_ADDR,
		    (char *) HERCULES_VIDEO_ADDR + HERCULES_WIDTH * 2,
		    HERCULES_WIDTH * (HERCULES_HEIGHT - 1) * 2);
      for (i = HERCULES_WIDTH * (HERCULES_HEIGHT - 1) / 2;
	   i < HERCULES_WIDTH * HERCULES_HEIGHT / 2;
	   i++)
	video[i] = 0x07200720;
    }
}

void
hercules_cls (void)
{
  int i;
  volatile unsigned long *video = (unsigned long *) HERCULES_VIDEO_ADDR;
  
  for (i = 0; i < HERCULES_WIDTH * HERCULES_HEIGHT / 2; i++)
    video[i] = 0x07200720;

  herc_x = herc_y = 0;
  herc_set_cursor ();
}

int
hercules_getxy (void)
{
  return (herc_x << 8) | herc_y;
}

void
hercules_gotoxy (int x, int y)
{
  herc_x = x;
  herc_y = y;
  herc_set_cursor ();
}

void
hercules_setcolorstate (color_state state)
{
  switch (state) {
    case COLOR_STATE_STANDARD:
      herc_current_color = herc_standard_color;
      break;
    case COLOR_STATE_NORMAL:
      herc_current_color = herc_normal_color;
      break;
    case COLOR_STATE_HIGHLIGHT:
      herc_current_color = herc_highlight_color;
      break;
    default:
      herc_current_color = herc_standard_color;
      break;
  }

  herc_color_state = state;
}

void
hercules_setcolor (int normal_color, int highlight_color)
{
  herc_normal_color = normal_color;
  herc_highlight_color = highlight_color;
  
  hercules_setcolorstate (herc_color_state);
}

int
hercules_setcursor (int on)
{
  int old_state = herc_cursor_state;
  
  outb (HERCULES_INDEX_REG, 0x0a);
  outb (0x80, 0);
  outb (HERCULES_DATA_REG, on ? 0 : (1 << 5));
  outb (0x80, 0);
  herc_cursor_state = on;

  return old_state;
}

#endif /* SUPPORT_HERCULES */