summaryrefslogtreecommitdiff
path: root/kernel/vesa.c
blob: 55c400ca049024eb9161fa1ea134d34763c97bed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//http://wiki.osdev.org/GUI
#include <stdarg.h>
#include "kernel.h"

#include "lib/logger/log.h"	// logger facilities
#include "lib/int/stdint.h"
#define FOOLOS_MODULE_NAME "vesa"

#define FOOLSOS_SHOW_VESAMODES

typedef struct vbeinfo_struct{
   char VbeSignature[4];             // == "VESA"
   uint16_t VbeVersion;                 // == 0x0300 for VBE 3.0
   uint16_t OemStringPtr[2];            // isa vbeFarPtr
   uint8_t Capabilities[4];
   uint16_t VideoModePtr[2];         // isa vbeFarPtr
   uint16_t TotalMemory;             // as # of 64KB blocks
}vbeinfo;

typedef struct ModeInfoBlock {
  uint16_t attributes;
  uint8_t winA,winB;
  uint16_t granularity;
  uint16_t winsize;
  uint16_t segmentA, segmentB;
  uint16_t realFctPtr[2];
//  VBE_FAR(realFctPtr);
  uint16_t pitch; // bytes per scanline
 
  uint16_t Xres, Yres;
  uint8_t Wchar, Ychar, planes, bpp, banks;
  uint8_t memory_model, bank_size, image_pages;
  uint8_t reserved0;
 
  uint8_t red_mask, red_position;
  uint8_t green_mask, green_position;
  uint8_t blue_mask, blue_position;
  uint8_t rsv_mask, rsv_position;
  uint8_t directcolor_attributes;
 
  volatile  uint32_t physbase;  // your LFB (Linear Framebuffer) address ;)
  uint32_t reserved1;
  uint16_t reserved2;
}vbemodeinfo;

typedef struct foolfont_struct
{
    uint8_t line[10]; //every single fool font consists of 10 lines a 8 bit

}foolfont;

static foolfont *deffont;
static vbemodeinfo *VbeModeInfoBlock;

static console_x;
static console_y;

static console_lines;
static console_cols;

void vesa_set_physbase(uint32_t addr)
{
    VbeModeInfoBlock->physbase=addr;
}


uint32_t vesa_init(vbeinfo *info,vbemodeinfo *mode,foolfont *rawfont)
{
    //the only functionallu important init lines! (rest is log)
    VbeModeInfoBlock=mode;
    deffont=rawfont;
    console_x=0;
    console_y=0;

    int line_height=12;
    int col_width=10;
    console_lines=mode->Yres/line_height;
    console_cols=mode->Xres/col_width;
    
    // vesa info
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"setup complete. vbe version: 0x%x / video mode ptr: 0x%x 0x%x",
	    info->VbeVersion, info->VideoModePtr[1], info->VideoModePtr[0]);

    // vesa info on selected mode:
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"colors r:%d 0x%x g:%d 0x%x b:%d 0x%x",
	    mode->red_position,mode->red_mask, 
	    mode->green_position,mode->green_mask, 
	    mode->blue_position,mode->blue_mask);

    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"res: %d * %d / banks: %d / attr: 0x%x / bpp: %d / physbase: 0x%x",
	    mode->Xres, mode->Yres, mode->banks, mode->attributes,  mode->bpp,mode->physbase);
     // vesa modes
     // todo: take segment from vbeinfo!
#ifdef FOOLSOS_SHOW_VESAMODES
    uint16_t *modeptr=info->VideoModePtr[0];

    while(*modeptr!=0xffff&&*modeptr!=0)
    {
	log(FOOLOS_MODULE_NAME,FOOLOS_LOG_DEBUG,"mode supported : 0x%x", (*modeptr));
	scr_put_hex(*modeptr);
	modeptr++;
    }
#endif

    return VbeModeInfoBlock->physbase;   

}

void PutPixel(int x,int y, int color){

    //do not write memory outside the screen buffer, check parameters against the VBE mode info
    if (x<0 || x>VbeModeInfoBlock->Xres|| y<0 || y>VbeModeInfoBlock->Yres) return;
    if (x) x = (x*(VbeModeInfoBlock->bpp>>3)); // get bytes (divide by 8)
    if (y) y = (y*VbeModeInfoBlock->pitch);
    uint8_t *cTemp=VbeModeInfoBlock->physbase;

    cTemp[x+y] =  (uint8_t)(color & 0xff);
    cTemp[x+y+1] = (uint8_t)((color>>8) & 0xff);
    //cTemp[x+y+2] = (uint8_t)((color>>16) & 0xff);
 }


void PutFont(char c, int x,int y, int color)
{

    int fnt=0x126-0x20;

    if(c>=0x20&&c<=0x126)fnt=c-0x20;
  


    int posx, posy, sizex=8, sizey=10;

    for(posx=x;posx<x+sizex;posx++)
    {

	for(posy=y;posy<y+sizey;posy++)
	{
	    if(deffont[fnt].line[posy-y]&1<<(7-(posx-x)))
	    {
		PutPixel(posx,posy,color);
	    }
	    else
	    {
		PutPixel(posx,posy,0);
	    }

	}
    }

}

void PutString(char *str, int x,int y, int color, va_list va)
{

    char buff[256];
    tfp_sprintf(buff,str,va);
    str=buff;

    int i=x;
    while((*str)!=0)
    {
	PutFont(*str, i,y, color);
	i+=9; // spacing 
	str++;
    }

}

void PutConsole(char *str, int color, va_list va)
{

    char buff[256];
    tfp_vsprintf(buff,str,va);
    str=buff;

    while((*str)!=0)
    {
	PutFont(*str, console_x*10,console_y*12, color);
	str++;
	console_x++;
	if(console_x>console_cols);//PutConsoleNL();
    }

}
void PutConsoleNL()
{
    console_x=0;
    console_y++;
    if(console_y>=console_lines)console_y=0;

    for(int i=0;i<console_cols;i++)
    {
	PutFont(' ',i*10,(console_y)*12,0);
    }
}