summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/archimds.c
blob: 1c96a71e4be9dca380db500cf8fa6cc76920c07e (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
/******************************************************************************
 *
 *  Acorn Archimedes custom chips (IOC, MEMC, VIDC)
 *
 *  Memory map (from http://b-em.bbcmicro.com/arculator/archdocs.txt)
 *
 *  0000000 - 1FFFFFF - logical RAM (32 meg)
 *  2000000 - 2FFFFFF - physical RAM (supervisor only - max 16MB - requires quad MEMCs)
 *  3000000 - 33FFFFF - IOC (IO controllers - supervisor only)
 *  3310000 - FDC - WD1772
 *  33A0000 - Econet - 6854
 *  33B0000 - Serial - 6551
 *  3240000 - 33FFFFF - internal expansion cards
 *  32D0000 - hard disc controller (not IDE) - HD63463
 *  3350010 - printer
 *  3350018 - latch A
 *  3350040 - latch B
 *  3270000 - external expansion cards
 *
 *  3400000 - 3FFFFFF - ROM (read - 12 meg - Arthur and RiscOS 2 512k, RiscOS 3 2MB)
 *  3400000 - 37FFFFF - Low ROM  (4 meg, I think this is expansion ROMs)
 *  3800000 - 3FFFFFF - High ROM (main OS ROM)
 *
 *  3400000 - 35FFFFF - VICD10 (write - supervisor only)
 *  3600000 - 3FFFFFF - MEMC (write - supervisor only)
 *
 *****************************************************************************/

#include "emu.h"
#include "cpu/arm/arm.h"
#include "sound/dac.h"
#include "includes/archimds.h"
#include "machine/i2cmem.h"
#include "debugger.h"

#ifdef MESS
#include "machine/wd17xx.h"
#endif

static const int page_sizes[4] = { 4096, 8192, 16384, 32768 };

#define IOC_LOG 0

UINT32 *archimedes_memc_physmem;
static UINT32 memc_pagesize;
static int memc_latchrom;
static UINT32 ioc_timercnt[4], ioc_timerout[4];
static UINT32 vidc_vidstart, vidc_vidend, vidc_vidinit,vidc_vidcur;
static UINT32 vidc_sndstart, vidc_sndend, vidc_sndcur;
static UINT8 video_dma_on,audio_dma_on;
UINT8 i2c_clk;
INT16 memc_pages[0x2000];	// the logical RAM area is 32 megs, and the smallest page size is 4k
UINT32 vidc_regs[256];
UINT8 ioc_regs[0x80/4];
UINT8 vidc_bpp_mode;
UINT8 vidc_interlace;
static UINT8 vidc_pixel_clk;
static const UINT32 pixel_rate[4] = { 8000000, 12000000, 16000000, 24000000};
static UINT8 vidc_stereo_reg[8];

static emu_timer *timer[4], *snd_timer, *vid_timer;
static emu_timer *vbl_timer;

void archimedes_request_irq_a(running_machine &machine, int mask)
{
	ioc_regs[IRQ_STATUS_A] |= mask;

	if (ioc_regs[IRQ_MASK_A] & mask)
	{
		cputag_set_input_line(machine, "maincpu", ARM_IRQ_LINE, ASSERT_LINE);
	}
}

void archimedes_request_irq_b(running_machine &machine, int mask)
{
	ioc_regs[IRQ_STATUS_B] |= mask;

	if (ioc_regs[IRQ_MASK_B] & mask)
	{
		generic_pulse_irq_line(machine.device("maincpu"), ARM_IRQ_LINE);
	}
}

void archimedes_request_fiq(running_machine &machine, int mask)
{
	ioc_regs[FIQ_STATUS] |= mask;

	if (ioc_regs[FIQ_MASK] & mask)
	{
		generic_pulse_irq_line(machine.device("maincpu"), ARM_FIRQ_LINE);
	}
}

void archimedes_clear_irq_a(running_machine &machine, int mask)
{
	ioc_regs[IRQ_STATUS_A] &= ~mask;
}

void archimedes_clear_irq_b(running_machine &machine, int mask)
{
	ioc_regs[IRQ_STATUS_B] &= ~mask;
}

void archimedes_clear_fiq(running_machine &machine, int mask)
{
	ioc_regs[FIQ_STATUS] &= ~mask;
}

static TIMER_CALLBACK( vidc_vblank )
{
	archimedes_request_irq_a(machine, ARCHIMEDES_IRQA_VBL);

	// set up for next vbl
	vbl_timer->adjust(machine.primary_screen->time_until_pos(vidc_regs[0xb4]));
}

/* video DMA */
/* TODO: what type of DMA this is, burst or cycle steal? Docs doesn't explain it (4 usec is the DRAM refresh). */
static TIMER_CALLBACK( vidc_video_tick )
{
	address_space *space = machine.device("maincpu")->memory().space(AS_PROGRAM);
	static UINT8 *vram = machine.region("vram")->base();
	UINT32 size;

	size = vidc_vidend-vidc_vidstart+0x10;

	for(vidc_vidcur = 0;vidc_vidcur < size;vidc_vidcur++)
		vram[vidc_vidcur] = (space->read_byte(vidc_vidstart+vidc_vidcur+vidc_vidinit));

	if(video_dma_on)
		vid_timer->adjust(space->machine().primary_screen->time_until_pos(vidc_regs[0xb4]));
	else
		vid_timer->adjust(attotime::never);
}

/* audio DMA */
static TIMER_CALLBACK( vidc_audio_tick )
{
	address_space *space = machine.device("maincpu")->memory().space(AS_PROGRAM);
	UINT8 ulaw_comp;
	INT16 res;
	UINT8 ch;
	static const char *const dac_port[8] = { "dac0", "dac1", "dac2", "dac3", "dac4", "dac5", "dac6", "dac7" };
	static const INT16 mulawTable[256] =
	{
		-32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
		-23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
		-15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
		-11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
		-7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
		-5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
		-3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
		-2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
		-1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
		-1372, -1308, -1244, -1180, -1116, -1052,  -988,  -924,
		-876,  -844,  -812,  -780,  -748,  -716,  -684,  -652,
		-620,  -588,  -556,  -524,  -492,  -460,  -428,  -396,
		-372,  -356,  -340,  -324,  -308,  -292,  -276,  -260,
		-244,  -228,  -212,  -196,  -180,  -164,  -148,  -132,
		-120,  -112,  -104,   -96,   -88,   -80,   -72,   -64,
		-56,   -48,   -40,   -32,   -24,   -16,    -8,     -1,
		32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
		23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
		15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
		11900, 11388, 10876, 10364,  9852,  9340,  8828,  8316,
		7932,  7676,  7420,  7164,  6908,  6652,  6396,  6140,
		5884,  5628,  5372,  5116,  4860,  4604,  4348,  4092,
		3900,  3772,  3644,  3516,  3388,  3260,  3132,  3004,
		2876,  2748,  2620,  2492,  2364,  2236,  2108,  1980,
		1884,  1820,  1756,  1692,  1628,  1564,  1500,  1436,  						 
		1372,  1308,  1244,  1180,  1116,  1052,   988,   924,
		876,   844,   812,   780,   748,   716,   684,   652,   						 
		620,   588,   556,   524,   492,   460,   428,   396,
		372,   356,   340,   324,   308,   292,   276,   260,   						 
		244,   228,   212,   196,   180,   164,   148,   132,
		120,   112,   104,    96,    88,    80,    72,    64,
		 56,    48,    40,    32,    24,    16,     8,     0
	}; 

	for(ch=0;ch<8;ch++)
	{
		UINT8 ulaw_temp = (space->read_byte(vidc_sndstart+vidc_sndcur + ch)) ^ 0xff;

		ulaw_comp = (ulaw_temp>>1) | ((ulaw_temp&1)<<7);

		res = mulawTable[ulaw_comp];

		dac_signed_data_16_w(space->machine().device(dac_port[ch & 7]), res^0x8000);
	}

	vidc_sndcur+=8;

	if (vidc_sndcur >= (vidc_sndend-vidc_sndstart)+0x10)
	{
		vidc_sndcur = 0;
		archimedes_request_irq_b(machine, ARCHIMEDES_IRQB_SOUND_EMPTY);

		if(!audio_dma_on)
		{
			snd_timer->adjust(attotime::never);
			for(ch=0;ch<8;ch++)
				dac_signed_data_16_w(space->machine().device(dac_port[ch & 7]), 0x8000);
		}
	}
}

static void a310_set_timer(int tmr)
{
	double freq;

	switch(tmr)
	{
		case 0:
		case 1:
			timer[tmr]->adjust(attotime::from_usec(ioc_timercnt[tmr]/8), tmr); // TODO: ARM timings are quite off there, it should be latch and not latch/8
			break;
		case 2:
			freq = 1000000.0 / (double)(ioc_timercnt[tmr]+1);
			timer[tmr]->adjust(attotime::from_hz(freq), tmr);
			break;
		case 3:
			freq = 1000000.0 / (double)((ioc_timercnt[tmr]+1)*16);
			timer[tmr]->adjust(attotime::from_hz(freq), tmr);
			break;
	}
}

// param
static TIMER_CALLBACK( ioc_timer )
{
	// all timers always run
	a310_set_timer(param);

	// but only timers 0 and 1 generate IRQs
	switch (param)
	{
		case 0:
			archimedes_request_irq_a(machine, ARCHIMEDES_IRQA_TIMER0);
			break;

		case 1:
			archimedes_request_irq_a(machine, ARCHIMEDES_IRQA_TIMER1);
			break;
	}
}

void archimedes_reset(running_machine &machine)
{
	int i;

	memc_latchrom = 1;			// map in the boot ROM

	// kill all memc mappings
	for (i = 0; i < (32*1024*1024)/(4096); i++)
	{
		memc_pages[i] = -1;		// indicate unmapped
	}

	ioc_regs[IRQ_STATUS_A] = 0x10 | 0x80; //set up POR (Power On Reset) and Force IRQ at start-up
	ioc_regs[IRQ_STATUS_B] = 0x02; //set up IL[1] On
	ioc_regs[FIQ_STATUS] = 0x80;   //set up Force FIQ
	ioc_regs[CONTROL] = 0xff;
}

void archimedes_init(running_machine &machine)
{
	memc_pagesize = 0;

	vbl_timer = machine.scheduler().timer_alloc(FUNC(vidc_vblank));
	vbl_timer->adjust(attotime::never);

	timer[0] = machine.scheduler().timer_alloc(FUNC(ioc_timer));
	timer[1] = machine.scheduler().timer_alloc(FUNC(ioc_timer));
	timer[2] = machine.scheduler().timer_alloc(FUNC(ioc_timer));
	timer[3] = machine.scheduler().timer_alloc(FUNC(ioc_timer));
	timer[0]->adjust(attotime::never);
	timer[1]->adjust(attotime::never);
	timer[2]->adjust(attotime::never);
	timer[3]->adjust(attotime::never);

	vid_timer = machine.scheduler().timer_alloc(FUNC(vidc_video_tick));
	snd_timer = machine.scheduler().timer_alloc(FUNC(vidc_audio_tick));
	snd_timer->adjust(attotime::never);
}

READ32_HANDLER(archimedes_memc_logical_r)
{
	UINT32 page, poffs;

	// are we mapping in the boot ROM?
	if (memc_latchrom)
	{
		UINT32 *rom;

		rom = (UINT32 *)space->machine().region("maincpu")->base();

		return rom[offset & 0x1fffff];
	}
	else
	{
		// figure out the page number and offset in the page
		page = (offset<<2) / page_sizes[memc_pagesize];
		poffs = (offset<<2) % page_sizes[memc_pagesize];

//      printf("Reading offset %x (addr %x): page %x (size %d %d) offset %x ==> %x %x\n", offset, offset<<2, page, memc_pagesize, page_sizes[memc_pagesize], poffs, memc_pages[page], memc_pages[page]*page_sizes[memc_pagesize]);

		if (memc_pages[page] != -1)
		{
			return archimedes_memc_physmem[((memc_pages[page] * page_sizes[memc_pagesize]) + poffs)>>2];
		}
		else
		{
			//printf("ARCHIMEDES_MEMC: Reading unmapped page %02x\n",page);
			return 0xdeadbeef;
		}
	}

	return 0;
}

WRITE32_HANDLER(archimedes_memc_logical_w)
{
	UINT32 page, poffs;

	// if the boot ROM is mapped, ignore writes
	if (memc_latchrom)
	{
		return;
	}
	else
	{
		// figure out the page number and offset in the page
		page = (offset<<2) / page_sizes[memc_pagesize];
		poffs = (offset<<2) % page_sizes[memc_pagesize];

//      printf("Writing offset %x (addr %x): page %x (size %d %d) offset %x ==> %x %x\n", offset, offset<<2, page, memc_pagesize, page_sizes[memc_pagesize], poffs, memc_pages[page], memc_pages[page]*page_sizes[memc_pagesize]);

		if (memc_pages[page] != -1)
		{
			COMBINE_DATA(&archimedes_memc_physmem[((memc_pages[page] * page_sizes[memc_pagesize]) + poffs)>>2]);
		}
		else
		{
			//printf("ARCHIMEDES_MEMC: Writing unmapped page %02x, what do we do?\n",page);
		}
	}
}

#if 0
DIRECT_UPDATE_HANDLER( a310_setopbase )
{
	// if we're not in logical memory, MAME can do the right thing
	if (address > 0x1ffffff)
	{
		return address;
	}

	// if the boot ROM is mapped in, do some trickery to make it show up
	if (memc_latchrom)
	{
		direct.explicit_configure(0x000000, 0x1fffff, 0x1fffff, *direct.space().machine().region("maincpu"));
	}
	else	// executing from logical memory
	{
		offs_t pagesize = page_sizes[memc_pagesize];
		UINT32 page = address / pagesize;

		direct.explicit_configure(page * pagesize, page * pagesize - 1, pagesize - 1, &archimedes_memc_physmem[(memc_pages[page] * pagesize)>>2]);
	}

	return ~0;
}
#endif

void archimedes_driver_init(running_machine &machine)
{
//  address_space *space = machine.device<arm_device>("maincpu")->space(AS_PROGRAM);
//  space->set_direct_update_handler(direct_update_delegate(FUNC(a310_setopbase), &machine));
}

static const char *const ioc_regnames[] =
{
	"(rw) Control",					// 0
	"(read) Keyboard receive (write) keyboard send",	// 1
	"?",
	"?",
	"(read) IRQ status A",				// 4
	"(read) IRQ request A (write) IRQ clear",	// 5
	"(rw) IRQ mask A",				// 6
	"?",
	"(read) IRQ status B",		// 8
	"(read) IRQ request B",		// 9
	"(rw) IRQ mask B",		// 10
	"?",
	"(read) FIQ status",		// 12
	"(read) FIQ request",		// 13
	"(rw) FIQ mask",		// 14
	"?",
	"(read) Timer 0 count low (write) Timer 0 latch low",		// 16
	"(read) Timer 0 count high (write) Timer 0 latch high",		// 17
	"(write) Timer 0 go command",					// 18
	"(write) Timer 0 latch command",				// 19
	"(read) Timer 1 count low (write) Timer 1 latch low",		// 20
	"(read) Timer 1 count high (write) Timer 1 latch high",		// 21
	"(write) Timer 1 go command",					// 22
	"(write) Timer 1 latch command",				// 23
	"(read) Timer 2 count low (write) Timer 2 latch low",		// 24
	"(read) Timer 2 count high (write) Timer 2 latch high",		// 25
	"(write) Timer 2 go command",					// 26
	"(write) Timer 2 latch command",				// 27
	"(read) Timer 3 count low (write) Timer 3 latch low",		// 28
	"(read) Timer 3 count high (write) Timer 3 latch high",		// 29
	"(write) Timer 3 go command",					// 30
	"(write) Timer 3 latch command"					// 31
};

static void latch_timer_cnt(int tmr)
{
	double time = timer[tmr]->elapsed().as_double();
	time *= 2000000.0;	// find out how many 2 MHz ticks have gone by
	ioc_timerout[tmr] = ioc_timercnt[tmr] - (UINT32)time;
}

/* TODO: should be a 8-bit handler */
static READ32_HANDLER( ioc_ctrl_r )
{
	if(IOC_LOG)
	logerror("IOC: R %s = %02x (PC=%x) %02x\n", ioc_regnames[offset&0x1f], ioc_regs[offset&0x1f], cpu_get_pc( &space->device() ),offset & 0x1f);

	switch (offset & 0x1f)
	{
		case CONTROL:
		{
			UINT8 i2c_data;
			static UINT8 flyback; //internal name for vblank here
			int vert_pos;

			vert_pos = space->machine().primary_screen->vpos();
			flyback = (vert_pos <= vidc_regs[VIDC_VDSR] || vert_pos >= vidc_regs[VIDC_VDER]) ? 0x80 : 0x00;

			i2c_data = (i2cmem_sda_read(space->machine().device("i2cmem")) & 1);

			return (flyback) | (ioc_regs[CONTROL] & 0x7c) | (i2c_clk<<1) | i2c_data;
		}

		case KART:	// keyboard read
			archimedes_request_irq_b(space->machine(), ARCHIMEDES_IRQB_KBD_XMIT_EMPTY);
			break;

		case IRQ_STATUS_A:
			return (ioc_regs[IRQ_STATUS_A] & 0x7f) | 0x80; // Force IRQ is always '1'

		case IRQ_REQUEST_A:
			return (ioc_regs[IRQ_STATUS_A] & ioc_regs[IRQ_MASK_A]);

		case IRQ_MASK_A:
			return (ioc_regs[IRQ_MASK_A]);

		case IRQ_STATUS_B:
			return (ioc_regs[IRQ_STATUS_B]);

		case IRQ_REQUEST_B:
			return (ioc_regs[IRQ_STATUS_B] & ioc_regs[IRQ_MASK_B]);

		case IRQ_MASK_B:
			return (ioc_regs[IRQ_MASK_B]);

		case FIQ_STATUS:
			return (ioc_regs[FIQ_STATUS] & 0x7f) | 0x80; // Force FIQ is always '1'

		case FIQ_REQUEST:
			return (ioc_regs[FIQ_STATUS] & ioc_regs[FIQ_MASK]);

		case FIQ_MASK:
			return (ioc_regs[FIQ_MASK]);

		case T0_LATCH_LO: return ioc_timerout[0]&0xff;
		case T0_LATCH_HI: return (ioc_timerout[0]>>8)&0xff;

		case T1_LATCH_LO: return ioc_timerout[1]&0xff;
		case T1_LATCH_HI: return (ioc_timerout[1]>>8)&0xff;

		case T2_LATCH_LO: return ioc_timerout[2]&0xff;
		case T2_LATCH_HI: return (ioc_timerout[2]>>8)&0xff;

		case T3_LATCH_LO: return ioc_timerout[3]&0xff;
		case T3_LATCH_HI: return (ioc_timerout[3]>>8)&0xff;
		default:
			if(!IOC_LOG)
				logerror("IOC: R %s = %02x (PC=%x) %02x\n", ioc_regnames[offset&0x1f], ioc_regs[offset&0x1f], cpu_get_pc( &space->device() ),offset & 0x1f);
			break;
	}

	return ioc_regs[offset&0x1f];
}

/* TODO: should be a 8-bit handler */
static WRITE32_HANDLER( ioc_ctrl_w )
{
	if(IOC_LOG)
	logerror("IOC: W %02x @ reg %s (PC=%x)\n", data&0xff, ioc_regnames[offset&0x1f], cpu_get_pc( &space->device() ));

	switch (offset&0x1f)
	{
		case CONTROL:	// I2C bus control
			//logerror("IOC I2C: CLK %d DAT %d\n", (data>>1)&1, data&1);
			i2cmem_sda_write(space->machine().device("i2cmem"), data & 0x01);
			i2cmem_scl_write(space->machine().device("i2cmem"), (data & 0x02) >> 1);
			i2c_clk = (data & 2) >> 1;
			break;

		case KART:
			#if 0
			if(data == 0x0d)
				printf("\n");
			else
				printf("%c",data);
			#endif
			break;

		case IRQ_MASK_A:
			ioc_regs[IRQ_MASK_A] = data & 0xff;

			if(data & 0x80) //force an IRQ
				archimedes_request_irq_a(space->machine(),ARCHIMEDES_IRQA_FORCE);

			if(data & 0x08) //set up the VBLANK timer
				vbl_timer->adjust(space->machine().primary_screen->time_until_pos(vidc_regs[0xb4]));

			break;

		case FIQ_MASK:
			ioc_regs[FIQ_MASK] = data & 0xff;

			if(data & 0x80) //force a FIRQ
				archimedes_request_fiq(space->machine(),ARCHIMEDES_FIQ_FORCE);

			break;

		case IRQ_REQUEST_A: 	// IRQ clear A
			ioc_regs[IRQ_STATUS_A] &= ~(data&0xff);

			// if that did it, clear the IRQ
			//if (ioc_regs[IRQ_STATUS_A] == 0)
			{
				//printf("IRQ clear A\n");
				cputag_set_input_line(space->machine(), "maincpu", ARM_IRQ_LINE, CLEAR_LINE);
			}
			break;

		case T0_LATCH_LO:
		case T0_LATCH_HI:
			ioc_regs[offset&0x1f] = data & 0xff;
			break;

		case T1_LATCH_LO:
		case T1_LATCH_HI:
			ioc_regs[offset&0x1f] = data & 0xff;
			break;

		case T2_LATCH_LO:
		case T2_LATCH_HI:
			ioc_regs[offset&0x1f] = data & 0xff;
			break;

		case T3_LATCH_LO:
		case T3_LATCH_HI:
			ioc_regs[offset&0x1f] = data & 0xff;
			break;

		case T0_LATCH:	// Timer 0 latch
			latch_timer_cnt(0);
			break;

		case T1_LATCH:	// Timer 1 latch
			latch_timer_cnt(1);
			break;

		case T2_LATCH:	// Timer 2 latch
			latch_timer_cnt(2);
			break;

		case T3_LATCH:	// Timer 3 latch
			latch_timer_cnt(3);
			break;

		case T0_GO:	// Timer 0 start
			ioc_timercnt[0] = ioc_regs[T0_LATCH_HI]<<8 | ioc_regs[T0_LATCH_LO];
			a310_set_timer(0);
			break;

		case T1_GO:	// Timer 1 start
			ioc_timercnt[1] = ioc_regs[T1_LATCH_HI]<<8 | ioc_regs[T1_LATCH_LO];
			a310_set_timer(1);
			break;

		case T2_GO:	// Timer 2 start
			ioc_timercnt[2] = ioc_regs[T2_LATCH_HI]<<8 | ioc_regs[T2_LATCH_LO];
			a310_set_timer(2);
			break;

		case T3_GO:	// Timer 3 start
			ioc_timercnt[3] = ioc_regs[T3_LATCH_HI]<<8 | ioc_regs[T3_LATCH_LO];
			a310_set_timer(3);
			break;

		default:
			if(!IOC_LOG)
				logerror("IOC: W %02x @ reg %s (PC=%x)\n", data&0xff, ioc_regnames[offset&0x1f], cpu_get_pc( &space->device() ));

			ioc_regs[offset&0x1f] = data & 0xff;
			break;
	}
}

READ32_HANDLER(archimedes_ioc_r)
{
	UINT32 ioc_addr;
	#ifdef MESS
	device_t *fdc = (device_t *)space->machine().device("wd1772");
	#endif

	ioc_addr = offset*4;

	switch((ioc_addr & 0x300000) >> 20)
	{
		/*82c711*/
		case 0:
			logerror("82c711 read at address %08x\n",ioc_addr);
			return 0;
		case 2:
		case 3:
		{
			switch((ioc_addr & 0x70000) >> 16)
			{
				case 0: return ioc_ctrl_r(space,offset,mem_mask);
				case 1:
					#ifdef MESS
						logerror("17XX: R @ addr %x mask %08x\n", offset*4, mem_mask);
						return wd17xx_data_r(fdc, offset&0xf);
					#else
						logerror("Read from FDC device?\n");
						return 0;
					#endif
				case 2:
					logerror("IOC: Econet Read %08x\n",ioc_addr);
					return 0xffff;
				case 3:
					logerror("IOC: Serial Read\n");
					return 0xffff;
				case 4:
					logerror("IOC: Internal Podule Read\n");
					return 0xffff;
				case 5:
					switch(ioc_addr & 0xfffc)
					{
						#ifdef MESS
						case 0x50: return 0; //fdc type, new model returns 5 here
						#endif
					}

					logerror("IOC: Internal Latches Read %08x\n",ioc_addr);

					return 0xffff;
			}
		}
	}

	logerror("IOC: Unknown read at %08x\n",ioc_addr);

	return 0;
}

WRITE32_HANDLER(archimedes_ioc_w)
{
	UINT32 ioc_addr;
	#ifdef MESS
	device_t *fdc = (device_t *)space->machine().device("wd1772");
	#endif

	ioc_addr = offset*4;

	switch((ioc_addr & 0x300000) >> 20)
	{
		/*82c711*/
		case 0:
			logerror("82c711 write %08x to address %08x\n",data,ioc_addr);
			return;
		case 2:
		case 3:
		{
			switch((ioc_addr & 0x70000) >> 16)
			{
				case 0: ioc_ctrl_w(space,offset,data,mem_mask); return;
				case 1:
					#ifdef MESS
						logerror("17XX: %x to addr %x mask %08x\n", data, offset*4, mem_mask);
						wd17xx_data_w(fdc, offset&0xf, data&0xff);
					#else
						logerror("Write to FDC device?\n");
					#endif
						return;
				case 2:
					logerror("IOC: Econet Write %02x at %08x\n",data,ioc_addr);
					return;
				case 3:
					logerror("IOC: Serial Write %02x (%c) at %08x\n",data,data,ioc_addr);
					return;
				case 4:
					logerror("IOC: Internal Podule Write\n");
					return;
				case 5:
					switch(ioc_addr & 0xfffc)
					{
						#ifdef MESS
						case 0x18: // latch B
							wd17xx_dden_w(fdc, BIT(data, 1));
							return;

						case 0x40: // latch A
							if (data & 1) { wd17xx_set_drive(fdc,0); }
							if (data & 2) {	wd17xx_set_drive(fdc,1); }
							if (data & 4) { wd17xx_set_drive(fdc,2); }
							if (data & 8) {	wd17xx_set_drive(fdc,3); }

							wd17xx_set_side(fdc,(data & 0x10)>>4);
							//bit 5 is motor on
							return;
						#endif
					}
					break;
			}
		}
	}


	logerror("(PC=%08x) I/O: W %x @ %x (mask %08x)\n", cpu_get_pc(&space->device()), data, (offset*4)+0x3000000, mem_mask);
}

READ32_HANDLER(archimedes_vidc_r)
{
	return 0;
}

static void vidc_dynamic_res_change(running_machine &machine)
{
	/* sanity checks - first pass */
	/*
        total cycles + border end
    */
	if(vidc_regs[VIDC_HCR] && vidc_regs[VIDC_HBER] &&
	   vidc_regs[VIDC_VCR] && vidc_regs[VIDC_VBER])
	{
		/* sanity checks - second pass */
		/*
        total cycles >= border end >= border start
        */
		if((vidc_regs[VIDC_HCR] >= vidc_regs[VIDC_HBER]) &&
		   (vidc_regs[VIDC_HBER] >= vidc_regs[VIDC_HBSR]) &&
		   (vidc_regs[VIDC_VCR] >= vidc_regs[VIDC_VBER]) &&
		   (vidc_regs[VIDC_VBER] >= vidc_regs[VIDC_VBSR]))
		{
			rectangle visarea;
			attoseconds_t refresh;

			visarea.min_x = 0;
			visarea.min_y = 0;
			visarea.max_x = vidc_regs[VIDC_HBER] - vidc_regs[VIDC_HBSR] - 1;
			visarea.max_y = vidc_regs[VIDC_VBER] - vidc_regs[VIDC_VBSR];

			logerror("Configuring: htotal %d vtotal %d border %d x %d display %d x %d\n",
				vidc_regs[VIDC_HCR], vidc_regs[VIDC_VCR],
				visarea.max_x, visarea.max_y,
				vidc_regs[VIDC_HDER]-vidc_regs[VIDC_HDSR],vidc_regs[VIDC_VDER]-vidc_regs[VIDC_VDSR]+1);

			/* FIXME: pixel clock */
			refresh = HZ_TO_ATTOSECONDS(pixel_rate[vidc_pixel_clk]*2) * vidc_regs[VIDC_HCR] * vidc_regs[VIDC_VCR];

			machine.primary_screen->configure(vidc_regs[VIDC_HCR], vidc_regs[VIDC_VCR], visarea, refresh);
		}
	}
}

WRITE32_HANDLER(archimedes_vidc_w)
{
	UINT32 reg = data>>24;
	UINT32 val = data & 0xffffff;
	//#ifdef DEBUG
	static const char *const vrnames[] =
	{
		"horizontal total",
		"horizontal sync width",
		"horizontal border start",
		"horizontal display start",
		"horizontal display end",
		"horizontal border end",
		"horizontal cursor start",
		"horizontal interlace",
		"vertical total",
		"vertical sync width",
		"vertical border start",
		"vertical display start",
		"vertical display end",
		"vertical border end",
		"vertical cursor start",
		"vertical cursor end",
	};
	//#endif


	// 0x00 - 0x3c Video Palette Logical Colors (16 colors)
	// 0x40 Border Color
	// 0x44 - 0x4c Cursor Palette Logical Colors
	if (reg >= 0x00 && reg <= 0x4c)
	{
		int r,g,b;

		//i = (val & 0x1000) >> 12; //supremacy bit
		b = (val & 0x0f00) >> 8;
		g = (val & 0x00f0) >> 4;
		r = (val & 0x000f) >> 0;

		if(reg == 0x40 && val & 0xfff)
			logerror("WARNING: border color write here (PC=%08x)!\n",cpu_get_pc(&space->device()));

		palette_set_color_rgb(space->machine(), reg >> 2, pal4bit(r), pal4bit(g), pal4bit(b) );

		/* handle 8bpp colors here */
		if(reg <= 0x3c)
		{
			int i;

			for(i=0;i<0x100;i+=0x10)
			{
				b = ((val & 0x700) >> 8) | ((i & 0x80) >> 4);
				g = ((val & 0x030) >> 4) | ((i & 0x20) >> 3) | ((i & 0x40) >> 3);
				r = ((val & 0x007) >> 0) | ((i & 0x10) >> 1);

				palette_set_color_rgb(space->machine(), (reg >> 2) + 0x100 + i, pal4bit(r), pal4bit(g), pal4bit(b) );
			}
		}

	}
	else if (reg >= 0x60 && reg <= 0x7c)
	{
		vidc_stereo_reg[(reg >> 2) & 7] = val & 0x07;

//      popmessage("%02x %02x %02x %02x %02x %02x %02x %02x",vidc_stereo_reg[0],vidc_stereo_reg[1],vidc_stereo_reg[2],vidc_stereo_reg[3]
//      ,vidc_stereo_reg[4],vidc_stereo_reg[5],vidc_stereo_reg[6],vidc_stereo_reg[7]);
	}
	else if (reg >= 0x80 && reg <= 0xbc)
	{
		switch(reg)
		{
			case VIDC_HCR:  vidc_regs[VIDC_HCR] =  ((val >> 14)<<1)+1;	break;
//          case VIDC_HSWR: vidc_regs[VIDC_HSWR] = (val >> 14)+1;   break;
			case VIDC_HBSR: vidc_regs[VIDC_HBSR] = ((val >> 14)<<1)+1;	break;
			case VIDC_HDSR: vidc_regs[VIDC_HDSR] = (val >> 14); 	break;
			case VIDC_HDER: vidc_regs[VIDC_HDER] = (val >> 14); 	break;
			case VIDC_HBER: vidc_regs[VIDC_HBER] = ((val >> 14)<<1)+1;	break;
//          #define VIDC_HCSR       0x98
//          #define VIDC_HIR        0x9c

			case VIDC_VCR:  vidc_regs[VIDC_VCR] = ((val >> 14)<<1)+1;	break;
//          #define VIDC_VSWR       0xa4
			case VIDC_VBSR: vidc_regs[VIDC_VBSR] = (val >> 14)+1;	break;
			case VIDC_VDSR: vidc_regs[VIDC_VDSR] = (val >> 14)+1;	break;
			case VIDC_VDER: vidc_regs[VIDC_VDER] = (val >> 14)+1;	break;
			case VIDC_VBER: vidc_regs[VIDC_VBER] = (val >> 14)+1;	break;
//          #define VIDC_VCSR       0xb8
//          #define VIDC_VCER       0xbc
		}


		//#ifdef DEBUG
		logerror("VIDC: %s = %d\n", vrnames[(reg-0x80)/4], vidc_regs[reg]);
		//#endif

		vidc_dynamic_res_change(space->machine());
	}
	else if(reg == 0xe0)
	{
		vidc_bpp_mode = ((val & 0x0c) >> 2);
		vidc_interlace = ((val & 0x40) >> 6);
		vidc_pixel_clk = (val & 0x03);
		vidc_dynamic_res_change(space->machine());
	}
	else
	{
		logerror("VIDC: %x to register %x\n", val, reg);
		vidc_regs[reg] = val&0xffff;
	}
}

READ32_HANDLER(archimedes_memc_r)
{
	return 0;
}

WRITE32_HANDLER(archimedes_memc_w)
{
	// is it a register?
	if ((data & 0x0fe00000) == 0x03600000)
	{
		switch ((data >> 17) & 7)
		{
			case 0: /* video init */
				vidc_vidinit = ((data>>2)&0x7fff)*16;
				//logerror("MEMC: VIDINIT %08x\n",vidc_vidinit);
				break;

			case 1: /* video start */
				vidc_vidstart = 0x2000000 | (((data>>2)&0x7fff)*16);
				//logerror("MEMC: VIDSTART %08x\n",vidc_vidstart);
				break;

			case 2: /* video end */
				vidc_vidend = 0x2000000 | (((data>>2)&0x7fff)*16);
				//logerror("MEMC: VIDEND %08x\n",vidc_vidend);
				break;

			case 4:	/* sound start */
				//logerror("MEMC: SNDSTART %08x\n",data);
				vidc_sndstart = 0x2000000 | ((data>>2)&0x7fff)*16;
				ioc_regs[IRQ_STATUS_B] &= ~ARCHIMEDES_IRQB_SOUND_EMPTY;
				break;

			case 5: /* sound end */
				//logerror("MEMC: SNDEND %08x\n",data);
				vidc_sndend = 0x2000000 | ((data>>2)&0x7fff)*16;
				break;

			case 6:
				vidc_sndcur = 0;
				archimedes_request_irq_b(space->machine(), ARCHIMEDES_IRQB_SOUND_EMPTY);
				break;

			case 7:	/* Control */
				memc_pagesize = ((data>>2) & 3);

				logerror("(PC = %08x) MEMC: %x to Control (page size %d, %s, %s)\n", cpu_get_pc(&space->device()), data & 0x1ffc, page_sizes[memc_pagesize], ((data>>10)&1) ? "Video DMA on" : "Video DMA off", ((data>>11)&1) ? "Sound DMA on" : "Sound DMA off");

				video_dma_on = ((data>>10)&1);
				audio_dma_on = ((data>>11)&1);

				if ((data>>10)&1)
				{
					vidc_vidcur = 0;
					vid_timer->adjust(space->machine().primary_screen->time_until_pos(vidc_regs[0xb4]));
				}

				if ((data>>11)&1)
				{
					double sndhz;

					/* FIXME: is the frequency correct? */
					sndhz = (250000.0 / 2) / (double)((vidc_regs[0xc0]&0xff)+2);

					printf("MEMC: Starting audio DMA at %f Hz, buffer from %x to %x\n", sndhz, vidc_sndstart, vidc_sndend);

					snd_timer->adjust(attotime::zero, 0, attotime::from_hz(sndhz));
				}

				break;

			default:
				logerror("MEMC: %x to Unk reg %d\n", data&0x1ffff, (data >> 17) & 7);
				break;
		}
	}
	else
	{
		logerror("MEMC non-reg: W %x @ %x (mask %08x)\n", data, offset, mem_mask);
	}
}

/*
          22 2222 1111 1111 1100 0000 0000
          54 3210 9876 5432 1098 7654 3210
4k  page: 11 1LLL LLLL LLLL LLAA MPPP PPPP
8k  page: 11 1LLL LLLL LLLM LLAA MPPP PPPP
16k page: 11 1LLL LLLL LLxM LLAA MPPP PPPP
32k page: 11 1LLL LLLL LxxM LLAA MPPP PPPP
       3   8    2   9    0    f    f

L - logical page
P - physical page
A - access permissions
M - MEMC number (for machines with multiple MEMCs)

The logical page is encoded with bits 11+10 being the most significant bits
(in that order), and the rest being bit 22 down.

The physical page is encoded differently depending on the page size :

4k  page:   bits 6-0 being bits 6-0
8k  page:   bits 6-1 being bits 5-0, bit 0 being bit 6
16k page:   bits 6-2 being bits 4-0, bits 1-0 being bits 6-5
32k page:   bits 6-3 being bits 4-0, bit 0 being bit 4, bit 2 being bit 5, bit
            1 being bit 6
*/

WRITE32_HANDLER(archimedes_memc_page_w)
{
	UINT32 log, phys, memc;

//	perms = (data & 0x300)>>8;
	log = phys = memc = 0;

	switch (memc_pagesize)
	{
		case 0:
			phys = data & 0x7f;
			log = ((data & 0x7ff000)>>12) | ((data & 0xc00)<<1);
			memc = (data & 0x80) ? 1 : 0;
			break;

		case 1:
			phys = ((data & 0x7f) >> 1) | ((data & 1) << 6);
			log = ((data & 0x7fe000)>>13) | (data & 0xc00);
			memc = ((data & 0x80) ? 1 : 0) | ((data & 0x1000) ? 2 : 0);
			break;

		case 2:
			phys = ((data & 0x7f) >> 2) | ((data & 3) << 5);
			log = ((data & 0x7fc000)>>14) | ((data & 0xc00)>>1);
			memc = ((data & 0x80) ? 1 : 0) | ((data & 0x1000) ? 2 : 0);
			break;

		case 3:
			phys = ((data & 0x7f) >> 3) | ((data & 1)<<4) | ((data & 2) << 5) | ((data & 4)<<3);
			log = ((data & 0x7f8000)>>15) | ((data & 0xc00)>>2);
			memc = ((data & 0x80) ? 1 : 0) | ((data & 0x1000) ? 2 : 0);
			//printf("Mapping %08X to %08X\n",0x2000000+(phys*32768),(((data >> 15)&0xff)|((data >> 2)&0x300)));
			break;
	}

//  log >>= (12 + memc_pagesize);

	// always make sure ROM mode is disconnected when this occurs
	memc_latchrom = 0;

	// now go ahead and set the mapping in the page table
	memc_pages[log] = phys + (memc*0x80);

//  printf("PC=%08x = MEMC_PAGE(%d): W %08x: log %x to phys %x, MEMC %d, perms %d\n", cpu_get_pc(&space->device()),memc_pagesize, data, log, phys, memc, perms);
}