summaryrefslogtreecommitdiff
path: root/Q1.core/Arch/InstructionSet.cs
blob: 212e77e1645bd3fae12e6778117f5b19d000ad07 (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
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
namespace Q1.core.Arch;

public static class InstructionSet
{
    public static readonly InstructionGroup[] Lookup = new InstructionGroup[128];
    
    static InstructionSet()
    {
        var nop = new ImplicitInstruction { Name = "NOP", Execute = InstructionSet.Nop };
        var nopSimple = new SimpleInstruction { Name = "NOP", Execute = InstructionSet.Nop };
        var nopExtended = new ExtendedInstruction { Name = "NOP", Execute = InstructionSet.Nop };

        #region Control Group

        InstructionSet.Lookup[0x00] = new InstructionGroup
        {
            Implicit = nop,
            SimpleAddressing = new SimpleInstruction { Name = "JMP", Execute = InstructionSet.Jump },
            ExtendedImplicit = new ImplicitInstruction { Name = "HALT", Execute = InstructionSet.Halt },
            ExtendedAddressing = new ExtendedInstruction { Name = "MOV", Execute = InstructionSet.Move }
        };
        
        InstructionSet.Lookup[0x01] = new InstructionGroup
        {
            Implicit = new ImplicitInstruction { Name = "RET", Execute = InstructionSet.Return },
            SimpleAddressing = new SimpleInstruction { Name = "CALL", Execute = InstructionSet.Call },
            ExtendedImplicit = new ImplicitInstruction { Name = "SUS", Execute = InstructionSet.Suspend },
            ExtendedAddressing = nopExtended
        };
        
        InstructionSet.Lookup[0x02] = new InstructionGroup
        {
            Implicit = new ImplicitInstruction { Name = "BZ(LX)", Execute = InstructionSet.BranchIfZeroLx },
            SimpleAddressing = new SimpleInstruction { Name = "BZ", Execute = InstructionSet.BranchIfZero },
            ExtendedImplicit = new ImplicitInstruction { Name = "BR", Execute = InstructionSet.Branch },
            ExtendedAddressing = nopExtended
        };
        
        InstructionSet.Lookup[0x03] = new InstructionGroup
        {
            Implicit = nop,
            SimpleAddressing = new SimpleInstruction { Name = "PUSH", Execute = InstructionSet.Push },
            ExtendedImplicit = nop,
            ExtendedAddressing = nopExtended
        };
        
        InstructionSet.Lookup[0x04] = new InstructionGroup
        {
            Implicit = nop,
            SimpleAddressing = new SimpleInstruction { Name = "POP", Execute = InstructionSet.Pop },
            ExtendedImplicit = nop,
            ExtendedAddressing = nopExtended
        };
        
        InstructionSet.Lookup[0x05] = new InstructionGroup
        {
            Implicit = nop,
            SimpleAddressing = new SimpleInstruction { Name = "INT", Execute = InstructionSet.Interrupt },
            ExtendedImplicit = nop,
            ExtendedAddressing = nopExtended
        };

        #endregion
        
        #region Data Group

        InstructionSet.Lookup[0x10] = new InstructionGroup
        {
            Implicit = new ImplicitInstruction { Name = "INV(DX)", Execute = InstructionSet.InverseDx },
            SimpleAddressing = new SimpleInstruction { Name = "INV", Execute = InstructionSet.Inverse },
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "AND", Execute = InstructionSet.And }
        };
        
        InstructionSet.Lookup[0x11] = new InstructionGroup
        {
            Implicit = nop,
            SimpleAddressing = nopSimple,
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "OR", Execute = InstructionSet.Or }
        };
        
        InstructionSet.Lookup[0x12] = new InstructionGroup
        {
            Implicit = nop,
            SimpleAddressing = nopSimple,
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "XOR", Execute = InstructionSet.Xor }
        };
        
        InstructionSet.Lookup[0x13] = new InstructionGroup
        {
            Implicit = new ImplicitInstruction { Name = "SHPL(DX)", Execute = InstructionSet.ShiftPlaceLeftDx },
            SimpleAddressing = new SimpleInstruction { Name = "SHPL", Execute = InstructionSet.ShiftPlaceLeft },
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "SHL", Execute = InstructionSet.ShiftLeft }
        };
        
        InstructionSet.Lookup[0x14] = new InstructionGroup
        {
            Implicit = new ImplicitInstruction { Name = "SHPR(DX)", Execute = InstructionSet.ShiftPlaceRightDx },
            SimpleAddressing = new SimpleInstruction { Name = "SHPR", Execute = InstructionSet.ShiftPlaceRight },
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "SHR", Execute = InstructionSet.ShiftRight }
        };

        #endregion

        #region Logic Group

        InstructionSet.Lookup[0x20] = new InstructionGroup
        {
            Implicit = new ImplicitInstruction { Name = "NOT(LX)", Execute = InstructionSet.NotLx },
            SimpleAddressing = new SimpleInstruction { Name = "NOT", Execute = InstructionSet.Not },
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "CMP", Execute = InstructionSet.Compare }
        };
        
        InstructionSet.Lookup[0x21] = new InstructionGroup
        {
            Implicit = nop,
            SimpleAddressing = nopSimple,
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "LT", Execute = InstructionSet.LessThan }
        };
        
        InstructionSet.Lookup[0x22] = new InstructionGroup
        {
            Implicit = nop,
            SimpleAddressing = nopSimple,
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "GT", Execute = InstructionSet.GreaterThan }
        };

        #endregion
        
        #region Arithmatic Group

        InstructionSet.Lookup[0x30] = new InstructionGroup
        {
            Implicit = new ImplicitInstruction { Name = "INC(AX)", Execute = InstructionSet.IncrementAx },
            SimpleAddressing = new SimpleInstruction { Name = "INC", Execute = InstructionSet.Increment },
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "ADD", Execute = InstructionSet.Add }
        };
        
        InstructionSet.Lookup[0x31] = new InstructionGroup
        {
            Implicit = new ImplicitInstruction { Name = "DEC(AX)", Execute = InstructionSet.DecrementAx },
            SimpleAddressing = new SimpleInstruction { Name = "DEC", Execute = InstructionSet.Decrement },
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "SUB", Execute = InstructionSet.Subtract }
        };
        
        InstructionSet.Lookup[0x32] = new InstructionGroup
        {
            Implicit = nop,
            SimpleAddressing = nopSimple,
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "DIV", Execute = InstructionSet.Divide }
        };
        
        InstructionSet.Lookup[0x33] = new InstructionGroup
        {
            Implicit = nop,
            SimpleAddressing = nopSimple,
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "MUL", Execute = InstructionSet.Multiply }
        };
        
        InstructionSet.Lookup[0x34] = new InstructionGroup
        {
            Implicit = nop,
            SimpleAddressing = nopSimple,
            ExtendedImplicit = nop,
            ExtendedAddressing = new ExtendedInstruction { Name = "DIV", Execute = InstructionSet.Modulo }
        };

        #endregion
    }

    private static void Nop(Chip chip, bool word) { /* No operation */ }
    private static void Nop(Chip chip, u8 m1, bool word) { /* No operation */ }
    private static void Nop(Chip chip, u8 m1, u8 m2, bool word) { /* No operation */ }

    #region Control Instructions

    private static void Halt(Chip chip, bool word)
    {
        throw new NotImplementedException("HALT instruction is not implemented yet.");
    }

    private static void Suspend(Chip chip, bool word)
    {
        throw new NotImplementedException("SUS instruction is not implemented yet.");
    }
    
    private static void Interrupt(Chip chip, u8 m1, bool word)
    {
        u16 interruptNumber = chip.Load(m1, word);
        
        chip.Interrupt(interruptNumber);
    }
    
    private static void Move(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value = chip.Load(m1, word);
        chip.Store(m2, value, word);
    }
    
    private static void Jump(Chip chip, u8 m1, bool word)
    {
        u16 address = chip.Load(m1, word);
        chip.Pc = address;
    }

    private static void Call(Chip chip, u8 m1, bool word)
    {
        u16 targetAddress = chip.Load(m1, word);
        chip.Push(chip.Pc);
        chip.Pc = targetAddress;
    }
    
    private static void Return(Chip chip, bool word)
    {
        u16 returnAddress = chip.Pop();
        chip.Pc = returnAddress;
    }
    
    private static void BranchIfZero(Chip chip, u8 m1, bool word)
    {
        u16 value = chip.Load(m1, word);
        if (value == 0)
            chip.SkipInstruction();
    }
    
    private static void BranchIfZeroLx(Chip chip, bool word)
    {
        if (chip.Lx == 0)
            chip.SkipInstruction();
    }

    private static void Branch(Chip chip, bool word)
    {
        chip.SkipInstruction();
    }
    
    private static void Push(Chip chip, u8 m1, bool word)
    {
        u16 value = chip.Load(m1, word);
        chip.Push(value);
    }
    
    private static void Pop(Chip chip, u8 m1, bool word)
    {
        u16 value = chip.Pop();
        chip.Store(m1, value, word);
    }
    
    #endregion

    #region Data Instructions
    
    private static void Inverse(Chip chip, u8 m1, bool word)
    {
        u16 value = chip.Load(m1, word);
        value = (u16)~value;

        chip.Dx = value;
    }
    
    private static void InverseDx(Chip chip, bool word)
    {
        chip.Dx = (u16)~chip.Dx;
    }
    
    private static void And(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value1 = chip.Load(m1, word);
        u16 value2 = chip.Load(m2, word);
        u16 result = (u16)(value1 & value2);

        chip.Dx = result;
    }
    
    private static void Or(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value1 = chip.Load(m1, word);
        u16 value2 = chip.Load(m2, word);
        u16 result = (u16)(value1 | value2);

        chip.Dx = result;
    }
    
    private static void Xor(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value1 = chip.Load(m1, word);
        u16 value2 = chip.Load(m2, word);
        u16 result = (u16)(value1 ^ value2);

        chip.Dx = result;
    }

    private static void ShiftLeft(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value = chip.Load(m1, word);
        u16 shiftAmount = (u16) (chip.Load(m2, word) & 0xF); // Limit shift to 0-15
        u16 result = (u16) (value << shiftAmount);

        chip.Dx = result;
    }
    
    private static void ShiftRight(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value = chip.Load(m1, word);
        u16 shiftAmount = (u16) (chip.Load(m2, word) & 0xF); // Limit shift to 0-15
        u16 result = (u16) (value >> shiftAmount);

        chip.Dx = result;
    }
    
    private static void ShiftPlaceLeft(Chip chip, u8 m1, bool word)
    {
        u16 value = chip.Load(m1, word);
        u16 result = (u16) ((value << 1) | (value >> 15)); // Rotate left

        chip.Dx = result;
    }
    
    private static void ShiftPlaceRight(Chip chip, u8 m1, bool word)
    {
        u16 value = chip.Load(m1, word);
        u16 result = (u16) ((value >> 1) | (value << 15)); // Rotate right

        chip.Dx = result;
    }
    
    private static void ShiftPlaceLeftDx(Chip chip, bool word)
    {
        u16 value = chip.Dx;
        u16 result = (u16) ((value << 1) | (value >> 15)); // Rotate left

        chip.Dx = result;
    }
    
    private static void ShiftPlaceRightDx(Chip chip, bool word)
    {
        u16 value = chip.Dx;
        u16 result = (u16) ((value >> 1) | (value << 15)); // Rotate right

        chip.Dx = result;
    }

    #endregion
    
    #region Logic Instructions
    
    private static void Not(Chip chip, u8 m1, bool word)
    {
        u16 value = chip.Load(m1, word);
        chip.Lx = value == 0 ? (u16)1 : (u16)0;
    }
    
    private static void NotLx(Chip chip, bool word)
    {
        chip.Lx = chip.Lx == 0 ? (u16)1 : (u16)0;
    }
    
    private static void Compare(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value1 = chip.Load(m1, word);
        u16 value2 = chip.Load(m2, word);
        chip.Lx = value1 == value2 ? (u16)1 : (u16)0;
    }
    
    private static void LessThan(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value1 = chip.Load(m1, word);
        u16 value2 = chip.Load(m2, word);
        chip.Lx = value1 < value2 ? (u16)1 : (u16)0;
    }
    
    private static void GreaterThan(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value1 = chip.Load(m1, word);
        u16 value2 = chip.Load(m2, word);
        chip.Lx = value1 > value2 ? (u16)1 : (u16)0;
    }

    #endregion
    
    #region Arithmetic Instructions

    private static void Increment(Chip chip, u8 m1, bool word)
    {
        u16 value = chip.Load(m1, word);
        value++;
        chip.Store(m1, value, word);
    }
    
    private static void IncrementAx(Chip chip, bool word)
    {
        chip.Ax++;
    }
    
    private static void Decrement(Chip chip, u8 m1, bool word)
    {
        u16 value = chip.Load(m1, word);
        value--;
        chip.Store(m1, value, word);
    }
    
    private static void DecrementAx(Chip chip, bool word)
    {
        chip.Ax--;
    }
    
    private static void Add(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value1 = chip.Load(m1, word);
        u16 value2 = chip.Load(m2, word);
        u16 result = (u16)(value1 + value2);

        chip.Ax = result;
    }
    
    private static void Subtract(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value1 = chip.Load(m1, word);
        u16 value2 = chip.Load(m2, word);
        u16 result = (u16)(value1 - value2);

        chip.Ax = result;
    }
    
    private static void Multiply(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value1 = chip.Load(m1, word);
        u16 value2 = chip.Load(m2, word);
        u16 result = (u16)(value1 * value2);

        chip.Ax = result;
    }
    
    private static void Divide(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value1 = chip.Load(m1, word);
        u16 value2 = chip.Load(m2, word);
        if (value2 == 0)
            throw new DivideByZeroException("Attempted to divide by zero.");

        u16 result = (u16)(value1 / value2);
        chip.Ax = result;
    }
    
    private static void Modulo(Chip chip, u8 m1, u8 m2, bool word)
    {
        u16 value1 = chip.Load(m1, word);
        u16 value2 = chip.Load(m2, word);
        if (value2 == 0)
            throw new DivideByZeroException("Attempted to modulo by zero.");

        u16 result = (u16)(value1 % value2);
        chip.Ax = result;
    }

    #endregion
}