↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

dwnd.c
/*
EPOS
http://www.atanaslaskov.com/epos/

FILE:        dwnd.c
DESCRIPTION: Kernel debug window  (for use in kernel debugger)
 
BSD LICENSE
Copyright (c) 2006, Atanas Laskov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "dwnd.h"
#include "stdarg.h"

// Globals
//
#define MAX_PAGE_CNT 8
struct DebugPage{
    char nChar;
    char data[DEBUG_W][DEBUG_H];
    char x, y;
};

static struct DebugPage g_arPages[MAX_PAGE_CNT];
unsigned long g_nCnt=0;
unsigned long g_nCr=0;
char          b_bUpdate=0;

// Initalize debug window
//
void InitializeDebug()
{
    DEBUGREGISTER('d');
    DEBUGSET('d');
    DEBUGS('d', "Kernel debug enabled\n");
    DEBUGF('d', "Size: %dx%d\n", DEBUG_W, DEBUG_H);
    DEBUGF('d', "Pos:  %dx%d\n", DEBUG_X, DEBUG_Y);
    DEBUGF('d', "Max %d debug consoles\n", MAX_PAGE_CNT);
    //DRAWDEBUG;
}

// Debug window routines
//
void RegisterDebugChar(char nChar) {
    unsigned long x,y;
   
    g_arPages[g_nCnt].nChar = nChar;
   
    for(x=0;x<DEBUG_W;x++)
        for(y=0;y<DEBUG_H;y++)
            g_arPages[g_nCnt].data[x][y] = ' ';
   
    g_arPages[g_nCnt].x = g_arPages[g_nCnt].y = 0;
    g_nCnt++;
    b_bUpdate=1;
}

void SetDebugChar(char nChar) {
    unsigned long i;
   
    for(i=0;i<g_nCnt;i++)
        if(g_arPages[i].nChar==nChar) {

        g_nCr=i;
        b_bUpdate=1;
        return;
    }
}

void DrawDebug() {
    unsigned long x,y;
    unsigned long pos;

    // Contents
    //
    for(y=0;y<DEBUG_H;y++)
    {
        for(x=0;x<DEBUG_W;x++)
        {  
            pos = 0xB8000 + ((y+DEBUG_Y+1)*80+x+DEBUG_X+1)*2;
            gdt_linear_setb(pos, g_arPages[g_nCr].data[x][y]);
            gdt_linear_setb(pos+1, 11);
        }
    }
    // Borders
    //
    for(y=0; y<=DEBUG_H+1; y++)
    {
        gdt_linear_setb(0xB8000 + ((y+DEBUG_Y)*80+DEBUG_X)*2, '\xB3');
        gdt_linear_setb(0xB8000 + ((y+DEBUG_Y)*80+DEBUG_X)*2+1, 12);

        gdt_linear_setb(0xB8000 + ((y+DEBUG_Y)*80+DEBUG_X+DEBUG_W+1)*2, '\xB3');
        gdt_linear_setb(0xB8000 + ((y+DEBUG_Y)*80+DEBUG_X+DEBUG_W+1)*2+1, 12);
    }
    for(x=0; x<=DEBUG_W+1; x++)
    {
        gdt_linear_setb(0xB8000 + ((DEBUG_Y)*80+DEBUG_X+x)*2, '\xC4');
        gdt_linear_setb(0xB8000 + ((DEBUG_Y)*80+DEBUG_X+x)*2+1, 12);

        gdt_linear_setb(0xB8000 + ((DEBUG_Y+DEBUG_H+1)*80+DEBUG_X+x)*2, '\xC4');
        gdt_linear_setb(0xB8000 + ((DEBUG_Y+DEBUG_H+1)*80+DEBUG_X+x)*2+1, 12);
    }
    gdt_linear_setb(0xB8000 + ((0+DEBUG_Y)*80+DEBUG_X)*2, '\xDA');
    gdt_linear_setb(0xB8000 + ((0+DEBUG_Y)*80+DEBUG_X)*2+1, 12);

    gdt_linear_setb(0xB8000 + ((0+DEBUG_Y)*80+DEBUG_X+DEBUG_W+1)*2, '\xBF');
    gdt_linear_setb(0xB8000 + ((0+DEBUG_Y)*80+DEBUG_X+DEBUG_W+1)*2+1, 12);

    gdt_linear_setb(0xB8000 + ((DEBUG_H+1+DEBUG_Y)*80+DEBUG_X)*2, '\xC0');
    gdt_linear_setb(0xB8000 + ((DEBUG_H+1+DEBUG_Y)*80+DEBUG_X)*2+1, 12);

    gdt_linear_setb(0xB8000 + ((DEBUG_H+1+DEBUG_Y)*80+DEBUG_X+DEBUG_W+1)*2, '\xD9');
    gdt_linear_setb(0xB8000 + ((DEBUG_H+1+DEBUG_Y)*80+DEBUG_X+DEBUG_W+1)*2+1, 12);

    // Char
    gdt_linear_setb(0xB8000 + ((DEBUG_Y)*80+DEBUG_X+1)*2,   '[');
    gdt_linear_setb(0xB8000 + ((DEBUG_Y)*80+DEBUG_X+1)*2+1, 12);

    gdt_linear_setb(0xB8000 + ((DEBUG_Y)*80+DEBUG_X+2)*2,   g_arPages[g_nCr].nChar);
    gdt_linear_setb(0xB8000 + ((DEBUG_Y)*80+DEBUG_X+2)*2+1, 12);

    gdt_linear_setb(0xB8000 + ((DEBUG_Y)*80+DEBUG_X+3)*2,   ']');
    gdt_linear_setb(0xB8000 + ((DEBUG_Y)*80+DEBUG_X+3)*2+1, 12);

    // Misc
    gdt_linear_setb(0xB8000 + ((0)*80+76)*2,   '[');
    gdt_linear_setb(0xB8000 + ((0)*80+76)*2+1, 12);

    gdt_linear_setb(0xB8000 + ((0)*80+76+2)*2,   ']');
    gdt_linear_setb(0xB8000 + ((0)*80+76+2)*2+1, 12);


    b_bUpdate=0;
}

void Debugc(char nChar, char c) {
    unsigned long i, x, y, pos;
   
    for(i=0;i<g_nCnt;i++)
        if(g_arPages[i].nChar==nChar) {
       
        b_bUpdate=1;
        break;
    }
   
    x = g_arPages[i].x;
    y = g_arPages[i].y;

    if(x>=DEBUG_W){ x=0; y++; }
    if(y>=DEBUG_H)return;
   
    if(c=='\n') { x=0; y++; }
    else {
   
        g_arPages[i].data[x++][y]=c;
       
        if(g_nCr==i){
            pos = 0xB8000 + ((y+DEBUG_Y)*80+x+DEBUG_X+1)*2;
            gdt_linear_setb(pos, g_arPages[g_nCr].data[x][y]);
            gdt_linear_setb(pos+1, 11);
        }
    }

    g_arPages[i].x = x;
    g_arPages[i].y = y;
}

void Debugs(char nChar, char*s) {
    unsigned long i=0;
    while(s[i]!='\0') Debugc(nChar, s[i++]);
    b_bUpdate=1;
}

void Debugd(char nChar, unsigned long num) {
    if(num>9) Debugd(nChar, num/10);
    Debugc(nChar, '0'+ (num%10) );
    b_bUpdate=1;
}

void Debugh(char nChar, unsigned long num) {
    if(num>15/*17*/) Debugh(nChar, num/16);
    if((num%16)<=9) Debugc(nChar, '0'+ (num%16) );
    else            Debugc(nChar, 'A'+ (num%16)-10 );
    b_bUpdate=1;
}

void Debugf(char nChar, char*format, ...) {
    unsigned long i=0;
    va_list marker;
   
    va_start(marker, format);
    while(1)switch(format[i++])
    {
        case '\0':
            va_end(marker);
            return;
        case '%':
            switch(format[i++])
            {
            case 's':
                Debugs(nChar,va_arg(marker, char*));
                break;
            case 'd':
                Debugd(nChar,va_arg(marker, unsigned long));
                break;
            case 'h':
                Debugh(nChar,va_arg(marker, unsigned long));
                break;
            }
            break;
        default:
            Debugc(nChar,format[i-1]);
            break;
    }
}

void DebugTop(char nChar) {
    unsigned long i;
   
    for(i=0;i<g_nCnt;i++)
     if(g_arPages[i].nChar==nChar) {
     
        g_arPages[i].x = 0;
        g_arPages[i].y = 0;
        return;
    }
}

char IsPartOfDebugWindow(unsigned long address) {
    return 0;
}