source: branches/virtualcpu/UFormMain.pas

Last change on this file was 87, checked in by chronos, 9 years ago

Another experimental virtual CPU implementation.

File size: 3.5 KB
Line 
1unit UFormMain;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 ComCtrls, Menus, UMachine;
10
11type
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
38var
39 Form1: TForm1;
40
41implementation
42
43{$R *.lfm}
44
45{ TForm1 }
46
47procedure TForm1.FormCreate(Sender: TObject);
48begin
49 Machine := TMachine.Create;
50end;
51
52procedure TForm1.ButtonRunClick(Sender: TObject);
53begin
54 Machine.Run;
55 ShowMemory;
56 ShowRegisters;
57end;
58
59procedure TForm1.FormDestroy(Sender: TObject);
60begin
61 Machine.Free;
62end;
63
64procedure TForm1.FormShow(Sender: TObject);
65begin
66 SetLength(Machine.Memory, 1000);
67 Addr := 0;
68
69 Example;
70 ShowMemory;
71 ShowRegisters;
72end;
73
74procedure TForm1.ListViewMemoryData(Sender: TObject; Item: TListItem);
75var
76 Data: string;
77 I: Integer;
78begin
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;
86end;
87
88procedure TForm1.ListViewRegistersData(Sender: TObject; Item: TListItem);
89begin
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;
94end;
95
96procedure TForm1.Example;
97const
98 Acc = 0;
99 Source = 1;
100 Dest = 2;
101 Counter = 3;
102 Temp = 4;
103begin
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);
122end;
123
124procedure TForm1.ShowMemory;
125begin
126 ListViewMemory.Items.Count := Length(Machine.Memory) div 16;
127 ListViewMemory.Refresh;
128end;
129
130procedure TForm1.ShowRegisters;
131begin
132 ListViewRegisters.Items.Count := Length(Machine.Registers);
133 ListViewRegisters.Refresh;
134end;
135
136procedure TForm1.AddData(Data: Integer);
137begin
138 Machine.Memory[Addr] := Data;
139 Inc(Addr);
140end;
141
142procedure TForm1.AddOpcode(Opcode: TOpcode);
143begin
144 AddData(Integer(Opcode));
145end;
146
147procedure TForm1.AddOpcode1(Opcode: TOpcode; Param1: Integer);
148begin
149 AddData(Integer(Opcode));
150 AddData(Param1);
151end;
152
153procedure TForm1.AddOpcode2(Opcode: TOpcode; Param1, Param2: Integer);
154begin
155 AddData(Integer(Opcode));
156 AddData(Param1);
157 AddData(Param2);
158end;
159
160end.
161
Note: See TracBrowser for help on using the repository browser.