source: branches/virtcpu varint/UInstructionWriter.pas

Last change on this file was 197, checked in by chronos, 5 years ago
  • Modified: All parts of virtual machine have own form in Forms subdirectory.
  • Modified: Main form moved to Forms subdirectory.
  • Modified: TCpu class moved to UCpu unit.
  • Added: Assembler and dissasembler forms.
File size: 4.0 KB
Line 
1unit UInstructionWriter;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UCpu;
9
10type
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
45implementation
46
47{ TInstructionWriter }
48
49function TInstructionWriter.GetRelativeAddr(BaseIP, TargetAddr: T): T;
50begin
51 Result := TargetAddr - (BaseIP + 1 + T.GetByteSize);
52end;
53
54procedure TInstructionWriter.WriteString(Text: string);
55var
56 I: Integer;
57begin
58 for I := 1 to Length(Text) do
59 Write(Ord(Text[I]));
60end;
61
62procedure TInstructionWriter.NoOperation;
63begin
64 WriteOpcode(opNop);
65end;
66
67procedure TInstructionWriter.Load(R1, R2: Integer);
68begin
69 WriteOpcode(opLoad);
70 Write(R1);
71 Write(R2);
72end;
73
74procedure TInstructionWriter.Halt;
75begin
76 WriteOpcode(opHalt);
77end;
78
79procedure TInstructionWriter.LoadConst(R, Value: Integer);
80begin
81 WriteOpcode(opLoadConst);
82 Write(R);
83 Write(Value);
84end;
85
86procedure TInstructionWriter.LoadMemory(R1, R2: Integer);
87begin
88 WriteOpcode(opLoadMem);
89 Write(R1);
90 Write(R2);
91end;
92
93procedure TInstructionWriter.StoreMemory(R1, R2: Integer);
94begin
95 WriteOpcode(opStoreMem);
96 Write(R1);
97 Write(R2);
98end;
99
100procedure TInstructionWriter.Neg(R: Integer);
101begin
102 WriteOpcode(opNeg);
103 Write(R);
104end;
105
106procedure TInstructionWriter.Input(R1, R2: Integer);
107begin
108 WriteOpcode(opInput);
109 Write(R1);
110 Write(R2);
111end;
112
113procedure TInstructionWriter.Output(R1, R2: Integer);
114begin
115 WriteOpcode(opOutput);
116 Write(R1);
117 Write(R2);
118end;
119
120procedure TInstructionWriter.Subtract(R1, R2: Integer);
121begin
122 WriteOpcode(opSub);
123 Write(R1);
124 Write(R2);
125end;
126
127procedure TInstructionWriter.Jump(Addr: Integer);
128begin
129 WriteOpcode(opJump);
130 Write(Addr);
131end;
132
133procedure TInstructionWriter.JumpCond(Addr: Integer);
134begin
135 WriteOpcode(opJumpCond);
136 Write(Addr);
137end;
138
139procedure TInstructionWriter.TestEqual(R1, R2: Integer);
140begin
141 WriteOpcode(opTestEqual);
142 Write(R1);
143 Write(R2);
144end;
145
146procedure TInstructionWriter.JumpRelative(R1: Integer; Addr: Integer);
147begin
148 WriteOpcode(opJumpRel);
149 Write(R1);
150 Write(Addr);
151end;
152
153procedure TInstructionWriter.JumpRelativeCond(Addr: Integer);
154begin
155 WriteOpcode(opJumpRelCond);
156 Write(Addr - IP - 1);
157end;
158
159procedure TInstructionWriter.Increment(R: Integer);
160begin
161 WriteOpcode(opInc);
162 Write(R);
163end;
164
165procedure TInstructionWriter.Decrement(R: Integer);
166begin
167 WriteOpcode(opDec);
168 Write(R);
169end;
170
171constructor TInstructionWriter.Create;
172begin
173 Memory := nil;
174 Init;
175end;
176
177procedure TInstructionWriter.Init;
178begin
179 IP := 0;
180end;
181
182procedure TInstructionWriter.Write(Value: T);
183var
184 C: Integer;
185begin
186 C := Value.WriteToAddr(Pointer(NativeUInt(Memory) + IP));
187 Inc(IP, C);
188end;
189
190procedure TInstructionWriter.WriteSigned(Value: T);
191var
192 C: Integer;
193begin
194 C := Value.WriteToAddr(Pointer(NativeUInt(Memory) + IP));
195 Inc(IP, C);
196end;
197
198procedure TInstructionWriter.WriteOpcode(Value: TOpcode);
199begin
200 Write(Integer(Value));
201end;
202
203
204end.
205
Note: See TracBrowser for help on using the repository browser.