-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
cmi.cpp
2273 lines (1876 loc) · 67.8 KB
/
cmi.cpp
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
// license:BSD-3-Clause
// copyright-holders:Phil Bennett
/***************************************************************************
Fairlight CMI Series
driver by Phil Bennett
Systems supported:
* CMI IIx
To do:
* V12 system software reports that it can't load MIDI support and then hangs.
Information from:
CMI SYSTEM SERVICE MANUAL
FAIRLIGHT INSTRUMENTS, FEBRUARY 1985
Revision 2.1
This document is available on archive.org at the following URL:
https://archive.org/details/fairlight_CMI-IIx_SERVICE_MANUAL
Summary:
The Fairlight CMI system conists typically of:
- One velocity-sensitive unweighted keyboard, with a numeric
keypad and several control surfaces
- (Optionally) one additional keyboard, not velocity-sensitive
- One alphanumeric keyboard for manual control
- A 15-inch green-screen monitor and light pen for more direct
control
- A box consisting of:
* An audio board including balanced line drivers for eight
channels and mixed output
* A 500-watt power supply
* A 21-slot backplane
* Two 8-inch double-density floppy disk drives. The format
used is soft-sectored, 128 bytes per sector (single density),
or 256 bytes per sector (double density), using FM
recording.
* And the following cards:
Slot 1: Master Card CMI-02
Slot 2: General Interface Card CMI-08/28 (Optional)
Slots 3-11: 8 Channel Controller Cards & 1 Voice Master Module Card, order unknown
Slot 12: 64K System RAM Q-096
Slot 13: 256K System RAM Q-256
Slot 14: 256K System RAM Q-256
Slot 15: 4-Port ACIA Module Q-014 (Optional)
Slot 16: Processor Control Module Q-133
Slot 17: Central Processor Module Q-209
Slot 18: Lightpen/Graphics Interface Q-219
Slot 19: Floppy Disk Controller QFC-9
Slot 20: Hard Disk Controller Q-077 (Optional)
Q209 Dual 6809 Central Processor Card
-------------------------------------
The CPU card has two 6809 processors, with robust inter-CPU
communications capabilities including:
- Uninterruptible instructions
- CPU-specific ID register and memory map registers
- Interprocessor interrupts
- Automatic memory map-switching register
The CPUs are multiplexed onto the address and data buses
in an interleaved manner such that there is no contention
on simultaneous memory accesses.
All system timing is derived from a 40MHz clock crystal, which
is divided into two opposite-phase 20MHz squre waves.
Other data entry from service manual to be completed later - RH 12 Aug 2016
****************************************************************************/
#include "emu.h"
#include "cmi01a.h"
#include "cmi_ankbd.h"
#include "cmi_mkbd.h"
#include "bus/midi/midi.h"
#include "cpu/m6809/m6809.h"
#include "cpu/m68000/m68000.h"
#include "imagedev/floppy.h"
#include "machine/6821pia.h"
#include "machine/6850acia.h"
#include "machine/6840ptm.h"
#include "machine/7474.h"
#include "machine/bankdev.h"
#include "machine/clock.h"
#include "machine/i8214.h"
#include "machine/input_merger.h"
#include "machine/mos6551.h"
#include "machine/msm5832.h"
#include "machine/wd_fdc.h"
#include "emupal.h"
#include "screen.h"
#include "speaker.h"
#define LOG_CHANNELS (1U << 1)
#define VERBOSE (0)
#include "logmacro.h"
namespace {
#define Q209_CPU_CLOCK (40.21_MHz_XTAL / 40) // verified by manual
#define SYSTEM_CAS_CLOCK (40.21_MHz_XTAL / 20) // likewise
#define M6809_CLOCK 8000000 // wrong
#define MASTER_OSCILLATOR 34.291712_MHz_XTAL
#define CPU_1 0
#define CPU_2 1
#define MAPPING_A 1
#define MAPPING_B 0
#define NUM_Q256_CARDS 1 // Max of 2
#define NUM_CHANNEL_CARDS 8
#define PAGE_SIZE 2048
#define PAGE_COUNT (65536 / PAGE_SIZE)
#define PAGE_MASK (PAGE_SIZE - 1)
#define PAGE_SHIFT 5
#define PIXEL_CLOCK 10.38_MHz_XTAL
#define HTOTAL 672
#define HBLANK_END 0
#define HBLANK_START 512
#define VTOTAL 304
#define VBLANK_END 0
#define VBLANK_START 256
#define HBLANK_FREQ (PIXEL_CLOCK / HTOTAL)
#define VBLANK_FREQ (HBLANK_FREQ / VTOTAL)
#define MAPSEL_P2_B 0x00
#define MAPSEL_P2_A 0x03
#define MAPSEL_P2_A_DMA1 0x04
#define MAPSEL_P2_A_DMA2 0x05
#define MAPSEL_P2_A_DMA3 0x06
#define MAPSEL_P2_A_DMA4 0x07
#define MAPSEL_P1_B 0x08
#define MAPSEL_P1_A 0x0b
#define MAPSEL_P1_A_DMA1 0x0c
#define MAPSEL_P1_A_DMA2 0x0d
#define MAPSEL_P1_A_DMA3 0x0e
#define MAPSEL_P1_A_DMA4 0x0f
#define IRQ_ACINT_LEVEL (0 ^ 7)
#define IRQ_MIDINT_LEVEL (0 ^ 7)
#define IRQ_TIMINT_LEVEL (1 ^ 7)
#define IRQ_INTP1_LEVEL (2 ^ 7)
#define IRQ_IPI1_LEVEL (3 ^ 7)
#define IRQ_SMIDINT_LEVEL (3 ^ 7)
#define IRQ_AIC_LEVEL (4 ^ 7)
#define IRQ_PERRINT_LEVEL (0 ^ 7)
#define IRQ_RTCINT_LEVEL (0 ^ 7)
#define IRQ_RINT_LEVEL (1 ^ 7)
#define IRQ_INTP2_LEVEL (2 ^ 7)
#define IRQ_IPI2_LEVEL (3 ^ 7)
#define IRQ_TOUCHINT_LEVEL (4 ^ 7)
#define IRQ_PENINT_LEVEL (5 ^ 7)
#define IRQ_ADINT_LEVEL (6 ^ 7)
#define IRQ_DISKINT_LEVEL (7 ^ 7)
#define FDC_CONTROL_INTEN (1 << 2)
#define FDC_STATUS_READY (1 << 3)
#define FDC_STATUS_TWO_SIDED (1 << 4)
#define FDC_STATUS_DISK_CHANGE (1 << 5)
#define FDC_STATUS_INTERRUPT (1 << 6)
#define FDC_STATUS_DRIVER_LOAD (1 << 7)
class cmi_state : public driver_device
{
public:
cmi_state(const machine_config &mconfig, device_type type, const char *tag)
: driver_device(mconfig, type, tag)
, m_maincpu1(*this, "maincpu1")
, m_maincpu2(*this, "maincpu2")
, m_midicpu(*this, "smptemidi")
, m_cmi07cpu(*this, "cmi07cpu")
, m_maincpu1_irq_merger(*this, "maincpu1_irq_merger")
, m_maincpu2_irq0_merger(*this, "maincpu2_irq0_merger")
, m_msm5832(*this, "msm5832")
, m_i8214(*this, "i8214_%u", 1U)
, m_q133_pia(*this, "q133_pia_%u", 1U)
, m_q133_ptm(*this, "q133_ptm")
, m_q133_acia(*this, "q133_acia_%u", 0U)
, m_q133_region(*this, "q133")
, m_q219_pia(*this, "q219_pia")
, m_q219_ptm(*this, "q219_ptm")
, m_cmi02_pia(*this, "cmi02_pia_%u", 1U)
, m_cmi02_ptm(*this, "cmi02_ptm")
, m_cmi07_ptm(*this, "cmi07_ptm")
, m_midi_ptm(*this, "midi_ptm_%u", 1U)
, m_midi_acia(*this, "midi_acia_%u", 1U)
, m_midi_out(*this, "midi_out_%u", 1U)
, m_midi_in(*this, "midi_in_%u", 1U)
, m_qfc9_region(*this, "qfc9")
, m_floppy(*this, "wd1791:%u", 0U)
, m_wd1791(*this, "wd1791")
, m_channels(*this, "cmi01a_%u", 0)
, m_screen(*this, "screen")
, m_palette(*this, "palette")
, m_lp_x_port(*this, "LP_X")
, m_lp_y_port(*this, "LP_Y")
, m_lp_touch_port(*this, "LP_TOUCH")
, m_cmi07_ram(*this, "cmi07_ram")
, m_cpu_periphs(*this, "cpu%u_periphs", 1U)
{
}
virtual void machine_reset() override;
virtual void machine_start() override;
void set_interrupt(int cpunum, int level, int state);
void init_cmi2x();
// CPU card
void q133_acia_irq(int state);
void i8214_cpu1_w(u8 data);
void i8214_cpu2_w(u8 data);
void maincpu2_irq0_w(int state);
void i8214_1_int_w(int state);
void i8214_2_int_w(int state);
void i8214_3_int_w(int state);
void i8214_3_enlg(int state);
u8 shared_ram_r(offs_t offset);
void shared_ram_w(offs_t offset, u8 data);
template<int CpuNum> u8 perr_r(offs_t offset);
template<int CpuNum> void perr_w(offs_t offset, u8 data);
u16 m_aic_ad565_in[16]{};
u8 m_aic_mux_latch = 0;
u8 aic_ad574_r();
template<int Dac> void aic_dac_w(u8 data);
void aic_mux_latch_w(u8 data);
void aic_ad565_msb_w(u8 data);
void aic_ad565_lsb_w(u8 data);
u8 q133_1_porta_r();
void q133_1_porta_w(u8 data);
void q133_1_portb_w(u8 data);
void cmi_iix_vblank(int state);
IRQ_CALLBACK_MEMBER(cpu1_interrupt_callback);
IRQ_CALLBACK_MEMBER(cpu2_interrupt_callback);
// Video-related
u8 video_r(offs_t offset);
u8 lightpen_r(offs_t offset);
u8 pia_q219_b_r();
void video_w(offs_t offset, u8 data);
void vscroll_w(u8 data);
void video_attr_w(u8 data);
u8 vram_r(offs_t offset);
void vram_w(offs_t offset, u8 data);
template <int CpuNum, u16 base> u8 ram_range_r(offs_t offset);
template <int CpuNum, u16 base> void ram_range_w(offs_t offset, u8 data);
template <int CpuNum> u8 vram_range_r(offs_t offset);
template <int CpuNum> void vram_range_w(offs_t offset, u8 data);
template <int CpuNum> u8 cards_range_r(offs_t offset);
template <int CpuNum> void cards_range_w(offs_t offset, u8 data);
template <int CpuNum> u8 periphs_range_r(offs_t offset);
template <int CpuNum> void periphs_range_w(offs_t offset, u8 data);
[[maybe_unused]] u8 tvt_r();
[[maybe_unused]] void tvt_w(u8 data);
void pia_q219_irqa(int state);
void pia_q219_irqb(int state);
void ptm_q219_irq(int state);
u32 screen_update_cmi2x(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect);
// Memory mapping
template<int CpuNum> u8 rom_r(offs_t offset);
void map_ram_w(offs_t offset, u8 data);
template<int CpuNum> u8 vector_r(offs_t offset);
template<int CpuNum> u8 map_r();
template<int CpuNum> void map_w(u8 data);
u8 atomic_r();
void cpufunc_w(u8 data);
u8 parity_r(offs_t offset);
void mapsel_w(offs_t offset, u8 data);
template<int CpuNum> u8 irq_ram_r(offs_t offset);
template<int CpuNum> void irq_ram_w(offs_t offset, u8 data);
template<int CpuNum> u8 scratch_ram_r(offs_t offset);
template<int CpuNum> void scratch_ram_w(offs_t offset, u8 data);
template<int CpuNum> u8 scratch_ram_fa_r(offs_t offset);
template<int CpuNum> void scratch_ram_fa_w(offs_t offset, u8 data);
// MIDI/SMPTE
void midi_dma_w(offs_t offset, u16 data, u16 mem_mask = ~0);
u16 midi_dma_r(offs_t offset);
void midi_ptm0_c3_w(int state);
void midi_latch_w(u8 data);
// Floppy
void fdc_w(offs_t offset, u8 data);
u8 fdc_r(offs_t offset);
void wd1791_irq(int state);
void wd1791_drq(int state);
// Master card
u8 cmi02_r(offs_t offset);
void cmi02_w(offs_t offset, u8 data);
void cmi02_chsel_w(u8 data);
u8 cmi02_chsel_r();
void master_tune_w(u8 data);
u8 master_tune_r();
void cmi02_ptm_irq(int state);
void cmi02_ptm_o2(int state);
void cmi02_pia2_irqa_w(int state);
void cmi02_pia2_cb2_w(int state);
u8 cmi07_r();
void cmi07_w(u8 data);
void msm5832_irq_w(int state);
void cmi07_irq(int state);
void q133_acia_clock(int state);
template<int Channel> void channel_irq(int state);
void cmi2x(machine_config &config);
void cmi07cpu_map(address_map &map);
void maincpu1_map(address_map &map);
void maincpu2_map(address_map &map);
void midicpu_map(address_map &map);
template <int CpuNum> void cpu_periphs_map(address_map &map);
protected:
required_device<mc6809e_device> m_maincpu1;
required_device<mc6809e_device> m_maincpu2;
required_device<m68000_device> m_midicpu;
required_device<mc6809e_device> m_cmi07cpu;
required_device<input_merger_any_high_device> m_maincpu1_irq_merger;
required_device<input_merger_any_high_device> m_maincpu2_irq0_merger;
required_device<msm5832_device> m_msm5832;
required_device_array<i8214_device, 3> m_i8214;
required_device_array<pia6821_device, 2> m_q133_pia;
required_device<ptm6840_device> m_q133_ptm;
required_device_array<mos6551_device, 4> m_q133_acia;
required_memory_region m_q133_region;
required_device<pia6821_device> m_q219_pia;
required_device<ptm6840_device> m_q219_ptm;
required_device_array<pia6821_device, 2> m_cmi02_pia;
required_device<ptm6840_device> m_cmi02_ptm;
required_device<ptm6840_device> m_cmi07_ptm;
required_device_array<ptm6840_device, 2> m_midi_ptm;
required_device_array<acia6850_device, 4> m_midi_acia;
required_device_array<midi_port_device, 4> m_midi_out;
required_device_array<midi_port_device, 3> m_midi_in;
required_memory_region m_qfc9_region;
required_device_array<floppy_connector, 2> m_floppy;
required_device<fd1791_device> m_wd1791;
required_device_array<cmi01a_device, 8> m_channels;
required_device<screen_device> m_screen;
required_device<palette_device> m_palette;
required_ioport m_lp_x_port;
required_ioport m_lp_y_port;
required_ioport m_lp_touch_port;
required_shared_ptr<u8> m_cmi07_ram;
required_device_array<address_map_bank_device, 2> m_cpu_periphs;
address_space *m_cpu1space = nullptr;
address_space *m_cpu2space = nullptr;
private:
emu_timer *m_map_switch_timer = nullptr;
emu_timer *m_hblank_timer = nullptr;
emu_timer *m_jam_timeout_timer = nullptr;
u8 m_video_data = 0;
// Memory
TIMER_CALLBACK_MEMBER(switch_map);
TIMER_CALLBACK_MEMBER(jam_timeout);
bool map_is_active(int cpunum, int map, u8 *map_info);
void update_address_space(int cpunum, u8 mapinfo);
// Video
TIMER_CALLBACK_MEMBER(hblank);
template <int Y, int X, bool ByteSize> void update_video_pos();
// Floppy
void dma_fdc_rom();
void write_fdc_ctrl(u8 data);
void fdc_dma_transfer();
// Q133 CPU Card
u8 *m_q133_rom = nullptr;
u16 m_int_state[2]{};
u8 m_lp_int = 0;
u8 m_hp_int = 0;
std::unique_ptr<u8[]> m_shared_ram;
std::unique_ptr<u8[]> m_scratch_ram[2];
/* Memory management */
u8 m_map_sel[16]{};
std::unique_ptr<u8[]> m_map_ram[2];
std::unique_ptr<u8[]> m_q256_ram[2];
u8 m_map_ram_latch = 0;
int m_cpu_active_space[2]{};
int m_cpu_map_switch[2]{};
u8 m_curr_mapinfo[2]{};
u8 m_irq_address[2][2]{};
int m_m6809_bs_hack_cnt[2]{};
/* Q219 lightpen/graphics card */
std::unique_ptr<u8[]> m_video_ram;
u16 m_x_pos = 0;
u8 m_y_pos = 0;
u16 m_lp_x = 0;
u8 m_lp_y = 0;
u8 m_q219_b_touch = 0;
/* QFC9 floppy disk controller card */
u8 * m_qfc9_region_ptr = 0;
int m_fdc_drq = 0;
u8 m_fdc_addr = 0;
u8 m_fdc_ctrl = 0;
u8 m_fdc_status = 0;
PAIR m_fdc_dma_addr{};
PAIR m_fdc_dma_cnt{};
/* CMI-07 */
u8 m_cmi07_ctrl = 0;
bool m_cmi07_base_enable[2]{};
u16 m_cmi07_base_addr = 0;
u8 m_msm5832_addr = 0;
// Master card (CMI-02)
int m_cmi02_ptm_irq = 0;
u8 m_cmi02_pia_chsel = 0;
u8 m_master_tune = 0;
};
/**************************************
*
* Video hardware
*
*************************************/
u32 cmi_state::screen_update_cmi2x(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
{
const pen_t *pen = m_palette->pens();
u8 y_scroll = m_q219_pia->a_output();
u8 invert = BIT(~m_q219_pia->b_output(), 3);
for (int y = cliprect.min_y; y <= cliprect.max_y; ++y)
{
u8 *src = &m_video_ram[(512/8) * ((y + y_scroll) & 0xff)];
u32 *dest = &bitmap.pix(y, cliprect.min_x);
for (int x = cliprect.min_x; x <= cliprect.max_x; x += 8)
{
u8 data = *src++;
/* Store 8 pixels */
for (int i = 0; i < 8; ++i)
*dest++ = pen[BIT(data, 7 - i) ^ invert];
}
}
/* Get lightpen position */
//if (LPCEN && NOT_TOUCHING)
if (m_lp_touch_port->read() && BIT(m_q219_pia->b_output(), 1))
{
/* Invert target pixel */
bitmap.pix(m_lp_y_port->read(), m_lp_x_port->read()) ^= 0x00ffffff;
}
return 0;
}
TIMER_CALLBACK_MEMBER(cmi_state::hblank)
{
int v = m_screen->vpos();
if (!m_screen->vblank())
{
int _touch = m_lp_touch_port->read();
int _tfh = !BIT(m_q219_pia->b_output(), 2);
if (v == m_lp_y_port->read())
{
m_q219_b_touch = _touch ? 0 : (1 << 5);
m_q219_pia->ca1_w(_touch ? 0 : 1);
if (!_touch || !_tfh)
{
/* Latch the counters */
m_lp_x = m_lp_x_port->read();
m_lp_y = m_lp_y_port->read();
/* LPSTB */
m_q219_pia->cb1_w(1);
}
}
}
/* Adjust for next scanline */
if (++v >= VTOTAL)
v = 0;
m_hblank_timer->adjust(m_screen->time_until_pos(v, HBLANK_START));
}
template void cmi_state::update_video_pos< 0, 0, false>();
template void cmi_state::update_video_pos< 0, 1, false>();
template void cmi_state::update_video_pos< 0, -1, false>();
template void cmi_state::update_video_pos< 0, 0, true>();
template void cmi_state::update_video_pos< 1, 0, false>();
template void cmi_state::update_video_pos< 1, 1, false>();
template void cmi_state::update_video_pos< 1, -1, false>();
template void cmi_state::update_video_pos< 1, 0, true>();
template void cmi_state::update_video_pos<-1, 0, false>();
template void cmi_state::update_video_pos<-1, 1, false>();
template void cmi_state::update_video_pos<-1, -1, false>();
template void cmi_state::update_video_pos<-1, 0, true>();
template <int Y, int X, bool ByteSize> void cmi_state::update_video_pos()
{
u8 *video_addr = &m_video_ram[m_y_pos * (512 / 8) + (m_x_pos / 8)];
if (ByteSize)
{
*video_addr = m_video_data;
}
else
{
int bit_mask = 1 << ((7 ^ m_x_pos) & 7);
*video_addr &= ~bit_mask;
*video_addr |= m_video_data & bit_mask;
}
m_y_pos = (m_y_pos + Y) & 0xff;
m_x_pos = (m_x_pos + X) & 0x1ff;
}
u8 cmi_state::video_r(offs_t offset)
{
if (machine().side_effects_disabled())
return m_video_data;
m_video_data = m_video_ram[m_y_pos * (512 / 8) + (m_x_pos / 8)];
switch (offset & 0x0f)
{
case 0x0: update_video_pos< 0, 0, false>(); break;
case 0x1: update_video_pos< 0, 1, false>(); break;
case 0x2: update_video_pos< 0, -1, false>(); break;
case 0x3: update_video_pos< 0, 0, true>(); break;
case 0x4: update_video_pos< 1, 0, false>(); break;
case 0x5: update_video_pos< 1, 1, false>(); break;
case 0x6: update_video_pos< 1, -1, false>(); break;
case 0x7: update_video_pos< 1, 0, true>(); break;
case 0x8: update_video_pos<-1, 0, false>(); break;
case 0x9: update_video_pos<-1, 1, false>(); break;
case 0xa: update_video_pos<-1, -1, false>(); break;
case 0xb: update_video_pos<-1, 0, true>(); break;
default: break;
}
return m_video_data;
}
u8 cmi_state::lightpen_r(offs_t offset)
{
if (offset & 2)
return m_lp_y;
else
return m_lp_x >> 1;
}
u8 cmi_state::pia_q219_b_r()
{
return ((m_lp_x << 7) & 0x80) | m_q219_b_touch;
}
void cmi_state::video_w(offs_t offset, u8 data)
{
m_video_data = data;
switch (offset & 0x0f)
{
case 0x0: update_video_pos< 0, 0, false>(); break;
case 0x1: update_video_pos< 0, 1, false>(); break;
case 0x2: update_video_pos< 0, -1, false>(); break;
case 0x3: update_video_pos< 0, 0, true>(); break;
case 0x4: update_video_pos< 1, 0, false>(); break;
case 0x5: update_video_pos< 1, 1, false>(); break;
case 0x6: update_video_pos< 1, -1, false>(); break;
case 0x7: update_video_pos< 1, 0, true>(); break;
case 0x8: update_video_pos<-1, 0, false>(); break;
case 0x9: update_video_pos<-1, 1, false>(); break;
case 0xa: update_video_pos<-1, -1, false>(); break;
case 0xb: update_video_pos<-1, 0, true>(); break;
default: break;
}
}
void cmi_state::vscroll_w(u8 data)
{
// TODO: Partial updates. Also, this should be done through a PIA
}
void cmi_state::video_attr_w(u8 data)
{
// TODO
}
void cmi_state::tvt_w(u8 data)
{
if ((data >= 0x20 && data <= 0x7e) || data == 0x0a || data == 0x0d)
{
osd_printf_debug("%c", data);
}
}
u8 cmi_state::tvt_r()
{
return 0;
}
void cmi_state::vram_w(offs_t offset, u8 data)
{
m_video_ram[offset] = data;
}
u8 cmi_state::vram_r(offs_t offset)
{
if (machine().side_effects_disabled())
return m_video_ram[offset];
/* Latch the current video position */
m_y_pos = (offset >> 6) & 0xff;
m_x_pos = (offset & 0x3f) << 3;
return m_video_ram[offset];
}
/* Memory handling */
template<int CpuNum> u8 cmi_state::rom_r(offs_t offset)
{
u16 base = (CpuNum ? 0x1000 : 0x2000);
return *(((u8 *)m_q133_region->base()) + base + offset);
}
template<int CpuNum> u8 cmi_state::perr_r(offs_t offset)
{
m_maincpu2_irq0_merger->in_w<1>(1);
const u8 page = offset >> 11;
const u8 mapinfo = m_curr_mapinfo[CpuNum];
const u8 page_info = m_map_ram[0][((mapinfo & 0x1f) << PAGE_SHIFT) + page];
const u8 data = m_q256_ram[0][(page_info & 0x7f) * PAGE_SIZE + (offset & 0x7ff)];
return data;
}
template<int CpuNum> void cmi_state::perr_w(offs_t offset, u8 data)
{
const u8 page = offset >> 11;
const u8 mapinfo = m_curr_mapinfo[CpuNum];
const u8 page_info = m_map_ram[0][((mapinfo & 0x1f) << PAGE_SHIFT) + page];
m_q256_ram[0][(page_info & 0x7f) * PAGE_SIZE + (offset & 0x7ff)] = data;
}
template <int CpuNum, u16 base> u8 cmi_state::ram_range_r(offs_t offset)
{
const u16 addr = base + offset;
const u8 mapinfo = m_curr_mapinfo[CpuNum];
const bool perr_en = BIT(mapinfo, 6);
const u8 page = addr >> 11;
const u8 page_info = m_map_ram[0][((mapinfo & 0x1f) << PAGE_SHIFT) + page];
if (m_cmi07_base_enable[CpuNum] && (addr & 0xc000) == m_cmi07_base_addr)
{
return m_cmi07_ram[(page * PAGE_SIZE) & 0x3fff];
}
if (perr_en)
{
return perr_r<CpuNum>(addr);
}
else if (BIT(page_info, 7))
{
const u32 ram_base = (page_info & 0x7f) << 11;
return m_q256_ram[0][ram_base | (addr & 0x7ff)];
}
return 0x00;
}
template <int CpuNum, u16 base> void cmi_state::ram_range_w(offs_t offset, u8 data)
{
const u16 addr = base + offset;
const u8 mapinfo = m_curr_mapinfo[CpuNum];
const bool perr_en = BIT(mapinfo, 6);
const u8 page = addr >> 11;
const u8 page_info = m_map_ram[0][((mapinfo & 0x1f) << PAGE_SHIFT) + page];
if (m_cmi07_base_enable[CpuNum] && (addr & 0xc000) == m_cmi07_base_addr)
{
m_cmi07_ram[(page * PAGE_SIZE) & 0x3fff] = data;
return;
}
if (perr_en)
{
perr_w<CpuNum>(addr, data);
}
else if (BIT(page_info, 7))
{
const u32 ram_base = (page_info & 0x7f) << 11;
m_q256_ram[0][ram_base | (addr & 0x7ff)] = data;
}
}
template <int CpuNum> u8 cmi_state::vram_range_r(offs_t offset)
{
const u8 mapinfo = m_curr_mapinfo[CpuNum];
if (!BIT(mapinfo, 5))
{
return vram_r(offset);
}
else
{
const u16 address = 0x8000 + offset;
const u8 page = (offset >> 11) + 16;
const u8 page_info = m_map_ram[0][((mapinfo & 0x1f) << PAGE_SHIFT) + page];
if (m_cmi07_base_enable[CpuNum] && (address & 0xc000) == m_cmi07_base_addr)
{
return m_cmi07_ram[(page * PAGE_SIZE) & 0x3fff];
}
if (BIT(page_info, 7))
{
const u32 ram_base = (page_info & 0x7f) << 11;
return m_q256_ram[0][ram_base | (offset & 0x7ff)];
}
}
return 0x00;
}
template <int CpuNum> void cmi_state::vram_range_w(offs_t offset, u8 data)
{
const u8 mapinfo = m_curr_mapinfo[CpuNum];
if (!BIT(mapinfo, 5))
{
vram_w(offset, data);
}
else
{
const u16 address = 0x8000 + offset;
const u8 page = (offset >> 11) + 16;
const u8 page_info = m_map_ram[0][((mapinfo & 0x1f) << PAGE_SHIFT) + page];
if (m_cmi07_base_enable[CpuNum] && (address & 0xc000) == m_cmi07_base_addr)
{
m_cmi07_ram[(page * PAGE_SIZE) & 0x3fff] = data;
return;
}
if (BIT(page_info, 7))
{
const u32 ram_base = (page_info & 0x7f) << 11;
m_q256_ram[0][ram_base | (offset & 0x7ff)] = data;
}
}
}
template <int CpuNum> u8 cmi_state::cards_range_r(offs_t offset)
{
const u8 mapinfo = m_curr_mapinfo[CpuNum];
if (!BIT(mapinfo, 7) && offset < 0x40)
{
return cmi02_r(offset);
}
else
{
const u8 page = (offset >> 11) + 28;
const u8 page_info = m_map_ram[0][((mapinfo & 0x1f) << PAGE_SHIFT) + page];
if (BIT(page_info, 7))
{
const u32 ram_base = (page_info & 0x7f) << 11;
return m_q256_ram[0][ram_base | (offset & 0x7ff)];
}
}
return 0x00;
}
template <int CpuNum> void cmi_state::cards_range_w(offs_t offset, u8 data)
{
const u8 mapinfo = m_curr_mapinfo[CpuNum];
if (!BIT(mapinfo, 7) && offset < 0x40)
{
cmi02_w(offset, data);
}
else
{
const u8 page = (offset >> 11) + 28;
const u8 page_info = m_map_ram[0][((mapinfo & 0x1f) << PAGE_SHIFT) + page];
if (BIT(page_info, 7))
{
const u32 ram_base = (page_info & 0x7f) << 11;
m_q256_ram[0][ram_base | (offset & 0x7ff)] = data;
}
}
}
template <int CpuNum> u8 cmi_state::periphs_range_r(offs_t offset)
{
const u8 mapinfo = m_curr_mapinfo[CpuNum];
if (!BIT(mapinfo, 7))
{
return m_cpu_periphs[CpuNum]->read8(offset);
}
else
{
const u8 page = (offset >> 11) + 30;
const u8 page_info = m_map_ram[0][((mapinfo & 0x1f) << PAGE_SHIFT) + page];
if (BIT(page_info, 7))
{
const u32 ram_base = (page_info & 0x7f) << 11;
return m_q256_ram[0][ram_base | (offset & 0x7ff)];
}
}
return 0x00;
}
template <int CpuNum> void cmi_state::periphs_range_w(offs_t offset, u8 data)
{
const u8 mapinfo = m_curr_mapinfo[CpuNum];
if (!BIT(mapinfo, 7))
{
m_cpu_periphs[CpuNum]->write8(offset, data);
}
else
{
const u8 page = (offset >> 11) + 30;
const u8 page_info = m_map_ram[0][((mapinfo & 0x1f) << PAGE_SHIFT) + page];
if (BIT(page_info, 7))
{
const u32 ram_base = (page_info & 0x7f) << 11;
m_q256_ram[0][ram_base | (offset & 0x7ff)] = data;
}
}
}
void cmi_state::map_ram_w(offs_t offset, u8 data)
{
if ((offset & 1) == 0)
{
m_map_ram_latch = data;
}
else
{
u8 map_info;
int map = (offset >> 6);
int page_enable = ((m_map_ram_latch & 0x80) && (0 == (m_map_ram_latch & 7))) ? 0x80 : 0;
m_map_ram[0][offset >> 1] = page_enable | (data & 0x7f);
/* Determine if this map is in use by either CPU */
if (map_is_active(CPU_1, map, &map_info))
update_address_space(0, map_info);
if (map_is_active(CPU_2, map, &map_info))
update_address_space(1, map_info);
}
}
template<int CpuNum> u8 cmi_state::vector_r(offs_t offset)
{
return m_q133_rom[(CpuNum ? 0xbfe : 0xffe) + offset];
}
template<int CpuNum> u8 cmi_state::map_r()
{
return (m_cpu_active_space[1] << 2) | (m_cpu_active_space[0] << 1) | CpuNum;
}
template<int CpuNum> void cmi_state::map_w(u8 data)
{
m_map_switch_timer->adjust(attotime::from_ticks(data & 0xf, M6809_CLOCK), CpuNum);
}
template<int CpuNum> u8 cmi_state::irq_ram_r(offs_t offset)
{
if (machine().side_effects_disabled())
return m_scratch_ram[CpuNum][0xf8 + offset];
if (m_m6809_bs_hack_cnt[CpuNum] > 0)
{
m_m6809_bs_hack_cnt[CpuNum]--;
LOG("CPU%d IRQ vector byte %d (offset %d): %02x\n", CpuNum + 1, 1 - m_m6809_bs_hack_cnt[CpuNum], offset, m_irq_address[CpuNum][offset]);
return m_irq_address[CpuNum][offset];
}
return m_scratch_ram[CpuNum][0xf8 + offset];
}
template<int CpuNum> void cmi_state::irq_ram_w(offs_t offset, u8 data)
{
m_scratch_ram[CpuNum][0xf8 + offset] = data;
}
TIMER_CALLBACK_MEMBER(cmi_state::switch_map)
{
m_cpu_active_space[param] = m_cpu_map_switch[param];
u8 map_info = (m_cpu_map_switch[param] == MAPPING_A) ?
m_map_sel[param ? MAPSEL_P2_A : MAPSEL_P1_A] :
m_map_sel[param ? MAPSEL_P2_B : MAPSEL_P1_B];
update_address_space(param, map_info);
m_map_switch_timer->adjust(attotime::never);
}
TIMER_CALLBACK_MEMBER(cmi_state::jam_timeout)
{
m_maincpu2->set_input_line(INPUT_LINE_HALT, CLEAR_LINE);
m_jam_timeout_timer->adjust(attotime::never);
}
u8 cmi_state::atomic_r()
{
// TODO
//osd_printf_debug("atomic access\n");
return 0;
}
void cmi_state::cpufunc_w(u8 data)
{
int cpunum = data & 1;
int idx = data & 6;
int bit = (data & 8) >> 3;
switch (idx)
{
case 0: set_interrupt(cpunum, IRQ_IPI2_LEVEL, bit ? ASSERT_LINE : CLEAR_LINE);
break;
case 2: // TODO: Hardware trace
break;
case 4: m_cpu_map_switch[cpunum] = bit;
break;
case 6: if (cpunum == CPU_1)
m_maincpu1->set_input_line(M6809_FIRQ_LINE, bit ? ASSERT_LINE : CLEAR_LINE);
else
m_maincpu2->set_input_line(M6809_FIRQ_LINE, bit ? ASSERT_LINE : CLEAR_LINE);
break;
}
}
u8 cmi_state::parity_r(offs_t offset)
{
m_maincpu2_irq0_merger->in_w<1>(0);
LOG("%s: parity_r %04x\n", machine().describe_context(), offset);