Skip to content

Commit fc82b5d

Browse files
Implemented a simple status bar.
1 parent 357232b commit fc82b5d

File tree

12 files changed

+257
-541
lines changed

12 files changed

+257
-541
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"idt.h": "c",
55
"screen.h": "c",
66
"irq.h": "c",
7-
"keyboard.h": "c"
7+
"keyboard.h": "c",
8+
"date.h": "c"
89
}
910
}

main64/kernel/common.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,25 @@ int strcmp(char *s1, char *s2)
120120
return *(unsigned char *)s1 - *(unsigned char *)s2;
121121
}
122122

123+
// A simple strcat implementation
124+
char *strcat(char *destination, char *source)
125+
{
126+
// "ptr" points to the end of the destination string
127+
char *ptr = destination + strlen(destination);
128+
129+
// Add the source characters to the destination string
130+
while (*source != '\0')
131+
{
132+
*ptr++ = *source++;
133+
}
134+
135+
// Null terminate the destination string
136+
*ptr = '\0';
137+
138+
// Return the final string
139+
return destination;
140+
}
141+
123142
// Returns a substring from a given string
124143
int substring(char *source, int from, int n, char *target)
125144
{
@@ -290,4 +309,23 @@ int atoi(char *str)
290309
}
291310

292311
return res;
312+
}
313+
314+
// Formats an Integer value with a leading zero.
315+
void FormatInteger(int Value, char *Buffer)
316+
{
317+
char str[32] = "";
318+
319+
// Empty the buffer
320+
strcpy(Buffer, "");
321+
322+
// Add a leading zero - if necessary
323+
if (Value < 10)
324+
{
325+
strcat(Buffer, "0");
326+
}
327+
328+
// Add the integer value
329+
itoa(Value, 10, str);
330+
strcat(Buffer, str);
293331
}

main64/kernel/common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ char *strcpy(char *destination, const char *source);
4848
// A simple strcmp implementation
4949
int strcmp(char *s1, char *s2);
5050

51+
// A simple strcat implementation
52+
char *strcat(char *destination, char *source);
53+
5154
// Returns a substring from a given string
5255
int substring(char *source, int from, int n, char *target);
5356

@@ -78,4 +81,7 @@ static void ltoa_helper(unsigned long i, unsigned base, char *buf);
7881
// Converts an ASCII string to its integer value
7982
int atoi(char *str);
8083

84+
// Formats an Integer value with a leading zero.
85+
void FormatInteger(int Value, char *Buffer);
86+
8187
#endif

main64/kernel/date.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "common.h"
2+
#include "date.h"
3+
4+
// The number of days for each month
5+
int NumberOfDaysPerMonth[12] =
6+
{
7+
31, // January
8+
28, // February
9+
31, // March
10+
30, // April
11+
31, // Mai
12+
30, // June
13+
31, // July
14+
31, // August
15+
30, // September
16+
31, // October
17+
30, // November
18+
31 // December
19+
};
20+
21+
// Increments the system data by 1 second.
22+
void IncrementSystemDate()
23+
{
24+
// Getting a reference to the BIOS Information Block
25+
BiosInformationBlock *bib = (BiosInformationBlock *)BIB_OFFSET;
26+
27+
// Increment the system date by 1 second
28+
bib->Second++;
29+
30+
// Roll over to the next minute
31+
if (bib->Second > 59)
32+
{
33+
bib->Second = 0;
34+
bib->Minute++;
35+
}
36+
37+
// Roll over to the next hour
38+
if (bib->Minute > 59)
39+
{
40+
bib->Minute = 0;
41+
bib->Hour++;
42+
}
43+
44+
// Roll over to the next day
45+
if (bib->Hour > 23)
46+
{
47+
bib->Hour = 0;
48+
bib->Day++;
49+
}
50+
51+
// Roll over to the next month
52+
// We don't check here for the leap year!!!
53+
if (bib->Day > NumberOfDaysPerMonth[bib->Month - 1])
54+
{
55+
bib->Day = 1;
56+
bib->Month++;
57+
}
58+
}
59+
60+
// Sets the system date.
61+
void SetDate(int Year, int Month, int Day)
62+
{
63+
// Getting a reference to the BIOS Information Block
64+
BiosInformationBlock *bib = (BiosInformationBlock *)BIB_OFFSET;
65+
66+
// Set the date
67+
bib->Year = Year;
68+
bib->Month = Month;
69+
bib->Day = Day;
70+
}
71+
72+
// Sets the system time.
73+
void SetTime(int Hour, int Minute, int Second)
74+
{
75+
// Getting a reference to the BIOS Information Block
76+
BiosInformationBlock *bib = (BiosInformationBlock *)BIB_OFFSET;
77+
78+
// Set the time
79+
bib->Hour = Hour;
80+
bib->Minute = Minute;
81+
bib->Second = Second;
82+
}

main64/kernel/date.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef DATE_H
2+
#define DATE_H
3+
4+
// Increments the system data by 1 second.
5+
void IncrementSystemDate();
6+
7+
// Sets the system date.
8+
void SetDate(int Year, int Month, int Day);
9+
10+
// Sets the system time.
11+
void SetTime(int Hour, int Minute, int Second);
12+
13+
#endif

main64/kernel/drivers/keyboard.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void scanf(char *buffer, int buffer_size)
4444
// When we have hit the ENTER key, we have finished entering our input data
4545
if (key == KEY_RETURN)
4646
{
47-
print_char('\n', 1);
47+
print_char('\n');
4848
break;
4949
}
5050

@@ -66,7 +66,7 @@ void scanf(char *buffer, int buffer_size)
6666
// Clear out the last printed key
6767
// This also moves the cursor one character forward, so we have to go back
6868
// again with the cursor in the next step
69-
print_char(' ', 1);
69+
print_char(' ');
7070

7171
// Move the cursor position one character back again
7272
GetCursorPosition(&row, &col);
@@ -84,7 +84,7 @@ void scanf(char *buffer, int buffer_size)
8484
// If we have pressed a non-printable key, the character is not printed out
8585
if (key != 0)
8686
{
87-
print_char(key, 1);
87+
print_char(key);
8888
}
8989

9090
// Write the entered character into the provided buffer

main64/kernel/drivers/screen.c

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@
44
// Define a variable for the screen location information
55
ScreenLocation screenLocation;
66

7+
// The number of rows of the video memory
8+
int NumberOfRows;
9+
10+
// The number of columns of the video memory
11+
int NumberOfColumns;
12+
13+
// The blank character
14+
unsigned char BLANK = 0x20;
15+
716
// Initializes the screen
8-
void InitializeScreen()
17+
void InitializeScreen(int Cols, int Rows)
918
{
19+
NumberOfColumns = Cols;
20+
NumberOfRows = Rows;
21+
1022
screenLocation.Row = 1;
1123
screenLocation.Col = 1;
1224
screenLocation.Attributes = COLOR_WHITE;
@@ -41,7 +53,7 @@ void SetCursorPosition(int Row, int Col)
4153
void MoveCursor()
4254
{
4355
// Calculate the linear offset of the cursor
44-
short cursorLocation = (screenLocation.Row - 1) * COLS + (screenLocation.Col - 1);
56+
short cursorLocation = (screenLocation.Row - 1) * NumberOfColumns + (screenLocation.Col - 1);
4557

4658
// Setting the cursor's high byte
4759
outb(0x3D4, 14);
@@ -58,12 +70,12 @@ void ClearScreen()
5870
char *video_memory = (char *)VIDEO_MEMORY;
5971
int row, col;
6072

61-
for (row = 0; row < ROWS; row++)
73+
for (row = 0; row < NumberOfRows; row++)
6274
{
63-
for (col = 0; col < COLS; col++)
75+
for (col = 0; col < NumberOfColumns; col++)
6476
{
65-
int offset = row * COLS * 2 + col * 2;
66-
video_memory[offset] = 0x20; // Blank
77+
int offset = row * NumberOfColumns * 2 + col * 2;
78+
video_memory[offset] = BLANK;
6779
video_memory[offset + 1] = screenLocation.Attributes;
6880
}
6981
}
@@ -74,40 +86,30 @@ void ClearScreen()
7486
MoveCursor();
7587
}
7688

77-
int SetScreenRow(int Row)
78-
{
79-
int currentRow = screenLocation.Row;
80-
screenLocation.Row = Row;
81-
screenLocation.Col = 1;
82-
83-
return currentRow;
84-
}
85-
8689
// Scrolls the screen, when we have used more than 25 rows
8790
void Scroll()
8891
{
89-
// Get a space character with the default colour attributes.
90-
unsigned char attributeByte = (0 /*black*/ << 4) | (15 /*white*/ & 0x0F);
91-
unsigned short blank = 0x20 /* space */ | (attributeByte << 8);
92-
char* video_memory = (char *)VIDEO_MEMORY;
92+
unsigned char attributeByte = (COLOR_BLACK << 4) | (COLOR_WHITE & 0x0F);
93+
char *video_memory = (char *)VIDEO_MEMORY;
94+
int i;
9395

94-
// Row 25 is the end, this means we need to scroll up
95-
if (screenLocation.Row > ROWS)
96+
// Check if we have reached the last row of the screen.
97+
// This means we need to scroll up
98+
if (screenLocation.Row > NumberOfRows)
9699
{
97-
int i;
98-
for (i = 0; i < COLS * 2 * (ROWS - 1); i++)
100+
for (i = 0; i < NumberOfColumns * 2 * (NumberOfRows - 1); i++)
99101
{
100-
video_memory[i] = video_memory[i + (COLS * 2)];
102+
video_memory[i] = video_memory[i + (NumberOfColumns * 2)];
101103
}
102104

103105
// Blank the last line
104-
for (i = (ROWS - 1) * COLS * 2; i < ROWS * COLS * 2; i += 2)
106+
for (i = (NumberOfRows - 1) * NumberOfColumns * 2; i < NumberOfRows * NumberOfColumns * 2; i += 2)
105107
{
106-
video_memory[i] = blank;
108+
video_memory[i] = BLANK;
107109
video_memory[i + 1] = attributeByte;
108110
}
109111

110-
screenLocation.Row = ROWS;
112+
screenLocation.Row = NumberOfRows;
111113
}
112114
}
113115

@@ -116,26 +118,33 @@ void printf(char *string)
116118
{
117119
while (*string != '\0')
118120
{
119-
print_char(*string, 1);
121+
print_char(*string);
120122
string++;
121123
}
122124
}
123125

124-
// Prints out a null-terminated string, without scrolling
125-
// the terminal window.
126-
void printf_noscrolling(char *string)
126+
// Prints out the status line string
127+
void PrintStatusLine(char *string)
127128
{
129+
unsigned char color = (COLOR_GREEN << 4) | (COLOR_BLACK & 0x0F);
130+
char *video_memory = (char *)VIDEO_MEMORY;
131+
int colStatusLine = 1;
132+
128133
while (*string != '\0')
129134
{
130-
print_char(*string, 0);
135+
int offset = (25 - 1) * NumberOfColumns * 2 + (colStatusLine - 1) * 2;
136+
video_memory[offset] = *string;
137+
video_memory[offset + 1] = color;
138+
colStatusLine++;
139+
131140
string++;
132141
}
133142
}
134143

135144
// Prints a single character on the screen
136-
void print_char(char character, int scrolling)
145+
void print_char(char character)
137146
{
138-
char* video_memory = (char *)VIDEO_MEMORY;
147+
char *video_memory = (char *)VIDEO_MEMORY;
139148

140149
switch(character)
141150
{
@@ -155,7 +164,7 @@ void print_char(char character, int scrolling)
155164
}
156165
default:
157166
{
158-
int offset = (screenLocation.Row - 1) * COLS * 2 + (screenLocation.Col - 1) * 2;
167+
int offset = (screenLocation.Row - 1) * NumberOfColumns * 2 + (screenLocation.Col - 1) * 2;
159168
video_memory[offset] = character;
160169
video_memory[offset + 1] = screenLocation.Attributes;
161170
screenLocation.Col++;
@@ -164,11 +173,8 @@ void print_char(char character, int scrolling)
164173
}
165174
}
166175

167-
if (scrolling)
168-
{
169-
Scroll();
170-
MoveCursor();
171-
}
176+
Scroll();
177+
MoveCursor();
172178
}
173179

174180
// Prints out an integer value

0 commit comments

Comments
 (0)