diff options
| author | Alexey Gavrilov <rebovas@yahoo.com> | 2023-08-20 20:12:36 +0700 |
|---|---|---|
| committer | Alexey Gavrilov <rebovas@yahoo.com> | 2023-08-20 20:12:36 +0700 |
| commit | b82ccebd3a0cce28177b5fac046139e2de7737fa (patch) | |
| tree | b4e048eb7bed81a726340fb98690dce62eed7475 | |
| parent | a601dbb1dd5569243fd3006a27e914491fc3f35e (diff) | |
Getting started with print on display + some fixes
| -rw-r--r-- | arch/x86/inc/TypeConverter.cpp | 41 | ||||
| -rw-r--r-- | arch/x86/inc/TypeConverter.h | 17 | ||||
| -rw-r--r-- | arch/x86/inc/types.h | 5 | ||||
| -rw-r--r-- | arch/x86/io/GlobalObj.cpp | 3 | ||||
| -rw-r--r-- | arch/x86/io/GlobalObj.h | 8 | ||||
| -rw-r--r-- | arch/x86/io/VGADisplay.cpp | 119 | ||||
| -rw-r--r-- | arch/x86/io/VGADisplay.h | 36 | ||||
| -rw-r--r-- | arch/x86/io/VGADisplayInfo.h | 27 |
8 files changed, 256 insertions, 0 deletions
diff --git a/arch/x86/inc/TypeConverter.cpp b/arch/x86/inc/TypeConverter.cpp new file mode 100644 index 0000000..23119a2 --- /dev/null +++ b/arch/x86/inc/TypeConverter.cpp @@ -0,0 +1,41 @@ +#include "TypeConverter.h" + +void IntConverter::clearBuff() +{ + for(int i = 0; i < 21; i++) buffer[i] = '\0'; +}; + +char *IntConverter::intToChar(int n) +{ + clearBuff(); + + int number = n >> 31; // n < 0 - n = -1, else n = 0 + number = (n ^ number) - number; // Clear sign bit + + bool isNegative = n < 0; + unsigned int indx = 19; + + do { + buffer[indx--] = 48 + (number % 10); + number /= 10; + }while(number != 0); + + if(isNegative) buffer[indx--] = '-'; + + return &buffer[++indx]; +}; + +char *IntConverter::uintToChar(unsigned int n) +{ + clearBuff(); + + unsigned int number = n; + unsigned int indx = 19; + + do { + buffer[indx--] = 48 + (number % 10); + number /= 10; + }while(number != 0); + + return &buffer[++indx]; +}; diff --git a/arch/x86/inc/TypeConverter.h b/arch/x86/inc/TypeConverter.h new file mode 100644 index 0000000..93d2c8c --- /dev/null +++ b/arch/x86/inc/TypeConverter.h @@ -0,0 +1,17 @@ +#ifndef TYPECONVERTER_H +#define TYPECONVERTER_H + +#include "../io/VGADisplay.h" + +class IntConverter +{ + private: + char buffer[21]; + void clearBuff(); + + public: + char *intToChar(int n); + char *uintToChar(unsigned int n); +}; + +#endif diff --git a/arch/x86/inc/types.h b/arch/x86/inc/types.h new file mode 100644 index 0000000..d688a35 --- /dev/null +++ b/arch/x86/inc/types.h @@ -0,0 +1,5 @@ + +typedef unsigned char uint8; +typedef unsigned int uint16; +typedef unsigned long uint32; +typedef unsigned long long uint64; diff --git a/arch/x86/io/GlobalObj.cpp b/arch/x86/io/GlobalObj.cpp new file mode 100644 index 0000000..a1c4f03 --- /dev/null +++ b/arch/x86/io/GlobalObj.cpp @@ -0,0 +1,3 @@ +#include "VGADisplay.h" + +VGADisplay display; diff --git a/arch/x86/io/GlobalObj.h b/arch/x86/io/GlobalObj.h new file mode 100644 index 0000000..72c6b06 --- /dev/null +++ b/arch/x86/io/GlobalObj.h @@ -0,0 +1,8 @@ +#ifndef GLOBALOBJ_H +#define GLOBALOBJ_H + +#include "VGADisplay.h" + +extern VGADisplay display; + +#endif diff --git a/arch/x86/io/VGADisplay.cpp b/arch/x86/io/VGADisplay.cpp new file mode 100644 index 0000000..b74858b --- /dev/null +++ b/arch/x86/io/VGADisplay.cpp @@ -0,0 +1,119 @@ +#include "VGADisplay.h" + +VGADisplay::VGADisplay() +{ + isBright = isBlink = shift = 0; + xCurr = yCurr = 0; + color = (char)VGADisplayInfo::colorAttr::gray; + videoMem = (dchar *)VGADisplayInfo::videoMemAddr; +}; + +VGADisplay::VGADisplay(uint16 x, uint16 y) +{ + isBright = isBlink = shift = 0; + xCurr = yCurr = 0; + color = (char)VGADisplayInfo::colorAttr::gray; + videoMem = (dchar *)VGADisplayInfo::videoMemAddr; +}; + +VGADisplay& VGADisplay::operator<<(const char &c) +{ + printChar(c); + + return *this; +}; + +VGADisplay& VGADisplay::operator++(int) +{ + if(++xCurr == VGADisplayInfo::width) + { + yCurr++; + xCurr = 0; + } + if(yCurr == VGADisplayInfo::height) + { + if(shift) + { + this->doShift(); + xCurr = 0; + yCurr--; + } + else xCurr = 0, yCurr = 0; + } + videoMem = (dchar *)(VGADisplayInfo::videoMemAddr) + + yCurr * VGADisplayInfo::width + xCurr; + + return *this; +}; + +VGADisplay& VGADisplay::operator<<(const char *c) +{ + for(int i = 0; c[i] != '\0'; i++) + { + (*this) << c[i]; + } + + return *this; +}; + +void VGADisplay::doShift() +{ + for(uint16 i = 0; i < VGADisplayInfo::height; i++) + { + for(uint16 j = 0; j < VGADisplayInfo::width; j++) + { + *((dchar *)(VGADisplayInfo::videoMemAddr) + (i - 1) * VGADisplayInfo::width + j) = *((dchar *)(VGADisplayInfo::videoMemAddr) + i * VGADisplayInfo::width + j); + *((dchar *)(VGADisplayInfo::videoMemAddr) + i * VGADisplayInfo::width + j) = (*((dchar *)(VGADisplayInfo::videoMemAddr) + i * VGADisplayInfo::width + j) & 0x00) | ' '; + } + } +}; + +void VGADisplay::setShift(bool status) +{ + shift = status; +}; + +void VGADisplay::setColor(char colorAttr) +{ + color = colorAttr; +}; + +void VGADisplay::setBlink(bool blinkAttr) +{ + isBlink = blinkAttr; +}; + +void VGADisplay::setBright(bool brigthAttr) +{ + isBright = brigthAttr; +}; + +void VGADisplay::goNewLine() +{ + uint16 yCurrOld = yCurr; + + while(yCurrOld == yCurr) (*this)++; +}; + +void VGADisplay::goHorizontalTab() +{ + while(xCurr % 8 != 0) (*this)++; +}; + +bool VGADisplay::printChar(const char &c) +{ + switch(c) + { + case '\0': return true; + case '\n': this->goNewLine(); return true; + case '\t': this->goHorizontalTab(); return true; + default: + { + char attr = isBlink << 8 | isBright << 4 + | color; + *videoMem = c | attr << 8; + (*this)++; + return false; + } + } +}; diff --git a/arch/x86/io/VGADisplay.h b/arch/x86/io/VGADisplay.h new file mode 100644 index 0000000..67f28b3 --- /dev/null +++ b/arch/x86/io/VGADisplay.h @@ -0,0 +1,36 @@ +#ifndef VGAGRAPHICS_H +#define VGAGRAPHICS_H + +#include "VGADisplayInfo.h" + +typedef unsigned short dchar; + +class VGADisplay +{ + private: + uint16 xCurr; + uint16 yCurr; + dchar *videoMem; + bool shift; + char color; + bool isBlink; + bool isBright; + + void doShift(); + // 0 if passed char isn't escape sequence, else 1 + bool printChar(const char &c); + void goNewLine(); + void goHorizontalTab(); + + public: + VGADisplay(); + VGADisplay(uint16 x, uint16 y); + VGADisplay& operator<<(const char &c); + VGADisplay& operator<<(const char *c); + VGADisplay& operator++(int); + void setShift(bool status); + void setColor(char colorAttr); + void setBlink(bool blinkAttr); + void setBright(bool brigthAttr); +}; +#endif diff --git a/arch/x86/io/VGADisplayInfo.h b/arch/x86/io/VGADisplayInfo.h new file mode 100644 index 0000000..33235cc --- /dev/null +++ b/arch/x86/io/VGADisplayInfo.h @@ -0,0 +1,27 @@ +#ifndef VGAINFOCLASS_H +#define VGAINFOCLASS_H + +#include "../inc/types.h" + +#define TOBG(attr) attr << 4 + +class VGADisplayInfo +{ + public: + const static uint32 videoMemAddr = 0xB8000; + const static uint16 width = 80; + const static uint16 height = 25; + const static uint16 tabSize = 8; + enum class colorAttr { + black = 0, + blue = 1, + green = 2, + cyan = 3, + red = 4, + magenta = 5, + brown = 6, + gray = 7 + }; +}; + +#endif |
