-
-
Notifications
You must be signed in to change notification settings - Fork 603
/
Copy pathvga.cc
228 lines (190 loc) · 5.95 KB
/
vga.cc
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (C) 2013 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
#include "vga.hh"
#include "console.hh"
#include "pci-function.hh"
#include <osv/mmu.hh>
namespace console {
volatile unsigned short * const VGAConsole::_buffer
= reinterpret_cast<volatile unsigned short *>(mmu::phys_mem + 0xb8000);
static void tsm_log_cb(void *data, const char *file, int line, const char *func,
const char *subs, unsigned int sev, const char *format, va_list args)
{
printf("%s:%d:%s subs:%s sev:%d ", file, line, func, subs, sev);
vprintf(format, args);
printf("\n");
}
static void tsm_write_cb(struct tsm_vte *vte, const char *u8, size_t len,
void *data)
{
VGAConsole *vga = reinterpret_cast<VGAConsole *>(data);
vga->push_queue(u8, len);
}
static int tsm_draw_cb(struct tsm_screen *screen, uint32_t id,
const uint32_t *ch, size_t len, unsigned int cwidth, unsigned int posx,
unsigned int posy, const struct tsm_screen_attr *attr, tsm_age_t age,
void *data)
{
VGAConsole *vga = reinterpret_cast<VGAConsole *>(data);
if (len)
vga->draw(*ch, attr, posx, posy);
else
vga->draw(' ', attr, posx, posy);
return 0;
}
static int tsm_cursor_cb(struct tsm_screen *screen, unsigned int posx,
unsigned int posy, void *data)
{
VGAConsole *vga = reinterpret_cast<VGAConsole *>(data);
vga->move_cursor(posx, posy);
return 0;
}
static int tsm_scroll_cb(struct tsm_screen *screen, int scroll_count, void *data)
{
VGAConsole *vga = reinterpret_cast<VGAConsole *>(data);
vga->update_offset(scroll_count);
return 0;
}
VGAConsole::VGAConsole(pci::device& pci_dev)
: hw_driver()
, _pci_dev(pci_dev)
, _offset()
, _offset_dirty(false)
{
tsm_screen_new(&_tsm_screen, tsm_log_cb, this);
tsm_screen_resize(_tsm_screen, NCOLS, NROWS);
tsm_screen_set_max_sb(_tsm_screen, 1024);
tsm_vte_new(&_tsm_vte, _tsm_screen, tsm_write_cb, this, tsm_log_cb, this);
// copy the current screen to _history
for (unsigned i = 0; i < BUFFER_SIZE; i++) {
_history[i] = _buffer[i];
}
/* This driver does not clear framebuffer, since most of hypervisor clears on start up */
debugf("vga: Add VGA device instance\n");
console::console_driver_add(this);
}
void VGAConsole::push_queue(const char *str, size_t len)
{
for (size_t i = 0; i < len; i++)
_read_queue.push(str[i]);
_thread->wake();
}
hw_driver* VGAConsole::probe(hw_device* hw_dev)
{
if (auto pci_dev = dynamic_cast<pci::device*>(hw_dev)) {
auto base_class = pci_dev->get_base_class_code();
if (base_class == pci::function::PCI_CLASS_DISPLAY) {
return new VGAConsole(*pci_dev);
}
}
return nullptr;
}
void VGAConsole::dump_config()
{
u8 B, D, F;
_pci_dev.get_bdf(B, D, F);
_pci_dev.dump_config();
}
void VGAConsole::draw(const uint32_t c, const struct tsm_screen_attr *attr,
unsigned int x, unsigned int y)
{
uint32_t c2 = ((attr->bccode << 4) | (attr->fccode & 0xf) << 8) |
(c & 0xff);
unsigned idx = (y + _offset) * NCOLS + x;
if (_history[idx] != c2) {
_buffer[idx] = c2;
_history[idx] = c2;
}
}
void VGAConsole::move_cursor(unsigned int x, unsigned int y)
{
uint16_t cursor = (y + _offset) * NCOLS + x;
processor::outw((VGA_CRTC_CURSOR_LO) | (cursor & 0xff) << 8, VGA_CRT_IC);
processor::outw((VGA_CRTC_CURSOR_HI) | ((cursor >> 8) & 0xff) << 8, VGA_CRT_IC);
}
void VGAConsole::update_offset(int scroll_count)
{
_offset += scroll_count;
/* Don't have anymore buffer, need to rotate */
if (_offset > OFFSET_LIMIT || _offset < 0)
_offset = 0;
_offset_dirty = true;
}
void VGAConsole::apply_offset()
{
uint16_t start = _offset * NCOLS;
processor::outw((VGA_CRTC_START_LO) | (start & 0xff) << 8, VGA_CRT_IC);
processor::outw((VGA_CRTC_START_HI) | ((start >> 8) & 0xff) << 8, VGA_CRT_IC);
_offset_dirty = false;
}
void VGAConsole::write(const char *str, size_t len)
{
tsm_vte_input(_tsm_vte, str, len);
}
void VGAConsole::flush()
{
tsm_screen_draw(_tsm_screen, tsm_draw_cb, tsm_cursor_cb, tsm_scroll_cb, this);
if (_offset_dirty)
apply_offset();
}
bool VGAConsole::input_ready()
{
return !_read_queue.empty() || _kbd->input_ready();
}
char VGAConsole::readch()
{
uint32_t key;
unsigned int mods = 0;
char c;
while(1) {
if (!_read_queue.empty()) {
c = _read_queue.front();
_read_queue.pop();
return c;
}
key = _kbd->readkey();
if (!key) {
if (_read_queue.empty())
return 0;
continue;
}
if (_kbd->shift & MOD_SHIFT)
mods |= TSM_SHIFT_MASK;
if (_kbd->shift & MOD_CTL)
mods |= TSM_CONTROL_MASK;
if (_kbd->shift & MOD_ALT)
mods |= TSM_ALT_MASK;
switch (key) {
case KEY_Up:
tsm_screen_sb_up(_tsm_screen, 1);
tsm_screen_draw(_tsm_screen, tsm_draw_cb, tsm_cursor_cb, tsm_scroll_cb, this);
break;
case KEY_Down:
tsm_screen_sb_down(_tsm_screen, 1);
tsm_screen_draw(_tsm_screen, tsm_draw_cb, tsm_cursor_cb, tsm_scroll_cb, this);
break;
case KEY_Page_Up:
tsm_screen_sb_page_up(_tsm_screen, 1);
tsm_screen_draw(_tsm_screen, tsm_draw_cb, tsm_cursor_cb, tsm_scroll_cb, this);
break;
case KEY_Page_Down:
tsm_screen_sb_page_down(_tsm_screen, 1);
tsm_screen_draw(_tsm_screen, tsm_draw_cb, tsm_cursor_cb, tsm_scroll_cb, this);
break;
default:
if (tsm_vte_handle_keyboard(_tsm_vte, key, mods))
tsm_screen_sb_reset(_tsm_screen);
}
if (_read_queue.empty())
return 0;
}
}
void VGAConsole::dev_start()
{
_kbd = new Keyboard(_thread);
}
}