-
Notifications
You must be signed in to change notification settings - Fork 82
/
console.d
224 lines (174 loc) · 5.4 KB
/
console.d
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
// This implements a console (text-mode) VGA driver.
module kernel.dev.console;
// Import system info
import kernel.system.info;
// For Gibs
import kernel.mem.gib;
import kernel.mem.giballocator;
import kernel.filesystem.ramfs;
// Errors
import kernel.core.error;
import kernel.core.kprintf;
// Shared structures for userspace
public import user.console;
import architecture.cpu;
import architecture.mutex;
// This is the true interface to the console
class Console {
static:
public:
// The number of columns and lines on the screen.
const uint COLUMNS = 80;
const uint LINES = 25;
// The default color.
const ubyte DEFAULTCOLORS = Color.LightGray;
// The width of a tab
const auto TABSTOP = 4;
void virtualAddress(void* addr) {
videoMemoryLocation = cast(ubyte*)addr;
}
// This will init the console driver
ErrorVal initialize() {
info.width = COLUMNS;
info.height = LINES;
Gib video = RamFS.create("/devices/video", Access.Kernel | Access.Read | Access.Write);
MetaData* videoMetaData = cast(MetaData*)video.ptr;
*videoMetaData = info;
video.seek(4096);
video.map(cast(ubyte*)0xB8000, 1024*1024);
videoMemoryLocation = cast(ubyte*)video.ptr + 4096;
videoInfo = videoMetaData;
uint temp = LINES * COLUMNS;
temp++;
Cpu.ioOut!(ushort, "0x3D4")(14);
Cpu.ioOut!(ushort, "0x3D5")(temp >> 8);
Cpu.ioOut!(ushort, "0x3D4")(15);
Cpu.ioOut!(ushort, "0x3D5")(temp);
return ErrorVal.Success;
}
// This method will clear the screen and return the cursor to (0,0).
void clearScreen() {
int i;
for (i=0; i < COLUMNS * LINES * 2; i++) {
*(videoMemoryLocation + i) = 0;
}
videoInfo.xpos = 0;
videoInfo.ypos = 0;
}
long getGlobalY() {
return videoInfo.globalY;
}
// This method will return the current location of the cursor
void getPosition(out int x, out int y) {
x = videoInfo.xpos;
y = videoInfo.ypos;
}
// This method will set the current location of the cursor to the x and y given.
synchronized void setPosition(int x, int y) {
if (x < 0) { x = 0; }
if (y < 0) { y = 0; }
if (x >= COLUMNS) { x = COLUMNS - 1; }
if (y >= LINES) { y = LINES - 1; }
long difference = cast(long)y - cast(long)videoInfo.ypos;
videoInfo.globalY += difference;
videoInfo.xpos = x;
videoInfo.ypos = y;
}
// This method will post the character to the screen at the current location.
synchronized void putChar(char c) {
if (c == '\t') {
// Insert a tab.
videoInfo.xpos += TABSTOP;
}
else if (c != '\n' && c != '\r') {
videoInfo.xpos %= COLUMNS;
videoInfo.ypos %= LINES;
ubyte* videoAddress = videoMemoryLocation;
videoAddress += (videoInfo.xpos + (videoInfo.ypos * COLUMNS)) * 2;
// Set the current piece of video memory to the character to print.
*(videoAddress) = c & 0xFF;
*(videoAddress + 1) = videoInfo.colorAttribute;
// increase the cursor position
videoInfo.xpos++;
}
// if you have reached the end of the line, or printing a newline, increase the y position
if (c == '\n' || c == '\r' || videoInfo.xpos >= COLUMNS) {
videoInfo.xpos = 0;
videoInfo.ypos++;
videoInfo.globalY++;
if (videoInfo.ypos >= LINES) {
_scrollDisplay(1);
}
}
}
// This mehtod will post a string to the screen at the current location.
void putString(char[] s) {
foreach(c; s) {
putChar(c);
}
}
// This function sets the console colors back to their defaults.
void resetColors() {
videoInfo.colorAttribute = DEFAULTCOLORS;
}
// This function will set the text foreground to a new color.
void setForeColor(Color newColor) {
videoInfo.colorAttribute = (videoInfo.colorAttribute & 0xf0) | newColor;
}
// This function will set the text background to a new color.
void setBackColor(Color newColor) {
videoInfo.colorAttribute = (videoInfo.colorAttribute & 0x0f) | (newColor << 4);
}
// This function will set both the foreground and background colors.
void setColors(Color foreColor, Color backColor) {
videoInfo.colorAttribute = (foreColor & 0x0f) | (backColor << 4);
}
synchronized void scrollDisplay(uint numLines) {
_scrollDisplay(numLines);
}
void* physicalLocation() {
return videoMemoryPhysLocation;
}
uint width() {
return COLUMNS;
}
uint height() {
return LINES;
}
private:
MetaData info;
MetaData* videoInfo = &info;
// Where the video memory lives (can be changed)
ubyte* videoMemoryLocation = cast(ubyte*)0xB8000UL;
const ubyte* videoMemoryPhysLocation = cast(ubyte*)0xB8000UL;
// This function will scroll the entire screen.
void _scrollDisplay(uint numLines) {
// obviously, scrolling all lines results in a cleared display. Use the faster function.
if (numLines >= LINES) {
clearScreen();
return;
}
int cury = 0;
int offset1 = 0;
int offset2 = numLines * COLUMNS;
// Go through and shift the correct amount.
for ( ; cury <= LINES - numLines; cury++) {
for (int curx = 0; curx < COLUMNS; curx++) {
*(videoMemoryLocation + (curx + offset1) * 2) = *(videoMemoryLocation + (curx + offset1 + offset2) * 2);
*(videoMemoryLocation + (curx + offset1) * 2 + 1) = *(videoMemoryLocation + (curx + offset1 + offset2) * 2 + 1);
}
offset1 += COLUMNS;
}
// clear the remaining lines
for (; cury <= LINES; cury++) {
for (int curx = 0; curx < COLUMNS; curx++) {
*(videoMemoryLocation + (curx + offset1) * 2) = 0x00;
*(videoMemoryLocation + (curx + offset1) * 2 + 1) = 0x00;
}
}
videoInfo.ypos -= numLines;
if (videoInfo.ypos < 0) {
videoInfo.ypos = 0;
}
}
}