Программа эмуляции работы КЭШ-памяти


Ниже приведен код программы эмуляции работы КЭШ-памяти. Краткий принцип работы КЭШ-памяти: КЭШ-память обеспечивает быстрый доступ к памяти. Поскольку сделать всю память быстродействующий будет дорого, поэтому используют следующий принцип работы: в КЭШ-памяти должны хранится данные, к которым происходит обращение чаще всего.

В программе реализуется принцип КЭШ-памяти.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, ExtCtrls, StdCtrls;

type
TForm1 = class(TForm)
Panel1: TPanel;
StringGrid1: TStringGrid;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
StringGrid2: TStringGrid;
StringGrid3: TStringGrid;
StringGrid4: TStringGrid;
StringGrid5: TStringGrid;
Edit1: TEdit;
Label7: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit2: TEdit;
Button4: TButton;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
Edit3: TEdit;
Edit4: TEdit;
Button5: TButton;
Label12: TLabel;
Label13: TLabel;
Label14: TLabel;
Button6: TButton;
Label15: TLabel;
Label16: TLabel;
Edit5: TEdit;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
op:array[1..4096]of longint;
k1,k2,k3,k4:array[1..4,1..3]of word;
fl:byte;
n,sum:integer;
flag_npoMax:boolean;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i,ii,j0:integer; r:word;

begin
randomize;
//—-Заполнение КЭША заголовка—-
StringGrid2.Cells[0,1]:=IntToStr(1);
StringGrid2.Cells[0,2]:=IntToStr(2);
StringGrid2.Cells[0,3]:=IntToStr(3);
StringGrid2.Cells[0,4]:=IntToStr(4);
StringGrid2.Cells[1,0]:=’Addr’;
StringGrid2.Cells[2,0]:=’Data’;
StringGrid2.Cells[3,0]:=’Status’;

StringGrid3.Cells[0,1]:=IntToStr(1);
StringGrid3.Cells[0,2]:=IntToStr(2);
StringGrid3.Cells[0,3]:=IntToStr(3);
StringGrid3.Cells[0,4]:=IntToStr(4);
StringGrid3.Cells[1,0]:=’Addr’;
StringGrid3.Cells[2,0]:=’Data’;
StringGrid3.Cells[3,0]:=’Status’;

StringGrid4.Cells[0,1]:=IntToStr(1);
StringGrid4.Cells[0,2]:=IntToStr(2);
StringGrid4.Cells[0,3]:=IntToStr(3);
StringGrid4.Cells[0,4]:=IntToStr(4);
StringGrid4.Cells[1,0]:=’Addr’;
StringGrid4.Cells[2,0]:=’Data’;
StringGrid4.Cells[3,0]:=’Status’;

StringGrid5.Cells[0,1]:=IntToStr(1);
StringGrid5.Cells[0,2]:=IntToStr(2);
StringGrid5.Cells[0,3]:=IntToStr(3);
StringGrid5.Cells[0,4]:=IntToStr(4);
StringGrid5.Cells[1,0]:=’Addr’;
StringGrid5.Cells[2,0]:=’Data’;
StringGrid5.Cells[3,0]:=’Status’;
//—-Заполнение КЭША заголовка—-
//—-Заполнение случайным образом ОЗУ—-
for i:=1 to 4096 do
begin
op[i]:=random(65536);
StringGrid1.Cells[1,i-1]:=IntToStr(op[i]);
StringGrid1.Cells[0,i-1]:=IntToStr(i);
end;
//—-Заполнение случайным образом ОЗУ—-

//—-Заполнение КЭШ случайным образом—-
for ii:=1 to 4 do
for i:=1 to 4 do
begin
//—-Случайный СТАТУС—-
r:=random(4);
k1[i,3]:=r;
r:=random(4);
while k1[i,3]=r do
r:=random(4);
k2[i,3]:=r;
r:=random(4);
while(k2[i,3]=r)or(k1[i,3]=r) do
r:=random(4);
k3[i,3]:=r;
r:=random(4);
while(k3[i,3]=r)or(k2[i,3]=r)or(k1[i,3]=r) do
r:=random(4);
k4[i,3]:=r;
StringGrid2.Cells[3,i]:=IntToStr(k1[i,3]);
StringGrid3.Cells[3,i]:=IntToStr(k2[i,3]);
StringGrid4.Cells[3,i]:=IntToStr(k3[i,3]);
StringGrid5.Cells[3,i]:=IntToStr(k4[i,3]);
//—-Случайный СТАТУС—-
if i=4 then j0:=0 else j0:=i;
r:=random(4096);
while(r mod 4)<>j0 do
r:=random(4096);
if ((r mod 4)=j0) then
begin
if ii=1 then
begin
k1[i,1]:=r;
k1[i,2]:=op[r];
StringGrid2.Cells[1,i]:=IntToStr(k1[i,1]);
StringGrid2.Cells[2,i]:=IntToStr(k1[i,2]);
end;
if ii=2 then
begin
k2[i,1]:=r;
while(k1[i,1]=k2[i,1])and((r mod 4)<>j0) do
begin r:=random(4096); k2[i,1]:=r;end;
k2[i,2]:=op[r];
StringGrid3.Cells[1,i]:=IntToStr(k2[i,1]);
StringGrid3.Cells[2,i]:=IntToStr(k2[i,2]);
end;
if ii=3 then
begin
k3[i,1]:=r;
while((k1[i,1]=k2[i,1])or(k1[i,1]=k3[i,1])or(k2[i,1]=k3[i,1]))and((r mod 4)<>j0) do
begin r:=random(4096); k3[i,1]:=r;end;
k3[i,2]:=op[r];
StringGrid4.Cells[1,i]:=IntToStr(k3[i,1]);
StringGrid4.Cells[2,i]:=IntToStr(k3[i,2]);
end;
if ii=4 then
begin
k4[i,1]:=r;
while((k1[i,1]=k2[i,1])or(k1[i,1]=k3[i,1])or(k1[i,1]=k4[i,1])or(k2[i,1]=k3[i,1])
or(k2[i,1]=k4[i,1])or(k3[i,1]=k4[i,1]))and((r mod 4)<>j0) do
begin r:=random(4096); k3[i,1]:=r;end;
k4[i,2]:=op[r];
StringGrid5.Cells[1,i]:=IntToStr(k4[i,1]);
StringGrid5.Cells[2,i]:=IntToStr(k4[i,2]);
end;
end;
// end;
end;
fl:=0; // для последовательного считывание. 0 — считываем одну ячейку
sum:=0; // количество промахов
flag_npoMax:=false;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i,j,i_stroka,read:integer; flag:boolean; ii:byte;
begin
flag:=false;
if fl=0 then read:=StrToInt(Edit1.Text) else read:=n;
//———————-
for i:=1 to 4 do
begin
if i=4 then j:=0 else j:=i;
if (read mod 4)=j then
if(k1[i,1]=read) then
begin
flag:=true;
if fl=0 then ShowMessage(‘nonagaHue. 4TeHue u3 KELLIA: ‘+IntToStr(k1[i,2]));
ii:=0;
i_stroka:=i;
end
else
if(k2[i,1]=read) then
begin
flag:=true;
if fl=0 then ShowMessage(‘nonagaHue. 4TeHue u3 KELLIA: ‘+IntToStr(k2[i,2]));
ii:=1;
i_stroka:=i;
end
else
if(k3[i,1]=read) then
begin
flag:=true;
if fl=0 then ShowMessage(‘nonagaHue. 4TeHue u3 KELLIA: ‘+IntToStr(k3[i,2]));
ii:=2;
i_stroka:=i;
end
else
if(k4[i,1]=read) then
begin
flag:=true;
if fl=0 then ShowMessage(‘nonagaHue. 4TeHue u3 KELLIA: ‘+IntToStr(k4[i,2]));
ii:=3;
i_stroka:=i;
end;
end;

//——II=0————————
if (flag=true) then
begin
if (ii=0)and(fl=0) then
begin
if (k1[i_stroka,3])=0 then
begin
k1[i_stroka,3]:=3;
k2[i_stroka,3]:=k2[i_stroka,3]-1;
k3[i_stroka,3]:=k3[i_stroka,3]-1;
k4[i_stroka,3]:=k4[i_stroka,3]-1;
end
else
if (k1[i_stroka,3]=1) then
begin
k1[i_stroka,3]:=3;
if(k2[i_stroka,3]=3)then k2[i_stroka,3]:=2
else if(k2[i_stroka,3]=2)then k2[i_stroka,3]:=1;
if(k3[i_stroka,3]=3)then k3[i_stroka,3]:=2
else if(k3[i_stroka,3]=2)then k3[i_stroka,3]:=1;
if(k4[i_stroka,3]=3)then k4[i_stroka,3]:=2
else if(k4[i_stroka,3]=2)then k4[i_stroka,3]:=1;
end
else if(k1[i_stroka,3]=2) then
begin
k1[i_stroka,3]:=3;
if(k2[i_stroka,3]=3)then k2[i_stroka,3]:=2
else if(k3[i_stroka,3]=3) then k3[i_stroka,3]:=2
else if(k4[i_stroka,3]=3) then k4[i_stroka,3]:=2;
end;
end;
//——II=0————————
//——II=1————————
if (ii=1)and(fl=0) then
begin
if (k2[i_stroka,3])=0 then
begin
k2[i_stroka,3]:=3;
k1[i_stroka,3]:=k1[i_stroka,3]-1;
k3[i_stroka,3]:=k3[i_stroka,3]-1;
k4[i_stroka,3]:=k4[i_stroka,3]-1;
end
else
if (k2[i_stroka,3]=1) then
begin
k2[i_stroka,3]:=3;
if(k1[i_stroka,3]=3)then k1[i_stroka,3]:=2
else if(k1[i_stroka,3]=2)then k1[i_stroka,3]:=1;
if(k3[i_stroka,3]=3)then k3[i_stroka,3]:=2
else if(k3[i_stroka,3]=2)then k3[i_stroka,3]:=1;
if(k4[i_stroka,3]=3)then k4[i_stroka,3]:=2
else if(k4[i_stroka,3]=2)then k4[i_stroka,3]:=1;
end
else if(k2[i_stroka,3]=2) then
begin
k2[i_stroka,3]:=3;
if(k1[i_stroka,3]=3)then k1[i_stroka,3]:=2
else if(k3[i_stroka,3]=3) then k3[i_stroka,3]:=2
else if(k4[i_stroka,3]=3) then k4[i_stroka,3]:=2;
end;
end;
//——II=1————————
//——II=2————————
if (ii=2)and(fl=0) then
begin
if (k3[i_stroka,3])=0 then
begin
k3[i_stroka,3]:=3;
k1[i_stroka,3]:=k1[i_stroka,3]-1;
k2[i_stroka,3]:=k2[i_stroka,3]-1;
k4[i_stroka,3]:=k4[i_stroka,3]-1;
end
else
if (k3[i_stroka,3]=1) then
begin
k3[i_stroka,3]:=3;
if(k1[i_stroka,3]=3)then k1[i_stroka,3]:=2
else if(k1[i_stroka,3]=2)then k1[i_stroka,3]:=1;
if(k2[i_stroka,3]=3)then k2[i_stroka,3]:=2
else if(k2[i_stroka,3]=2)then k2[i_stroka,3]:=1;
if(k4[i_stroka,3]=3)then k4[i_stroka,3]:=2
else if(k4[i_stroka,3]=2)then k4[i_stroka,3]:=1;
end
else if(k3[i_stroka,3]=2) then
begin
k3[i_stroka,3]:=3;
if(k1[i_stroka,3]=3)then k1[i_stroka,3]:=2
else if(k2[i_stroka,3]=3) then k2[i_stroka,3]:=2
else if(k4[i_stroka,3]=3) then k4[i_stroka,3]:=2;
end;
end;
//——II=2————————
//——II=3————————
if (ii=3)and(fl=0) then
begin
if (k4[i_stroka,3])=0 then
begin
k4[i_stroka,3]:=3;
k1[i_stroka,3]:=k1[i_stroka,3]-1;
k2[i_stroka,3]:=k2[i_stroka,3]-1;
k3[i_stroka,3]:=k3[i_stroka,3]-1;
end
else
if (k4[i_stroka,3]=1) then
begin
k4[i_stroka,3]:=3;
if(k1[i_stroka,3]=3)then k1[i_stroka,3]:=2
else if(k1[i_stroka,3]=2)then k1[i_stroka,3]:=1;
if(k2[i_stroka,3]=3)then k2[i_stroka,3]:=2
else if(k2[i_stroka,3]=2)then k2[i_stroka,3]:=1;
if(k3[i_stroka,3]=3)then k3[i_stroka,3]:=2
else if(k3[i_stroka,3]=2)then k3[i_stroka,3]:=1;
end
else if(k4[i_stroka,3]=2) then
begin
k4[i_stroka,3]:=3;
if(k1[i_stroka,3]=3)then k1[i_stroka,3]:=2
else if(k2[i_stroka,3]=3) then k2[i_stroka,3]:=2
else if(k3[i_stroka,3]=3) then k3[i_stroka,3]:=2;
end;
end;
StringGrid2.Cells[3,i_stroka]:=IntToStr(k1[i_stroka,3]);
StringGrid3.Cells[3,i_stroka]:=IntToStr(k2[i_stroka,3]);
StringGrid4.Cells[3,i_stroka]:=IntToStr(k3[i_stroka,3]);
StringGrid5.Cells[3,i_stroka]:=IntToStr(k4[i_stroka,3]);
end
else begin inc(sum);if fl=0 then ShowMessage(‘npoMax. C4uTbIBaHue c O3Y ‘+IntToStr(op[read]));end;
if (flag=false)and(fl=0)and(flag_npoMax=false) then
begin
flag_npoMax:=true;Button4Click(Button4);
end;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.FormCreate(Form1);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;

procedure TForm1.Button4Click(Sender: TObject);
var i,ii,nomer,o,j0:integer;
begin
nomer:=StrToInt(Edit1.Text);
if flag_npoMax=true then o:=op[nomer] else
begin
o:=StrToInt(Edit2.Text);
op[nomer]:=o;
end;
StringGrid1.Cells[1,nomer-1]:=IntToStr(op[nomer]);
if (nomer mod 4)=1 then i:=1;
if (nomer mod 4)=2 then i:=2;
if (nomer mod 4)=3 then i:=3;
if (nomer mod 4)=0 then i:=4; // определяем строку

if(k1[i,1]=nomer)or(k2[i,1]=nomer)or(k3[i,1]=nomer)or(k4[i,1]=nomer) then
begin
ShowMessage(‘Y}I{e ectb B KELLI’);

if(k1[i,1]=nomer) then ii:=0
else
if(k2[i,1]=nomer) then ii:=1
else
if(k3[i,1]=nomer) then ii:=2
else
if(k4[i,1]=nomer) then ii:=3;

if (ii=0) then
begin
k1[i,1]:=nomer;
k1[i,2]:=StrToInt(Edit2.Text);
if (k1[i,3])=0 then
begin
k1[i,3]:=3;
k2[i,3]:=k2[i,3]-1;
k3[i,3]:=k3[i,3]-1;
k4[i,3]:=k4[i,3]-1;

end
else
if (k1[i,3]=1) then
begin
k1[i,3]:=3;
if(k2[i,3]=3)then k2[i,3]:=2
else if(k2[i,3]=2)then k2[i,3]:=1;
if(k3[i,3]=3)then k3[i,3]:=2
else if(k3[i,3]=2)then k3[i,3]:=1;
if(k4[i,3]=3)then k4[i,3]:=2
else if(k4[i,3]=2)then k4[i,3]:=1;
end
else if(k1[i,3]=2) then
begin
k1[i,3]:=3;
if(k2[i,3]=3)then k2[i,3]:=2
else if(k3[i,3]=3) then k3[i,3]:=2
else if(k4[i,3]=3) then k4[i,3]:=2;
end;
end;
//——II=0————————
//——II=1————————
if (ii=1) then
begin
k2[i,1]:=nomer;
k2[i,2]:=StrToInt(Edit2.Text);
if (k2[i,3])=0 then
begin
k2[i,3]:=3;
k1[i,3]:=k1[i,3]-1;
k3[i,3]:=k3[i,3]-1;
k4[i,3]:=k4[i,3]-1;

end
else
if (k2[i,3]=1) then
begin
k2[i,3]:=3;
if(k1[i,3]=3)then k1[i,3]:=2
else if(k1[i,3]=2)then k1[i,3]:=1;
if(k3[i,3]=3)then k3[i,3]:=2
else if(k3[i,3]=2)then k3[i,3]:=1;
if(k4[i,3]=3)then k4[i,3]:=2
else if(k4[i,3]=2)then k4[i,3]:=1;
end
else if(k2[i,3]=2) then
begin
k2[i,3]:=3;
if(k1[i,3]=3)then k1[i,3]:=2
else if(k3[i,3]=3) then k3[i,3]:=2
else if(k4[i,3]=3) then k4[i,3]:=2;
end;
end;
//——II=1————————
//——II=2————————
if (ii=2) then
begin
k3[i,1]:=nomer;
k3[i,2]:=StrToInt(Edit2.Text);
if (k3[i,3])=0 then
begin
k3[i,3]:=3;
k1[i,3]:=k1[i,3]-1;
k2[i,3]:=k2[i,3]-1;
k4[i,3]:=k4[i,3]-1;

end
else
if (k3[i,3]=1) then
begin
k3[i,3]:=3;
if(k1[i,3]=3)then k1[i,3]:=2
else if(k1[i,3]=2)then k1[i,3]:=1;
if(k2[i,3]=3)then k2[i,3]:=2
else if(k2[i,3]=2)then k2[i,3]:=1;
if(k4[i,3]=3)then k4[i,3]:=2
else if(k4[i,3]=2)then k4[i,3]:=1;
end
else if(k3[i,3]=2) then
begin
k3[i,3]:=3;
if(k1[i,3]=3)then k1[i,3]:=2
else if(k2[i,3]=3) then k2[i,3]:=2
else if(k4[i,3]=3) then k4[i,3]:=2;
end;
end;
//——II=2————————
//——II=3————————
if (ii=3) then
begin
k4[i,1]:=nomer;
k4[i,2]:=StrToInt(Edit2.Text);
if (k4[i,3])=0 then
begin
k4[i,3]:=3;
k1[i,3]:=k1[i,3]-1;
k2[i,3]:=k2[i,3]-1;
k3[i,3]:=k3[i,3]-1;

end
else
if (k4[i,3]=1) then
begin
k4[i,3]:=3;
if(k1[i,3]=3)then k1[i,3]:=2
else if(k1[i,3]=2)then k1[i,3]:=1;
if(k2[i,3]=3)then k2[i,3]:=2
else if(k2[i,3]=2)then k2[i,3]:=1;
if(k3[i,3]=3)then k3[i,3]:=2
else if(k3[i,3]=2)then k3[i,3]:=1;
end
else if(k4[i,3]=2) then
begin
k4[i,3]:=3;
if(k1[i,3]=3)then k1[i,3]:=2
else if(k2[i,3]=3) then k2[i,3]:=2
else if(k3[i,3]=3) then k3[i,3]:=2;
end;
end;
StringGrid2.Cells[3,i]:=IntToStr(k1[i,3]);
StringGrid3.Cells[3,i]:=IntToStr(k2[i,3]);
StringGrid4.Cells[3,i]:=IntToStr(k3[i,3]);
StringGrid5.Cells[3,i]:=IntToStr(k4[i,3]);

StringGrid2.Cells[1,i]:=IntToStr(k1[i,1]);
StringGrid2.Cells[2,i]:=IntToStr(k1[i,2]);
StringGrid3.Cells[1,i]:=IntToStr(k2[i,1]);
StringGrid3.Cells[2,i]:=IntToStr(k2[i,2]);
StringGrid4.Cells[1,i]:=IntToStr(k3[i,1]);
StringGrid4.Cells[2,i]:=IntToStr(k3[i,2]);
StringGrid5.Cells[1,i]:=IntToStr(k4[i,1]);
StringGrid5.Cells[2,i]:=IntToStr(k4[i,2]);
end
else
begin
if (k1[i,3]=0)then
begin
k1[i,3]:=3;
k1[i,1]:=nomer;
k1[i,2]:=o;
k2[i,3]:=k2[i,3]-1;
k3[i,3]:=k3[i,3]-1;
k4[i,3]:=k4[i,3]-1;
StringGrid2.Cells[1,i]:=IntToStr(k1[i,1]);
StringGrid2.Cells[2,i]:=IntToStr(k1[i,2]);
end
else
if (k2[i,3]=0) then
begin
k2[i,3]:=3;
k2[i,1]:=nomer;
k2[i,2]:=o;
k1[i,3]:=k1[i,3]-1;
k3[i,3]:=k3[i,3]-1;
k4[i,3]:=k4[i,3]-1;
StringGrid3.Cells[1,i]:=IntToStr(k2[i,1]);
StringGrid3.Cells[2,i]:=IntToStr(k2[i,2]);
end
else
if (k3[i,3]=0) then
begin
k3[i,3]:=3;
k3[i,1]:=nomer;
k3[i,2]:=o;
k1[i,3]:=k1[i,3]-1;
k2[i,3]:=k2[i,3]-1;
k4[i,3]:=k4[i,3]-1;
StringGrid4.Cells[1,i]:=IntToStr(k3[i,1]);
StringGrid4.Cells[2,i]:=IntToStr(k3[i,2]);
end
else
if (k4[i,3]=0) then
begin
k4[i,3]:=3;
k4[i,1]:=nomer;
k4[i,2]:=o;
k1[i,3]:=k1[i,3]-1;
k3[i,3]:=k3[i,3]-1;
k2[i,3]:=k2[i,3]-1;
StringGrid5.Cells[1,i]:=IntToStr(k4[i,1]);
StringGrid5.Cells[2,i]:=IntToStr(k4[i,2]);
end;

StringGrid2.Cells[3,i]:=IntToStr(k1[i,3]);
StringGrid3.Cells[3,i]:=IntToStr(k2[i,3]);
StringGrid4.Cells[3,i]:=IntToStr(k3[i,3]);
StringGrid5.Cells[3,i]:=IntToStr(k4[i,3]);
end;
flag_npoMax:=false;
end;

procedure TForm1.Button5Click(Sender: TObject);
var nomer1,nomer2,i:integer;
begin
sum:=0;
nomer1:=StrToInt(Edit3.Text);
nomer2:=StrToInt(Edit4.Text);
fl:=1;
if(nomer1>nomer2) then ShowMessage(‘ERROR’)else
begin
for i:=nomer1 to nomer2 do
begin
n:=i;
Button1Click(Button1);
end;
end;
label13.Caption:=FloatToStrF((sum*100/(nomer2-nomer1+1)),ffFixed,7,5)+’ %’;
label13.Left:=400;
label13.Left:=label13.Left-67;
fl:=0;

end;

procedure TForm1.Button6Click(Sender: TObject);
var nomer,i:integer;
begin
sum:=0;
nomer:=StrToInt(Edit5.Text);
fl:=1;
for i:=1 to nomer do
begin
n:=random(4096);
Button1Click(Button1);
end;
label16.Caption:=FloatToStrF((sum*100/(nomer)),ffFixed,7,5)+’ %’;
label16.Left:=400;
label16.Left:=label16.Left-67;
fl:=0;
end;

end.


Комментарии запрещены.




Статистика