Ignore:
Timestamp:
Apr 10, 2019, 11:49:27 PM (6 years ago)
Author:
chronos
Message:
  • Added: Build modes.
  • Fixed: Background thread code should not access main thread visual controls.
Location:
branches/virtualcpu4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/virtualcpu4

    • Property svn:ignore
      •  

        old new  
        11lib
        22virtucpu4.exe
         3virtucpu4
        34*.lps
        45*.res
  • branches/virtualcpu4/UFormMain.pas

    r171 r172  
    77uses
    88  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
    9   ComCtrls, UCpu, UInstructionWriter;
     9  ComCtrls, UCpu, UInstructionWriter, syncobjs;
    1010
    1111type
     
    3232    procedure Timer1Timer(Sender: TObject);
    3333  private
    34     KeyInputBuffer: array of Char;
     34    InputBuffer: string;
     35    OutputBuffer: string;
     36    Lock: TCriticalSection;
    3537    function CpuInput(Port: TAddress): TRegister;
    3638    procedure CpuOutput(Port: TAddress; Value: TRegister);
     
    6769procedure TFormMain.FormCreate(Sender: TObject);
    6870begin
     71  Lock := TCriticalSection.Create;
    6972  Cpu := TCpu.Create;
    7073  Cpu.OnInput := CpuInput;
     
    8083  InstructionWriter.Free;
    8184  Cpu.Free;
     85  Lock.Free;
    8286end;
    8387
     
    120124procedure TFormMain.Memo1KeyPress(Sender: TObject; var Key: char);
    121125begin
    122   SetLength(KeyInputBuffer, Length(KeyInputBuffer) + 1);
    123   KeyInputBuffer[High(KeyInputBuffer)] := Key;
     126  Lock.Acquire;
     127  InputBuffer := InputBuffer + Key;
     128  Lock.Release;
    124129end;
    125130
     
    129134  ReloadMemoryDump;
    130135  ReloadRegisterDump;
     136  Lock.Acquire;
     137  Memo1.Lines.Text := Memo1.Lines.Text + OutputBuffer;
     138  SetLength(OutputBuffer, 0);
     139  Lock.Release;
    131140end;
    132141
     
    186195  case Port of
    187196    0: begin
    188       while (Length(KeyInputBuffer) = 0) and not Cpu.Terminated do begin
     197      Lock.Acquire;
     198      while (Length(InputBuffer) = 0) and not Cpu.Terminated do begin
     199        Lock.Release;
    189200        Sleep(100);
     201        Lock.Acquire;
    190202      end;
    191       if Length(KeyInputBuffer) > 0 then begin
    192         Result.B := Ord(KeyInputBuffer[0]);
    193         if Length(KeyInputBuffer) > 1 then
    194           Move(KeyInputBuffer[1], KeyInputBuffer[0], Length(KeyInputBuffer) - 1);
    195         SetLength(KeyInputBuffer, Length(KeyInputBuffer) - 1);
     203      if Length(InputBuffer) > 0 then begin
     204        Result.B := Ord(InputBuffer[1]);
     205        Delete(InputBuffer, 1, 1);
    196206      end else Result.B := 0;
     207      Lock.Release;
    197208    end;
    198209  end;
     
    202213begin
    203214  case Port of
    204     0: Memo1.Lines.Text := Memo1.Lines.Text + Char(Value.B);
     215    0: begin
     216      Lock.Acquire;
     217      OutputBuffer := OutputBuffer + Char(Value.B);
     218      Lock.Release;
     219    end;
    205220  end;
    206221end;
Note: See TracChangeset for help on using the changeset viewer.