2 * Simulator of microcontrollers (z80mac.h)
4 * some z80 code base from Karl Bongers karl@turbobit.com
6 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
8 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
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
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) + (char)(_d) ) & 0xffff)
32 #define sub_A_bytereg(br) { \
33 regs.F &= ~(BIT_ALL); /* clear these */ \
34 if (regs.A < br) regs.F |= (BIT_C | BIT_P); \
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 */ \
43 #define rr_byte(reg) { \
44 if (regs.F & BIT_C) { \
45 regs.F &= ~(BIT_ALL); /* clear these */ \
48 reg = (reg >> 1) | 0x80; \
50 regs.F &= ~(BIT_ALL); /* clear these */ \
55 if (reg == 0) regs.F |= BIT_Z; \
56 if (reg & 0x80) regs.F |= BIT_S; \
57 /* fixme: BIT_P(lookup table?) */ \
60 #define rl_byte(reg) { \
61 if (regs.F & BIT_C) { \
62 regs.F &= ~(BIT_ALL); /* clear these */ \
65 reg = (reg << 1) | 0x01; \
67 regs.F &= ~(BIT_ALL); /* clear these */ \
72 if (reg == 0) regs.F |= BIT_Z; \
73 if (reg & 0x80) regs.F |= BIT_S; \
74 /* fixme: BIT_P(lookup table?) */ \
77 #define rrc_byte(reg) { \
78 regs.F &= ~(BIT_ALL); /* clear these */ \
81 reg = (reg >> 1) | 0x80; \
85 if (reg == 0) regs.F |= BIT_Z; \
86 if (reg & 0x80) regs.F |= BIT_S; \
87 /* fixme: BIT_P(lookup table?) */ \
90 #define rlc_byte(reg) { \
91 regs.F &= ~(BIT_ALL); /* clear these */ \
94 reg = (reg << 1) | 0x01; \
97 if (reg == 0) regs.F |= BIT_Z; \
98 if (reg & 0x80) regs.F |= BIT_S; \
99 /* fixme: BIT_P(lookup table?) */ \
102 #define sla_byte(reg) { \
103 regs.F &= ~(BIT_ALL); /* clear these */ \
107 if (reg == 0) regs.F |= BIT_Z; \
108 if (reg & 0x80) regs.F |= BIT_S; \
109 /* fixme: BIT_P(lookup table?) */ \
112 #define sra_byte(reg) { \
113 regs.F &= ~(BIT_ALL); /* clear these */ \
117 reg = (reg >> 1) | 0x80; \
123 if (reg == 0) regs.F |= BIT_Z; \
124 if (reg & 0x80) regs.F |= BIT_S; \
125 /* fixme: BIT_P(lookup table?) */ \
128 #define srl_byte(reg) { \
129 regs.F &= ~(BIT_ALL); /* clear these */ \
133 if (reg == 0) regs.F |= BIT_Z; \
134 if (reg & 0x80) regs.F |= BIT_S; \
135 /* fixme: BIT_P(lookup table?) */ \
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 */ \
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?) */ \
149 #define bit_byte(reg, _bitnum) { \
150 regs.F &= ~(BIT_ALL); /* clear these */ \
152 if (!(reg & (1 << (_bitnum)))) \
154 /* book shows BIT_S & BIT_P as unknown state */ \
157 #define add_A_bytereg(br) { \
159 regs.F &= ~(BIT_ALL); /* clear these */ \
160 tmp = (unsigned short)regs.A + (unsigned short)(br); \
161 regs.A = (unsigned short) tmp; \
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 */ \
169 #define adc_A_bytereg(br) { \
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 */ \
181 #define adc_HL_wordreg(reg) { \
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 */ \
193 #define sbc_A_bytereg(br) { \
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; \
203 /* Skip BIT_A for now */ \
206 #define sbc_HL_wordreg(reg) { \
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; \
216 /* Skip BIT_A for now */ \
219 #define and_A_bytereg(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; \
226 #define xor_A_bytereg(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; \
233 #define or_A_bytereg(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; \
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 */ \
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; \
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; \