rust-raspberrypi-OS-tutorials/.0A_pcscreenfont/lfb.c
2018-03-31 20:06:27 +02:00

151 lines
4.6 KiB
C

/*
* Copyright (C) 2018 bzt (bztsrc@github)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#include "uart.h"
#include "mbox.h"
/* PC Screen Font as used by Linux Console */
typedef struct {
unsigned int magic;
unsigned int version;
unsigned int headersize;
unsigned int flags;
unsigned int numglyph;
unsigned int bytesperglyph;
unsigned int height;
unsigned int width;
unsigned char glyphs;
} __attribute__((packed)) psf_t;
extern volatile unsigned char _binary_font_psf_start;
unsigned int width, height, pitch;
unsigned char *lfb;
/**
* Set screen resolution to 1024x768
*/
void lfb_init()
{
mbox[0] = 35*4;
mbox[1] = MBOX_REQUEST;
mbox[2] = 0x48003; //set phy wh
mbox[3] = 8;
mbox[4] = 8;
mbox[5] = 1024; //FrameBufferInfo.width
mbox[6] = 768; //FrameBufferInfo.height
mbox[7] = 0x48004; //set virt wh
mbox[8] = 8;
mbox[9] = 8;
mbox[10] = 1024; //FrameBufferInfo.virtual_width
mbox[11] = 768; //FrameBufferInfo.virtual_height
mbox[12] = 0x48009; //set virt offset
mbox[13] = 8;
mbox[14] = 8;
mbox[15] = 0; //FrameBufferInfo.x_offset
mbox[16] = 0; //FrameBufferInfo.y.offset
mbox[17] = 0x48005; //set depth
mbox[18] = 4;
mbox[19] = 4;
mbox[20] = 32; //FrameBufferInfo.depth
mbox[21] = 0x48006; //set pixel order
mbox[22] = 4;
mbox[23] = 4;
mbox[24] = 1; //RGB, not BGR preferably
mbox[25] = 0x40001; //get framebuffer, gets alignment on request
mbox[26] = 8;
mbox[27] = 8;
mbox[28] = 4096; //FrameBufferInfo.pointer
mbox[29] = 0; //FrameBufferInfo.size
mbox[30] = 0x40008; //get pitch
mbox[31] = 4;
mbox[32] = 4;
mbox[33] = 0; //FrameBufferInfo.pitch
mbox[34] = MBOX_TAG_LAST;
if(mbox_call(MBOX_CH_PROP) && mbox[20]==32 && mbox[28]!=0) {
mbox[28]&=0x3FFFFFFF;
width=mbox[5];
height=mbox[6];
pitch=mbox[33];
lfb=(void*)((unsigned long)mbox[28]);
} else {
uart_puts("Unable to set screen resolution to 1024x768x32\n");
}
}
/**
* Display a string
*/
void lfb_print(int x, int y, char *s)
{
// get our font
psf_t *font = (psf_t*)&_binary_font_psf_start;
// draw next character if it's not zero
while(*s) {
// get the offset of the glyph. Need to adjust this to support unicode table
unsigned char *glyph = (unsigned char*)&_binary_font_psf_start +
font->headersize + (*((unsigned char*)s)<font->numglyph?*s:0)*font->bytesperglyph;
// calculate the offset on screen
int offs = (y * font->height * pitch) + (x * (font->width+1) * 4);
// variables
int i,j, line,mask, bytesperline=(font->width+7)/8;
// handle carrige return
if(*s=='\r') {
x=0;
} else
// new line
if(*s=='\n') {
x=0; y++;
} else {
// display a character
for(j=0;j<font->height;j++){
// display one row
line=offs;
mask=1<<(font->width-1);
for(i=0;i<font->width;i++){
// if bit set, we use white color, otherwise black
*((unsigned int*)(lfb + line))=((int)*glyph) & mask?0xFFFFFF:0;
mask>>=1;
line+=4;
}
// adjust to next line
glyph+=bytesperline;
offs+=pitch;
}
x++;
}
// next character
s++;
}
}