-
Notifications
You must be signed in to change notification settings - Fork 2
/
m68k.cpu
1052 lines (928 loc) · 15.5 KB
/
m68k.cpu
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
info
prefix m68k_
opcode_size 16
body m68k_run_op
header m68k.h
interrupt m68k_interrupt
include m68k_util.c
sync_cycle m68k_sync_cycle
declare
typedef m68k_context *(*m68k_reset_handler)(m68k_context *context);
void init_m68k_opts(m68k_options *opts, memmap_chunk * memmap, uint32_t num_chunks, uint32_t clock_divider);
m68k_context *init_68k_context(m68k_options * opts, m68k_reset_handler reset_handler);
void m68k_reset(m68k_context *context);
void m68k_print_regs(m68k_context *context);
regs
dregs 32 d0 d1 d2 d3 d4 d5 d6 d7
aregs 32 a0 a1 a2 a3 a4 a5 a6 a7
pc 32
other_sp 32
scratch1 32
scratch2 32
int_cycle 32
prefetch 16
int_priority 8
int_num 8
int_pending 8
int_pending_num 8
int_ack 8
status 8
ccr 8
xflag 8
nflag 8
zflag 8
vflag 8
cflag 8
reset_handler ptrvoid
mem_pointers ptrvoid 8
flags
register ccr
X 4 carry xflag
N 3 sign nflag
Z 2 zero zflag
V 1 overflow vflag
C 0 carry cflag
m68k_prefetch
if dynarec
ccall m68k_read16_noinc context pc
mov result prefetch
end
if interp
mov pc scratch1
ocall read_16
mov scratch1 prefetch
end
add 2 pc pc
check_user_mode_swap_ssp_usp
local tmp 8
and 0x20 status tmp
if tmp
else
xchg other_sp a7
end
m68k_get_sr
lsl status 8 scratch1
or ccr scratch1 scratch1
m68k_write32_lowfirst
arg value 32
add 2 scratch2 scratch2
mov value scratch1
ocall write_16
sub 2 scratch2 scratch2
lsr value 16 scratch1
ocall write_16
m68k_write32
arg value 32
local tmp 32
mov value tmp
lsr value 16 scratch1
ocall write_16
add 2 scratch2 scratch2
mov tmp scratch1
ocall write_16
m68k_read32
local tmp 32
add 2 scratch1 tmp
ocall read_16
xchg scratch1 tmp
ocall read_16
lsl tmp 16 tmp
or tmp scratch1 scratch1
m68k_interrupt
cmp int_cycle cycles
if >=U
#INT_PENDING_NONE
cmp 255 int_pending
if =
mov int_priority int_pending
mov int_num int_pending_num
else
#INT_PENDING_SR_CHANGE
cmp 254 int_pending
if =
mov int_priority int_pending
mov int_num int_pending_num
else
check_user_mode_swap_ssp_usp
cycles 6
#save status reg
sub 6 a7 a7
m68k_get_sr
mov a7 scratch2
ocall write_16
#update status register
and 0x78 status status
or int_priority status status
or 0x20 status status
#Interrupt ack cycle
mov int_pending int_ack
if int_pending_num
cycles 4
else
#TODO: do the whole E clock variable latency nonsense
cycles 13
add 24 int_pending int_pending_num
end
#save pc
add 2 a7 scratch2
m68k_write32_lowfirst pc
lsl int_pending_num 2 scratch1
m68k_read32
mov scratch1 pc
update_sync
end
m68k_run_op
dispatch prefetch
m68k_mem_src
arg address 32
arg size 16
arg isdst 8
mov address scratch1
if isdst
mov address scratch2
meta ismem 1
end
switch size
case 0
ocall read_8
case 1
ocall read_16
case 2
m68k_read32
end
meta op scratch1
m68k_write_size
arg size 16
arg lowfirst 8
switch size
case 0
ocall write_8
case 1
ocall write_16
case 2
if lowfirst
m68k_write32_lowfirst scratch1
else
m68k_write32 scratch1
end
end
m68k_index_word
m68k_prefetch
local disp 32
and prefetch 255 disp
sext 16 disp disp
sext 32 disp disp
local index 16
lsr prefetch 12 index
local isareg 16
and index 8 isareg
and index 7 index
local islong 16
and prefetch 2048 islong
switch isareg
case 0
switch islong
case 0
sext 32 dregs.index scratch1
case 2048
mov dregs.index scratch1
end
case 8
switch islong
case 0
sext 32 aregs.index scratch1
case 2048
mov aregs.index scratch1
end
end
add disp scratch1 scratch1
m68k_fetch_op_ea
arg mode 16
arg reg 16
arg Z 16
arg isdst 8
switch mode
case 0
#data reg direct
meta op dregs.reg
if isdst
meta ismem 0
end
case 1
#address reg direct
meta op aregs.reg
if isdst
meta ismem 0
end
case 2
#address reg indirect
m68k_mem_src aregs.reg Z isdst
case 3
#postincrement
m68k_mem_src aregs.reg Z isdst
switch reg
case 7
if Z
addsize Z aregs.reg aregs.reg
else
addsize 1 aregs.reg aregs.reg
end
default
addsize Z aregs.reg aregs.reg
end
case 4
#predecrement
switch reg
case 7
if Z
decsize Z aregs.reg aregs.reg
else
decsize 1 aregs.reg aregs.reg
end
default
decsize Z aregs.reg aregs.reg
end
cycles 2
m68k_mem_src aregs.reg Z isdst
case 5
#displacement
m68k_prefetch
sext 32 prefetch scratch1
add scratch1 aregs.reg scratch1
m68k_mem_src scratch1 Z isdst
case 6
#indexed
m68k_index_word
add aregs.reg scratch1 scratch1
m68k_mem_src scratch1 Z isdst
case 7
#pc-relative and absolute modes
switch reg
case 0
#absolute short
m68k_prefetch
sext 32 prefetch scratch1
m68k_mem_src scratch1 Z isdst
case 1
#absolute long
local address 32
m68k_prefetch
lsl prefetch 16 address
m68k_prefetch
or prefetch address scratch1
m68k_mem_src scratch1 Z isdst
case 2
#pc displaceent
m68k_prefetch
sext 32 prefetch scratch1
add scratch1 pc scratch1
sub 2 scratch1 scratch1
m68k_mem_src scratch1 Z isdst
case 3
#pc indexed
m68k_index_word
add pc scratch1 scratch1
sub 2 scratch1 scratch1
m68k_mem_src scratch1 Z isdst
case 4
#immediate
switch Z
case 2
local tmp32 32
m68k_prefetch
lsl prefetch 16 tmp32
m68k_prefetch
or prefetch tmp32 scratch1
default
m68k_prefetch
mov prefetch scratch1
end
meta op scratch1
end
end
m68k_fetch_src_ea
arg mode 16
arg reg 16
arg Z 16
m68k_fetch_op_ea mode reg Z 0
meta src op
switch mode
case 0
meta src_is_mem 0
case 1
meta src_is_mem 0
default
meta src_is_mem 1
end
m68k_fetch_dst_ea
arg mode 16
arg reg 16
arg Z 16
m68k_fetch_op_ea mode reg Z 1
meta dst op
m68k_save_dst
arg Z 16
if ismem
m68k_write_size Z 0
end
1101DDD0ZZMMMRRR add_ea_dn
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
invalid Z 3
m68k_fetch_src_ea M R Z
add src dregs.D dregs.D Z
update_flags XNZVC
m68k_prefetch
1101DDD1ZZMMMRRR add_dn_ea
invalid M 0
invalid M 1
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
invalid Z 3
m68k_fetch_dst_ea M R Z
add dregs.D dst dst Z
update_flags XNZVC
m68k_save_dst Z
m68k_prefetch
1101AAAZ11MMMRRR adda
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
local size 16
local ext_src 32
if Z
mov 2 size
else
mov 1 size
end
m68k_fetch_src_ea M R size
switch size
case 1
sext 32 src ext_src
meta src ext_src
end
add src aregs.A aregs.A
m68k_prefetch
00000110ZZMMMRRR addi
local immed 32
invalid Z 3
invalid M 1
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
#fetch immediate operand
m68k_prefetch
switch Z
case 2
lsl prefetch 16 immed
m68k_prefetch
or prefetch immed immed
default
mov prefetch immed
end
#fetch dst EA
m68k_fetch_dst_ea M R Z
add immed dst dst Z
update_flags XNZVC
m68k_save_dst Z
m68k_prefetch
0101III0ZZMMMRRR addq
invalid Z 3
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
local src 32
switch I
case 0
mov 8 src
default
mov I src
end
m68k_fetch_dst_ea M R Z
switch M
case 1
add src dst dst Z
default
add src dst dst Z
update_flags XNZVC
end
m68k_save_dst Z
m68k_prefetch
1101DDD1ZZ000SSS addx_dy_dx
invalid Z 3
adc dregs.S dregs.D dregs.D Z
update_flags XNVC
switch Z
case 0
local tmp8 8
mov dregs.D tmp8
if tmp8
update_flags Z0
end
case 1
local tmp16 16
mov dregs.D tmp16
if tmp16
update_flags Z0
end
case 2
if dregs.D
update_flags Z0
end
end
m68k_prefetch
1101DDD1ZZ001SSS addx_ay_ax
invalid Z 3
if Z
decsize Z aregs.S aregs.S
else
switch S
case 7
sub 2 aregs.S aregs.S
default
decsize Z aregs.S aregs.S
end
end
mov aregs.S scratch1
switch Z
case 0
ocall read_8
case 1
ocall read_16
case 2
m68k_read32
end
mov scratch1 scratch2
if Z
decsize Z aregs.D aregs.D
else
switch D
case 7
sub 2 aregs.D aregs.D
default
decsize Z aregs.D aregs.D
end
end
mov aregs.D scratch1
switch Z
case 0
ocall read_8
case 1
ocall read_16
case 2
m68k_read32
end
adc scratch2 scratch1 scratch1 Z
update_flags XNVC
switch Z
case 0
local tmp8 8
mov dregs.D tmp8
if tmp8
update_flags Z0
end
case 1
local tmp16 16
mov dregs.D tmp16
if tmp16
update_flags Z0
end
case 2
if dregs.D
update_flags Z0
end
end
mov aregs.D scratch2
m68k_write_size Z 0
m68k_prefetch
1100DDD0ZZMMMRRR and_ea_dn
invalid M 1
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
invalid Z 3
m68k_fetch_src_ea M R Z
and src dregs.D dregs.D Z
update_flags NZV0C0
m68k_prefetch
1100DDD1ZZMMMRRR and_dn_ea
invalid M 0
invalid M 1
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
invalid Z 3
m68k_fetch_dst_ea M R Z
and dregs.D dst dst Z
update_flags NZV0C0
m68k_save_dst Z
m68k_prefetch
00000010ZZMMMRRR andi
local immed 32
invalid Z 3
invalid M 1
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
#fetch immediate operand
m68k_prefetch
switch Z
case 2
lsl prefetch 16 immed
m68k_prefetch
or prefetch immed immed
default
mov prefetch immed
end
#fetch dst EA
m68k_fetch_dst_ea M R Z
and immed dst dst Z
update_flags NZV0C0
m68k_save_dst Z
m68k_prefetch
0000001000111100 andi_to_ccr
#fetch immediate operand
m68k_prefetch
and prefetch ccr ccr
m68k_prefetch
1011DDD1ZZMMMRRR eor_dn_ea
invalid M 1
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
invalid Z 3
m68k_fetch_dst_ea M R Z
xor dregs.D dst dst Z
update_flags NZV0C0
m68k_save_dst Z
m68k_prefetch
00001010ZZMMMRRR eori
local immed 32
invalid Z 3
invalid M 1
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
#fetch immediate operand
m68k_prefetch
switch Z
case 2
lsl prefetch 16 immed
m68k_prefetch
or prefetch immed immed
default
mov prefetch immed
end
#fetch dst EA
m68k_fetch_dst_ea M R Z
xor immed dst dst Z
update_flags NZV0C0
m68k_save_dst Z
m68k_prefetch
0000001000111100 eori_to_ccr
#fetch immediate operand
m68k_prefetch
xor prefetch ccr ccr
m68k_prefetch
1000DDD0ZZMMMRRR or_ea_dn
invalid M 1
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
invalid Z 3
m68k_fetch_src_ea M R Z
or src dregs.D dregs.D Z
update_flags NZV0C0
m68k_prefetch
1000DDD1ZZMMMRRR or_dn_ea
invalid M 0
invalid M 1
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
invalid Z 3
m68k_fetch_dst_ea M R Z
or dregs.D dst dst Z
update_flags NZV0C0
m68k_save_dst Z
m68k_prefetch
00000000ZZMMMRRR ori
local immed 32
invalid Z 3
invalid M 1
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
#fetch immediate operand
m68k_prefetch
switch Z
case 2
lsl prefetch 16 immed
m68k_prefetch
or prefetch immed immed
default
mov prefetch immed
end
#fetch dst EA
m68k_fetch_dst_ea M R Z
or immed dst dst Z
update_flags NZV0C0
m68k_save_dst Z
m68k_prefetch
0000000000111100 ori_to_ccr
#fetch immediate operand
m68k_prefetch
or prefetch ccr ccr
m68k_prefetch
1001DDD0ZZMMMRRR sub_ea_dn
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
invalid Z 3
m68k_fetch_src_ea M R Z
sub src dregs.D dregs.D Z
update_flags XNZVC
m68k_prefetch
1001DDD1ZZMMMRRR sub_dn_ea
invalid M 0
invalid M 1
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
invalid Z 3
m68k_fetch_dst_ea M R Z
sub dregs.D dst dst Z
update_flags XNZVC
m68k_save_dst Z
m68k_prefetch
1001AAAZ11MMMRRR suba
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
local size 16
local ext_src 32
if Z
mov 2 size
else
mov 1 size
end
m68k_fetch_src_ea M R size
switch size
case 1
sext 32 src ext_src
meta src ext_src
end
sub src aregs.A aregs.A
m68k_prefetch
00000100ZZMMMRRR subi
local immed 32
invalid Z 3
invalid M 1
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
#fetch immediate operand
m68k_prefetch
switch Z
case 2
lsl prefetch 16 immed
m68k_prefetch
or prefetch immed immed
default
mov prefetch immed
end
#fetch dst EA
m68k_fetch_dst_ea M R Z
sub immed dst dst Z
update_flags XNZVC
m68k_save_dst Z
m68k_prefetch
0101III1ZZMMMRRR subq
invalid Z 3
invalid M 7 R 2
invalid M 7 R 3
invalid M 7 R 4
invalid M 7 R 5
invalid M 7 R 6
invalid M 7 R 7
local src 32
switch I
case 0
mov 8 src
default
mov I src
end
m68k_fetch_dst_ea M R Z
switch M
case 1
sub src dst dst Z
default
sub src dst dst Z
update_flags XNZVC
end
m68k_save_dst Z
m68k_prefetch
00ZZRRRMMMEEESSS move
invalid Z 0
invalid M 1
invalid M 7 #not actually invalid, but will be handled separately due to DSL limitations
invalid E 7 S 5
invalid E 7 S 6
invalid E 7 S 7
local size 8
local memsrc 32
#move uses a different size format than most instructions
switch Z
case 1
mov 0 size
case 2
mov 2 size
case 3
mov 1 size
end
m68k_fetch_src_ea E S size
if src_is_mem
#avoid clobbering src if we need scratch1
mov src memsrc
meta src memsrc
end
cmp 0 src size
update_flags NZV0C0
switch M
case 0
mov src dregs.R size
case 2
mov aregs.R scratch2
mov src scratch1
m68k_write_size size 0
case 3
mov aregs.R scratch2
mov src scratch1
switch R
case 7
if size
addsize size aregs.R aregs.R
else
addsize 1 aregs.R aregs.R
end
default
addsize size aregs.R aregs.R
end
m68k_write_size size 0
case 4
mov src scratch1
switch R
case 7
if size
decsize size aregs.R aregs.R
else
decsize 1 aregs.R aregs.R
end
default
decsize size aregs.R aregs.R
end
mov aregs.R scratch2
m68k_write_size size 1
case 5
m68k_prefetch
sext 32 prefetch scratch2
add aregs.R scratch2 scratch2
mov src scratch1
m68k_write_size size 0
case 6
m68k_index_word
add aregs.R scratch1 scratch2
mov src scratch1
m68k_write_size size 0
end
m68k_prefetch
00ZZ00M111EEESSS move_abs
invalid E 7 S 5
invalid E 7 S 6
invalid E 7 S 7
invalid Z 0
local size 8
local memsrc 32
#move uses a different size format than most instructions
switch Z
case 1
mov 0 size
case 2
mov 2 size
case 3
mov 1 size
end
m68k_fetch_src_ea E S size
if src_is_mem
#avoid clobbering src if we need scratch1
mov src memsrc
meta src memsrc
end
cmp 0 src size
update_flags NZV0C0
switch M
case 0
m68k_prefetch
sext 32 prefetch scratch2
case 1
m68k_prefetch
lsl prefetch 16 scratch2
m68k_prefetch
or prefetch scratch2 scratch2
end
mov src scratch1
m68k_write_size size 0
m68k_prefetch
00ZZRRR001EEESSS movea
local size 8
invalid Z 0
invalid Z 1
invalid E 7 S 5
invalid E 7 S 6
invalid E 7 S 7
switch Z
case 2
mov 2 size