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 | Cpu: TCpu;
|
---|
17 | IP: T;
|
---|
18 | procedure Write(Value: T);
|
---|
19 | procedure NoOperation;
|
---|
20 | procedure Load(R1, R2: Integer);
|
---|
21 | procedure Halt;
|
---|
22 | procedure LoadConst(R, Value: Integer);
|
---|
23 | procedure LoadMemory(R1, R2: Integer);
|
---|
24 | procedure StoreMemory(R1, R2: Integer);
|
---|
25 | procedure Neg(R: Integer);
|
---|
26 | procedure Input(R1, R2: Integer);
|
---|
27 | procedure Output(R1, R2: Integer);
|
---|
28 | procedure Subtract(R1, R2: Integer);
|
---|
29 | procedure Jump(Addr: Integer);
|
---|
30 | procedure JumpCond(Addr: Integer);
|
---|
31 | procedure TestZero(R: Integer);
|
---|
32 | {$IFDEF EXT_REL_JUMP}procedure JumpRelative(Addr: Integer);{$ENDIF}
|
---|
33 | procedure SysCall;
|
---|
34 | procedure Increment(R: Integer);
|
---|
35 | procedure Decrement(R: Integer);
|
---|
36 | constructor Create;
|
---|
37 | end;
|
---|
38 |
|
---|
39 |
|
---|
40 | implementation
|
---|
41 |
|
---|
42 | { TInstructionWriter }
|
---|
43 |
|
---|
44 | procedure TInstructionWriter.NoOperation;
|
---|
45 | begin
|
---|
46 | Write(T(opNop));
|
---|
47 | end;
|
---|
48 |
|
---|
49 | procedure TInstructionWriter.Load(R1, R2: Integer);
|
---|
50 | begin
|
---|
51 | Write(T(opLoad));
|
---|
52 | Write(R1);
|
---|
53 | Write(R2);
|
---|
54 | end;
|
---|
55 |
|
---|
56 | procedure TInstructionWriter.Halt;
|
---|
57 | begin
|
---|
58 | Write(T(opHalt));
|
---|
59 | end;
|
---|
60 |
|
---|
61 | procedure TInstructionWriter.LoadConst(R, Value: Integer);
|
---|
62 | begin
|
---|
63 | Write(T(opLoadConst));
|
---|
64 | Write(R);
|
---|
65 | Write(Value);
|
---|
66 | end;
|
---|
67 |
|
---|
68 | procedure TInstructionWriter.LoadMemory(R1, R2: Integer);
|
---|
69 | begin
|
---|
70 | Write(T(opLoadMem));
|
---|
71 | Write(R1);
|
---|
72 | Write(R2);
|
---|
73 | end;
|
---|
74 |
|
---|
75 | procedure TInstructionWriter.StoreMemory(R1, R2: Integer);
|
---|
76 | begin
|
---|
77 | Write(T(opStoreMem));
|
---|
78 | Write(R1);
|
---|
79 | Write(R2);
|
---|
80 | end;
|
---|
81 |
|
---|
82 | procedure TInstructionWriter.Neg(R: Integer);
|
---|
83 | begin
|
---|
84 | Write(T(opNeg));
|
---|
85 | Write(R);
|
---|
86 | end;
|
---|
87 |
|
---|
88 | procedure TInstructionWriter.Input(R1, R2: Integer);
|
---|
89 | begin
|
---|
90 | Write(T(opInput));
|
---|
91 | Write(R1);
|
---|
92 | Write(R2);
|
---|
93 | end;
|
---|
94 |
|
---|
95 | procedure TInstructionWriter.Output(R1, R2: Integer);
|
---|
96 | begin
|
---|
97 | Write(T(opOutput));
|
---|
98 | Write(R1);
|
---|
99 | Write(R2);
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TInstructionWriter.Subtract(R1, R2: Integer);
|
---|
103 | begin
|
---|
104 | Write(T(opSub));
|
---|
105 | Write(R1);
|
---|
106 | Write(R2);
|
---|
107 | end;
|
---|
108 |
|
---|
109 | procedure TInstructionWriter.Jump(Addr: Integer);
|
---|
110 | begin
|
---|
111 | Write(T(opJump));
|
---|
112 | Write(Addr);
|
---|
113 | end;
|
---|
114 |
|
---|
115 | procedure TInstructionWriter.JumpCond(Addr: Integer);
|
---|
116 | begin
|
---|
117 | Write(T(opJumpCond));
|
---|
118 | Write(Addr);
|
---|
119 | end;
|
---|
120 |
|
---|
121 | procedure TInstructionWriter.TestZero(R: Integer);
|
---|
122 | begin
|
---|
123 | Write(T(opTestZero));
|
---|
124 | Write(R);
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TInstructionWriter.SysCall;
|
---|
128 | begin
|
---|
129 | Write(T(opSysCall));
|
---|
130 | end;
|
---|
131 |
|
---|
132 | {$IFDEF EXT_REL_JUMP}
|
---|
133 | procedure TInstructionWriter.JumpRelative(Addr: Integer);
|
---|
134 | begin
|
---|
135 | Write(T(opJumpRel));
|
---|
136 | Write(Addr - IP - 1);
|
---|
137 | end;
|
---|
138 | {$ENDIF}
|
---|
139 |
|
---|
140 | procedure TInstructionWriter.Increment(R: Integer);
|
---|
141 | begin
|
---|
142 | Write(T(opInc));
|
---|
143 | Write(R);
|
---|
144 | end;
|
---|
145 |
|
---|
146 | procedure TInstructionWriter.Decrement(R: Integer);
|
---|
147 | begin
|
---|
148 | Write(T(opDec));
|
---|
149 | Write(R);
|
---|
150 | end;
|
---|
151 |
|
---|
152 | constructor TInstructionWriter.Create;
|
---|
153 | begin
|
---|
154 | IP := 0;
|
---|
155 | Cpu := nil;
|
---|
156 | end;
|
---|
157 |
|
---|
158 | procedure TInstructionWriter.Write(Value: T);
|
---|
159 | begin
|
---|
160 | Cpu.Memory[IP] := Value;
|
---|
161 | Inc(IP);
|
---|
162 | end;
|
---|
163 |
|
---|
164 |
|
---|
165 | end.
|
---|
166 |
|
---|