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
// TODO later: look into bitflags! macro

#[cfg(test)]
#[path = "./flag_test.rs"]
mod flag_test;

/// https://en.wikipedia.org/wiki/FLAGS_register
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct Flags {
    // ____ O___ SZ_A _P_C
    pub carry: bool, // 0: carry flag
    reserved1: bool, // 1: reserved, always 1 in EFLAGS
    pub parity: bool, // 2: parity flag
    reserved3: bool,
    pub adjust: bool, // 4: adjust flag
    reserved5: bool,
    pub zero: bool, // 6: zero flag
    pub sign: bool, // 7: sign flag
    pub trap: bool, // 8: trap flag (single step)
    pub interrupt: bool, // 9: interrupt flag
    pub direction: bool, // 10: direction flag (control with cld, std)
    pub overflow: bool, // 11: overflow flag
    iopl12: bool, // 12: I/O privilege level (286+ only), always 1 on 8086 and 186
    iopl13: bool, // 13 --""---
    nested_task: bool, // 14: Nested task flag (286+ only), always 1 on 8086 and 186
    reserved15: bool, // 15: Reserved, always 1 on 8086 and 186, always 0 on later models
}

// XXX make use of flag mask
pub const FLAG_CF: u16 = 0x0000_0001;
pub const FLAG_PF: u16 = 0x0000_0004;
pub const FLAG_AF: u16 = 0x0000_0010;
pub const FLAG_ZF: u16 = 0x0000_0040;
pub const FLAG_SF: u16 = 0x0000_0080;
pub const FLAG_TF: u16 = 0x0000_0100;
pub const FLAG_IF: u16 = 0x0000_0200;
pub const FLAG_DF: u16 = 0x0000_0400;
pub const FLAG_OF: u16 = 0x0000_0800;

static PARITY_LOOKUP: [u16; 256] = [
    FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF,
    0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0,
    0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0,
    FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF,
    0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0,
    FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF,
    FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF,
    0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0,
    0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0,
    FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF,
    FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF,
    0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0,
    FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF,
    0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0,
    0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0,
    FLAG_PF, 0, 0, FLAG_PF, 0, FLAG_PF, FLAG_PF, 0, 0, FLAG_PF, FLAG_PF, 0, FLAG_PF, 0, 0, FLAG_PF
];

impl Flags {
    pub fn new() -> Self {
        Flags {
            carry: false, // bit 0
            reserved1: false,
            parity: false,
            reserved3: false,
            adjust: false,
            reserved5: false,
            zero: false,
            sign: false, // bit 7
            trap: false,
            interrupt: false,
            direction: false,
            overflow: false,
            iopl12: false,
            iopl13: false,
            nested_task: false,
            reserved15: false, // bit 15
        }
    }

    pub fn new_from_u16(val: u16) -> Flags {
        let mut f = Flags::new();
        f.set_u16(val);
        f
    }

    /// sets sign, zero, parity flags according to `b`
    pub fn set_szp(&mut self, b: bool) {
        self.sign = b;
        self.zero = b;
        self.parity = b;
    }

    pub fn set_sign_u8(&mut self, v: usize) {
        // Set equal to the most-significant bit of the result,
        // which is the sign bit of a signed integer.
        // (0 indicates a positive value and 1 indicates a negative value.)
        self.sign = v & 0x80 != 0;
    }
    pub fn set_sign_u16(&mut self, v: usize) {
        self.sign = v & 0x8000 != 0;
    }
    pub fn set_sign_u32(&mut self, v: usize) {
        self.sign = v & 0x8000_0000 != 0;
    }
    pub fn set_parity(&mut self, v: usize) {
        // Set if the least-significant byte of the result contains an
        // even number of 1 bits; cleared otherwise.

        // TODO later: rework flag register to be a u16 directly, use FLAG_PF
        self.parity = PARITY_LOOKUP[v & 0xFF] != 0
    }
    pub fn set_zero_u8(&mut self, v: usize) {
        // Zero flag — Set if the result is zero; cleared otherwise.
        self.zero = v.trailing_zeros() >= 8;
    }
    pub fn set_zero_u16(&mut self, v: usize) {
        self.zero = v.trailing_zeros() >= 16;
    }
    pub fn set_zero_u32(&mut self, v: usize) {
        self.zero = v.trailing_zeros() >= 32;
    }
    pub fn set_adjust(&mut self, res: usize, v1: usize, v2: usize) {
        // Set if an arithmetic operation generates a carry or a borrow out
        // of bit 3 of the result; cleared otherwise. This flag is used in
        // binary-coded decimal (BCD) arithmetic.
        self.adjust = (res ^ (v1 ^ v2)) & 0x10 != 0;
    }
    pub fn set_overflow_add_u8(&mut self, res: usize, v1: usize, v2: usize) {
        // Set if the integer result is too large a positive number or too
        // small a negative number (excluding the sign-bit) to fit in the
        // destination operand; cleared otherwise. This flag indicates an
        // overflow condition for signed-integer (two’s complement) arithmetic.
        self.overflow = (res ^ v1) & (res ^ v2) & 0x80 != 0;
    }
    pub fn set_overflow_add_u16(&mut self, res: usize, v1: usize, v2: usize) {
        self.overflow = (res ^ v1) & (res ^ v2) & 0x8000 != 0;
    }
    pub fn set_overflow_add_u32(&mut self, res: usize, v1: usize, v2: usize) {
        self.overflow = (res ^ v1) & (res ^ v2) & 0x8000_0000 != 0;
    }
    pub fn set_overflow_sub_u8(&mut self, res: usize, v1: usize, v2: usize) {
        self.overflow = (v2 ^ v1) & (v2 ^ res) & 0x80 != 0;
    }
    pub fn set_overflow_sub_u16(&mut self, res: usize, v1: usize, v2: usize) {
        self.overflow = (v2 ^ v1) & (v2 ^ res) & 0x8000 != 0;
    }
    pub fn set_overflow_sub_u32(&mut self, res: usize, v1: usize, v2: usize) {
        self.overflow = (v2 ^ v1) & (v2 ^ res) & 0x8000_0000 != 0;
    }
    pub fn set_carry_u8(&mut self, res: usize) {
        // Set if an arithmetic operation generates a carry or a borrow out of
        // the most-significant bit of the result; cleared otherwise. This flag
        // indicates an overflow condition for unsigned-integer arithmetic.
        self.carry = res & 0x100 != 0;
    }
    pub fn set_carry_u16(&mut self, res: usize) {
        self.carry = res & 0x1_0000 != 0;
    }
    pub fn set_carry_u32(&mut self, res: usize) {
        self.carry = res & 0x1_0000_0000 != 0;
    }

    /// initializes the flags with a packed u16
    pub fn set_u16(&mut self, val: u16) {
        self.carry       = val & 0x1 != 0;
        //self.reserved1   = val & 0x2 != 0;
        self.parity      = val & 0x4 != 0;
        self.adjust      = val & 0x10 != 0;
        self.zero        = val & 0x40 != 0;
        self.sign        = val & 0x80 != 0;
        self.trap        = val & 0x100 != 0;
        //self.interrupt   = val & 0x200 != 0;
        self.direction   = val & 0x400 != 0;
        self.overflow    = val & 0x800 != 0;
        //self.iopl12      = val & 0x1000 != 0;
        //self.iopl13      = val & 0x2000 != 0;
        //self.nested_task = val & 0x4000 != 0;
    }

    pub fn carry_val(&self) -> usize {
        if self.carry {
            1
        } else {
            0
        }
    }
    pub fn carry_numeric(&self) -> String {
        format!("{}", if self.carry {
            1
        } else {
            0
        })
    }
    pub fn zero_numeric(&self) -> String {
        format!("{}", if self.zero {
            1
        } else {
            0
        })
    }
    pub fn sign_numeric(&self) -> String {
        format!("{}", if self.sign { 1 } else { 0 })
    }
    pub fn overflow_numeric(&self) -> String {
        format!("{}", if self.overflow {
            1
        } else {
            0
        })
    }
    pub fn adjust_numeric(&self) -> String {
        format!("{}", if self.adjust {
            1
        } else {
            0
        })
    }
    pub fn parity_numeric(&self) -> String {
        format!("{}", if self.parity {
            1
        } else {
            0
        })
    }
    pub fn direction_numeric(&self) -> String {
        format!("{}", if self.direction {
            1
        } else {
            0
        })
    }
    pub fn interrupt_numeric(&self) -> String {
        format!("{}", if self.interrupt {
            1
        } else {
            0
        })
    }

    /// returns the FLAGS register
    pub fn u16(&self) -> u16 {
        let mut val = 0 as u16;
        if self.carry {
            val |= 1;
        }
        if self.reserved1 {
            val |= 1 << 1;
        }
        if self.parity {
            val |= 1 << 2;
        }
        if self.adjust {
            val |= 1 << 4;
        }
        if self.zero {
            val |= 1 << 6;
        }
        if self.sign {
            val |= 1 << 7;
        }
        if self.trap {
            val |= 1 << 8;
        }
        if self.interrupt {
            val |= 1 << 9;
        }
        if self.direction {
            val |= 1 << 10;
        }
        if self.overflow {
            val |= 1 << 11;
        }
        if self.iopl12 {
            val |= 1 << 12;
        }
        if self.iopl13 {
            val |= 1 << 13;
        }
        if self.nested_task {
            val |= 1 << 14;
        }
        val
    }
}