summaryrefslogtreecommitdiff
path: root/Noddybox.Emulation.EightBit.Z80/Z80Cpu.cs
blob: d967bb9072627a5e08fbe405618fd3e1b1a77d7b (plain)
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
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Noddybox.Emulation.EightBit.Z80
{
    /// <summary>
    /// Provides an implementation of a Zilog Z80 processor.
    /// </summary>
    public partial class Z80Cpu : ICpu
    {
        #region Private types

        [Flags]
        private enum Z80Flags
        {
            None        = 0x00,
            Carry       = 0x01,
            Neg		    = 0x02,
            PV		    = 0x04,
            Hidden3	    = 0x08,
            HalfCarry   = 0x10,
            Hidden5     = 0x20,
            Zero		= 0x40,
            Sign		= 0x80            
        };

        #endregion

        #region Private data

        // Lookup tables
        //
        private Z80Flags[] PSZtable = new Z80Flags[512];
        private Z80Flags[] SZtable = new Z80Flags[512];
        private Z80Flags[] Ptable = new Z80Flags[512];
        private Z80Flags[] Stable = new Z80Flags[512];
        private Z80Flags[] Ztable = new Z80Flags[512];

        // Machine accessors
        //
        private IMemory memory;
        private IDevice device;
        private Clock   clock;

        // Main registers
        //
        private byte        A;
        private byte        F;
        private byte        R;
        private byte        IFF1;
        private byte        IFF2;
        private IRegister16 BC = Register16Factory.Create();
        private IRegister16 DE = Register16Factory.Create();
        private IRegister16 HL = Register16Factory.Create();
        private IRegister16 IX = Register16Factory.Create();
        private IRegister16 IY = Register16Factory.Create();
        private IRegister16 SP = Register16Factory.Create();
        private IRegister16 PC = Register16Factory.Create();

        // Alternate registers
        //
        private IRegister16 AF_ = Register16Factory.Create();
        private IRegister16 BC_ = Register16Factory.Create();
        private IRegister16 DE_ = Register16Factory.Create();
        private IRegister16 HL_ = Register16Factory.Create();

        #endregion

        #region ICpu Members

        public void Initialise(IMemory memory, IDevice device, Clock clock)
        {
            this.memory = memory;
            this.device = device;
            this.clock = clock;

            Reset();
        }

        public void Reset()
        {
            // TODO: Initial register settings.
        }

        public void Step()
        {
            // TODO: Single step.
        }

        public void Run()
        {
            // TODO: Run for a frame
        }

        public void MaskableInterrupt(byte value)
        {
            // TODO: INT
        }

        public void NonMaskableInterrupt(byte value)
        {
            // TODO: NMI
        }

        #endregion

        #region Constructors

        public Z80Cpu()
        {
            // Setup lookup tables
            //
            for(int f = 0; f < 256; f++)
            {
                Z80Flags p = Z80Flags.None;
                Z80Flags z = Z80Flags.None;
                Z80Flags s = Z80Flags.None;
                int parity = 0;

                for(int b=0; b < 8; b++)
                {
                    if ((f & (1<<b)) == (1<<b))
                    {
                        parity++;
                    }
                }

                if ((parity & 1) == 1)
                {
                    p = Z80Flags.PV;
                }

                if (f == 0)
                {
                    z = Z80Flags.Zero;
                }

                if ((f & 0x80) == 0x80)
                {
                    s = Z80Flags.Sign;
                }

                Ptable[f] = p;
                Stable[f] = s;
                Ztable[f] = z;
                SZtable[f] =z|s;
                PSZtable[f] = z|s|p;

                Ptable[f+256] = Ptable[f]|Z80Flags.Carry;
                Stable[f+256] = Stable[f]|Z80Flags.Carry;
                Ztable[f+256] = Ztable[f]|Z80Flags.Carry;
                SZtable[f+256] = SZtable[f]|Z80Flags.Carry;
                PSZtable[f+256] = PSZtable[f]|Z80Flags.Carry;
            }
        }

        #endregion
    }
}