* sim/ucsim/z80.src/z80mac.h (add_u16_disp): use explicit signed char cast
[fw/sdcc] / sim / ucsim / z80.src / z80mac.h
1 /*
2  * Simulator of microcontrollers (z80mac.h)
3  *
4  * some z80 code base from Karl Bongers karl@turbobit.com
5  *
6  * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
7  *
8  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
9  *
10  */
11
12 // shift positions
13 #define BITPOS_C 0  // 1
14 #define BITPOS_P 2  // 4H
15 #define BITPOS_A 4  // 10H
16 #define BITPOS_Z 6  // 40H
17 #define BITPOS_S 7  // 80H
18
19 #define store2(addr, val) { ram->set((t_addr) (addr), val & 0xff); \
20                             ram->set((t_addr) (addr+1), (val >> 8) & 0xff); }
21 #define store1(addr, val) ram->set((t_addr) (addr), val)
22 #define get1(addr) ram->get((t_addr) (addr))
23 #define get2(addr) (ram->get((t_addr) (addr)) | (ram->get((t_addr) (addr+1)) << 8) )
24 #define fetch2() (fetch() | (fetch() << 8))
25 #define fetch1() fetch()
26 #define push2(val) {regs.SP-=2; store2(regs.SP,(val));}
27 #define push1(val) {regs.SP-=1; store1(regs.SP,(val));}
28 #define pop2(var) {var=get2(regs.SP),regs.SP+=2;}
29 //#define pop1(var) {var=get1(regs.SP),regs.SP+=1;}
30 #define add_u16_disp(_w, _d) (( (unsigned short)(_w) + (signed char)(_d) ) & 0xffff)
31
32 #define sub_A_bytereg(br) { \
33       regs.F &= ~(BIT_ALL);  /* clear these */ \
34       if (regs.A < br) regs.F |= (BIT_C | BIT_P); \
35       regs.A -= (br); \
36       regs.F |= BIT_N; /* not addition */ \
37       if (regs.A == 0) regs.F |= BIT_Z; \
38       if (regs.A & 0x80) regs.F |= BIT_S; \
39       /* Skip BIT_A for now */ \
40   }
41
42
43 #define rr_byte(reg) { \
44    if (regs.F & BIT_C) {  \
45      regs.F &= ~(BIT_ALL);  /* clear these */ \
46      if (reg & 0x01)   \
47        regs.F |= BIT_C;   \
48      reg = (reg >> 1) | 0x80; \
49    } else { \
50      regs.F &= ~(BIT_ALL);  /* clear these */ \
51      if (reg & 0x01)   \
52        regs.F |= BIT_C;   \
53      reg >>= 1; \
54    } \
55    if (reg == 0) regs.F |= BIT_Z; \
56    if (reg & 0x80) regs.F |= BIT_S; \
57    /* fixme: BIT_P(lookup table?) */ \
58 }
59
60 #define rl_byte(reg) { \
61    if (regs.F & BIT_C) {  \
62      regs.F &= ~(BIT_ALL);  /* clear these */ \
63      if (reg & 0x80)   \
64        regs.F |= BIT_C;   \
65      reg = (reg << 1) | 0x01; \
66    } else { \
67      regs.F &= ~(BIT_ALL);  /* clear these */ \
68      if (reg & 0x80)   \
69        regs.F |= BIT_C;   \
70      reg = (reg << 1); \
71    } \
72    if (reg == 0) regs.F |= BIT_Z; \
73    if (reg & 0x80) regs.F |= BIT_S; \
74    /* fixme: BIT_P(lookup table?) */ \
75 }
76
77 #define rrc_byte(reg) { \
78    regs.F &= ~(BIT_ALL);  /* clear these */ \
79    if (reg & 0x01) { \
80      regs.F |= BIT_C;   \
81      reg = (reg >> 1) | 0x80; \
82    } \
83    else \
84      reg = (reg >> 1); \
85    if (reg == 0) regs.F |= BIT_Z; \
86    if (reg & 0x80) regs.F |= BIT_S; \
87    /* fixme: BIT_P(lookup table?) */ \
88 }
89
90 #define rlc_byte(reg) { \
91    regs.F &= ~(BIT_ALL);  /* clear these */ \
92    if (reg & 0x80) {  \
93      regs.F |= BIT_C;   \
94      reg = (reg << 1) | 0x01; \
95    } else \
96      reg <<= 1; \
97    if (reg == 0) regs.F |= BIT_Z; \
98    if (reg & 0x80) regs.F |= BIT_S; \
99    /* fixme: BIT_P(lookup table?) */ \
100 }
101
102 #define sla_byte(reg) { \
103    regs.F &= ~(BIT_ALL);  /* clear these */ \
104    if (reg & 0x80)      \
105      regs.F |= BIT_C;   \
106    reg <<= 1; \
107    if (reg == 0) regs.F |= BIT_Z; \
108    if (reg & 0x80) regs.F |= BIT_S; \
109    /* fixme: BIT_P(lookup table?) */ \
110 }
111
112 #define sra_byte(reg) { \
113    regs.F &= ~(BIT_ALL);  /* clear these */ \
114    if (reg & 0x80) {  \
115      if (reg & 0x01)  \
116        regs.F |= BIT_C;   \
117      reg = (reg >> 1) | 0x80; \
118    } else { \
119      if (reg & 0x01)  \
120        regs.F |= BIT_C;   \
121      reg >>= 1; \
122    } \
123    if (reg == 0) regs.F |= BIT_Z; \
124    if (reg & 0x80) regs.F |= BIT_S; \
125    /* fixme: BIT_P(lookup table?) */ \
126 }
127
128 #define srl_byte(reg) { \
129    regs.F &= ~(BIT_ALL);  /* clear these */ \
130    if (reg & 0x01)  \
131      regs.F |= BIT_C;   \
132    reg >>= 1; \
133    if (reg == 0) regs.F |= BIT_Z; \
134    if (reg & 0x80) regs.F |= BIT_S; \
135    /* fixme: BIT_P(lookup table?) */ \
136 }
137
138 /* following not in my book, best guess based on z80.txt comments */
139 #define slia_byte(reg) { \
140    regs.F &= ~(BIT_ALL);  /* clear these */ \
141    if (reg & 0x80)      \
142      regs.F |= BIT_C;   \
143    reg = (reg << 1) | 1; \
144    if (reg == 0) regs.F |= BIT_Z; \
145    if (reg & 0x80) regs.F |= BIT_S; \
146    /* fixme: BIT_P(lookup table?) */ \
147 }
148
149 #define bit_byte(reg, _bitnum) { \
150    regs.F &= ~(BIT_ALL);  /* clear these */ \
151    regs.F |= BIT_A; \
152    if (!(reg & (1 << (_bitnum))))  \
153      regs.F |= BIT_Z; \
154    /* book shows BIT_S & BIT_P as unknown state */ \
155 }
156
157 #define add_A_bytereg(br) { \
158     unsigned int tmp; \
159       regs.F &= ~(BIT_ALL);  /* clear these */ \
160       tmp = (unsigned short)regs.A + (unsigned short)(br); \
161       regs.A = (unsigned short) tmp; \
162       /* C Z S A */ \
163       if (tmp > 0xff) regs.F |= (BIT_C | BIT_P); \
164       if (regs.A == 0) regs.F |= BIT_Z; \
165       if (regs.A & 0x80) regs.F |= BIT_S; \
166       /* Skip BIT_A for now */ \
167   }
168
169 #define adc_A_bytereg(br) { \
170     unsigned int tmp; \
171       tmp = (unsigned short)regs.A + (unsigned short)(br); \
172       if (regs.F & BIT_C) ++tmp; \
173       regs.F &= ~(BIT_ALL);  /* clear these */ \
174       regs.A = (unsigned char) tmp; \
175       if (tmp > 0xff) regs.F |= (BIT_C | BIT_P); \
176       if (regs.A == 0) regs.F |= BIT_Z; \
177       if (regs.A & 0x80) regs.F |= BIT_S; \
178       /* Skip BIT_A for now */ \
179   }
180
181 #define adc_HL_wordreg(reg) { \
182     unsigned int tmp; \
183       tmp = (unsigned int)regs.HL + (unsigned int)(reg); \
184       if (regs.F & BIT_C) ++tmp; \
185       regs.F &= ~(BIT_ALL);  /* clear these */ \
186       regs.HL = (unsigned short) tmp; \
187       if (tmp > 0xffff) regs.F |= (BIT_C | BIT_P); \
188       if (regs.HL == 0) regs.F |= BIT_Z; \
189       if (regs.HL & 0x8000) regs.F |= BIT_S; \
190       /* Skip BIT_A for now */ \
191   }
192
193 #define sbc_A_bytereg(br) { \
194     unsigned int tmp; \
195       tmp = (unsigned short)regs.A - (unsigned short)(br); \
196       if (regs.F & BIT_C) --tmp; \
197       regs.F &= ~(BIT_ALL);  /* clear these */ \
198       regs.A = (unsigned char) tmp; \
199       if (tmp > 0xff) regs.F |= (BIT_C | BIT_P); \
200       if (regs.A == 0) regs.F |= BIT_Z; \
201       if (regs.A & 0x80) regs.F |= BIT_S; \
202       regs.F |= BIT_N; \
203       /* Skip BIT_A for now */ \
204   }
205
206 #define sbc_HL_wordreg(reg) { \
207     unsigned int tmp; \
208       tmp = (unsigned int)regs.HL - (unsigned int)(reg); \
209       if (regs.F & BIT_C) --tmp; \
210       regs.F &= ~(BIT_ALL);  /* clear these */ \
211       regs.HL = (unsigned short) tmp; \
212       if (tmp > 0xffff) regs.F |= (BIT_C | BIT_P); \
213       if (regs.HL == 0) regs.F |= BIT_Z; \
214       if (regs.HL & 0x8000) regs.F |= BIT_S; \
215       regs.F |= BIT_N; \
216       /* Skip BIT_A for now */ \
217   }
218
219 #define and_A_bytereg(br) { \
220       regs.A &= (br); \
221       regs.F &= ~(BIT_ALL);  /* clear these */ \
222       if (regs.A == 0) regs.F |= BIT_Z; \
223       if (regs.A & 0x80) regs.F |= BIT_S; \
224   }
225
226 #define xor_A_bytereg(br) { \
227       regs.A ^= (br); \
228       regs.F &= ~(BIT_ALL);  /* clear these */ \
229       if (regs.A == 0) regs.F |= BIT_Z; \
230       if (regs.A & 0x80) regs.F |= BIT_S; \
231   }
232
233 #define or_A_bytereg(br) { \
234       regs.A |= (br); \
235       regs.F &= ~(BIT_ALL);  /* clear these */ \
236       if (regs.A == 0) regs.F |= BIT_Z; \
237       if (regs.A & 0x80) regs.F |= BIT_S; \
238   }
239
240 #define cp_bytereg(br) { unsigned char _tmp1; \
241       regs.F &= ~(BIT_ALL);  /* clear these */ \
242       if (regs.A < br) regs.F |= BIT_C; \
243       _tmp1 = regs.A - (br); \
244       regs.F |= BIT_N; /* not addition */ \
245       if (_tmp1 == 0) regs.F |= BIT_Z; \
246       if (_tmp1 & 0x80) regs.F |= BIT_S; \
247       /* Skip BIT_A for now */ \
248   }
249
250 #define inc(var) /* 8-bit increment */ { var++; \
251    regs.F &= ~(BIT_N |BIT_P |BIT_A |BIT_Z |BIT_S);  /* clear these */ \
252    if (var == 0) regs.F |= BIT_Z; \
253    if (var & 0x80) regs.F |= BIT_S; \
254    if ((var & 0x0f) == 0) regs.F |= BIT_A; \
255    }
256
257 #define dec(var)  { \
258  --var; \
259  regs.F &= ~(BIT_N |BIT_P |BIT_A |BIT_Z |BIT_S);  /* clear these */ \
260  regs.F |= BIT_N;  /* Not add */ \
261  if (var == 0) regs.F |= BIT_Z; \
262  if (var & 0x80) regs.F |= BIT_S; \
263  if ((var & 0x0f) == 0) regs.F |= BIT_A; \
264  }
265
266