aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics/Graphics3d/MacroInterpreter.cs
blob: 845762133c1fe9f9504da821e06c4868290d4349 (plain) (blame)
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
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.Memory;
using System;
using System.Collections.Generic;

namespace Ryujinx.Graphics.Graphics3d
{
    class MacroInterpreter
    {
        private enum AssignmentOperation
        {
            IgnoreAndFetch                  = 0,
            Move                            = 1,
            MoveAndSetMaddr                 = 2,
            FetchAndSend                    = 3,
            MoveAndSend                     = 4,
            FetchAndSetMaddr                = 5,
            MoveAndSetMaddrThenFetchAndSend = 6,
            MoveAndSetMaddrThenSendHigh     = 7
        }

        private enum AluOperation
        {
            AluReg                = 0,
            AddImmediate          = 1,
            BitfieldReplace       = 2,
            BitfieldExtractLslImm = 3,
            BitfieldExtractLslReg = 4,
            ReadImmediate         = 5
        }

        private enum AluRegOperation
        {
            Add                = 0,
            AddWithCarry       = 1,
            Subtract           = 2,
            SubtractWithBorrow = 3,
            BitwiseExclusiveOr = 8,
            BitwiseOr          = 9,
            BitwiseAnd         = 10,
            BitwiseAndNot      = 11,
            BitwiseNotAnd      = 12
        }

        private NvGpuFifo    _pFifo;
        private INvGpuEngine _engine;

        public Queue<int> Fifo { get; private set; }

        private int[] _gprs;

        private int _methAddr;
        private int _methIncr;

        private bool _carry;

        private int _opCode;

        private int _pipeOp;

        private int _pc;

        public MacroInterpreter(NvGpuFifo pFifo, INvGpuEngine engine)
        {
            _pFifo  = pFifo;
            _engine = engine;

            Fifo = new Queue<int>();

            _gprs = new int[8];
        }

        public void Execute(NvGpuVmm vmm, int[] mme, int position, int param)
        {
            Reset();

            _gprs[1] = param;

            _pc = position;

            FetchOpCode(mme);

            while (Step(vmm, mme));

            //Due to the delay slot, we still need to execute
            //one more instruction before we actually exit.
            Step(vmm, mme);
        }

        private void Reset()
        {
            for (int index = 0; index < _gprs.Length; index++)
            {
                _gprs[index] = 0;
            }

            _methAddr = 0;
            _methIncr = 0;

            _carry = false;
        }

        private bool Step(NvGpuVmm vmm, int[] mme)
        {
            int baseAddr = _pc - 1;

            FetchOpCode(mme);

            if ((_opCode & 7) < 7)
            {
                //Operation produces a value.
                AssignmentOperation asgOp = (AssignmentOperation)((_opCode >> 4) & 7);

                int result = GetAluResult();

                switch (asgOp)
                {
                    //Fetch parameter and ignore result.
                    case AssignmentOperation.IgnoreAndFetch:
                    {
                        SetDstGpr(FetchParam());

                        break;
                    }

                    //Move result.
                    case AssignmentOperation.Move:
                    {
                        SetDstGpr(result);

                        break;
                    }

                    //Move result and use as Method Address.
                    case AssignmentOperation.MoveAndSetMaddr:
                    {
                        SetDstGpr(result);

                        SetMethAddr(result);

                        break;
                    }

                    //Fetch parameter and send result.
                    case AssignmentOperation.FetchAndSend:
                    {
                        SetDstGpr(FetchParam());

                        Send(vmm, result);

                        break;
                    }

                    //Move and send result.
                    case AssignmentOperation.MoveAndSend:
                    {
                        SetDstGpr(result);

                        Send(vmm, result);

                        break;
                    }

                    //Fetch parameter and use result as Method Address.
                    case AssignmentOperation.FetchAndSetMaddr:
                    {
                        SetDstGpr(FetchParam());

                        SetMethAddr(result);

                        break;
                    }

                    //Move result and use as Method Address, then fetch and send paramter.
                    case AssignmentOperation.MoveAndSetMaddrThenFetchAndSend:
                    {
                        SetDstGpr(result);

                        SetMethAddr(result);

                        Send(vmm, FetchParam());

                        break;
                    }

                    //Move result and use as Method Address, then send bits 17:12 of result.
                    case AssignmentOperation.MoveAndSetMaddrThenSendHigh:
                    {
                        SetDstGpr(result);

                        SetMethAddr(result);

                        Send(vmm, (result >> 12) & 0x3f);

                        break;
                    }
                }
            }
            else
            {
                //Branch.
                bool onNotZero = ((_opCode >> 4) & 1) != 0;

                bool taken = onNotZero
                    ? GetGprA() != 0
                    : GetGprA() == 0;

                if (taken)
                {
                    _pc = baseAddr + GetImm();

                    bool noDelays = (_opCode & 0x20) != 0;

                    if (noDelays)
                    {
                        FetchOpCode(mme);
                    }

                    return true;
                }
            }

            bool exit = (_opCode & 0x80) != 0;

            return !exit;
        }

        private void FetchOpCode(int[] mme)
        {
            _opCode = _pipeOp;

            _pipeOp = mme[_pc++];
        }

        private int GetAluResult()
        {
            AluOperation op = (AluOperation)(_opCode & 7);

            switch (op)
            {
                case AluOperation.AluReg:
                {
                    AluRegOperation aluOp = (AluRegOperation)((_opCode >> 17) & 0x1f);

                    return GetAluResult(aluOp, GetGprA(), GetGprB());
                }

                case AluOperation.AddImmediate:
                {
                    return GetGprA() + GetImm();
                }

                case AluOperation.BitfieldReplace:
                case AluOperation.BitfieldExtractLslImm:
                case AluOperation.BitfieldExtractLslReg:
                {
                    int bfSrcBit = (_opCode >> 17) & 0x1f;
                    int bfSize   = (_opCode >> 22) & 0x1f;
                    int bfDstBit = (_opCode >> 27) & 0x1f;

                    int bfMask = (1 << bfSize) - 1;

                    int dst = GetGprA();
                    int src = GetGprB();

                    switch (op)
                    {
                        case AluOperation.BitfieldReplace:
                        {
                            src = (int)((uint)src >> bfSrcBit) & bfMask;

                            dst &= ~(bfMask << bfDstBit);

                            dst |= src << bfDstBit;

                            return dst;
                        }

                        case AluOperation.BitfieldExtractLslImm:
                        {
                            src = (int)((uint)src >> dst) & bfMask;

                            return src << bfDstBit;
                        }

                        case AluOperation.BitfieldExtractLslReg:
                        {
                            src = (int)((uint)src >> bfSrcBit) & bfMask;

                            return src << dst;
                        }
                    }

                    break;
                }

                case AluOperation.ReadImmediate:
                {
                    return Read(GetGprA() + GetImm());
                }
            }

            throw new ArgumentException(nameof(_opCode));
        }

        private int GetAluResult(AluRegOperation aluOp, int a, int b)
        {
            switch (aluOp)
            {
                case AluRegOperation.Add:
                {
                    ulong result = (ulong)a + (ulong)b;

                    _carry = result > 0xffffffff;

                    return (int)result;
                }

                case AluRegOperation.AddWithCarry:
                {
                    ulong result = (ulong)a + (ulong)b + (_carry ? 1UL : 0UL);

                    _carry = result > 0xffffffff;

                    return (int)result;
                }

                case AluRegOperation.Subtract:
                {
                    ulong result = (ulong)a - (ulong)b;

                    _carry = result < 0x100000000;

                    return (int)result;
                }

                case AluRegOperation.SubtractWithBorrow:
                {
                    ulong result = (ulong)a - (ulong)b - (_carry ? 0UL : 1UL);

                    _carry = result < 0x100000000;

                    return (int)result;
                }

                case AluRegOperation.BitwiseExclusiveOr: return   a ^  b;
                case AluRegOperation.BitwiseOr:          return   a |  b;
                case AluRegOperation.BitwiseAnd:         return   a &  b;
                case AluRegOperation.BitwiseAndNot:      return   a & ~b;
                case AluRegOperation.BitwiseNotAnd:      return ~(a &  b);
            }

            throw new ArgumentOutOfRangeException(nameof(aluOp));
        }

        private int GetImm()
        {
            //Note: The immediate is signed, the sign-extension is intended here.
            return _opCode >> 14;
        }

        private void SetMethAddr(int value)
        {
            _methAddr = (value >>  0) & 0xfff;
            _methIncr = (value >> 12) & 0x3f;
        }

        private void SetDstGpr(int value)
        {
            _gprs[(_opCode >> 8) & 7] = value;
        }

        private int GetGprA()
        {
            return GetGprValue((_opCode >> 11) & 7);
        }

        private int GetGprB()
        {
            return GetGprValue((_opCode >> 14) & 7);
        }

        private int GetGprValue(int index)
        {
            return index != 0 ? _gprs[index] : 0;
        }

        private int FetchParam()
        {
            int value;

            if (!Fifo.TryDequeue(out value))
            {
                Logger.PrintWarning(LogClass.Gpu, "Macro attempted to fetch an inexistent argument.");

                return 0;
            }

            return value;
        }

        private int Read(int reg)
        {
            return _engine.Registers[reg];
        }

        private void Send(NvGpuVmm vmm, int value)
        {
            GpuMethodCall methCall = new GpuMethodCall(_methAddr, value);

            _engine.CallMethod(vmm, methCall);

            _methAddr += _methIncr;
        }
    }
}