forked from 400plus/400plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp.c
190 lines (146 loc) · 4.85 KB
/
bmp.c
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
/**
* $Revision$
* $Date$
* $Author$
*/
/*##################################################################################
# #
# _____ _ _ #
# | ___| | | | | #
# |___ \ __| |_ __ | |_ _ ___ #
# \ \/ _` | '_ \| | | | / __| #
# /\__/ / (_| | |_) | | |_| \__ \ #
# \____/ \__,_| .__/|_|\__,_|___/ #
# | | #
# |_| #
# #
#################################################################################*/
#include <sys/types.h>
#include <stdio.h>
#include <stdarg.h>
#include "bmp.h"
#include "debug.h"
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned int uint32_t;
typedef long long int64_t;
typedef unsigned long long uint64_t;
static void _draw_char(unsigned fontspec, uint8_t * bmp_vram_row, char c) {
//~ if (!bmp_enabled) return;
unsigned i,j;
const struct font * const font = fontspec_font( fontspec );
uint32_t fg_color = fontspec_fg( fontspec ) << 24;
uint32_t bg_color = fontspec_bg( fontspec ) << 24;
// Special case -- fg=bg=0 => white on black
if( fg_color == 0 && bg_color == 0 ) {
fg_color = COLOR_WHITE << 24;
bg_color = COLOR_BLACK << 24;
}
const uint32_t pitch = BMPPITCH / 4;
uint32_t * front_row = (uint32_t *) bmp_vram_row;
//uint32_t flags = cli();
for( i=0 ; i<font->height ; i++ ) {
// Start this scanline
uint32_t * row = front_row;
// move to the next scanline
front_row += pitch;
uint32_t pixels = font->bitmap[ c + (i << 7) ];
uint8_t pixel;
for( j=0 ; j<font->width/4 ; j++ ) {
uint32_t bmp_pixels = 0;
for( pixel=0 ; pixel<4 ; pixel++, pixels <<=1 ) {
bmp_pixels >>= 8;
bmp_pixels |= (pixels & 0x80000000) ? fg_color : bg_color;
}
if ( (int)row < vram_end && (int)row >= vram_start ) {
*(row++) = bmp_pixels;
} else {
debug_log("VRAM: draw outside vram region (0x%08X)", row);
return;
}
// handle characters wider than 32 bits
if( j == 28/4 )
pixels = font->bitmap[ c + ((i+128) << 7) ];
}
}
//sei( flags );
}
void bmp_puts(unsigned fontspec, unsigned * x, unsigned * y, const char * s) {
const uint32_t pitch = BMPPITCH;
int * vram = bmp_vram();
if( !vram || ((int)vram & 1) == 1 )
return;
const unsigned initial_x = *x;
uint8_t * first_row = (uint8_t*) ((int)vram + ((*y) * pitch) + (*x));
uint8_t * row = first_row;
char c;
const struct font * const font = fontspec_font( fontspec );
while( (c = *s++) ) {
if( c == '\n' ) {
row = first_row += pitch * font->height;
(*y) += font->height;
(*x) = initial_x;
continue;
}
_draw_char( fontspec, row, c );
row += font->width;
(*x) += font->width;
}
}
void bmp_printf(unsigned fontspec, unsigned x, unsigned y, const char * fmt, ...) {
va_list ap;
char buf[ 256 ];
va_start( ap, fmt );
vsnprintf( buf, sizeof(buf), fmt, ap );
va_end( ap );
bmp_puts( fontspec, &x, &y, buf );
}
// Andrew:
// bmp_hexdump(FONT_SMALL, 0, y_offset, 0xlocation_to_dump, 16*[# of lines to show on screen]);
// always keep x offset as 0 because it fills the whole screen as is :P
//
//~ Scaled in half from ML version. always have len as a multiple of 16. General
//~ use is 16 * num of lines desired printed.
void bmp_hexdump(unsigned fontspec, unsigned x, unsigned y, const void * buf, int len) {
if( len == 0 )
return;
// Round up
len = (len + 15) & ~15;
const uint32_t * d = (uint32_t*) buf;
do {
bmp_printf(fontspec, x, y,
"%08x: %08x %08x %08x %08x",
(unsigned) d,
len > 0 ? MEM(d+0) : 0,
len > 4 ? MEM(d+1) : 0,
len > 8 ? MEM(d+2) : 0,
len > 12 ? MEM(d+3) : 0
);
y += fontspec_height( fontspec );
d += 4;
len -= 16;
} while(len > 0);
}
/** Draw a picture of the BMP color palette. */
void bmp_draw_palette( void ) {
uint32_t x, y, msb, lsb;
const uint32_t height = 15;
const uint32_t width = 22;
for( msb=0 ; msb<16; msb++ ) {
for( y=0 ; y<height; y++ ) {
int * const row = (int*) (int)bmp_vram() + (y + height*msb) * BMPPITCH;
for( lsb=0 ; lsb<16 ; lsb++ ) {
for( x=0 ; x<width ; x++ )
row[x+width*lsb] = (msb << 4) | lsb;
}
}
}
static int written;
if( !written )
//dispcheck();
written = 1;
SleepTask(2000);
}