source: branches/configured machine/Cpu2.pas

Last change on this file was 239, checked in by chronos, 16 months ago
File size: 3.6 KB
Line 
1unit Cpu2;
2
3interface
4
5uses
6 Classes, SysUtils;
7
8type
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
57implementation
58
59{ TCpu2 }
60
61function TCpu2<TAddress>.ReadMem8(Address: TAddress): Byte;
62begin
63
64end;
65
66function TCpu2<TAddress>.ReadMem16(Address: TAddress): Word;
67begin
68
69end;
70
71function TCpu2<TAddress>.ReadMemAddress(Address: TAddress): TAddress;
72begin
73
74end;
75
76function TCpu2<TAddress>.ReadMemPc8: Byte;
77begin
78 Result := ReadMem8(PC);
79 Inc(PC, SizeOf(Byte));
80end;
81
82function TCpu2<TAddress>.ReadMemPc16: Word;
83begin
84 Result := ReadMem16(PC);
85 Inc(PC, SizeOf(Word));
86end;
87
88function TCpu2<TAddress>.ReadMemPcAddress: TAddress;
89begin
90 Result := ReadMemAddress(PC);
91 Inc(PC, SizeOf(TAddress));
92end;
93
94procedure TCpu2<TAddress>.WriteMem8(Address: TAddress; Data: Byte);
95begin
96
97end;
98
99procedure TCpu2<TAddress>.WriteMem16(Address: TAddress; Data: Word);
100begin
101
102end;
103
104procedure TCpu2<TAddress>.WriteMemAddress(Address: TAddress; Data: TAddress);
105begin
106
107end;
108
109procedure TCpu2<TAddress>.Run;
110begin
111 while not Halted do
112 Step;
113end;
114
115procedure TCpu2<TAddress>.Step;
116var
117 Opcode: Byte;
118 Instruction: TInstruction;
119 Addr: TAddress;
120begin
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;
138end;
139
140{ TValue }
141
142class function TValue.Create8(Value: Byte): TValue;
143begin
144 Result.Value8 := Value;
145end;
146
147class function TValue.Create16(Value: Word): TValue;
148begin
149 Result.Value16 := Value;
150end;
151
152class function TValue.Create32(Value: DWord): TValue;
153begin
154 Result.Value32 := Value;
155end;
156
157class function TValue.Create64(Value: QWord): TValue;
158begin
159 Result.Value64 := Value;
160end;
161
162end.
163
164LD8 A, n
165LD16 B, nn
166LD32 B, nnnn
167LD A.b, n
168LD B.w, nn
169LD B.d, nnnn
170LD A8, n
171LD B16, nn
172LD B32, nn
173LD B32, A32
174LD32 B, A
175LD B, A
176
177
178IN A, (n)
179OUT (n), A
180LD A, (n)
181LD (n), A
Note: See TracBrowser for help on using the repository browser.