-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmac.c
1501 lines (1257 loc) · 36.4 KB
/
dmac.c
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
// Code copied from XM6 2.05
// and translated to C
// some code commented out, as it wasn't needed for v68
#include <string.h>
#include "v68.h"
#include "musashi/m68k.h"
#include "dmac.h"
#define ASSERT(...)
static uint32_t dmac_channel_read(struct dmac *dmac, int ch, uint32_t addr);
static void dmac_channel_write(struct dmac *dmac, int ch, uint32_t addr, uint32_t data);
static void dmac_channel_set_dcr(struct dmac *dmac, int ch, uint32_t data);
static uint32_t dmac_channel_get_dcr(struct dmac *dmac, int ch);
static void dmac_channel_set_ocr(struct dmac *dmac, int ch, uint32_t data);
static uint32_t dmac_channel_get_ocr(struct dmac *dmac, int ch);
static void dmac_channel_set_scr(struct dmac *dmac, int ch, uint32_t data);
static uint32_t dmac_channel_get_scr(struct dmac *dmac, int ch);
static void dmac_channel_set_ccr(struct dmac *dmac, int ch, uint32_t data);
static uint32_t dmac_channel_get_ccr(struct dmac *dmac, int ch);
static void dmac_channel_set_csr(struct dmac *dmac, int ch, uint32_t data);
static uint32_t dmac_channel_get_csr(struct dmac *dmac, int ch);
static void dmac_channel_set_gcr(struct dmac *dmac, uint32_t data);
static void dmac_channel_reset(struct dmac *dmac, int ch);
static void dmac_channel_start(struct dmac *dmac, int ch);
static void dmac_channel_continue(struct dmac *dmac, int ch);
static void dmac_channel_abort(struct dmac *dmac, int ch);
static void dmac_channel_load(struct dmac *dmac, int ch);
static void dmac_channel_set_error(struct dmac *dmac, int ch, uint32_t code);
static void dmac_interrupt(struct dmac *dmac);
static int dmac_channel_transfer(struct dmac *dmac, int ch);
static int mem_diff_table[8][4] = {
{ 0, 1, -1, 0}, // 8bit, バイト
{ 0, 2, -2, 0}, // 8bit, ワード
{ 0, 4, -4, 0}, // 8bit, ロングワード
{ 0, 1, -1, 0}, // 8bit, パックバイト
{ 0, 1, -1, 0}, // 16bit, バイト
{ 0, 2, -2, 0}, // 16bit, ワード
{ 0, 4, -4, 0}, // 16bit, ロングワード
{ 0, 1, -1, 0} // 16bit, パックバイト
};
static int dev_diff_table[8][4] = {
{ 0, 2, -2, 0}, // 8bit, バイト
{ 0, 4, -4, 0}, // 8bit, ワード
{ 0, 8, -8, 0}, // 8bit, ロングワード
{ 0, 2, -2, 0}, // 8bit, パックバイト
{ 0, 1, -1, 0}, // 16bit, バイト
{ 0, 2, -2, 0}, // 16bit, ワード
{ 0, 4, -4, 0}, // 16bit, ロングワード
{ 0, 1, -1, 0} // 16bit, パックバイト
};
int dmac_init(struct dmac *dmac) {
for(int ch = 0; ch < 4; ch++) {
memset(&dmac->channels[ch], 0, sizeof(dmac->channels[ch]));
}
return 1;
}
void dmac_deinit(struct dmac *dmac) {
}
void dmac_reset(struct dmac *dmac) {
int ch;
// グローバル
dmac->control.transfer = 0;
dmac->control.load = 0;
dmac->control.exec = 0;
dmac->control.current_ch = 0;
dmac->control.cpu_cycle = 0;
dmac->control.vector = -1;
// DMACチャネルを回る
for (ch=0; ch<4; ch++) {
dmac_channel_reset(dmac, ch);
}
}
uint32_t dmac_read_8(struct dmac *dmac, uint32_t addr) {
ASSERT(dmac);
ASSERT((addr >= memdev.first) && (addr <= memdev.last));
// ウェイト
//scheduler->Wait(7);
// チャネルに割り当て
int ch = (int)(addr >> 6);
ch &= 3;
addr &= 0x3f;
// チャネル単位で行う
return dmac_channel_read(dmac, ch, addr);
}
uint32_t dmac_read_16(struct dmac *dmac, uint32_t addr) {
ASSERT((addr >= memdev.first) && (addr <= memdev.last));
ASSERT((addr & 1) == 0);
// ウェイト
//scheduler->Wait(7);
// チャネルに割り当て
int ch = (int)(addr >> 6);
ch &= 3;
addr &= 0x3f;
// チャネル単位で行う
return ((dmac_channel_read(dmac, ch, addr) << 8) | dmac_channel_read(dmac, ch, addr + 1));
}
void dmac_write_8(struct dmac *dmac, uint32_t addr, uint32_t data) {
ASSERT((addr >= memdev.first) && (addr <= memdev.last));
ASSERT(data < 0x100);
// ウェイト
// scheduler->Wait(7);
// チャネルに割り当て
int ch = (int)(addr >> 6);
ch &= 3;
addr &= 0x3f;
// チャネル単位で行う
dmac_channel_write(dmac, ch, addr, data);
}
void dmac_write_16(struct dmac *dmac, uint32_t addr, uint32_t data) {
ASSERT((addr >= memdev.first) && (addr <= memdev.last));
ASSERT((addr & 1) == 0);
ASSERT(data < 0x10000);
// ウェイト
// scheduler->Wait(7);
// チャネルに割り当て
int ch = (int)(addr >> 6);
ch &= 3;
addr &= 0x3f;
// チャネル単位で行う
dmac_channel_write(dmac, ch, addr, (uint8_t)(data >> 8));
dmac_channel_write(dmac, ch, addr + 1, (uint8_t)data);
}
static uint32_t dmac_channel_read(struct dmac *dmac, int ch, uint32_t addr) {
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT(addr <= 0x3f);
switch (addr) {
// CSR
case 0x00:
return dmac_channel_get_csr(dmac, ch);
// CER
case 0x01:
return dmac->channels[ch].ecode;
// DCR
case 0x04:
return dmac_channel_get_dcr(dmac, ch);
// OCR
case 0x05:
return dmac_channel_get_ocr(dmac, ch);
// SCR
case 0x06:
return dmac_channel_get_scr(dmac, ch);
// CCR
case 0x07:
return dmac_channel_get_ccr(dmac, ch);
// MTC
case 0x0a:
return (uint8_t)(dmac->channels[ch].mtc >> 8);
case 0x0b:
return (uint8_t)dmac->channels[ch].mtc;
// MAR
case 0x0c:
return 0;
case 0x0d:
return (uint8_t)(dmac->channels[ch].mar >> 16);
case 0x0e:
return (uint8_t)(dmac->channels[ch].mar >> 8);
case 0x0f:
return (uint8_t)dmac->channels[ch].mar;
// DAR
case 0x14:
return 0;
case 0x15:
return (uint8_t)(dmac->channels[ch].dar >> 16);
case 0x16:
return (uint8_t)(dmac->channels[ch].dar >> 8);
case 0x17:
return (uint8_t)dmac->channels[ch].dar;
// BTC
case 0x1a:
return (uint8_t)(dmac->channels[ch].btc >> 8);
case 0x1b:
return (uint8_t)dmac->channels[ch].btc;
// BAR
case 0x1c:
return 0;
case 0x1d:
return (uint8_t)(dmac->channels[ch].bar >> 16);
case 0x1e:
return (uint8_t)(dmac->channels[ch].bar >> 8);
case 0x1f:
return (uint8_t)dmac->channels[ch].bar;
// NIV
case 0x25:
return dmac->channels[ch].niv;
// EIV
case 0x27:
return dmac->channels[ch].eiv;
// MFC
case 0x29:
return dmac->channels[ch].mfc;
// CPR
case 0x2d:
return dmac->channels[ch].cp;
// DFC
case 0x31:
return dmac->channels[ch].dfc;
// BFC
case 0x39:
return dmac->channels[ch].bfc;
// GCR
case 0x3f:
if (ch == 3) {
// チャネル3のみバースト転送情報を返す
ASSERT(dmac->channels[ch].bt <= 3);
ASSERT(dmac->channels[ch].br <= 3);
uint32_t data = dmac->channels[ch].bt;
data <<= 2;
data |= dmac->channels[ch].br;
return data;
}
return 0xff;
}
return 0xff;
}
static void dmac_channel_write(struct dmac *dmac, int ch, uint32_t addr, uint32_t data) {
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT(addr <= 0x3f);
ASSERT(data < 0x100);
switch (addr) {
// CSR
case 0x00:
dmac_channel_set_csr(dmac, ch, data);
return;
// CER(Read Only)
case 0x01:
return;
// DCR
case 0x04:
dmac_channel_set_dcr(dmac, ch, data);
return;
// OCR
case 0x05:
dmac_channel_set_ocr(dmac, ch, data);
return;
// SCR
case 0x06:
dmac_channel_set_scr(dmac, ch, data);
return;
// CCR
case 0x07:
dmac_channel_set_ccr(dmac, ch, data);
return;
// MTC
case 0x0a:
dmac->channels[ch].mtc &= 0x00ff;
dmac->channels[ch].mtc |= (data << 8);
return;
case 0x0b:
dmac->channels[ch].mtc &= 0xff00;
dmac->channels[ch].mtc |= data;
return;
// MAR
case 0x0c:
return;
case 0x0d:
dmac->channels[ch].mar &= 0x0000ffff;
dmac->channels[ch].mar |= (data << 16);
return;
case 0x0e:
dmac->channels[ch].mar &= 0x00ff00ff;
dmac->channels[ch].mar |= (data << 8);
return;
case 0x0f:
dmac->channels[ch].mar &= 0x00ffff00;
dmac->channels[ch].mar |= data;
return;
// DAR
case 0x14:
return;
case 0x15:
dmac->channels[ch].dar &= 0x0000ffff;
dmac->channels[ch].dar |= (data << 16);
return;
case 0x16:
dmac->channels[ch].dar &= 0x00ff00ff;
dmac->channels[ch].dar |= (data << 8);
return;
case 0x17:
dmac->channels[ch].dar &= 0x00ffff00;
dmac->channels[ch].dar |= data;
return;
// BTC
case 0x1a:
dmac->channels[ch].btc &= 0x00ff;
dmac->channels[ch].btc |= (data << 8);
return;
case 0x1b:
dmac->channels[ch].btc &= 0xff00;
dmac->channels[ch].btc |= data;
return;
// BAR
case 0x1c:
return;
case 0x1d:
dmac->channels[ch].bar &= 0x0000ffff;
dmac->channels[ch].bar |=(data << 16);
return;
case 0x1e:
dmac->channels[ch].bar &= 0x00ff00ff;
dmac->channels[ch].bar |= (data << 8);
return;
case 0x1f:
dmac->channels[ch].bar &= 0x00ffff00;
dmac->channels[ch].bar |= data;
return;
// NIV
case 0x25:
dmac->channels[ch].niv = data;
return;
// EIV
case 0x27:
dmac->channels[ch].eiv = data;
return;
// MFC
case 0x29:
dmac->channels[ch].mfc = data;
return;
// CPR
case 0x2d:
dmac->channels[ch].cp = data & 0x03;
return;
// DFC
case 0x31:
dmac->channels[ch].dfc = data;
return;
// BFC
case 0x39:
dmac->channels[ch].bfc = data;
return;
// GCR
case 0x3f:
if (ch == 3) {
// チャネル3のみ
dmac_channel_set_gcr(dmac, data);
}
return;
}
}
static void dmac_channel_set_dcr(struct dmac *dmac, int ch, uint32_t data) {
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT(data < 0x100);
// ACTが上がっていればタイミングエラー
if (dmac->channels[ch].act) {
dmac_channel_set_error(dmac, ch, 0x02);
return;
}
// XRM
dmac->channels[ch].xrm = data >> 6;
// DTYP
dmac->channels[ch].dtyp = (data >> 4) & 0x03;
// DPS
if (data & 0x08) {
dmac->channels[ch].dps = 1;
}
else {
dmac->channels[ch].dps = 0;
}
// PCL
dmac->channels[ch].pcl = (data & 0x03);
// 割り込みチェック
dmac_interrupt(dmac);
}
static uint32_t dmac_channel_get_dcr(struct dmac *dmac, int ch) {
uint32_t data;
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT(dmac->channels[ch].xrm <= 3);
ASSERT(dmac->channels[ch].dtyp <= 3);
ASSERT(dmac->channels[ch].pcl <= 3);
// データ作成
data = dmac->channels[ch].xrm;
data <<= 2;
data |= dmac->channels[ch].dtyp;
data <<= 1;
if (dmac->channels[ch].dps) {
data |= 0x01;
}
data <<= 3;
data |= dmac->channels[ch].pcl;
return data;
}
static void dmac_channel_set_ocr(struct dmac *dmac, int ch, uint32_t data) {
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT(data < 0x100);
// ACTが上がっていればタイミングエラー
if (dmac->channels[ch].act) {
dmac_channel_set_error(dmac, ch, 0x02);
return;
}
// DIR
if (data & 0x80) {
dmac->channels[ch].dir = 1;
}
else {
dmac->channels[ch].dir = 0;
}
// BTD
if (data & 0x40) {
dmac->channels[ch].btd = 1;
}
else {
dmac->channels[ch].btd = 0;
}
// SIZE
dmac->channels[ch].size = (data >> 4) & 0x03;
// CHAIN
dmac->channels[ch].chain = (data >> 2) & 0x03;
// REQG
dmac->channels[ch].reqg = (data & 0x03);
}
static uint32_t dmac_channel_get_ocr(struct dmac *dmac, int ch) {
uint32_t data;
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT(dmac->channels[ch].size <= 3);
ASSERT(dmac->channels[ch].chain <= 3);
ASSERT(dmac->channels[ch].reqg <= 3);
// データ作成
data = 0;
if (dmac->channels[ch].dir) {
data |= 0x02;
}
if (dmac->channels[ch].btd) {
data |= 0x01;
}
data <<= 2;
data |= dmac->channels[ch].size;
data <<= 2;
data |= dmac->channels[ch].chain;
data <<= 2;
data |= dmac->channels[ch].reqg;
return data;
}
static void dmac_channel_set_scr(struct dmac *dmac, int ch, uint32_t data) {
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT(data < 0x100);
// ACTが上がっていればタイミングエラー
if (dmac->channels[ch].act) {
dmac_channel_set_error(dmac, ch, 0x02);
return;
}
dmac->channels[ch].mac = (data >> 2) & 0x03;
dmac->channels[ch].dac = (data & 0x03);
}
static uint32_t dmac_channel_get_scr(struct dmac *dmac, int ch) {
uint32_t data;
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT(dmac->channels[ch].mac <= 3);
ASSERT(dmac->channels[ch].dac <= 3);
// データ作成
data = dmac->channels[ch].mac;
data <<= 2;
data |= dmac->channels[ch].dac;
return data;
}
static void dmac_channel_set_ccr(struct dmac *dmac, int ch, uint32_t data) {
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT(data < 0x100);
// INT
if (data & 0x08) {
dmac->channels[ch].intr = 1;
} else {
dmac->channels[ch].intr = 0;
}
// HLT
if (data & 0x20) {
dmac->channels[ch].hlt = 1;
} else {
dmac->channels[ch].hlt = 0;
}
// STR
if (data & 0x80) {
dmac->channels[ch].str = 1;
dmac_channel_start(dmac, ch);
}
// CNT
if (data & 0x40) {
dmac->channels[ch].cnt = 1;
dmac_channel_continue(dmac, ch);
}
// SAB
if (data & 0x10) {
dmac->channels[ch].sab = 1;
dmac_channel_abort(dmac, ch);
}
}
static uint32_t dmac_channel_get_ccr(struct dmac *dmac, int ch) {
uint32_t data;
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
// INT,HLT,STR,CNTだけ返す
data = 0;
if (dmac->channels[ch].intr) {
data |= 0x08;
}
if (dmac->channels[ch].hlt) {
data |= 0x20;
}
if (dmac->channels[ch].str) {
data |= 0x80;
}
if (dmac->channels[ch].cnt) {
data |= 0x40;
}
return data;
}
static void dmac_channel_set_csr(struct dmac *dmac, int ch, uint32_t data) {
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT(data < 0x100);
// ACT,PCS以外は1を書き込むことによりクリアできる
if (data & 0x80) {
dmac->channels[ch].coc = 0;
}
if (data & 0x40) {
dmac->channels[ch].boc = 0;
}
if (data & 0x20) {
dmac->channels[ch].ndt = 0;
}
if (data & 0x10) {
dmac->channels[ch].err = 0;
}
if (data & 0x04) {
dmac->channels[ch].dit = 0;
}
if (data & 0x02) {
dmac->channels[ch].pct = 0;
}
// 割り込み処理
dmac_interrupt(dmac);
}
static uint32_t dmac_channel_get_csr(struct dmac *dmac, int ch) {
uint32_t data;
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
// データ作成
data = 0;
if (dmac->channels[ch].coc) {
data |= 0x80;
}
if (dmac->channels[ch].boc) {
data |= 0x40;
}
if (dmac->channels[ch].ndt) {
data |= 0x20;
}
if (dmac->channels[ch].err) {
data |= 0x10;
}
if (dmac->channels[ch].act) {
data |= 0x08;
}
if (dmac->channels[ch].dit) {
data |= 0x04;
}
if (dmac->channels[ch].pct) {
data |= 0x02;
}
if (dmac->channels[ch].pcs) {
data |= 0x01;
}
return data;
}
static void dmac_channel_set_gcr(struct dmac *dmac, uint32_t data) {
int ch;
uint32_t bt;
uint32_t br;
ASSERT(dmac);
ASSERT(data < 0x100);
// データ分離
bt = (data >> 2) & 0x03;
br = data & 0x03;
// 全チャネルに設定
for (ch=0; ch<4; ch++) {
dmac->channels[ch].bt = bt;
dmac->channels[ch].br = br;
}
}
static void dmac_channel_reset(struct dmac *dmac, int ch) {
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
// GCR初期化
dmac->channels[ch].bt = 0;
dmac->channels[ch].br = 0;
// DCR初期化
dmac->channels[ch].xrm = 0;
dmac->channels[ch].dtyp = 0;
dmac->channels[ch].dps = 0;
dmac->channels[ch].pcl = 0;
// OCR初期化
dmac->channels[ch].dir = 0;
dmac->channels[ch].btd = 0;
dmac->channels[ch].size = 0;
dmac->channels[ch].chain = 0;
dmac->channels[ch].reqg = 0;
// SCR初期化
dmac->channels[ch].mac = 0;
dmac->channels[ch].dac = 0;
// CCR初期化
dmac->channels[ch].str = 0;
dmac->channels[ch].cnt = 0;
dmac->channels[ch].hlt = 0;
dmac->channels[ch].sab = 0;
dmac->channels[ch].intr = 0;
// CSR初期化
dmac->channels[ch].coc = 0;
dmac->channels[ch].boc = 0;
dmac->channels[ch].ndt = 0;
dmac->channels[ch].err = 0;
dmac->channels[ch].act = 0;
dmac->channels[ch].dit = 0;
dmac->channels[ch].pct = 0;
if (ch == 0) {
// FDCは'L'
dmac->channels[ch].pcs = 0;
} else {
// それ以外は'H'
dmac->channels[ch].pcs = 1;
}
// CPR初期化
dmac->channels[ch].cp = 0;
// CER初期化
dmac->channels[ch].ecode = 0;
// 割り込みベクタ初期化
dmac->channels[ch].niv = 0x0f;
dmac->channels[ch].eiv = 0x0f;
// アドレス及びカウンタはリセットしない(データシートによる)
dmac->channels[ch].mar &= 0x00ffffff;
dmac->channels[ch].dar &= 0x00ffffff;
dmac->channels[ch].bar &= 0x00ffffff;
dmac->channels[ch].mtc &= 0x0000ffff;
dmac->channels[ch].btc &= 0x0000ffff;
// 転送タイプ、カウンタ初期化
dmac->channels[ch].type = 0;
dmac->channels[ch].startcnt = 0;
dmac->channels[ch].errorcnt = 0;
}
static void dmac_channel_start(struct dmac *dmac, int ch) {
int c;
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
// ACT,COC,BOC,NDT,ERRが上がっていればタイミングエラー
if (dmac->channels[ch].act || dmac->channels[ch].coc || dmac->channels[ch].boc || dmac->channels[ch].ndt || dmac->channels[ch].err) {
dmac_channel_set_error(dmac, ch, 0x02);
return;
}
// チェインなしの場合は、MTC=0ならメモリカウントエラー
if (dmac->channels[ch].chain == 0) {
if (dmac->channels[ch].mtc == 0) {
dmac_channel_set_error(dmac, ch, 0x0d);
return;
}
}
// アレイチェインの場合は、BTC=0ならベースカウントエラー
if (dmac->channels[ch].chain == 0x02) {
if (dmac->channels[ch].btc == 0) {
dmac_channel_set_error(dmac, ch, 0x0f);
return;
}
}
// コンフィギュレーションエラーチェック
if ((dmac->channels[ch].xrm == 0x01) || (dmac->channels[ch].mac == 0x03) || (dmac->channels[ch].dac == 0x03)
|| (dmac->channels[ch].chain == 0x01)) {
dmac_channel_set_error(dmac, ch, 0x01);
return;
}
// 転送タイプ作成
dmac->channels[ch].type = 0;
if (dmac->channels[ch].dps) {
dmac->channels[ch].type += 4;
}
dmac->channels[ch].type += dmac->channels[ch].size;
// ワーク初期化
dmac->channels[ch].str = 0;
dmac->channels[ch].act = 1;
dmac->channels[ch].cnt = 0;
dmac->channels[ch].sab = 0;
// カウントアップ
dmac->channels[ch].startcnt++;
// アレイチェインまたはリンクアレイチェインは、最初のブロックをロード
if (dmac->channels[ch].chain != 0) {
dmac_channel_load(dmac, ch);
// ロード時にアドレスエラーまたはバスエラーが起きたら、エラーフラグが上がる
if (dmac->channels[ch].err) {
return;
}
}
// CPUサイクルをクリアして、モード別
dmac->control.cpu_cycle = 0;
switch (dmac->channels[ch].reqg) {
// オートリクエスト限定
case 0:
// オートリクエスト最大
case 1:
// 現在の残りだけDMAを動かして、CPUを止める
dmac->control.current_ch = ch;
dmac->control.cpu_cycle = 0;
dmac->control.exec = 1;
// scheduler->dma_active = 1;
c = dmac_channel_auto(dmac, 0/*cpu->Get_IOCycle()*/);
if (c == 0) {
//cpu->Release();
}
break;
// 外部要求転送
case 2:
break;
// オートリクエスト+外部要求転送
case 3:
// 現在の残りだけDMAを動かして、CPUを止める
dmac->control.current_ch = ch;
dmac->control.cpu_cycle = 0;
dmac->control.exec = 1;
// scheduler->dma_active= 1;
dmac->channels[ch].reqg = 1;
c = dmac_channel_auto(dmac, 0/*cpu->Get_IOCycle()*/);
if (c == 0) {
//cpu->Release();
}
dmac->channels[ch].reqg = 3;
break;
default:
ASSERT(0);
}
}
static void dmac_channel_continue(struct dmac *dmac, int ch) {
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
// ACTが上がっていないと動作タイミングエラー
if (!dmac->channels[ch].act) {
dmac_channel_set_error(dmac, ch, 0x02);
return;
}
// チェインモードの場合はコンフィグエラー
if (dmac->channels[ch].chain != 0) {
dmac_channel_set_error(dmac, ch, 0x01);
}
}
static void dmac_channel_abort(struct dmac *dmac, int ch) {
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
// 非アクティブならエラー処理を行わない(Marianne.pan)
if (!dmac->channels[ch].act) {
// さらにCOCを落とす(バラデューク)
dmac->channels[ch].coc = 0;
return;
}
// 転送完了、非アクティブ
dmac->channels[ch].coc = 1;
dmac->channels[ch].act = 0;
// ソフトウェアアボートでエラー発生
dmac_channel_set_error(dmac, ch, 0x11);
}
static void dmac_channel_load(struct dmac *dmac, int ch) {
uint32_t base;
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT(dmac->control.load == 0);
// ロード中(read_16でのアドレスエラー、バスエラーに注意)
dmac->control.load = (ch + 1);
if (dmac->channels[ch].bar & 1) {
// BARアドレスエラー
dmac_addr_err(dmac, dmac->channels[ch].bar, 1);
dmac->control.load = 0;
return;
}
// MAR読み込み
dmac->channels[ch].bar &= 0xfffffe;
dmac->channels[ch].mar = (m68k_read_memory_16(dmac->channels[ch].bar) & 0x00ff);
dmac->channels[ch].bar += 2;
dmac->channels[ch].mar <<= 16;
dmac->channels[ch].bar &= 0xfffffe;
dmac->channels[ch].mar |= (m68k_read_memory_16(dmac->channels[ch].bar) & 0xffff);
dmac->channels[ch].bar += 2;
// MTC読み込み
dmac->channels[ch].bar &= 0xfffffe;
dmac->channels[ch].mtc = (m68k_read_memory_16(dmac->channels[ch].bar) & 0xffff);
dmac->channels[ch].bar += 2;
if (dmac->channels[ch].err) {
// MAR,MTC読み込みエラー
dmac->control.load = 0;
return;
}
// アレイチェインではここまで
if (dmac->channels[ch].chain == 0x02) {
dmac->channels[ch].btc--;
dmac->control.load = 0;
return;
}
// リンクアレイチェイン(では次のリンクアドレスをBARへロード
dmac->channels[ch].bar &= 0xfffffe;
base = (m68k_read_memory_16(dmac->channels[ch].bar) & 0x00ff);
dmac->channels[ch].bar += 2;
base <<= 16;
dmac->channels[ch].bar &= 0xfffffe;
base |= (m68k_read_memory_16(dmac->channels[ch].bar) & 0xffff);
dmac->channels[ch].bar = base;
// ロード終了
dmac->control.load = 0;
}
static void dmac_channel_set_error(struct dmac *dmac, int ch, uint32_t code) {
ASSERT(dmac);
ASSERT((ch >= 0) && (ch <= 3));
ASSERT((code >= 0x01) && (code <= 17));
// ACTを降ろす(ファランクス ADPCM)
dmac->channels[ch].act = 0;
// エラーコードを書き込む
dmac->channels[ch].ecode = code;
// エラーフラグを立てる
dmac->channels[ch].err = 1;
// カウントアップ
dmac->channels[ch].errorcnt++;
// 割り込み処理
dmac_interrupt(dmac);
}
static void dmac_interrupt(struct dmac *dmac) {
uint32_t cp;
int ch;
ASSERT(dmac);
// DMAと同じ優先度で処理(データシートより)
for (cp=0; cp<=3; cp++) {
for (ch=0; ch<=3; ch++) {
// CPチェック