Z80 DAA instruction

后端 未结 4 1150
无人共我
无人共我 2021-02-12 16:24

Apologies for this seemingly minor question, but I can\'t seem to find the answer anywhere - I\'m just coming up to implementing the DAA instruction in my Z80 emulator, and I no

4条回答
  •  我寻月下人不归
    2021-02-12 17:20

    This is code in production, implementing DAA correctly and passes the zexall/zexdoc/z80test Z80 opcode test suits.

    Based on The Undocumented Z80 Documented, pag 17-18.

    void daa()
    {
       int t;
        
       t=0;
        
       // 4 T states
       T(4);
        
       if(flags.H || ((A & 0xF) > 9) )
             t++;
        
       if(flags.C || (A > 0x99) )
       {
             t += 2;
             flags.C = 1;
       }
        
       // builds final H flag
       if (flags.N && !flags.H)
          flags.H=0;
       else
       {
           if (flags.N && flags.H)
              flags.H = (((A & 0x0F)) < 6);
           else
              flags.H = ((A & 0x0F) >= 0x0A);
       }
        
       switch(t)
       {
            case 1:
                A += (flags.N)?0xFA:0x06; // -6:6
                break;
            case 2:
                A += (flags.N)?0xA0:0x60; // -0x60:0x60
                break;
            case 3:
                A += (flags.N)?0x9A:0x66; // -0x66:0x66
                break;
       }
        
       flags.S = (A & BIT_7);
       flags.Z = !A;
       flags.P = parity(A);
       flags.X = A & BIT_5;
       flags.Y = A & BIT_3;
    }
    

    For visualising the DAA interactions, for debugging purposes, I have written a small Z80 assembly program, that can be run in an actual ZX Spectrum or in an emulation that emulates accurately DAA: https://github.com/ruyrybeyro/daatable

    As how it behaves, got a table of flags N,C,H and register A before and after DAA produced with the aforementioned assembly program: https://github.com/ruyrybeyro/daatable/blob/master/daaoutput.txt

提交回复
热议问题