1 | unit Cpu2;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TBitWidth = (bw8, bw16, bw32, bw64, bw128, bw256, bw512, bw1024);
|
---|
10 |
|
---|
11 | TInstruction = (inNop, inHalt, inLoad8, inLoad16, inStore8, inStore16,
|
---|
12 | inAdd8, inAdd16, inJump, inJumpNZ);
|
---|
13 |
|
---|
14 | { TValue }
|
---|
15 |
|
---|
16 | TValue = record
|
---|
17 | BitWidth: TBitWidth;
|
---|
18 | class function Create8(Value: Byte): TValue; static;
|
---|
19 | class function Create16(Value: Word): TValue; static;
|
---|
20 | class function Create32(Value: DWord): TValue; static;
|
---|
21 | class function Create64(Value: QWord): TValue; static;
|
---|
22 | case TBitWidth of
|
---|
23 | bw8: (Value8: Byte);
|
---|
24 | bw16: (Value16: Word);
|
---|
25 | bw32: (Value32: DWord);
|
---|
26 | bw64: (Value64: QWord);
|
---|
27 | end;
|
---|
28 |
|
---|
29 | { TCpu2 }
|
---|
30 |
|
---|
31 | TCpu2<TAddress> = class
|
---|
32 | public
|
---|
33 | type
|
---|
34 | TReadMemEvent = function (Address: TAddress): TValue of object;
|
---|
35 | private
|
---|
36 | FReadMem: TReadMemEvent;
|
---|
37 | function ReadMem8(Address: TAddress): Byte;
|
---|
38 | function ReadMem16(Address: TAddress): Word;
|
---|
39 | function ReadMemAddress(Address: TAddress): TAddress;
|
---|
40 | function ReadMemPc8: Byte;
|
---|
41 | function ReadMemPc16: Word;
|
---|
42 | function ReadMemPcAddress: TAddress;
|
---|
43 | procedure WriteMem8(Address: TAddress; Data: Byte);
|
---|
44 | procedure WriteMem16(Address: TAddress; Data: Word);
|
---|
45 | procedure WriteMemAddress(Address: TAddress; Data: TAddress);
|
---|
46 | public
|
---|
47 | Halted: Boolean;
|
---|
48 | A: TValue;
|
---|
49 | PC: TAddress;
|
---|
50 | Zero: Boolean;
|
---|
51 | procedure Run;
|
---|
52 | procedure Step;
|
---|
53 | property ReadMem: TReadMemEvent read FReadMem write FReadMem;
|
---|
54 | end;
|
---|
55 |
|
---|
56 |
|
---|
57 | implementation
|
---|
58 |
|
---|
59 | { TCpu2 }
|
---|
60 |
|
---|
61 | function TCpu2<TAddress>.ReadMem8(Address: TAddress): Byte;
|
---|
62 | begin
|
---|
63 |
|
---|
64 | end;
|
---|
65 |
|
---|
66 | function TCpu2<TAddress>.ReadMem16(Address: TAddress): Word;
|
---|
67 | begin
|
---|
68 |
|
---|
69 | end;
|
---|
70 |
|
---|
71 | function TCpu2<TAddress>.ReadMemAddress(Address: TAddress): TAddress;
|
---|
72 | begin
|
---|
73 |
|
---|
74 | end;
|
---|
75 |
|
---|
76 | function TCpu2<TAddress>.ReadMemPc8: Byte;
|
---|
77 | begin
|
---|
78 | Result := ReadMem8(PC);
|
---|
79 | Inc(PC, SizeOf(Byte));
|
---|
80 | end;
|
---|
81 |
|
---|
82 | function TCpu2<TAddress>.ReadMemPc16: Word;
|
---|
83 | begin
|
---|
84 | Result := ReadMem16(PC);
|
---|
85 | Inc(PC, SizeOf(Word));
|
---|
86 | end;
|
---|
87 |
|
---|
88 | function TCpu2<TAddress>.ReadMemPcAddress: TAddress;
|
---|
89 | begin
|
---|
90 | Result := ReadMemAddress(PC);
|
---|
91 | Inc(PC, SizeOf(TAddress));
|
---|
92 | end;
|
---|
93 |
|
---|
94 | procedure TCpu2<TAddress>.WriteMem8(Address: TAddress; Data: Byte);
|
---|
95 | begin
|
---|
96 |
|
---|
97 | end;
|
---|
98 |
|
---|
99 | procedure TCpu2<TAddress>.WriteMem16(Address: TAddress; Data: Word);
|
---|
100 | begin
|
---|
101 |
|
---|
102 | end;
|
---|
103 |
|
---|
104 | procedure TCpu2<TAddress>.WriteMemAddress(Address: TAddress; Data: TAddress);
|
---|
105 | begin
|
---|
106 |
|
---|
107 | end;
|
---|
108 |
|
---|
109 | procedure TCpu2<TAddress>.Run;
|
---|
110 | begin
|
---|
111 | while not Halted do
|
---|
112 | Step;
|
---|
113 | end;
|
---|
114 |
|
---|
115 | procedure TCpu2<TAddress>.Step;
|
---|
116 | var
|
---|
117 | Opcode: Byte;
|
---|
118 | Instruction: TInstruction;
|
---|
119 | Addr: TAddress;
|
---|
120 | begin
|
---|
121 | Opcode := ReadMemPc8;
|
---|
122 | Instruction := TInstruction(Opcode);
|
---|
123 | case Instruction of
|
---|
124 | inNop: ;
|
---|
125 | inHalt: Halted := True;
|
---|
126 | inLoad8: A.Value8 := ReadMem8(ReadMemPcAddress);
|
---|
127 | inLoad16: A.Value16 := ReadMem16(ReadMemPcAddress);
|
---|
128 | inStore8: WriteMem8(ReadMemPcAddress, A.Value8);
|
---|
129 | inStore16: WriteMem16(ReadMemPcAddress, A.Value16);
|
---|
130 | inAdd8: A.Value8 := A.Value8 + ReadMem8(ReadMemPcAddress);
|
---|
131 | inAdd16: A.Value16 := A.Value16 + ReadMem16(ReadMemPcAddress);
|
---|
132 | inJump: PC := ReadMemAddress;
|
---|
133 | inJumpNZ: begin
|
---|
134 | Addr := ReadMemAddress;
|
---|
135 | if Zero then PC := Addr;
|
---|
136 | end;
|
---|
137 | end;
|
---|
138 | end;
|
---|
139 |
|
---|
140 | { TValue }
|
---|
141 |
|
---|
142 | class function TValue.Create8(Value: Byte): TValue;
|
---|
143 | begin
|
---|
144 | Result.Value8 := Value;
|
---|
145 | end;
|
---|
146 |
|
---|
147 | class function TValue.Create16(Value: Word): TValue;
|
---|
148 | begin
|
---|
149 | Result.Value16 := Value;
|
---|
150 | end;
|
---|
151 |
|
---|
152 | class function TValue.Create32(Value: DWord): TValue;
|
---|
153 | begin
|
---|
154 | Result.Value32 := Value;
|
---|
155 | end;
|
---|
156 |
|
---|
157 | class function TValue.Create64(Value: QWord): TValue;
|
---|
158 | begin
|
---|
159 | Result.Value64 := Value;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | end.
|
---|
163 |
|
---|
164 | LD8 A, n
|
---|
165 | LD16 B, nn
|
---|
166 | LD32 B, nnnn
|
---|
167 | LD A.b, n
|
---|
168 | LD B.w, nn
|
---|
169 | LD B.d, nnnn
|
---|
170 | LD A8, n
|
---|
171 | LD B16, nn
|
---|
172 | LD B32, nn
|
---|
173 | LD B32, A32
|
---|
174 | LD32 B, A
|
---|
175 | LD B, A
|
---|
176 |
|
---|
177 |
|
---|
178 | IN A, (n)
|
---|
179 | OUT (n), A
|
---|
180 | LD A, (n)
|
---|
181 | LD (n), A
|
---|