I use it to create a simulation about digital asynchronous binary n-bit counter, complete with the denary representation.
n-bit means it's very flexible, you can change n and its output (stringgrid, thats it) automatically adjust itself, :)
Here's the code
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, Grids, math;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
BitBtn1: TBitBtn;
function denary:integer;
function tostring(a:boolean):string;
procedure counter(m:integer);
procedure proses;
procedure isiStringgrid;
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const n=3;
var
Form1: TForm1;
Q:array[0..(n-1)]of boolean;
clock:boolean=true;
l,denary:integer;
running:boolean=false;
implementation
{$R *.dfm}
function tform1.denary:integer;
var i,j:integer;
begin
j:=0;
for i:=0 to n-1 do begin
j:=j+round(power(2,i))*strtoint(tostring(Q[i]));
end;
denary:=j;
end;
function tform1.tostring(a:boolean):string;
begin
{}
tostring:=inttostr(-1*strtoint(booltostr(a)))
end;
procedure tform1.counter(m:integer);
begin
if l<=n-1 then begin
Q[l]:=not Q[l];
if Q[l]=false then begin
l:=l+1;
counter(l);
end;
end;
end;
procedure tform1.proses;
begin
clock:= not clock;
if clock=false then begin
l:=0;
counter(l);
isiStringgrid;
end;
stringgrid1.Cells[1,1]:=tostring(clock);
end;
procedure tform1.isiStringgrid;
var i:integer;
begin
for i:=0 to n-1 do begin
stringgrid1.Cells[2+i,1]:=tostring(Q[i]);
end;
stringgrid1.Cells[n+2,1]:=inttostr(denary);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
running:=not running;
if running=true then bitbtn1.Caption:='stop'else bitbtn1.Caption:='run';
while running=true do begin
proses;
application.ProcessMessages;sleep(500);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
stringgrid1.ColCount:=n+3;
bitbtn1.Caption:='run';
stringgrid1.Cells[1,0]:='clock';
stringgrid1.Cells[1,1]:=tostring(clock);
for i:=0 to n-1 do begin
Q[i]:=false;
stringgrid1.Cells[2+i,0]:='Q'+inttostr(i);
stringgrid1.Cells[2+i,1]:=tostring(Q[i]);
end;
stringgrid1.Cells[n+2,0]:='Denary';
stringgrid1.Cells[n+2,1]:=inttostr(denary);
end;
end.
The screenshot.
And the result, :)
No comments:
Post a Comment