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
|
namespace Q1Emu.Chip;
using System;
using System.Collections.Generic;
public partial class Q1Cpu
{
public u16 FetchData(u8 mode)
{
return mode switch
{
0x0 => this.AddressingMode_Implicit(),
0x1 => this.AddressingMode_Immediate(),
0x2 => this.AddressingMode_Direct(),
0x3 => this.AddressingMode_Indirect(),
0x4 => this.AddressingMode_Relative(),
0x5 => this.AddressingMode_Register(),
0x6 => this.AddressingMode_RegisterIndirect(),
0x7 => this.AddressingMode_RegisterIndirectOffset(),
_ => throw new InvalidOperationException($"Unknown addressing mode: {mode:X2}"),
};
}
public void StoreData(u8 mode, u16 value)
{
switch (mode)
{
case 0x0:
throw new InvalidOperationException("Cannot store data in implicit mode.");
case 0x1:
throw new InvalidOperationException("Cannot store data in immediate mode.");
case 0x2:
this.AddressingMode_Direct_Store(value);
break;
case 0x3:
this.AddressingMode_Indirect_Store(value);
break;
case 0x4:
this.AddressingMode_Relative_Store(value);
break;
case 0x5:
this.AddressingMode_Register_Store(value);
break;
case 0x6:
this.AddressingMode_RegisterIndirect_Store(value);
break;
case 0x7:
this.AddressingMode_RegisterIndirectOffset_Store(value);
break;
default:
throw new InvalidOperationException($"Unknown addressing mode: {mode:X2}");
}
}
public void TransformData(u8 mode, Func<u16, u16> transform)
{
// if (mode != 0x05)
// {
// u16 address = this.FetchData(mode);
// u16 value = this.Bus.ReadWord(address);
// u16 transformedValue = transform(value);
// this.Bus.WriteWord(address, transformedValue);
// }
// else
// {
// u8 regIndex = this.Bus.Read(this.PC);
// this.PC += 1;
//
// u16 value = this.FetchRegister(regIndex);
// u16 transformedValue = transform(value);
// this.StoreRegister(regIndex, transformedValue);
// }
throw new NotImplementedException("TransformData is not implemented yet.");
}
public u8 AddressingModeLength(u8 mode)
{
return mode switch
{
0x0 => 0, // Implicit
0x1 => 2, // Immediate
0x2 => 2, // Direct
0x3 => 2, // Indirect
0x4 => 2, // Relative
0x5 => 1, // Register
0x6 => 1, // Register indirect
0x7 => 3, // Register indirect with offset
_ => throw new InvalidOperationException($"Unknown addressing mode: {mode:X2}"),
};
}
public u16 AddressingMode_Implicit()
{
return 0;
}
public u16 AddressingMode_Immediate()
{
u16 value = this.Bus.ReadWord(this.PC);
this.PC += 2;
return value;
}
public u16 AddressingMode_Direct()
{
u16 address = this.Bus.ReadWord(this.PC);
this.PC += 2;
u16 value = this.Bus.ReadWord(address);
return value;
}
public u16 AddressingMode_Indirect()
{
u16 address = this.Bus.ReadWord(this.PC);
this.PC += 2;
u16 indirectAddress = this.Bus.ReadWord(address);
u16 value = this.Bus.ReadWord(indirectAddress);
return value;
}
public u16 AddressingMode_Relative()
{
u16 offset = this.Bus.ReadWord(this.PC);
this.PC += 2;
u16 address = (u16) (this.PC + offset);
return address;
}
public u16 AddressingMode_Register()
{
u8 regIndex = this.Bus.Read(this.PC);
this.PC += 1;
return this.FetchRegister(regIndex);
}
public u16 AddressingMode_RegisterIndirect()
{
u8 regIndex = this.Bus.Read(this.PC);
this.PC += 1;
u16 address = this.FetchRegister(regIndex);
u16 value = this.Bus.ReadWord(address);
Console.WriteLine($"Value at {address:X4}: {value:X4}");
return value;
}
public u16 AddressingMode_RegisterIndirectOffset()
{
u8 regIndex = this.Bus.Read(this.PC);
this.PC += 1;
i16 offset = Make.i16(this.Bus.ReadSeq(this.PC, 2));
Console.WriteLine("Offset: " + offset);
this.PC += 2;
u16 address = (u16) (this.FetchRegister(regIndex) + offset);
u16 value = this.Bus.ReadWord(address);
return value;
}
public void AddressingMode_Direct_Store(u16 data)
{
u16 address = this.Bus.ReadWord(this.PC);
this.PC += 2;
this.Bus.WriteWord(address, data);
}
public void AddressingMode_Indirect_Store(u16 data)
{
u16 address = this.Bus.ReadWord(this.PC);
this.PC += 2;
u16 indirectAddress = this.Bus.ReadWord(address);
this.Bus.WriteWord(indirectAddress, data);
}
public void AddressingMode_Relative_Store(u16 data)
{
u16 offset = this.Bus.ReadWord(this.PC);
this.PC += 2;
u16 address = (u16) (this.PC + offset);
this.Bus.WriteWord(address, data);
}
public void AddressingMode_Register_Store(u16 data)
{
u8 regIndex = this.Bus.Read(this.PC);
this.PC += 1;
this.StoreRegister(regIndex, data);
}
public void AddressingMode_RegisterIndirect_Store(u16 data)
{
u8 regIndex = this.Bus.Read(this.PC);
this.PC += 1;
u16 address = this.FetchRegister(regIndex);
this.Bus.WriteWord(address, data);
}
public void AddressingMode_RegisterIndirectOffset_Store(u16 data)
{
u8 regIndex = this.Bus.Read(this.PC);
this.PC += 1;
i16 offset = Make.i16(this.Bus.ReadSeq(this.PC, 2));
this.PC += 2;
u16 address = (u16) (this.FetchRegister(regIndex) + offset);
this.Bus.WriteWord(address, data);
}
}
|