Add scrolling function for int

This commit is contained in:
Mathieu Maret 2018-07-18 15:35:49 +02:00
parent 0e37c3d426
commit ee414bc4aa
5 changed files with 42 additions and 22 deletions

View File

@ -2,11 +2,11 @@
#include "vga.h"
// Need GCC > 6
__attribute__ ((interrupt))
void print_handler(struct interrupt_frame *frame, ulong error_code){
__attribute__((interrupt)) void print_handler(struct interrupt_frame *frame, ulong error_code)
{
printStringDetails("EXCEPTION", RED, BLACK, 0, 20);
printInt(error_code, RED, BLACK, 11, 20);
(void) frame;
(void) error_code;
printStringDetails("EXCEPTION", RED, BLACK, 0, VGA_HEIGHT - 1);
printIntDetails(error_code, RED, BLACK, 11, VGA_HEIGHT - 1);
(void)frame;
(void)error_code;
}

View File

@ -1,8 +1,8 @@
#include "interrupt.h"
#include "vga.h"
#include "io.h"
#include "pic.h"
#include "irq.h"
#include "pic.h"
#include "vga.h"
// Need GCC > 6
__attribute__((interrupt)) void keyboard_handler(struct interrupt_frame *frame)
@ -11,9 +11,8 @@ __attribute__((interrupt)) void keyboard_handler(struct interrupt_frame *frame)
char c = 0;
if (inb(0x60) != c) {
c = inb(0x60);
if (c > 0){
printChar(c);
printChar('\n');
if (c > 0) {
printInt(c);
}
}
(void)frame;
@ -23,6 +22,6 @@ __attribute__((interrupt)) void timer_handler(struct interrupt_frame *frame)
{
static int timeCnt = 0;
EOIIrq(IRQ_TIMER);
printInt(timeCnt++, RED, BLACK, 20, VGA_HEIGHT -1 );
printIntDetails(timeCnt++, RED, BLACK, 20, VGA_HEIGHT - 1);
(void)frame;
}

2
main.c
View File

@ -31,7 +31,7 @@ void kmain()
asm volatile("sti\n");
int count = 0;
while (1) {
printInt(count++, GREEN, BLACK, 0, VGA_HEIGHT - 1);
printIntDetails(count++, GREEN, BLACK, 0, VGA_HEIGHT - 1);
}
printString("exiting\n");
}

36
vga.c
View File

@ -26,7 +26,26 @@ void clearScreen(uint bgColor)
}
}
void printInt(int integer, uint color, uint bgColor, int startX, int startY)
void printInt(int integer)
{
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 i = 0, k = 0;
if (integer < 0) {
printChar('-');
}
while (integer != 0) {
int digit = integer % 10;
num[i++] = (digit > 0) ? digit : -digit;
integer = integer / 10;
}
for (k = i - 1; k >= 0; k--) {
printChar(num[k] + '0');
}
}
void printIntDetails(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) =
@ -48,8 +67,8 @@ void printInt(int integer, uint color, uint bgColor, int startX, int startY)
void printCharDetails(const char str, uint color, uint bgColor, int startX, int startY)
{
volatile short *vga = (short *)VGA_ADDR;
long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8;
volatile short *vga = (short *)VGA_ADDR;
long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8;
vga[VGA_WIDTH * startY + startX] = colorAttr | str;
}
@ -57,20 +76,21 @@ void vgaScrollUp(void)
{
long int colorAttr = vgaBgColor << 12;
volatile short *vga = (short *)VGA_ADDR;
for (int i = 1; i < VGA_HEIGHT - 2; i++) { // last line is status line. Do not scroll it
for (int i = 1; i < VGA_HEIGHT - 2;
i++) { // last line is status line. Do not scroll it
memcpy((void *)&vga[VGA_WIDTH * (i - 1)], (void *)&vga[VGA_WIDTH * i],
VGA_WIDTH * sizeof(short));
}
for(int i = 0; i < VGA_WIDTH; i++){
for (int i = 0; i < VGA_WIDTH; i++) {
vga[(VGA_HEIGHT - 1) * VGA_WIDTH + i] = colorAttr;
}
}
void printString(const char *str) {
void printString(const char *str)
{
while (*str) {
printChar(*(str++));
}
}
void printChar(const char str)
@ -87,7 +107,7 @@ void printChar(const char str)
vgaScrollUp();
line--;
}
if(str == '\n')
if (str == '\n')
return;
printCharDetails(str, vgaColor, vgaBgColor, col++, line);
}

3
vga.h
View File

@ -18,7 +18,8 @@
int initVGA(uint bgColor, uint color);
void clearScreen(uint bgColor);
void printInt(int integer, uint color, uint bgColor, int startX, int startY);
void printInt(int integer);
void printIntDetails(int integer, uint color, uint bgColor, int startX, int startY);
void printCharDetails(char str, uint color, uint bgColor, int startX, int startY);
void printStringDetails(const char *str, uint color, uint bgColor, int startX, int startY);
void printString(const char *str);