Loading...
  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
630
631
/*
 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */

#include <vc.h>

#include <console/video_console.h>
#include <kdp/kdp_udp.h>
#include <kern/debug.h>

#include "panic_image.c"
#include "rendered_numbers.c"

extern struct vc_info vinfo;
extern boolean_t panicDialogDesired;

/* panic image clut */
static const unsigned char *clut = NULL;
extern void panic_ui_initialize(const unsigned char * system_clut);

/* We use this standard MacOS clut as a fallback */
static const unsigned char appleClut8[ 256 * 3 ] = { 
// 00
	0xFF,0xFF,0xFF, 0xFF,0xFF,0xCC,	0xFF,0xFF,0x99,	0xFF,0xFF,0x66,
	0xFF,0xFF,0x33, 0xFF,0xFF,0x00,	0xFF,0xCC,0xFF,	0xFF,0xCC,0xCC,
	0xFF,0xCC,0x99, 0xFF,0xCC,0x66,	0xFF,0xCC,0x33,	0xFF,0xCC,0x00,
	0xFF,0x99,0xFF, 0xFF,0x99,0xCC,	0xFF,0x99,0x99,	0xFF,0x99,0x66,
// 10
	0xFF,0x99,0x33, 0xFF,0x99,0x00,	0xFF,0x66,0xFF,	0xFF,0x66,0xCC,
	0xFF,0x66,0x99, 0xFF,0x66,0x66,	0xFF,0x66,0x33,	0xFF,0x66,0x00,
	0xFF,0x33,0xFF, 0xFF,0x33,0xCC,	0xFF,0x33,0x99,	0xFF,0x33,0x66,
	0xFF,0x33,0x33, 0xFF,0x33,0x00,	0xFF,0x00,0xFF,	0xFF,0x00,0xCC,
// 20
	0xFF,0x00,0x99, 0xFF,0x00,0x66,	0xFF,0x00,0x33,	0xFF,0x00,0x00,
	0xCC,0xFF,0xFF, 0xCC,0xFF,0xCC,	0xCC,0xFF,0x99,	0xCC,0xFF,0x66,
	0xCC,0xFF,0x33, 0xCC,0xFF,0x00,	0xCC,0xCC,0xFF,	0xCC,0xCC,0xCC,
	0xCC,0xCC,0x99, 0xCC,0xCC,0x66,	0xCC,0xCC,0x33,	0xCC,0xCC,0x00,
// 30
	0xCC,0x99,0xFF, 0xCC,0x99,0xCC,	0xCC,0x99,0x99,	0xCC,0x99,0x66,
	0xCC,0x99,0x33, 0xCC,0x99,0x00,	0xCC,0x66,0xFF,	0xCC,0x66,0xCC,
	0xCC,0x66,0x99, 0xCC,0x66,0x66,	0xCC,0x66,0x33,	0xCC,0x66,0x00,
	0xCC,0x33,0xFF, 0xCC,0x33,0xCC,	0xCC,0x33,0x99,	0xCC,0x33,0x66,
// 40
	0xCC,0x33,0x33, 0xCC,0x33,0x00,	0xCC,0x00,0xFF,	0xCC,0x00,0xCC,
	0xCC,0x00,0x99, 0xCC,0x00,0x66,	0xCC,0x00,0x33,	0xCC,0x00,0x00,
	0x99,0xFF,0xFF, 0x99,0xFF,0xCC,	0x99,0xFF,0x99,	0x99,0xFF,0x66,
	0x99,0xFF,0x33, 0x99,0xFF,0x00,	0x99,0xCC,0xFF,	0x99,0xCC,0xCC,
// 50
	0x99,0xCC,0x99, 0x99,0xCC,0x66,	0x99,0xCC,0x33,	0x99,0xCC,0x00,
	0x99,0x99,0xFF, 0x99,0x99,0xCC,	0x99,0x99,0x99,	0x99,0x99,0x66,
	0x99,0x99,0x33, 0x99,0x99,0x00,	0x99,0x66,0xFF,	0x99,0x66,0xCC,
	0x99,0x66,0x99, 0x99,0x66,0x66,	0x99,0x66,0x33,	0x99,0x66,0x00,
// 60
	0x99,0x33,0xFF, 0x99,0x33,0xCC,	0x99,0x33,0x99,	0x99,0x33,0x66,
	0x99,0x33,0x33, 0x99,0x33,0x00,	0x99,0x00,0xFF,	0x99,0x00,0xCC,
	0x99,0x00,0x99, 0x99,0x00,0x66,	0x99,0x00,0x33,	0x99,0x00,0x00,
	0x66,0xFF,0xFF, 0x66,0xFF,0xCC,	0x66,0xFF,0x99,	0x66,0xFF,0x66,
// 70
	0x66,0xFF,0x33, 0x66,0xFF,0x00,	0x66,0xCC,0xFF,	0x66,0xCC,0xCC,
	0x66,0xCC,0x99, 0x66,0xCC,0x66,	0x66,0xCC,0x33,	0x66,0xCC,0x00,
	0x66,0x99,0xFF, 0x66,0x99,0xCC,	0x66,0x99,0x99,	0x66,0x99,0x66,
	0x66,0x99,0x33, 0x66,0x99,0x00,	0x66,0x66,0xFF,	0x66,0x66,0xCC,
// 80
	0x66,0x66,0x99, 0x66,0x66,0x66,	0x66,0x66,0x33,	0x66,0x66,0x00,
	0x66,0x33,0xFF, 0x66,0x33,0xCC,	0x66,0x33,0x99,	0x66,0x33,0x66,
	0x66,0x33,0x33, 0x66,0x33,0x00,	0x66,0x00,0xFF,	0x66,0x00,0xCC,
	0x66,0x00,0x99, 0x66,0x00,0x66,	0x66,0x00,0x33,	0x66,0x00,0x00,
// 90
	0x33,0xFF,0xFF, 0x33,0xFF,0xCC,	0x33,0xFF,0x99,	0x33,0xFF,0x66,
	0x33,0xFF,0x33, 0x33,0xFF,0x00,	0x33,0xCC,0xFF,	0x33,0xCC,0xCC,
	0x33,0xCC,0x99, 0x33,0xCC,0x66,	0x33,0xCC,0x33,	0x33,0xCC,0x00,
	0x33,0x99,0xFF, 0x33,0x99,0xCC,	0x33,0x99,0x99,	0x33,0x99,0x66,
// a0
	0x33,0x99,0x33, 0x33,0x99,0x00,	0x33,0x66,0xFF,	0x33,0x66,0xCC,
	0x33,0x66,0x99, 0x33,0x66,0x66,	0x33,0x66,0x33,	0x33,0x66,0x00,
	0x33,0x33,0xFF, 0x33,0x33,0xCC,	0x33,0x33,0x99,	0x33,0x33,0x66,
	0x33,0x33,0x33, 0x33,0x33,0x00,	0x33,0x00,0xFF,	0x33,0x00,0xCC,
// b0
	0x33,0x00,0x99, 0x33,0x00,0x66,	0x33,0x00,0x33,	0x33,0x00,0x00,
	0x00,0xFF,0xFF, 0x00,0xFF,0xCC,	0x00,0xFF,0x99,	0x00,0xFF,0x66,
	0x00,0xFF,0x33, 0x00,0xFF,0x00,	0x00,0xCC,0xFF,	0x00,0xCC,0xCC,
	0x00,0xCC,0x99, 0x00,0xCC,0x66,	0x00,0xCC,0x33,	0x00,0xCC,0x00,
// c0
	0x00,0x99,0xFF, 0x00,0x99,0xCC,	0x00,0x99,0x99,	0x00,0x99,0x66,
	0x00,0x99,0x33, 0x00,0x99,0x00,	0x00,0x66,0xFF,	0x00,0x66,0xCC,
	0x00,0x66,0x99, 0x00,0x66,0x66,	0x00,0x66,0x33,	0x00,0x66,0x00,
	0x00,0x33,0xFF, 0x00,0x33,0xCC,	0x00,0x33,0x99,	0x00,0x33,0x66,
// d0
	0x00,0x33,0x33, 0x00,0x33,0x00,	0x00,0x00,0xFF,	0x00,0x00,0xCC,
	0x00,0x00,0x99, 0x00,0x00,0x66,	0x00,0x00,0x33,	0xEE,0x00,0x00,
	0xDD,0x00,0x00, 0xBB,0x00,0x00,	0xAA,0x00,0x00,	0x88,0x00,0x00,
	0x77,0x00,0x00, 0x55,0x00,0x00,	0x44,0x00,0x00,	0x22,0x00,0x00,
// e0
	0x11,0x00,0x00, 0x00,0xEE,0x00,	0x00,0xDD,0x00,	0x00,0xBB,0x00,
	0x00,0xAA,0x00, 0x00,0x88,0x00,	0x00,0x77,0x00,	0x00,0x55,0x00,
	0x00,0x44,0x00, 0x00,0x22,0x00,	0x00,0x11,0x00,	0x00,0x00,0xEE,
	0x00,0x00,0xDD, 0x00,0x00,0xBB,	0x00,0x00,0xAA,	0x00,0x00,0x88,
// f0
	0x00,0x00,0x77, 0x00,0x00,0x55,	0x00,0x00,0x44,	0x00,0x00,0x22,
	0x00,0x00,0x11, 0xEE,0xEE,0xEE,	0xDD,0xDD,0xDD,	0xBB,0xBB,0xBB,
	0xAA,0xAA,0xAA, 0x88,0x88,0x88,	0x77,0x77,0x77,	0x55,0x55,0x55,
	0x44,0x44,0x44, 0x22,0x22,0x22,	0x11,0x11,0x11,	0x00,0x00,0x00
};


/* panic dialog and info saving */
static int mac_addr_digit_x;
static int mac_addr_digit_y;
static void blit_digit( int digit );
static boolean_t panicDialogDrawn = FALSE;

static void 
panic_blit_rect(	unsigned int x, unsigned int y,
			unsigned int width, unsigned int height,
			int transparent, unsigned char * dataPtr );
			
static void 
panic_blit_rect_8(	unsigned int x, unsigned int y,
			unsigned int width, unsigned int height,
			int transparent, unsigned char * dataPtr );
			
static void 
panic_blit_rect_16(	unsigned int x, unsigned int y,
			unsigned int width, unsigned int height,
			int transparent, unsigned char * dataPtr );
			
static void 
panic_blit_rect_32(	unsigned int x, unsigned int y,
			unsigned int width, unsigned int height,
			int transparent, unsigned char * dataPtr );

static void 
dim_screen(void);

static void 
dim_screen_16(void);

static void 
dim_screen_32(void);

static int 
decode_rle( unsigned char * dataPtr, unsigned int * quantity, unsigned int * value );

void 
panic_ui_initialize(const unsigned char * system_clut)
{
	clut = system_clut;
}

void 
draw_panic_dialog( void )
{
	int pd_x,pd_y, iconx, icony, tx_line, tx_col;
	int line_width = 1;
	int f1, f2, d1, d2, d3, rem;
	char *pair = "ff";
	int count = 0;
	char digit;
	int nibble;
	char colon = ':';
	char dot = '.';
	struct ether_addr kdp_mac_addr  = kdp_get_mac_addr();
	unsigned int ip_addr = (unsigned int) ntohl(kdp_get_ip_address());

	if (!panicDialogDrawn && panicDialogDesired)
	{
		if ( !logPanicDataToScreen )
		{

			/* dim the screen 50% before putting up panic dialog */
		  	dim_screen();

			/* set up to draw background box */
			pd_x = (vinfo.v_width/2) - panic_dialog.pd_width/2;
			pd_y = (vinfo.v_height/2) - panic_dialog.pd_height/2;
		
			/*  draw image	*/
			panic_blit_rect( pd_x, pd_y, panic_dialog.pd_width, panic_dialog.pd_height, 0, (unsigned char*) panic_dialog.image_pixel_data);
		
			/* do not display the mac and ip addresses if the machine isn't attachable. */
			/* there's no sense in possibly confusing people. */
			if (panicDebugging)
			{
		
				/* offset for mac address text */
				mac_addr_digit_x = (vinfo.v_width/2) - 130; /* use 62 if no ip */
				mac_addr_digit_y = (vinfo.v_height/2) + panic_dialog.pd_height/2 - 20;
		
				if(kdp_mac_addr.ether_addr_octet[0] || kdp_mac_addr.ether_addr_octet[1]|| kdp_mac_addr.ether_addr_octet[2]
					|| kdp_mac_addr.ether_addr_octet[3] || kdp_mac_addr.ether_addr_octet[4] || kdp_mac_addr.ether_addr_octet[5])
				{
					/* blit the digits for mac address */
					for (count = 0; count < 6; count++ )
					{
						nibble =  (kdp_mac_addr.ether_addr_octet[count] & 0xf0) >> 4;
						digit = nibble < 10 ? nibble + '0':nibble - 10 + 'a';
						blit_digit(digit);
				
						nibble =  kdp_mac_addr.ether_addr_octet[count] & 0xf;
						digit = nibble < 10 ? nibble + '0':nibble - 10 + 'a';
						blit_digit(digit);
						if( count < 5 )
							blit_digit( colon );
					}
				}
				else	/* blit the ff's */
				{
					for( count = 0; count < 6; count++ )
					{
						digit = pair[0];
						blit_digit(digit);
						digit = pair[1];
						blit_digit(digit);
						if( count < 5 )
							blit_digit( colon );
					}
				}
				/* now print the ip address */
				mac_addr_digit_x = (vinfo.v_width/2) + 10;
				if(ip_addr != 0)
				{
					/* blit the digits for ip address */
					for (count = 0; count < 4; count++ )
					{
						nibble = (ip_addr & 0xff000000 ) >> 24;
				
						d3 = (nibble % 0xa) + '0';
						nibble = nibble/0xa;
						d2 = (nibble % 0xa) + '0';
						nibble = nibble /0xa;
						d1 = (nibble % 0xa) + '0';
					
						if( d1 != '0' ) blit_digit(d1);
						blit_digit(d2);
						blit_digit(d3);
						if( count < 3 )
							blit_digit(dot);
					
						d1= d2 = d3 = 0;
						ip_addr = ip_addr << 8;
					}
				}
			}
		}
	}
	panicDialogDrawn = TRUE;
	panicDialogDesired = FALSE;

}

static void 
blit_digit( int digit )
{
	switch( digit )
	{
		case '0':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_0.num_w, num_0.num_h, 255, (unsigned char*) num_0.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_0.num_w - 1;
			break;
		}
		case '1':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_1.num_w, num_1.num_h, 255, (unsigned char*) num_1.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_1.num_w ;
			break;
		}
		case '2':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_2.num_w, num_2.num_h, 255, (unsigned char*) num_2.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_2.num_w ;
			break;
		}
		case '3':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_3.num_w, num_3.num_h, 255, (unsigned char*) num_3.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_3.num_w ;
			break;
		}
		case '4':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_4.num_w, num_4.num_h, 255, (unsigned char*) num_4.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_4.num_w ;
			break;
		}
		case '5':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_5.num_w, num_5.num_h, 255, (unsigned char*) num_5.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_5.num_w ;
			break;
		}
		case '6':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_6.num_w, num_6.num_h, 255, (unsigned char*) num_6.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_6.num_w ;
			break;
		}
		case '7':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_7.num_w, num_7.num_h, 255, (unsigned char*) num_7.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_7.num_w ;
			break;
		}
		case '8':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_8.num_w, num_8.num_h, 255, (unsigned char*) num_8.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_8.num_w ;
			break;
		}
		case '9':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_9.num_w, num_9.num_h, 255, (unsigned char*) num_9.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_9.num_w ;
			break;
		}
		case 'a':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_a.num_w, num_a.num_h, 255, (unsigned char*) num_a.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_a.num_w ;
			break;
		}
		case 'b':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_b.num_w, num_b.num_h, 255, (unsigned char*) num_b.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_b.num_w ;
			break;
		}
		case 'c':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_c.num_w, num_c.num_h, 255, (unsigned char*) num_c.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_c.num_w ;
			break;
		}
		case 'd':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_d.num_w, num_d.num_h, 255, (unsigned char*) num_d.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_d.num_w ;
			break;
		}
		case 'e':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_e.num_w, num_e.num_h, 255, (unsigned char*) num_e.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_e.num_w ;
			break;
		}
		case 'f':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_f.num_w, num_f.num_h, 255, (unsigned char*) num_f.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_f.num_w ;
			break;
		}
		case ':':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y, num_colon.num_w, num_colon.num_h, 255, (unsigned char*) num_colon.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_colon.num_w;
			break;
		}
		case '.':  {
			panic_blit_rect( mac_addr_digit_x, mac_addr_digit_y + (num_colon.num_h/2), num_colon.num_w, num_colon.num_h/2, 255, (unsigned char*) num_colon.num_pixel_data);
			mac_addr_digit_x = mac_addr_digit_x + num_colon.num_w;
			break;
		}
		default:
			break;
	
	}
}

static void 
panic_blit_rect(	unsigned int x, unsigned int y,
			unsigned int width, unsigned int height,
			int transparent, unsigned char * dataPtr )
{
	if(!vinfo.v_depth)
		return;
		
    switch( vinfo.v_depth) {
	case 8:
	    panic_blit_rect_8( x, y, width, height, transparent, dataPtr);
	    break;
	case 16:
	    panic_blit_rect_16( x, y, width, height, transparent, dataPtr);
	    break;
	case 32:
	    panic_blit_rect_32( x, y, width, height, transparent, dataPtr);
	    break;
    }
}

/* 	panic_blit_rect_8 uses the built in clut for drawing.

*/
static void 
panic_blit_rect_8(	unsigned int x, unsigned int y,
			unsigned int width, unsigned int height,
			int transparent, unsigned char * dataPtr )
{
	volatile unsigned char * dst;
	int line, col;
	unsigned int data, quantity, value;
	
	dst = (volatile unsigned char *) (vinfo.v_baseaddr +
					  (y * vinfo.v_rowbytes) +
					  x);
	
	quantity = 0;
	
	for( line = 0; line < height; line++) {
		for( col = 0; col < width; col++) {
			if (quantity == 0) {
				dataPtr += decode_rle(dataPtr, &quantity, &value);
			}
			
			data = value;
			*(dst + col) = data;
			quantity--;
		}
		
		dst = (volatile unsigned char *) (((int)dst) + vinfo.v_rowbytes);
	}
}

/* 	panic_blit_rect_16 draws using a clut.
	
        panic_blit_rect_16 decodes the RLE encoded image data on the fly, looks up the
	color by indexing into the clut, uses the top 5 bits to fill in each of the three 
	pixel values (RGB) and writes each pixel to the screen.
*/
 static void 
 panic_blit_rect_16(	unsigned int x, unsigned int y,
			 unsigned int width, unsigned int height,
			 int transparent, unsigned char * dataPtr )
 {
	 volatile unsigned short * dst;
	 int line, col;
	 unsigned int  quantity, index, value, data;

	 /* If our clut has disappeared, use the standard MacOS 8-bit clut */
	 if(!clut) {
		 clut = appleClut8; 
	 }

	 dst = (volatile unsigned short *) (vinfo.v_baseaddr +
					    (y * vinfo.v_rowbytes) +
					    (x * 2));

	 quantity = 0;

	 for( line = 0; line < height; line++) {
		 for( col = 0; col < width; col++) {

			 if (quantity == 0) {
				 dataPtr += decode_rle(dataPtr, &quantity, &value);
				 index = value * 3;
			 }

			 data = ( (unsigned short) (0xf8 & (clut[index + 0])) << 7)
				 | ( (unsigned short) (0xf8 & (clut[index + 1])) << 2)
				 | ( (unsigned short) (0xf8 & (clut[index + 2])) >> 3);

			 *(dst + col) = data;
			 quantity--;
		 }

		 dst = (volatile unsigned short *) (((int)dst) + vinfo.v_rowbytes);
	 }

 }

 /*
	 panic_blit_rect_32 decodes the RLE encoded image data on the fly, and fills
	 in each of the three pixel values from the clut (RGB) for each pixel and 
	 writes it to the screen.
 */	
 static void 
 panic_blit_rect_32(	unsigned int x, unsigned int y,
			 unsigned int width, unsigned int height,
			 int transparent, unsigned char * dataPtr )
 {
	 volatile unsigned int * dst;
	 int line, col;
	 unsigned int value, quantity, index, data;


	/* If our clut has disappeared, use the standard MacOS 8-bit clut */
	if(!clut) {
		clut = appleClut8; 
	}

	dst = (volatile unsigned int *) (vinfo.v_baseaddr +
					 (y * vinfo.v_rowbytes) +
					 (x * 4));
	
	quantity = 0;
	
	for( line = 0; line < height; line++) {
		for( col = 0; col < width; col++) {
			if (quantity == 0) {
				dataPtr += decode_rle(dataPtr, &quantity, &value);
				index = value * 3;
			}
			
			data = ( (unsigned int) clut[index + 0] << 16)
				| ( (unsigned int) clut[index + 1] << 8)
				| ( (unsigned int) clut[index + 2]);
			
			*(dst + col) = data;
			quantity--;
		}
		
		dst = (volatile unsigned int *) (((int)dst) + vinfo.v_rowbytes);
	}
}

/* 	
        decode_rle decodes a single quantity/value pair of a "modified-RLE" encoded
        image. The encoding works as follows:
        
        The quantity and value will be described by either two or three bytes. If the
        most significant bit of the first byte is a 0, then the next seven bits are
        the quantity (run-length) and the following 8 bits are the value (index into
        a clut, in this case). If the msb of the first byte is a 1, then the next 15 bits
        are the quantity and the following 8 are the value. Visually, the two possible
        encodings are: (q = quantity, v = value)
        
          Byte 1			   Byte 2		        Byte 3
  case 1: [ 0 q6 q5 q4 q3 q2 q1 q0 ]       [ v7 v6 v5 v4 v3 v2 v1 v0 ]  [ ]
  case 2: [ 1 q14 q13 q12 a11 q10 q9 q8 ]  [ q7 q6 q5 q4 q3 q2 q1 q0 ]  [ v7 v6 v5 v4 v3 v2 v1 v0 ]
*/
static int
decode_rle( unsigned char * dataPtr, unsigned int * quantity, unsigned int * value )
{
	unsigned char byte1 = *dataPtr++;
	unsigned char byte2 = *dataPtr++;
	int num_slots = 0;
	
	/* if the most-significant bit is 0, then the first byte is quanity, the second is value */
	if ((byte1 >> 7) ==  0) {
		*quantity = (unsigned int) byte1;
		*value = (unsigned int) byte2;
		num_slots = 2;
	} else {
		/* clear the leading 1 */
		byte1 ^= 0x80;
		
		/* the first two bytes are the quantity, the third is value */
		*quantity = (unsigned int) byte1 << 8 | byte2;
		*value = *dataPtr++;
		num_slots = 3;
	}
	
	return num_slots;
}

static void 
dim_screen(void)
{
	if(!vinfo.v_depth)
		return;

    switch( vinfo.v_depth) {
	case 16:
	    dim_screen_16();
	    break;
	case 32:
	    dim_screen_32();
	    break;
    }
}

static void 
dim_screen_16(void)
{
	unsigned long *p, *endp, *row;
	int      col;
	int      rowline, rowlongs;
        unsigned long value, tmp;

	rowline = vinfo.v_rowscanbytes / 4;
	rowlongs = vinfo.v_rowbytes / 4;

	p = (unsigned long*) vinfo.v_baseaddr;
	endp = (unsigned long*) vinfo.v_baseaddr;

        endp += rowlongs * vinfo.v_height;

	for (row = p ; row < endp ; row += rowlongs) {
		for (col = 0; col < rowline; col++) {
                        value = *(row+col);
                        tmp =  ((value & 0x7C007C00) >> 1) & 0x3C003C00;
                        tmp |= ((value & 0x03E003E0) >> 1) & 0x01E001E0;
                        tmp |= ((value & 0x001F001F) >> 1) & 0x000F000F;
                        *(row+col) = tmp;		//half (dimmed)?
                }

	}

}

static void 
dim_screen_32(void)
{
	unsigned long *p, *endp, *row;
	int      col;
	int      rowline, rowlongs;
        unsigned long value, tmp;

	rowline = vinfo.v_rowscanbytes / 4;
	rowlongs = vinfo.v_rowbytes / 4;

	p = (unsigned long*) vinfo.v_baseaddr;
	endp = (unsigned long*) vinfo.v_baseaddr;

        endp += rowlongs * vinfo.v_height;

	for (row = p ; row < endp ; row += rowlongs) {
		for (col = 0; col < rowline; col++) {
                        value = *(row+col);
                        tmp =  ((value & 0x00FF0000) >> 1) & 0x007F0000;
                        tmp |= ((value & 0x0000FF00) >> 1) & 0x00007F00;
                        tmp |= (value & 0x000000FF) >> 1;
                        *(row+col) = tmp;		//half (dimmed)?
                }

	}

}