Add way to print int

This commit is contained in:
Mathieu Maret 2018-07-12 14:07:24 +02:00
parent 1dd69e16a2
commit 3ce167123a
2 changed files with 31 additions and 10 deletions

27
vga.c
View File

@ -1,6 +1,6 @@
#include "vga.h" #include "vga.h"
void clearScreen(int bgColor) void clearScreen(uint bgColor)
{ {
volatile short *vga = (short *)VGA_ADDR; volatile short *vga = (short *)VGA_ADDR;
long int colorAttr = bgColor << 12; long int colorAttr = bgColor << 12;
@ -9,15 +9,34 @@ void clearScreen(int bgColor)
} }
} }
void printChar(const char str, int color, int bgColor, int startX, int startY) void printInt(int integer, uint color, uint bgColor, int startX, int startY)
{
char num[sizeof(int) *
3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3) =
// 8^(sizeof(int)*8/3) ~ 10^(sizeof(int)*8/3)
int x = startX;
int i = 0, k = 0;
if (integer < 0) {
printChar('-', color, bgColor, x++, startY);
integer = -integer;
}
while (integer != 0) {
num[i++] = integer % 10;
integer = integer / 10;
}
for (k = i - 1; k >= 0; k--) {
printChar(num[k] + '0', color, bgColor, x++, startY);
}
}
void printChar(const char str, uint color, uint bgColor, int startX, int startY)
{ {
volatile short *vga = (short *)VGA_ADDR; volatile short *vga = (short *)VGA_ADDR;
long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8; long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8;
vga[80 * startY + startX] = colorAttr | str; vga[80 * startY + startX] = colorAttr | str;
} }
void printString(const char *str, int color, int bgColor, int startX, void printString(const char *str, uint color, uint bgColor, int startX, int startY)
int startY)
{ {
volatile short *vga = (short *)VGA_ADDR; volatile short *vga = (short *)VGA_ADDR;
int i = 0; int i = 0;

8
vga.h
View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "types.h"
#define BLACK 0x00 #define BLACK 0x00
#define BLUE 0x01 #define BLUE 0x01
@ -14,6 +15,7 @@
#define VGA_WIDTH 80 #define VGA_WIDTH 80
#define VGA_HEIGHT 15 #define VGA_HEIGHT 15
void clearScreen(int bgColor); void clearScreen(uint bgColor);
void printChar(const char str, int color, int bgColor, int startX, int startY); void printInt(int integer, uint color, uint bgColor, int startX, int startY);
void printString(const char *str, int color, int bgColor, int startX, int startY); void printChar(char str, uint color, uint bgColor, int startX, int startY);
void printString(const char *str, uint color, uint bgColor, int startX, int startY);