cga.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. #include "io.h"
  8. enum {
  9. Width = 160,
  10. Height = 25,
  11. Attr = 0x4f, /* white on blue */
  12. };
  13. static ulong cgabase;
  14. #define CGASCREENBASE ((uchar*)cgabase)
  15. static int cgapos;
  16. static int screeninitdone;
  17. static Lock cgascreenlock;
  18. static uchar
  19. cgaregr(int index)
  20. {
  21. outb(0x3D4, index);
  22. return inb(0x3D4+1) & 0xFF;
  23. }
  24. static void
  25. cgaregw(int index, int data)
  26. {
  27. outb(0x3D4, index);
  28. outb(0x3D4+1, data);
  29. }
  30. static void
  31. movecursor(void)
  32. {
  33. cgaregw(0x0E, (cgapos/2>>8) & 0xFF);
  34. cgaregw(0x0F, cgapos/2 & 0xFF);
  35. CGASCREENBASE[cgapos+1] = Attr;
  36. }
  37. static void
  38. cgascreenputc(int c)
  39. {
  40. int i;
  41. if(c == '\n'){
  42. cgapos = cgapos/Width;
  43. cgapos = (cgapos+1)*Width;
  44. }
  45. else if(c == '\t'){
  46. i = 8 - ((cgapos/2)&7);
  47. while(i-->0)
  48. cgascreenputc(' ');
  49. }
  50. else if(c == '\b'){
  51. if(cgapos >= 2)
  52. cgapos -= 2;
  53. cgascreenputc(' ');
  54. cgapos -= 2;
  55. }
  56. else{
  57. CGASCREENBASE[cgapos++] = c;
  58. CGASCREENBASE[cgapos++] = Attr;
  59. }
  60. if(cgapos >= Width*Height){
  61. memmove(CGASCREENBASE, &CGASCREENBASE[Width], Width*(Height-1));
  62. for (i = Width*(Height-1); i < Width*Height;) {
  63. CGASCREENBASE[i++] = 0x20;
  64. CGASCREENBASE[i++] = Attr;
  65. }
  66. cgapos = Width*(Height-1);
  67. }
  68. movecursor();
  69. }
  70. void
  71. screeninit(void)
  72. {
  73. cgabase = (ulong)arch->pcimem(0xB8000, 0x8000);
  74. cgapos = cgaregr(0x0E)<<8;
  75. cgapos |= cgaregr(0x0F);
  76. cgapos *= 2;
  77. screeninitdone = 1;
  78. }
  79. static void
  80. cgascreenputs(char* s, int n)
  81. {
  82. if(!screeninitdone)
  83. return;
  84. if(!islo()){
  85. /*
  86. * Don't deadlock trying to
  87. * print in an interrupt.
  88. */
  89. if(!canlock(&cgascreenlock))
  90. return;
  91. }
  92. else
  93. lock(&cgascreenlock);
  94. while(n-- > 0)
  95. cgascreenputc(*s++);
  96. unlock(&cgascreenlock);
  97. }
  98. void (*screenputs)(char*, int) = cgascreenputs;