001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019 #include <sos/klibc.h>
020 #include <hwcore/ioports.h>
021
022 #include "x86_videomem.h"
023
024
025
026
027 #define VIDEO 0xb8000
028
029
030
031 #define LINES 25
032 #define COLUMNS 80
033
034
035
036
037 typedef struct {
038 unsigned char character;
039 unsigned char attribute;
040 } __attribute__ ((packed)) x86_video_mem[LINES*COLUMNS];
041
042
043
044
045 static volatile x86_video_mem *video = (volatile x86_video_mem*)VIDEO;
046
047 sos_ret_t sos_x86_videomem_setup(void)
048 {
049
050
051
052
053 #define CRT_REG_INDEX 0x3d4
054 #define CRT_REG_DATA 0x3d5
055
056
057
058 outb(0x0a, CRT_REG_INDEX);
059
060
061 outb(1 << 5, CRT_REG_DATA);
062
063 return SOS_OK;
064 }
065
066
067 sos_ret_t sos_x86_videomem_cls(unsigned char attribute)
068 {
069
070 int i;
071 for(i = 0 ; i < LINES*COLUMNS ; i++)
072 {
073 (*video)[i].character = 0;
074 (*video)[i].attribute = attribute;
075 }
076
077 return SOS_OK;
078 }
079
080
081 sos_ret_t sos_x86_videomem_putstring(unsigned char row, unsigned char col,
082 unsigned char attribute,
083 const char *str)
084 {
085 unsigned video_offs = row*COLUMNS + col;
086
087 if (video_offs >= LINES*COLUMNS)
088 return -SOS_EINVAL;
089
090 for ( ; str && *str && (video_offs < LINES*COLUMNS) ; str++, video_offs++)
091 {
092 (*video)[video_offs].character = (unsigned char)*str;
093 (*video)[video_offs].attribute = attribute;
094 }
095
096 return SOS_OK;
097 }
098
099
100 sos_ret_t sos_x86_videomem_putchar(unsigned char row, unsigned char col,
101 unsigned char attribute,
102 unsigned char c)
103 {
104 unsigned video_offs = row*COLUMNS + col;
105
106 if (video_offs >= LINES*COLUMNS)
107 return -SOS_EINVAL;
108
109 (*video)[video_offs].character = c;
110 (*video)[video_offs].attribute = attribute;
111
112 return SOS_OK;
113 }
114
115
116 sos_ret_t sos_x86_videomem_printf(unsigned char row, unsigned char col,
117 unsigned char attribute,
118 const char *format, ...)
119 {
120 char buff[256];
121 va_list ap;
122
123 va_start(ap, format);
124 vsnprintf(buff, sizeof(buff), format, ap);
125 va_end(ap);
126
127 return sos_x86_videomem_putstring(row, col, attribute, buff);
128 }