| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| Flag |
|
| 1.09375;1.094 |
| 1 | /* |
|
| 2 | * Flag.java |
|
| 3 | * |
|
| 4 | * This file is part of JavaGear. |
|
| 5 | * |
|
| 6 | * JavaGear is free software; you can redistribute it and/or modify |
|
| 7 | * it under the terms of the GNU General Public License as published by |
|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|
| 9 | * (at your option) any later version. |
|
| 10 | * |
|
| 11 | * JavaGear is distributed in the hope that it will be useful, |
|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 | * GNU General Public License for more details. |
|
| 15 | * |
|
| 16 | * You should have received a copy of the GNU General Public License |
|
| 17 | * along with JavaGear; if not, write to the Free Software |
|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
| 19 | */ |
|
| 20 | ||
| 21 | package uk.co.javagear; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * Zilog Z80 CPU flag emulation. |
|
| 25 | * |
|
| 26 | * @author Copyright (C) 2002-2003 Chris White |
|
| 27 | * @version 18th January 2003 |
|
| 28 | * @see "JavaGear Final Project Report" |
|
| 29 | */ |
|
| 30 | public final class Flag { |
|
| 31 | ||
| 32 | /** |
|
| 33 | * Pre-calculated flag settings. |
|
| 34 | */ |
|
| 35 | 0 | private static final boolean[] PARITY = { |
| 36 | true, false, false, true, false, true, true, false, |
|
| 37 | false, true, true, false, true, false, false, true, |
|
| 38 | false, true, true, false, true, false, false, true, |
|
| 39 | true, false, false, true, false, true, true, false, |
|
| 40 | false, true, true, false, true, false, false, true, |
|
| 41 | true, false, false, true, false, true, true, false, |
|
| 42 | true, false, false, true, false, true, true, false, |
|
| 43 | false, true, true, false, true, false, false, true, |
|
| 44 | false, true, true, false, true, false, false, true, |
|
| 45 | true, false, false, true, false, true, true, false, |
|
| 46 | true, false, false, true, false, true, true, false, |
|
| 47 | false, true, true, false, true, false, false, true, |
|
| 48 | true, false, false, true, false, true, true, false, |
|
| 49 | false, true, true, false, true, false, false, true, |
|
| 50 | false, true, true, false, true, false, false, true, |
|
| 51 | true, false, false, true, false, true, true, false, |
|
| 52 | false, true, true, false, true, false, false, true, |
|
| 53 | true, false, false, true, false, true, true, false, |
|
| 54 | true, false, false, true, false, true, true, false, |
|
| 55 | false, true, true, false, true, false, false, true, |
|
| 56 | true, false, false, true, false, true, true, false, |
|
| 57 | false, true, true, false, true, false, false, true, |
|
| 58 | false, true, true, false, true, false, false, true, |
|
| 59 | true, false, false, true, false, true, true, false, |
|
| 60 | true, false, false, true, false, true, true, false, |
|
| 61 | false, true, true, false, true, false, false, true, |
|
| 62 | false, true, true, false, true, false, false, true, |
|
| 63 | true, false, false, true, false, true, true, false, |
|
| 64 | false, true, true, false, true, false, false, true, |
|
| 65 | true, false, false, true, false, true, true, false, |
|
| 66 | true, false, false, true, false, true, true, false, |
|
| 67 | false, true, true, false, true, false, false, true |
|
| 68 | }; |
|
| 69 | ||
| 70 | /** |
|
| 71 | * Carry (set when a standard carry occurred). |
|
| 72 | */ |
|
| 73 | private static final int F_CARRY = 0x01; |
|
| 74 | ||
| 75 | /** |
|
| 76 | * Negative (set when instruction is subtraction, clear when addition). |
|
| 77 | */ |
|
| 78 | private static final int F_NEGATIVE = 0x02; |
|
| 79 | ||
| 80 | /** |
|
| 81 | * True indicates even parity in the result, false for 2s complement sign overflow. |
|
| 82 | */ |
|
| 83 | private static final int F_PARITY = 0x04; |
|
| 84 | ||
| 85 | /** |
|
| 86 | * Bit3 (usually a copy of bit 3 of the result). |
|
| 87 | */ |
|
| 88 | private static final int F_BIT3 = 0x08; |
|
| 89 | ||
| 90 | /** |
|
| 91 | * Half carry (set when a carry occured between bit 3 / 4 of result - used for BCD). |
|
| 92 | */ |
|
| 93 | private static final int F_HALFCARRY = 0x10; |
|
| 94 | ||
| 95 | /** |
|
| 96 | * Bit5 (usually a copy of bit 5 of the result). |
|
| 97 | */ |
|
| 98 | private static final int F_BIT5 = 0x20; |
|
| 99 | ||
| 100 | /** |
|
| 101 | * Zero (set when a result is zero). |
|
| 102 | */ |
|
| 103 | private static final int F_ZERO = 0x40; |
|
| 104 | ||
| 105 | /** |
|
| 106 | * Sign (set when a result is negative). |
|
| 107 | */ |
|
| 108 | private static final int F_SIGN = 0x80; |
|
| 109 | ||
| 110 | /** |
|
| 111 | * Register. |
|
| 112 | */ |
|
| 113 | private int f; |
|
| 114 | ||
| 115 | /** |
|
| 116 | * The register's second bank. |
|
| 117 | */ |
|
| 118 | private int f2; |
|
| 119 | ||
| 120 | /** |
|
| 121 | * Flag Constructor. |
|
| 122 | */ |
|
| 123 | 0 | public Flag() { |
| 124 | 0 | f = 0; |
| 125 | 0 | f2 = 0; |
| 126 | 0 | } |
| 127 | ||
| 128 | ||
| 129 | /** |
|
| 130 | * Get value of flag register. |
|
| 131 | * |
|
| 132 | * @return value stored in flag register. |
|
| 133 | */ |
|
| 134 | public int get() { |
|
| 135 | 0 | return f; |
| 136 | } |
|
| 137 | ||
| 138 | ||
| 139 | /** |
|
| 140 | * Set flag register. |
|
| 141 | * |
|
| 142 | * @param value value to set register (0 - 0xFF). |
|
| 143 | */ |
|
| 144 | public void set(int value) { |
|
| 145 | 0 | f = value; |
| 146 | 0 | } |
| 147 | ||
| 148 | ||
| 149 | /** |
|
| 150 | * Clear register. |
|
| 151 | */ |
|
| 152 | public void clear() { |
|
| 153 | 0 | f = 0; |
| 154 | 0 | f2 = 0; |
| 155 | 0 | } |
| 156 | ||
| 157 | /** |
|
| 158 | * Carry Flag On. |
|
| 159 | */ |
|
| 160 | public void carryOn() { |
|
| 161 | 0 | f |= F_CARRY; |
| 162 | 0 | } |
| 163 | ||
| 164 | /** |
|
| 165 | * Carry Flag Off. |
|
| 166 | */ |
|
| 167 | public void carryOff() { |
|
| 168 | 0 | f &= ~F_CARRY; |
| 169 | 0 | } |
| 170 | ||
| 171 | /** |
|
| 172 | * Returns <code>true</code> if carry flag (<code>F_CARRY</code>) is set. |
|
| 173 | * |
|
| 174 | * @return <code>true</code> if set. |
|
| 175 | */ |
|
| 176 | public boolean carry() { |
|
| 177 | 0 | return ((f & F_CARRY) == F_CARRY); |
| 178 | } |
|
| 179 | ||
| 180 | ||
| 181 | /** |
|
| 182 | * Negative Flag On. |
|
| 183 | */ |
|
| 184 | public void negativeOn() { |
|
| 185 | 0 | f |= F_NEGATIVE; |
| 186 | 0 | } |
| 187 | ||
| 188 | ||
| 189 | /** |
|
| 190 | * Negative Flag Off. |
|
| 191 | */ |
|
| 192 | public void negativeOff() { |
|
| 193 | 0 | f &= ~F_NEGATIVE; |
| 194 | 0 | } |
| 195 | ||
| 196 | /** |
|
| 197 | * Returns <code>true</code> if the negative flag (<code>F_NEGATIVE</code>) is set. |
|
| 198 | * |
|
| 199 | * @return <code>true</code> if set. |
|
| 200 | */ |
|
| 201 | public boolean negative() { |
|
| 202 | 0 | return ((f & F_NEGATIVE) == F_NEGATIVE); |
| 203 | } |
|
| 204 | ||
| 205 | /** |
|
| 206 | * Sets the parity flag On (<code>F_PARITY</code>). |
|
| 207 | */ |
|
| 208 | public void parityOn() { |
|
| 209 | 0 | f |= F_PARITY; |
| 210 | 0 | } |
| 211 | ||
| 212 | /** |
|
| 213 | * Sets the parity flag Off (<code>F_PARITY</code>). |
|
| 214 | */ |
|
| 215 | public void parityOff() { |
|
| 216 | 0 | f &= ~F_PARITY; |
| 217 | 0 | } |
| 218 | ||
| 219 | /** |
|
| 220 | * Set parity. |
|
| 221 | * |
|
| 222 | * @param n value to test (<code>0</code> - <code>0xFF</code>). |
|
| 223 | */ |
|
| 224 | public void setParity(int n) { |
|
| 225 | 0 | if (PARITY[n & 0xff]) { |
| 226 | 0 | f |= F_PARITY; |
| 227 | } else { |
|
| 228 | 0 | f &= ~F_PARITY; |
| 229 | } |
|
| 230 | 0 | } |
| 231 | ||
| 232 | /** |
|
| 233 | * Returns true if the parity flag (<code>F_PARITY</code>) is set. |
|
| 234 | * |
|
| 235 | * @return <code>true</code> if set. |
|
| 236 | */ |
|
| 237 | public boolean parity() { |
|
| 238 | 0 | return ((f & F_PARITY) == F_PARITY); |
| 239 | } |
|
| 240 | ||
| 241 | public void setBit3(int v) { |
|
| 242 | 0 | if ((v & F_BIT3) == F_BIT3) { |
| 243 | 0 | f |= F_BIT3; |
| 244 | } else { |
|
| 245 | 0 | f &= ~F_BIT3; |
| 246 | } |
|
| 247 | 0 | } |
| 248 | ||
| 249 | /** |
|
| 250 | * Sets the bit 3 flag On (<code>F_BIT3</code>). |
|
| 251 | */ |
|
| 252 | public void bit3On() { |
|
| 253 | 0 | f |= F_BIT3; |
| 254 | 0 | } |
| 255 | ||
| 256 | /** |
|
| 257 | * Sets the bit 3 flag Off (<code>F_BIT3</code>). |
|
| 258 | */ |
|
| 259 | public void bit3Off() { |
|
| 260 | 0 | f &= ~F_BIT3; |
| 261 | 0 | } |
| 262 | ||
| 263 | /** |
|
| 264 | * Returns <code>true</code> if the bit 3 flag (<code>F_BIT3</code>) is set and |
|
| 265 | * <code>false</code> otherwise. |
|
| 266 | * |
|
| 267 | * @return <code>true</code> if set. |
|
| 268 | */ |
|
| 269 | public boolean bit3() { |
|
| 270 | 0 | return ((f & F_BIT3) == F_BIT3); |
| 271 | } |
|
| 272 | ||
| 273 | /** |
|
| 274 | * Sets the half carry flag On (<code>F_HALFCARRY</code>). |
|
| 275 | */ |
|
| 276 | public void halfCarryOn() { |
|
| 277 | 0 | f |= F_HALFCARRY; |
| 278 | 0 | } |
| 279 | ||
| 280 | /** |
|
| 281 | * Sets the half carry flag Off (<code>F_HALFCARRY</code>). |
|
| 282 | */ |
|
| 283 | public void halfCarryOff() { |
|
| 284 | 0 | f &= ~F_HALFCARRY; |
| 285 | 0 | } |
| 286 | ||
| 287 | /** |
|
| 288 | * Returns <code>true</code> if the half carry flag (<code>F_HALFCARRY</code>) is set, and |
|
| 289 | * <code>false</code> otherwise. |
|
| 290 | * |
|
| 291 | * @return <code>true</code> if set. |
|
| 292 | */ |
|
| 293 | public boolean halfcarry() { |
|
| 294 | 0 | return ((f & F_HALFCARRY) == F_HALFCARRY); |
| 295 | } |
|
| 296 | ||
| 297 | public void setBit5(int v) { |
|
| 298 | 0 | if ((v & F_BIT5) == F_BIT5) { |
| 299 | 0 | f |= F_BIT5; |
| 300 | } else { |
|
| 301 | 0 | f &= ~F_BIT5; |
| 302 | } |
|
| 303 | 0 | } |
| 304 | ||
| 305 | /** |
|
| 306 | * Sets the bit 5 flag On (<code>F_BIT5</code>). |
|
| 307 | */ |
|
| 308 | public void bit5On() { |
|
| 309 | 0 | f |= F_BIT5; |
| 310 | 0 | } |
| 311 | ||
| 312 | /** |
|
| 313 | * Sets the bit 5 flag Off (<code>F_BIT5</code>). |
|
| 314 | */ |
|
| 315 | public void bit5Off() { |
|
| 316 | 0 | f &= ~F_BIT5; |
| 317 | 0 | } |
| 318 | ||
| 319 | /** |
|
| 320 | * Returns <code>true</code> if the bit 5 flag (<code>F_BIT5</code>) is set, and |
|
| 321 | * <code>false</code> otherwise. |
|
| 322 | * |
|
| 323 | * @return <code>true</code> if set. |
|
| 324 | */ |
|
| 325 | public boolean bit5() { |
|
| 326 | 0 | return ((f & F_BIT5) == F_BIT5); |
| 327 | } |
|
| 328 | ||
| 329 | /** |
|
| 330 | * Sets the zero flag On (<code>F_ZERO</code>). |
|
| 331 | */ |
|
| 332 | public void zeroOn() { |
|
| 333 | 0 | f |= F_ZERO; |
| 334 | 0 | } |
| 335 | ||
| 336 | /** |
|
| 337 | * Sets the zero flag Off (<code>F_ZERO</code>). |
|
| 338 | */ |
|
| 339 | public void zeroOff() { |
|
| 340 | 0 | f &= ~F_ZERO; |
| 341 | 0 | } |
| 342 | ||
| 343 | /** |
|
| 344 | * Returns <code>true</code> if the zero flag (<code>F_ZERO</code>) is set, and |
|
| 345 | * <code>false</code> otherwise. |
|
| 346 | * |
|
| 347 | * @return <code>true</code> if set. |
|
| 348 | */ |
|
| 349 | public boolean zero() { |
|
| 350 | 0 | return ((f & F_ZERO) == F_ZERO); |
| 351 | } |
|
| 352 | ||
| 353 | /** |
|
| 354 | * Sets the sign flag On (<code>F_SIGN</code>). |
|
| 355 | */ |
|
| 356 | public void signOn() { |
|
| 357 | 0 | f |= F_SIGN; |
| 358 | 0 | } |
| 359 | ||
| 360 | /** |
|
| 361 | * Sets the sign flag Off (<code>F_SIGN</code>). |
|
| 362 | */ |
|
| 363 | public void signOff() { |
|
| 364 | 0 | f &= ~F_SIGN; |
| 365 | 0 | } |
| 366 | ||
| 367 | /** |
|
| 368 | * Returns <code>true</code> if the sign flag (<code>F_SIGN</code>) is set, and |
|
| 369 | * <code>false</code> otherwise. |
|
| 370 | * |
|
| 371 | * @return <code>true</code> if set. |
|
| 372 | */ |
|
| 373 | public boolean sign() { |
|
| 374 | 0 | return ((f & F_SIGN) == F_SIGN); |
| 375 | } |
|
| 376 | ||
| 377 | /** |
|
| 378 | * Exchange register banks. |
|
| 379 | */ |
|
| 380 | public void ex() { |
|
| 381 | int temp; |
|
| 382 | 0 | temp = f; |
| 383 | 0 | f = f2; |
| 384 | 0 | f2 = temp; |
| 385 | 0 | } |
| 386 | ||
| 387 | } |