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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// trick to not be distirb by Register8/16
use crate::preamble::*;


use num::integer::Integer;
use num::traits::{WrappingAdd, WrappingSub};
use num::One;
use std::fmt;
use std::fmt::Debug;
///! Manage z80 CPU
///! Could be used to simulate or generate code
use std::mem::swap;

/// Common trait for Register 8 and 6 bits
#[allow(missing_docs)]
pub trait HasValue {
    /// The type that encodes the value of interest
    type ValueType: Integer + One + WrappingAdd + WrappingSub;

    /// Retreive the stored value
    fn value(&self) -> Self::ValueType;

    #[inline]
    fn get(&self) -> Self::ValueType {
        self.value()
    }
    /// Change the stored value
    fn set(&mut self, value: Self::ValueType);

    fn add(&mut self, value: Self::ValueType) {
        self.set(self.value().wrapping_add(&value));
    }

    fn sub(&mut self, value: Self::ValueType) {
        self.set(self.value().wrapping_sub(&value));
    }

    fn inc(&mut self) {
        self.add(Self::ValueType::one());
    }

    fn dec(&mut self) {
        self.sub(Self::ValueType::one());
    }
}

/// Represents an 8 bit register
#[derive(Copy, Clone, Debug)]
pub struct Register8 {
    val: u8,
}

/// By default a Register8 is set to 0
/// TODO use an Unknown value ?
impl Default for Register8 {
    fn default() -> Self {
        Self { val: 0 }
    }
}

// TODO use macro for that
impl HasValue for Register8 {
    type ValueType = u8;

    fn value(&self) -> Self::ValueType {
        self.val
    }

    fn set(&mut self, value: Self::ValueType) {
        self.val = value;
    }
}

impl Register8 {
    pub fn res_bit(&mut self, bit: u8) {
        let new = self.value() & (!(1 << bit));
        self.set(new);
    }

    pub fn set_bit(&mut self, bit: u8) {
        let new = self.value() | (1 << bit);
        self.set(new);
    }

    pub fn and(&mut self, value: u8) {
        let new = self.value() & value;
        self.set(new);
    }

    pub fn or(&mut self, value: u8) {
        let new = self.value() | value;
        self.set(new);
    }

    pub fn xor(&mut self, value: u8) {
        let new = self.value() ^ value;
        self.set(new);
    }
}

/// Represents a 16 bits register by decomposing it in 2 8 bits registers
#[derive(Copy, Clone, Default)]
pub struct Register16 {
    low: Register8,
    high: Register8,
}

impl Debug for Register16 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({:?}, {:?})", &self.high, &self.low)
    }
}

impl HasValue for Register16 {
    type ValueType = u16;

    fn value(&self) -> Self::ValueType {
        256 * u16::from(self.high().value()) + u16::from(self.low().value())
    }

    fn set(&mut self, value: Self::ValueType) {
        self.low_mut().set((value % 256) as _);
        self.high_mut().set((value / 256) as _);
    }
}

impl Register16 {
    fn low(&self) -> &Register8 {
        &self.low
    }

    fn high(&self) -> &Register8 {
        &self.high
    }

    fn low_mut(&mut self) -> &mut Register8 {
        &mut self.low
    }

    fn high_mut(&mut self) -> &mut Register8 {
        &mut self.high
    }
}

/// Contains all needed stuff to make the emulation that does not belong to the CPU
/// This enable the execution of tokens that contains symbolic values
#[derive(Default, Debug, Clone)]
pub struct EmulationContext {
    /// The symbol table that can be used when ev
    pub(crate) symbols: tokens::SymbolsTableCaseDependent,
}

/// Highly simplify z80 model.
/// TODO Add memory
#[derive(Default, Debug, Clone)]
pub struct Z80 {
    reg_pc: Register16,
    reg_sp: Register16,

    reg_af: Register16,

    reg_bc: Register16,
    reg_de: Register16,
    reg_hl: Register16,

    reg_ix: Register16,
    reg_iy: Register16,

    reg_i: Register8,
    reg_r: Register8,

    reg_af_prime: Register16,

    reg_bc_prime: Register16,
    reg_de_prime: Register16,
    reg_hl_prime: Register16,

    pub(crate) context: EmulationContext,
}

#[allow(missing_docs)]
impl Z80 {
    // Immutable accessors
    pub fn pc(&self) -> &Register16 {
        &self.reg_pc
    }
    pub fn sp(&self) -> &Register16 {
        &self.reg_sp
    }

    pub fn af(&self) -> &Register16 {
        &self.reg_af
    }

    pub fn bc(&self) -> &Register16 {
        &self.reg_bc
    }
    pub fn de(&self) -> &Register16 {
        &self.reg_de
    }
    pub fn hl(&self) -> &Register16 {
        &self.reg_hl
    }

    pub fn ix(&self) -> &Register16 {
        &self.reg_ix
    }
    pub fn iy(&self) -> &Register16 {
        &self.reg_iy
    }

    pub fn a(&self) -> &Register8 {
        let tmp = self.af();
        tmp.high()
    }
    pub fn f(&self) -> &Register8 {
        let tmp = self.af();
        tmp.low()
    }

    pub fn b(&self) -> &Register8 {
        let tmp = self.bc();
        tmp.high()
    }
    pub fn c(&self) -> &Register8 {
        let tmp = self.bc();
        tmp.low()
    }

    pub fn d(&self) -> &Register8 {
        let tmp = self.de();
        tmp.high()
    }
    pub fn e(&self) -> &Register8 {
        let tmp = self.de();
        tmp.low()
    }

    pub fn h(&self) -> &Register8 {
        let tmp = self.hl();
        tmp.high()
    }
    pub fn l(&self) -> &Register8 {
        let tmp = self.hl();
        tmp.low()
    }

    pub fn ixh(&self) -> &Register8 {
        let tmp = self.ix();
        tmp.high()
    }
    pub fn ixl(&self) -> &Register8 {
        let tmp = self.ix();
        tmp.low()
    }

    pub fn iyh(&self) -> &Register8 {
        let tmp = self.iy();
        tmp.high()
    }
    pub fn iyl(&self) -> &Register8 {
        let tmp = self.iy();
        tmp.low()
    }

    // Mutable accessors
    pub fn pc_mut(&mut self) -> &mut Register16 {
        &mut self.reg_pc
    }
    pub fn sp_mut(&mut self) -> &mut Register16 {
        &mut self.reg_sp
    }

    pub fn af_mut(&mut self) -> &mut Register16 {
        &mut self.reg_af
    }

    pub fn bc_mut(&mut self) -> &mut Register16 {
        &mut self.reg_bc
    }
    pub fn de_mut(&mut self) -> &mut Register16 {
        &mut self.reg_de
    }
    pub fn hl_mut(&mut self) -> &mut Register16 {
        &mut self.reg_hl
    }

    pub fn ix_mut(&mut self) -> &mut Register16 {
        &mut self.reg_ix
    }
    pub fn iy_mut(&mut self) -> &mut Register16 {
        &mut self.reg_iy
    }

    pub fn a_mut(&mut self) -> &mut Register8 {
        let tmp = self.af_mut();
        tmp.high_mut()
    }
    pub fn f_mut(&mut self) -> &mut Register8 {
        let tmp = self.af_mut();
        tmp.low_mut()
    }

    pub fn b_mut(&mut self) -> &mut Register8 {
        let tmp = self.bc_mut();
        tmp.high_mut()
    }
    pub fn c_mut(&mut self) -> &mut Register8 {
        let tmp = self.bc_mut();
        tmp.low_mut()
    }

    pub fn d_mut(&mut self) -> &mut Register8 {
        let tmp = self.de_mut();
        tmp.high_mut()
    }
    pub fn e_mut(&mut self) -> &mut Register8 {
        let tmp = self.de_mut();
        tmp.low_mut()
    }

    pub fn h_mut(&mut self) -> &mut Register8 {
        let tmp = self.hl_mut();
        tmp.high_mut()
    }
    pub fn l_mut(&mut self) -> &mut Register8 {
        let tmp = self.hl_mut();
        tmp.low_mut()
    }

    pub fn ixh_mut(&mut self) -> &mut Register8 {
        let tmp = self.ix_mut();
        tmp.high_mut()
    }
    pub fn ixl_mut(&mut self) -> &mut Register8 {
        let tmp = self.ix_mut();
        tmp.low_mut()
    }

    pub fn iyh_mut(&mut self) -> &mut Register8 {
        let tmp = self.iy_mut();
        tmp.high_mut()
    }
    pub fn iyl_mut(&mut self) -> &mut Register8 {
        let tmp = self.iy_mut();
        tmp.low_mut()
    }

    pub fn ex_af_af_prime(&mut self) {
        swap(&mut self.reg_af_prime, &mut self.reg_af);
    }

    pub fn exx(&mut self) {
        swap(&mut self.reg_hl_prime, &mut self.reg_hl);
        swap(&mut self.reg_de_prime, &mut self.reg_de);
        swap(&mut self.reg_bc_prime, &mut self.reg_bc);
    }

    pub fn ex_de_hl(&mut self) {
        swap(&mut self.reg_hl, &mut self.reg_de);
    }
    // To reduce copy paste/implementation errors, all manipulation are translated as token usage
    pub fn copy_to_from(
        &mut self,
        to: tokens::Register8,
        from: tokens::Register8,
    ) {
        self.execute(&Token::OpCode(
            Mnemonic::Ld,
            Some(DataAccess::Register8(to)),
            Some(DataAccess::Register8(from)),
        ));
    }
}

// https://www.msx.org/wiki/Assembler_for_Dummies_%28Z80%29#Flags
#[allow(unused)]
pub enum FlagPos {
    // bit 7, SF, Sign flag. This is copy of the results most significant bit. If the bit is set (= 1 = "M") "Minus" the 2-complement value is negative other ways the result is positive (= 0 = "P") "Plus". Note that this flag can be used only with conditional JP-instruction.
    Sign = 7,

    // bit 6, ZF, Zero flag. If the result of mathematical operation is zero the bit is set (= 1 = "Z") other ways the bit is reset (= 0 = "NZ") "Not zero"
    Zero = 6,

    // Bit 5, YF, copy of the results 5th bit.
    Y = 5,

    // Bit 4, HF, "Half-carry" from bit 3 to 4. Z80 uses this internally for BCD correction.
    HalfCarry = 4,

    // Bit 3, XF, copy of the results 3rd bit.
    X = 3,

    // Bit 2, PF/VF, Parity flag. This is copy of the results least significant bit. If the bit is set (= 1 = "PO") the parity is odd otherways the result is even. (= 0 = "PE") On some cases this bit might be used to indicate 2-compliment signed overflow.(VF) Note that this flag can be used only with conditional JP-instruction.
    Parity = 2,

    // Bit 1, NF, this bit is used by Z80 internally to indicate if last operation was addition or subtraction (needed for BCD correction)
    N = 1,

    // Bit 0, CF, Carry flag = Overflow bit. This bit is the most high bit that did not fit to the result. In addition to large calculations and bit transfers it is mostly used in comparisons to see if the subtraction result was smaller or greater than zero. If the carry flag was set (= 1 = "C") the result did overflow. Other ways the flag is reset (= 0 = "NC") "No carry". Please note that 8bit INC/DEC commands do not update this flag.
    Carry = 0,
}

#[allow(unused)]
struct ExtraFlags {
    iff1: u8,
    iff2: u8,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn build_z80() {
        let _z80 = Z80::default();
    }

    #[test]
    fn test_register8() {
        let mut b = Register8::default();

        assert_eq!(b.value(), 0);

        b.set(22);
        assert_eq!(b.value(), 22);
    }

    #[test]
    fn test_register16() {
        let mut bc = Register16::default();

        assert_eq!(bc.value(), 0);

        bc.set(22);
        assert_eq!(bc.low().value(), 22);
        assert_eq!(bc.high().value(), 0);
        assert_eq!(bc.value(), 22);

        bc.set(50 * 256);
        assert_eq!(bc.low().value(), 0);
        assert_eq!(bc.high().value(), 50);
        assert_eq!(bc.value(), 50 * 256);

        bc.set(0xffff);
        bc.add(1);
        assert_eq!(bc.value(), 0);

        bc.set(0x4000);
        bc.add(1);
        assert_eq!(bc.value(), 0x4001);
    }

    #[test]
    fn z80_registers() {
        let mut z80 = Z80::default();

        z80.bc_mut().set(0x1234);
        z80.af_mut().set(0x4567);

        assert_eq!(z80.b().value(), 0x12);
        assert_eq!(z80.c().value(), 0x34);

        assert_eq!(z80.a().value(), 0x45);
        assert_eq!(z80.f().value(), 0x67);

        z80.ex_af_af_prime();
        assert_eq!(z80.a().value(), 0x00);
        assert_eq!(z80.b().value(), 0x12);
        assert_eq!(z80.c().value(), 0x34);

        z80.ex_af_af_prime();
        assert_eq!(z80.a().value(), 0x45);

        z80.a_mut().set(0);
        assert_eq!(0, z80.a().value());
        z80.a_mut().add(1);
        assert_eq!(1, z80.a().value());

        z80.a_mut().set(0);
        assert_eq!(0, z80.a().value());
        z80.a_mut().inc();
        assert_eq!(1, z80.a().value());
        z80.a_mut().dec();
        assert_eq!(0, z80.a().value());
        z80.a_mut().dec();
        assert_eq!(0xff, z80.a().value());
        z80.a_mut().inc();
        assert_eq!(0, z80.a().value());
    }

    #[test]
    fn eval() {

        let mut z80 = Z80::default();
        z80.pc_mut().set(0x4000);
        z80.hl_mut().set(0x8000);
        z80.de_mut().set(0xc000);
        z80.a_mut().set(0);

        let pop_bc = Token::OpCode(
            Mnemonic::Pop,
            Some(DataAccess::Register16(tokens::Register16::Bc)),
            None,
        );
        let ld_l_a = Token::OpCode(
            Mnemonic::Ld,
            Some(DataAccess::Register8(tokens::Register8::L)),
            Some(DataAccess::Register8(tokens::Register8::A)),
        );
        let add_a_b = Token::OpCode(
            Mnemonic::Add,
            Some(DataAccess::Register8(tokens::Register8::A)),
            Some(DataAccess::Register8(tokens::Register8::B)),
        );
        let ldi = Token::OpCode(Mnemonic::Ldi, None, None);

        assert_eq!(z80.pc().value(), 0x4000);

        z80.execute(&pop_bc);
        assert_eq!(z80.pc().value(), 0x4001);

        z80.execute(&add_a_b);
        assert_eq!(z80.pc().value(), 0x4002);

        z80.execute(&ld_l_a);
        assert_eq!(z80.pc().value(), 0x4003);
        assert_eq!(z80.a().value(), z80.l().value());

        z80.execute(&ldi);
        assert_eq!(z80.pc().value(), 0x4005);
        assert_eq!(z80.de().value(), 0xc001);
    }
}