File: mod_epfb.c

package info (click to toggle)
spl 1.0~pre6-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,352 kB
  • ctags: 2,070
  • sloc: ansic: 16,614; yacc: 3,182; sh: 299; makefile: 165; xml: 156
file content (629 lines) | stat: -rw-r--r-- 23,718 bytes parent folder | download | duplicates (3)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2006 Clifford Wolf <clifford@clifford.at>
 *  Copyright (C) 2007 Raphael Langerhorst <raphael@raphael.g-system.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  mod_epfb.c: Hardware Accelerated Raster Graphics Engine on EP9315.
 */

/**
 * SPL EPFB Module
 *
 * This Module provides hardware accelerated graphics on EP9315 processors.
 * It is tailored for the NetBSD/evbarm architecture.
 * If compiling fails, make sure that the file epio.h is in include path.
 */

#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <epio.h>
#include <stdio.h>

#include "spl.h"
#include "compat.h"

extern void SPL_ABI(spl_mod_epfb_init)(struct spl_vm *vm, struct spl_module *mod, int restore);
extern void SPL_ABI(spl_mod_epfb_done)(struct spl_vm *vm, struct spl_module *mod);

static int epfb_fd;

// FONT BITMAPS TAKEN FROM L3D (raster/ras3_sw.cc)

// L3D realtime 3D library, explained in book "Linux 3D Graphics Programming"
// Copyright (C) 2000  Norman Lin
// Contact: nlin@linux3dgraphicsprogramming.org (alt. nlin@geocities.com)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

///////////////////////////////////////////////////////////////////////////


static const unsigned char space[] =
  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

static const unsigned char digits[][13] = {
  {0x0,0x0, 0x18,0x3C,0x66,0xC3,0xC3,0xC3,0xC3,0xC3,0x66,0x3C,0x18},
  {0x0,0x0, 0x7F,0x7F,0xC,0xC,0xC,0xC,0xC,0xC,0x6C,0x3C,0xC},
  {0x0,0x0, 0xFF,0xFF,0xC0,0x60,0x30,0x18,0xC,0x6,0xC6,0x66,0x3C},
  {0x0,0x0, 0x7E,0xC3,0x3,0x6,0xC,0x38,0xC,0x6,0x3,0xC3,0x7E},
  {0x0,0x0, 0x6,0x6,0x6,0x6,0xFF,0xFF,0xC6,0xC6,0x66,0x36,0x1E},
  {0x0,0x0, 0x7E,0xFF,0xC3,0x3,0x3,0xFF,0xFE,0xC0,0xC0,0xC0,0xFE},
  {0x0,0x0, 0x7E,0xFF,0xC3,0xC3,0xC3,0xFF,0xFE,0xC0,0xC0,0xC0,0x7E},
  {0x0,0x0, 0x60,0x60,0x60,0x60,0x30,0x18,0xC,0x6,0x3,0x3,0xFF},
  {0x0,0x0, 0x7E,0xFF,0xC3,0xC3,0xC3,0x7E,0xC3,0xC3,0xC3,0xC3,0x7E},
  {0x0,0x0, 0x7E,0xFF,0xC3,0x3,0x3,0x7F,0xC3,0xC3,0xC3,0xC3,0x7E}
};


static const unsigned char upper_case_letters[][13] = {
  {0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18}, // A
  {0x00, 0x00, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe}, // B
  {0x00, 0x00, 0x7e, 0xe7, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e}, // C
  {0x00, 0x00, 0xfc, 0xce, 0xc7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc7, 0xce, 0xfc}, // D
  {0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xff}, // E
  {0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xff}, // F
  {0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xcf, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e}, // G
  {0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, // H
  {0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e}, // I
  {0x00, 0x00, 0x7c, 0xee, 0xc6, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06}, // J
  {0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xe0, 0xf0, 0xd8, 0xcc, 0xc6, 0xc3}, // K
  {0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0}, // L
  {0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xdb, 0xff, 0xff, 0xe7, 0xc3}, // M
  {0x00, 0x00, 0xc7, 0xc7, 0xcf, 0xcf, 0xdf, 0xdb, 0xfb, 0xf3, 0xf3, 0xe3, 0xe3}, // N
  {0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xe7, 0x7e}, // O
  {0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe}, // P
  {0x00, 0x00, 0x3f, 0x6e, 0xdf, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x66, 0x3c}, // Q
  {0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe}, // R
  {0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07, 0x7e, 0xe0, 0xc0, 0xc0, 0xe7, 0x7e}, // S
  {0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xff}, // T
  {0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, // U
  {0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, // V
  {0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, // W
  {0x00, 0x00, 0xc3, 0x66, 0x66, 0x3c, 0x3c, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3}, // X
  {0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3}, // Y
  {0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff}  // Z
};

/* lower case letters added by raphael */
static const unsigned char lower_case_letters[][13] = {
  {0x00, 0x00, 0x7f, 0xc3, 0xc3, 0xc3, 0xc7, 0x7b, 0x00, 0x00, 0x00, 0x00, 0x00}, // a
  {0x00, 0x00, 0xff, 0xc3, 0xc3, 0xc3, 0xc3, 0xfe, 0xc0, 0xc0, 0xc0, 0xc0, 0x00}, // b
  {0x00, 0x00, 0x7e, 0xc3, 0xc0, 0xc0, 0xc3, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00}, // c
  {0x00, 0x00, 0x7f, 0xc3, 0xc3, 0xc3, 0xc3, 0x7f, 0x03, 0x03, 0x03, 0x03, 0x00}, // d
  {0x00, 0x00, 0x7e, 0xc0, 0xc0, 0xff, 0xc2, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00}, // e
  {0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3e, 0x18, 0x1a, 0x1e, 0x00}, // f
  {0xfe, 0x03, 0x7f, 0xc3, 0xc3, 0xc3, 0xc3, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00}, // g
  {0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xfe, 0xc0, 0xc0, 0xc0, 0xc0, 0x00}, // h
  {0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x18, 0x00, 0x00}, // i
  {0x30, 0x58, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x18, 0x00, 0x00}, // j
  {0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xd8, 0xcc, 0xc6, 0xc0, 0xc0, 0x00}, // k
  {0x00, 0x00, 0x1c, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00}, // l
  {0x00, 0x00, 0xdb, 0xdb, 0xdb, 0xdb, 0xdb, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00}, // m
  {0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00}, // n
  {0x00, 0x00, 0x7e, 0xc3, 0xc3, 0xc3, 0xc3, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00}, // o
  {0xc0, 0xc0, 0xff, 0xc3, 0xc3, 0xc3, 0xc3, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00}, // p
  {0x03, 0x03, 0x7f, 0xc3, 0xc3, 0xc3, 0xc3, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00}, // q
  {0x00, 0x00, 0x30, 0x30, 0x30, 0x30, 0x38, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x00}, // r
  {0x00, 0x00, 0x1e, 0x23, 0x06, 0x08, 0x31, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00}, // s
  {0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3e, 0x18, 0x18, 0x18, 0x00}, // t
  {0x00, 0x00, 0x7f, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x00, 0x00, 0x00, 0x00, 0x00}, // u
  {0x00, 0x00, 0x18, 0x3c, 0x24, 0x66, 0x42, 0xc3, 0x00, 0x00, 0x00, 0x00, 0x00}, // v
  {0x00, 0x00, 0x6d, 0xdb, 0xdb, 0xdb, 0xc3, 0xc3, 0x00, 0x00, 0x00, 0x00, 0x00}, // w
  {0x00, 0x00, 0xc3, 0x66, 0x3c, 0x18, 0x3c, 0x66, 0xc3, 0x00, 0x00, 0x00, 0x00}, // x
  {0x60, 0x30, 0x18, 0x3c, 0x24, 0x66, 0x42, 0xc3, 0x00, 0x00, 0x00, 0x00, 0x00}, // y
  {0x00, 0x00, 0xff, 0xc0, 0x60, 0x18, 0x06, 0x03, 0xff, 0x00, 0x00, 0x00, 0x00}  // z
};

/* Umlaute by raphael */
static const unsigned char letter_Auml[] =
  {0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0xbd, 0xdb}; // A:
static const unsigned char letter_Ouml[] =
  {0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xe7, 0x3c, 0x00, 0x66}; // O:
static const unsigned char letter_Uuml[] =
  {0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x00, 0xc3, 0xc3}; // U:
static const unsigned char letter_auml[] =
  {0x00, 0x00, 0x7f, 0xc3, 0xc3, 0xc3, 0xc7, 0x7b, 0x00, 0x00, 0x66, 0x66, 0x00}; // a:
static const unsigned char letter_ouml[] =
  {0x00, 0x00, 0x7e, 0xc3, 0xc3, 0xc3, 0xc3, 0x7e, 0x00, 0x00, 0x66, 0x66, 0x00}; // o:
static const unsigned char letter_uuml[] =
  {0x00, 0x00, 0x7f, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x00, 0x00, 0x66, 0x66, 0x00}; // u:

/* ss by raphael */
static const unsigned char letter_ss[] =
  {0x00, 0x00, 0x9e, 0xc7, 0xc3, 0xc3, 0xc7, 0xbe, 0xc6, 0xc2, 0xc6, 0x7c, 0x00}; // ss

/* misc by raphael, 2007-10-19 */

/* misc 1, from 0x21 to 0x2f */
static const unsigned char misc_1[][13] = {
  {0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18}, // !
  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66}, // "
  {0x00, 0x00, 0x66, 0x66, 0xff, 0xff, 0x66, 0x66, 0xff, 0xff, 0x66, 0x66, 0x00}, // #
  {0x00, 0x18, 0x7e, 0xff, 0x8b, 0x8b, 0x8f, 0x7e, 0xf8, 0xd8, 0xd9, 0xff, 0x7e}, // $
  {0x00, 0x00, 0xc2, 0x45, 0x62, 0x20, 0x30, 0x10, 0x18, 0x0c, 0x24, 0x56, 0x22}, // %
  {0x00, 0x00, 0x73, 0xcc, 0xdb, 0x69, 0x18, 0x39, 0xc3, 0xc3, 0x39, 0x18, 0x00}, // &
  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18}, // '
  {0x00, 0x00, 0x0c, 0x1c, 0x18, 0x38, 0x30, 0x30, 0x30, 0x38, 0x18, 0x1c, 0x0c}, // (
  {0x00, 0x00, 0x30, 0x38, 0x18, 0x1c, 0x0c, 0x0c, 0x0c, 0x1c, 0x18, 0x38, 0x30}, // )
  {0x00, 0x00, 0x00, 0x18, 0x99, 0x5a, 0x3c, 0x3c, 0x5a, 0x99, 0x18, 0x00, 0x00}, // *
  {0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0xff, 0xff, 0x18, 0x18, 0x18, 0x00, 0x00}, // +
  {0x20, 0x30, 0x18, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // ,
  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00}, // -
  {0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // .
  {0x00, 0x00, 0xc0, 0x40, 0x60, 0x20, 0x30, 0x10, 0x18, 0x0c, 0x04, 0x06, 0x02}  // /
};

static const unsigned char misc_2[][13] = {
  {0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00}, // :
  {0x20, 0x30, 0x18, 0x08, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00}, // ,
  {0x00, 0x00, 0x01, 0x07, 0x1c, 0x70, 0xc0, 0x70, 0x1c, 0x07, 0x01, 0x00, 0x00}, // <
  {0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00}, // =
  {0x00, 0x00, 0x80, 0xe0, 0x38, 0x0e, 0x03, 0x0e, 0x38, 0xe0, 0x80, 0x00, 0x00}, // >
  {0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x0c, 0x04, 0x06, 0x06, 0x06, 0x4c, 0x38}, // ?
  {0x00, 0x7e, 0x80, 0x9f, 0xb5, 0xb5, 0x95, 0x9d, 0x81, 0x7e, 0x00, 0x00, 0x00}  // @
};

/* misc 3, from 0x5B to 0x60 */
static const unsigned char misc_3[][13] = {
  {0x00, 0x00, 0x3e, 0x3e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3e, 0x3e}, // [
  {0x00, 0x00, 0x02, 0x06, 0x04, 0x0c, 0x18, 0x10, 0x30, 0x20, 0x60, 0x40, 0xc0}, // 
  {0x00, 0x00, 0x7c, 0x7c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x7c, 0x7c}, // ]
  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x66, 0x24, 0x3c, 0x18}, // 
  {0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // _
  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x04, 0x0c, 0x18, 0x30}  // `
};

/* misc 4, from 0x7B to 0x7E */
static const unsigned char misc_4[][13] = {
  {0x00, 0x00, 0x0c, 0x18, 0x18, 0x18, 0x18, 0x30, 0x18, 0x18, 0x18, 0x18, 0x0c}, // {
  {0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18}, // |
  {0x00, 0x00, 0x30, 0x18, 0x18, 0x18, 0x18, 0x0c, 0x18, 0x18, 0x18, 0x18, 0x30}, // }
  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0x77, 0x00, 0x00, 0x00, 0x00}  // ~
};

unsigned char font_bitmaps[256][13];

void epfb_init_fontbitmaps()
{
  //- create font bitmaps
  int i,j;

  for(i=0; i<26; i++) {
    for(j=0; j<13; j++) {
      font_bitmaps['A' + i][j] = upper_case_letters[i][j];
      font_bitmaps['a' + i][j] = lower_case_letters[i][j];
    }
  }

  for(i=0; i<10; i++) {
    for(j=0; j<13; j++) {
      font_bitmaps['0' + i][j] = digits[i][j];
    }
  }

  for(j=0; j<13; j++) {
    font_bitmaps[' '][j] = space[j];
  }

  /* misc 1 */
  for(i=0; i<15; i++) {
    for(j=0; j<13; j++) {
      font_bitmaps[0x21 + i][j] = misc_1[i][j];
    }
  }

  /* misc 2 */
  for(i=0; i<7; i++) {
    for(j=0; j<13; j++) {
      font_bitmaps[0x3a + i][j] = misc_2[i][j];
    }
  }

  /* misc 3 */
  for(i=0; i<6; i++) {
    for(j=0; j<13; j++) {
      font_bitmaps[0x5b + i][j] = misc_3[i][j];
    }
  }

  /* misc 4 */
  for(i=0; i<4; i++) {
    for(j=0; j<13; j++) {
      font_bitmaps[0x7b + i][j] = misc_4[i][j];
    }
  }

  // Umlaute by raphael - 2007-06-28 - See Unicode code page for position (0080)
  // A:
  for(j=0; j<13; j++) {
    font_bitmaps[0xc4][j] = letter_Auml[j];
  }

  // O:
  for(j=0; j<13; j++) {
    font_bitmaps[0xD6][j] = letter_Ouml[j];
  }

  // U:
  for(j=0; j<13; j++) {
    font_bitmaps[0xDC][j] = letter_Uuml[j];
  }

  // a:
  for(j=0; j<13; j++) {
    font_bitmaps[0xe4][j] = letter_auml[j];
  }

  // o:
  for(j=0; j<13; j++) {
    font_bitmaps[0xf6][j] = letter_ouml[j];
  }

  // u:
  for(j=0; j<13; j++) {
    font_bitmaps[0xfc][j] = letter_uuml[j];
  }

  // ss - raphael, 2007-10-18
  for(j=0; j<13; j++) {
    font_bitmaps[0xdf][j] = letter_ss[j];
  }

}


void epfb_draw_text(int x, int y, const char *text, int col) {

#define FONT_Y_SIZE 13
#define FONT_X_SIZE 8
#define FONT_SPACING 2

  int font_y, font_x;

  struct epfb_pixelset pixel;
  pixel.col = col;

  // raphael 2007-06-28: we need to start at Y_SIZE - 1 (font_y=1) and go to 0 (font_y == Y_SIZE)
  for(font_y=1; font_y <= FONT_Y_SIZE; font_y++) {

    pixel.y = y + font_y;
    pixel.x = x;
    unsigned int c;
    for(c=0; c<strlen(text); c++) {
      int current_bit;
      for(font_x=0, current_bit=0x80;
          font_x < FONT_X_SIZE;
          font_x++, current_bit >>= 1)
      {
        if(font_bitmaps[text[c]][FONT_Y_SIZE-font_y] & current_bit)
	  ioctl(epfb_fd,EPFB_PIXELSET,&pixel);
	pixel.x++;
      }
      pixel.x += FONT_SPACING;
    }
  }
}


// END OF FONT BITMAPS BY NORMAN LIN

static struct spl_node *handler_epfb_output_status(struct spl_task *task, void *data)
{
  int ena;
  if (ioctl(epfb_fd,EPFB_OUTPUT_STATUS,&ena)==-1)
  {
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "setting epfb output failed\n");
    return 0;
  }
  return SPL_NEW_INT(ena);
}

static struct spl_node *handler_epfb_output_enable(struct spl_task *task, void *data)
{
  int ena = spl_clib_get_int(task);
  if (ioctl(epfb_fd,EPFB_OUTPUT_ENABLE,&ena)==-1)
  {
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "setting epfb output failed\n");
    return 0;
  }
  return 0;
}

static struct spl_node *handler_epfb_devinfo(struct spl_task *task, void *data)
{
  struct epfb_devinfo devinfo;
  if (ioctl(epfb_fd,EPFB_DEVINFO,&devinfo)==-1)
  {
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "getting epfb device info failed\n");
    return 0;
  }

  struct spl_node* i = spl_get(0);
  spl_create(task, i, "width",  SPL_NEW_INT(devinfo.width),  SPL_CREATE_LOCAL);
  spl_create(task, i, "height",  SPL_NEW_INT(devinfo.height),  SPL_CREATE_LOCAL);
  spl_create(task, i, "depth",  SPL_NEW_INT(devinfo.depth),  SPL_CREATE_LOCAL);
  spl_create(task, i, "stride",  SPL_NEW_INT(devinfo.stride),  SPL_CREATE_LOCAL);

  return i;
}

static struct spl_node *handler_epfb_clear(struct spl_task *task, void *data)
{
  if (ioctl(epfb_fd,EPFB_CLEAR)==-1)
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "clear ioctl failed\n");
  return 0;
}

/**
 * Set the colour of a single pixel on screen.
 */
// builtin epfb_pixelset(x,y,col)

static struct spl_node *handler_epfb_pixelset(struct spl_task *task, void *data)
{
  struct epfb_pixelset pixel;
  pixel.x = spl_clib_get_int(task);
  pixel.y = spl_clib_get_int(task);
  pixel.col = spl_clib_get_int(task);

  if (ioctl(epfb_fd,EPFB_PIXELSET,&pixel) == -1)
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "pixelset ioctl failed\n");
  return 0;
}

/**
 * Draw a line from (x1/y1) to (x2/y2) on screen.
 */
// builtin epfb_linedraw(startx,starty,stopx,stopy,col)

static struct spl_node *handler_epfb_linedraw(struct spl_task *task, void *data)
{
  struct epfb_linedraw line;
  line.startx = spl_clib_get_int(task);
  line.starty = spl_clib_get_int(task);
  line.stopx = spl_clib_get_int(task);
  line.stopy = spl_clib_get_int(task);
  line.col = spl_clib_get_int(task);

  if (ioctl(epfb_fd,EPFB_LINEDRAW,&line)==-1)
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "line draw ioctl failed\n");
  return 0;
}

/**
 * Rectangular blockfill from (x/y) with given width and height.
 */
// builtin epfb_blockfill(x,y,width,height,col)

static struct spl_node *handler_epfb_blockfill(struct spl_task *task, void *data)
{
  struct epfb_blockfill block;
  block.startx = spl_clib_get_int(task);
  block.starty = spl_clib_get_int(task);
  block.width = spl_clib_get_int(task);
  block.height = spl_clib_get_int(task);

  /*
  if (block.height <= 0)
	return 0;
  block.height--;
  */

  block.col = spl_clib_get_int(task);

  if (ioctl(epfb_fd,EPFB_BLOCKFILL,&block)==-1)
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "block fill ioctl failed\n");
  return 0;
}

/**
 * Renders text at (x,y) with given colour.
 */
// builtin epfb_drawtext(x,y,text,col)

static struct spl_node *handler_epfb_drawtext(struct spl_task *task, void *data)
{
  int x = spl_clib_get_int(task);
  int y = spl_clib_get_int(task);
  char* text = spl_clib_get_string(task);
  if (text == NULL)
  {
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "UTF8 text is NULL\n");
    return 0;
  }
  //else
  //  spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_DEBUG, task, "UTF8 text: %s\n",text);

  char* latin_1 = spl_utf8_export(text,"latin_1");
  if (latin_1 == NULL)
  {
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "Latin_1 text is NULL\n");
    return 0;
  }
  //else
  //  spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_DEBUG, task, "Latin_1 text: %s\n",latin_1);

  int col = spl_clib_get_int(task);
  epfb_draw_text(x,y,latin_1,col);
  free(latin_1);
  return 0;
}

/**
 * Setup Hardware Cursor.
 * The cursor must be enabled with [epfb_cursor_enable] as well.
 * The cursor position can be specified with [epfb_cursor_moveto].
 */
// builtin epfb_cursor_setup(width,height,col1,col2,blink1,blink2,blinkrate)

static struct spl_node *handler_epfb_cursor_setup(struct spl_task *task, void *data)
{
  struct epfb_cursor_setup cursor;
  cursor.width = spl_clib_get_int(task);
  cursor.height = spl_clib_get_int(task);
  cursor.col1 = spl_clib_get_int(task);
  cursor.col2 = spl_clib_get_int(task);
  cursor.blink1 = spl_clib_get_int(task);
  cursor.blink2 = spl_clib_get_int(task);
  cursor.blinkrate = spl_clib_get_int(task);

  if (ioctl(epfb_fd,EPFB_CURSOR_SETUP,&cursor)==-1)
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "setting up cursor failed\n");
  return 0;
}

/**
 * Set pixel value for hardware cursor.
 * The value must be in the range of 0 to 3 (00 to 11).
 * 0 means transparent, 1 means invert of video stream,
 * 2 means (blink) colour 1, 3 means (blink) colour 2.
 */
// builtin epfb_cursor_pixel(x,y,value)

static struct spl_node *handler_epfb_cursor_pixel(struct spl_task *task, void *data)
{
  struct epfb_cursor_pixelset pixel;
  pixel.x = spl_clib_get_int(task);
  pixel.y = spl_clib_get_int(task);
  pixel.value = spl_clib_get_int(task);
  if (ioctl(epfb_fd,EPFB_CURSOR_PIXELSET,&pixel)==-1)
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "failed setting cursor enable state\n");
  return 0;
}

/**
 * Enable or disable hardware cursor.
 * 1 is enable, 0 is disable.
 */
// builtin epfb_cursor_enable(enable)

static struct spl_node *handler_epfb_cursor_enable(struct spl_task *task, void *data)
{
  int enable = spl_clib_get_int(task);
  if (ioctl(epfb_fd,EPFB_CURSOR_ENABLE,&enable)==-1)
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "failed setting cursor enable state\n");
  return 0;
}


/**
 * Set Hardware Cursor Position on screen.
 */
// builtin epfb_cursor_moveto(x,y)

static struct spl_node *handler_epfb_cursor_moveto(struct spl_task *task, void *data)
{
  struct epfb_cursor_position pos;
  pos.x = spl_clib_get_int(task);
  pos.y = spl_clib_get_int(task);
  if (ioctl(epfb_fd,EPFB_CURSOR_MOVETO,&pos)==-1)
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "setting cursor position failed\n");
  return 0;
}

/**
 * Get Hardware Cursor Position on screen.
 */
// builtin epfb_cursor_position(x,y)

static struct spl_node *handler_epfb_cursor_position(struct spl_task *task, void *data)
{
  struct epfb_cursor_position pos;
  if (ioctl(epfb_fd,EPFB_CURSOR_POSITION,&pos)==-1)
  {
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "getting cursor position failed\n");
    return 0;
  }

  struct spl_node* position = spl_get(0);
  spl_create(task, position, "x",  SPL_NEW_INT(pos.x),  SPL_CREATE_LOCAL);
  spl_create(task, position, "y",  SPL_NEW_INT(pos.y),  SPL_CREATE_LOCAL);

  return position;
}

static struct spl_node *handler_epfb_copyimg(struct spl_task *task, void *data)
{
  struct epfb_copyimg img;
  img.srcptr = (void*)spl_clib_get_int(task);
  img.dest_x = spl_clib_get_int(task);
  img.dest_y = spl_clib_get_int(task);
  img.linesize = spl_clib_get_int(task);
  img.linestride = spl_clib_get_int(task);
  img.linecount = spl_clib_get_int(task);
  img.swap_rgb_order = spl_clib_get_int(task);

  if (ioctl(epfb_fd,EPFB_COPYIMG,&img)==-1)
  {
    spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task, "copying image failed\n");
  }
  return 0;
}

void SPL_ABI(spl_mod_epfb_init)(struct spl_vm *vm, struct spl_module *mod UNUSED, int restore UNUSED)
{
  epfb_fd = open("/dev/epfb0",0,O_RDWR);
  if (epfb_fd == -1)
    epfb_fd = open("/dev/epfb",0,O_RDWR);
  if (epfb_fd == -1)
  {
    // TODO: can we do SPL like error reporting here?
    printf("Failed to open the EPFB device file at /dev/epfb and /dev/epfb0, cannot load epfb module.\n");
    return;
  }
  epfb_init_fontbitmaps();

  spl_clib_reg(vm, "epfb_output_status", handler_epfb_output_status, 0);
  spl_clib_reg(vm, "epfb_output_enable", handler_epfb_output_enable, 0);
  spl_clib_reg(vm, "epfb_devinfo", handler_epfb_devinfo, 0);
  spl_clib_reg(vm, "epfb_clear", handler_epfb_clear, 0);
  spl_clib_reg(vm, "epfb_pixel", handler_epfb_pixelset, 0);
  spl_clib_reg(vm, "epfb_line", handler_epfb_linedraw, 0);
  spl_clib_reg(vm, "epfb_blockfill", handler_epfb_blockfill, 0);
  spl_clib_reg(vm, "epfb_text", handler_epfb_drawtext, 0);

  spl_clib_reg(vm, "epfb_cursor_setup", handler_epfb_cursor_setup, 0);
  spl_clib_reg(vm, "epfb_cursor_pixel", handler_epfb_cursor_pixel, 0);
  spl_clib_reg(vm, "epfb_cursor_enable", handler_epfb_cursor_enable, 0);
  spl_clib_reg(vm, "epfb_cursor_moveto", handler_epfb_cursor_moveto, 0);
  spl_clib_reg(vm, "epfb_cursor_position", handler_epfb_cursor_position, 0);

  spl_clib_reg(vm, "epfb_copyimage",handler_epfb_copyimg,0);
}

void SPL_ABI(spl_mod_epfb_done)(struct spl_vm *vm UNUSED, struct spl_module *mod UNUSED)
{
  return;
}