Subversion Repositories ht46f47_simulator

Rev

Blame | 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.   end;
  61.  
  62. var
  63.   Form2: TForm2;
  64.  
  65. implementation
  66.  
  67. {$R *.dfm}
  68.  
  69. uses
  70.   HT46F47;
  71.  
  72. type
  73.   ECodeTooLong = class(Exception);
  74.  
  75. { TForm2 }
  76.  
  77. procedure TForm2.BtnStartClick(Sender: TObject);
  78. var
  79.   x: THT46F47;
  80.   i, ci: Integer;
  81.   s: string;
  82.   tmp_din: integer;
  83. resourcestring
  84.   SCodeTooLong = 'Code too long. Max 256 Bytes.';
  85. begin
  86.   BtnStart.Enabled := false;
  87.   BtnStop.Enabled := true;
  88.   Memo2.Enabled := false;
  89.   BtnRandom.Enabled := false;
  90.  
  91.   x := THT46F47.Create;
  92.  
  93.   s := '';
  94.   ci := 0;
  95.   for i := 1 to Length(Memo2.Text) do
  96.   begin
  97.     if Memo2.Text[i] in ['a'..'f', 'A'..'F', '0'..'9'] then
  98.     begin
  99.       s := s + Memo2.Text[i];
  100.     end;
  101.     if Length(s) = 2 then
  102.     begin
  103.       if ci > $FF then raise ECodeTooLong.Create(SCodeTooLong);
  104.       x.ROM[ci] := StrToInt('$'+s);
  105.       s := '';
  106.       Inc(ci);
  107.     end;
  108.   end;
  109.  
  110.   while true do
  111.   begin
  112.     {$REGION 'Input stuff'}
  113.     tmp_din := 0;
  114.     if CheckBox1.Checked then Inc(tmp_din, 1);
  115.     if CheckBox2.Checked then Inc(tmp_din, 2);
  116.     if CheckBox3.Checked then Inc(tmp_din, 4);
  117.     if CheckBox4.Checked then Inc(tmp_din, 8);
  118.     x.DIn := tmp_din;
  119.     x.AD1 := SpinEdit1.Value;
  120.     x.AD2 := SpinEdit2.Value;
  121.     x.S1 := CheckBox5.Checked;
  122.     x.S2 := CheckBox6.Checked;
  123.     {$ENDREGION}
  124.     x.Step;
  125.     {$REGION 'Show output stuff (LEDs)'}
  126.     if ((x.PortOut and 1) shr 0) = 1 then Panel1.Color := clRed else Panel1.Color := clMaroon;
  127.     if ((x.PortOut and 2) shr 1) = 1 then Panel2.Color := clRed else Panel2.Color := clMaroon;
  128.     if ((x.PortOut and 4) shr 2) = 1 then Panel3.Color := clRed else Panel3.Color := clMaroon;
  129.     if ((x.PortOut and 8) shr 3) = 1 then Panel4.Color := clRed else Panel4.Color := clMaroon;
  130.     SpinEdit3.Value := x.PWM;
  131.     {$ENDREGION}
  132.     {$REGION 'Debug output: Internal state'}
  133.     Label8.Caption := IntToHex(x.PC, 2);
  134.     Label2.Caption := IntToHex(x.Page, 1);
  135.     Label4.Caption := IntToHex(x.ReturnAddress, 2);
  136.     Label6.Caption := IntToHex(x.RegA, 1);
  137.     Label10.Caption := IntToHex(x.RegB, 1);
  138.     Label12.Caption := IntToHex(x.RegC, 1);
  139.     Label14.Caption := IntToHex(x.RegD, 1);
  140.     {$ENDREGION}
  141.     Application.ProcessMessages;
  142.     if Application.Terminated or PleaseStop then
  143.     begin
  144.       PleaseStop := false;
  145.       Break;
  146.     end;
  147.   end;
  148.  
  149.   Memo2.Enabled := true;
  150.   BtnRandom.Enabled := true;
  151.   BtnStart.Enabled := true;
  152.   ResetGui;
  153. end;
  154.  
  155. procedure TForm2.BtnStopClick(Sender: TObject);
  156. begin
  157.   // TODO: actually we need to inform the THT46F47 object, so it can break out of a possible waiting loop
  158.   BtnStop.Enabled := false;
  159.   PleaseStop := true;
  160. end;
  161.  
  162. procedure TForm2.BtnRandomClick(Sender: TObject);
  163. var
  164.   i: integer;
  165. begin
  166.   Memo2.Text := '';
  167.   for i := $00 to $FF do
  168.   begin
  169.     Memo2.Text := Memo2.Text + IntToHex(Random($100),2) + ' ';
  170.   end;
  171.   Memo2.Text := Trim(Memo2.Text);
  172. end;
  173.  
  174. procedure TForm2.FormShow(Sender: TObject);
  175. begin
  176.   ResetGui;
  177. end;
  178.  
  179. procedure TForm2.ResetGui;
  180. begin
  181.   Panel1.Color := clMaroon;
  182.   Panel2.Color := clMaroon;
  183.   Panel3.Color := clMaroon;
  184.   Panel4.Color := clMaroon;
  185. end;
  186.  
  187. end.
  188.  
  189.