summaryrefslogtreecommitdiff
path: root/Q1.core/Arch/AddressingModes.cs
blob: 96b44758be88d93eeab6982d6ea4c9bba986218c (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
namespace Q1.core.Arch;

using Components;

public static class AddressingModes
{
    public static AddressingMode[] Lookup = new AddressingMode[8];
    
    static AddressingModes()
    {
        AddressingModes.Lookup[0x00] = new AddressingMode
        {
            Name = "Implicit",
            SizeB = 0,
            SizeW = 0,
            LoadByte = AddressingModes.ImplicitLoadByte,
            LoadWord = AddressingModes.ImplicitLoadWord,
            StoreByte = AddressingModes.ImplicitStoreByte,
            StoreWord = AddressingModes.ImplicitStoreWord
        };
        
        AddressingModes.Lookup[0x01] = new AddressingMode
        {
            Name = "Immediate",
            SizeB = 1,
            SizeW = 2,
            LoadByte = AddressingModes.ImmediateLoadByte,
            LoadWord = AddressingModes.ImmediateLoadWord,
            StoreByte = AddressingModes.ImmediateStoreByte,
            StoreWord = AddressingModes.ImmediateStoreWord
        };
        
        AddressingModes.Lookup[0x02] = new AddressingMode
        {
            Name = "Direct",
            SizeB = 2,
            SizeW = 2,
            LoadByte = AddressingModes.DirectLoadByte,
            LoadWord = AddressingModes.DirectLoadWord,
            StoreByte = AddressingModes.DirectStoreByte,
            StoreWord = AddressingModes.DirectStoreWord
        };
        
        AddressingModes.Lookup[0x03] = new AddressingMode
        {
            Name = "Indirect",
            SizeB = 2,
            SizeW = 2,
            LoadByte = AddressingModes.IndirectLoadByte,
            LoadWord = AddressingModes.IndirectLoadWord,
            StoreByte = AddressingModes.IndirectStoreByte,
            StoreWord = AddressingModes.IndirectStoreWord
        };
        
        AddressingModes.Lookup[0x04] = new AddressingMode
        {
            Name = "Relative",
            SizeB = 2,
            SizeW = 2,
            LoadByte = AddressingModes.RelativeLoadByte,
            LoadWord = AddressingModes.RelativeLoadWord,
            StoreByte = AddressingModes.RelativeStoreByte,
            StoreWord = AddressingModes.RelativeStoreWord
        };
        
        AddressingModes.Lookup[0x05] = new AddressingMode
        {
            Name = "Register",
            SizeB = 1,
            SizeW = 1,
            LoadByte = AddressingModes.RegisterLoadByte,
            LoadWord = AddressingModes.RegisterLoadWord,
            StoreByte = AddressingModes.RegisterStoreByte,
            StoreWord = AddressingModes.RegisterStoreWord
        };
        
        AddressingModes.Lookup[0x06] = new AddressingMode
        {
            Name = "Register Indirect",
            SizeB = 1,
            SizeW = 1,
            LoadByte = AddressingModes.RegisterIndirectLoadByte,
            LoadWord = AddressingModes.RegisterIndirectLoadWord,
            StoreByte = AddressingModes.RegisterIndirectStoreByte,
            StoreWord = AddressingModes.RegisterIndirectStoreWord
        };

        AddressingModes.Lookup[0x07] = new AddressingMode
        {
            Name = "Register Indirect with Offset",
            SizeB = 3,
            SizeW = 3,
            LoadByte = AddressingModes.RegisterIndirectOffsetLoadByte,
            LoadWord = AddressingModes.RegisterIndirectOffsetLoadWord,
            StoreByte = AddressingModes.RegisterIndirectOffsetStoreByte,
            StoreWord = AddressingModes.RegisterIndirectOffsetStoreWord
        };
    }

    private static u8 ImplicitLoadByte(Chip chip)
        => throw new InvalidOperationException("Cannot load from an implicit value");
    private static u16 ImplicitLoadWord(Chip chip)
        => throw new InvalidOperationException("Cannot load from an implicit value");
    private static void ImplicitStoreByte(Chip chip, u8 value)
        => throw new InvalidOperationException("Cannot store to an implicit value");
    private static void ImplicitStoreWord(Chip chip, u16 value)
        => throw new InvalidOperationException("Cannot store to an implicit value");


    private static u8 ImmediateLoadByte(Chip chip)
    {
        u8 value = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;
        return value;
    }
    private static u16 ImmediateLoadWord(Chip chip)
    {
        u16 value = chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;
        return value;
    }
    private static void ImmediateStoreByte(Chip chip, u8 value)
        => throw new InvalidOperationException("Cannot store to an immediate value");
    private static void ImmediateStoreWord(Chip chip, u16 value)
        => throw new InvalidOperationException("Cannot store to an immediate value");


    private static u8 DirectLoadByte(Chip chip)
    {
        u16 address = chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        return chip.Bus.Read(address);
    }
    private static u16 DirectLoadWord(Chip chip)
    {
        u16 address = chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        return chip.Bus.ReadWord(address);
    }
    private static void DirectStoreByte(Chip chip, u8 value)
    {
        u16 address = chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        chip.Bus.Write(address, value);
    }
    private static void DirectStoreWord(Chip chip, u16 value)
    {
        u16 address = chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        chip.Bus.WriteWord(address, value);
    }


    private static u8 IndirectLoadByte(Chip chip)
    {
        u16 pointer = chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = chip.Bus.ReadWord(pointer);
        return chip.Bus.Read(address);
    }
    private static u16 IndirectLoadWord(Chip chip)
    {
        u16 pointer = chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = chip.Bus.ReadWord(pointer);
        return chip.Bus.ReadWord(address);
    }
    private static void IndirectStoreByte(Chip chip, u8 value)
    {
        u16 pointer = chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = chip.Bus.ReadWord(pointer);
        chip.Bus.Write(address, value);
    }
    private static void IndirectStoreWord(Chip chip, u16 value)
    {
        u16 pointer = chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = chip.Bus.ReadWord(pointer);
        chip.Bus.WriteWord(address, value);
    }


    private static u8 RelativeLoadByte(Chip chip)
    {
        i16 offset = (i16) chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = (u16) (chip.Pc + offset);
        return chip.Bus.Read(address);
    }
    private static u16 RelativeLoadWord(Chip chip)
    {
        i16 offset = (i16) chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = (u16) (chip.Pc + offset);
        return chip.Bus.ReadWord(address);
    }
    private static void RelativeStoreByte(Chip chip, u8 value)
    {
        i16 offset = (i16) chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = (u16) (chip.Pc + offset);
        chip.Bus.Write(address, value);
    }
    private static void RelativeStoreWord(Chip chip, u16 value)
    {
        i16 offset = (i16) chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = (u16) (chip.Pc + offset);
        chip.Bus.WriteWord(address, value);
    }
    
    private static u8 RegisterLoadByte(Chip chip)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        return (u8) (chip.GetRegister(regIndex) & 0xFF);
    }
    private static u16 RegisterLoadWord(Chip chip)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        return chip.GetRegister(regIndex);
    }
    private static void RegisterStoreByte(Chip chip, u8 value)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        u16 current = chip.GetRegister(regIndex);
        u16 newValue = (u16)((current & 0xFF00) | value);
        chip.SetRegister(regIndex, newValue);
    }
    private static void RegisterStoreWord(Chip chip, u16 value)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        chip.SetRegister(regIndex, value);
    }
    
    private static u8 RegisterIndirectLoadByte(Chip chip)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        u16 address = chip.GetRegister(regIndex);
        return chip.Bus.Read(address);
    }
    private static u16 RegisterIndirectLoadWord(Chip chip)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        u16 address = chip.GetRegister(regIndex);
        return chip.Bus.ReadWord(address);
    }
    private static void RegisterIndirectStoreByte(Chip chip, u8 value)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        u16 address = chip.GetRegister(regIndex);
        chip.Bus.Write(address, value);
    }
    private static void RegisterIndirectStoreWord(Chip chip, u16 value)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        u16 address = chip.GetRegister(regIndex);
        chip.Bus.WriteWord(address, value);
    }
    
    private static u8 RegisterIndirectOffsetLoadByte(Chip chip)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        i16 offset = (i16) chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = (u16)(chip.GetRegister(regIndex) + offset);
        return chip.Bus.Read(address);
    }
    private static u16 RegisterIndirectOffsetLoadWord(Chip chip)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        i16 offset = (i16) chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = (u16)(chip.GetRegister(regIndex) + offset);
        return chip.Bus.ReadWord(address);
    }
    private static void RegisterIndirectOffsetStoreByte(Chip chip, u8 value)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        i16 offset = (i16) chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = (u16)(chip.GetRegister(regIndex) + offset);
        chip.Bus.Write(address, value);
    }
    private static void RegisterIndirectOffsetStoreWord(Chip chip, u16 value)
    {
        u8 regIndex = chip.Bus.Read(chip.Pc);
        chip.Pc += 1;

        i16 offset = (i16) chip.Bus.ReadWord(chip.Pc);
        chip.Pc += 2;

        u16 address = (u16)(chip.GetRegister(regIndex) + offset);
        chip.Bus.WriteWord(address, value);
    }
}