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
|
/* z80_macros.h: Some commonly used z80 things as macros
Copyright (c) 1999-2011 Philip Kendall
$Id: z80_macros.h 4624 2012-01-09 20:59:35Z pak21 $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#ifndef FUSE_Z80_MACROS_H
#define FUSE_Z80_MACROS_H
/* Macros used for accessing the registers */
#define A z80.af.b.h
#define F z80.af.b.l
#define AF z80.af.w
#define B z80.bc.b.h
#define C z80.bc.b.l
#define BC z80.bc.w
#define D z80.de.b.h
#define E z80.de.b.l
#define DE z80.de.w
#define H z80.hl.b.h
#define L z80.hl.b.l
#define HL z80.hl.w
#define A_ z80.af_.b.h
#define F_ z80.af_.b.l
#define AF_ z80.af_.w
#define B_ z80.bc_.b.h
#define C_ z80.bc_.b.l
#define BC_ z80.bc_.w
#define D_ z80.de_.b.h
#define E_ z80.de_.b.l
#define DE_ z80.de_.w
#define H_ z80.hl_.b.h
#define L_ z80.hl_.b.l
#define HL_ z80.hl_.w
#define IXH z80.ix.b.h
#define IXL z80.ix.b.l
#define IX z80.ix.w
#define IYH z80.iy.b.h
#define IYL z80.iy.b.l
#define IY z80.iy.w
#define SPH z80.sp.b.h
#define SPL z80.sp.b.l
#define SP z80.sp.w
#define PCH z80.pc.b.h
#define PCL z80.pc.b.l
#define PC z80.pc.w
#define I z80.i
#define R z80.r
#define R7 z80.r7
#define IFF1 z80.iff1
#define IFF2 z80.iff2
#define IM z80.im
#define IR ( ( z80.i ) << 8 | ( z80.r7 & 0x80 ) | ( z80.r & 0x7f ) )
/* The flags */
#define FLAG_C 0x01
#define FLAG_N 0x02
#define FLAG_P 0x04
#define FLAG_V FLAG_P
#define FLAG_3 0x08
#define FLAG_H 0x10
#define FLAG_5 0x20
#define FLAG_Z 0x40
#define FLAG_S 0x80
/* Get the appropriate contended memory delay. Use a macro for performance
reasons in the main core, but a function for flexibility when building
the core tester */
#ifndef CORETEST
#define contend_read(address,time) \
if( memory_map_read[ (address) >> MEMORY_PAGE_SIZE_LOGARITHM ].contended ) \
tstates += ula_contention[ tstates ]; \
tstates += (time);
#define contend_read_no_mreq(address,time) \
if( memory_map_read[ (address) >> MEMORY_PAGE_SIZE_LOGARITHM ].contended ) \
tstates += ula_contention_no_mreq[ tstates ]; \
tstates += (time);
#define contend_write_no_mreq(address,time) \
if( memory_map_write[ (address) >> MEMORY_PAGE_SIZE_LOGARITHM ].contended ) \
tstates += ula_contention_no_mreq[ tstates ]; \
tstates += (time);
#else /* #ifndef CORETEST */
void contend_read( libspectrum_word address, libspectrum_dword time );
void contend_read_no_mreq( libspectrum_word address, libspectrum_dword time );
void contend_write_no_mreq( libspectrum_word address, libspectrum_dword time );
#endif /* #ifndef CORETEST */
/* Some commonly used instructions */
#define AND(value)\
{\
A &= (value);\
F = FLAG_H | sz53p_table[A];\
}
#define ADC(value)\
{\
libspectrum_word adctemp = A + (value) + ( F & FLAG_C ); \
libspectrum_byte lookup = ( ( A & 0x88 ) >> 3 ) | \
( ( (value) & 0x88 ) >> 2 ) | \
( ( adctemp & 0x88 ) >> 1 ); \
A=adctemp;\
F = ( adctemp & 0x100 ? FLAG_C : 0 ) |\
halfcarry_add_table[lookup & 0x07] | overflow_add_table[lookup >> 4] |\
sz53_table[A];\
}
#define ADC16(value)\
{\
libspectrum_dword add16temp= HL + (value) + ( F & FLAG_C ); \
libspectrum_byte lookup = ( ( HL & 0x8800 ) >> 11 ) | \
( ( (value) & 0x8800 ) >> 10 ) | \
( ( add16temp & 0x8800 ) >> 9 ); \
HL = add16temp;\
F = ( add16temp & 0x10000 ? FLAG_C : 0 )|\
overflow_add_table[lookup >> 4] |\
( H & ( FLAG_3 | FLAG_5 | FLAG_S ) ) |\
halfcarry_add_table[lookup&0x07]|\
( HL ? 0 : FLAG_Z );\
}
#define ADD(value)\
{\
libspectrum_word addtemp = A + (value); \
libspectrum_byte lookup = ( ( A & 0x88 ) >> 3 ) | \
( ( (value) & 0x88 ) >> 2 ) | \
( ( addtemp & 0x88 ) >> 1 ); \
A=addtemp;\
F = ( addtemp & 0x100 ? FLAG_C : 0 ) |\
halfcarry_add_table[lookup & 0x07] | overflow_add_table[lookup >> 4] |\
sz53_table[A];\
}
#define ADD16(value1,value2)\
{\
libspectrum_dword add16temp = (value1) + (value2); \
libspectrum_byte lookup = ( ( (value1) & 0x0800 ) >> 11 ) | \
( ( (value2) & 0x0800 ) >> 10 ) | \
( ( add16temp & 0x0800 ) >> 9 ); \
(value1) = add16temp;\
F = ( F & ( FLAG_V | FLAG_Z | FLAG_S ) ) |\
( add16temp & 0x10000 ? FLAG_C : 0 )|\
( ( add16temp >> 8 ) & ( FLAG_3 | FLAG_5 ) ) |\
halfcarry_add_table[lookup];\
}
/* This may look fairly inefficient, but the (gcc) optimiser does the
right thing assuming it's given a constant for 'bit' */
#define BIT( bit, value ) \
{ \
F = ( F & FLAG_C ) | FLAG_H | ( value & ( FLAG_3 | FLAG_5 ) ); \
if( ! ( (value) & ( 0x01 << (bit) ) ) ) F |= FLAG_P | FLAG_Z; \
if( (bit) == 7 && (value) & 0x80 ) F |= FLAG_S; \
}
#define BIT_I( bit, value, address ) \
{ \
F = ( F & FLAG_C ) | FLAG_H | ( ( address >> 8 ) & ( FLAG_3 | FLAG_5 ) ); \
if( ! ( (value) & ( 0x01 << (bit) ) ) ) F |= FLAG_P | FLAG_Z; \
if( (bit) == 7 && (value) & 0x80 ) F |= FLAG_S; \
}
#define CALL()\
{\
libspectrum_byte calltempl, calltemph; \
calltempl=readbyte(PC++);\
calltemph=readbyte( PC ); \
contend_read_no_mreq( PC, 1 ); PC++;\
PUSH16(PCL,PCH);\
PCL=calltempl; PCH=calltemph;\
}
#define CP(value)\
{\
libspectrum_word cptemp = A - value; \
libspectrum_byte lookup = ( ( A & 0x88 ) >> 3 ) | \
( ( (value) & 0x88 ) >> 2 ) | \
( ( cptemp & 0x88 ) >> 1 ); \
F = ( cptemp & 0x100 ? FLAG_C : ( cptemp ? 0 : FLAG_Z ) ) | FLAG_N |\
halfcarry_sub_table[lookup & 0x07] |\
overflow_sub_table[lookup >> 4] |\
( value & ( FLAG_3 | FLAG_5 ) ) |\
( cptemp & FLAG_S );\
}
/* Macro for the {DD,FD} CB dd xx rotate/shift instructions */
#define DDFDCB_ROTATESHIFT(time, target, instruction)\
tstates+=(time);\
{\
(target) = readbyte( tempaddr );\
instruction( (target) );\
writebyte( tempaddr, (target) );\
}\
break
#define DEC(value)\
{\
F = ( F & FLAG_C ) | ( (value)&0x0f ? 0 : FLAG_H ) | FLAG_N;\
(value)--;\
F |= ( (value)==0x7f ? FLAG_V : 0 ) | sz53_table[value];\
}
#define Z80_IN( reg, port )\
{\
(reg)=readport((port));\
F = ( F & FLAG_C) | sz53p_table[(reg)];\
}
#define INC(value)\
{\
(value)++;\
F = ( F & FLAG_C ) | ( (value)==0x80 ? FLAG_V : 0 ) |\
( (value)&0x0f ? 0 : FLAG_H ) | sz53_table[(value)];\
}
#define LD16_NNRR(regl,regh)\
{\
libspectrum_word ldtemp; \
ldtemp=readbyte(PC++);\
ldtemp|=readbyte(PC++) << 8;\
writebyte(ldtemp++,(regl));\
writebyte(ldtemp,(regh));\
break;\
}
#define LD16_RRNN(regl,regh)\
{\
libspectrum_word ldtemp; \
ldtemp=readbyte(PC++);\
ldtemp|=readbyte(PC++) << 8;\
(regl)=readbyte(ldtemp++);\
(regh)=readbyte(ldtemp);\
break;\
}
#define JP()\
{\
libspectrum_word jptemp=PC; \
PCL=readbyte(jptemp++);\
PCH=readbyte(jptemp);\
}
#define JR()\
{\
libspectrum_signed_byte jrtemp = readbyte( PC ); \
contend_read_no_mreq( PC, 1 ); contend_read_no_mreq( PC, 1 ); \
contend_read_no_mreq( PC, 1 ); contend_read_no_mreq( PC, 1 ); \
contend_read_no_mreq( PC, 1 ); \
PC += jrtemp; \
}
#define OR(value)\
{\
A |= (value);\
F = sz53p_table[A];\
}
#define POP16(regl,regh)\
{\
(regl)=readbyte(SP++);\
(regh)=readbyte(SP++);\
}
#define PUSH16(regl,regh)\
{\
writebyte( --SP, (regh) );\
writebyte( --SP, (regl) );\
}
#define RET()\
{\
POP16(PCL,PCH);\
}
#define RL(value)\
{\
libspectrum_byte rltemp = (value); \
(value) = ( (value)<<1 ) | ( F & FLAG_C );\
F = ( rltemp >> 7 ) | sz53p_table[(value)];\
}
#define RLC(value)\
{\
(value) = ( (value)<<1 ) | ( (value)>>7 );\
F = ( (value) & FLAG_C ) | sz53p_table[(value)];\
}
#define RR(value)\
{\
libspectrum_byte rrtemp = (value); \
(value) = ( (value)>>1 ) | ( F << 7 );\
F = ( rrtemp & FLAG_C ) | sz53p_table[(value)];\
}
#define RRC(value)\
{\
F = (value) & FLAG_C;\
(value) = ( (value)>>1 ) | ( (value)<<7 );\
F |= sz53p_table[(value)];\
}
#define RST(value)\
{\
PUSH16(PCL,PCH);\
PC=(value);\
}
#define SBC(value)\
{\
libspectrum_word sbctemp = A - (value) - ( F & FLAG_C ); \
libspectrum_byte lookup = ( ( A & 0x88 ) >> 3 ) | \
( ( (value) & 0x88 ) >> 2 ) | \
( ( sbctemp & 0x88 ) >> 1 ); \
A=sbctemp;\
F = ( sbctemp & 0x100 ? FLAG_C : 0 ) | FLAG_N |\
halfcarry_sub_table[lookup & 0x07] | overflow_sub_table[lookup >> 4] |\
sz53_table[A];\
}
#define SBC16(value)\
{\
libspectrum_dword sub16temp = HL - (value) - (F & FLAG_C); \
libspectrum_byte lookup = ( ( HL & 0x8800 ) >> 11 ) | \
( ( (value) & 0x8800 ) >> 10 ) | \
( ( sub16temp & 0x8800 ) >> 9 ); \
HL = sub16temp;\
F = ( sub16temp & 0x10000 ? FLAG_C : 0 ) |\
FLAG_N | overflow_sub_table[lookup >> 4] |\
( H & ( FLAG_3 | FLAG_5 | FLAG_S ) ) |\
halfcarry_sub_table[lookup&0x07] |\
( HL ? 0 : FLAG_Z) ;\
}
#define SLA(value)\
{\
F = (value) >> 7;\
(value) <<= 1;\
F |= sz53p_table[(value)];\
}
#define SLL(value)\
{\
F = (value) >> 7;\
(value) = ( (value) << 1 ) | 0x01;\
F |= sz53p_table[(value)];\
}
#define SRA(value)\
{\
F = (value) & FLAG_C;\
(value) = ( (value) & 0x80 ) | ( (value) >> 1 );\
F |= sz53p_table[(value)];\
}
#define SRL(value)\
{\
F = (value) & FLAG_C;\
(value) >>= 1;\
F |= sz53p_table[(value)];\
}
#define SUB(value)\
{\
libspectrum_word subtemp = A - (value); \
libspectrum_byte lookup = ( ( A & 0x88 ) >> 3 ) | \
( ( (value) & 0x88 ) >> 2 ) | \
( (subtemp & 0x88 ) >> 1 ); \
A=subtemp;\
F = ( subtemp & 0x100 ? FLAG_C : 0 ) | FLAG_N |\
halfcarry_sub_table[lookup & 0x07] | overflow_sub_table[lookup >> 4] |\
sz53_table[A];\
}
#define XOR(value)\
{\
A ^= (value);\
F = sz53p_table[A];\
}
#endif /* #ifndef FUSE_Z80_MACROS_H */
|