summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/i386/go32/console/outch.c
blob: 07cfc0625b391614a8410cae90f7f6355864b980 (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
/*
 *  $Id$
 */

#include <go32.h>
#include <bsp.h>

#include <memory.h>

#define	MAX_COL	80
#define MAX_ROW	50

static unsigned		nrow		= 25;
static unsigned		ncol		= 80;
static unsigned short *	tvram		= TVRAM;
static unsigned char	current_col	= 0;
static unsigned char	current_row	= 0;
static unsigned short	screen_copy[ MAX_ROW*MAX_COL ];

static void init_cons( void );

/*
 * set_cursor_pos()
 *	Set cursor position based on current absolute screen offset
 */
static void
set_cursor_pos(void)
{
    register unsigned short gdc_pos = current_row * ncol + current_col;
    outport_byte( GDC_REG_PORT, 0xe );
    outport_byte( GDC_VAL_PORT, (gdc_pos >> 8) & 0xff );
    outport_byte( GDC_REG_PORT, 0xf );
    outport_byte( GDC_VAL_PORT, gdc_pos & 0xff );
}

/*
 * scroll_up()
 *	Scroll screen up one line
 */
static void
scroll_up( unsigned short * tv, unsigned short * copy, unsigned int lines )
{
    if ( lines > nrow )
	lines = nrow;

    /* move everything up */
    memmove( copy, copy+ncol*lines, (nrow-lines)*ncol*sizeof copy[0] );

    /* fill bottom with blanks */
    {
	int loop = ncol*lines;
	unsigned short * ptr = copy + ncol*(nrow-lines);
	while ( --loop >= 0 )
	    *ptr++ = (WHITE<<8) | ' ';
    }

    /* copy new screen to video buffer */
    dosmemput( copy, nrow*ncol*sizeof copy[0], (int)tv );
}


/*
 * PUT()
 *	Write character at current screen location
 */
inline static void PUT( char c )
{
    unsigned short loc = current_row*ncol+current_col;
    unsigned short val = (WHITE<<8) | c;
    screen_copy[loc] = val;
    dosmemput( &screen_copy[loc], sizeof screen_copy[0], (int)(tvram+loc) );
}

/*
 * cons_putc()
 *	Place a character on next screen position
 */
static void
cons_putc( unsigned char c )
{
    static int first = 1;
    if ( first )  {
	init_cons();
	first = 0;
    }

	switch (c) {
	case '\t':
		while ( current_row % 8 )
			cons_putc(' ');
		break;
	case '\r':
		current_col = 0;
		break;
	case '\n':
		if ( ++current_row >= nrow ) {
		        scroll_up( tvram, screen_copy, 1 );
			current_row -= 1;
		}
		break;
	case '\b':
		if ( current_col > 0 ) {
			--current_col;
			PUT(' ');
		}
		break;
	default:
		PUT(c);
		current_col += 1;
		if ( current_col >= ncol ) {
			current_col = 0;
			current_row += 1;
			if ( current_row >= nrow ) {
			        scroll_up( tvram, screen_copy, 1 );
				current_row -= 1;
			}
		}
	};
	set_cursor_pos();
}


/*
 * init_cons()
 *	Hook for any early setup
 */
static void
init_cons( void )
{
#if 0
    /* Get a copy of original screen */
    dosmemget( (int)tvram, nrow*ncol*sizeof *tvram, screen_copy );
#else
    /* Clear entire screen */
    scroll_up( tvram, screen_copy, nrow );
#endif
}



void _IBMPC_outch( unsigned char ch )
{
    extern int _IBMPC_Use_Go32_IO;

    if ( _IBMPC_Use_Go32_IO )  {
	write( 1, &ch, 1 );
    } else {
	cons_putc( ch );
    }
}