Mtgxyz

MTGos Fail print.h

Dec 6th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.86 KB | None | 0 0
  1. //    MTGos - Ein einfaches Hobby-OS in C geschrieben
  2. //    Copyright (C) 2013 Morten Delenk
  3. //
  4. //    This program is free software: you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation, either version 3 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef __MTGOS_PRINT_H
  17. #define __MTGOS_PRINT_H
  18. #define SPALTEN 80
  19. #define REIHEN 24 // 25, Bei Null Beginnend
  20. #include <stdarg.h>
  21. unsigned short * const pScreen = (unsigned short *) 0xB8000; // Einen Pointer den wir als Array Missbrauchen
  22. // Juhuu! :-D (Geht wirklich :D)
  23. unsigned gPos = 0; // Ist eigentlich egal welchen Datentyp der Compiler nimmt;
  24. // Hauptsache es ist vorzeichenlos
  25. unsigned short gAttr=0x1F00; // Bluescreenalarm^^ Darüber wird noch abgestimmt
  26. // Aber Weiß auf Blau sieht super aus :-D
  27. // Ich könnte mir aber auch 0xF000 denken. (Schwarz auf Weiß) :D
  28. unsigned getX()
  29. {
  30.   return (gPos % SPALTEN);
  31. }
  32. unsigned getY()
  33. {
  34.   return ((gPos-getX()) / SPALTEN);
  35. }
  36. void gotoxy(unsigned x, unsigned y)
  37. {
  38.   gPos = x+(y*REIHEN); // Cusorposition := Reihe + (Zeile*REIHEN)
  39. }
  40. void scroll() { // Mit dieser Funktion Scrollen wir ne Reihe nach oben :D
  41.   unsigned quellePos = SPALTEN;
  42.   unsigned zielPos = 0;
  43.   for(zielPos=0; quellePos<SPALTEN*REIHEN+SPALTEN; quellePos++) {
  44.     pScreen[zielPos++] = pScreen[quellePos]; // MISSBRAUCHT!!!
  45.   }
  46.   for (zielPos=SPALTEN*REIHEN; zielPos<=SPALTEN*REIHEN+SPALTEN; zielPos++) {
  47.     pScreen[zielPos] = gAttr; // Und Bit Blau Machen!
  48.   }
  49.   gPos-=SPALTEN;
  50. }
  51. void scrollMultiLine(unsigned lines) { // Gibt bessere Methoden, da eine VGA-Karte
  52.   //Lahm ist dauert das ein Wenig^^
  53.   unsigned c=0;
  54.   for(c=0; c<lines; c++) {
  55.     scroll();
  56.   }
  57. }
  58. void MonitorInit() // Färbe den Kompletten Bildschirm ein. Nützlich, wenn man clrscr() nicht verwenden will!
  59. {
  60.   unsigned i;
  61.   for(i=0; i<SPALTEN*REIHEN+SPALTEN; i++) {
  62.     pScreen[i]&= 0x00FF;
  63.     pScreen[i]|= gAttr;
  64.   }
  65. }
  66. void setColor(unsigned char foreground, unsigned char background, int recolor) {
  67.   gAttr=((background << 4) | foreground) << 8;
  68.   if(recolor) {
  69.     MonitorInit();
  70.   }
  71. }
  72. void clrscr()
  73. {
  74.   scrollMultiLine(REIHEN+1);
  75.   unsigned short tmp=gAttr;
  76.   gAttr=0x1F00;
  77.   MonitorInit();
  78.   gAttr=tmp;
  79.   gPos=0;
  80. }
  81. void putchar(char ch) {
  82.   switch(ch) {
  83.     case '\n':
  84.       if(getY()==REIHEN) {
  85.     scroll();
  86.       }
  87.       gotoxy(0,getY()+1);
  88.       break;
  89.     case '\r':
  90.       gotoxy(0,getY());
  91.       break;
  92.     default:
  93.       if(gPos==SPALTEN*REIHEN+SPALTEN) {
  94.     scroll();
  95.       }
  96.       pScreen[gPos++] = (unsigned short) ch | gAttr;
  97.       break;
  98.   }
  99. }
  100. void puts(const char *s) {
  101.   while(*s)
  102.     putchar(*(s++));
  103. }
  104. void puti(unsigned long x, int base)
  105. {
  106.   char buf[65]; // 65-Bittiger Buffer
  107.   const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  108.   char* p; // Buffer
  109.   if (base > 36) {
  110.     return; // Tut mir leid, Wir können nur Alphanumerische Zeichen drucken!
  111.   }
  112.   if (base == 16) {
  113.     puts("0x"); // Hexadezimal? Du bekommst ein extra 0x!
  114.   }
  115.   p = buf+64;
  116.   *p = '\0';
  117.   do {
  118.     *--p = digits[x%base]; // Von Hinten Füllen :D
  119.     x /= base;
  120.   } while (x);
  121.   puts(p); // Jetzt ausgeben :D
  122.   if (base == 2) {
  123.     putchar('b'); // Binär? Dann bekommst du noch ein Nachzeichen :D (b)
  124.   }
  125. }
  126. int printf(const char* fmt, ...)
  127. {
  128.   va_list ap;
  129.   const char* s;
  130.   unsigned long n;
  131.   unsigned int base=10;
  132.   va_start(ap, fmt); // Liste Starten
  133.   unsigned int kprintf_res=0;
  134.   while (* fmt) {
  135.     if (*fmt == '%') { //Ist das ein Steuerzeichen?
  136.       fmt++;
  137.       switch (*fmt) {
  138.     case 's':
  139.       s = va_arg(ap, char*); // Drucke einen String
  140.       kputs(s);
  141.       break;
  142.     case 'd':
  143.     case 'u':
  144.       n=va_arg(ap, unsigned long int); // Und einen Integer als Dezimalzahl
  145.       puti(n,10);
  146.       break;
  147.     case 'x':
  148.     case 'p':
  149.       n = va_arg(ap, unsigned long int); // Und einen Integer als Hexadezimalzahl
  150.       puti(n, 16);
  151.       break;
  152.     case 'b':
  153.       base = va_arg(ap, unsigned int); // Und jetzt wird die Basis Gesetzt
  154.       break;
  155.     case 'B':
  156.       n = va_arg(ap, unsigned long int); // Und jetzt kommt die Zahl zur Basis
  157.       puti(n, base);
  158.       break;
  159.     case '%':
  160.       putc('%'); // "%%" wird nicht ausgegeben als "" sondern als "%"
  161.       break;
  162.     case '\0':
  163.       goto out; // Null? Weg damit!
  164.     default:
  165.       putc('%'); // nicht existent!
  166.       putc(*fmt);
  167.       break;
  168.       }
  169.     } else {
  170.       kputc(*fmt);
  171.     }
  172.     fmt++;
  173.   }
  174. out:
  175.   va_end(ap);
  176.   return kprintf_res;
  177. }
  178. #endif
Advertisement
Add Comment
Please, Sign In to add comment