-
Notifications
You must be signed in to change notification settings - Fork 2
/
em.c
1477 lines (1331 loc) · 27.1 KB
/
em.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
#include "M68000.h"
#include "readcpu.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <unistd.h>
#include "expr.h"
#include "command.h"
#include "em.h"
#include "fm.h"
#include <sys/timeb.h>
#include <sys/types.h>
#include <signal.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <poll.h>
//#define PERFRAME 8896
#define PERFRAME (PERLINE*268)
#define PERLINE 64
#define HBLANK 8
#define NUMLINES 224
#define VINT (VBLANK + PERLINE) // (PERFRAME-400)
#define VBLANK (PERLINE*NUMLINES)
#define ENDMARK 0x01
#define Z80TICK 0x02
#define SCANLINE 0x04
#define HI_F 0x08
#define DISPLAY 0x10
#define VI_F 0x20
#define VB_F 0x800
#define HB_F 0x400
#define SAMPLESPERTICK (SAMPLERATE/60)
#define OFTEN 64
#define Z80CYCLES (4000000/60/(PERFRAME/OFTEN))
#define INTRESOLUTION 64
int soundhandle=-1;
short *soundbuffer;
int soundput, soundtake;
int on1=1,on2=1,on3=1, on4=1, onz=1;
int framecount=0;
int fps=0;
unsigned char fmreg1,fmreg2;
//#define CURRENTLINE ((intat-inttab)/PERLINE)
unsigned short *inttab,*intend,*intat;
int currentline;
int hintcount;
int next_hint;
int hint_limit;
int dcount=0;
int32_t starttime;
int get_hvpos(void)
{
int v;
v = (currentline-1)<<8;
if(currentline<NUMLINES)
v += 40+215*((intat-inttab)%PERLINE)/PERLINE;
return v & 0xffff;
}
void makeinttab(void)
{
int i,j;
unsigned short *p;
intat=inttab;
intend=inttab+PERFRAME;
for(i=0;i<PERFRAME;i++)
{
inttab[i]=(i<VBLANK ? 0 : VB_F) |
(!(i%OFTEN) ? Z80TICK : 0);
j=i%PERLINE;
if(!j)
{
j=i/PERLINE;
inttab[i] |= SCANLINE;
if(j==NUMLINES)
inttab[i] |= DISPLAY;
}
}
inttab[VINT]|=VI_F;
for(i=0;i<NUMLINES;i++)
{
p=inttab+i*PERLINE;
for(j=0;j<HBLANK;j++)
*p++ |= HB_F;
}
inttab[PERFRAME-1]|=ENDMARK;
}
void updatehint(int newfreq)
{
next_hint = newfreq;
}
void doview(void);
char trapcr;
char blanks;
char writeprotect;
int32_t *cpuhistory,*cpuput,*cpuend,*cputake;
int cpuinhistory;
int movem_index1[256];
int movem_index2[256];
int movem_next[256];
UBYTE *actadr;
int keymode=0;
int32_t lastpc=-1;
#define LINEBUFFLEN 128
char linebuff[LINEBUFFLEN],*linepntr;
regstruct regs, lastint_regs;
union flagu intel_flag_lookup[256];
union flagu regflags;
extern int cpu_interrupt(void);
extern void BuildCPU(void);
#define MC68000_interrupt() (cpu_interrupt())
#define cpu_readop(v) cpu_readmem24_word(v)
#define ReadMEM(A) (cpu_readmem24(A))
#define WriteMEM(A,V) (cpu_writemem24(A,V))
int MC68000_ICount;
uchar pending_interrupts;
static int InitStatus=0;
char exitflag;
unsigned char trace;
extern FILE * errorlog;
ushort *mainmemory;
int32_t mainlen;
ushort *ffpage;
unsigned char *a0page;
ushort gfxmap[256];
unsigned short gfxmask;
unsigned char gfxregs[32];
unsigned short gfxloc;
ushort *gfxmem,*gfxvscroll;
unsigned short gfxpoint;
unsigned char fillprime;
uchar c0mode;
uchar xmode;
ushort *xdest;
ushort xoffset;
ushort xmask;
void dofill(unsigned short v1)
{
uint32_t numwords;
int step;
if(!xdest) return;
numwords=(gfxregs[0x14]<<8)|gfxregs[0x13];
++numwords;
step=gfxregs[15];
if(step==1)
{
int v2;
while(numwords--)
{
v2=(xoffset>>1)&xmask;
v1&=255;
if(xoffset++&1)
xdest[v2]=(xdest[v2]&0xff00) | v1;
else
xdest[v2]=(v1<<8) | (xdest[v2]&0x00ff);
}
} else
{
while(numwords--)
{
xdest[(xoffset>>1)&xmask]=v1;
xoffset+=step;
}
}
}
void dumpregs(void)
{
int i,j;
printf("register dump\n");
for(j=0;j<24;j+=12)
{
for(i=0;i<12;i++)
printf("%04x ",gfxregs[j+i]+0x8000+((i+j)<<8));
printf("\n");
}
}
void doblit(ushort v1)
{
uint32_t source;
unsigned short numwords,source2;
int step;
if(~gfxregs[0x01]&0x10) {
static int wtfc = 0;
int max = 5;
if(wtfc<max) printf("doblit with dma (m1) 0, pc=%x, %d/%d\n", regs.pc, ++wtfc, max);
return;
}
source=((gfxregs[0x17]<<16)|(gfxregs[0x16]<<8)|gfxregs[0x15])<<1;
source&=0xffffff;
numwords=(gfxregs[0x14]<<8)|gfxregs[0x13];
if(xmode<8)
{
if(!xmode && (v1&0x40) && (gfxregs[0x17]&0x80))
{
/* this is only used by herzog zwei */
uchar t1;
ushort t2;
step=gfxregs[15];
source>>=1;
while(numwords--)
{
t1=(source&1) ? gfxmem[((ushort)source)>>1] : (gfxmem[((ushort)source)>>1]>>8);
t2=((ushort)xoffset)>>1;
if(xoffset&1)
gfxmem[t2]=(gfxmem[t2]&0xff00) | t1;
else
gfxmem[t2]=(t1<<8) | (gfxmem[t2]&0x00ff);
xoffset+=step;
source+=step;
}
return;
}
printf("blit with xmode<8, xmode=%d\n",xmode);
dumpregs();
enterdebug();
return;
}
if(gfxregs[0x17]&0x80) {fillprime=1;return;}
if(!xdest) return;
step=gfxregs[15];
if(source>=0xff0000)
{
source>>=1;
source2=source;
while(numwords--)
{
xdest[(xoffset>>1)&xmask]=ffpage[source2++ & 0x7fff];
xoffset+=step;
}
} else if(source<mainlen)
{
if(source+numwords+numwords>mainlen)
numwords=(mainlen-source)>>1;
source>>=1;
while(numwords--)
{
xdest[(xoffset>>1)&xmask]=mainmemory[source++];
xoffset+=step;
}
}
}
void newxmode(int v1)
{
xmode=v1&=15;
switch(v1)
{
case 8:
case 0:
xdest=gfxmem;
xmask=0x7fff;
break;
case 9:
case 1:
xdest=gfxvscroll;
xmask=0x7f;
break;
case 12:
case 2:
xdest=gfxmap;
xmask=0x3f;
break;
default:
xdest=0;
xmask=0;
break;
}
}
void deb(char *arg1,...)
{
char temp[64],*p;
static int online=0;
int len;
va_list ap;
va_start(ap, arg1);
vsprintf(temp,arg1,ap);
va_end(ap);
len=strlen(temp);
online+=len;
if(online>=80)
{
online=len;
putchar('\n');
}
printf("%s", temp);
p=temp+len;
len=0;
while(p-->temp && *p!='\n') ++len;
if(p>=temp) online=len;
}
void writec0(short v1,unsigned short v2)
{
int t;
v1>>=2;
//deb("%d:%04x ",v1,v2);
switch(c0mode | v1)
{
case 2: /* mode 1, write to c00000 or c00002 */
c0mode=0;
case 0: /* mode 0, write to c00000 or c00002 */
if(xmode<8)
newxmode(15);
if(fillprime) {dofill(v2);fillprime=0;break;}
if(xdest)
{
xdest[(xoffset>>1)&xmask]=v2;
xoffset+=gfxregs[15];
}
break;
case 1: /* mode 0, write to c00004 or c00006 */
switch(v2>>14)
{
case 0:
c0mode=2;
xoffset=(xoffset&0xc000) | v2;
newxmode(xmode&3);
break;
case 1:
c0mode=2;
xoffset=(xoffset&0xc000) | (v2&0x3fff);
newxmode((xmode&3)|8);
break;
case 2:
t=(v2>>8)&0x1f;
gfxregs[t]=v2;
// if(t==10 || !t) updatehint((v2&255)+1);
if(t==10) updatehint((v2&255)+1);
break;
case 3:
c0mode=2;
xoffset=(xoffset&0xc0000) | (v2&0x3fff);
newxmode((xmode&3)|12);
break;
}
break;
case 3: /* mode 1, write to c00004 or c00006 */
c0mode=0;
xoffset=(xoffset&0x3fff) | ((v2&3)<<14);
newxmode((xmode&12) | ((v2&0x30)>>4));
if(v2&0x80) doblit(v2);
}
}
unsigned short readc0(void)
{
ushort val;
if(c0mode || xmode>=8)
{
printf("Lockup!\n");
c0mode=0;
return 0;
}
c0mode=0;
if(xdest)
{
val=xdest[(xoffset>>1)&xmask];
xoffset+=gfxregs[15];
return val;
}
printf("Invalid read xmode=%04x\n",xmode);
return 0;
}
int gfxinit(void)
{
int i;
gfxmem=malloc(0x10000);
if(!gfxmem) return 1;
gfxmask=0;
gfxloc=0;
for(i=0;i<32;i++) gfxregs[i]=0;
return 0;
}
short a11100,a11200;
int32_t z80page;
unsigned Z80_RDMEM(dword a)
{
if(a<0x2000)
return a0page[a];
else if(a>=0x8000)
return cpu_readmem24(z80page | (a&0x7fff));
else if(a>=0x4000 && a<=0x4003)
{
return YM2612Read(0,a-0x4000);
}
return 0;
}
void Z80_WRMEM(dword a,byte V)
{
if(a<0x2000)
a0page[a]=V;
else if(a>=0x8000)
cpu_writemem24(z80page | (a&0x7fff),V);
else if(a==0x6000)
z80page=((z80page>>1) & 0x7f8000) | (V&1 ? 0x800000 : 0);
else if(a>=0x4000 && a<=0x4003)
{
YM2612Write(0,a-0x4000,V);
}
}
byte Z80_In(byte Port)
{
printf("z80 in %x\n", Port);
return 0;
}
void Z80_Out(byte Port,byte Value)
{
printf("z80 out %x, %x\n", Port, Value);
}
void Z80_Reti(void)
{
}
void Z80_Retn(void)
{
}
void Z80_Patch(Z80_Regs *Regs)
{
}
int Z80_Interrupt(void)
{
return 0;
}
int jspad[]={MYUP,MYDOWN,MYLEFT,MYRIGHT,'x','c','z','v'};
int jsor[]={1,2,4,8,1<<12,1<<4,1<<5,1<<13};
int jspad2[]={'1', '2', '3', '4', '5', '6', '7', '8'};
int getporta(void)
{
int i;
int v=~0;
for(i=0;i<8;++i)
if(checkpressed(jspad[i]))
v&= ~jsor[i];
return v;
}
int getportb(void)
{
int i;
int v=~0;
for(i=0;i<8;++i)
if(checkpressed(jspad2[i]))
v&= ~jsor[i];
return v;
}
int aoo3_six,aoo3_toggle;
int aoo5_six,aoo5_toggle;
void writea10003(unsigned char d)
{
if (aoo3_six>=0 && (d&0x40)==0 && aoo3_toggle ) aoo3_six++;
if (aoo3_six>0xc00000) aoo3_six&=~0x400000;
// keep it circling around a high value
if (d&0x40) aoo3_toggle=1; else aoo3_toggle=0;
}
void writea10005(unsigned char d)
{
if (aoo5_six>=0 && (d&0x40)==0 && aoo5_toggle ) aoo5_six++;
if (aoo5_six>0xc00000) aoo5_six&=~0x400000;
// keep it circling around a high value
if (d&0x40) aoo5_toggle=1; else aoo5_toggle=0;
}
unsigned char reada10003(void)
{
int porta=getporta();
if (aoo3_six==3)
{
// Extended pad info
if (aoo3_toggle==0) return ((porta>>8)&0x30)+0x00;
else return ((porta)&0x30)+0x40+((porta>>16)&0xf);
} else
{
if (aoo3_toggle==0)
{
if (aoo3_six==4) return ((porta>>8)&0x30)+0x00+0x0f;
else return ((porta>>8)&0x30)+0x00+(porta&3);
} else return ((porta)&0x30)+0x40+(porta&15);
}
}
unsigned char reada10005(void)
{
int portb=getportb();
if (aoo5_six==3)
{
// Extended pad info
if (aoo5_toggle==0x00) return ((portb>>8)&0x30)+0x00;
else return ((portb)&0x30)+0x40+((portb>>16)&0xf);
} else
{
if (aoo5_toggle==0x00)
{
if (aoo5_six==4) return ((portb>>8)&0x30)+0x00+0x0f;
else return ((portb>>8)&0x30)+0x00+(portb&3);
} else return ((portb)&0x30)+0x40+(portb&15);
}
}
void trysomez80(void)
{
if((a11200&0x100) && !(a11100&0x100)) if(onz) doz80(20);
}
void cpu_writemem24(uint32_t v1,unsigned char v2)
{
v1&=0xffffff;
if(v1>=0xff0000)
{
ushort v3;
v3=((ushort)v1)>>1;
if(v1&1)
ffpage[v3]=(ffpage[v3]&0xff00) | v2;
else
ffpage[v3]=(v2<<8) | (ffpage[v3]&0x00ff);
}
else if(v1==0xa11100)
{
a11100=v2<<8;
trysomez80();
}
else if(v1==0xa11200)
{
a11200=v2<<8;
if(!(a11200&0x100)) Z80_Reset();
else trysomez80();
}
else if(v1==0xa10003) writea10003(v2);
else if(v1==0xa10005) writea10005(v2);
else if(v1>=0xa00000 && v1<0xa10000)
{
v1&=0xffff;
// if(a11100&a11200&0x100)
{
if(v1<0x2000) a0page[v1]=v2;
else if(v1>=0x4000 && v1<=0x4003) YM2612Write(0,v1-0x4000,v2);
}
} else if(v1>=0xc00000 && v1<0xc00008)
{
writec0(v1&7,v2);
// printf("%06x <- %02x\n",v1,v2);
} else if(v1<mainlen)
{
if(!writeprotect)
{
int32_t v3;
v3=v1>>1;
if(v1&1)
mainmemory[v3]=(mainmemory[v3]&0xff00) | v2;
else
mainmemory[v3]=(v2<<8) | (mainmemory[v3]&0x00ff);
}
} else if(v1==0xc00011)
{
// printf("Write %02x to PSG!\n", v2);
}
}
void cpu_writemem24_word(uint32_t v1,unsigned short v2)
{
v1&=0xffffff;
if(v1>=0xff0000)
{
ffpage[(v1&0xffff)>>1]=v2;
}
else if((v1&0xfffff8)==0xc00000)
writec0(v1&7,v2);
else if(v1==0xa11100)
{
a11100=v2;
trysomez80();
}
else if(v1==0xa11200)
{
a11200=v2;
if(!(v2&0x100)) Z80_Reset();
else trysomez80();
}
else if(v1<mainlen)
{
if(!writeprotect)
mainmemory[v1>>1]=v2;
} else if(v1>=0xa00000 && v1<0xa10000) printf("16 bit access to A0 page %x\n",regs.pc);
}
void cpu_writemem24_dword(uint32_t v1,uint32_t v2)
{
cpu_writemem24_word(v1,(unsigned short)(v2>>16));
cpu_writemem24_word(v1+2,(unsigned short) v2);
}
unsigned char cpu_readmem24(uint32_t v1)
{
v1&=0xffffff;
if(v1<mainlen)
{
if(v1&1)
return mainmemory[v1>>1];
else
return mainmemory[v1>>1]>>8;
}
else if(v1>=0xff0000)
{
if(v1&1)
return ffpage[((ushort)v1)>>1];
else
return ffpage[((ushort)v1)>>1]>>8;
}
else if(v1==0xa10003) return reada10003();
else if(v1==0xa10005) return reada10005();
else if(v1>=0xa10004 && v1<0xa10020) return 0;
else if(v1==0xa11100)
return (~a11100|~a11200)>>8;
else if(v1>=0xa00000 && v1<0xa10000)
{
v1&=0xffff;
if(a11100&a11200&0x100)
{
if(v1<0x2000) return a0page[v1];
if(v1>=0x4000 && v1<=0x4003) return YM2612Read(0,v1-0x4000);
}
} else if(v1==0xc00005 || v1==0xc00007)
return 0x80 | (*intat>>8);
else if(v1==0xa10001) return 0xa0;
else if(v1==0xc00008) return get_hvpos()>>8;
else if(v1==0xc00009) return get_hvpos();
return (v1&1) ? 0x00 : 0x61;
}
unsigned short cpu_readmem24_word(uint32_t v1)
{
v1&=0xffffff;
if(v1<mainlen)
return mainmemory[v1>>1];
else if(v1>=0xff0000)
return ffpage[((ushort)v1)>>1];
else if(v1==0xa11100)
return ~a11100|~a11200;
else if(v1>=0xa10004 && v1<0xa10020) return 0;
else if(v1==0xc00004 || v1==0xc00006)
return 0x6280 | (*intat>>8);
else if(v1==0xc00000 || v1==0xc00002)
return readc0();
else if(v1==0xc00008) return get_hvpos();
else return 0x6100;
}
uint32_t cpu_readmem24_dword(uint32_t v1)
{
return (cpu_readmem24_word(v1)<<16L) | cpu_readmem24_word(v1+2);
}
void m68k_dumpstate()
{
int i;
CPTR nextpc;
for(i = 0; i < 8; i++){
printf("D%d: %08x ", i, regs.d[i]);
if ((i & 3) == 3) printf("\n");
}
for(i = 0; i < 8; i++){
printf("A%d: %08x ", i, regs.a[i]);
if ((i & 3) == 3) printf("\n");
}
if (regs.s == 0) regs.usp = regs.a[7];
if (regs.s && regs.m) regs.msp = regs.a[7];
if (regs.s && regs.m == 0) regs.isp = regs.a[7];
printf("USP=%08x ISP=%08x MSP=%08x VBR=%08x SR=%08x\n",
regs.usp,regs.isp,regs.msp,regs.vbr,regs.sr);
printf ("T=%d%d S=%d M=%d N=%d Z=%d V=%d C=%d IMASK=%d\n",
regs.t1, regs.t0, regs.s, regs.m,
NFLG, ZFLG, VFLG, CFLG, regs.intmask);
for(i = 0; i < 8; i++){
printf("FP%d: %g ", i, regs.fp[i]);
if ((i & 3) == 3) printf("\n");
}
printf("N=%d Z=%d I=%d NAN=%d\n",
(regs.fpsr & 0x8000000) != 0,
(regs.fpsr & 0x4000000) != 0,
(regs.fpsr & 0x2000000) != 0,
(regs.fpsr & 0x1000000) != 0);
MC68000_disasm(m68k_getpc(), &nextpc, 1);
printf("Next PC = 0x%0x\n", nextpc);
}
int32_t gtime()
{
struct timeb tb;
ftime(&tb);
return tb.time*1000 + tb.millitm;
}
static void initCPU(void)
{
int i,j;
for (i = 0 ; i < 256 ; i++) {
for (j = 0 ; j < 8 ; j++) {
if (i & (1 << j)) break;
}
movem_index1[i] = j;
movem_index2[i] = 7-j;
movem_next[i] = i & (~(1 << j));
}
for (i = 0; i < 256; i++) {
intel_flag_lookup[i].flags.c = !!(i & 1);
intel_flag_lookup[i].flags.z = !!(i & 64);
intel_flag_lookup[i].flags.n = !!(i & 128);
intel_flag_lookup[i].flags.v = 0;
}
}
void Exception(int nr, CPTR oldpc)
{
MakeSR();
if(!regs.s) {
regs.a[7]=regs.isp;
regs.s=1;
}
regs.a[7] -= 4;
put_long (regs.a[7], m68k_getpc ());
regs.a[7] -= 2;
put_word (regs.a[7], regs.sr);
m68k_setpc(get_long(regs.vbr + 4*nr));
regs.t1 = regs.t0 = regs.m = 0;
}
void Exception2(int nr, int level)
{
MakeSR();
if(!regs.s) {
regs.a[7]=regs.isp;
regs.s=1;
}
regs.a[7] -= 4;
put_long (regs.a[7], m68k_getpc ());
regs.a[7] -= 2;
put_word (regs.a[7], regs.sr);
m68k_setpc(get_long(regs.vbr + 4*nr));
regs.intmask=level;
regs.t1 = regs.t0 = regs.m = 0;
}
void Interrupt68k(int level)
{
if(level>=regs.intmask)
{
Exception(24+level,0);
pending_interrupts &= ~(1 << (level-1)); /* ASG 971105 */
}
}
void Initialisation() {
/* Init 68000 emulator */
BuildCPU();
initCPU();
}
void MC68000_Reset(void) /* ASG 971105 */
{
if (!InitStatus)
{
Initialisation();
InitStatus=1;
currentline = 0;
// hintcount = 0;
}
/* ASG 971105 MC68000_IPeriod = IPeriod;
icount = IPeriod;*/
regs.a[7]=get_long(0);
m68k_setpc(get_long(4));
regs.s = 1;
regs.m = 0;
regs.stopped = 0;
regs.t1 = 0;
regs.t0 = 0;
ZFLG = CFLG = NFLG = VFLG = 0;
regs.intmask = 7;
regs.vbr = regs.sfc = regs.dfc = 0;
regs.fpcr = regs.fpsr = regs.fpiar = 0;
pending_interrupts = 0; /* ASG 971105 */
}
void MC68000_SetRegs(MC68000_Regs *src)
{
regs = src->regs;
NFLG = (regs.sr >> 3) & 1;
ZFLG = (regs.sr >> 2) & 1;
VFLG = (regs.sr >> 1) & 1;
CFLG = regs.sr & 1;
pending_interrupts = src->pending_interrupts;
}
void MC68000_GetRegs(MC68000_Regs *dst)
{
regs.sr = (regs.sr & 0xfff0) | (NFLG << 3) | (ZFLG << 2) | (VFLG << 1) |
CFLG;
dst->regs = regs;
dst->pending_interrupts = pending_interrupts;
}
/* ASG 971105 */
void MC68000_Cause_Interrupt(int level)
{
if (level >= 1 && level <= 7)
pending_interrupts |= (1 << (level-1));
}
/* ASG 971105 */
void MC68000_Clear_Pending_Interrupts(void)
{
pending_interrupts = 0;
}
/* ASG 971105 */
int MC68000_GetPC(void)
{
return regs.pc;
}
void sound_process(int t)
{
void *tbuf[2];
int s1,s2;
FMSAMPLE buff0[4096], buff1[4096];
int i;
int n;
s1=SAMPLESPERTICK*t/PERFRAME;
s2=SAMPLESPERTICK*(t+OFTEN)/PERFRAME;
tbuf[0] = buff0;
tbuf[1] = buff1;
t=s2-s1;
if(t>0)
{
YM2612UpdateOne(0, (void **)tbuf, t);
for(i=0;i<t;++i)
{
soundbuffer[soundput]=buff0[i];
soundbuffer[soundput+1]=buff1[i];
n=soundput + 2;
soundput = (n==SOUNDBUFFERSIZE) ? 0 : n;
}
}
}
int soundinit(void)
{
// int frag,value;
soundbuffer=malloc(SOUNDBUFFERSIZE*sizeof(soundbuffer[0]));
if(!soundbuffer)
{
printf("Could not allocate soundbuffer\n");
return 10;
}
soundput = soundtake = 0;
return 0;
}
/* Execute one 68000 instruction */
/* ASG 971105 */
int MC68000_Execute(int cycles)
{
MC68000_ICount = cycles;
do
{
MC68000_ICount -= 15;
if(keymode) break;
stepone(1);
}
while (MC68000_ICount > 0);
return (cycles - MC68000_ICount);
}
unsigned char inttable[128] =
{
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
};
int ic=0;
void stepone(int num)
{
UWORD opcode;
unsigned char t;
//printf("%x\n",regs.pc);
int oldmode = keymode;
while(keymode==oldmode && num--)
{
if((t=*intat++))
{
if(t&Z80TICK)
{
t&=~Z80TICK;
if((a11200&0x100) && !(a11100&0x100)) if(onz) doz80(Z80CYCLES);
sound_process(intat-inttab-1);
}
if(t&ENDMARK)
{
t&=~ENDMARK;
intat=inttab;
// Z80_int();
hintcount = 0;
hint_limit = next_hint;
currentline = 0;
}
if(t&VI_F)
Z80_int();
if(t&SCANLINE)
{
t&=~SCANLINE;
if(currentline<224)
{
makeline(currentline++);
if(++hintcount >= hint_limit)
{
if(*gfxregs&0x10)
{
hintcount = 0;