1 | unit UFormMain;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
---|
9 | ComCtrls, Menus, UMachine;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TForm1 }
|
---|
14 |
|
---|
15 | TForm1 = class(TForm)
|
---|
16 | ButtonRun: TButton;
|
---|
17 | ListViewMemory: TListView;
|
---|
18 | ListViewRegisters: TListView;
|
---|
19 | procedure ButtonRunClick(Sender: TObject);
|
---|
20 | procedure FormCreate(Sender: TObject);
|
---|
21 | procedure FormDestroy(Sender: TObject);
|
---|
22 | procedure FormShow(Sender: TObject);
|
---|
23 | procedure ListViewMemoryData(Sender: TObject; Item: TListItem);
|
---|
24 | procedure ListViewRegistersData(Sender: TObject; Item: TListItem);
|
---|
25 | private
|
---|
26 | Addr: Integer;
|
---|
27 | procedure Example;
|
---|
28 | procedure ShowMemory;
|
---|
29 | procedure ShowRegisters;
|
---|
30 | procedure AddData(Data: Integer);
|
---|
31 | procedure AddOpcode(Opcode: TOpcode);
|
---|
32 | procedure AddOpcode1(Opcode: TOpcode; Param1: Integer);
|
---|
33 | procedure AddOpcode2(Opcode: TOpcode; Param1, Param2: Integer);
|
---|
34 | public
|
---|
35 | Machine: TMachine;
|
---|
36 | end;
|
---|
37 |
|
---|
38 | var
|
---|
39 | Form1: TForm1;
|
---|
40 |
|
---|
41 | implementation
|
---|
42 |
|
---|
43 | {$R *.lfm}
|
---|
44 |
|
---|
45 | { TForm1 }
|
---|
46 |
|
---|
47 | procedure TForm1.FormCreate(Sender: TObject);
|
---|
48 | begin
|
---|
49 | Machine := TMachine.Create;
|
---|
50 | end;
|
---|
51 |
|
---|
52 | procedure TForm1.ButtonRunClick(Sender: TObject);
|
---|
53 | begin
|
---|
54 | Machine.Run;
|
---|
55 | ShowMemory;
|
---|
56 | ShowRegisters;
|
---|
57 | end;
|
---|
58 |
|
---|
59 | procedure TForm1.FormDestroy(Sender: TObject);
|
---|
60 | begin
|
---|
61 | Machine.Free;
|
---|
62 | end;
|
---|
63 |
|
---|
64 | procedure TForm1.FormShow(Sender: TObject);
|
---|
65 | begin
|
---|
66 | SetLength(Machine.Memory, 1000);
|
---|
67 | Addr := 0;
|
---|
68 |
|
---|
69 | Example;
|
---|
70 | ShowMemory;
|
---|
71 | ShowRegisters;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | procedure TForm1.ListViewMemoryData(Sender: TObject; Item: TListItem);
|
---|
75 | var
|
---|
76 | Data: string;
|
---|
77 | I: Integer;
|
---|
78 | begin
|
---|
79 | if (Item.Index >= 0) and (Item.Index < (Length(Machine.Memory) div 16)) then begin
|
---|
80 | Item.Caption := IntToHex(Item.Index * 16, 8);
|
---|
81 | Data := '';
|
---|
82 | for I := 0 to 15 do
|
---|
83 | Data := Data + ' ' + IntToHex(Machine.Memory[Item.Index * 16 + I], 2);
|
---|
84 | Item.SubItems.Add(Data);
|
---|
85 | end;
|
---|
86 | end;
|
---|
87 |
|
---|
88 | procedure TForm1.ListViewRegistersData(Sender: TObject; Item: TListItem);
|
---|
89 | begin
|
---|
90 | if (Item.Index >= 0) and (Item.Index < Length(Machine.Registers)) then begin
|
---|
91 | Item.Caption := IntToHex(Item.Index, 2);
|
---|
92 | Item.SubItems.Add(IntToHex(Machine.Registers[Item.Index], 8));
|
---|
93 | end;
|
---|
94 | end;
|
---|
95 |
|
---|
96 | procedure TForm1.Example;
|
---|
97 | const
|
---|
98 | Acc = 0;
|
---|
99 | Source = 1;
|
---|
100 | Dest = 2;
|
---|
101 | Counter = 3;
|
---|
102 | Temp = 4;
|
---|
103 | begin
|
---|
104 | AddOpcode1(opGetDataSize, Temp);
|
---|
105 | AddOpcode2(opSetRegSize, Source, Temp);
|
---|
106 | AddOpcode2(opSetRegSize, Dest, Temp);
|
---|
107 | AddOpcode2(opSetRegSize, Counter, Temp);
|
---|
108 | AddOpcode2(opCopyConst, Temp, 1);
|
---|
109 | AddOpcode2(opSetRegSize, Acc, Temp);
|
---|
110 |
|
---|
111 | AddOpcode2(opCopyConst, Counter, 8);
|
---|
112 | AddOpcode2(opCopyConst, Source, $30);
|
---|
113 | AddOpcode2(opCopyConst, Dest, $40);
|
---|
114 |
|
---|
115 | AddOpcode2(opCopyFromMem, Acc, Source);
|
---|
116 | AddOpcode2(opCopyToMem, Acc, Dest);
|
---|
117 | AddOpcode1(opInc, Source);
|
---|
118 | AddOpcode1(opInc, Dest);
|
---|
119 | AddOpcode1(opDec, Counter);
|
---|
120 | AddOpcode2(opJumpRelNotZero, Counter, -15);
|
---|
121 | AddOpcode(opHalt);
|
---|
122 | end;
|
---|
123 |
|
---|
124 | procedure TForm1.ShowMemory;
|
---|
125 | begin
|
---|
126 | ListViewMemory.Items.Count := Length(Machine.Memory) div 16;
|
---|
127 | ListViewMemory.Refresh;
|
---|
128 | end;
|
---|
129 |
|
---|
130 | procedure TForm1.ShowRegisters;
|
---|
131 | begin
|
---|
132 | ListViewRegisters.Items.Count := Length(Machine.Registers);
|
---|
133 | ListViewRegisters.Refresh;
|
---|
134 | end;
|
---|
135 |
|
---|
136 | procedure TForm1.AddData(Data: Integer);
|
---|
137 | begin
|
---|
138 | Machine.Memory[Addr] := Data;
|
---|
139 | Inc(Addr);
|
---|
140 | end;
|
---|
141 |
|
---|
142 | procedure TForm1.AddOpcode(Opcode: TOpcode);
|
---|
143 | begin
|
---|
144 | AddData(Integer(Opcode));
|
---|
145 | end;
|
---|
146 |
|
---|
147 | procedure TForm1.AddOpcode1(Opcode: TOpcode; Param1: Integer);
|
---|
148 | begin
|
---|
149 | AddData(Integer(Opcode));
|
---|
150 | AddData(Param1);
|
---|
151 | end;
|
---|
152 |
|
---|
153 | procedure TForm1.AddOpcode2(Opcode: TOpcode; Param1, Param2: Integer);
|
---|
154 | begin
|
---|
155 | AddData(Integer(Opcode));
|
---|
156 | AddData(Param1);
|
---|
157 | AddData(Param2);
|
---|
158 | end;
|
---|
159 |
|
---|
160 | end.
|
---|
161 |
|
---|