Subversion Repositories ht46f47_simulator

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. unit MainForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls,
  8.   Vcl.Samples.Spin;
  9.  
  10. type
  11.   TForm2 = class(TForm)
  12.     BtnStart: TButton;
  13.     BtnStop: TButton;
  14.     GroupBox1: TGroupBox;
  15.     Label7: TLabel;
  16.     Label14: TLabel;
  17.     Label13: TLabel;
  18.     Label12: TLabel;
  19.     Label11: TLabel;
  20.     Label10: TLabel;
  21.     Label9: TLabel;
  22.     Label8: TLabel;
  23.     Label6: TLabel;
  24.     Label5: TLabel;
  25.     Label4: TLabel;
  26.     Label3: TLabel;
  27.     Label2: TLabel;
  28.     Label1: TLabel;
  29.     BtnRandom: TButton;
  30.     GroupBox2: TGroupBox;
  31.     CheckBox1: TCheckBox;
  32.     CheckBox2: TCheckBox;
  33.     CheckBox3: TCheckBox;
  34.     CheckBox4: TCheckBox;
  35.     GroupBox3: TGroupBox;
  36.     Panel1: TPanel;
  37.     Panel2: TPanel;
  38.     Panel3: TPanel;
  39.     Panel4: TPanel;
  40.     Label15: TLabel;
  41.     Label16: TLabel;
  42.     SpinEdit2: TSpinEdit;
  43.     SpinEdit1: TSpinEdit;
  44.     Label17: TLabel;
  45.     Label18: TLabel;
  46.     SpinEdit3: TSpinEdit;
  47.     Label19: TLabel;
  48.     CheckBox5: TCheckBox;
  49.     CheckBox6: TCheckBox;
  50.     Memo1: TMemo;
  51.     Label20: TLabel;
  52.     Memo2: TMemo;
  53.     procedure BtnStartClick(Sender: TObject);
  54.     procedure BtnStopClick(Sender: TObject);
  55.     procedure FormShow(Sender: TObject);
  56.     procedure BtnRandomClick(Sender: TObject);
  57.   private
  58.     PleaseStop: boolean;
  59.     procedure ResetGui;
  60.     procedure WaitMs(milliseconds: integer);
  61.   end;
  62.  
  63. var
  64.   Form2: TForm2;
  65.  
  66. implementation
  67.  
  68. {$R *.dfm}
  69.  
  70. uses
  71.   HT46F47;
  72.  
  73. type
  74.   ECodeTooLong = class(Exception);
  75.  
  76. { TForm2 }
  77.  
  78. procedure TForm2.WaitMs(milliseconds: integer);
  79. var
  80.   i: integer;
  81. begin
  82.   for i := 0 to milliseconds div 10 do
  83.   begin
  84.     Sleep(10);
  85.     Application.ProcessMessages;
  86.     if PleaseStop or Application.Terminated then break;
  87.   end;
  88. end;
  89.  
  90. procedure TForm2.BtnStartClick(Sender: TObject);
  91. var
  92.   x: THT46F47;
  93.   i, ci: Integer;
  94.   s: string;
  95.   tmp_din: integer;
  96. resourcestring
  97.   SCodeTooLong = 'Code too long. Max 256 Bytes.';
  98. begin
  99.   BtnStart.Enabled := false;
  100.   BtnStop.Enabled := true;
  101.   Memo2.Enabled := false;
  102.   BtnRandom.Enabled := false;
  103.  
  104.   x := THT46F47.Create;
  105.   try
  106.     x.OnWait := WaitMs;
  107.  
  108.     s := '';
  109.     ci := 0;
  110.     for i := 1 to Length(Memo2.Text) do
  111.     begin
  112.       {$IFDEF UNICODE}
  113.       if CharInSet(Memo2.Text[i], ['a'..'f', 'A'..'F', '0'..'9']) then
  114.       {$ELSE}
  115.       if Memo2.Text[i] in ['a'..'f', 'A'..'F', '0'..'9'] then
  116.       {$ENDIF}
  117.       begin
  118.         s := s + Memo2.Text[i];
  119.       end;
  120.       if Length(s) = 2 then
  121.       begin
  122.         if ci > $FF then raise ECodeTooLong.Create(SCodeTooLong);
  123.         x.ROM[ci] := StrToInt('$'+s);
  124.         s := '';
  125.         Inc(ci);
  126.       end;
  127.     end;
  128.  
  129.     while true do
  130.     begin
  131.       {$REGION 'Input stuff'}
  132.       tmp_din := 0;
  133.       if CheckBox1.Checked then Inc(tmp_din, 1);
  134.       if CheckBox2.Checked then Inc(tmp_din, 2);
  135.       if CheckBox3.Checked then Inc(tmp_din, 4);
  136.       if CheckBox4.Checked then Inc(tmp_din, 8);
  137.       x.DIn := tmp_din;
  138.       x.AD1 := SpinEdit1.Value;
  139.       x.AD2 := SpinEdit2.Value;
  140.       x.S1 := CheckBox5.Checked;
  141.       x.S2 := CheckBox6.Checked;
  142.       {$ENDREGION}
  143.       x.Step;
  144.       {$REGION 'Show output stuff (LEDs)'}
  145.       if ((x.PortOut and 1) shr 0) = 1 then Panel1.Color := clRed else Panel1.Color := clMaroon;
  146.       if ((x.PortOut and 2) shr 1) = 1 then Panel2.Color := clRed else Panel2.Color := clMaroon;
  147.       if ((x.PortOut and 4) shr 2) = 1 then Panel3.Color := clRed else Panel3.Color := clMaroon;
  148.       if ((x.PortOut and 8) shr 3) = 1 then Panel4.Color := clRed else Panel4.Color := clMaroon;
  149.       SpinEdit3.Value := x.PWM;
  150.       {$ENDREGION}
  151.       {$REGION 'Debug output: Internal state'}
  152.       Label8.Caption := IntToHex(x.PC, 2);
  153.       Label2.Caption := IntToHex(x.Page, 1);
  154.       Label4.Caption := IntToHex(x.ReturnAddress, 2);
  155.       Label6.Caption := IntToHex(x.RegA, 1);
  156.       Label10.Caption := IntToHex(x.RegB, 1);
  157.       Label12.Caption := IntToHex(x.RegC, 1);
  158.       Label14.Caption := IntToHex(x.RegD, 1);
  159.       {$ENDREGION}
  160.       Application.ProcessMessages;
  161.       if Application.Terminated or PleaseStop then
  162.       begin
  163.         PleaseStop := false;
  164.         Break;
  165.       end;
  166.     end;
  167.  
  168.     Memo2.Enabled := true;
  169.     BtnRandom.Enabled := true;
  170.     BtnStart.Enabled := true;
  171.     ResetGui;
  172.   finally
  173.     FreeAndNil(x);
  174.   end;
  175. end;
  176.  
  177. procedure TForm2.BtnStopClick(Sender: TObject);
  178. begin
  179.   BtnStop.Enabled := false;
  180.   PleaseStop := true;
  181. end;
  182.  
  183. procedure TForm2.BtnRandomClick(Sender: TObject);
  184. var
  185.   i: integer;
  186. begin
  187.   Memo2.Text := '';
  188.   for i := $00 to $FF do
  189.   begin
  190.     Memo2.Text := Memo2.Text + IntToHex(Random($100),2) + ' ';
  191.   end;
  192.   Memo2.Text := Trim(Memo2.Text);
  193. end;
  194.  
  195. procedure TForm2.FormShow(Sender: TObject);
  196. begin
  197.   ResetGui;
  198. end;
  199.  
  200. procedure TForm2.ResetGui;
  201. begin
  202.   Panel1.Color := clMaroon;
  203.   Panel2.Color := clMaroon;
  204.   Panel3.Color := clMaroon;
  205.   Panel4.Color := clMaroon;
  206. end;
  207.  
  208. end.
  209.  
  210.