summaryrefslogtreecommitdiffstats
path: root/bsps/arm/raspberrypi/console/outch.c
blob: 20601384c737781354bcb5171f19647125d58e30 (plain) (blame)
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
/**
 * @file
 *
 * @ingroup raspberrypi
 *
 * @brief displaying characters on the console
 */

/**
 *
 * Copyright (c) 2015 Yang Qiao
 * based on work by:
 * Copyright (C) 1998  Eric Valette (valette@crf.canon.fr)
 *                     Canon Centre Recherche France.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 *
 * Till Straumann <strauman@slac.stanford.edu>, 2003/9:
 *  - added handling of basic escape sequences (cursor movement
 *    and erasing; just enough for the line editor 'libtecla' to
 *    work...)
 *
 */

#include <bsp.h>
#include <bsp/vc.h>
#include <bsp/rpi-fb.h>
#include <rtems/fb.h>

#include <stdlib.h>
#include <string.h>
#include "font_data.h"

static void wr_cursor(
  int           r,
  int           c
)
{
  /* dummy function for now */
}

#define TAB_SPACE 4
#define CONSOLE_BG_COL 0x00
#define CONSOLE_FG_COL 0xa0

static void          *fb_mem = NULL;
static unsigned short maxCol;
static unsigned short maxRow;
static unsigned short bytes_per_pixel;
static unsigned int   bytes_per_line;
static unsigned int   bytes_per_char_line;
static unsigned char  row;
static unsigned char  column;
static unsigned int   nLines;
static uint32_t       fgx, bgx, eorx;
static int            rpi_video_initialized;

static const int video_font_draw_table32[ 16 ][ 4 ] = {
  { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
  { 0x00000000, 0x00000000, 0x00000000, 0x00ffffff },
  { 0x00000000, 0x00000000, 0x00ffffff, 0x00000000 },
  { 0x00000000, 0x00000000, 0x00ffffff, 0x00ffffff },
  { 0x00000000, 0x00ffffff, 0x00000000, 0x00000000 },
  { 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff },
  { 0x00000000, 0x00ffffff, 0x00ffffff, 0x00000000 },
  { 0x00000000, 0x00ffffff, 0x00ffffff, 0x00ffffff },
  { 0x00ffffff, 0x00000000, 0x00000000, 0x00000000 },
  { 0x00ffffff, 0x00000000, 0x00000000, 0x00ffffff },
  { 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000 },
  { 0x00ffffff, 0x00000000, 0x00ffffff, 0x00ffffff },
  { 0x00ffffff, 0x00ffffff, 0x00000000, 0x00000000 },
  { 0x00ffffff, 0x00ffffff, 0x00000000, 0x00ffffff },
  { 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00000000 },
  { 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff }
};

static void scroll( void )
{
  int      i, j;      /* Counters */
  uint8_t *pt_scroll, *pt_bitmap;  /* Pointers on the bit-map  */

  pt_bitmap = fb_mem;
  j = 0;
  pt_bitmap = pt_bitmap + j;
  pt_scroll = pt_bitmap + bytes_per_char_line;

  for ( i = j; i < maxRow - 1; i++ ) {
    memcpy( pt_bitmap, pt_scroll, bytes_per_char_line );
    pt_bitmap = pt_bitmap + bytes_per_char_line;
    pt_scroll = pt_bitmap + bytes_per_char_line;
  }

  /*
   * Blank characters are displayed on the last line.
   */
  memset( pt_bitmap, 0, bytes_per_char_line );
}

static void doCRNL(
  int cr,
  int nl
)
{
  if ( nl ) {
    if ( ++row == maxRow ) {
      scroll();   /* Scroll the screen now */
      row = maxRow - 1;
    }

    nLines++;
  }

  if ( cr )
    column = 0;

  /* Move cursor on the next location  */
  if ( cr || nl ) {
    wr_cursor( row, column );
  }
}

static void advanceCursor( void )
{
  if ( ++column == maxCol )
    doCRNL( 1, 1 );
  else
    wr_cursor( row, column );
}

static void gotorc(
  int r,
  int c
)
{
  column = c;
  row = r;
  wr_cursor( row, column );
}

static void video_drawchars(
  int           r,
  int           c,
  unsigned char ch
)
{
  if ( fb_mem == NULL ) {
    return;
  }

  uint8_t *cdat, *dest, *dest0;
  int      rows, offset;

  offset = r * bytes_per_char_line + c * bytes_per_pixel * RPI_FONT_WIDTH;
  dest0 = fb_mem + offset;

  /*
   * only 32-bit per pixel format is supported for now
   */
  cdat = rpi_font + ch * RPI_FONT_HEIGHT;

  for ( rows = RPI_FONT_HEIGHT, dest = dest0;
        rows--; dest += bytes_per_line ) {
    uint8_t bits = *cdat++;

    ( (uint32_t *) dest )[ 0 ] =
      ( video_font_draw_table32
        [ bits >> 4 ][ 0 ] & eorx ) ^ bgx;
    ( (uint32_t *) dest )[ 1 ] =
      ( video_font_draw_table32
        [ bits >> 4 ][ 1 ] & eorx ) ^ bgx;
    ( (uint32_t *) dest )[ 2 ] =
      ( video_font_draw_table32
        [ bits >> 4 ][ 2 ] & eorx ) ^ bgx;
    ( (uint32_t *) dest )[ 3 ] =
      ( video_font_draw_table32
        [ bits >> 4 ][ 3 ] & eorx ) ^ bgx;

    ( (uint32_t *) dest )[ 4 ] =
      ( video_font_draw_table32
        [ bits & 15 ][ 0 ] & eorx ) ^ bgx;
    ( (uint32_t *) dest )[ 5 ] =
      ( video_font_draw_table32
        [ bits & 15 ][ 1 ] & eorx ) ^ bgx;
    ( (uint32_t *) dest )[ 6 ] =
      ( video_font_draw_table32
        [ bits & 15 ][ 2 ] & eorx ) ^ bgx;
    ( (uint32_t *) dest )[ 7 ] =
      ( video_font_draw_table32
        [ bits & 15 ][ 3 ] & eorx ) ^ bgx;
  }
}

#define ESC ( (char) 27 )
/* erase current location without moving the cursor */
#define BLANK ( (char) 0x7f )

static void videoPutChar( char ch )
{
  switch ( ch ) {
    case '\b': {
      if ( column )
        column--;

      /* Move cursor on the previous location  */
      wr_cursor( row, column );
      return;
    }
    case '\t': {
      int i;

      i = TAB_SPACE - ( column & ( TAB_SPACE - 1 ) );

      while ( i-- ) {

        video_drawchars( row, column, ' ' );
        column += 1;

        if ( column >= maxCol ) {
          doCRNL( 1, 1 );
          return;
        }
      }

      wr_cursor( row, column );

      return;
    }
    case '\n': {
      doCRNL( 0, 1 );
      return;
    }
    case 7:   {     /* Bell code must be inserted here */
      return;
    }
    case '\r': {
      doCRNL( 1, 0 );
      return;
    }
    case BLANK: {
      video_drawchars( row, column, ' ' );

      wr_cursor( row, column );

      return;
    }
    default: {
      // *pt_bitmap = (unsigned char)ch | attribute;
      video_drawchars( row, column, ch );
      advanceCursor();
      return;
    }
  }
}

/* trivial state machine to handle escape sequences:
 *
 *                    ---------------------------------
 *                   |                                 |
 *                   |                                 |
 * KEY:        esc   V    [          DCABHKJ       esc |
 * STATE:   0 -----> 27 -----> '[' ----------> -1 -----
 *          ^\        \          \               \
 * KEY:     | \other   \ other    \ other         \ other
 *           <-------------------------------------
 *
 * in state '-1', the DCABHKJ cases are handled
 *
 * (cursor motion and screen clearing)
 */

#define DONE ( -1 )

static int handleEscape(
  int  oldState,
  char ch
)
{
  int rval = 0;
  int ro, co;

  switch ( oldState ) {
    case DONE:  /*  means the previous char terminated an ESC sequence... */
    case 0:

      if ( 27 == ch ) {
        rval = 27;   /* START of an ESC sequence */
      }

      break;

    case 27:

      if ( '[' == ch ) {
        rval = ch;  /* received ESC '[', so far */
      } else {
        /* dump suppressed 'ESC'; outch will append the char */
        videoPutChar( ESC );
      }

      break;

    case '[':
      /* handle 'ESC' '[' sequences here */
      ro = row;
      co = column;
      rval = DONE; /* done */

      switch ( ch ) {
        case 'D': /* left */

          if ( co > 0 )
            co--;

          break;
        case 'C': /* right */

          if ( co < maxCol )
            co++;

          break;
        case 'A': /* up    */

          if ( ro > 0 )
            ro--;

          break;
        case 'B': /* down */

          if ( ro < maxRow )
            ro++;

          break;
        case 'H': /* home */
          ro = co = 0;
          break;
        case 'K': /* clear to end of line */

          while ( column < maxCol - 1 )
            videoPutChar( ' ' );

          videoPutChar( BLANK );
          break;
        case 'J':     /* clear to end of screen */

          while ( ( ( row < maxRow - 1 ) || ( column < maxCol - 1 ) ) )
            videoPutChar( ' ' );

          videoPutChar( BLANK );
          break;
        default:
          videoPutChar( ESC );
          videoPutChar( '[' );
          /* DONT move the cursor */
          ro = -1;
          rval = 0;
          break;
      }

      // /* reset cursor */
      if ( ro >= 0 )
        gotorc( ro, co );

    default:
      break;
  }

  return rval;
}

static void clear_screen( void )
{
  int i, j;

  for ( j = 0; j < maxRow; j++ ) {
    for ( i = 0; i < maxCol; i++ ) {
      videoPutChar( ' ' );
    }
  }

  column = 0;
  row = 0;
}

void rpi_fb_outch( char c )
{
  static int escaped = 0;

  if ( !( escaped = handleEscape( escaped, c ) ) ) {
    if ( '\n' == c )
      videoPutChar( '\r' );

    videoPutChar( c );
  }
}

void rpi_video_init( void )
{
  int ret = rpi_fb_init();

  if ( ( ret != RPI_FB_INIT_OK ) &&
       ( ret != RPI_FB_INIT_ALREADY_INITIALIZED ) ) {
    rpi_video_initialized = 0;
    return;
  }

  struct fb_var_screeninfo fb_var_info;
  struct fb_fix_screeninfo fb_fix_info;
  rpi_get_var_screen_info( &fb_var_info );
  rpi_get_fix_screen_info( &fb_fix_info );
  maxCol = fb_var_info.xres / RPI_FONT_WIDTH;
  maxRow = fb_var_info.yres / RPI_FONT_HEIGHT;
  bytes_per_pixel = fb_var_info.bits_per_pixel / 8;
  bytes_per_line = bytes_per_pixel * fb_var_info.xres;
  bytes_per_char_line = RPI_FONT_HEIGHT * bytes_per_line;
  fb_mem = RTEMS_DEVOLATILE( void *, fb_fix_info.smem_start );
  column = 0;
  row = 0;
  nLines = 0;
  fgx = ( CONSOLE_FG_COL << 24 ) |
        ( CONSOLE_FG_COL << 16 ) |
        ( CONSOLE_FG_COL << 8 ) |
        CONSOLE_FG_COL;
  bgx = ( CONSOLE_BG_COL << 24 ) |
        ( CONSOLE_BG_COL << 16 ) |
        ( CONSOLE_BG_COL << 8 ) |
        CONSOLE_BG_COL;
  eorx = fgx ^ bgx;
  clear_screen();
  rpi_video_initialized = 1;
}

int rpi_video_is_initialized( void )
{
  return rpi_video_initialized;
}

/* for old DOS compatibility n-curses type of applications */
void gotoxy(
  int x,
  int y
);
int whereX( void );
int whereY( void );

void gotoxy(
  int x,
  int y
)
{
  gotorc( y, x );
}

int whereX( void )
{
  return row;
}

int whereY( void )
{
  return column;
}