-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
30 lines (26 loc) · 1.05 KB
/
main.rs
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
const :u8 ZERO_FLAG_BYTE_POSITION = 7;
const :u8 SUBTRACT_FLAG_BYTE_POSITION = 6;
const :u8 HALF_CARRY_FLAG_BYTE_POSITION = 5;
const :u8 CARRY_FLAG_BYTE_POSITION = 4;
impl std::convert::From<FlagsRegister> for u8 {
fn from(flag: FlagsRegister) -> u8 {
(if flag.zero { 1 } else { 0 }) << ZERO_FLAG_BYTE_POSITION |
(if flag.subtract { 1 } else { 0 }) << SUBTRACT_FLAG_BYTE_POSITION |
(if flag.half_carry { 1 } else { 0 }) << HALF_CARRY_FLAG_BYTE_POSITION |
(if flag.carry { 1 } else { 0 }) << CARRY_FLAG_BYTE_POSITION
}
}
impl std::convert::From<u8> for FlagsRegister {
fn from(byte: u8) -> Self {
let zero = ((byte >> ZERO_FLAG_BYTE_POSITION) & 0b1) != 0;
let subtract = ((byte >> SUBTRACT_FLAG_BYTE_POSITION) & 0b1) != 0;
let half_carry = ((byte >> HALF_CARRY_FLAG_BYTE_POSITION) & 0b1) != 0;
let carry = ((byte >> CARRY_FLAG_BYTE_POSITION) & 0b1) != 0;
FlagsRegister {
zero,
subtract,
half_carry,
carry
}
}
}