translate keyboard scancode

This commit is contained in:
2026-01-29 12:20:41 +11:00
parent 341018d6d1
commit 7ee5ff5bd4
31 changed files with 142 additions and 11 deletions

View File

@@ -7,7 +7,7 @@ CC = gcc
ASMC = nasm
LD = ld
CFLAGS = -ffreestanding -fshort-wchar
CFLAGS = -ffreestanding -fshort-wchar -mno-red-zone
ASMFLAGS =
LDFLAGS = -T $(LDS) -static -Bsymbolic -nostdlib

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -25,6 +25,15 @@ void BasicRenderer::PutChar(char chr, unsigned int xOff, unsigned int yOff) {
}
}
void BasicRenderer::PutChar(char chr) {
PutChar(chr, cursorPosition.x, cursorPosition.y);
cursorPosition.x += 8;
if (cursorPosition.x + 8 > targetFramebuffer->Width) {
cursorPosition.x = 0;
cursorPosition.y += 16;
}
}
void BasicRenderer::Print(const char* str) {
unsigned int x = 0;
char* chr = (char*)str;
@@ -42,7 +51,7 @@ void BasicRenderer::Print(const char* str) {
}
}
void BasicRenderer::Clear(uint32_t colour) {
void BasicRenderer::Clear() {
uint64_t fbBase = (uint64_t)targetFramebuffer->BaseAddress;
uint64_t bytesPerScanline = targetFramebuffer->PixelsPerScanline * 4;
uint64_t fbHeight = targetFramebuffer->Height;
@@ -51,11 +60,39 @@ void BasicRenderer::Clear(uint32_t colour) {
for (int verticalScanline = 0; verticalScanline < fbHeight; verticalScanline ++){
uint64_t pixPtrBase = fbBase + (bytesPerScanline * verticalScanline);
for (uint32_t* pixPtr = (uint32_t*)pixPtrBase; pixPtr < (uint32_t*)(pixPtrBase + bytesPerScanline); pixPtr ++){
*pixPtr = colour;
*pixPtr = ClearColour;
}
}
}
void BasicRenderer::ClearChar(){
if (cursorPosition.x == 0){
cursorPosition.x = targetFramebuffer->Width;
cursorPosition.y -= 16;
if (cursorPosition.y < 0) cursorPosition.y = 0;
}
unsigned int xOff = cursorPosition.x;
unsigned int yOff = cursorPosition.y;
unsigned int* pixPtr = (unsigned int*)targetFramebuffer->BaseAddress;
for (unsigned long y = yOff; y < yOff + 16; y++){
for (unsigned long x = xOff - 8; x < xOff; x++){
*(unsigned int*)(pixPtr + x + (y * targetFramebuffer->PixelsPerScanline)) = ClearColour;
}
}
cursorPosition.x -= 8;
if (cursorPosition.x < 0){
cursorPosition.x = targetFramebuffer->Width;
cursorPosition.y -= 16;
if (cursorPosition.y < 0) cursorPosition.y = 0;
}
}
void BasicRenderer::Next() {
cursorPosition.x = 0;
cursorPosition.y += 16;

View File

@@ -11,9 +11,12 @@ class BasicRenderer {
Framebuffer* targetFramebuffer;
PSF1_FONT* PSF1_Font;
unsigned int Colour;
unsigned int ClearColour;
void Print(const char* str);
void PutChar(char chr, unsigned int xOff, unsigned int yOff);
void Clear(uint32_t colour);
void PutChar(char chr);
void ClearChar();
void Clear();
void Next();
};

View File

@@ -1,6 +1,7 @@
#include "interrupts.h"
#include "../panic.h"
#include "../IO.h"
#include "../userinput/keyboard.h"
__attribute__((interrupt)) void PageFault_Handler(struct interrupt_frame* frame) {
Panic("Page Fault");
@@ -18,10 +19,10 @@ __attribute__((interrupt)) void GPFault_Handler(struct interrupt_frame* frame) {
}
__attribute__((interrupt)) void KeyboardInterrupt_Handler(struct interrupt_frame* frame) {
GlobalRenderer->Print("A");
uint8_t scancode = inb(0x60);
HandleKeyboard(scancode);
PIC_EndMaster();
}

View File

@@ -4,7 +4,8 @@ extern "C" void _start(BootInfo* bootInfo) {
KernelInfo kernelInfo = InitializeKernel(bootInfo);
PageTableManager* pageTableManager = kernelInfo.pageTableManager;
GlobalRenderer->Clear(0xFF191919);
GlobalRenderer->ClearColour = 0xFF191919;
GlobalRenderer->Clear();
GlobalRenderer->Print("Kernel initialized successfully!");
GlobalRenderer->Next();

View File

@@ -1,6 +1,6 @@
#pragma once
struct Point {
unsigned int x;
unsigned int y;
long x;
long y;
};

View File

@@ -33,7 +33,7 @@ void PageFrameAllocator::ReadEFIMemoryMap(EFI_MEMORY_DESCRIPTOR* mMap, size_t mM
InitBitmap(bitmapSize, largestFreeMemSeg);
// lock pages of bitmap
LockPages(&PageBitmap, PageBitmap.Size / 4096 + 1);
LockPages(PageBitmap.Buffer, PageBitmap.Size / 4096 + 1);
//reserve pages of unsable/reserverd memory
for (int i = 0; i < mMapEntries; i++) {

View File

@@ -2,7 +2,8 @@
#include "BasicRenderer.h"
void Panic(const char* panicMessage) {
GlobalRenderer->Clear(0xFF000000);
GlobalRenderer->ClearColour = 0xFF000000;
GlobalRenderer->Clear();
GlobalRenderer->Colour = 0xFFFF0000;
GlobalRenderer->cursorPosition = {0,0};
GlobalRenderer->Print("PANIC!!! ");

View File

@@ -0,0 +1,31 @@
#include "kbScancodeTranslation.h"
namespace QWERTYKeyboard {
const char ASCIITable[] = {
0 , 0 , '1', '2',
'3', '4', '5', '6',
'7', '8', '9', '0',
'-', '=', 0 , 0 ,
'q', 'w', 'e', 'r',
't', 'y', 'u', 'i',
'o', 'p', '[', ']',
0 , 0 , 'a', 's',
'd', 'f', 'g', 'h',
'j', 'k', 'l', ';',
'\'','`', 0 , '\\',
'z', 'x', 'c', 'v',
'b', 'n', 'm', ',',
'.', '/', 0 , '*',
0 , ' '
};
char Translate(uint8_t scancode, bool uppercase) {
if (scancode > 58) return 0; // longer than the length of our ascii array
if (uppercase) {
return ASCIITable[scancode] - 32;
}
return ASCIITable[scancode];
}
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
namespace QWERTYKeyboard {
#define LeftShift 0x2A
#define RightShift 0x36
#define Enter 0x1C
#define Backspace 0x0E
#define Spacebar 0x39
extern const char ASCIITable[];
char Translate(uint8_t scancode, bool uppercase);
}

View File

@@ -0,0 +1,36 @@
#include "keyboard.h"
bool isLeftShiftPressed = false;
bool isRightShiftPressed = false;
void HandleKeyboard(uint8_t scancode) {
switch (scancode) {
case LeftShift:
isLeftShiftPressed = true;
return;
case LeftShift + 0x80:
isLeftShiftPressed = false;
return;
case RightShift:
isRightShiftPressed = true;
return;
case RightShift + 0x80:
isRightShiftPressed = false;
return;
case Enter:
GlobalRenderer->Next();
return;
case Spacebar:
GlobalRenderer->PutChar(' ');
return;
case Backspace:
GlobalRenderer->ClearChar();
break;
}
char ascii = QWERTYKeyboard::Translate(scancode, isLeftShiftPressed || isRightShiftPressed);
if (ascii != 0) {
GlobalRenderer->PutChar(ascii);
}
}

View File

@@ -0,0 +1,6 @@
#pragma once
#include <stdint.h>
#include "kbScancodeTranslation.h"
#include "../BasicRenderer.h"
void HandleKeyboard(uint8_t scancode);