I am trying to create a getchar function:
Code: Select all
int getchar()
{
for (;;) {
if (kb_interrupt) { // check if a keyboard interrupt was received, if yes,break infinite loop
break;
}
}
int c = ((uint8_t*)stdin)[in_size - 1]; // read last char from 'stdin', is my stdin version, i know it is different from its Unix definition
putchar(c); // put the char
kb_interrupt = 0; // set the keyboard interrupt check variable to 0
return c;
}
Code: Select all
void keyboard_interrupt_handler(__attribute__ ((unused)) registers_t regs)
{
uint8_t scancode = inb(0x60);
int special = 0;
if (scancode & 0x80) {
scancode &= 0x7F;
if (scancode == KRLEFT_SHIFT || scancode == KRRIGHT_SHIFT) {
shift = 0;
special = 1;
}
} else {
if (scancode == KRLEFT_SHIFT || scancode == KRRIGHT_SHIFT) {
shift = 1;
special = 1;
}
if (shift) {
kb_buffer[last++] = ascii_S[scancode];
} else {
kb_buffer[last++] = ascii_s[scancode];
}
if (special != 1) {
cli();
read_kb_buff(kb_buffer, last);
sti();
}
if (last == KEYBOARD_BUFFER_SIZE) {
last = 0;
}
}
}
void read_kb_buff(uint8_t *buf, uint16_t size)
{
((uint8_t*) stdin)[in_size] = buf[size - 1];
in_size++;
kb_interrupt = 1;
}
New getchar code:
Code: Select all
int getchar()
{
for (;;) {
putchar('\g');
if (kb_interrupt) {
break;
}
}
int c = ((uint8_t*)stdin)[in_size - 1];
putchar(c);
kb_interrupt = 0;
return c;
}
So, why is this happening, why I need to print something in the getchar loop to make it works?
PS Kernel code at: https://github.com/JustBeYou/MyOS/tree/devel/src
Thanks!