-
Notifications
You must be signed in to change notification settings - Fork 0
/
Madam.cpp
2703 lines (2172 loc) · 61.9 KB
/
Madam.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
/*
3DOplay v1.8.2 is 3do emulator based on FreeDOcore
3doplay.do.am
Developer: Viktor Ivanov
Any uses of the 3DOplay sources or any other material published by Viktor Ivanov have to be accompanied with full credits.
All rights reserved.
*/
/*
www.freedo.org
The first and only working 3DO multiplayer emulator.
The FreeDO licensed under modified GNU LGPL, with following notes:
* The owners and original authors of the FreeDO have full right to develop closed source derivative work.
* Any non-commercial uses of the FreeDO sources or any knowledge obtained by studying or reverse engineering
of the sources, or any other material published by FreeDO have to be accompanied with full credits.
* Any commercial uses of FreeDO sources or any knowledge obtained by studying or reverse engineering of the sources,
or any other material published by FreeDO is strictly forbidden without owners approval.
The above notes are taking precedence over GNU LGPL in conflicting situations.
Project authors:
Alexander Troosh
Maxim Grishin
Allen Wright
John Sammons
Felix Lazarev
*/
#include "stdafx.h"
#include "Madam.h"
#include "Clio.h"
#include "vdlp.h"
#include "arm.h"
#include <math.h>
#include <memory.h>
#include "bitop.h"
BitReaderBig bitoper;
#include "3doplay.h"
extern _ext_Interface io_interface;
/* === CCB control word flags === */
#define CCB_SKIP 0x80000000
#define CCB_LAST 0x40000000
#define CCB_NPABS 0x20000000
#define CCB_SPABS 0x10000000
#define CCB_PPABS 0x08000000
#define CCB_LDSIZE 0x04000000
#define CCB_LDPRS 0x02000000
#define CCB_LDPPMP 0x01000000
#define CCB_LDPLUT 0x00800000
#define CCB_CCBPRE 0x00400000
#define CCB_YOXY 0x00200000
#define CCB_ACSC 0x00100000
#define CCB_ALSC 0x00080000
#define CCB_ACW 0x00040000
#define CCB_ACCW 0x00020000
#define CCB_TWD 0x00010000
#define CCB_LCE 0x00008000
#define CCB_ACE 0x00004000
#define CCB_reserved13 0x00002000
#define CCB_MARIA 0x00001000
#define CCB_PXOR 0x00000800
#define CCB_USEAV 0x00000400
#define CCB_PACKED 0x00000200
#define CCB_POVER_MASK 0x00000180
#define CCB_PLUTPOS 0x00000040
#define CCB_BGND 0x00000020
#define CCB_NOBLK 0x00000010
#define CCB_PLUTA_MASK 0x0000000F
#define CCB_POVER_SHIFT 7
#define CCB_PLUTA_SHIFT 0
#define PMODE_PDC ((0x00000000)<<CCB_POVER_SHIFT) /* Normal */
#define PMODE_ZERO ((0x00000002)<<CCB_POVER_SHIFT)
#define PMODE_ONE ((0x00000003)<<CCB_POVER_SHIFT)
/* === Cel first preamble word flags === */
#define PRE0_LITERAL 0x80000000
#define PRE0_BGND 0x40000000
#define PREO_reservedA 0x30000000
#define PRE0_SKIPX_MASK 0x0F000000
#define PREO_reservedB 0x00FF0000
#define PRE0_VCNT_MASK 0x0000FFC0
#define PREO_reservedC 0x00000020
#define PRE0_LINEAR 0x00000010
#define PRE0_REP8 0x00000008
#define PRE0_BPP_MASK 0x00000007
#define PRE0_SKIPX_SHIFT 24
#define PRE0_VCNT_SHIFT 6
#define PRE0_BPP_SHIFT 0
/* PRE0_BPP_MASK definitions */
#define PRE0_BPP_1 0x00000001
#define PRE0_BPP_2 0x00000002
#define PRE0_BPP_4 0x00000003
#define PRE0_BPP_6 0x00000004
#define PRE0_BPP_8 0x00000005
#define PRE0_BPP_16 0x00000006
/* Subtract this value from the actual vertical source line count */
#define PRE0_VCNT_PREFETCH 1
/* === Cel second preamble word flags === */
#define PRE1_WOFFSET8_MASK 0xFF000000
#define PRE1_WOFFSET10_MASK 0x03FF0000
#define PRE1_NOSWAP 0x00004000
#define PRE1_TLLSB_MASK 0x00003000
#define PRE1_LRFORM 0x00000800
#define PRE1_TLHPCNT_MASK 0x000007FF
#define PRE1_WOFFSET8_SHIFT 24
#define PRE1_WOFFSET10_SHIFT 16
#define PRE1_TLLSB_SHIFT 12
#define PRE1_TLHPCNT_SHIFT 0
#define PRE1_TLLSB_0 0x00000000
#define PRE1_TLLSB_PDC0 0x00001000 /* Normal */
#define PRE1_TLLSB_PDC4 0x00002000
#define PRE1_TLLSB_PDC5 0x00003000
/* Subtract this value from the actual word offset */
#define PRE1_WOFFSET_PREFETCH 2
/* Subtract this value from the actual pixel count */
#define PRE1_TLHPCNT_PREFETCH 1
#define PPMP_0_SHIFT 0
#define PPMP_1_SHIFT 16
#define PPMPC_1S_MASK 0x00008000
#define PPMPC_MS_MASK 0x00006000
#define PPMPC_MF_MASK 0x00001C00
#define PPMPC_SF_MASK 0x00000300
#define PPMPC_2S_MASK 0x000000C0
#define PPMPC_AV_MASK 0x0000003E
#define PPMPC_2D_MASK 0x00000001
#define PPMPC_MS_SHIFT 13
#define PPMPC_MF_SHIFT 10
#define PPMPC_SF_SHIFT 8
#define PPMPC_2S_SHIFT 6
#define PPMPC_AV_SHIFT 1
/* PPMPC_1S_MASK definitions */
#define PPMPC_1S_PDC 0x00000000
#define PPMPC_1S_CFBD 0x00008000
/* PPMPC_MS_MASK definitions */
#define PPMPC_MS_CCB 0x00000000
#define PPMPC_MS_PIN 0x00002000
#define PPMPC_MS_PDC_MFONLY 0x00004000
#define PPMPC_MS_PDC 0x00004000
/* PPMPC_MF_MASK definitions */
#define PPMPC_MF_1 0x00000000
#define PPMPC_MF_2 0x00000400
#define PPMPC_MF_3 0x00000800
#define PPMPC_MF_4 0x00000C00
#define PPMPC_MF_5 0x00001000
#define PPMPC_MF_6 0x00001400
#define PPMPC_MF_7 0x00001800
#define PPMPC_MF_8 0x00001C00
/* PPMPC_SF_MASK definitions */
#define PPMPC_SF_2 0x00000100
#define PPMPC_SF_4 0x00000200
#define PPMPC_SF_8 0x00000300
#define PPMPC_SF_16 0x00000000
/* PPMPC_2S_MASK definitions */
#define PPMPC_2S_0 0x00000000
#define PPMPC_2S_CCB 0x00000040
#define PPMPC_2S_CFBD 0x00000080
#define PPMPC_2S_PDC 0x000000C0
/* PPMPC_2D_MASK definitions */
#define PPMPC_2D_1 0x00000000
#define PPMPC_2D_2 0x00000001
#pragma pack(push,1)
struct cp1btag{
unsigned short c:1;
unsigned short pad:15;
};
struct cp2btag{
unsigned short c:2;
unsigned short pad:14;
};
typedef struct cp4btag{
unsigned short c:4;
unsigned short pad:12;
} cp4b;
struct cp6btag{
unsigned short c:5;
unsigned short pw:1;
unsigned short pad:10;
};
struct cp8btag{
unsigned short c:5;
unsigned short mpw:1;
unsigned short m:2;
unsigned short pad:8;
};
struct cp16btag{
unsigned short c:5;
unsigned short mb:3;
unsigned short mg:3;
unsigned short mr:3;
unsigned short pad:1;
unsigned short pw:1;
};
struct up8btag{
unsigned short b:2;
unsigned short g:3;
unsigned short r:3;
unsigned short pad:8;
};
struct up16btag{
unsigned short bw:1;
unsigned short b:4;
unsigned short g:5;
unsigned short r:5;
unsigned short p:1;
};
struct res16btag{
unsigned short b:5;
unsigned short g:5;
unsigned short r:5;
unsigned short p:1;
};
union pdeco{
unsigned int raw;
cp1btag c1b;
cp2btag c2b;
cp4btag c4b;
cp6btag c6b;
cp8btag c8b;
cp16btag c16b;
up8btag u8b;
up16btag u16b;
res16btag r16b;
};
struct avtag
{
unsigned char NEG:1;
unsigned char XTEND:1;
unsigned char nCLIP:1;
unsigned char dv3:2;
unsigned char pad:3;
};
union AVS{
avtag avsignal;
unsigned int raw;
};
struct pixctag
{
unsigned char dv2:1;
unsigned char av:5; // why int don't work???
unsigned char s2:2;
unsigned char dv1:2;
unsigned char mxf:3;
unsigned char ms:2;
unsigned char s1:1;
};
union PXC
{
pixctag meaning;
unsigned int raw;
};
#pragma pack(pop)
//*******************************************
#pragma pack(push,1)
struct MADAMDatum
{
unsigned int mregs[2048+64];
unsigned short PLUT[32];
unsigned char PBUSQueue[20];
int RMOD;
int WMOD;
unsigned int _madam_FSM;
};
#pragma pack(pop)
static MADAMDatum madam;
unsigned int Get_madam_FSM(){return madam._madam_FSM;};
void Set_madam_FSM(unsigned int val){madam._madam_FSM=val;};
unsigned int _madam_SaveSize()
{
return sizeof(MADAMDatum);
}
void _madam_Save(void *buff)
{
memcpy(buff,&madam,sizeof(MADAMDatum));
}
void _madam_Load(void *buff)
{
memcpy(&madam,buff,sizeof(MADAMDatum));
}
#define mregs madam.mregs
#define PLUT madam.PLUT
#define PBUSQueue madam.PBUSQueue
#define RMOD madam.RMOD
#define WMOD madam.WMOD
#define _madam_FSM madam._madam_FSM
//*******************************************
unsigned int PXOR1, PXOR2;
#define PDV(x) ((((x)-1)&3)+1)
#define MIN(x,y) (x)+(((signed int)((y)-(x))>>31&((y)-(x))))
#define MAX(x,y) (y)-(((signed int)((y)-(x))>>31&((y)-(x))))
#define TESTCLIP(cx,cy) ( ((int)cx>=0)&&((int)cx<=((CLIPXVAL)<<16))&&((int)cy>=0)&&((int)cy<=((CLIPYVAL)<<16)))
#define FLT(a) ((float)(int)(a)/65536.0)
#define XY2OFF(a,b,c) ( (((int)b>>1)*c/*bitmap width*/) + (((int)(b)&1)<<1) + (a) )
#define PBMASK 0x80000000
#define KUP 0x08000000
#define KDN 0x10000000
#define KRI 0x04000000
#define KLE 0x02000000
#define KA 0x01000000
#define KB 0x00800000
#define KC 0x00400000
#define KP 0x00200000
#define KX 0x00100000
#define KRS 0x00080000
#define KLS 0x00040000
#define FIXP16_SHIFT 16
#define FIXP16_MAG 65536
#define FIXP16_DP_MASK 0x0000ffff
#define FIXP16_WP_MASK 0xffff0000
#define FIXP16_ROUND_UP 0x0000ffff //0x8000
// TYPES ///////////////////////////////////////////////////////////////////
// CLASSES /////////////////////////////////////////////////////////////////
unsigned int __fastcall mread(unsigned int addr);
void __fastcall mwrite(unsigned int addr, unsigned int val);
int TestInitVisual(int packed);
int Init_Line_Map();
void Init_Scale_Map();
void Init_Arbitrary_Map();
int __fastcall TexelDraw_Line(unsigned short CURPIX, unsigned short LAMV, int xcur, int ycur, int cnt);
int __fastcall TexelDraw_Scale(unsigned short CURPIX, unsigned short LAMV, int xcur, int ycur, int deltax, int deltay);
int __fastcall TexelDraw_Arbitrary(unsigned short CURPIX, unsigned short LAMV, int xA, int yA, int xB, int yB, int xC, int yC, int xD, int yD);
void __fastcall DrawPackedCel_New();
void __fastcall DrawLiteralCel_New();
void __fastcall DrawLRCel_New();
void HandleDMA8();
void DMAPBus();
unsigned int MAPPING;
// general 3D vertex class
#define INT1220(a) ((signed int)(a)>>20)
#define INT1220up(a) ((signed int)((a)+(1<<19))>>20)
static struct
{
// int mode;
unsigned int plutaCCBbits;
unsigned int pixelBitsMask;
unsigned int pmodeORmask;
unsigned int pmodeANDmask;
bool tmask;
} pdec;
unsigned int pbus=0;
unsigned char * Mem;
unsigned int retuval;
unsigned int BITADDR;
//static unsigned int * BITPTR;
//static unsigned int * BITEND;
unsigned int BITBUFLEN;
unsigned int BITBUF;
unsigned int CCBFLAGS,/*PLUTDATA*/PIXC,PRE0,PRE1,TARGETPROJ,SRCDATA,debug;
int SPRWI,SPRHI;
unsigned int PLUTF,PDATF,NCCBF;
int CELCYCLES,__smallcicles;
bool ADD;
//static SDL_Event cpuevent;
int BITCALC;
unsigned short bitbuf; //bit buffer
unsigned char subbitbuf;// bit sub buffer
int bitcount; // bit counter
long compsize; // size of commpressed!!! in bytes!!! actually pixcount*bpp/8!!!
unsigned int gFINISH;
unsigned short RRR;
int USECEL;
unsigned int const BPP[8]={1,1,2,4,6,8,16,1};
unsigned char PSCALAR[8][4][32];
unsigned short MAPu8b[256+64], MAPc8bAMV[256+64], MAPc16bAMV[8*8*8+64];
int currentrow;
unsigned int bpp;
int pixcount;
unsigned int type;
unsigned int offsetl;
unsigned int offset;
//static unsigned int begining;
unsigned int eor;
int calcx;
int nrows;
unsigned int pix;
unsigned short ttt;
unsigned int OFF;
unsigned int pSource;
bool Transparent;
//AString str;
//CelEngine STATBits
#define STATBITS mregs[0x28]
#define SPRON 0x10
#define SPRPAU 0x20
//CelEngine Registers
#define SPRSTRT 0x100
#define SPRSTOP 0x104
#define SPRCNTU 0x108
#define SPRPAUS 0x10c
#define CCBCTL0 mregs[0x110]
#define REGCTL0 mregs[0x130]
#define REGCTL1 mregs[0x134]
#define REGCTL2 mregs[0x138]
#define REGCTL3 mregs[0x13c]
#define CLIPXVAL ((int)mregs[0x134]&0x3ff)
#define CLIPYVAL ((int)(mregs[0x134]>>16)&0x3ff)
#define PIXSOURCE (mregs[0x138])
#define FBTARGET (mregs[0x13c])
#define CURRENTCCB mregs[0x5a0]
//next ccb == 0 stop the engine
#define NEXTCCB mregs[0x5a4]
#define PLUTDATA mregs[0x5a8]
#define PDATA mregs[0x5ac]
#define ENGAFETCH mregs[0x5b0]
#define ENGALEN mregs[0x5b4]
#define ENGBFETCH mregs[0x5b8]
#define ENGBLEN mregs[0x5bc]
#define PAL_EXP (&mregs[0x5d0])
#define CHAR_BIT (8)
int FLOAT1612(int a)
{
return a<<4;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//void MapCoord(poly *pol);
//void RenderPoly();
unsigned int __fastcall _madam_Peek(unsigned int addr)
{
// if((addr>=0x400)&&(addr<=0x53f))
// printf("#Madam Peek [%X]=%X\n",addr,mregs[addr]);
if((addr>=0x400)&&(addr<=0x53f))
{
//we need to return actual fifo status!!!!
return _clio_FIFOStruct(addr);
}
if(addr==0x28) // STATUS OF CEL
{
switch(_madam_FSM)
{
case FSM_IDLE:return 0x0;
case FSM_SUSPENDED:return 0x30;
case FSM_INPROCESS:return 0x10;
}
}
if(addr>=0x580 && addr<0x5A0)
{
//sprintf(str,"CLUT - MADAM Read madam[0x%X]\n",addr);
//CDebug::DPrint(str);
}
return mregs[addr];
}
void __fastcall _madam_Poke(unsigned int addr, unsigned int val)
{
if(addr>0x2ff && addr<0x400)
{
// io_interface(EXT_DEBUG_PRINT,(void*)str.print("MADAM Write madam[0x%X] = 0x%8.8X\n",addr,val).CStr());
}
/*
if(addr==0x13c)
{
sprintf(str,"Switch screen SWI 0x%8X addr 0x%8X\n",last_SWI,val);
CDebug::DPrint(str);
}*/
if((addr>=0x400)&&(addr<=0x53f))
{
_clio_SetFIFO(addr,val);
return;
}
else
switch(addr)
{
case 0x4:
val=0x29;
mregs[addr]=val;
break;
case 0x8:
mregs[addr]=val;
HandleDMA8();
break;
case 0x580:
_vdl_ProcessVDL(val);
return;
case 0x584:
case 0x588:
case 0x58C:
case 0x590:
case 0x594:
case 0x598:
case 0x59C:
//_3do_DPrint(str.print("CLUT - MADAM Write madam[0x%X] = 0x%8.8X\n",addr,val));
mregs[addr]=val;
return;
case 0x0:
// io_interface(EXT_KPRINT,(void*)val);
return;
case SPRSTRT:
if(_madam_FSM==FSM_IDLE)
_madam_FSM=FSM_INPROCESS;
return;
case SPRSTOP:
_madam_FSM=FSM_IDLE;
NEXTCCB=0;
return;
case SPRCNTU:
if(_madam_FSM==FSM_SUSPENDED)
_madam_FSM=FSM_INPROCESS;
return;
case SPRPAUS:
if(_madam_FSM==FSM_INPROCESS)
_madam_FSM=FSM_SUSPENDED;
return;
//Matrix engine macros
#define M00 ((double)(signed int)mregs[0x600])
#define M01 ((double)(signed int)mregs[0x604])
#define M02 ((double)(signed int)mregs[0x608])
#define M03 ((double)(signed int)mregs[0x60C])
#define M10 ((double)(signed int)mregs[0x610])
#define M11 ((double)(signed int)mregs[0x614])
#define M12 ((double)(signed int)mregs[0x618])
#define M13 ((double)(signed int)mregs[0x61C])
#define M20 ((double)(signed int)mregs[0x620])
#define M21 ((double)(signed int)mregs[0x624])
#define M22 ((double)(signed int)mregs[0x628])
#define M23 ((double)(signed int)mregs[0x62C])
#define M30 ((double)(signed int)mregs[0x630])
#define M31 ((double)(signed int)mregs[0x634])
#define M32 ((double)(signed int)mregs[0x638])
#define M33 ((double)(signed int)mregs[0x63C])
#define V0 ((double)(signed int)mregs[0x640])
#define V1 ((double)(signed int)mregs[0x644])
#define V2 ((double)(signed int)mregs[0x648])
#define V3 ((double)(signed int)mregs[0x64C])
#define Rez0 mregs[0x660]
#define Rez1 mregs[0x664]
#define Rez2 mregs[0x668]
#define Rez3 mregs[0x66C]
//#define Nfrac16 ((__int64)mregs[0x680]<<32|(unsigned int)mregs[0x684])
#define Nfrac16 (((__int64)mregs[0x680]<<32)|(unsigned int)mregs[0x684])
// Matix engine
case 0x7fc:
mregs[0x7fc]=0; // Ours matrix engine already ready
static double Rez0T,Rez1T,Rez2T,Rez3T;
// io_interface(EXT_DEBUG_PRINT,(void*)str.print("MADAM Write madam[0x%X] = 0x%8.8X\n",addr,val).CStr());
switch(val) // Cmd
{
case 0: //printf("#Matrix = NOP\n");
Rez0=Rez0T;
Rez1=Rez1T;
Rez2=Rez2T;
Rez3=Rez3T;
return; // NOP
case 1: //multiply a 4x4 matrix of 16.16 values by a vector of 16.16 values
Rez0=Rez0T;
Rez1=Rez1T;
Rez2=Rez2T;
Rez3=Rez3T;
Rez0T=(int)((M00*V0+M01*V1+M02*V2+M03*V3)/65536.0);
Rez1T=(int)((M10*V0+M11*V1+M12*V2+M13*V3)/65536.0);
Rez2T=(int)((M20*V0+M21*V1+M22*V2+M23*V3)/65536.0);
Rez3T=(int)((M30*V0+M31*V1+M32*V2+M33*V3)/65536.0);
return;
case 2: //multiply a 3x3 matrix of 16.16 values by a vector of 16.16 values
Rez0=Rez0T;
Rez1=Rez1T;
Rez2=Rez2T;
Rez3=Rez3T;
Rez0T=(int)((M00*V0+M01*V1+M02*V2)/65536.0);
Rez1T=(int)((M10*V0+M11*V1+M12*V2)/65536.0);
Rez2T=(int)((M20*V0+M21*V1+M22*V2)/65536.0);
//printf("#Matrix CMD2, R0=0x%8.8X, R1=0x%8.8X, R2=0x%8.8X\n",Rez0,Rez1,Rez2);
return;
case 3: // Multiply a 3x3 matrix of 16.16 values by multiple vectors, then multiply x and y by n/z
{ // Return the result vectors {x*n/z, y*n/z, z}
Rez0=Rez0T;
Rez1=Rez1T;
Rez2=Rez2T;
Rez3=Rez3T;
double M;
Rez2T=(signed int)((M20*V0+M21*V1+M22*V2)/65536.0); // z
if(Rez2T!=0)
M=Nfrac16/(double)Rez2T; // n/z
else
{
M=Nfrac16;
// io_interface(EXT_DEBUG_PRINT,(void*)"!!!Division by zero!!!\n");
}
Rez0T=(signed int)((M00*V0+M01*V1+M02*V2)/65536.0);
Rez1T=(signed int)((M10*V0+M11*V1+M12*V2)/65536.0);
Rez0T=(double)((Rez0T*M)/65536.0/65536.0); // x * n/z
Rez1T=(double)((Rez1T*M)/65536.0/65536.0); // y * n/z
}
return;
default:
//io_interface(EXT_DEBUG_PRINT,(void*)str.print("??? Unknown cmd MADAM[0x7FC]==0x%x\n", val).CStr());
return;
}
break;
case 0x130:
mregs[addr]=val; //modulo variables :)
RMOD=((val&1)<<7)+((val&12)<<8)+((val&0x70)<<4);
val>>=8;
WMOD=((val&1)<<7)+((val&12)<<8)+((val&0x70)<<4);
break;
default:
mregs[addr]=val;
break;
}
}
unsigned int OFFSET;
unsigned int temp1;
unsigned int Flag;
double HDDX,HDDY,HDX,HDY,VDX,VDY,XPOS,YPOS,HDX_2,HDY_2;
int HDDX1616,HDDY1616,HDX1616,HDY1616,VDX1616,VDY1616,XPOS1616,YPOS1616,HDX1616_2,HDY1616_2;
char TEXEL_FUN_NUMBER;
int TEXTURE_WI_START,TEXTURE_HI_START,TEXEL_INCX,TEXEL_INCY;
int TEXTURE_WI_LIM, TEXTURE_HI_LIM;
void LoadPLUT(unsigned int pnt,int n)
{
int i;
for(i=0;i<n;i++)
{
PLUT[i]=_mem_read16((((pnt>>1)+i)^1)<<1);
}
}
int CCBCOUNTER;
int _madam_HandleCEL()
{
__smallcicles=CELCYCLES=0;
if(NEXTCCB!=0)CCBCOUNTER=0;
STATBITS|=SPRON;
Flag=0;
while((NEXTCCB!=0)&&(!Flag))
//if(_madam_FSM==FSM_INPROCESS)
{
CCBCOUNTER++;
if((NEXTCCB==0)||(Flag))
{
_madam_FSM=FSM_IDLE;
return CELCYCLES;
}
//1st step -- parce CCB and load it into registers
CURRENTCCB=NEXTCCB&0xfffffc;
if((CURRENTCCB>>20)>2)
{
_madam_FSM=FSM_IDLE;
return CELCYCLES;
}
OFFSET=CURRENTCCB;
CCBFLAGS=mread(CURRENTCCB);
CURRENTCCB+=4;
if(CCBFLAGS&CCB_PXOR)
{
PXOR1=0;
PXOR2=0x1f1f1f1f;
}
else
{
PXOR1=0xFFffFFff;
PXOR2=0;
}
Flag=0;
PLUTF=PDATF=NCCBF=0;
NEXTCCB=mread(CURRENTCCB)&(~3);
if(!(CCBFLAGS&CCB_NPABS))
{
NEXTCCB+=CURRENTCCB+4;
NEXTCCB&=0xffffff;
}
if((NEXTCCB==0))
NCCBF=1;
if((NEXTCCB>>20)>2)
NCCBF=1;
CURRENTCCB+=4;
PDATA=mread(CURRENTCCB)&(~3);
//if((PDATA==0))
// PDATF=1;
if(!(CCBFLAGS&CCB_SPABS))
{
PDATA+=CURRENTCCB+4;
PDATA&=0xffffff;
}
if((PDATA>>20)>2)
PDATF=1;
CURRENTCCB+=4;
if((CCBFLAGS&CCB_LDPLUT))
{
PLUTDATA=mread(CURRENTCCB)&(~3);
//if((PLUTDATA==0))
// PLUTF=1;
if(!(CCBFLAGS&CCB_PPABS))
{
PLUTDATA+=CURRENTCCB+4;
PLUTDATA&=0xffffff;
}
if((PLUTDATA>>20)>2)
PLUTF=1;
}
CURRENTCCB+=4;
if(NCCBF)
CCBFLAGS|=CCB_LAST;
if(CCBFLAGS&CCB_LAST)
Flag=1;
if(CCBFLAGS&CCB_YOXY)
{
XPOS1616=mread(CURRENTCCB);
XPOS=XPOS1616/65536.0;
CURRENTCCB+=4;
YPOS1616=mread(CURRENTCCB);
YPOS=YPOS1616/65536.0;
CURRENTCCB+=4;
}
else
CURRENTCCB+=8;
//if((CCBFLAGS&CCB_SKIP)&& debug)
// printf("###Cel skipped!!! PDATF=%d PLUTF=%d NCCBF=%d\n",PDATF,PLUTF,NCCBF);
if(CCBFLAGS&CCB_LAST)
NEXTCCB=0;
if(CCBFLAGS&CCB_LDSIZE)
{
HDX1616=((int)mread(CURRENTCCB))>>4;
HDX=HDX1616/65536.0;
CURRENTCCB+=4;
HDY1616=((int)mread(CURRENTCCB))>>4;
HDY=HDY1616/65536.0;
CURRENTCCB+=4;
VDX1616=mread(CURRENTCCB);
VDX=VDX1616/65536.0;
CURRENTCCB+=4;
VDY1616=mread(CURRENTCCB);
VDY=VDY1616/65536.0;
CURRENTCCB+=4;
}
if(CCBFLAGS&CCB_LDPRS)
{
HDDX1616=((int)mread(CURRENTCCB))>>4;
HDDX=HDDX1616/65536.0;
CURRENTCCB+=4;
HDDY1616=((int)mread(CURRENTCCB))>>4;
HDDY=HDDY1616/65536.0;
CURRENTCCB+=4;
}
if(CCBFLAGS&CCB_LDPPMP)
{
PIXC=mread(CURRENTCCB);
CURRENTCCB+=4;
}
if(CCBFLAGS&CCB_CCBPRE)
{
PRE0=mread(CURRENTCCB);
CURRENTCCB+=4;
if(!(CCBFLAGS&CCB_PACKED))
{
PRE1=mread(CURRENTCCB);
CURRENTCCB+=4;
}
}
else if(!PDATF)
{
PRE0=mread(PDATA);
PDATA+=4;
if(!(CCBFLAGS&CCB_PACKED))
{
PRE1=mread(PDATA);
PDATA+=4;
}
}
{// PDEC data compute
//pdec.mode=PRE0&PRE0_BPP_MASK;
switch(PRE0&PRE0_BPP_MASK)
{
case 0:
case 7:
continue;
case 1:
pdec.plutaCCBbits=(CCBFLAGS&0xf)*4;
pdec.pixelBitsMask=1; // 1 bit
break;
case 2:
pdec.plutaCCBbits=(CCBFLAGS&0xe)*4;
pdec.pixelBitsMask=3; // 2 bit
break;
default://case 3:
pdec.plutaCCBbits=(CCBFLAGS&0x8)*4;
pdec.pixelBitsMask=15; // 4 bit
break;
}
pdec.tmask=!(CCBFLAGS&CCB_BGND);
pdec.pmodeORmask= ((CCBFLAGS&CCB_POVER_MASK)==PMODE_ONE )? 0x8000:0x0000;
pdec.pmodeANDmask=((CCBFLAGS&CCB_POVER_MASK)!=PMODE_ZERO)? 0xFFFF:0x7FFF;
}
if((CCBFLAGS&CCB_LDPLUT) && !PLUTF) //load PLUT
{
switch(PRE0&PRE0_BPP_MASK)
{
case 1:
LoadPLUT(PLUTDATA,2);
break;
case 2:
LoadPLUT(PLUTDATA,4);
break;
case 3:
LoadPLUT(PLUTDATA,16);
break;
default:
LoadPLUT(PLUTDATA,32);
};
}
//ok -- CCB decoded -- let's print out our current status
//step#2 -- getting CEL data
//*
if(!(CCBFLAGS&CCB_SKIP) && !PDATF)
{
if(CCBFLAGS&CCB_PACKED)
{
DrawPackedCel_New();
}
else
{
if((PRE1&PRE1_LRFORM)&&(BPP[PRE0&PRE0_BPP_MASK]==16))
DrawLRCel_New();
else
DrawLiteralCel_New();
}
}//if(!(CCBFLAGS& CCB_SKIP))
}//while
//STATBITS&=~SPRON;
if((NEXTCCB==0)||(Flag))
{
_madam_FSM=FSM_IDLE;
}
return CELCYCLES;
}//HandleCEL