1 | unit Cpu;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Int;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TInstructionEvent = procedure of object;
|
---|
10 |
|
---|
11 | TInstruction = (inNop, inHalt, inLoad, inLoadConst, inInput, inOutput,
|
---|
12 | inJump, inJumpZero, inJumpNotZero, inInc, inDec, inLoadIndex, inPush, inPop,
|
---|
13 | inCall, inRet, inLoadMem, inStoreMem);
|
---|
14 |
|
---|
15 | { TCpu }
|
---|
16 |
|
---|
17 | TCpu = class
|
---|
18 | private
|
---|
19 | FOnReadIo: TReadAddrEvent;
|
---|
20 | FOnReadMem: TReadAddrEvent;
|
---|
21 | FOnWriteIo: TWriteAddrEvent;
|
---|
22 | FOnWriteMem: TWriteAddrEvent;
|
---|
23 | Instructions: array[TInstruction] of TInstructionEvent;
|
---|
24 | procedure InstructionNop;
|
---|
25 | procedure InstructionHalt;
|
---|
26 | procedure InstructionLoad;
|
---|
27 | procedure InstructionLoadConst;
|
---|
28 | procedure InstructionLoadIndex;
|
---|
29 | procedure InstructionLoadMem;
|
---|
30 | procedure InstructionStoreMem;
|
---|
31 | procedure InstructionInput;
|
---|
32 | procedure InstructionOutput;
|
---|
33 | procedure InstructionInc;
|
---|
34 | procedure InstructionDec;
|
---|
35 | procedure InstructionJump;
|
---|
36 | procedure InstructionJumpZero;
|
---|
37 | procedure InstructionJumpNotZero;
|
---|
38 | procedure InstructionPush;
|
---|
39 | procedure InstructionPop;
|
---|
40 | procedure InstructionCall;
|
---|
41 | procedure InstructionRet;
|
---|
42 | procedure WriteMem(Address, Data: TInt);
|
---|
43 | function ReadMem(Address: TInt): TInt;
|
---|
44 | function ReadMemPc: TInt;
|
---|
45 | procedure WriteIo(Address, Data: TInt);
|
---|
46 | function ReadIo(Address: TInt): TInt;
|
---|
47 | procedure Push(Data: TInt);
|
---|
48 | function Pop: TInt;
|
---|
49 | public
|
---|
50 | Halted: Boolean;
|
---|
51 | PC: TInt;
|
---|
52 | SP: TInt;
|
---|
53 | procedure Reset;
|
---|
54 | procedure Run;
|
---|
55 | procedure Step;
|
---|
56 | constructor Create;
|
---|
57 | property OnReadMem: TReadAddrEvent read FOnReadMem write FOnReadMem;
|
---|
58 | property OnWriteMem: TWriteAddrEvent read FOnWriteMem write FOnWriteMem;
|
---|
59 | property OnReadIo: TReadAddrEvent read FOnReadIo write FOnReadIo;
|
---|
60 | property OnWriteIo: TWriteAddrEvent read FOnWriteIo write FOnWriteIo;
|
---|
61 | end;
|
---|
62 |
|
---|
63 | implementation
|
---|
64 |
|
---|
65 | { TCpu }
|
---|
66 |
|
---|
67 | procedure TCpu.InstructionNop;
|
---|
68 | begin
|
---|
69 | // No operation
|
---|
70 | end;
|
---|
71 |
|
---|
72 | procedure TCpu.InstructionHalt;
|
---|
73 | begin
|
---|
74 | Halted := True;
|
---|
75 | end;
|
---|
76 |
|
---|
77 | procedure TCpu.InstructionLoad;
|
---|
78 | var
|
---|
79 | Dst, Src: TInt;
|
---|
80 | begin
|
---|
81 | Dst := ReadMemPc;
|
---|
82 | Src := ReadMemPc;
|
---|
83 | WriteMem(Dst, ReadMem(Src));
|
---|
84 | end;
|
---|
85 |
|
---|
86 | procedure TCpu.InstructionLoadConst;
|
---|
87 | var
|
---|
88 | Dst, Data: TInt;
|
---|
89 | begin
|
---|
90 | Dst := ReadMemPc;
|
---|
91 | Data := ReadMemPc;
|
---|
92 | WriteMem(Dst, Data);
|
---|
93 | end;
|
---|
94 |
|
---|
95 | procedure TCpu.InstructionLoadIndex;
|
---|
96 | var
|
---|
97 | Dst, Src, Index: TInt;
|
---|
98 | begin
|
---|
99 | Dst := ReadMemPc;
|
---|
100 | Src := ReadMemPc;
|
---|
101 | Index := ReadMemPc;
|
---|
102 | WriteMem(Dst, ReadMem(Src + Index));
|
---|
103 | end;
|
---|
104 |
|
---|
105 | procedure TCpu.InstructionLoadMem;
|
---|
106 | var
|
---|
107 | Dst, Src: TInt;
|
---|
108 | begin
|
---|
109 | Dst := ReadMemPc;
|
---|
110 | Src := ReadMemPc;
|
---|
111 | WriteMem(Dst, ReadMem(ReadMem(Src)));
|
---|
112 | end;
|
---|
113 |
|
---|
114 | procedure TCpu.InstructionStoreMem;
|
---|
115 | var
|
---|
116 | Dst, Src: TInt;
|
---|
117 | begin
|
---|
118 | Dst := ReadMemPc;
|
---|
119 | Src := ReadMemPc;
|
---|
120 | WriteMem(ReadMem(Dst), ReadMem(Src));
|
---|
121 | end;
|
---|
122 |
|
---|
123 | procedure TCpu.InstructionInput;
|
---|
124 | var
|
---|
125 | Dst, Src: TInt;
|
---|
126 | begin
|
---|
127 | Dst := ReadMemPc;
|
---|
128 | Src := ReadMemPc;
|
---|
129 | WriteMem(Dst, ReadIo(Src));
|
---|
130 | end;
|
---|
131 |
|
---|
132 | procedure TCpu.InstructionOutput;
|
---|
133 | var
|
---|
134 | Dst, Src: TInt;
|
---|
135 | begin
|
---|
136 | Dst := ReadMemPc;
|
---|
137 | Src := ReadMemPc;
|
---|
138 | WriteIo(Dst, ReadMem(Src));
|
---|
139 | end;
|
---|
140 |
|
---|
141 | procedure TCpu.InstructionInc;
|
---|
142 | var
|
---|
143 | Addr: TInt;
|
---|
144 | begin
|
---|
145 | Addr := ReadMemPc;
|
---|
146 | WriteMem(Addr, ReadMem(Addr) + 1);
|
---|
147 | end;
|
---|
148 |
|
---|
149 | procedure TCpu.InstructionDec;
|
---|
150 | var
|
---|
151 | Addr: TInt;
|
---|
152 | begin
|
---|
153 | Addr := ReadMemPc;
|
---|
154 | WriteMem(Addr, ReadMem(Addr) - 1);
|
---|
155 | end;
|
---|
156 |
|
---|
157 | procedure TCpu.InstructionJump;
|
---|
158 | begin
|
---|
159 | PC := ReadMemPc;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | procedure TCpu.InstructionJumpZero;
|
---|
163 | var
|
---|
164 | Condition: TInt;
|
---|
165 | Addr: TInt;
|
---|
166 | begin
|
---|
167 | Condition := ReadMemPc;
|
---|
168 | Addr := ReadMemPc;
|
---|
169 | if ReadMem(Condition) = 0 then PC := Addr;
|
---|
170 | end;
|
---|
171 |
|
---|
172 | procedure TCpu.InstructionJumpNotZero;
|
---|
173 | var
|
---|
174 | Condition: TInt;
|
---|
175 | Addr: TInt;
|
---|
176 | begin
|
---|
177 | Condition := ReadMemPc;
|
---|
178 | Addr := ReadMemPc;
|
---|
179 | if ReadMem(Condition) <> 0 then PC := Addr;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | procedure TCpu.InstructionPush;
|
---|
183 | begin
|
---|
184 | Push(ReadMem(ReadMemPc));
|
---|
185 | end;
|
---|
186 |
|
---|
187 | procedure TCpu.InstructionPop;
|
---|
188 | begin
|
---|
189 | WriteMem(ReadMemPc, Pop);
|
---|
190 | end;
|
---|
191 |
|
---|
192 | procedure TCpu.InstructionCall;
|
---|
193 | var
|
---|
194 | Addr: TInt;
|
---|
195 | begin
|
---|
196 | Addr := ReadMemPc;
|
---|
197 | Push(PC);
|
---|
198 | PC := Addr;
|
---|
199 | end;
|
---|
200 |
|
---|
201 | procedure TCpu.InstructionRet;
|
---|
202 | begin
|
---|
203 | PC := Pop;
|
---|
204 | end;
|
---|
205 |
|
---|
206 | procedure TCpu.WriteMem(Address, Data: TInt);
|
---|
207 | begin
|
---|
208 | if Assigned(FOnWriteMem) then FOnWriteMem(Address, Data);
|
---|
209 | end;
|
---|
210 |
|
---|
211 | function TCpu.ReadMem(Address: TInt): TInt;
|
---|
212 | begin
|
---|
213 | if Assigned(FOnReadMem) then Result := FOnReadMem(Address)
|
---|
214 | else Result := 0;
|
---|
215 | end;
|
---|
216 |
|
---|
217 | function TCpu.ReadMemPc: TInt;
|
---|
218 | begin
|
---|
219 | Result := ReadMem(PC);
|
---|
220 | Inc(PC);
|
---|
221 | end;
|
---|
222 |
|
---|
223 | procedure TCpu.WriteIo(Address, Data: TInt);
|
---|
224 | begin
|
---|
225 | if Assigned(FOnWriteIo) then FOnWriteIo(Address, Data);
|
---|
226 | end;
|
---|
227 |
|
---|
228 | function TCpu.ReadIo(Address: TInt): TInt;
|
---|
229 | begin
|
---|
230 | if Assigned(FOnReadIo) then Result := FOnReadIo(Address)
|
---|
231 | else Result := 0;
|
---|
232 | end;
|
---|
233 |
|
---|
234 | procedure TCpu.Push(Data: TInt);
|
---|
235 | begin
|
---|
236 | Dec(SP);
|
---|
237 | WriteMem(SP, Data);
|
---|
238 | end;
|
---|
239 |
|
---|
240 | function TCpu.Pop: TInt;
|
---|
241 | begin
|
---|
242 | Result := ReadMem(SP);
|
---|
243 | Inc(SP);
|
---|
244 | end;
|
---|
245 |
|
---|
246 | procedure TCpu.Reset;
|
---|
247 | begin
|
---|
248 | PC := 0;
|
---|
249 | SP := 1000;
|
---|
250 | Halted := False;
|
---|
251 | end;
|
---|
252 |
|
---|
253 | procedure TCpu.Run;
|
---|
254 | begin
|
---|
255 | Reset;
|
---|
256 | while not Halted do
|
---|
257 | Step;
|
---|
258 | end;
|
---|
259 |
|
---|
260 | procedure TCpu.Step;
|
---|
261 | var
|
---|
262 | Opcode: TInstruction;
|
---|
263 | begin
|
---|
264 | Opcode := TInstruction(FOnReadMem(PC));
|
---|
265 | Inc(PC);
|
---|
266 | Instructions[Opcode];
|
---|
267 | end;
|
---|
268 |
|
---|
269 | constructor TCpu.Create;
|
---|
270 | begin
|
---|
271 | Instructions[inNop] := InstructionNop;
|
---|
272 | Instructions[inHalt] := InstructionHalt;
|
---|
273 | Instructions[inLoad] := InstructionLoad;
|
---|
274 | Instructions[inLoadIndex] := InstructionLoadIndex;
|
---|
275 | Instructions[inLoadConst] := InstructionLoadConst;
|
---|
276 | Instructions[inLoadMem] := InstructionLoadMem;
|
---|
277 | Instructions[inStoreMem] := InstructionStoreMem;
|
---|
278 | Instructions[inInput] := InstructionInput;
|
---|
279 | Instructions[inOutput] := InstructionOutput;
|
---|
280 | Instructions[inJump] := InstructionJump;
|
---|
281 | Instructions[inJumpZero] := InstructionJumpZero;
|
---|
282 | Instructions[inJumpNotZero] := InstructionJumpNotZero;
|
---|
283 | Instructions[inInc] := InstructionInc;
|
---|
284 | Instructions[inDec] := InstructionDec;
|
---|
285 | Instructions[inPush] := InstructionPush;
|
---|
286 | Instructions[inPop] := InstructionPop;
|
---|
287 | Instructions[inCall] := InstructionCall;
|
---|
288 | Instructions[inRet] := InstructionRet;
|
---|
289 | end;
|
---|
290 |
|
---|
291 | end.
|
---|
292 |
|
---|