aboutsummaryrefslogtreecommitdiff
path: root/sdk-modifications/libsrc/console/console.c
blob: f455ac532222d7c271a07fa53cb51cbbcddd4551 (plain)
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
//console.c

#include <stdio.h>
#include <stdarg.h>
#include "ds2io.h"
#include "memory.h"
#include "font_dot.h"

#define STRING_SIZE		2048

#define CONSOLE_WIDTH	32
#define CONSOLE_HEIGHT	24
#define TAB_SIZE		3

static void ConsoleView(void);
static void ConsoleDrawfontall(void);

static int console_init_done = 0;
static unsigned short f_color;
static unsigned short b_color;
static enum SCREEN_ID console_id;

static int print_row;
static int print_col;
static int print_row_saved;
static int print_col_saved;

static unsigned char*	console_buf;
static unsigned int		console_buf_size;
static unsigned char*	console_buf_front;
static unsigned char*	console_buf_end;
static unsigned char*	console_print_header;
static unsigned short*	console_screen;
static unsigned char*	print_header_saved;

static void ConsoleFlush(void)
{
	unsigned short* screen_addr;
	enum SCREEN_ID id;

	if(console_id & UP_MASK) {
		screen_addr = up_screen_addr;
		id = UP_SCREEN;
	}
	else {
		screen_addr = down_screen_addr;
		id = DOWN_SCREEN;
	}

	memcpy((void*)screen_addr, (void*)console_screen, SCREEN_WIDTH*SCREEN_HEIGHT*2);
	ds2_flipScreen(id, 1);
}

static void ConsoleClearscreen(void)
{
	unsigned short *scr;
	unsigned int i;

	scr = console_screen;
	i = 0;
	while(i < SCREEN_WIDTH*SCREEN_HEIGHT)
		scr[i++] = b_color;
}

static void ConsoleMovewin(int dir, int sw_screen)
{
	unsigned char *pt;
	
	if(dir || sw_screen)
		ConsoleClearscreen();

	//switch to another screen to dispaly text
	if(sw_screen)
	{
		ConsoleFlush();
		//now up screen
		if(console_id & UP_MASK)	{
			console_screen = down_screen_addr;
			console_id = DOWN_SCREEN;
		}
		//switch to up screen
		else
		{
			console_screen = up_screen_addr;
			console_id = UP_SCREEN;
		}
	}

	pt = console_print_header + dir*CONSOLE_WIDTH;

	//screen scroll down
	if(dir > 0)
	{
		if(console_buf_end > console_print_header) {
			if(pt > console_buf_end)
				pt = console_buf_end;
			console_print_header = pt;
		}
		else if(console_buf_end < console_print_header)	{
			if((pt - console_buf) >= console_buf_size) {
				pt -= console_buf_size;
				if(pt > console_buf_end)
					pt = console_buf_end;
			}
			console_print_header = pt;
		}
	}
	//screen scroll up
	else if(dir < 0)
	{
		if(console_buf_front > console_print_header) {
			if(pt < console_buf) {
				pt += console_buf_size;
				if(pt < console_buf_front)
					pt = console_buf_front;
			}
			console_print_header = pt;
		}
		else if(console_buf_front < console_print_header) {
			if(pt < console_buf_front)
				pt = console_buf_front;
			console_print_header = pt;
		}
	}

	if(dir || sw_screen)
	{
		print_row_saved = 0;	//redraw entire screen
		print_col_saved = 0;

		ConsoleDrawfontall();
		ConsoleFlush();
	}
}

void ConsoleClr(int mode)
{
	unsigned char *pt, *pt_end;
	unsigned int i;

	//Clear current screen buffer
	if(0 == mode)
	{
		if(print_col > 0) {
			console_buf_end += CONSOLE_WIDTH;
			if((console_buf_end - console_buf) >= console_buf_size)
				console_buf_end -= console_buf_size;
		}

		console_print_header = console_buf_end;
		print_row = 0;
		print_col = 0;
		print_row_saved = 0;
		print_col_saved = 0;
	}
	//Clear all
	else if(1 == mode)
	{
		console_buf_front = console_buf;
		console_buf_end = console_buf;
		console_print_header = console_buf;
		print_row = 0;
		print_col = 0;
		print_row_saved = 0;
		print_col_saved = 0;

		pt = console_buf;
		pt_end = console_buf + console_buf_size;
		while(pt < pt_end)
		{
			pt[0] = '\0';
			pt += CONSOLE_WIDTH;
		}
	}

	ConsoleClearscreen();
	ConsoleFlush();
}

//Draw part of the screen
static void ConsoleDrawfont(void)
{
	unsigned char *pt, *dot_map;
	unsigned char ch, dot;
	unsigned short *dst, *pt_r;
	unsigned int row, col;
	unsigned int x, j, k;

	pt_r = console_screen;
	row = print_row_saved;
	col = print_col_saved;
	x = col*8;

	pt = console_print_header + row * CONSOLE_WIDTH;

	while(row != print_row || col != print_col)
	{
		ch = pt[col++] & 0x7F;
		//'\n'
		if('\n' == ch || '\0' == ch || col > CONSOLE_WIDTH) {
			pt += CONSOLE_WIDTH;
			if((pt - console_buf) >= console_buf_size) pt -= console_buf_size;
			col = 0;
			row += 1;
			x = 0;
		}
		//character not '\n' nor '\0'
		else {
			dot_map = (unsigned char*)font_map[ch];

			for(j= 0; j < 8; j++)
			{
				dot = *dot_map++;
				dst = pt_r + (row*8+j)*SCREEN_WIDTH + x;
				for(k = 0; k < 8; k++)
					*dst++ = (dot & (0x80>>k)) ? f_color : b_color;
			}
			x += 8;
		}
	}
}

//Redraw the hole screen
static void ConsoleDrawfontall(void)
{
	unsigned char *pt, *end_pt, *dot_map;
	unsigned int i, j, k;
	unsigned char ch, dot;
	unsigned short *dst, *pt_r;
	unsigned int x, y;

	//Clear screen to b_color
	pt_r = console_screen;
	i = 0;
	while(i < SCREEN_WIDTH*SCREEN_HEIGHT)
		pt_r[i++] = b_color;

	pt = console_print_header;
	end_pt = console_buf_end;
	x = 0;
	y = 0;
	i = 0;
	while(pt != end_pt)
	{
		ch = pt[i++] & 0x7F;
		//'\n'
		if('\n' == ch || '\0' == ch || i > CONSOLE_WIDTH) {
			pt += CONSOLE_WIDTH;
			if((pt - console_buf) >= console_buf_size) pt -= console_buf_size;
			i = 0;
			x = 0;
			y += 1;
			if(y >= CONSOLE_HEIGHT) break;
		}
		//character not '\n' nor '\0'
		else {
			dot_map = (unsigned char*)font_map[ch];

			for(j= 0; j < 8; j++)
			{
				dot = *dot_map++;
				dst = pt_r + (y*8+j)*SCREEN_WIDTH + x;
				for(k = 0; k < 8; k++)
					*dst++ = (dot & (0x80>>k)) ? f_color : b_color;
			}
			x += 8;
		}
	}
}

static void ConsoleNewline(void)
{
	print_row += 1;
	if(print_row >= CONSOLE_HEIGHT)
	{
		print_row -= 1;
		console_print_header += CONSOLE_WIDTH;
		if((console_print_header - console_buf) >= console_buf_size)
			console_print_header = console_buf;

		print_row_saved = 0;
		print_col_saved = 0;

		ConsoleClearscreen();
	}

	console_buf_end += CONSOLE_WIDTH;
	if((console_buf_end - console_buf) >= console_buf_size)
		console_buf_end = console_buf;

	//scrollback
	if(console_buf_end == console_buf_front)
	{
		console_buf_front += CONSOLE_WIDTH;
		if((console_buf_front - console_buf) >= console_buf_size)
			console_buf_front = console_buf;

		console_buf_end[0] = '\0';
	}
}

static void ConsolePrintchar(unsigned char ch)
{
	int i;

	if(print_col >= CONSOLE_WIDTH) {
		print_col = 0;
		ConsoleNewline();
	}

	switch(ch) {
		case 9:									//'\t'
			if((print_col + TAB_SIZE) < CONSOLE_WIDTH)
			{
				i = print_col % TAB_SIZE;
				i = TAB_SIZE - i;
				while(i--)
				{
					console_buf_end[print_col] = ' ';
					print_col += 1;
				}
			}
			break;
		case 10:								//'\n'
		case 13:								//'\r'
			console_buf_end[print_col] = '\n';
			print_col = 0;
			ConsoleNewline();
			break;
		default:
			console_buf_end[print_col] = ch;
			if(ch != '\0')
				print_col += 1;
			break;
	}
}

void ConsolePrintstring(unsigned char* string)
{
	unsigned char *pt;
	unsigned char ch;

	print_row_saved = print_row;
	print_col_saved = print_col;
	console_print_header = print_header_saved;
//cprintf("print_row %d; print_col %d; [%s]\n", print_row, print_col, string);
	pt = string;
	do
	{
		ch = *pt++;
		ConsolePrintchar(ch);
	}
	while ('\0' != ch);

	print_header_saved = console_print_header;

	ConsoleDrawfont();
	ConsoleFlush();
}

//---------------------------------------------------------------------------------
//parameter:
//	front_color: color of character
//	background_color: background color
//	screen: UP-up screen used as output, DOWN-down screen used as output, 
//			using only one screen at a time
//	buf_size: buffer size to hold the output, it's unit is screen
//---------------------------------------------------------------------------------
int ConsoleInit(unsigned short front_color, unsigned short background_color, enum SCREEN_ID screen, unsigned int buf_size)
{
	unsigned char *pt;
	unsigned int size;
	unsigned short **scr_ppt;

	console_init_done = 0;
	f_color = front_color;
	b_color = background_color;


	//Using only one screen at a time
	if(screen & UP_MASK)
		console_id = UP_SCREEN;
	else
		console_id = DOWN_SCREEN;

	if(!buf_size) buf_size = 1;
	size = buf_size *CONSOLE_WIDTH *CONSOLE_HEIGHT;

	pt = (unsigned char*)Drv_alloc(size);
	if(NULL == pt)
		return -1;							//there is something error

	console_buf = pt;
	memset(console_buf, 0, size);

	print_row = 0;
	print_col = 0;
	print_row_saved = print_row;
	print_col_saved = print_col;
	console_buf_size = size;
	console_buf_front = console_buf;
	console_buf_end = console_buf;
	console_print_header = console_buf;
	print_header_saved = console_print_header;

	console_screen = (unsigned short*)Drv_alloc(SCREEN_WIDTH * SCREEN_HEIGHT*2);
	if(NULL == console_screen) {
		Drv_deAlloc((void*)console_buf);
		return -1;
	}

	ConsoleClr(1);

	console_init_done = 1;

	regist_escape_key(ConsoleView, (KEY_L | KEY_R | KEY_A | KEY_B | KEY_X));
	return 0;
}

void ConsoleView(void)
{
	unsigned int key;

cprintf("enter console mode\n");

	do {
		key = getKey();

		switch(key)
		{
			//screen scroll down 1 line
			case KEY_UP:
					ConsoleMovewin(-1, 0);
				break;

			//screen scroll up 1 line
			case KEY_DOWN:
					ConsoleMovewin(1, 0);
				break;

			//screen scroll done (CONSOLE_HEIGHT-1) line
			case KEY_LEFT:
					ConsoleMovewin(-(CONSOLE_HEIGHT-1), 0);
				break;

			//screen scroll up (CONSOLE_HEIGHT-1) line
			case KEY_RIGHT:
					ConsoleMovewin(CONSOLE_HEIGHT-1, 0);
				break;

			//switch screen
			case KEY_B:
cprintf("switch to another screen\n");
					ConsoleMovewin(0, 1);
				break;

			default: break;
		}

		mdelay(20);
	} while(key != KEY_Y);

cprintf("exit console mode\n");
}

int printf(const char *format, ...)
{
	char string[STRING_SIZE];
	int ret;
	va_list ap;

	if(!console_init_done)
		return 0;

	va_start (ap, format);
	ret = vsnprintf(string, STRING_SIZE, format, ap);
	va_end (ap);

	ConsolePrintstring(string);

	return ret;
}