-
Notifications
You must be signed in to change notification settings - Fork 16
/
asm.h
1884 lines (1578 loc) · 54.3 KB
/
asm.h
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
/*
MIT License
Copyright (c) 2017 Franck GOTTHOLD, xor2003
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef __asm_h__
#define __asm_h__
#define MYINLINE inline
#if M2CDEBUG==-1
#define OPTINLINE __attribute__ ((noinline))
#else
#define OPTINLINE MYINLINE
#endif
#include <cstdlib>
#include <cstdarg>
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#ifndef NOSDL
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#endif
extern bool from_callf;
#ifdef DOSBOX_CUSTOM
#include "json.hpp"
#include <typeinfo>
#include "custom.h"
#include "regs.h"
#if M2CDEBUG != -1
#include "cpu.h"
#elif DOSBOX_CUSTOM
void CPU_IRET(bool use32,Bitu oldeip);
bool CPU_CLI(void);
bool CPU_STI(void);
void CPU_Exception(Bitu which,Bitu error=0);
Bitu CPU_Pop16(void);
Bitu CPU_Pop32(void);
void CPU_Push16(Bitu value);
void CPU_Push32(Bitu value);
#endif
#include "mem.h"
#include "inout.h"
#include <pic.h>
#include <video.h>
#include <timer.h>
#include <vector>
extern int ticksRemain;
extern volatile bool from_interpreter;
extern bool trace_instructions;
extern bool collect_rt_info;
extern volatile bool compare_jump;
extern bool compare_instructions;
extern bool complex_self_modifications;
void increaseticks();
#include <callback.h>
#endif
// Types
#ifdef __BORLANDC__
typedef unsigned long uint32_t;
typedef long int32_t;
typedef unsigned short int uint16_t;
typedef short int int16_t;
typedef unsigned char uint8_t;
typedef char int8_t;
struct uint64_t
{
long a;
long b;
};
#define MYPACKED
#define MYINT_ENUM
#else
#include <stdint.h>
#include <stdbool.h>
#define MYPACKED __attribute__((__packed__))
#define MYINT_ENUM : int
#endif
#ifdef __GNUC__
#define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
#endif
#ifdef _MSC_VER
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
#endif
typedef uint8_t db;
typedef uint16_t dw;
typedef uint32_t dd;
typedef uint64_t dq;
//typedef uint80_t dt;
typedef db byte;
typedef dw word;
typedef dd dword;
typedef dq qword;
//typedef dt tbyte;
typedef float real4;
typedef double real8;
typedef long double real10;
#ifndef DOSBOX_CUSTOM
#include "memmgr.h"
static void CPU_Exception(int){assert(0);}
typedef int Bits;
#endif
#ifndef NOSDL
extern struct SDL_Renderer *renderer;
#endif
namespace m2c {
extern db vgaPalette[256*3];
extern struct Memory m;
extern size_t debug;
extern size_t counter;
extern db _indent;
extern const char *_str;
extern size_t inst_size(dw cs, dd eip);
struct _STATE;
void stackDump(_STATE *_state=0);
bool fix_segs();
#if DOSBOX_CUSTOM
struct CPU_Regs;
struct Segments;
extern void log_regs_dbx(const char *file, int line, const char *instr, const CPU_Regs &r, const Segments &s);
extern void execute_irqs();
void run_hw_interrupts();
extern void single_step();
#else
extern void log_regs_m2c(const char *file, int line, const char *instr, _STATE* _state);
#endif
struct flagBits{
unsigned int _CF : 1,
unused1:1,
_PF:1,
unused2:1,
_AF:1,
unused3:1,
_ZF:1,
_SF:1,
_TF:1,
_IF:1,
_DF:1,
_OF:1;
};
union flagsUnion{
flagBits bits;
dd value;
};
typedef dd _offsets;
// Regs
struct _STATE{
_STATE() {
call_source=0;
}
dd eax;
dd ebx;
dd ecx;
dd edx;
dd esi;
dd edi;
dd esp;
dd ebp;
dd eip;
dw cs;
dw ds;
dw es;
dw fs;
dw gs;
dw ss;
bool CF;
bool PF;
bool AF;
bool ZF;
bool SF;
bool DF;
bool OF;
bool IF;
bool TF;
dd other_flags;
int call_source;
};
#if M2CDEBUG==-1 //decomp
#include "asm_regs_decomp.h"
#elif DOSBOX_CUSTOM==0 //masm2c
#include "asm_regs_m2c.h"
#else // libdosbox
#include "asm_regs_dbx.h"
#endif
typedef bool m2cf(_offsets, struct _STATE*); // common masm2c function
template<class S>
constexpr bool isaddrbelongtom(const S *const a) {
return ((const db *const) &m < (const db *const) a) && ((const db *const) &m + 16 * 1024 * 1024 >
(const db *const) a);
}
static bool isaddrbelongtovga(dd a) {
return (0xa0000 <= a) && (a < 0xc0000);
}
//template<class S>
//S getdata(const S &s);
// extern struct Memory types;
// static int log_debug(const char *format, ...);
template<class S>
inline void check_type(const S &) {
/*
#if M2CDEBUG >=4
size_t addr = (((db*)&s)-((db*)&m2c::m));
if (addr >= (0x1920+0x100) && addr < (0x1920+0x10000) && ( *(S*)(((db*)&m2c::types)+addr) )==0 && s !=0)
log_debug("Read of uninit addr:%zx size:%zd %zx\n",addr-0x1920,(size_t)sizeof(S),(*(S*)(((db*)&m2c::types)+addr)) );
#endif
*/
}
#if DOSBOX_CUSTOM && M2CDEBUG != -1
static inline db getdata(const db &s) {
if (m2c::isaddrbelongtom(&s)) {
check_type(s);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)&s, sizeof(s));
return mem_readb((db *) &s - (db *) &m);
}
else return s;
}
static inline dw getdata(const dw &s) {
if (m2c::isaddrbelongtom(&s)) {
check_type(s);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)&s, sizeof(s));
return mem_readw((db *) &s - (db *) &m);
}
else return s;
}
static inline dd getdata(const dd &s) {
if (m2c::isaddrbelongtom(&s)) {
check_type(s);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)&s, sizeof(s));
return mem_readd((db *) &s - (db *) &m);
}
else return s;
}
// template<>
static inline db getdata(const char &s) {
if (m2c::isaddrbelongtom(&s)) {
check_type(s);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)&s, sizeof(s));
return mem_readb((db *) &s - (db *) &m);
}
else return s;
}
static inline dw getdata(const short int &s) {
if (m2c::isaddrbelongtom(&s)) {
check_type(s);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)&s, sizeof(s));
return mem_readw((db *) &s - (db *) &m);
}
else return s;
}
static inline dd getdata(const int &s) {
if (m2c::isaddrbelongtom(&s)) {
check_type(s);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)&s, sizeof(s));
return mem_readd((db *) &s - (db *) &m);
}
else return s;
}
static inline dd getdata(const long &s) {
if (m2c::isaddrbelongtom(&s)) {
check_type(s);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)&s, 4);
return mem_readd((db *) &s - (db *) &m);
}
else return s;
}
static inline dd getdata(const long long &s) {
if (m2c::isaddrbelongtom(&s)) {
check_type(s);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)&s, 4);
return mem_readd((db *) &s - (db *) &m);
}
else return s;
}
template<class S>
inline void set_type(const S &) {
}
static inline void setdata(db *d, db s) {
if (m2c::isaddrbelongtom(d)) {
set_type(*d);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)d, sizeof(*d));
mem_writeb((db *) d - (db *) &m, s);
}
else *d = s;
}
static inline void setdata(char *d, db s) {
if (m2c::isaddrbelongtom(d)) {
set_type(*d);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)d, sizeof(*d));
mem_writeb((db *) d - (db *) &m, s);
}
else *d = s;
}
static inline void setdata(dw *d, dw s) {
if (m2c::isaddrbelongtom(d)) {
set_type(*d);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)d, sizeof(*d));
mem_writew((db *) d - (db *) &m, s);
}
else *d = s;
}
static inline void setdata(dd *d, dd s) {
if (m2c::isaddrbelongtom(d)) {
set_type(*d);
// if (collect_rt_info) m2c::shadow_memory.collect_data((db*)d, sizeof(*d));
mem_writed((db *) d - (db *) &m, s);
}
else *d = s;
}
#else
template<class S>
inline void set_type(const S &) {
}
inline db getdata(const db& s)
{ return s; }
inline dw getdata(const dw& s)
{ return s; }
inline dd getdata(const dd& s)
{ return s; }
inline char getdata(const char& s)
{ return s; }
inline short int getdata(const short int& s)
{ return s; }
inline int getdata(const int& s)
{ return s; }
inline long getdata(const long& s)
{ return s; }
#ifdef _WIN32
inline dd getdata(const __int64& s)
{
return s;
}
#endif
static inline void setdata(db *d, db s) {
#if SDL_MAJOR_VERSION == 2 && !defined(NOSDL) && M2CDEBUG != -1
if (m2c::isaddrbelongtom(d) && d < ((db*)&m) + 0xc0000 && d >= ((db*)&m) + 0xa0000)
{
dw di = d - ((db*)&m) - 0xa0000;
// printf("x=%d y=%d c=%d\n",di%320, di/320,s);
SDL_SetRenderDrawColor(renderer, vgaPalette[3*s+2], vgaPalette[3*s+1], vgaPalette[3*s], 255);
SDL_RenderDrawPoint(renderer, di%320, di/320); \
SDL_RenderPresent(renderer);
}
else
#endif
{
*d = s;
}
}
static inline void setdata(char *d, db s) {
*d = s;
}
static inline void setdata(dw *d, dw s) {
*d = s;
}
static inline void setdata(dd *d, dd s) {
*d = s;
}
#endif
extern FILE * logDebug;
// Asm functions
#ifdef DOSBOX_CUSTOM
extern int log_debug(const char *format, ...);
//#define log_debug printf
#define log_error log_debug
#define log_info log_debug
extern const char *log_spaces(int n);
#else
void log_error(const char *fmt, ...);
void log_debug(const char *fmt, ...);
void log_info(const char *fmt, ...);
void log_debug2(const char *fmt, ...);
const char* log_spaces(int n);
#endif
#define VGARAM_SIZE (320*200)
#ifdef __BORLANDC__
#define STACK_SIZE 4096
#define HEAP_SIZE 1024
#else
#define STACK_SIZE (1024*64-16)
#define HEAP_SIZE 1024*1024 - 16 - STACK_SIZE
#endif
#define NB_SELECTORS 128
#ifdef __cplusplus
//extern "C" {
#endif
static const uint32_t MASK[]={0, 0xff, 0xffff, 0xffffff, 0xffffffff};
template <class D>
constexpr size_t bitsizeof(D) // size of type in bits
{ return 8*sizeof(D); }
template <class D>
inline size_t getbit(D dest,int bit) // get specific bit
{ return ((bit>=0)?(( (dest) >> bit)&1):0); }
template <class D>
inline size_t shiftmodule(D dest,size_t shift) // get module of shift steps based on destiantion size in bits
{ return shift&(bitsizeof(dest)-1); }
template <class D>
inline dd nthbitone(D dest,size_t bit) // return n-th bit with 1
{ return ( (dd)1 << shiftmodule(dest,bit)); }
template <class D, class S>
inline void bitset(D& dest, S src, size_t bit) // set n-th bit to 1
{dest=(bit>=0)?(( (dest) & (~nthbitone(dest,bit))) | ((src&1) << bit)):dest;}
inline static bool LSB(dd a) {return (a&1) != 0;} // get lower bit
template <class D>
inline bool MSB(D a) // get highest bit
{return (( (a)>>( m2c::bitsizeof(a)-1) )&1) != 0;}
#if defined(_WIN32) || defined(__INTEL_COMPILER)
#define INLINE __inline
#elif defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
#define INLINE inline
#elif defined(__GNUC__)
#define INLINE __inline__
#else
#define INLINE
#endif
#if _BITS == 32
#include "asm_32.h"
#else
#include "asm_16.h"
#endif
#define raddr(s, o) m2c::raddr_(s, o)
#define realAddress(offset, segment) m2c::raddr_(segment,offset)
#define seg_offset(segment) ((dw)(((db*)(&segment)-(db*)(&m2c::m))>>4))
// DJGPP
#define MASK_LINEAR(addr) (((size_t)addr) & 0x000FFFFF)
#define RM_TO_LINEAR(addr) (((((size_t)addr) & 0xFFFF0000) >> 12) + (((size_t)addr) & 0xFFFF))
#define RM_OFFSET(addr) (((size_t)addr) & 0xF)
#define RM_SEGMENT(addr) ((((size_t)addr) >> 4) & 0xFFFF)
extern class ShadowStack shadow_stack;
#define GET_DF() m2cflags.getDF()
#define GET_CF() m2cflags.getCF()
#define GET_AF() m2cflags.getAF()
#define GET_OF() m2cflags.getOF()
#define GET_SF() m2cflags.getSF()
#define GET_ZF() m2cflags.getZF()
#define GET_PF() m2cflags.getPF()
#define GET_IF() m2cflags.getIF()
#define AFFECT_DF(a) m2cflags.setDF(a)
#define AFFECT_CF(a) m2cflags.setCF(a)
#define AFFECT_AF(a) m2cflags.setAF(a)
#define AFFECT_OF(a) m2cflags.setOF(a)
#define AFFECT_IF(a) m2cflags.setIF(a)
#define ISNEGATIVE(f, a) ( (a) & (1 << (m2c::bitsizeof(f)-1)) )
#define AFFECT_SF(a) m2cflags.setSF(a)
#define AFFECT_SF_(f, a) {AFFECT_SF(ISNEGATIVE(f,a));}
#define AFFECT_ZF(a) m2cflags.setZF(a)
#define AFFECT_ZFifz(a) m2cflags.setZF((a)==0)
#define AFFECT_PF(a) m2cflags.setPF(a)
#ifdef DOSBOX_CUSTOM
#define PUSH(a) {m2c::PUSH_(a);}
#define POP(a) {m2c::POP_(a);}
template<typename S>
void PUSH_(S a);
template<>
OPTINLINE void PUSH_<dw>(dw a) { fix_segs();CPU_Push16(a); }
template<>
OPTINLINE void PUSH_<dd>(dd a) { fix_segs();CPU_Push32(a); }
template<>
OPTINLINE void PUSH_<short int>(short int a) { fix_segs();CPU_Push16(a); }
template<>
OPTINLINE void PUSH_<int>(int a) { fix_segs();CPU_Push32(a); }
OPTINLINE void POP_(dw &a) { fix_segs();a = CPU_Pop16(); }
OPTINLINE void POP_(dd &a) { fix_segs();a = CPU_Pop32(); }
#else
#define PUSH(a) {m2c::PUSH_(a, _state);}
#define POP(a) {m2c::POP_(a, _state);}
template<typename S>
OPTINLINE void PUSH_(S a, _STATE *_state)
{
X86_REGREF
dd averytemporary=a;stackPointer-=sizeof(a);
memcpy (m2c::raddr_(ss,stackPointer), &averytemporary, sizeof (a));
#if M2CDEBUG > 0
m2c::log_debug("after push %x\n",stackPointer);
#endif
#ifdef SHADOW_STACK
m2c::shadow_stack.push(_state,(dd)(a));
#endif
}
// assert((m2c::raddr_(ss,stackPointer) - ((db*)&stack))>8);
template<typename S>
OPTINLINE void POP_(S& a, _STATE *_state)
{
X86_REGREF
#ifdef SHADOW_STACK
m2c::shadow_stack.pop(_state);
#endif
#if M2CDEBUG > 0
m2c::log_debug("before pop %x\n",stackPointer);
#endif
memcpy (&a, m2c::raddr_(ss,stackPointer), sizeof (a));stackPointer+=sizeof(a);
}
#endif
#define PUSHAD m2c::PUSHAD_(_state)
//pusha AX, CX, DX, BX, SP, BP, SI, DI
static void PUSHAD_(_STATE* _state) {
X86_REGREF
dd oldesp = esp;
PUSH(eax);
PUSH(ecx);
PUSH(edx);
PUSH(ebx);
PUSH(oldesp);
PUSH(ebp);
PUSH(esi);
PUSH(edi);
}
//pushad EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI
#define POPAD m2c::POPAD_(_state)
static void POPAD_(_STATE* _state) {
X86_REGREF
POP(edi);
POP(esi);
POP(ebp);
POP(ebx);
POP(ebx);
POP(edx);
POP(ecx);
POP(eax);
}
#define PUSHA m2c::PUSHA_(_state)
static void PUSHA_(_STATE* _state) {
X86_REGREF
dw oldsp = sp;
PUSH(ax);
PUSH(cx);
PUSH(dx);
PUSH(bx);
PUSH(oldsp);
PUSH(bp);
PUSH(si);
PUSH(di);
}
#define POPA m2c::POPA_(_state)
static void POPA_(_STATE* _state) {
X86_REGREF
POP(di);
POP(si);
POP(bp);
POP(bx);
POP(bx);
POP(dx);
POP(cx);
POP(ax);
}
extern bool defered_irqs;
OPTINLINE static void defer_irqs()
{defered_irqs=true;}
#if DOSBOX_CUSTOM
#define STI {CPU_STI();m2c::defer_irqs();}
#define CLI {CPU_CLI();}
#else
#define STI UNIMPLEMENTED
#define CLI UNIMPLEMENTED
#endif
#define CMP(a, b) m2c::CMP_(a, b, m2cflags)
template <class D, class S>
MYINLINE void CMP_(const D &dest_, const S &src_, m2c::eflags &m2cflags) {
//printf("\n\n%s %s ",typeid(D).name(),typeid(S).name());
auto dest = m2c::getdata(dest_);
auto src = m2c::getdata(src_);
decltype(dest) result = dest - src;
AFFECT_CF(result>dest);
const D highestbitset = (1 << (m2c::bitsizeof(dest) - 1));
AFFECT_OF(((dest ^ src) & (dest ^ result)) & highestbitset);
AFFECT_ZFifz(result);
AFFECT_SF_(result,result);
}
#define OR(a, b) m2c::OR_(a, b, m2cflags)
template <class D, class S>
MYINLINE void OR_(D &dest, const S &src, m2c::eflags &m2cflags) {
D result = m2c::getdata(dest) | static_cast<D>(m2c::getdata(src));
m2c::setdata(&dest, result);
AFFECT_ZFifz(result);
AFFECT_SF_(result, result);
AFFECT_CF(0);
AFFECT_OF(0);
}
#define XOR(a, b) m2c::XOR_(a, b, m2cflags)
template <class D, class S>
MYINLINE void XOR_(D &dest, const S &src, m2c::eflags &m2cflags) {
D result = m2c::getdata(dest) ^ static_cast<D>(m2c::getdata(src));
m2c::setdata(&dest, result);
AFFECT_ZFifz(result);
AFFECT_SF_(result, result);
AFFECT_CF(0);
AFFECT_OF(0);
}
#define AND(a, b) m2c::AND_(a, b, m2cflags)
template <class D, class S>
MYINLINE void AND_(D &dest, const S &src, m2c::eflags &m2cflags) {
D result = m2c::getdata(dest) & static_cast<D>(m2c::getdata(src));
m2c::setdata(&dest, result);
AFFECT_ZFifz(result);
AFFECT_SF_(result, result);
AFFECT_CF(0);
AFFECT_OF(0);
}
#define NEG(a) m2c::NEG_(a, m2cflags)
template <class D>
MYINLINE void NEG_(D &a, m2c::eflags &m2cflags) {
AFFECT_CF((a)!=0);
D highestbitset = (1<<( m2c::bitsizeof(a)-1));
AFFECT_OF(a==highestbitset);
a=-a;
AFFECT_ZFifz(a);
AFFECT_SF_(a,a);
}
#define TEST(a, b) m2c::TEST_(a, b, m2cflags)
template <class D, class S>
MYINLINE void TEST_(D &a, const S &b, m2c::eflags &m2cflags) {
AFFECT_ZFifz((a) & (b));
AFFECT_CF(0);
AFFECT_SF_(a,(a)&(b));
AFFECT_OF(0);
}
#define SHR(a, b) m2c::SHR_(a, b, m2cflags)
template <class D, class S>
MYINLINE void SHR_(D &a, const S &b, m2c::eflags &m2cflags) {
if (b) {
AFFECT_CF((a >> (b - 1)) & 1);
const D highestbitset = (1 << (m2c::bitsizeof(a) - 1));
D res=a>>b;
AFFECT_OF((b & 0x1f) == 1 ? (a & highestbitset) != 0 : false);
AFFECT_ZFifz(res);
AFFECT_SF_(res,res);
a = res;
}
}
#define SHL(a, b) m2c::SHL_(a, b, m2cflags)
template <class D, class S>
MYINLINE void SHL_(D &a, const S &b, m2c::eflags &m2cflags) {
if (b) {
AFFECT_CF((a) & (1 << (m2c::bitsizeof(a) - (b))));
D olda = a;
a = a << b;
AFFECT_ZFifz(a);
AFFECT_SF_(a, a);
D highestbitset = (1 << (m2c::bitsizeof(a) - 1));
AFFECT_OF((a ^ olda) & highestbitset);
}
}
#define ROR(a, b) m2c::ROR_(a, b, m2cflags)
template <class D, class S>
MYINLINE void ROR_(D &a, S b, m2c::eflags &m2cflags) {
if (b) {
AFFECT_CF(((a) >> (m2c::shiftmodule(a, b) - 1)) & 1);\
a=((a)>>(m2c::shiftmodule(a,b)) | a<<(m2c::bitsizeof(a)-(m2c::shiftmodule(a,b))));
D highestbitset = (1 << (m2c::bitsizeof(a) - 1));
AFFECT_OF((a ^ (a << 1)) & highestbitset);
}
}
#define ROL(a, b) m2c::ROL_(a, b, m2cflags)
template <class D, class S>
MYINLINE void ROL_(D &a, S b, m2c::eflags &m2cflags) {
if (b) {
a = (((a) << (shiftmodule(a, b))) | (a) >> (bitsizeof(a) - (shiftmodule(a, b))));\
AFFECT_CF(LSB(a));
AFFECT_OF((a & 1) ^ (a >> (m2c::bitsizeof(a) - 1)));
}
}
#define RCL(a, b) m2c::RCL_(a, b, m2cflags)
template <class D, class C>
MYINLINE void RCL_(D &op1, C op2, m2c::eflags &m2cflags) {
db lf_var2b=op2%(m2c::bitsizeof(op1) + 1);
if (!lf_var2b) return;
D cf=GET_CF()&1;
D lf_var1w=op1;
D lf_resw;
if (lf_var2b == 1) {
lf_resw = (lf_var1w << 1) | cf;
} else {
lf_resw=(lf_var1w << lf_var2b) |
(cf << (lf_var2b-1)) |
(lf_var1w >> ((m2c::bitsizeof(op1) + 1)-lf_var2b));
}
op1 = lf_resw;
AFFECT_CF((lf_var1w >> (m2c::bitsizeof(op1)-lf_var2b)) & 1);
AFFECT_OF(GET_CF() ^ m2c::MSB(op1));
}
#define RCR(a, b) m2c::RCR_(a, b, m2cflags)
template <class D, class C>
MYINLINE void RCR_(D &op1, C op2, m2c::eflags &m2cflags) {
db lf_var2b=op2%(m2c::bitsizeof(op1) + 1);
if (!lf_var2b) return;
D cf=GET_CF()&1;
D lf_var1w=op1;
D lf_resw;
if (lf_var2b == 1) {
lf_resw = (lf_var1w >> 1) | (cf << (m2c::bitsizeof(op1) - 1));
} else {
lf_resw = (lf_var1w >> lf_var2b) | (cf << (m2c::bitsizeof(op1) - lf_var2b)) |
(lf_var1w << ((m2c::bitsizeof(op1) + 1) - lf_var2b));
}
op1 = lf_resw;
AFFECT_CF((lf_var1w >> (lf_var2b-1)) & 1);
D highestbitset = (1<<( m2c::bitsizeof(op1)-1));
AFFECT_OF((lf_resw ^ (lf_resw << 1))&highestbitset);
}
template <class D>
void SHLD_(D& Destination, D Source, size_t Count, m2c::eflags& m2cflags);
template <class D>
MYINLINE void SHRD_(D &Destination, D Source, size_t Count, m2c::eflags &m2cflags) {
if(Count != 0) {
size_t TCount = Count & (2 * m2c::bitsizeof(Destination) - 1);
if (TCount > m2c::bitsizeof(Destination)) {
SHLD_(Destination, Source, 2 * m2c::bitsizeof(Destination) - TCount, m2cflags);
}
else {
AFFECT_CF(m2c::getbit(Destination, TCount - 1));
Destination >>= TCount;
for(int i = m2c::bitsizeof(Destination) - TCount; i <= m2c::bitsizeof(Destination) - 1; ++i)
if (i >= 0) {
m2c::bitset(Destination, m2c::getbit(Source, i + TCount - m2c::bitsizeof(Destination)), i);
}
if (m2c::bitsizeof(Destination) - TCount < 0) {
AFFECT_CF(m2c::getbit(Source, TCount - m2c::bitsizeof(Destination) - 1));
}
}
}
}
//template <class D>
//void SHLD_(D& op1, D op2, size_t op3, m2c::eflags& m2cflags);
/*
{
if(Count != 0) {
int TCount = Count&(2*m2c::bitsizeof(Destination)-1);
if (TCount>m2c::bitsizeof(Destination)) {SHRD_(Destination, Source, 2*m2c::bitsizeof(Destination)-TCount, m2cflags);}
else
{
AFFECT_CF(((Destination<<m2c::bitsizeof(Destination)+Source) >> (32 - Count)) & 1);
// AFFECT_CF(m2c::getbit(Destination,m2c::bitsizeof(Destination)-TCount));
Destination<<=TCount;
for(int i = 0; i < TCount; ++i)
if (i>=0) {m2c::bitset(Destination,m2c::getbit(Source,m2c::bitsizeof(Destination) - TCount + i),i);}
}
}
}
*/
//template <>
static void SHLD_(dw &Destination, dw Source, size_t Count, m2c::eflags &m2cflags) {
if(Count != 0) {
size_t TCount = Count & (2 * m2c::bitsizeof(Destination) - 1);
if (TCount > m2c::bitsizeof(Destination)) {
SHRD_(Destination, Source, 2 * m2c::bitsizeof(Destination) - TCount, m2cflags);
}
else {
//AFFECT_CF(((Destination<<m2c::bitsizeof(Destination)+Source) >> (32 - Count)) & 1);
AFFECT_CF(m2c::getbit(Destination,m2c::bitsizeof(Destination)-TCount));
dw originalDest = Destination;
Destination<<=TCount;
for(int i = 0; i < TCount; ++i)
if (i >= 0) {
m2c::bitset(Destination, m2c::getbit(Source, m2c::bitsizeof(Destination) - TCount + i), i);
}
// AFFECT_CF((Destination >> (32 - TCount)) & 1);
AFFECT_OF((Destination ^ originalDest) & 0x8000);
}
}
}
//template <>
static void SHLD_(dd &op1, dd op2, size_t op3, m2c::eflags &m2cflags) {
db val=op3 & 0x1F;
if (!val) return;
db lf_var2b=val;
dd lf_var1d=op1;
op1 = (lf_var1d << lf_var2b) | (op2 >> (32-lf_var2b));
AFFECT_CF((lf_var1d >> (32 - lf_var2b)) & 1);
AFFECT_OF((op1 ^ lf_var1d) & 0x80000000);
}
#define SHRD(a, b, c) {m2c::SHRD_(a, b, c, m2cflags);if (c) {AFFECT_ZFifz(a);AFFECT_SF_(a,a);}}
#define SHLD(a, b, c) {m2c::SHLD_(a, b, c, m2cflags);if (c) {AFFECT_ZFifz(a);AFFECT_SF_(a,a);}}
/*
#define SAR(a,b) {if (b) {bool sign = m2c::MSB(a);\
int shift = (m2c::bitsizeof(a)-b);\
shift = shift>0?shift:0;\
dd sigg=shift<(m2c::bitsizeof(a))?( (sign?-1:0)<<shift):0;\
a = b>m2c::bitsizeof(a)?0:a;\
AFFECT_CF((a >> (b-1))&1);\
a=sigg | (a >> b);\
AFFECT_ZFifz(a);\
AFFECT_SF_(a,a);}} // TODO optimize
*/
#define SAR(a, b) m2c::SAR_(a, b, m2cflags)
template <class D, class S>
MYINLINE void SAR_(D &op1, const S &op2, m2c::eflags &m2cflags) {
if (op2){
D lf_var1w = op1;
db lf_var2b = op2;
AFFECT_CF((op1>>(op2-1))&1);
if (lf_var2b>m2c::bitsizeof(op1)) lf_var2b=m2c::bitsizeof(op1);
D highestbitset = (1<<( m2c::bitsizeof(op1)-1));
D lf_resw;
if (lf_var1w & highestbitset) {
lf_resw=(lf_var1w >> lf_var2b)|
(((D)(-1)) << (m2c::bitsizeof(op1) - lf_var2b));
} else {
lf_resw=lf_var1w >> lf_var2b;
}
op1 = lf_resw;
AFFECT_ZFifz(lf_resw);
AFFECT_SF_(lf_resw,lf_resw);
AFFECT_OF(false);
}
}
#define SAL(a,b) SHL(a,b)
#define DAA { \
if (((al & 0x0F)>0x09) || GET_AF()) { \
if ((al > 0x99) || GET_CF()) { \
al+=0x60; \
AFFECT_CF(true); \
} else { \
AFFECT_CF(false); \
} \
al+=0x06; \
AFFECT_AF(true); \
} else { \
if ((al > 0x99) || GET_CF()) { \
al+=0x60; \
AFFECT_CF(true); \
} else { \
AFFECT_CF(false); \
} \
AFFECT_AF(false); \
} \