-
Notifications
You must be signed in to change notification settings - Fork 90
/
cpummu30.cpp
3564 lines (3165 loc) · 109 KB
/
cpummu30.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
/* Emulation of MC68030 MMU
* This code has been written for Previous - a NeXT Computer emulator
*
* This file is distributed under the GNU General Public License, version 2
* or at your option any later version. Read the file gpl.txt for details.
*
*
* Written by Andreas Grabher
*
* Many thanks go to Thomas Huth and the Hatari community for helping
* to test and debug this code!
*
*
* Release notes:
* 01-09-2012: First release
* 29-09-2012: Improved function code handling
* 16-11-2012: Improved exception handling
*
*
* - Check if read-modify-write operations are correctly detected for
* handling transparent access (see TT matching functions)
* - If possible, test mmu030_table_search with all kinds of translations
* (early termination, invalid descriptors, bus errors, indirect
* descriptors, PTEST in different levels, etc).
* - Check which bits of an ATC entry or the status register should be set
* and which should be un-set, if an invalid translation occurs.
* - Handle cache inhibit bit when accessing ATC entries
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "memory.h"
#include "newcpu.h"
#include "debug.h"
#include "cpummu030.h"
#include "cputbl.h"
#include "savestate.h"
// Prefetch mode and prefetch bus error: always flush and refill prefetch pipeline
#define MMU030_ALWAYS_FULL_PREFETCH 1
// if CPU is 68030 and faulted access' addressing mode was -(an) or (an)+
// register content is not restored when exception starts.
#define MMU030_REG_FIXUP 1
#define MMU030_OP_DBG_MSG 0
#define MMU030_ATC_DBG_MSG 0
#define MMU030_REG_DBG_MSG 0
#define TT_FC_MASK 0x00000007
#define TT_FC_BASE 0x00000070
#define TT_RWM 0x00000100
#define TT_RW 0x00000200
#define TT_CI 0x00000400
#define TT_ENABLE 0x00008000
#define TT_ADDR_MASK 0x00FF0000
#define TT_ADDR_BASE 0xFF000000
static int bBusErrorReadWrite;
static int atcindextable[32];
static int tt_enabled;
int mmu030_idx, mmu030_idx_done;
uae_u32 mm030_stageb_address;
bool mmu030_retry;
int mmu030_opcode;
int mmu030_opcode_stageb;
int mmu030_fake_prefetch;
uaecptr mmu030_fake_prefetch_addr;
uae_u16 mmu030_state[3];
uae_u32 mmu030_data_buffer_out;
uae_u32 mmu030_disp_store[2];
uae_u32 mmu030_fmovem_store[2];
uae_u8 mmu030_cache_state;
struct mmu030_access mmu030_ad[MAX_MMU030_ACCESS + 1];
bool ismoves030, islrmw030;
static void mmu030_ptest_atc_search(uaecptr logical_addr, uae_u32 fc, bool write);
static uae_u32 mmu030_table_search(uaecptr addr, uae_u32 fc, bool write, int level);
static TT_info mmu030_decode_tt(uae_u32 TT);
#if MMU_DPAGECACHE030
#define MMUFASTCACHE_ENTRIES030 256
struct mmufastcache030
{
uae_u32 log;
uae_u32 phys;
uae_u8 cs;
};
static struct mmufastcache030 atc_data_cache_read[MMUFASTCACHE_ENTRIES030];
static struct mmufastcache030 atc_data_cache_write[MMUFASTCACHE_ENTRIES030];
#endif
/* for debugging messages */
static char table_letter[4] = {'A','B','C','D'};
static const uae_u32 mmu030_size[3] = { MMU030_SSW_SIZE_B, MMU030_SSW_SIZE_W, MMU030_SSW_SIZE_L };
uae_u64 srp_030, crp_030;
uae_u32 tt0_030, tt1_030, tc_030;
uae_u16 mmusr_030;
/* ATC struct */
#define ATC030_NUM_ENTRIES 22
typedef struct {
struct {
uaecptr addr;
bool modified;
bool write_protect;
uae_u8 cache_inhibit;
bool bus_error;
} physical;
struct {
uaecptr addr;
uae_u32 fc;
bool valid;
} logical;
/* history bit */
int mru;
} MMU030_ATC_LINE;
/* MMU struct for 68030 */
static struct {
/* Translation tables */
struct {
struct {
uae_u32 mask;
uae_u8 shift;
} table[4];
struct {
uae_u32 mask;
uae_u32 imask;
uae_u32 size;
uae_u32 size3m;
} page;
uae_u8 init_shift;
uae_u8 last_table;
} translation;
/* Transparent translation */
struct {
TT_info tt0;
TT_info tt1;
} transparent;
/* Address translation cache */
MMU030_ATC_LINE atc[ATC030_NUM_ENTRIES];
/* Condition */
bool enabled;
uae_u16 status;
#if MMU_IPAGECACHE030
uae_u8 mmu030_cache_state;
#if MMU_DIRECT_ACCESS
uae_u8 *mmu030_last_physical_address_real;
#else
uae_u32 mmu030_last_physical_address;
#endif
uae_u32 mmu030_last_logical_address;
#endif
} mmu030;
/* MMU Status Register
*
* ---x ---x x-xx x---
* reserved (all 0)
*
* x--- ---- ---- ----
* bus error
*
* -x-- ---- ---- ----
* limit violation
*
* --x- ---- ---- ----
* supervisor only
*
* ---- x--- ---- ----
* write protected
*
* ---- -x-- ---- ----
* invalid
*
* ---- --x- ---- ----
* modified
*
* ---- ---- -x-- ----
* transparent access
*
* ---- ---- ---- -xxx
* number of levels (number of tables accessed during search)
*
*/
#define MMUSR_BUS_ERROR 0x8000
#define MMUSR_LIMIT_VIOLATION 0x4000
#define MMUSR_SUPER_VIOLATION 0x2000
#define MMUSR_WRITE_PROTECTED 0x0800
#define MMUSR_INVALID 0x0400
#define MMUSR_MODIFIED 0x0200
#define MMUSR_TRANSP_ACCESS 0x0040
#define MMUSR_NUM_LEVELS_MASK 0x0007
/* -- ATC flushing functions -- */
static void mmu030_flush_cache(uaecptr addr)
{
#if MMU_IPAGECACHE030
mmu030.mmu030_last_logical_address = 0xffffffff;
#endif
#if MMU_DPAGECACHE030
if (addr == 0xffffffff) {
memset(&atc_data_cache_read, 0xff, sizeof atc_data_cache_read);
memset(&atc_data_cache_write, 0xff, sizeof atc_data_cache_write);
} else {
uae_u32 idx = ((addr & mmu030.translation.page.imask) >> mmu030.translation.page.size3m) | 7;
for (int i = 0; i < MMUFASTCACHE_ENTRIES030; i++) {
if ((atc_data_cache_read[i].log | 7) == idx)
atc_data_cache_read[i].log = 0xffffffff;
if ((atc_data_cache_write[i].log | 7) == idx)
atc_data_cache_write[i].log = 0xffffffff;
}
}
#endif
}
/* This function flushes ATC entries depending on their function code */
static void mmu030_flush_atc_fc(uae_u32 fc_base, uae_u32 fc_mask) {
int i;
for (i=0; i<ATC030_NUM_ENTRIES; i++) {
if (((fc_base&fc_mask)==(mmu030.atc[i].logical.fc&fc_mask)) &&
mmu030.atc[i].logical.valid) {
mmu030.atc[i].logical.valid = false;
#if MMU030_OP_DBG_MSG
write_log(_T("ATC: Flushing %08X\n"), mmu030.atc[i].physical.addr);
#endif
}
}
mmu030_flush_cache(0xffffffff);
}
/* This function flushes ATC entries depending on their logical address
* and their function code */
static void mmu030_flush_atc_page_fc(uaecptr logical_addr, uae_u32 fc_base, uae_u32 fc_mask) {
int i;
logical_addr &= mmu030.translation.page.imask;
for (i=0; i<ATC030_NUM_ENTRIES; i++) {
if (((fc_base&fc_mask)==(mmu030.atc[i].logical.fc&fc_mask)) &&
(mmu030.atc[i].logical.addr == logical_addr) &&
mmu030.atc[i].logical.valid) {
mmu030.atc[i].logical.valid = false;
#if MMU030_OP_DBG_MSG
write_log(_T("ATC: Flushing %08X\n"), mmu030.atc[i].physical.addr);
#endif
}
}
mmu030_flush_cache(logical_addr);
}
/* This function flushes ATC entries depending on their logical address */
static void mmu030_flush_atc_page(uaecptr logical_addr) {
int i;
logical_addr &= mmu030.translation.page.imask;
for (i=0; i<ATC030_NUM_ENTRIES; i++) {
if ((mmu030.atc[i].logical.addr == logical_addr) &&
mmu030.atc[i].logical.valid) {
mmu030.atc[i].logical.valid = false;
#if MMU030_OP_DBG_MSG
write_log(_T("ATC: Flushing %08X\n"), mmu030.atc[i].physical.addr);
#endif
}
}
mmu030_flush_cache(logical_addr);
}
/* This function flushes all ATC entries */
void mmu030_flush_atc_all(void) {
#if MMU030_OP_DBG_MSG
write_log(_T("ATC: Flushing all entries\n"));
#endif
int i;
for (i=0; i<ATC030_NUM_ENTRIES; i++) {
mmu030.atc[i].logical.valid = false;
}
mmu030_flush_cache(0xffffffff);
}
/* -- Helper function for MMU instructions -- */
static bool mmu_op30_helper_get_fc(uae_u16 next, uae_u32 *fc) {
switch (next & 0x0018) {
case 0x0010:
*fc = next & 0x7;
return true;
case 0x0008:
*fc = m68k_dreg(regs, next & 0x7) & 0x7;
return true;
case 0x0000:
if (next & 1) {
*fc = regs.dfc;
} else {
*fc = regs.sfc;
}
return true;
default:
write_log(_T("MMU_OP30 ERROR: bad fc source! (%04X)\n"), next & 0x0018);
return false;
}
}
/* -- MMU instructions -- */
static bool mmu_op30_invea(uae_u32 opcode)
{
int eamode = (opcode >> 3) & 7;
int rreg = opcode & 7;
// Dn, An, (An)+, -(An), immediate and PC-relative not allowed
if (eamode == 0 || eamode == 1 || eamode == 3 || eamode == 4 || (eamode == 7 && rreg > 1))
return true;
return false;
}
int mmu_op30_pmove(uaecptr pc, uae_u32 opcode, uae_u16 next, uaecptr extra)
{
int preg = (next >> 10) & 31;
int rw = (next >> 9) & 1;
int fd = (next >> 8) & 1;
int unused = (next & 0xff);
if (mmu_op30_invea(opcode))
return 1;
// unused low 8 bits must be zeroed
if (unused)
return 1;
// read and fd set?
if (rw && fd)
return 1;
#if MMU030_OP_DBG_MSG
switch (preg) {
case 0x10:
write_log(_T("PMOVE: %s TC %08X PC=%08x\n"), rw ? _T("read"): _T("write"),
rw ? tc_030 : x_get_long(extra), m68k_getpc());
break;
case 0x12:
write_log(_T("PMOVE: %s SRP %08X%08X PC=%08x\n"), rw ? _T("read") : _T("write"),
rw?(uae_u32)(srp_030>>32)&0xFFFFFFFF:x_get_long(extra),
rw?(uae_u32)srp_030&0xFFFFFFFF:x_get_long(extra+4), m68k_getpc());
break;
case 0x13:
write_log(_T("PMOVE: %s CRP %08X%08X PC=%08x\n"), rw ? _T("read") : _T("write"),
rw?(uae_u32)(crp_030>>32)&0xFFFFFFFF:x_get_long(extra),
rw?(uae_u32)crp_030&0xFFFFFFFF:x_get_long(extra+4), m68k_getpc());
break;
case 0x18:
write_log(_T("PMOVE: %s MMUSR %04X PC=%08x\n"), rw ? _T("read") : _T("write"),
rw?mmusr_030:x_get_word(extra), m68k_getpc());
break;
case 0x02:
write_log(_T("PMOVE: %s TT0 %08X PC=%08x\n"), rw ? _T("read") : _T("write"),
rw?tt0_030:x_get_long(extra), m68k_getpc());
break;
case 0x03:
write_log(_T("PMOVE: %s TT1 %08X PC=%08x\n"), rw ? _T("read") : _T("write"),
rw?tt1_030:x_get_long(extra), m68k_getpc());
break;
default:
break;
}
if (!fd && !rw && !(preg==0x18)) {
write_log(_T("PMOVE: flush ATC\n"));
}
#endif
switch (preg)
{
case 0x10: // TC
if (rw)
x_put_long (extra, tc_030);
else {
tc_030 = x_get_long (extra);
if (mmu030_decode_tc(tc_030, true))
return -1;
}
break;
case 0x12: // SRP
if (rw) {
x_put_long (extra, srp_030 >> 32);
x_put_long (extra + 4, (uae_u32)srp_030);
} else {
srp_030 = (uae_u64)x_get_long (extra) << 32;
srp_030 |= x_get_long (extra + 4);
if (mmu030_decode_rp(srp_030))
return -1;
}
break;
case 0x13: // CRP
if (rw) {
x_put_long (extra, crp_030 >> 32);
x_put_long (extra + 4, (uae_u32)crp_030);
} else {
crp_030 = (uae_u64)x_get_long (extra) << 32;
crp_030 |= x_get_long (extra + 4);
if (mmu030_decode_rp(crp_030))
return -1;
}
break;
case 0x18: // MMUSR
if (fd) {
// FD must be always zero when MMUSR read or write
return 1;
}
if (rw)
x_put_word (extra, mmusr_030);
else
mmusr_030 = x_get_word (extra);
break;
case 0x02: // TT0
if (rw)
x_put_long (extra, tt0_030);
else {
tt0_030 = x_get_long (extra);
mmu030.transparent.tt0 = mmu030_decode_tt(tt0_030);
}
break;
case 0x03: // TT1
if (rw)
x_put_long (extra, tt1_030);
else {
tt1_030 = x_get_long (extra);
mmu030.transparent.tt1 = mmu030_decode_tt(tt1_030);
}
break;
default:
write_log (_T("Bad PMOVE at %08x\n"),m68k_getpc());
return 1;
}
if (!fd && !rw && preg != 0x18) {
mmu030_flush_atc_all();
}
tt_enabled = (tt0_030 & TT_ENABLE) || (tt1_030 & TT_ENABLE);
return 0;
}
bool mmu_op30_ptest (uaecptr pc, uae_u32 opcode, uae_u16 next, uaecptr extra)
{
mmu030.status = mmusr_030 = 0;
int level = (next&0x1C00)>>10;
int rw = (next >> 9) & 1;
int a = (next >> 8) & 1;
int areg = (next&0xE0)>>5;
uae_u32 fc;
bool write = rw ? false : true;
uae_u32 ret = 0;
if (mmu_op30_invea(opcode))
return true;
if (!mmu_op30_helper_get_fc(next, &fc))
return true;
if (!level && a) {
write_log(_T("PTEST: Bad instruction causing F-line unimplemented instruction exception!\n"));
return true;
}
#if MMU030_OP_DBG_MSG
write_log(_T("PTEST%c: addr = %08X, fc = %i, level = %i, PC=%08x, "),
rw?'R':'W', extra, fc, level, m68k_getpc());
if (a) {
write_log(_T("return descriptor to register A%i\n"), areg);
} else {
write_log(_T("do not return descriptor\n"));
}
#endif
if (!level) {
mmu030_ptest_atc_search(extra, fc, write);
} else {
ret = mmu030_table_search(extra, fc, write, level);
if (a) {
m68k_areg (regs, areg) = ret;
}
}
mmusr_030 = mmu030.status;
#if MMU030_OP_DBG_MSG
write_log(_T("PTEST status: %04X, B = %i, L = %i, S = %i, W = %i, I = %i, M = %i, T = %i, N = %i\n"),
mmusr_030, (mmusr_030&MMUSR_BUS_ERROR)?1:0, (mmusr_030&MMUSR_LIMIT_VIOLATION)?1:0,
(mmusr_030&MMUSR_SUPER_VIOLATION)?1:0, (mmusr_030&MMUSR_WRITE_PROTECTED)?1:0,
(mmusr_030&MMUSR_INVALID)?1:0, (mmusr_030&MMUSR_MODIFIED)?1:0,
(mmusr_030&MMUSR_TRANSP_ACCESS)?1:0, mmusr_030&MMUSR_NUM_LEVELS_MASK);
#endif
return false;
}
static bool mmu_op30_pload (uaecptr pc, uae_u32 opcode, uae_u16 next, uaecptr extra)
{
int rw = (next >> 9) & 1;
int unused = (next & (0x100 | 0x80 | 0x40 | 0x20));
uae_u32 fc;
bool write = rw ? false : true;
if (mmu_op30_invea(opcode))
return true;
if (unused)
return true;
if (!mmu_op30_helper_get_fc(next, &fc))
return true;
#if MMU030_OP_DBG_MSG
write_log (_T("PLOAD%c: Create ATC entry for %08X, FC = %i\n"), write?'W':'R', extra, fc);
#endif
mmu030_flush_atc_page(extra);
mmu030_table_search(extra, fc, write, 0);
return false;
}
bool mmu_op30_pflush (uaecptr pc, uae_u32 opcode, uae_u16 next, uaecptr extra)
{
uae_u16 mode = (next >> 8) & 31;
uae_u32 fc_mask = (next & 0x00E0) >> 5;
uae_u32 fc_base;
uae_u32 fc_bits = next & 0x7f;
#if MMU030_OP_DBG_MSG
switch (mode) {
case 0x1:
write_log(_T("PFLUSH: Flush all entries\n"));
break;
case 0x4:
write_log(_T("PFLUSH: Flush by function code only\n"));
write_log(_T("PFLUSH: function code: base = %08X, mask = %08X\n"), fc_base, fc_mask);
break;
case 0x6:
write_log(_T("PFLUSH: Flush by function code and effective address\n"));
write_log(_T("PFLUSH: function code: base = %08X, mask = %08X\n"), fc_base, fc_mask);
write_log(_T("PFLUSH: effective address = %08X\n"), extra);
break;
default:
break;
}
#endif
switch (mode) {
case 0x00: // PLOAD W
case 0x02: // PLOAD R
return mmu_op30_pload(pc, opcode, next, extra);
case 0x04:
if (fc_bits)
return true;
mmu030_flush_atc_all();
break;
case 0x10:
if (!mmu_op30_helper_get_fc(next, &fc_base))
return true;
mmu030_flush_atc_fc(fc_base, fc_mask);
break;
case 0x18:
if (mmu_op30_invea(opcode))
return true;
if (!mmu_op30_helper_get_fc(next, &fc_base))
return true;
mmu030_flush_atc_page_fc(extra, fc_base, fc_mask);
break;
default:
write_log(_T("PFLUSH %04x-%04x ERROR: bad mode! (%i)\n"), opcode, next, mode);
return true;
}
return false;
}
/* Transparent Translation Registers (TT0 and TT1)
*
* ---- ---- ---- ---- -xxx x--- x--- x---
* reserved, must be 0
*
* ---- ---- ---- ---- ---- ---- ---- -xxx
* function code mask (FC bits to be ignored)
*
* ---- ---- ---- ---- ---- ---- -xxx ----
* function code base (FC value for transparent block)
*
* ---- ---- ---- ---- ---- ---x ---- ----
* 0 = r/w field used, 1 = read and write is transparently translated
*
* ---- ---- ---- ---- ---- --x- ---- ----
* r/w field: 0 = write ..., 1 = read access transparent
*
* ---- ---- ---- ---- ---- -x-- ---- ----
* cache inhibit: 0 = caching allowed, 1 = caching inhibited
*
* ---- ---- ---- ---- x--- ---- ---- ----
* 0 = transparent translation enabled disabled, 1 = enabled
*
* ---- ---- xxxx xxxx ---- ---- ---- ----
* logical address mask
*
* xxxx xxxx ---- ---- ---- ---- ---- ----
* logical address base
*
*/
/* TT comparison results */
#define TT_NO_MATCH 0x1
#define TT_OK_MATCH 0x2
#define TT_NO_READ 0x4
#define TT_NO_WRITE 0x8
TT_info mmu030_decode_tt(uae_u32 TT) {
TT_info ret;
ret.fc_mask = ~((TT&TT_FC_MASK)|0xFFFFFFF8);
ret.fc_base = (TT&TT_FC_BASE)>>4;
ret.addr_base = TT & TT_ADDR_BASE;
ret.addr_mask = ~(((TT&TT_ADDR_MASK)<<8)|0x00FFFFFF);
#if MMU030_OP_DBG_MSG
if ((TT&TT_ENABLE) && !(TT&TT_RWM)) {
write_log(_T("MMU Warning: Transparent translation of read-modify-write cycle is not correctly handled!\n"));
}
#endif
#if MMU030_REG_DBG_MSG /* enable or disable debugging messages */
write_log(_T("\n"));
write_log(_T("TRANSPARENT TRANSLATION: %08X\n"), TT);
write_log(_T("\n"));
write_log(_T("TT: transparent translation "));
if (TT&TT_ENABLE) {
write_log(_T("enabled\n"));
} else {
write_log(_T("disabled\n"));
return ret;
}
write_log(_T("TT: caching %s\n"), (TT&TT_CI) ? _T("inhibited") : _T("enabled"));
write_log(_T("TT: read-modify-write "));
if (TT&TT_RWM) {
write_log(_T("enabled\n"));
} else {
write_log(_T("disabled (%s only)\n"), (TT&TT_RW) ? _T("read") : _T("write"));
}
write_log(_T("\n"));
write_log(_T("TT: function code base: %08X\n"), ret.fc_base);
write_log(_T("TT: function code mask: %08X\n"), ret.fc_mask);
write_log(_T("\n"));
write_log(_T("TT: address base: %08X\n"), ret.addr_base);
write_log(_T("TT: address mask: %08X\n"), ret.addr_mask);
write_log(_T("\n"));
#endif
return ret;
}
/* This function checks if an address matches a transparent
* translation register */
/* FIXME:
* If !(tt&TT_RMW) neither the read nor the write portion
* of a read-modify-write cycle is transparently translated! */
static int mmu030_do_match_ttr(uae_u32 tt, TT_info comp, uaecptr addr, uae_u32 fc, bool write)
{
if (tt & TT_ENABLE) { /* transparent translation enabled */
/* Compare actual function code with function code base using mask */
if ((comp.fc_base&comp.fc_mask)==(fc&comp.fc_mask)) {
/* Compare actual address with address base using mask */
if ((comp.addr_base&comp.addr_mask)==(addr&comp.addr_mask)) {
if (tt&TT_RWM) { /* r/w field disabled */
return TT_OK_MATCH;
} else {
if (tt&TT_RW) { /* read access transparent */
return write ? TT_NO_WRITE : TT_OK_MATCH;
} else { /* write access transparent */
return write ? TT_OK_MATCH : TT_NO_READ; /* TODO: check this! */
}
}
}
}
}
return TT_NO_MATCH;
}
static int mmu030_do_match_lrmw_ttr(uae_u32 tt, TT_info comp, uaecptr addr, uae_u32 fc)
{
if ((tt & TT_ENABLE) && (tt & TT_RWM)) { /* transparent translation enabled */
/* Compare actual function code with function code base using mask */
if ((comp.fc_base&comp.fc_mask)==(fc&comp.fc_mask)) {
/* Compare actual address with address base using mask */
if ((comp.addr_base&comp.addr_mask)==(addr&comp.addr_mask)) {
return TT_OK_MATCH;
}
}
}
return TT_NO_MATCH;
}
/* This function compares the address with both transparent
* translation registers and returns the result */
static int mmu030_match_ttr(uaecptr addr, uae_u32 fc, bool write)
{
int tt0, tt1;
tt0 = mmu030_do_match_ttr(tt0_030, mmu030.transparent.tt0, addr, fc, write);
if (tt0&TT_OK_MATCH) {
if (tt0_030&TT_CI)
mmu030_cache_state = CACHE_DISABLE_MMU;
}
tt1 = mmu030_do_match_ttr(tt1_030, mmu030.transparent.tt1, addr, fc, write);
if (tt1&TT_OK_MATCH) {
if (tt0_030&TT_CI)
mmu030_cache_state = CACHE_DISABLE_MMU;
}
return (tt0|tt1);
}
static int mmu030_match_ttr_access(uaecptr addr, uae_u32 fc, bool write)
{
int tt0, tt1;
if (!tt_enabled)
return 0;
tt0 = mmu030_do_match_ttr(tt0_030, mmu030.transparent.tt0, addr, fc, write);
tt1 = mmu030_do_match_ttr(tt1_030, mmu030.transparent.tt1, addr, fc, write);
return (tt0|tt1) & TT_OK_MATCH;
}
/* Locked Read-Modify-Write */
static int mmu030_match_lrmw_ttr_access(uaecptr addr, uae_u32 fc)
{
int tt0, tt1;
if (!tt_enabled)
return 0;
tt0 = mmu030_do_match_lrmw_ttr(tt0_030, mmu030.transparent.tt0, addr, fc);
tt1 = mmu030_do_match_lrmw_ttr(tt1_030, mmu030.transparent.tt1, addr, fc);
return (tt0|tt1) & TT_OK_MATCH;
}
/* Translation Control Register:
*
* x--- ---- ---- ---- ---- ---- ---- ----
* translation: 1 = enable, 0 = disable
*
* ---- --x- ---- ---- ---- ---- ---- ----
* supervisor root: 1 = enable, 0 = disable
*
* ---- ---x ---- ---- ---- ---- ---- ----
* function code lookup: 1 = enable, 0 = disable
*
* ---- ---- xxxx ---- ---- ---- ---- ----
* page size:
* 1000 = 256 bytes
* 1001 = 512 bytes
* 1010 = 1 kB
* 1011 = 2 kB
* 1100 = 4 kB
* 1101 = 8 kB
* 1110 = 16 kB
* 1111 = 32 kB
*
* ---- ---- ---- xxxx ---- ---- ---- ----
* initial shift
*
* ---- ---- ---- ---- xxxx ---- ---- ----
* number of bits for table index A
*
* ---- ---- ---- ---- ---- xxxx ---- ----
* number of bits for table index B
*
* ---- ---- ---- ---- ---- ---- xxxx ----
* number of bits for table index C
*
* ---- ---- ---- ---- ---- ----- ---- xxxx
* number of bits for table index D
*
*/
#define TC_ENABLE_TRANSLATION 0x80000000
#define TC_ENABLE_SUPERVISOR 0x02000000
#define TC_ENABLE_FCL 0x01000000
#define TC_PS_MASK 0x00F00000
#define TC_IS_MASK 0x000F0000
#define TC_TIA_MASK 0x0000F000
#define TC_TIB_MASK 0x00000F00
#define TC_TIC_MASK 0x000000F0
#define TC_TID_MASK 0x0000000F
static void mmu030_do_fake_prefetch(void)
{
if (currprefs.cpu_compatible)
return;
// fetch next opcode before MMU state switches.
// There are programs that do following:
// - enable MMU
// - JMP (An)
// "enable MMU" unmaps memory under us.
TRY (prb) {
uaecptr pc = m68k_getpci();
mmu030_fake_prefetch = -1;
mmu030_fake_prefetch_addr = mmu030_translate(pc, regs.s != 0, false, false);
mmu030_fake_prefetch = x_prefetch(0);
// A26x0 ROM code switches off rom
// NOP
// JMP (a0)
if (mmu030_fake_prefetch == 0x4e71)
mmu030_fake_prefetch = x_prefetch(2);
} CATCH (prb) {
// didn't work, oh well..
mmu030_fake_prefetch = -1;
} ENDTRY
}
bool mmu030_decode_tc(uae_u32 TC, bool check)
{
#if MMU_IPAGECACHE030
mmu030.mmu030_last_logical_address = 0xffffffff;
#endif
if (currprefs.mmu_ec)
TC &= ~TC_ENABLE_TRANSLATION;
/* Set MMU condition */
if (TC & TC_ENABLE_TRANSLATION) {
if (!mmu030.enabled && check)
mmu030_do_fake_prefetch();
mmu030.enabled = true;
} else {
if (mmu030.enabled) {
mmu030_do_fake_prefetch();
write_log(_T("MMU disabled PC=%08x\n"), M68K_GETPC);
}
mmu030.enabled = false;
return false;
}
/* Note: 0 = Table A, 1 = Table B, 2 = Table C, 3 = Table D */
int i, j;
uae_u8 TI_bits[4] = {0,0,0,0};
/* Reset variables before extracting new values from TC */
for (i = 0; i < 4; i++) {
mmu030.translation.table[i].mask = 0;
mmu030.translation.table[i].shift = 0;
}
/* Extract initial shift and page size values from TC register */
mmu030.translation.page.size = (TC & TC_PS_MASK) >> 20;
mmu030.translation.page.size3m = mmu030.translation.page.size - 3;
mmu030.translation.init_shift = (TC & TC_IS_MASK) >> 16;
regs.mmu_page_size = 1 << mmu030.translation.page.size;
write_log(_T("68030 MMU enabled. Page size = %d PC=%08x\n"), regs.mmu_page_size, M68K_GETPC);
if (mmu030.translation.page.size<8) {
write_log(_T("MMU Configuration Exception: Bad value in TC register! (bad page size: %i byte)\n"),
1<<mmu030.translation.page.size);
Exception(56); /* MMU Configuration Exception */
return true;
}
mmu030.translation.page.mask = regs.mmu_page_size - 1;
mmu030.translation.page.imask = ~mmu030.translation.page.mask;
/* Calculate masks and shifts for later extracting table indices
* from logical addresses using: index = (addr&mask)>>shift */
/* Get number of bits for each table index */
for (i = 0; i < 4; i++) {
j = (3-i)*4;
TI_bits[i] = (TC >> j) & 0xF;
}
/* Calculate masks and shifts for each table */
mmu030.translation.last_table = 0;
uae_u8 shift = 32 - mmu030.translation.init_shift;
for (i = 0; (i < 4) && TI_bits[i]; i++) {
/* Get the shift */
shift -= TI_bits[i];
mmu030.translation.table[i].shift = shift;
/* Build the mask */
for (j = 0; j < TI_bits[i]; j++) {
mmu030.translation.table[i].mask |= (1<<(mmu030.translation.table[i].shift + j));
}
/* Update until reaching the last table */
mmu030.translation.last_table = i;
}
#if MMU030_REG_DBG_MSG
/* At least one table has to be defined using at least
* 1 bit for the index. At least 2 bits are necessary
* if there is no second table. If these conditions are
* not met, it will automatically lead to a sum <32
* and cause an exception (see below). */
if (!TI_bits[0]) {
write_log(_T("MMU Configuration Exception: Bad value in TC register! (no first table index defined)\n"));
} else if ((TI_bits[0]<2) && !TI_bits[1]) {
write_log(_T("MMU Configuration Exception: Bad value in TC register! (no second table index defined and)\n"));
write_log(_T("MMU Configuration Exception: Bad value in TC register! (only 1 bit for first table index)\n"));
}
#endif
/* TI fields are summed up until a zero field is reached (see above
* loop). The sum of all TI field values plus page size and initial
* shift has to be 32: IS + PS + TIA + TIB + TIC + TID = 32 */
if ((shift-mmu030.translation.page.size)!=0) {
write_log(_T("MMU Configuration Exception: Bad value in TC register! (bad sum)\n"));
Exception(56); /* MMU Configuration Exception */
return true;
}
#if MMU030_REG_DBG_MSG /* enable or disable debugging output */
write_log(_T("\n"));
write_log(_T("TRANSLATION CONTROL: %08X\n"), TC);
write_log(_T("\n"));
write_log(_T("TC: translation %s\n"), (TC&TC_ENABLE_TRANSLATION ? _T("enabled") : _T("disabled")));
write_log(_T("TC: supervisor root pointer %s\n"), (TC&TC_ENABLE_SUPERVISOR ? _T("enabled") : _T("disabled")));
write_log(_T("TC: function code lookup %s\n"), (TC&TC_ENABLE_FCL ? _T("enabled") : _T("disabled")));
write_log(_T("\n"));
write_log(_T("TC: Initial Shift: %i\n"), mmu030.translation.init_shift);
write_log(_T("TC: Page Size: %i byte\n"), (1<<mmu030.translation.page.size));
write_log(_T("\n"));
for (i = 0; i <= mmu030.translation.last_table; i++) {
write_log(_T("TC: Table %c: mask = %08X, shift = %i\n"), table_letter[i], mmu030.translation.table[i].mask, mmu030.translation.table[i].shift);
}
write_log(_T("TC: Page: mask = %08X\n"), mmu030.translation.page.mask);
write_log(_T("\n"));
write_log(_T("TC: Last Table: %c\n"), table_letter[mmu030.translation.last_table]);
write_log(_T("\n"));
#endif
return false;
}
/* Root Pointer Registers (SRP and CRP)
*
* ---- ---- ---- ---- xxxx xxxx xxxx xx-- ---- ---- ---- ---- ---- ---- ---- xxxx
* reserved, must be 0
*
* ---- ---- ---- ---- ---- ---- ---- ---- xxxx xxxx xxxx xxxx xxxx xxxx xxxx ----
* table A address
*
* ---- ---- ---- ---- ---- ---- ---- --xx ---- ---- ---- ---- ---- ---- ---- ----
* descriptor type
*
* -xxx xxxx xxxx xxxx ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
* limit
*
* x--- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
* 0 = upper limit, 1 = lower limit
*
*/
#define RP_ADDR_MASK (UVAL64(0x00000000FFFFFFF0))
#define RP_DESCR_MASK (UVAL64(0x0000000300000000))
#define RP_LIMIT_MASK (UVAL64(0x7FFF000000000000))
#define RP_LOWER_MASK (UVAL64(0x8000000000000000))
#define RP_ZERO_BITS 0x0000FFFC /* These bits in upper longword of RP must be 0 */
bool mmu030_decode_rp(uae_u64 RP) {
uae_u8 descriptor_type = (uae_u8)((RP & RP_DESCR_MASK) >> 32);
if (!descriptor_type) { /* If descriptor type is invalid */
write_log(_T("MMU Configuration Exception: Root Pointer is invalid!\n"));
Exception(56); /* MMU Configuration Exception */
return true;
}
return false;