-
Notifications
You must be signed in to change notification settings - Fork 1
/
emu68k.c
3225 lines (2626 loc) · 80.9 KB
/
emu68k.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
/*
* emu68k.c
*
* emulation of the 68000
*/
/* $Id: emu68k.c,v 1.58 2001/02/25 22:05:30 nyef Exp $ */
#include <stdlib.h>
#include "cal.h"
#include "ui.h"
#include "emu68k.h"
#include "tool.h"
#include "types.h"
/* #define USE_UNTESTED */
/*
* Instruction handler interface
*/
typedef void (*m68k_instr_handler)(struct emu68k_context *, u16);
struct m68k_instr {
u16 mask;
u16 pattern;
m68k_instr_handler handler;
char *name;
};
struct m68k_instr *m68k_instrs[16];
/*
* Register and memory access
*/
#define WRITE_D_REG_BYTE(context, reg, data) {context->regs_d[reg] &= ~0xff; context->regs_d[reg] |= data;}
#define WRITE_D_REG_WORD(context, reg, data) {context->regs_d[reg] &= ~0xffff; context->regs_d[reg] |= data;}
#define WRITE_D_REG_LONG(context, reg, data) {context->regs_d[reg] = data;}
u8 emu68k_read_8(struct emu68k_context *context, u32 addr)
{
context->cycles_left -= 4;
return context->read8table[(addr >> context->memshift)
& context->memmask](context->cpu, addr);
}
u16 emu68k_read_16(struct emu68k_context *context, u32 addr)
{
/* FIXME: BUS error check goes here */
context->cycles_left -= 4;
return context->read16table[(addr >> context->memshift)
& context->memmask](context->cpu, addr);
}
u32 emu68k_read_32(struct emu68k_context *context, u32 addr)
{
u16 tmp1;
u16 tmp2;
/* FIXME: BUS error check goes here */
/* FIXME: check if this is the correct order */
tmp1 = emu68k_read_16(context, addr);
tmp2 = emu68k_read_16(context, addr + 2);
return (tmp1 << 16) | tmp2;
}
void emu68k_write_8(struct emu68k_context *context, u32 addr, u8 data)
{
context->cycles_left -= 4;
context->write8table[(addr >> context->memshift)
& context->memmask](context->cpu, addr, data);
}
void emu68k_write_16(struct emu68k_context *context, u32 addr, u16 data)
{
/* FIXME: BUS error check goes here */
context->cycles_left -= 4;
context->write16table[(addr >> context->memshift)
& context->memmask](context->cpu, addr, data);
}
void emu68k_write_32(struct emu68k_context *context, u32 addr, u32 data)
{
/* FIXME: BUS error check goes here */
/* FIXME: check if this is the correct order */
emu68k_write_16(context, addr, data >> 16);
emu68k_write_16(context, addr + 2, data & 0xffff);
}
/*
* Main loop and support code
*/
u16 emu68k_opfetch_16(struct emu68k_context *context)
{
u16 retval;
retval = emu68k_read_16(context, context->pc);
context->pc += 2;
return retval;
}
u32 emu68k_opfetch_32(struct emu68k_context *context)
{
u32 retval;
retval = emu68k_read_32(context, context->pc);
context->pc += 4;
return retval;
}
void emu68k_step(struct emu68k_context *context)
{
u16 opword;
struct m68k_instr *cur_instr;
if (context->pc & 1) {
/* FIXME: bad things happen here */
}
opword = emu68k_opfetch_16(context);
cur_instr = m68k_instrs[opword >> 12];
while ((opword & cur_instr->mask) != cur_instr->pattern) {
cur_instr++;
}
cur_instr->handler(context, opword);
}
void emu68k_run(struct emu68k_context *context)
{
while (context->cycles_left > 0) {
emu68k_step(context);
if (system_flags & F_UNIMPL) {
return;
}
}
}
void emu68k_reset(struct emu68k_context *context)
{
context->regs_a[7] = emu68k_read_32(context, 0);
context->pc = emu68k_read_32(context, 4);
#if 0
context->flags = 0x2000; /* FIXME: may be wrong */
#else
emu68k_set_flags(context, 0x2000); /* FIXME: may be wrong */
#endif
}
/*
* CAL interface
*/
void cal_torr68k_irq(cal_cpu cpu, int irqno);
void cal_junk68k_reset(cal_cpu cpu)
{
emu68k_reset(cpu->data.d_junk68k);
}
void cal_junk68k_run(cal_cpu cpu)
{
/* emu68k_step(cpu->data.d_junk68k); */
emu68k_run(cpu->data.d_junk68k);
}
void cal_junk68k_runfor(cal_cpu cpu, int cycles)
{
cpu->data.d_junk68k->cycles_left += cycles;
}
void cal_junk68k_irq(cal_cpu cpu, int irqno)
{
/* emuz80_IRQ(cpu->data.d_emuz80); */
}
void cal_junk68k_nmi(cal_cpu cpu)
{
/* emuz80_NMI(cpu->data.d_emuz80); */
}
void cal_junk68k_setzpage(cal_cpu cpu, void *page0)
{
}
int cal_junk68k_timeleft(cal_cpu cpu)
{
/* return cpu->data.d_emuz80->cycles_left; */
return 0;
}
void cal_junk68k_setmmu8(cal_cpu cpu, int shift, int mask, memread8_t *rtbl, memwrite8_t *wtbl)
{
cpu->data.d_junk68k->memshift = shift;
cpu->data.d_junk68k->memmask = mask;
cpu->data.d_junk68k->read8table = rtbl;
cpu->data.d_junk68k->write8table = wtbl;
}
void cal_junk68k_setmmu16(cal_cpu cpu, int shift, int mask, memread16_t *rtbl, memwrite16_t *wtbl)
{
cpu->data.d_junk68k->read16table = rtbl;
cpu->data.d_junk68k->write16table = wtbl;
}
void cal_junk68k_init(cal_cpu *cpu)
{
(*cpu)->data.d_junk68k = calloc(1, sizeof(struct emu68k_context));
if (!(*cpu)->data.d_junk68k) {
deb_printf("Insufficient memory to create CPU.\n");
free(*cpu);
*cpu = NULL;
return;
}
(*cpu)->reset = cal_junk68k_reset;
(*cpu)->run = cal_junk68k_run;
(*cpu)->runfor = cal_junk68k_runfor;
(*cpu)->irq = cal_torr68k_irq;
(*cpu)->nmi = cal_junk68k_nmi;
(*cpu)->setzpage = cal_junk68k_setzpage;
(*cpu)->timeleft = cal_junk68k_timeleft;
(*cpu)->setmmu8 = cal_junk68k_setmmu8;
(*cpu)->setmmu16 = cal_junk68k_setmmu16;
(*cpu)->data.d_junk68k->cpu = *cpu;
/* emuz80_init((*cpu)->data.d_junk68k); */
}
/*
* Condition flag code and macros
*/
#define FLAG_NZ_byte(context, data) {context->flag_z = data; context->flag_n = data; context->flag_c = 0; context->flag_v = 0;}
#define FLAG_NZ_word(context, data) {context->flag_z = data; context->flag_n = data >> 8; context->flag_c = 0; context->flag_v = 0;}
#define FLAG_NZ_long(context, data) {context->flag_z = data; context->flag_n = data >> 24; context->flag_c = 0; context->flag_v = 0;}
/* FIXME: I don't understand these next few macros */
#define FLAG_VC_ADD_byte(context, dest, src1, src2) {context->flag_c = (dest < src1); context->flag_v = (((dest) ^ (src1)) & ((dest) ^ (src2)) & 0x80);}
#define FLAG_VC_ADD_word(context, dest, src1, src2) {context->flag_c = (dest < src1); context->flag_v = (((dest) ^ (src1)) & ((dest) ^ (src2)) & 0x8000);}
#define FLAG_VC_ADD_long(context, dest, src1, src2) {context->flag_c = (dest < src1); context->flag_v = (((dest) ^ (src1)) & ((dest) ^ (src2)) & 0x80000000);}
#define FLAG_VC_SUB_byte(context, dest, src1, src2) {context->flag_c = (src1 > src2); context->flag_v = (((dest) ^ (src1)) & ((src1) ^ (src2)) & 0x80);}
#define FLAG_VC_SUB_word(context, dest, src1, src2) {context->flag_c = (src1 > src2); context->flag_v = (((dest) ^ (src1)) & ((src1) ^ (src2)) & 0x8000);}
#define FLAG_VC_SUB_long(context, dest, src1, src2) {context->flag_c = (src1 > src2); context->flag_v = (((dest) ^ (src1)) & ((src1) ^ (src2)) & 0x80000000);}
#define FLAG_X(context) {context->flag_x = context->flag_c;}
u16 emu68k_get_flags(struct emu68k_context *context)
{
u16 retval;
retval = context->flags & ~0x001f;
if (!context->flag_z) {
retval |= 0x0004;
}
if (context->flag_n & 0x80) {
retval |= 0x0008;
}
if (context->flag_c) {
retval |= 0x0001;
}
if (context->flag_x) {
retval |= 0x0010;
}
if (context->flag_v) {
retval |= 0x0002;
}
return retval;
}
void emu68k_set_flags(struct emu68k_context *context, u16 flags)
{
context->flags = flags & ~0x001f;
context->flag_z = !(flags & 0x04);
context->flag_n = flags << 4;
context->flag_x = flags & 0x10;
context->flag_c = flags & 0x01;
context->flag_v = flags & 0x02;
}
/*
* Addressing modes
*/
#define AMT_DATA 0x1
#define AMT_MEMORY 0x2
#define AMT_CONTROL 0x4
#define AMT_ALTERABLE 0x8
#define AMT_DA (AMT_DATA | AMT_ALTERABLE)
#define AMT_MA (AMT_MEMORY | AMT_ALTERABLE)
#define AMT_CA (AMT_CONTROL | AMT_ALTERABLE)
#define AMODE_IS(amode, am_type) ((amode.type & am_type) == am_type)
struct m68k_amode {
u8 type;
u32 data;
u32 flags;
int reg;
struct emu68k_context *context;
struct m68k_amode_api *api;
};
typedef void (*m68k_amode_resolver)(struct m68k_amode *, struct emu68k_context *, int);
typedef u8 (*m68k_amode_read_byte)(struct m68k_amode *);
typedef u16 (*m68k_amode_read_word)(struct m68k_amode *);
typedef u32 (*m68k_amode_read_long)(struct m68k_amode *);
typedef void (*m68k_amode_write_byte)(struct m68k_amode *, u8);
typedef void (*m68k_amode_write_word)(struct m68k_amode *, u16);
typedef void (*m68k_amode_write_long)(struct m68k_amode *, u32);
typedef void (*m68k_amode_after)(struct m68k_amode *);
struct m68k_amode_api {
m68k_amode_resolver resolve;
m68k_amode_read_byte readbyte;
m68k_amode_read_word readword;
m68k_amode_read_long readlong;
m68k_amode_write_byte writebyte;
m68k_amode_write_word writeword;
m68k_amode_write_long writelong;
m68k_amode_after after;
};
#define AMS_BOGUS 0
#define AMS_BYTE 1
#define AMS_WORD 2
#define AMS_LONG 3
void m68k_amode_none_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
deb_printf("m68k: unimplemented amode (api: 0x%08x).\n", amode->api);
system_flags |= F_UNIMPL;
}
u8 m68k_control_amode_readbyte(struct m68k_amode *amode)
{
return emu68k_read_8(amode->context, amode->data);
}
u16 m68k_control_amode_readword(struct m68k_amode *amode)
{
return emu68k_read_16(amode->context, amode->data);
}
u32 m68k_control_amode_readlong(struct m68k_amode *amode)
{
return emu68k_read_32(amode->context, amode->data);
}
void m68k_control_amode_writebyte(struct m68k_amode *amode, u8 data)
{
emu68k_write_8(amode->context, amode->data, data);
}
void m68k_control_amode_writeword(struct m68k_amode *amode, u16 data)
{
emu68k_write_16(amode->context, amode->data, data);
}
void m68k_control_amode_writelong(struct m68k_amode *amode, u32 data)
{
emu68k_write_32(amode->context, amode->data, data);
}
void m68k_control_amode_after(struct m68k_amode *amode)
{
}
#define CONTROL_AMODE_API \
m68k_control_amode_readbyte, \
m68k_control_amode_readword, \
m68k_control_amode_readlong, \
m68k_control_amode_writebyte, \
m68k_control_amode_writeword, \
m68k_control_amode_writelong, \
m68k_control_amode_after
void m68k_amode_0_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
amode->context = context;
}
u8 m68k_amode_0_readbyte(struct m68k_amode *amode)
{
return (u8)amode->context->regs_d[amode->reg];
}
u16 m68k_amode_0_readword(struct m68k_amode *amode)
{
return (u16)amode->context->regs_d[amode->reg];
}
u32 m68k_amode_0_readlong(struct m68k_amode *amode)
{
return amode->context->regs_d[amode->reg];
}
void m68k_amode_0_writebyte(struct m68k_amode *amode, u8 data)
{
WRITE_D_REG_BYTE(amode->context, amode->reg, data);
}
void m68k_amode_0_writeword(struct m68k_amode *amode, u16 data)
{
WRITE_D_REG_WORD(amode->context, amode->reg, data);
}
void m68k_amode_0_writelong(struct m68k_amode *amode, u32 data)
{
WRITE_D_REG_LONG(amode->context, amode->reg, data);
}
void m68k_amode_0_after(struct m68k_amode *amode)
{
}
struct m68k_amode_api m68k_amode_api_0 = {
m68k_amode_0_resolve,
m68k_amode_0_readbyte,
m68k_amode_0_readword,
m68k_amode_0_readlong,
m68k_amode_0_writebyte,
m68k_amode_0_writeword,
m68k_amode_0_writelong,
m68k_amode_0_after,
};
void m68k_amode_1_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
amode->context = context;
}
u8 m68k_amode_1_readbyte(struct m68k_amode *amode)
{
/* FIXME: signal exception */
return 0;
}
u16 m68k_amode_1_readword(struct m68k_amode *amode)
{
return (u16)amode->context->regs_a[amode->reg];
}
u32 m68k_amode_1_readlong(struct m68k_amode *amode)
{
return amode->context->regs_a[amode->reg];
}
void m68k_amode_1_writebyte(struct m68k_amode *amode, u8 data)
{
/* FIXME: signal exception */
}
void m68k_amode_1_writeword(struct m68k_amode *amode, u16 data)
{
amode->context->regs_a[amode->reg] &= ~0xffff;
amode->context->regs_a[amode->reg] |= data;
}
void m68k_amode_1_writelong(struct m68k_amode *amode, u32 data)
{
amode->context->regs_a[amode->reg] = data;
}
void m68k_amode_1_after(struct m68k_amode *amode)
{
}
struct m68k_amode_api m68k_amode_api_1 = {
m68k_amode_1_resolve,
m68k_amode_1_readbyte,
m68k_amode_1_readword,
m68k_amode_1_readlong,
m68k_amode_1_writebyte,
m68k_amode_1_writeword,
m68k_amode_1_writelong,
m68k_amode_1_after,
};
void m68k_amode_2_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
amode->context = context;
amode->data = context->regs_a[amode->reg];
}
struct m68k_amode_api m68k_amode_api_2 = {
m68k_amode_2_resolve,
CONTROL_AMODE_API
};
void m68k_amode_3_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
amode->context = context;
if (size == AMS_BYTE) {
if (amode->reg != 7) {
amode->flags = 1;
} else {
amode->flags = 2;
}
} else if (size == AMS_WORD) {
amode->flags = 2;
} else if (size == AMS_LONG) {
amode->flags = 4;
} else {
deb_printf("m68k: BUG: bogus size passed to amode 3.\n");
}
amode->data = context->regs_a[amode->reg];
}
u8 m68k_amode_3_readbyte(struct m68k_amode *amode)
{
return emu68k_read_8(amode->context, amode->data);
}
u16 m68k_amode_3_readword(struct m68k_amode *amode)
{
return emu68k_read_16(amode->context, amode->data);
}
u32 m68k_amode_3_readlong(struct m68k_amode *amode)
{
return emu68k_read_32(amode->context, amode->data);
}
void m68k_amode_3_writebyte(struct m68k_amode *amode, u8 data)
{
emu68k_write_8(amode->context, amode->data, data);
}
void m68k_amode_3_writeword(struct m68k_amode *amode, u16 data)
{
emu68k_write_16(amode->context, amode->data, data);
}
void m68k_amode_3_writelong(struct m68k_amode *amode, u32 data)
{
emu68k_write_32(amode->context, amode->data, data);
}
void m68k_amode_3_after(struct m68k_amode *amode)
{
amode->context->regs_a[amode->reg] += amode->flags;
amode->data += amode->flags;
}
struct m68k_amode_api m68k_amode_api_3 = {
m68k_amode_3_resolve,
m68k_amode_3_readbyte,
m68k_amode_3_readword,
m68k_amode_3_readlong,
m68k_amode_3_writebyte,
m68k_amode_3_writeword,
m68k_amode_3_writelong,
m68k_amode_3_after,
};
void m68k_amode_4_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
/* FIXME: this may not do the right thing for MOVE.L A7, -(A7) */
amode->context = context;
if (size == AMS_BYTE) {
if (amode->reg != 7) {
amode->flags = 1;
} else {
amode->flags = 2;
}
} else if (size == AMS_WORD) {
amode->flags = 2;
} else if (size == AMS_LONG) {
amode->flags = 4;
} else {
deb_printf("m68k: BUG: bogus size passed to amode 4.\n");
}
amode->data = context->regs_a[amode->reg] - amode->flags;
}
u8 m68k_amode_4_readbyte(struct m68k_amode *amode)
{
amode->context->cycles_left -= 2;
return emu68k_read_8(amode->context, amode->data);
}
u16 m68k_amode_4_readword(struct m68k_amode *amode)
{
amode->context->cycles_left -= 2;
return emu68k_read_16(amode->context, amode->data);
}
u32 m68k_amode_4_readlong(struct m68k_amode *amode)
{
amode->context->cycles_left -= 2;
return emu68k_read_32(amode->context, amode->data);
}
void m68k_amode_4_writebyte(struct m68k_amode *amode, u8 data)
{
emu68k_write_8(amode->context, amode->data, data);
}
void m68k_amode_4_writeword(struct m68k_amode *amode, u16 data)
{
emu68k_write_16(amode->context, amode->data, data);
}
void m68k_amode_4_writelong(struct m68k_amode *amode, u32 data)
{
emu68k_write_32(amode->context, amode->data, data);
}
void m68k_amode_4_after(struct m68k_amode *amode)
{
amode->context->regs_a[amode->reg] -= amode->flags;
amode->data = amode->context->regs_a[amode->reg] - amode->flags;
}
struct m68k_amode_api m68k_amode_api_4 = {
m68k_amode_4_resolve,
m68k_amode_4_readbyte,
m68k_amode_4_readword,
m68k_amode_4_readlong,
m68k_amode_4_writebyte,
m68k_amode_4_writeword,
m68k_amode_4_writelong,
m68k_amode_4_after,
};
void m68k_amode_5_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
s16 data;
amode->context = context;
amode->data = context->regs_a[amode->reg];
data = emu68k_opfetch_16(context);
amode->data += (signed long)data;
}
struct m68k_amode_api m68k_amode_api_5 = {
m68k_amode_5_resolve,
CONTROL_AMODE_API
};
void m68k_amode_6_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
s16 data;
amode->context = context;
amode->data = context->regs_a[amode->reg];
data = emu68k_opfetch_16(context);
amode->data += (s8)data;
if (data & 0x0800) {
if (data & 0x8000) {
/* A.L */
amode->data += amode->context->regs_a[(data >> 12) & 7];
} else {
/* D.L */
amode->data += amode->context->regs_d[(data >> 12) & 7];
}
} else {
if (data & 0x8000) {
/* A.W */
amode->data += (s16)amode->context->regs_a[(data >> 12) & 7];
} else {
/* D.W */
amode->data += (s16)amode->context->regs_d[(data >> 12) & 7];
}
}
amode->context->cycles_left -= 2;
}
struct m68k_amode_api m68k_amode_api_6 = {
m68k_amode_6_resolve,
CONTROL_AMODE_API
};
void m68k_amode_7_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
s16 tmp;
amode->context = context;
tmp = emu68k_opfetch_16(context);
amode->data = tmp; /* needed for sign extension */
}
struct m68k_amode_api m68k_amode_api_7 = {
m68k_amode_7_resolve,
CONTROL_AMODE_API
};
void m68k_amode_8_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
amode->context = context;
amode->data = emu68k_opfetch_32(context);
}
struct m68k_amode_api m68k_amode_api_8 = {
m68k_amode_8_resolve,
CONTROL_AMODE_API
};
void m68k_amode_9_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
s16 data;
amode->context = context;
amode->data = context->pc; /* FIXME: may be too small by 2 */
data = emu68k_opfetch_16(context);
amode->data += (signed long)data;
}
struct m68k_amode_api m68k_amode_api_9 = {
m68k_amode_9_resolve,
CONTROL_AMODE_API
};
void m68k_amode_10_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
s16 data;
amode->context = context;
amode->data = context->pc;
data = emu68k_opfetch_16(context);
amode->data += (s8)data;
if (data & 0x0800) {
if (data & 0x8000) {
/* A.L */
amode->data += amode->context->regs_a[(data >> 12) & 7];
} else {
/* D.L */
amode->data += amode->context->regs_d[(data >> 12) & 7];
}
} else {
if (data & 0x8000) {
/* A.W */
amode->data += (s16)amode->context->regs_a[(data >> 12) & 7];
} else {
/* D.W */
amode->data += (s16)amode->context->regs_d[(data >> 12) & 7];
}
}
context->cycles_left -= 2;
}
struct m68k_amode_api m68k_amode_api_10 = {
m68k_amode_10_resolve,
CONTROL_AMODE_API
};
void m68k_amode_11_resolve(struct m68k_amode *amode, struct emu68k_context *context, int size)
{
amode->context = context;
if ((size == AMS_BYTE) || (size == AMS_WORD)) {
amode->data = emu68k_opfetch_16(context);
} else if (size == AMS_LONG) {
amode->data = emu68k_opfetch_32(context);
} else {
deb_printf("m68k: BUG: amode 11 reports illegal size %d.\n", size);
amode->data = 0;
}
}
u8 m68k_amode_11_readbyte(struct m68k_amode *amode)
{
return amode->data & 0xff;
}
u16 m68k_amode_11_readword(struct m68k_amode *amode)
{
return amode->data;
}
u32 m68k_amode_11_readlong(struct m68k_amode *amode)
{
return amode->data;
}
void m68k_amode_11_writebyte(struct m68k_amode *amode, u8 data)
{
deb_printf("m68k: CPU BUG: Write to non-alterable.\n");
abort();
}
void m68k_amode_11_writeword(struct m68k_amode *amode, u16 data)
{
deb_printf("m68k: CPU BUG: Write to non-alterable.\n");
abort();
}
void m68k_amode_11_writelong(struct m68k_amode *amode, u32 data)
{
deb_printf("m68k: CPU BUG: Write to non-alterable.\n");
abort();
}
void m68k_amode_11_after(struct m68k_amode *amode)
{
}
struct m68k_amode_api m68k_amode_api_11 = {
m68k_amode_11_resolve,
m68k_amode_11_readbyte,
m68k_amode_11_readword,
m68k_amode_11_readlong,
m68k_amode_11_writebyte,
m68k_amode_11_writeword,
m68k_amode_11_writelong,
m68k_amode_11_after,
};
struct m68k_amode m68k_amodes[] = {
{0x09, 0, 0, 0, NULL, &m68k_amode_api_0}, /* Dn */
{0x08, 0, 0, 0, NULL, &m68k_amode_api_1}, /* An */
{0x0f, 0, 0, 0, NULL, &m68k_amode_api_2}, /* (An) */
{0x0b, 0, 0, 0, NULL, &m68k_amode_api_3}, /* (An)+ */
{0x0b, 0, 0, 0, NULL, &m68k_amode_api_4}, /* -(An) */
{0x0f, 0, 0, 0, NULL, &m68k_amode_api_5}, /* d(An) */
{0x0f, 0, 0, 0, NULL, &m68k_amode_api_6}, /* d(An,Ri) */
{0x0f, 0, 0, 0, NULL, &m68k_amode_api_7}, /* xxxx */
{0x0f, 0, 0, 0, NULL, &m68k_amode_api_8}, /* xxxxxxxx */
{0x07, 0, 0, 0, NULL, &m68k_amode_api_9}, /* d */
{0x07, 0, 0, 0, NULL, &m68k_amode_api_10}, /* d(Ri) */
{0x03, 0, 0, 0, NULL, &m68k_amode_api_11}, /* #xxxx */
{0x00, 0, 0, 0, NULL, NULL}, /* <invalid> */
};
struct m68k_amode m68k_get_src_amode(u16 opword)
{
struct m68k_amode retval;
if ((opword & 0x38) == 0x38) {
if ((opword & 7) > 4) {
retval = m68k_amodes[12];
} else {
retval = m68k_amodes[7 + (opword & 7)];
}
} else {
retval = m68k_amodes[(opword & 0x38) >> 3];
}
retval.reg = opword & 7;
return retval;
}
struct m68k_amode m68k_get_dest_amode(u16 opword)
{
struct m68k_amode retval;
if ((opword & 0x1c0) == 0x1c0) {
if ((opword & 0xe00) > 0x800) {
retval = m68k_amodes[12];
} else {
retval = m68k_amodes[7 + ((opword & 0xe00) >> 9)];
}
} else {
retval = m68k_amodes[(opword & 0x1c0) >> 6];
}
retval.reg = (opword & 0xe00) >> 9;
return retval;
}
/*
* Stack operations
*/
/* FIXME: Why do the word stack ops use long data? */
void m68k_push_word(struct emu68k_context *context, u32 data)
{
context->regs_a[7] -= 2;
emu68k_write_16(context, context->regs_a[7], data);
}
u32 m68k_pop_word(struct emu68k_context *context)
{
u16 retval;
retval = emu68k_read_16(context, context->regs_a[7]);
context->regs_a[7] += 2;
return retval;
}
void m68k_push_long(struct emu68k_context *context, u32 data)
{
context->regs_a[7] -= 4;
emu68k_write_32(context, context->regs_a[7], data);
}
u32 m68k_pop_long(struct emu68k_context *context)
{
u32 retval;
retval = emu68k_read_32(context, context->regs_a[7]);
context->regs_a[7] += 4;
return retval;
}
/*
* Opcode handlers
*/
#define BEGIN_OPHANDLER(instr) \
void m68k_do_##instr(struct emu68k_context *context, u16 opword)
void m68k_do_unimp(struct emu68k_context *context, u16 opword)
{
deb_printf("emu68k: unimplemented opcode 0x%04hx at 0x%06x.\n", opword, context->pc);
system_flags |= F_UNIMPL;
/* FIXME: signal invalid opcode */
}
void m68k_do_alinetrap(struct emu68k_context *context, u16 opword)
{
deb_printf("emu68k: A-Line trap 0x%04hx at 0x%06x.\n", opword, context->pc);
/* FIXME: signal a-line trap */
}
void m68k_do_flinetrap(struct emu68k_context *context, u16 opword)
{
deb_printf("emu68k: F-Line trap 0x%04hx at 0x%06x.\n", opword, context->pc);
/* FIXME: signal f-line trap */
}
#define AMODE_CHECK_DA(side) \
side = m68k_get_##side##_amode(opword); \
if (!AMODE_IS(side, AMT_DA)) { m68k_do_unimp(context, opword); return; }
#define AMODE_CHECK_MA(side) \
side = m68k_get_##side##_amode(opword); \
if (!AMODE_IS(side, AMT_MA)) { m68k_do_unimp(context, opword); return; }
#define AMODE_CHECK_DATA(side) \
side = m68k_get_##side##_amode(opword); \
if (!AMODE_IS(side, AMT_DA)) { m68k_do_unimp(context, opword); return; }
#define AMODE_CHECK_ANY(side) \
side = m68k_get_##side##_amode(opword); \
if (!side.type) { m68k_do_unimp(context, opword); return; }
#define ALU_FLAG_VC_TEST_NONE(size)
#define ALU_FLAG_VC_TEST_CMP(size) \
FLAG_VC_SUB_##size(context, data3, data2, data);
#define ALU_FLAG_VC_TEST_SUB(size) \
FLAG_VC_SUB_##size(context, data3, data2, data); FLAG_X(context);
#define ALU_FLAG_VC_TEST_ADD(size) \
FLAG_VC_ADD_##size(context, data3, data2, data); FLAG_X(context);
#define ALU_IMMED_FUNCTION(instr, size1, size2, size3, size4, op, vc_test, cycle_penalty, amode) \
BEGIN_OPHANDLER(instr##_##size1) { \
size2 data; size2 data2; size2 data3; struct m68k_amode src; \
AMODE_CHECK_##amode(src); \
data2 = emu68k_opfetch_##size4(context); \
src.api->resolve(&src, context, AMS_##size3); \
data = src.api->read##size1(&src); \
src.api->after(&src); \
data3 = data op data2; \
ALU_IMMED_WRITE_##amode(size1); \
FLAG_NZ_##size1(context, data3); \