I’ve been working on a kernel that my system needs. I’ve implement some functions that acts as a wrapper for I/O.
But somehow, the get_char() only returns 0.
Code:
<code>#include <cstdint>
#include <cstdio>
extern "C" void cout(char c, int position) {
// 0xb8000 is the starting address for video memory in text mode
// Each character on the screen is represented by 2 bytes:
// - The first byte is the ASCII character code.
// - The second byte is the attribute byte (color).
// position: the index on the screen where the character will be displayed.
// 80 characters per line, so a position of 0 is the top-left corner.
char* video_memory = (char*)0xb8000;
video_memory[position * 2] = c; // Set the character
video_memory[position * 2 + 1] = 0x0F; // White text on black background
}
extern "C" void cout_string(const char* str, int start_pos) {
int pos = start_pos;
while (*str) {
cout(*str++, pos++);
}
}
extern "C" int strlen(const char* str) {
int length = 0;
while (str[length] != '') {
length++;
}
return length;
}
uint8_t inb(uint16_t port) {
uint8_t result;
__asm__ __volatile__ ("inb %1, %0" : "=a"(result) : "Nd"(port));
return result;
}
void wait_for_keypress() {
// Poll the keyboard status port (0x64) until the output buffer is full
while (!(inb(0x60) & 0x01)) {
// Busy-wait until data is available in the output buffer
}
}
// Simple scancode to ASCII conversion (only handling basic characters for example purposes)
char scancode_to_ascii(uint8_t scancode) {
static char scancode_map[256] = { //testing
46, 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,
};
if (scancode > 255) {
return 0; // Unknown scancode
}
return scancode_map[scancode];
}
char get_char() {
uint8_t scancode;
// Wait for a key press by polling the keyboard status port
wait_for_keypress();
// Now read the scancode from the keyboard data port (0x60)
scancode = inb(0x60);
// Convert the scancode to ASCII, ignoring key releases
if (scancode & 0x80) {
return 0; // Ignore break codes (key releases)
}
return scancode_to_ascii(scancode);
}
void cin(const char* prompt, int length, char* buffer) {
int pos = 0;
// Display the prompt
cout_string(prompt, pos);
pos += strlen(prompt);
// Read input from keyboard
int index = 0;
char c;
while (index < length - 1) { // Leave space for null terminator
c = get_char();
// Check for Enter key (ASCII 13)
if (c == 'n') {
break;
}
// Check for Backspace (ASCII 8)
else if (c == 'b') {
if (index > 0) {
pos--;
// Clear the character on screen
cout(' ', pos);
index--;
continue;
}
}
// Prints the char to the screen
else {
if (c > 0) {
cout(c, pos);
pos++;
buffer[index++] = c;
}
if (c == 0) {
cout('A', pos);
pos++;
buffer[index++] = c;
}
}
}
buffer[index] = ''; // Null-terminate the string
cout('s', 0);
}
extern "C" void main() {
// Example usage:
char input[20]; // Buffer for up to 19 characters + null terminator
// Prompt the user for input
cin("Enter something: ", 20, input);
// Display the input back on the screen
cout_string("You entered: ", 0);
cout_string(input, 13);
return;
}
</code>
<code>#include <cstdint>
#include <cstdio>
extern "C" void cout(char c, int position) {
// 0xb8000 is the starting address for video memory in text mode
// Each character on the screen is represented by 2 bytes:
// - The first byte is the ASCII character code.
// - The second byte is the attribute byte (color).
// position: the index on the screen where the character will be displayed.
// 80 characters per line, so a position of 0 is the top-left corner.
char* video_memory = (char*)0xb8000;
video_memory[position * 2] = c; // Set the character
video_memory[position * 2 + 1] = 0x0F; // White text on black background
}
extern "C" void cout_string(const char* str, int start_pos) {
int pos = start_pos;
while (*str) {
cout(*str++, pos++);
}
}
extern "C" int strlen(const char* str) {
int length = 0;
while (str[length] != '') {
length++;
}
return length;
}
uint8_t inb(uint16_t port) {
uint8_t result;
__asm__ __volatile__ ("inb %1, %0" : "=a"(result) : "Nd"(port));
return result;
}
void wait_for_keypress() {
// Poll the keyboard status port (0x64) until the output buffer is full
while (!(inb(0x60) & 0x01)) {
// Busy-wait until data is available in the output buffer
}
}
// Simple scancode to ASCII conversion (only handling basic characters for example purposes)
char scancode_to_ascii(uint8_t scancode) {
static char scancode_map[256] = { //testing
46, 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,
};
if (scancode > 255) {
return 0; // Unknown scancode
}
return scancode_map[scancode];
}
char get_char() {
uint8_t scancode;
// Wait for a key press by polling the keyboard status port
wait_for_keypress();
// Now read the scancode from the keyboard data port (0x60)
scancode = inb(0x60);
// Convert the scancode to ASCII, ignoring key releases
if (scancode & 0x80) {
return 0; // Ignore break codes (key releases)
}
return scancode_to_ascii(scancode);
}
void cin(const char* prompt, int length, char* buffer) {
int pos = 0;
// Display the prompt
cout_string(prompt, pos);
pos += strlen(prompt);
// Read input from keyboard
int index = 0;
char c;
while (index < length - 1) { // Leave space for null terminator
c = get_char();
// Check for Enter key (ASCII 13)
if (c == 'n') {
break;
}
// Check for Backspace (ASCII 8)
else if (c == 'b') {
if (index > 0) {
pos--;
// Clear the character on screen
cout(' ', pos);
index--;
continue;
}
}
// Prints the char to the screen
else {
if (c > 0) {
cout(c, pos);
pos++;
buffer[index++] = c;
}
if (c == 0) {
cout('A', pos);
pos++;
buffer[index++] = c;
}
}
}
buffer[index] = ''; // Null-terminate the string
cout('s', 0);
}
extern "C" void main() {
// Example usage:
char input[20]; // Buffer for up to 19 characters + null terminator
// Prompt the user for input
cin("Enter something: ", 20, input);
// Display the input back on the screen
cout_string("You entered: ", 0);
cout_string(input, 13);
return;
}
</code>
#include <cstdint>
#include <cstdio>
extern "C" void cout(char c, int position) {
// 0xb8000 is the starting address for video memory in text mode
// Each character on the screen is represented by 2 bytes:
// - The first byte is the ASCII character code.
// - The second byte is the attribute byte (color).
// position: the index on the screen where the character will be displayed.
// 80 characters per line, so a position of 0 is the top-left corner.
char* video_memory = (char*)0xb8000;
video_memory[position * 2] = c; // Set the character
video_memory[position * 2 + 1] = 0x0F; // White text on black background
}
extern "C" void cout_string(const char* str, int start_pos) {
int pos = start_pos;
while (*str) {
cout(*str++, pos++);
}
}
extern "C" int strlen(const char* str) {
int length = 0;
while (str[length] != '') {
length++;
}
return length;
}
uint8_t inb(uint16_t port) {
uint8_t result;
__asm__ __volatile__ ("inb %1, %0" : "=a"(result) : "Nd"(port));
return result;
}
void wait_for_keypress() {
// Poll the keyboard status port (0x64) until the output buffer is full
while (!(inb(0x60) & 0x01)) {
// Busy-wait until data is available in the output buffer
}
}
// Simple scancode to ASCII conversion (only handling basic characters for example purposes)
char scancode_to_ascii(uint8_t scancode) {
static char scancode_map[256] = { //testing
46, 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,
};
if (scancode > 255) {
return 0; // Unknown scancode
}
return scancode_map[scancode];
}
char get_char() {
uint8_t scancode;
// Wait for a key press by polling the keyboard status port
wait_for_keypress();
// Now read the scancode from the keyboard data port (0x60)
scancode = inb(0x60);
// Convert the scancode to ASCII, ignoring key releases
if (scancode & 0x80) {
return 0; // Ignore break codes (key releases)
}
return scancode_to_ascii(scancode);
}
void cin(const char* prompt, int length, char* buffer) {
int pos = 0;
// Display the prompt
cout_string(prompt, pos);
pos += strlen(prompt);
// Read input from keyboard
int index = 0;
char c;
while (index < length - 1) { // Leave space for null terminator
c = get_char();
// Check for Enter key (ASCII 13)
if (c == 'n') {
break;
}
// Check for Backspace (ASCII 8)
else if (c == 'b') {
if (index > 0) {
pos--;
// Clear the character on screen
cout(' ', pos);
index--;
continue;
}
}
// Prints the char to the screen
else {
if (c > 0) {
cout(c, pos);
pos++;
buffer[index++] = c;
}
if (c == 0) {
cout('A', pos);
pos++;
buffer[index++] = c;
}
}
}
buffer[index] = ''; // Null-terminate the string
cout('s', 0);
}
extern "C" void main() {
// Example usage:
char input[20]; // Buffer for up to 19 characters + null terminator
// Prompt the user for input
cin("Enter something: ", 20, input);
// Display the input back on the screen
cout_string("You entered: ", 0);
cout_string(input, 13);
return;
}
I tried renaming 0 on the scancode map to something readable, but it didn’t show up. I’ve also tried to detect whether the output is 0 and it is.