summaryrefslogtreecommitdiffstatshomepage
path: root/src/mess/video/pc_t1t.c
blob: 1243ccca8585f7e5846a61b7c68a89695f605a45 (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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
/***************************************************************************

    IBM PC junior
    Tandy 1000 Graphics Adapter (T1T) section

    Note that in the IBM PC Junior world, the term 'vga' is not the 'vga' that
    most people think of

***************************************************************************/

#include "emu.h"
#include "pc_t1t.h"
#include "video/mc6845.h"
#include "machine/pic8259.h"
#include "machine/ram.h"

/***************************************************************************

    Static declarations

***************************************************************************/

static PALETTE_INIT( pcjr );
static VIDEO_START( pc_t1t );
static VIDEO_START( pc_pcjr );
static MC6845_UPDATE_ROW( t1000_update_row );
static WRITE_LINE_DEVICE_HANDLER( t1000_de_changed );
static WRITE_LINE_DEVICE_HANDLER( t1000_vsync_changed );
static WRITE_LINE_DEVICE_HANDLER( pcjr_vsync_changed );


static MC6845_INTERFACE( mc6845_t1000_intf )
{
	T1000_SCREEN_NAME,      /* screen number */
	false,                  /* show border area */
	8,                      /* numbers of pixels per video memory address */
	NULL,                   /* begin_update */
	t1000_update_row,       /* update_row */
	NULL,                   /* end_update */
	DEVCB_LINE(t1000_de_changed),       /* on_de_changed */
	DEVCB_NULL,                 /* on_cur_changed */
	DEVCB_NULL,                 /* on_hsync_changed */
	DEVCB_LINE(t1000_vsync_changed),        /* on_vsync_changed */
	NULL,
};


MACHINE_CONFIG_FRAGMENT( pcvideo_t1000 )
	MCFG_SCREEN_ADD(T1000_SCREEN_NAME, RASTER)
	MCFG_SCREEN_RAW_PARAMS(XTAL_14_31818MHz,912,0,640,262,0,200)
	MCFG_SCREEN_UPDATE_DEVICE( T1000_MC6845_NAME, mc6845_device, screen_update )

	MCFG_PALETTE_LENGTH( 32 )
	MCFG_PALETTE_INIT(pcjr)

	MCFG_MC6845_ADD(T1000_MC6845_NAME, MC6845, XTAL_14_31818MHz/8, mc6845_t1000_intf)

	MCFG_VIDEO_START(pc_t1t)
MACHINE_CONFIG_END


static MC6845_INTERFACE( mc6845_pcjr_intf )
{
	T1000_SCREEN_NAME,      /* screen number */
	false,                  /* show border area */
	8,                      /* numbers of pixels per video memory address */
	NULL,                   /* begin_update */
	t1000_update_row,       /* update_row */
	NULL,                   /* end_update */
	DEVCB_LINE(t1000_de_changed),       /* on_de_chaged */
	DEVCB_NULL,                 /* on_cur_changed */
	DEVCB_NULL,                 /* on_hsync_changed */
	DEVCB_LINE(pcjr_vsync_changed),     /* on_vsync_changed */
	NULL
};


MACHINE_CONFIG_FRAGMENT( pcvideo_pcjr )
	MCFG_SCREEN_ADD(T1000_SCREEN_NAME, RASTER)
	MCFG_SCREEN_RAW_PARAMS(XTAL_14_31818MHz,912,0,640,262,0,200)
	MCFG_SCREEN_UPDATE_DEVICE( T1000_MC6845_NAME, mc6845_device, screen_update )

	MCFG_PALETTE_LENGTH( 32 )
	MCFG_PALETTE_INIT(pcjr)

	MCFG_MC6845_ADD(T1000_MC6845_NAME, MC6845, XTAL_14_31818MHz/16, mc6845_pcjr_intf)

	MCFG_VIDEO_START(pc_pcjr)
MACHINE_CONFIG_END


/***************************************************************************

    Methods

***************************************************************************/

/* Initialise the cga palette */
static PALETTE_INIT( pcjr )
{
	const static unsigned char tga_palette[16][3] =
	{
		{ 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0xaa }, { 0x00, 0xaa, 0x00 }, { 0x00, 0xaa, 0xaa },
		{ 0xaa, 0x00, 0x00 }, { 0xaa, 0x00, 0xaa }, { 0xaa, 0x55, 0x00 }, { 0xaa, 0xaa, 0xaa },
		{ 0x55, 0x55, 0x55 }, { 0x55, 0x55, 0xff }, { 0x55, 0xff, 0x55 }, { 0x55, 0xff, 0xff },
		{ 0xff, 0x55, 0x55 }, { 0xff, 0x55, 0xff }, { 0xff, 0xff, 0x55 }, { 0xff, 0xff, 0xff }
	};
	int i;

	/* colors */
	for(i = 0; i < 16; i++)
		palette_set_color_rgb(machine, i, tga_palette[i][0], tga_palette[i][1], tga_palette[i][2]);

	/* b/w mode shades */
	for(i = 0; i < 16; i++)
		palette_set_color_rgb( machine, 16+i, ( i << 4 ) | i, ( i << 4 ) | i, ( i << 4 ) | i );
}


static struct
{
	UINT8 mode_control, color_select;
	UINT8 status;

	// used in tandy1000hx; used in pcjr???
	struct {
		UINT8 index;
		UINT8 data[0x20];
		/* see vgadoc
		   0 mode control 1
		   1 palette mask
		   2 border color
		   3 mode control 2
		   4 reset
		   0x10-0x1f palette registers
		*/
	} reg;

	UINT8 bank;

	int pc_framecnt;

	UINT8 *displayram;
	UINT8 *t1_displayram;

	UINT8 *chr_gen;
	UINT8   chr_size;

	UINT8   address_data_ff;

	mc6845_update_row_func  update_row;
	UINT8   display_enable;
	UINT8   vsync;
	UINT8   palette_base;
	UINT8   *jxkanji;
} pcjr = { 0 };


static MC6845_UPDATE_ROW( t1000_text_inten_update_row )
{
	const rgb_t *palette = palette_entry_list_raw(bitmap.palette());
	UINT32  *p = &bitmap.pix32(y);
	int i;

	if ( y == 0 ) logerror("t1000_text_inten_update_row\n");
	for ( i = 0; i < x_count; i++ )
	{
		UINT16 offset = ( ( ma + i ) << 1 ) & 0x3fff;
		UINT8 chr = pcjr.displayram[ offset ];
		UINT8 attr = pcjr.displayram[ offset +1 ];
		UINT8 data = pcjr.chr_gen[ chr * pcjr.chr_size + ra ];
		UINT16 fg = pcjr.palette_base + ( attr & 0x0F );
		UINT16 bg = pcjr.palette_base + ( ( attr >> 4 ) & 0x07 );

		if ( i == cursor_x && ( pcjr.pc_framecnt & 0x08 ) )
		{
			data = 0xFF;
		}

		*p = palette[( data & 0x80 ) ? fg : bg]; p++;
		*p = palette[( data & 0x40 ) ? fg : bg]; p++;
		*p = palette[( data & 0x20 ) ? fg : bg]; p++;
		*p = palette[( data & 0x10 ) ? fg : bg]; p++;
		*p = palette[( data & 0x08 ) ? fg : bg]; p++;
		*p = palette[( data & 0x04 ) ? fg : bg]; p++;
		*p = palette[( data & 0x02 ) ? fg : bg]; p++;
		*p = palette[( data & 0x01 ) ? fg : bg]; p++;
	}
}


static MC6845_UPDATE_ROW( t1000_text_blink_update_row )
{
	const rgb_t *palette = palette_entry_list_raw(bitmap.palette());
	UINT32  *p = &bitmap.pix32(y);
	int i;

	for ( i = 0; i < x_count; i++ )
	{
		UINT16 offset = ( ( ma + i ) << 1 ) & 0x3fff;
		UINT8 chr = pcjr.displayram[ offset ];
		UINT8 attr = pcjr.displayram[ offset +1 ];
		UINT8 data = pcjr.chr_gen[ chr * pcjr.chr_size + ra ];
		UINT16 fg = pcjr.palette_base + ( attr & 0x0F );
		UINT16 bg = pcjr.palette_base + ( ( attr >> 4 ) & 0x07 );

		if ( i == cursor_x )
		{
			if ( pcjr.pc_framecnt & 0x08 )
			{
				data = 0xFF;
			}
		}
		else
		{
			if ( ( attr & 0x80 ) && ( pcjr.pc_framecnt & 0x10 ) )
			{
				data = 0x00;
			}
		}

		*p = palette[( data & 0x80 ) ? fg : bg]; p++;
		*p = palette[( data & 0x40 ) ? fg : bg]; p++;
		*p = palette[( data & 0x20 ) ? fg : bg]; p++;
		*p = palette[( data & 0x10 ) ? fg : bg]; p++;
		*p = palette[( data & 0x08 ) ? fg : bg]; p++;
		*p = palette[( data & 0x04 ) ? fg : bg]; p++;
		*p = palette[( data & 0x02 ) ? fg : bg]; p++;
		*p = palette[( data & 0x01 ) ? fg : bg]; p++;
	}
}

static MC6845_UPDATE_ROW( pcjx_text_update_row )
{
	const rgb_t *palette = palette_entry_list_raw(bitmap.palette());
	UINT32  *p = &bitmap.pix32(y);
	int i;

	for ( i = 0; i < x_count; i++ )
	{
		UINT16 offset = ( ( ma + i ) << 1 ) & 0x3fff;
		UINT8 chr = pcjr.displayram[ offset ];
		UINT8 attr = pcjr.displayram[ offset +1 ];
		UINT16 fg = pcjr.palette_base + ( attr & 0x07 );
		UINT16 bg = pcjr.palette_base + ( ( attr >> 4 ) & 0x07 );
		UINT16 code = chr & 0x1f;
		if((attr & 0x88) == 0x88)
		{
			code = pcjr.displayram[ offset - 2 ] & 0x1f;
			code = (code << 8) + chr;
		}
		else if(attr & 0x80)
			code = (code << 8) + pcjr.displayram[ offset + 2 ];
		else
			code = chr;

		UINT8 data;
		if(ra < 16)
			data = pcjr.jxkanji[code * 16 * 2 + (ra * 2) + ((attr & 8)?1:0)];
		else
			data = ((i == cursor_x) && (pcjr.pc_framecnt & 8)) ? 0xff: 0;

		*p = palette[( data & 0x80 ) ? fg : bg]; p++;
		*p = palette[( data & 0x40 ) ? fg : bg]; p++;
		*p = palette[( data & 0x20 ) ? fg : bg]; p++;
		*p = palette[( data & 0x10 ) ? fg : bg]; p++;
		*p = palette[( data & 0x08 ) ? fg : bg]; p++;
		*p = palette[( data & 0x04 ) ? fg : bg]; p++;
		*p = palette[( data & 0x02 ) ? fg : bg]; p++;
		*p = palette[( data & 0x01 ) ? fg : bg]; p++;
	}
}

static MC6845_UPDATE_ROW( t1000_gfx_4bpp_update_row )
{
	const rgb_t *palette = palette_entry_list_raw(bitmap.palette());
	UINT32  *p = &bitmap.pix32(y);
	UINT8   *vid = pcjr.displayram + ( ra << 13 );
	int i;

	for ( i = 0; i < x_count; i++ )
	{
		UINT16 offset = ( ( ma + i ) << 1 ) & 0x1fff;
		UINT8 data = vid[ offset ];

		*p = palette[pcjr.palette_base + pcjr.reg.data[0x10 + ( data >> 4 )]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[0x10 + ( data >> 4 )]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[0x10 + ( data & 0x0F )]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[0x10 + ( data & 0x0F )]]; p++;

		data = vid[ offset + 1 ];

		*p = palette[pcjr.palette_base + pcjr.reg.data[0x10 + ( data >> 4 )]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[0x10 + ( data >> 4 )]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[0x10 + ( data & 0x0F )]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[0x10 + ( data & 0x0F )]]; p++;
	}
}


static MC6845_UPDATE_ROW( t1000_gfx_2bpp_update_row )
{
	const rgb_t *palette = palette_entry_list_raw(bitmap.palette());
	UINT32  *p = &bitmap.pix32(y);
	UINT8   *vid = pcjr.displayram + ( ra << 13 );
	int i;

	for ( i = 0; i < x_count; i++ )
	{
		UINT16 offset = ( ( ma + i ) << 1 ) & 0x1fff;
		UINT8 data = vid[ offset ];

		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( data >> 6 ) & 0x03 ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( data >> 4 ) & 0x03 ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( data >> 2 ) & 0x03 ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + (   data        & 0x03 ) ]]; p++;

		data = vid[ offset+1 ];

		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( data >> 6 ) & 0x03 ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( data >> 4 ) & 0x03 ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( data >> 2 ) & 0x03 ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + (   data        & 0x03 ) ]]; p++;
	}
}


static MC6845_UPDATE_ROW( pcjr_gfx_2bpp_high_update_row )
{
	const rgb_t *palette = palette_entry_list_raw(bitmap.palette());
	UINT32  *p = &bitmap.pix32(y);
	UINT8   *vid = pcjr.displayram + ( ra << 13 );
	int i;

	for ( i = 0; i < x_count; i++ )
	{
		UINT16 offset = ( ( ma + i ) << 1 ) & 0x1fff;
		UINT8 data0 = vid[ offset ];
		UINT8 data1 = vid[ offset + 1 ];

		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( ( data0 & 0x80 ) >> 7 ) | ( ( data1 & 0x80 ) >> 6 ) ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( ( data0 & 0x40 ) >> 6 ) | ( ( data1 & 0x40 ) >> 5 ) ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( ( data0 & 0x20 ) >> 5 ) | ( ( data1 & 0x20 ) >> 4 ) ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( ( data0 & 0x10 ) >> 4 ) | ( ( data1 & 0x10 ) >> 3 ) ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( ( data0 & 0x08 ) >> 3 ) | ( ( data1 & 0x08 ) >> 2 ) ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( ( data0 & 0x04 ) >> 2 ) | ( ( data1 & 0x04 ) >> 1 ) ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( ( data0 & 0x02 ) >> 1 ) | ( ( data1 & 0x02 )      ) ) ]]; p++;
		*p = palette[pcjr.palette_base + pcjr.reg.data[ 0x10 + ( ( ( data0 & 0x01 )      ) | ( ( data1 & 0x01 ) << 1 ) ) ]]; p++;
	}
}


static MC6845_UPDATE_ROW( t1000_gfx_2bpp_tga_update_row )
{
	const rgb_t *palette = palette_entry_list_raw(bitmap.palette());
	UINT32  *p = &bitmap.pix32(y);
	UINT8   *vid = pcjr.displayram + ( ra << 13 );
	int i;

	if ( y == 0 ) logerror("t1000_gfx_2bpp_tga_update_row\n");
	for ( i = 0; i < x_count; i++ )
	{
		UINT16 offset = ( ( ma + i ) << 1 ) & 0x1fff;
		UINT8 data = vid[ offset ];
		UINT16 data2 = ( vid[ offset + 1 ] ) << 1;

		*p = palette[pcjr.reg.data[ 0x10 + ( ( ( data2 & 0x100 ) | ( data & 0x80 ) ) >> 7 ) ]]; p++;
		*p = palette[pcjr.reg.data[ 0x10 + ( ( ( data2 & 0x80 ) | ( data & 0x40 ) ) >> 6 ) ]]; p++;
		*p = palette[pcjr.reg.data[ 0x10 + ( ( ( data2 & 0x40 ) | ( data & 0x20 ) ) >> 5 ) ]]; p++;
		*p = palette[pcjr.reg.data[ 0x10 + ( ( ( data2 & 0x20 ) | ( data & 0x10 ) ) >> 4 ) ]]; p++;

		*p = palette[pcjr.reg.data[ 0x10 + ( ( ( data2 & 0x10 ) | ( data & 0x08 ) ) >> 3 ) ]]; p++;
		*p = palette[pcjr.reg.data[ 0x10 + ( ( ( data2 & 0x08 ) | ( data & 0x04 ) ) >> 2 ) ]]; p++;
		*p = palette[pcjr.reg.data[ 0x10 + ( ( ( data2 & 0x04 ) | ( data & 0x02 ) ) >> 1 ) ]]; p++;
		*p = palette[pcjr.reg.data[ 0x10 + (   ( data2 & 0x02 ) | ( data & 0x01 )        ) ]]; p++;
	}
}


static MC6845_UPDATE_ROW( t1000_gfx_1bpp_update_row )
{
	const rgb_t *palette = palette_entry_list_raw(bitmap.palette());
	UINT32  *p = &bitmap.pix32(y);
	UINT8   *vid = pcjr.displayram + ( ra << 13 );
	UINT8   fg = pcjr.palette_base + pcjr.reg.data[0x11];
	UINT8   bg = pcjr.palette_base + pcjr.reg.data[0x10];
	int i;

	if ( y == 0 ) logerror("t1000_gfx_1bpp_update_row\n");
	for ( i = 0; i < x_count; i++ )
	{
		UINT16 offset = ( ( ma + i ) << 1 ) & 0x1fff;
		UINT8 data = vid[ offset ];

		*p = palette[( data & 0x80 ) ? fg : bg]; p++;
		*p = palette[( data & 0x40 ) ? fg : bg]; p++;
		*p = palette[( data & 0x20 ) ? fg : bg]; p++;
		*p = palette[( data & 0x10 ) ? fg : bg]; p++;
		*p = palette[( data & 0x08 ) ? fg : bg]; p++;
		*p = palette[( data & 0x04 ) ? fg : bg]; p++;
		*p = palette[( data & 0x02 ) ? fg : bg]; p++;
		*p = palette[( data & 0x01 ) ? fg : bg]; p++;

		data = vid[ offset + 1 ];

		*p = palette[( data & 0x80 ) ? fg : bg]; p++;
		*p = palette[( data & 0x40 ) ? fg : bg]; p++;
		*p = palette[( data & 0x20 ) ? fg : bg]; p++;
		*p = palette[( data & 0x10 ) ? fg : bg]; p++;
		*p = palette[( data & 0x08 ) ? fg : bg]; p++;
		*p = palette[( data & 0x04 ) ? fg : bg]; p++;
		*p = palette[( data & 0x02 ) ? fg : bg]; p++;
		*p = palette[( data & 0x01 ) ? fg : bg]; p++;
	}
}


static MC6845_UPDATE_ROW( t1000_update_row )
{
	if ( pcjr.update_row )
	{
		pcjr.update_row( device, bitmap, cliprect, ma, ra, y, x_count, cursor_x, param );
	}
}


READ8_HANDLER ( pc_t1t_videoram_r )
{
	UINT8 *videoram = pcjr.t1_displayram;
	int data = 0xff;
	if( videoram )
		data = videoram[offset];
	return data;
}

WRITE8_HANDLER ( pc_t1t_videoram_w )
{
	UINT8 *videoram = pcjr.t1_displayram;
	if( videoram )
		videoram[offset] = data;
}

static void pc_t1t_mode_switch( void )
{
	switch( pcjr.mode_control & 0x3B )
	{
	case 0x08: case 0x09:
		pcjr.update_row = t1000_text_inten_update_row;
		break;
	case 0x28: case 0x29:
		pcjr.update_row = t1000_text_blink_update_row;
		break;
	case 0x0A: case 0x0B: case 0x2A: case 0x2B:
		switch( pcjr.bank & 0xc0 )
		{
		case 0x00:
		case 0x40:
			//logerror("t1t_gfx_2bpp - 1\n");
			pcjr.update_row = t1000_gfx_2bpp_update_row;
			if ( pcjr.color_select )
			{
				pcjr.reg.data[0x10] = 0x00;
				pcjr.reg.data[0x11] = 0x0B;
				pcjr.reg.data[0x12] = 0x0D;
				pcjr.reg.data[0x13] = 0x0F;
			}
			else
			{
				pcjr.reg.data[0x10] = 0x00;
				pcjr.reg.data[0x11] = 0x0A;
				pcjr.reg.data[0x12] = 0x0C;
				pcjr.reg.data[0x13] = 0x0E;
			}
			break;
		case 0x80:
		case 0xc0:
			//logerror("t1t_gfx_4bpp\n");
			pcjr.update_row = t1000_gfx_4bpp_update_row;
			break;
		}
		break;
	case 0x18: case 0x19: case 0x1A: case 0x1B:
	case 0x38: case 0x39: case 0x3A: case 0x3B:
		switch( pcjr.bank & 0xc0 )
		{
		case 0x00:
		case 0x40:
			//logerror("t1t_gfx_1bpp\n");
			pcjr.update_row = t1000_gfx_1bpp_update_row;
			break;
		case 0x80:
		case 0xc0:
			//logerror("t1t_gfx_2bpp - 2\n");
			pcjr.update_row = t1000_gfx_2bpp_tga_update_row;
			break;
		}
		break;
	default:
		pcjr.update_row = NULL;
		break;
	}
}


/* mode control 1 ( pcjr.reg.data[0] ) */
/* bit0 - 0 = low bandwidth, 1 = high bandwidth */
/* bit1 - 0 = alpha, 1 = graphics */
/* bit2 - 0 = color, 1 = b/w */
/* bit3 - 0 = video disable, 1 = video enable */
/* bit4 - 1 = 16 color graphics */
/* mode control 2 ( pcjr.reg.data[3] ) */
/* bit1 - 1 = enable blink */
/* bit3 - 1 = 2 color graphics */

static void pc_pcjr_mode_switch( running_machine &machine )
{
	mc6845_device *mc6845 = machine.device<mc6845_device>(T1000_MC6845_NAME);

	switch( pcjr.reg.data[0] & 0x1A )
	{
	case 0x08:      /* 01x0x */
		if(pcjr.jxkanji)
		{
			pcjr.update_row = pcjx_text_update_row;
			break;
		}
		if ( pcjr.reg.data[3] & 0x02 )
		{
			pcjr.update_row = t1000_text_blink_update_row;
		}
		else
		{
			pcjr.update_row = t1000_text_inten_update_row;
		}
		break;
	case 0x0A:      /* 01x1x */
		/* By default use medium resolution mode */
		pcjr.update_row = t1000_gfx_2bpp_update_row;

		/* Check for high resolution mode */
		if ( ( pcjr.bank & 0xc0 ) == 0xc0 )
			pcjr.update_row = pcjr_gfx_2bpp_high_update_row;

		/* Check for 640x200 b/w 2 shades mode */
		if ( ( pcjr.reg.data[0] & 0x04 ) && ( pcjr.reg.data[3] & 0x08 ) )
		{
			pcjr.update_row = t1000_gfx_1bpp_update_row;
		}
		break;
	case 0x18:      /* 11x0x - invalid?? */
		pcjr.update_row = NULL;
		break;
	case 0x1A:      /* 11x1x */
		pcjr.update_row = t1000_gfx_4bpp_update_row;
		break;
	default:
		pcjr.update_row = NULL;
		break;
	}

	/* Determine mc6845 input clock */
	if ( pcjr.reg.data[0] & 0x01 )
	{
		mc6845->set_clock( XTAL_14_31818MHz/8 );
	}
	else
	{
		mc6845->set_clock( XTAL_14_31818MHz/16 );
	}

	/* color or b/w? */
	pcjr.palette_base = ( pcjr.reg.data[0] & 0x04 ) ? 16 : 0;
}


/*
 * 3d8 rW   T1T mode control register (see #P138)
 */
static void pc_t1t_mode_control_w(int data)
{
	pcjr.mode_control = data;

	pc_t1t_mode_switch();
}

static int pc_t1t_mode_control_r(void)
{
	int data = pcjr.mode_control;
	return data;
}

/*
 * 3d9 ?W   color select register on color adapter
 */
static void pc_t1t_color_select_w(int data)
{
	if (pcjr.color_select == data)
		return;
	pcjr.color_select = data;
}

static int pc_t1t_color_select_r(void)
{
	int data = pcjr.color_select;
	return data;
}

/*  Bitfields for T1T status register:
 *  Bit(s)  Description (Table P179)
 *  7-6 not used
 *  5-4 color EGA, color ET4000: diagnose video display feedback, select
 *      from color plane enable
 *  4   holds current dot being displayed
 *  3   in vertical retrace
 *  2   (CGA,color EGA) light pen switch is off
 *      (MCGA,color ET4000) reserved (0)
 *  1   (CGA,color EGA) positive edge from light pen has set trigger
 *      (MCGA,color ET4000) reserved (0)
 *  0   display enabled
 *      =0  do not use memory
 *      =1  memory access without interfering with display
 *      (Genoa SuperEGA) horizontal or vertical retrace
 */
static int pc_t1t_status_r(void)
{
	int data = pcjr.vsync | pcjr.status | pcjr.display_enable;
	/* HACK HACK HACK */
	data |= ( pcjr.display_enable ? 0x10 : 0x00 );
	/* end HACK */
	return data;
}

/*
 * 3db -W   light pen strobe reset (on any value)
 */
static void pc_t1t_lightpen_strobe_w(int data)
{
//  pc_port[0x3db] = data;
}


/*
 * 3da -W   (mono EGA/mono VGA) feature control register
 *          (see PORT 03DAh-W for details; VGA, see PORT 03CAh-R)
 */
static void pc_t1t_vga_index_w(int data)
{
	pcjr.reg.index = data;
}

static void pc_t1t_vga_data_w(int data)
{
	pcjr.reg.data[pcjr.reg.index] = data;

	switch (pcjr.reg.index)
	{
		case 0x00: /* mode control 1 */
			break;
		case 0x01: /* palette mask (bits 3-0) */
			break;
		case 0x02: /* border color (bits 3-0) */
			break;
		case 0x03: /* mode control 2 */
			break;
		case 0x04: /* reset register */
			break;
		/* palette array */
		case 0x10: case 0x11: case 0x12: case 0x13:
		case 0x14: case 0x15: case 0x16: case 0x17:
		case 0x18: case 0x19: case 0x1a: case 0x1b:
		case 0x1c: case 0x1d: case 0x1e: case 0x1f:
			pcjr.reg.data[pcjr.reg.index] = data & 0x0F;
			break;
	}
}


static void pc_pcjr_vga_data_w(running_machine &machine, int data)
{
	pcjr.reg.data[pcjr.reg.index] = data;

	switch (pcjr.reg.index)
	{
		case 0x00:  /* mode control 1 */
					/* bit0 - 0 = low bandwidth, 1 = high bandwidth */
					/* bit1 - 0 = alpha, 1 = graphics */
					/* bit2 - 0 = color, 1 = b/w */
					/* bit3 - 0 = video disable, 1 = video enable */
					/* bit4 - 1 = 16 color graphics */
			pc_pcjr_mode_switch(machine);
			break;
		case 0x01:  /* palette mask (bits 3-0) */
			break;
		case 0x02:  /* border color (bits 3-0) */
			break;
		case 0x03:  /* mode control 2 */
					/* bit1 - 1 = enable blink */
					/* bit3 - 1 = 2 color graphics */
			pc_pcjr_mode_switch(machine);
			break;
		case 0x04:  /* reset register */
			break;
					/* palette array */
		case 0x10: case 0x11: case 0x12: case 0x13:
		case 0x14: case 0x15: case 0x16: case 0x17:
		case 0x18: case 0x19: case 0x1a: case 0x1b:
		case 0x1c: case 0x1d: case 0x1e: case 0x1f:
			pcjr.reg.data[pcjr.reg.index] = data & 0x0F;
			break;
	}
}


static int pc_t1t_vga_data_r(void)
{
	int data = pcjr.reg.data[pcjr.reg.index];

	switch (pcjr.reg.index)
	{
		case 0x00: /* mode control 1 */
			break;
		case 0x01: /* palette mask (bits 3-0) */
			break;
		case 0x02: /* border color (bits 3-0) */
			break;
		case 0x03: /* mode control 2 */
			break;
		case 0x04: /* reset register */
			break;
		/* palette array */
		case 0x10: case 0x11: case 0x12: case 0x13:
		case 0x14: case 0x15: case 0x16: case 0x17:
		case 0x18: case 0x19: case 0x1a: case 0x1b:
		case 0x1c: case 0x1d: case 0x1e: case 0x1f:
			break;
	}
	return data;
}

/*
 * 3df RW   display bank, access bank, mode
 * bit 0-2  Identifies the page of main memory being displayed in units of 16K.
 *          0: 0K, 1: 16K...7: 112K. In 32K modes (bits 6-7 = 2) only 0,2,4 and
 *          6 are valid, as the next page will also be used.
 *     3-5  Identifies the page of main memory that can be read/written at B8000h
 *          in units of 16K. 0: 0K, 1: 16K...7: 112K. In 32K modes (bits 6-7 = 2)
 *          only 0,2,4 and 6 are valid, as the next page will also be used.
 *     6-7  Display mode. 0: Text, 1: 16K graphics mode (4,5,6,8)
 *          2: 32K graphics mode (9,Ah)
 */
static void pc_t1t_bank_w(running_machine &machine, int data)
{
	if (pcjr.bank != data)
	{
		UINT8 *ram = machine.root_device().memregion("maincpu")->base();
		int dram, vram;
		pcjr.bank = data;
	/* it seems the video ram is mapped to the last 128K of main memory */
#if 1
		if ((data&0xc0)==0xc0) /* needed for lemmings */
		{
			dram = 0x80000 + ((data & 0x06) << 14);
			vram = 0x80000 + ((data & 0x30) << (14-3));
		}
		else
		{
			dram = 0x80000 + ((data & 0x07) << 14);
			vram = 0x80000 + ((data & 0x38) << (14-3));
		}
#else
		dram = (data & 0x07) << 14;
		vram = (data & 0x38) << (14-3);
#endif
		pcjr.t1_displayram = &ram[vram];
		pcjr.displayram = &ram[dram];
		pc_t1t_mode_switch();
	}
}


static void pc_pcjr_bank_w(running_machine &machine, int data)
{
	if (pcjr.bank != data)
	{
		int dram, vram;
		pcjr.bank = data;
		/* it seems the video ram is mapped to the last 128K of main memory */
		if ((data&0xc0)==0xc0) /* needed for lemmings */
		{
			dram = ((data & 0x06) << 14);
			vram = ((data & 0x30) << (14-3));
		}
		else
		{
			dram = ((data & 0x07) << 14);
			vram = ((data & 0x38) << (14-3));
		}
		machine.root_device().membank( "bank14" )->set_base( machine.device<ram_device>(RAM_TAG)->pointer() + vram );
		pcjr.displayram = machine.device<ram_device>(RAM_TAG)->pointer() + dram;
		pc_pcjr_mode_switch(machine);
	}
}

static void pc_pcjx_bank_w(running_machine &machine, int data)
{
	if (pcjr.bank != data)
	{
		int dram, vram;
		pcjr.bank = data;
		/* this probably isn't right, but otherwise the memory test stomps on the vram */
		if ((data&0xc0)==0xc0) /* needed for lemmings */
		{
			dram = 0x80000 + ((data & 0x06) << 14);
			vram = 0x80000 + ((data & 0x30) << (14-3));
		}
		else
		{
			dram = 0x80000 + ((data & 0x07) << 14);
			vram = 0x80000 + ((data & 0x38) << (14-3));
		}
		machine.root_device().membank( "bank14" )->set_base( machine.device<ram_device>(RAM_TAG)->pointer() + vram );
		pcjr.displayram = machine.device<ram_device>(RAM_TAG)->pointer() + dram;
		pc_pcjr_mode_switch(machine);
	}
}

static int pc_t1t_bank_r(void)
{
	return pcjr.bank;
}

/*************************************************************************
 *
 *      T1T
 *      Tandy 1000 / PCjr
 *
 *************************************************************************/

WRITE8_HANDLER ( pc_T1T_w )
{
	mc6845_device *mc6845;

	switch( offset )
	{
		case 0: case 2: case 4: case 6:
			mc6845 = space.machine().device<mc6845_device>(T1000_MC6845_NAME);
			mc6845->address_w( space, offset, data );
			break;
		case 1: case 3: case 5: case 7:
			mc6845 = space.machine().device<mc6845_device>(T1000_MC6845_NAME);
			mc6845->register_w( space, offset, data );
			break;
		case 8:
			pc_t1t_mode_control_w(data);
			break;
		case 9:
			pc_t1t_color_select_w(data);
			break;
		case 10:
			pc_t1t_vga_index_w(data);
			break;
		case 11:
			pc_t1t_lightpen_strobe_w(data);
			break;
		case 12:
			break;
		case 13:
			break;
		case 14:
			pc_t1t_vga_data_w(data);
			break;
		case 15:
			pc_t1t_bank_w(space.machine(), data);
			break;
	}
}


WRITE8_HANDLER( pc_pcjr_w )
{
	mc6845_device *mc6845;

	switch( offset )
	{
		case 0: case 4:
			mc6845 = space.machine().device<mc6845_device>(T1000_MC6845_NAME);
			mc6845->address_w( space, offset, data );
			break;
		case 1: case 5:
			mc6845 = space.machine().device<mc6845_device>(T1000_MC6845_NAME);
			mc6845->register_w( space, offset, data );
			break;
		case 10:
			if ( pcjr.address_data_ff & 0x01 )
			{
				pc_pcjr_vga_data_w( space.machine(), data );
			}
			else
			{
				pc_t1t_vga_index_w( data );
			}
			pcjr.address_data_ff ^= 0x01;
			break;
		case 11:
			pc_t1t_lightpen_strobe_w(data);
			break;
		case 12:
			break;
		case 15:
			if(pcjr.jxkanji)
				pc_pcjx_bank_w(space.machine(), data);
			else
				pc_pcjr_bank_w(space.machine(), data);
			break;

		default:
			break;
	}
}


READ8_HANDLER ( pc_T1T_r )
{
	mc6845_device *mc6845;
	int             data = 0xff;

	switch( offset )
	{
		case 0: case 2: case 4: case 6:
			/* return last written mc6845 address value here? */
			break;

		case 1: case 3: case 5: case 7:
			mc6845 = space.machine().device<mc6845_device>(T1000_MC6845_NAME);
			data = mc6845->register_r( space, offset );
			break;

		case 8:
			data = pc_t1t_mode_control_r();
			break;

		case 9:
			data = pc_t1t_color_select_r();
			break;

		case 10:
			pcjr.address_data_ff = 0;
			data = pc_t1t_status_r();
			break;

		case 11:
			/* -W lightpen strobe reset */
			break;

		case 12:
		case 13:
			break;

		case 14:
			data = pc_t1t_vga_data_r();
			break;

		case 15:
			data = pc_t1t_bank_r();
			break;
	}
	return data;
}


static WRITE_LINE_DEVICE_HANDLER( t1000_de_changed )
{
	pcjr.display_enable = state ? 1 : 0;
}


static WRITE_LINE_DEVICE_HANDLER( t1000_vsync_changed )
{
	pcjr.vsync = state ? 8 : 0;
	if ( state )
	{
		pcjr.pc_framecnt++;
	}
}


static WRITE_LINE_DEVICE_HANDLER( pcjr_vsync_changed )
{
	pcjr.vsync = state ? 8 : 0;
	if ( state )
	{
		pcjr.pc_framecnt++;
	}
	pic8259_ir5_w(device->machine().device("pic8259"), state);
}

static VIDEO_START( pc_t1t )
{
	int buswidth;
	address_space &space = machine.firstcpu->space(AS_PROGRAM);
	address_space &spaceio = machine.firstcpu->space(AS_IO);

	pcjr.chr_gen = machine.root_device().memregion("gfx1")->base();
	pcjr.update_row = NULL;
	pcjr.bank = 0;
	pcjr.chr_size = 16;

	buswidth = machine.firstcpu->space_config(AS_PROGRAM)->m_databus_width;
	switch(buswidth)
	{
		case 8:
			space.install_legacy_readwrite_handler(0xb8000, 0xbffff, FUNC(pc_t1t_videoram_r), FUNC(pc_t1t_videoram_w) );
			spaceio.install_legacy_readwrite_handler(0x3d0, 0x3df, FUNC(pc_T1T_r),FUNC(pc_T1T_w) );
			break;

		case 16:
			space.install_legacy_readwrite_handler(0xb8000, 0xbffff, FUNC(pc_t1t_videoram_r), FUNC(pc_t1t_videoram_w), 0xffff );
			spaceio.install_legacy_readwrite_handler(0x3d0, 0x3df, FUNC(pc_T1T_r),FUNC(pc_T1T_w), 0xffff );
			break;

		default:
			fatalerror("T1T: Bus width %d not supported\n", buswidth);
			break;
	}
}


static VIDEO_START( pc_pcjr )
{
	int buswidth;
	address_space &spaceio = machine.firstcpu->space(AS_IO);

	pcjr.chr_gen = machine.root_device().memregion("gfx1")->base();
	pcjr.update_row = NULL;
	pcjr.bank = 0;
	pcjr.mode_control = 0x08;
	pcjr.chr_size = 8;
	if(!strncmp(machine.system().name, "ibmpcjx", 7))
		pcjr.jxkanji = machine.root_device().memregion("kanji")->base();
	else
		pcjr.jxkanji = NULL;

	buswidth = machine.firstcpu->space_config(AS_PROGRAM)->m_databus_width;
	switch(buswidth)
	{
		case 8:
			spaceio.install_legacy_readwrite_handler(0x3d0, 0x3df, FUNC(pc_T1T_r),FUNC(pc_pcjr_w) );
			break;

		default:
			fatalerror("PCJR: Bus width %d not supported\n", buswidth);
			break;
	}
}