summaryrefslogtreecommitdiff
path: root/driver/vesa.c
blob: 848b0bfb63915ede4c0707e946e32f44f0f73033 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "kernel/kernel.h"
#include "log.h"

#include <stdarg.h>
#include "kernel/mem.h"
#include "vesa.h"

#include "lib/printf/printf.h"

static foolfont *deffont;
//static vbemodeinfo *VbeModeInfoBlock;

static int console_x;
static int console_y;

static int console_lines;
static int console_cols;

static uint8_t* buffer;
static uint8_t* physbase;

void PutConsoleNL();
void PutPixel(int x,int y, int color);

// we need to obtain this from somewhere else..
static uint32_t vesaXres;
static uint32_t vesaYres;
static uint32_t vesaPitch;
static uint32_t vesaBpp;
static uint32_t vesaAddr;

//static char buf[80][24];

void vesa_update_cursor(uint32_t col,uint32_t row)
{
    console_x=col;
    console_y=row;
}

// helper_funcs
/*
static void vesa_print_char_col(int x, int y, char c, char col_fg, char col_bg)
{
 //   uint16_t attrib = (col_bg << 4) | (col_fg & 0x0F);
 //   uint16_t* video_mem=(uint16_t *)SCR_VIDEOMEM+(x+y*SCR_REAL_WIDTH);
  //  *video_mem=c | (attrib << 8) ;
}
*/

// same colors as in screen.h
static uint32_t cols[] = {
    0x0,      // black
    0x0066cc, //blue
    0x009900, //green
    0x33ffff, //cyan
    0xff3333, //red
    0xcc00cc, //magenta
    0x994c00, //brown
    0xa0a0a0, //light gray
    0x404040, //dark gray
    0x3399ff, //light blue
    0x99ff33, //light green
    0x99ffff, //cyan light
    0xff9999, //red light
    0xff99ff, //magenta light
    0xffff00, //yellow
    0xffffff, //white
};

// glue func for terminal 
void vesa_console_put_char(uint8_t c,uint8_t color_bg, uint8_t color_fg, uint32_t x, uint32_t y)
{
//    print_char_col(x,y,c, color_bg, color_fg);
	//PutFont(c, console_x*10,console_y*12, cols[color_fg],cols[color_bg]);
	
	//PutFont(c, x*10,y*12, cols[color_bg],cols[color_fg]);
	PutFont(c, x*8,y*11, cols[color_bg],cols[color_fg]);

//	buf[console_x][console_y]=c;
	
//	console_x++;

//	#ifdef FOOLOS_CONSOLE_AUTOBREAK
//	if(console_x>=console_cols)PutConsoleNL();
//	#endif
}

void vesa_switch_buffers()
{
    for(int i=0;i<800*600*2;i++)physbase[i]=buffer[i];
}

void vesa_put_rect(int x, int y, int w , int h, int color)
{
    for(int i=x;i<x+w;i++)
	for(int j=y;j<y+h;j++)
	    PutPixel(i,j,color);
}

void vesa_clear_screen()
{
    vesa_put_rect(0,0,vesaXres, vesaYres, 0xffffff);
}

void vesa_set_physbase(uint32_t addr)
{
    vesaAddr=addr;
}

    // 
    // We want to get output to the screen as fast as possible!
    //
    // Our Fool-Boot-Loader did set up VESA already for us.
    // The desired VESA mode is hardcoded in [boot/mbr.asm].
    //
    // The [vesa_init(...)] function requires:
    //
    //	* the addresses of the vbeinfo struct
    //	* the address of the vbemodeinfo struct (for selected mode).
    //	* Fool Font loaded inside ramimage
    //
    //	The first two paramters are hardcoded in [boot/mbr.asm],
    //	while the last one is set in the Makefile. The font binary
    //	is integrated in the kernel image.
    //
    //  this function returns the physical base address of 
    //  our video memory 
    //
    
uint32_t vesa_init(multiboot_information *inf, foolfont *rawfont)
{
//inf->framebuffer_type

    vesaXres=inf->framebuffer_width;
    vesaYres=inf->framebuffer_height;
    vesaPitch=inf->framebuffer_pitch;
    vesaBpp=inf->framebuffer_bpp;
    vesaAddr=inf->framebuffer_addr;

    //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=vesaYres/line_height;
    console_cols=vesaXres/col_width;

    //TODO dynamic (but need to sync with terminal!)
    console_cols=80;
    console_lines=24;
   /* 
    // vesa info
    klog("vbe version: 0x%x / video mode ptr: 0x%x 0x%x",
	    info->VbeVersion, info->VideoModePtr[1], info->VideoModePtr[0]);

    // vesa info on selected mode:
    klog("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);

    klog("res: %d * %d / banks: %d / attr: 0x%x",
	    mode->Xres, mode->Yres, mode->banks, mode->attributes);
    klog("bpp: %d / physbase: 0x%x",
	    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)
    {
	klog("mode supported : 0x%X", (*modeptr));
	modeptr++;
    }
#endif
*/


    return vesaAddr;   
}


// TODO: what will happen in 24bit mode?
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>vesaXres|| y<0 || y>vesaYres) return;
    if (x) x = (x*(vesaBpp>>3)); // get bytes (divide by 8)
    if (y) y = (y*vesaPitch);
    //uint8_t *cTemp=VbeModeInfoBlock->physbase;
    uint8_t *cTemp=VMEM_FRAMEBUFFER;

    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_fg,int color_bg)
{

    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++)
    {
		PutPixel(posx,y-1,color_bg);
    }
    for(posy=y;posy<y+sizey;posy++)
    {
		PutPixel(x-1,posy,color_bg);
    }

    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_fg);
	    }
	    else
	    {
		PutPixel(posx,posy,color_bg);
	    }

	}
    }

}

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,0x0);
	i+=9; // spacing 
	str++;
    }

}

void PutConsoleChar(char c, int color)
{
	if(c=='\n')
	{
	    PutConsoleNL();
	    return;
	}
	//PutFont(c, console_x*10,console_y*12, color);
	console_x++;


	#ifdef FOOLOS_CONSOLE_AUTOBREAK
	if(console_x>=console_cols)PutConsoleNL();
	#endif
}

void PutConsole(char *str, int color)
{

    while((*str)!=0)
    {
	PutConsoleChar(*str,color);
	str++;
    }

}
void PutConsoleNL()
{
    console_y++;
    if(console_y<console_lines)return;

    for(int line=0;line<24;line++)
    {
	for(int j=0;j<console_cols;j++)
	{
//		buf[j][line]=buf[j][line+1];
//		PutFont(buf[j][line],j*10,line*12,0x33ff66,0);
//		PutFont(buf[j][line+1],j*10,line*12,0x33ff66,0);
	}
    }

    for(int i=0;i<console_cols;i++)
    {
	PutFont(' ',i*10,23*12,0x33ff66,0);
	//buf[i][23]=' ';
    }

    console_x=0;
    console_y--;
}

static int boxx;
static int boxy;
////////////////
void vesa_render()
{
    vesa_clear_screen();
    vesa_put_rect(100,100,vesaXres-200,vesaYres-200,0xff);
    vesa_put_rect(boxx-10,boxy-10,20,20,0x999999);

    boxx++;
//    boxy+=boxx;
    if(boxx>vesaXres-100)boxx=100;
  //  if(boxy>VbeModeInfoBlock->Yres-200)boxy=200;

    vesa_switch_buffers();
}

/*
void vesa_init_doublebuff()
{
    boxx=300;
    boxy=300;

    int blocks=800*600*2/4096+1;
    physbase=VbeModeInfoBlock->physbase;
    buffer=pmmngr_alloc_blocks(blocks);
    klog("Init buffer of %d blocks at 0x%08X",blocks,buffer);

    VbeModeInfoBlock->physbase=buffer;
}
*/