1 | unit UInstructionWriter;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UCpu;
|
---|
9 |
|
---|
10 | type
|
---|
11 | { TInstructionWriter }
|
---|
12 |
|
---|
13 | TInstructionWriter = class
|
---|
14 | private
|
---|
15 | public
|
---|
16 | Memory: Pointer;
|
---|
17 | IP: T;
|
---|
18 | function GetRelativeAddr(BaseIP, TargetAddr: T): T;
|
---|
19 | procedure Write(Value: T);
|
---|
20 | procedure WriteSigned(Value: T);
|
---|
21 | procedure WriteOpcode(Value: TOpcode);
|
---|
22 | procedure WriteString(Text: string);
|
---|
23 | procedure NoOperation;
|
---|
24 | procedure Load(R1, R2: Integer);
|
---|
25 | procedure Halt;
|
---|
26 | procedure LoadConst(R, Value: Integer);
|
---|
27 | procedure LoadMemory(R1, R2: Integer);
|
---|
28 | procedure StoreMemory(R1, R2: Integer);
|
---|
29 | procedure Neg(R: Integer);
|
---|
30 | procedure Input(R1, R2: Integer);
|
---|
31 | procedure Output(R1, R2: Integer);
|
---|
32 | procedure Subtract(R1, R2: Integer);
|
---|
33 | procedure Jump(Addr: Integer);
|
---|
34 | procedure JumpCond(Addr: Integer);
|
---|
35 | procedure JumpRelative(R1: Integer; Addr: Integer);
|
---|
36 | procedure JumpRelativeCond(Addr: Integer);
|
---|
37 | procedure TestEqual(R1, R2: Integer);
|
---|
38 | procedure Increment(R: Integer);
|
---|
39 | procedure Decrement(R: Integer);
|
---|
40 | constructor Create;
|
---|
41 | procedure Init;
|
---|
42 | end;
|
---|
43 |
|
---|
44 |
|
---|
45 | implementation
|
---|
46 |
|
---|
47 | { TInstructionWriter }
|
---|
48 |
|
---|
49 | function TInstructionWriter.GetRelativeAddr(BaseIP, TargetAddr: T): T;
|
---|
50 | begin
|
---|
51 | Result := TargetAddr - (BaseIP + 1 + T.GetByteSize);
|
---|
52 | end;
|
---|
53 |
|
---|
54 | procedure TInstructionWriter.WriteString(Text: string);
|
---|
55 | var
|
---|
56 | I: Integer;
|
---|
57 | begin
|
---|
58 | for I := 1 to Length(Text) do
|
---|
59 | Write(Ord(Text[I]));
|
---|
60 | end;
|
---|
61 |
|
---|
62 | procedure TInstructionWriter.NoOperation;
|
---|
63 | begin
|
---|
64 | WriteOpcode(opNop);
|
---|
65 | end;
|
---|
66 |
|
---|
67 | procedure TInstructionWriter.Load(R1, R2: Integer);
|
---|
68 | begin
|
---|
69 | WriteOpcode(opLoad);
|
---|
70 | Write(R1);
|
---|
71 | Write(R2);
|
---|
72 | end;
|
---|
73 |
|
---|
74 | procedure TInstructionWriter.Halt;
|
---|
75 | begin
|
---|
76 | WriteOpcode(opHalt);
|
---|
77 | end;
|
---|
78 |
|
---|
79 | procedure TInstructionWriter.LoadConst(R, Value: Integer);
|
---|
80 | begin
|
---|
81 | WriteOpcode(opLoadConst);
|
---|
82 | Write(R);
|
---|
83 | Write(Value);
|
---|
84 | end;
|
---|
85 |
|
---|
86 | procedure TInstructionWriter.LoadMemory(R1, R2: Integer);
|
---|
87 | begin
|
---|
88 | WriteOpcode(opLoadMem);
|
---|
89 | Write(R1);
|
---|
90 | Write(R2);
|
---|
91 | end;
|
---|
92 |
|
---|
93 | procedure TInstructionWriter.StoreMemory(R1, R2: Integer);
|
---|
94 | begin
|
---|
95 | WriteOpcode(opStoreMem);
|
---|
96 | Write(R1);
|
---|
97 | Write(R2);
|
---|
98 | end;
|
---|
99 |
|
---|
100 | procedure TInstructionWriter.Neg(R: Integer);
|
---|
101 | begin
|
---|
102 | WriteOpcode(opNeg);
|
---|
103 | Write(R);
|
---|
104 | end;
|
---|
105 |
|
---|
106 | procedure TInstructionWriter.Input(R1, R2: Integer);
|
---|
107 | begin
|
---|
108 | WriteOpcode(opInput);
|
---|
109 | Write(R1);
|
---|
110 | Write(R2);
|
---|
111 | end;
|
---|
112 |
|
---|
113 | procedure TInstructionWriter.Output(R1, R2: Integer);
|
---|
114 | begin
|
---|
115 | WriteOpcode(opOutput);
|
---|
116 | Write(R1);
|
---|
117 | Write(R2);
|
---|
118 | end;
|
---|
119 |
|
---|
120 | procedure TInstructionWriter.Subtract(R1, R2: Integer);
|
---|
121 | begin
|
---|
122 | WriteOpcode(opSub);
|
---|
123 | Write(R1);
|
---|
124 | Write(R2);
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TInstructionWriter.Jump(Addr: Integer);
|
---|
128 | begin
|
---|
129 | WriteOpcode(opJump);
|
---|
130 | Write(Addr);
|
---|
131 | end;
|
---|
132 |
|
---|
133 | procedure TInstructionWriter.JumpCond(Addr: Integer);
|
---|
134 | begin
|
---|
135 | WriteOpcode(opJumpCond);
|
---|
136 | Write(Addr);
|
---|
137 | end;
|
---|
138 |
|
---|
139 | procedure TInstructionWriter.TestEqual(R1, R2: Integer);
|
---|
140 | begin
|
---|
141 | WriteOpcode(opTestEqual);
|
---|
142 | Write(R1);
|
---|
143 | Write(R2);
|
---|
144 | end;
|
---|
145 |
|
---|
146 | procedure TInstructionWriter.JumpRelative(R1: Integer; Addr: Integer);
|
---|
147 | begin
|
---|
148 | WriteOpcode(opJumpRel);
|
---|
149 | Write(R1);
|
---|
150 | Write(Addr);
|
---|
151 | end;
|
---|
152 |
|
---|
153 | procedure TInstructionWriter.JumpRelativeCond(Addr: Integer);
|
---|
154 | begin
|
---|
155 | WriteOpcode(opJumpRelCond);
|
---|
156 | Write(Addr - IP - 1);
|
---|
157 | end;
|
---|
158 |
|
---|
159 | procedure TInstructionWriter.Increment(R: Integer);
|
---|
160 | begin
|
---|
161 | WriteOpcode(opInc);
|
---|
162 | Write(R);
|
---|
163 | end;
|
---|
164 |
|
---|
165 | procedure TInstructionWriter.Decrement(R: Integer);
|
---|
166 | begin
|
---|
167 | WriteOpcode(opDec);
|
---|
168 | Write(R);
|
---|
169 | end;
|
---|
170 |
|
---|
171 | constructor TInstructionWriter.Create;
|
---|
172 | begin
|
---|
173 | Memory := nil;
|
---|
174 | Init;
|
---|
175 | end;
|
---|
176 |
|
---|
177 | procedure TInstructionWriter.Init;
|
---|
178 | begin
|
---|
179 | IP := 0;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | procedure TInstructionWriter.Write(Value: T);
|
---|
183 | var
|
---|
184 | C: Integer;
|
---|
185 | begin
|
---|
186 | C := Value.WriteToAddr(Pointer(NativeUInt(Memory) + IP));
|
---|
187 | Inc(IP, C);
|
---|
188 | end;
|
---|
189 |
|
---|
190 | procedure TInstructionWriter.WriteSigned(Value: T);
|
---|
191 | var
|
---|
192 | C: Integer;
|
---|
193 | begin
|
---|
194 | C := Value.WriteToAddr(Pointer(NativeUInt(Memory) + IP));
|
---|
195 | Inc(IP, C);
|
---|
196 | end;
|
---|
197 |
|
---|
198 | procedure TInstructionWriter.WriteOpcode(Value: TOpcode);
|
---|
199 | begin
|
---|
200 | Write(Integer(Value));
|
---|
201 | end;
|
---|
202 |
|
---|
203 |
|
---|
204 | end.
|
---|
205 |
|
---|