Skip to content

Commit 19b9efa

Browse files
Merge branch '0007-Timer-Support'
2 parents c8dc758 + fc82b5d commit 19b9efa

File tree

11 files changed

+325
-63
lines changed

11 files changed

+325
-63
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/screen.c

Lines changed: 49 additions & 20 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
}
@@ -77,28 +89,27 @@ void ClearScreen()
7789
// Scrolls the screen, when we have used more than 25 rows
7890
void Scroll()
7991
{
80-
// Get a space character with the default colour attributes.
81-
unsigned char attributeByte = (0 /*black*/ << 4) | (15 /*white*/ & 0x0F);
82-
unsigned short blank = 0x20 /* space */ | (attributeByte << 8);
83-
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;
8495

85-
// Row 25 is the end, this means we need to scroll up
86-
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)
8799
{
88-
int i;
89-
for (i = 0; i < COLS * 2 * (ROWS - 1); i++)
100+
for (i = 0; i < NumberOfColumns * 2 * (NumberOfRows - 1); i++)
90101
{
91-
video_memory[i] = video_memory[i + (COLS * 2)];
102+
video_memory[i] = video_memory[i + (NumberOfColumns * 2)];
92103
}
93104

94105
// Blank the last line
95-
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)
96107
{
97-
video_memory[i] = blank;
108+
video_memory[i] = BLANK;
98109
video_memory[i + 1] = attributeByte;
99110
}
100111

101-
screenLocation.Row = 25;
112+
screenLocation.Row = NumberOfRows;
102113
}
103114
}
104115

@@ -112,10 +123,28 @@ void printf(char *string)
112123
}
113124
}
114125

126+
// Prints out the status line string
127+
void PrintStatusLine(char *string)
128+
{
129+
unsigned char color = (COLOR_GREEN << 4) | (COLOR_BLACK & 0x0F);
130+
char *video_memory = (char *)VIDEO_MEMORY;
131+
int colStatusLine = 1;
132+
133+
while (*string != '\0')
134+
{
135+
int offset = (25 - 1) * NumberOfColumns * 2 + (colStatusLine - 1) * 2;
136+
video_memory[offset] = *string;
137+
video_memory[offset + 1] = color;
138+
colStatusLine++;
139+
140+
string++;
141+
}
142+
}
143+
115144
// Prints a single character on the screen
116145
void print_char(char character)
117146
{
118-
char* video_memory = (char *)VIDEO_MEMORY;
147+
char *video_memory = (char *)VIDEO_MEMORY;
119148

120149
switch(character)
121150
{
@@ -135,7 +164,7 @@ void print_char(char character)
135164
}
136165
default:
137166
{
138-
int offset = (screenLocation.Row - 1) * COLS * 2 + (screenLocation.Col - 1) * 2;
167+
int offset = (screenLocation.Row - 1) * NumberOfColumns * 2 + (screenLocation.Col - 1) * 2;
139168
video_memory[offset] = character;
140169
video_memory[offset + 1] = screenLocation.Attributes;
141170
screenLocation.Col++;

main64/kernel/drivers/screen.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
// Video output memory address
55
#define VIDEO_MEMORY 0xFFFF8000000B8000
66

7-
// The number of rows of the video memory
8-
#define ROWS 25
9-
10-
// The number of columns of the video memory
11-
#define COLS 80
12-
137
#define CRLF '\n'
148
#define TAB '\t'
159

@@ -48,7 +42,7 @@ typedef struct ScreenLocation
4842
} ScreenLocation;
4943

5044
// Initializes the screen
51-
void InitializeScreen();
45+
void InitializeScreen(int Cols, int Rows);
5246

5347
// Sets the specific color
5448
int SetColor(int Color);
@@ -68,6 +62,9 @@ void ClearScreen();
6862
// Scrolls the screen, when we have used more than 25 rows
6963
void Scroll();
7064

65+
// Prints out a status line string
66+
void PrintStatusLine(char *string);
67+
7168
// Prints a null-terminated string
7269
void printf(char *string);
7370

0 commit comments

Comments
 (0)