Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MTGos - Ein einfaches Hobby-OS in C geschrieben
- // Copyright (C) 2013 Morten Delenk
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- #ifndef __MTGOS_PRINT_H
- #define __MTGOS_PRINT_H
- #define SPALTEN 80
- #define REIHEN 24 // 25, Bei Null Beginnend
- #include <stdarg.h>
- unsigned short * const pScreen = (unsigned short *) 0xB8000; // Einen Pointer den wir als Array Missbrauchen
- // Juhuu! :-D (Geht wirklich :D)
- unsigned gPos = 0; // Ist eigentlich egal welchen Datentyp der Compiler nimmt;
- // Hauptsache es ist vorzeichenlos
- unsigned short gAttr=0x1F00; // Bluescreenalarm^^ Darüber wird noch abgestimmt
- // Aber Weiß auf Blau sieht super aus :-D
- // Ich könnte mir aber auch 0xF000 denken. (Schwarz auf Weiß) :D
- unsigned getX()
- {
- return (gPos % SPALTEN);
- }
- unsigned getY()
- {
- return ((gPos-getX()) / SPALTEN);
- }
- void gotoxy(unsigned x, unsigned y)
- {
- gPos = x+(y*REIHEN); // Cusorposition := Reihe + (Zeile*REIHEN)
- }
- void scroll() { // Mit dieser Funktion Scrollen wir ne Reihe nach oben :D
- unsigned quellePos = SPALTEN;
- unsigned zielPos = 0;
- for(zielPos=0; quellePos<SPALTEN*REIHEN+SPALTEN; quellePos++) {
- pScreen[zielPos++] = pScreen[quellePos]; // MISSBRAUCHT!!!
- }
- for (zielPos=SPALTEN*REIHEN; zielPos<=SPALTEN*REIHEN+SPALTEN; zielPos++) {
- pScreen[zielPos] = gAttr; // Und Bit Blau Machen!
- }
- gPos-=SPALTEN;
- }
- void scrollMultiLine(unsigned lines) { // Gibt bessere Methoden, da eine VGA-Karte
- //Lahm ist dauert das ein Wenig^^
- unsigned c=0;
- for(c=0; c<lines; c++) {
- scroll();
- }
- }
- void MonitorInit() // Färbe den Kompletten Bildschirm ein. Nützlich, wenn man clrscr() nicht verwenden will!
- {
- unsigned i;
- for(i=0; i<SPALTEN*REIHEN+SPALTEN; i++) {
- pScreen[i]&= 0x00FF;
- pScreen[i]|= gAttr;
- }
- }
- void setColor(unsigned char foreground, unsigned char background, int recolor) {
- gAttr=((background << 4) | foreground) << 8;
- if(recolor) {
- MonitorInit();
- }
- }
- void clrscr()
- {
- scrollMultiLine(REIHEN+1);
- unsigned short tmp=gAttr;
- gAttr=0x1F00;
- MonitorInit();
- gAttr=tmp;
- gPos=0;
- }
- void putchar(char ch) {
- switch(ch) {
- case '\n':
- if(getY()==REIHEN) {
- scroll();
- }
- gotoxy(0,getY()+1);
- break;
- case '\r':
- gotoxy(0,getY());
- break;
- default:
- if(gPos==SPALTEN*REIHEN+SPALTEN) {
- scroll();
- }
- pScreen[gPos++] = (unsigned short) ch | gAttr;
- break;
- }
- }
- void puts(const char *s) {
- while(*s)
- putchar(*(s++));
- }
- void puti(unsigned long x, int base)
- {
- char buf[65]; // 65-Bittiger Buffer
- const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- char* p; // Buffer
- if (base > 36) {
- return; // Tut mir leid, Wir können nur Alphanumerische Zeichen drucken!
- }
- if (base == 16) {
- puts("0x"); // Hexadezimal? Du bekommst ein extra 0x!
- }
- p = buf+64;
- *p = '\0';
- do {
- *--p = digits[x%base]; // Von Hinten Füllen :D
- x /= base;
- } while (x);
- puts(p); // Jetzt ausgeben :D
- if (base == 2) {
- putchar('b'); // Binär? Dann bekommst du noch ein Nachzeichen :D (b)
- }
- }
- int printf(const char* fmt, ...)
- {
- va_list ap;
- const char* s;
- unsigned long n;
- unsigned int base=10;
- va_start(ap, fmt); // Liste Starten
- unsigned int kprintf_res=0;
- while (* fmt) {
- if (*fmt == '%') { //Ist das ein Steuerzeichen?
- fmt++;
- switch (*fmt) {
- case 's':
- s = va_arg(ap, char*); // Drucke einen String
- kputs(s);
- break;
- case 'd':
- case 'u':
- n=va_arg(ap, unsigned long int); // Und einen Integer als Dezimalzahl
- puti(n,10);
- break;
- case 'x':
- case 'p':
- n = va_arg(ap, unsigned long int); // Und einen Integer als Hexadezimalzahl
- puti(n, 16);
- break;
- case 'b':
- base = va_arg(ap, unsigned int); // Und jetzt wird die Basis Gesetzt
- break;
- case 'B':
- n = va_arg(ap, unsigned long int); // Und jetzt kommt die Zahl zur Basis
- puti(n, base);
- break;
- case '%':
- putc('%'); // "%%" wird nicht ausgegeben als "" sondern als "%"
- break;
- case '\0':
- goto out; // Null? Weg damit!
- default:
- putc('%'); // nicht existent!
- putc(*fmt);
- break;
- }
- } else {
- kputc(*fmt);
- }
- fmt++;
- }
- out:
- va_end(ap);
- return kprintf_res;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment