Ignore:
Timestamp:
Jul 12, 2022, 10:43:40 PM (2 years ago)
Author:
chronos
Message:
  • Modified: More instructions.
  • Modified: Optimized instruction execution with procedure array instead case.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/CpuSingleSize/UMachine.pas

    r220 r223  
    11unit UMachine;
    2 
    3 {$mode delphi}
    42
    53interface
     
    97
    108type
    11   TDeviceClass = (dcNone, dcKeyboard, dcMouse, dcStorage, dcScreen, dcConsole);
     9  TDeviceClass = (dcNone, dcKeyboard, dcMouse, dcStorage, dcScreen, dcConsole,
     10    dcTimer);
    1211  TDeviceClassSet = set of TDeviceClass;
    1312
     
    3534    InterruptVector: Integer;
    3635    Form: TFormDevice;
     36    procedure PulseInterrupt;
    3737    procedure OutputHandler(Port, Data: TInteger); virtual;
    3838    function InputHandler(Port: TInteger): TInteger; virtual;
     
    6464  TConsoleOutputPort = (opConsoleWriteChar);
    6565
    66   { TConsole }
    67 
    68   TConsole = class(TDevice)
     66  { TDeviceConsole }
     67
     68  TDeviceConsole = class(TDevice)
    6969    Lock: TCriticalSection;
    7070    InputBuffer: TQueue<TInteger>;
     
    7979  TScreenOutputPort = (opScreenSetAddr, opScreenWrite);
    8080
    81   { TScreen }
    82 
    83   TScreen = class(TDevice)
     81  { TDeviceScreen }
     82
     83  TDeviceScreen = class(TDevice)
    8484  private
    8585    FSize: TPoint;
     
    9797  TKeyboardInputPort = (ipKeyboardRead);
    9898
    99   { TKeyboard }
    100 
    101   TKeyboard = class(TDevice)
     99  { TDeviceKeyboard }
     100
     101  TDeviceKeyboard = class(TDevice)
    102102    constructor Create;
    103103    function ReadKey: TInteger;
     
    108108  TStorageOutputPort = (opStorageSetAddr, opStorageWrite);
    109109
    110   { TStorage }
    111 
    112   TStorage = class(TDevice)
     110  { TDeviceStorage }
     111
     112  TDeviceStorage = class(TDevice)
    113113  public
    114114    F: TFileStream;
     
    119119  end;
    120120
    121   { TMouse }
    122 
    123   TMouse = class(TDevice)
    124     constructor Create;
     121  { TDeviceMouse }
     122
     123  TDeviceMouse = class(TDevice)
     124    constructor Create;
     125  end;
     126
     127  TTimerOutputPort = (opTimerSetInterval, opTimerSetEnabled);
     128  { TDeviceTimer }
     129
     130  TDeviceTimer = class(TDevice)
     131    Interval: Integer;
     132    Enabled: Boolean;
     133    constructor Create;
     134    procedure OutputHandler(Port, Data: TInteger); override;
    125135  end;
    126136
     
    144154
    145155const
    146   DeviceClassText: array[TDeviceClass] of string = ('None', 'Keyboard', 'Mouse', 'Storage', 'Screen', 'Console');
     156  DeviceClassText: array[TDeviceClass] of string = ('None', 'Keyboard', 'Mouse', 'Storage', 'Screen', 'Console', 'Timer');
    147157
    148158
    149159implementation
     160
     161{ TDeviceTimer }
     162
     163constructor TDeviceTimer.Create;
     164begin
     165  DeviceClass := dcTimer;
     166end;
     167
     168procedure TDeviceTimer.OutputHandler(Port, Data: TInteger);
     169begin
     170  case TTimerOutputPort(Port) of
     171    opTimerSetInterval: Interval := Data;
     172    opTimerSetEnabled: Enabled := Data > 0;
     173  end;
     174end;
    150175
    151176{ TDeviceManager }
     
    225250{ TDevice }
    226251
     252procedure TDevice.PulseInterrupt;
     253begin
     254  Cpu.Interrupt(InterruptVector);
     255end;
     256
    227257procedure TDevice.OutputHandler(Port, Data: TInteger);
    228258begin
     
    234264end;
    235265
    236 { TMouse }
    237 
    238 constructor TMouse.Create;
     266{ TDeviceMouse }
     267
     268constructor TDeviceMouse.Create;
    239269begin
    240270  DeviceClass := dcMouse;
    241271end;
    242272
    243 { TStorage }
    244 
    245 constructor TStorage.Create;
     273{ TDeviceStorage }
     274
     275constructor TDeviceStorage.Create;
    246276begin
    247277  DeviceClass := dcStorage;
    248278end;
    249279
    250 procedure TStorage.OutputHandler(Port, Data: TInteger);
     280procedure TDeviceStorage.OutputHandler(Port, Data: TInteger);
    251281begin
    252282  case TStorageOutputPort(Port) of
     
    258288end;
    259289
    260 function TStorage.InputHandler(Port: TInteger): TInteger;
     290function TDeviceStorage.InputHandler(Port: TInteger): TInteger;
    261291begin
    262292  case TStorageInputPort(Port) of
     
    268298end;
    269299
    270 { TConsole }
    271 
    272 constructor TConsole.Create;
     300{ TDeviceConsole }
     301
     302constructor TDeviceConsole.Create;
    273303begin
    274304  DeviceClass := dcConsole;
     
    278308end;
    279309
    280 destructor TConsole.Destroy;
     310destructor TDeviceConsole.Destroy;
    281311begin
    282312  InputBuffer.Free;
     
    286316end;
    287317
    288 procedure TConsole.OutputHandler(Port, Data: TInteger);
     318procedure TDeviceConsole.OutputHandler(Port, Data: TInteger);
    289319begin
    290320  case TConsoleOutputPort(Port) of
     
    300330end;
    301331
    302 function TConsole.InputHandler(Port: TInteger): TInteger;
     332function TDeviceConsole.InputHandler(Port: TInteger): TInteger;
    303333begin
    304334  case TConsoleInputPort(Port) of
     
    324354end;
    325355
    326 { TScreen }
    327 
    328 procedure TScreen.SetSize(AValue: TPoint);
     356{ TDeviceScreen }
     357
     358procedure TDeviceScreen.SetSize(AValue: TPoint);
    329359begin
    330360  if FSize = AValue then Exit;
     
    333363end;
    334364
    335 constructor TScreen.Create;
     365constructor TDeviceScreen.Create;
    336366begin
    337367  DeviceClass := dcScreen;
     
    339369end;
    340370
    341 procedure TScreen.OutputHandler(Port, Data: TInteger);
     371procedure TDeviceScreen.OutputHandler(Port, Data: TInteger);
    342372begin
    343373  case TScreenOutputPort(Port) of
     
    351381end;
    352382
    353 function TScreen.InputHandler(Port: TInteger): TInteger;
     383function TDeviceScreen.InputHandler(Port: TInteger): TInteger;
    354384begin
    355385  case TScreenInputPort(Port) of
     
    359389end;
    360390
    361 { TKeyboard }
    362 
    363 function TKeyboard.ReadKey: TInteger;
     391{ TDeviceKeyboard }
     392
     393function TDeviceKeyboard.ReadKey: TInteger;
    364394begin
    365395  Result := 0;
    366396end;
    367397
    368 function TKeyboard.InputHandler(Port: TInteger): TInteger;
     398function TDeviceKeyboard.InputHandler(Port: TInteger): TInteger;
    369399begin
    370400  case TKeyboardInputPort(Port) of
     
    373403end;
    374404
    375 constructor TKeyboard.Create;
     405constructor TDeviceKeyboard.Create;
    376406begin
    377407  DeviceClass := dcKeyboard;
     
    404434procedure TMachine.InitDevices;
    405435var
    406   Screen: TScreen;
    407   Keyboard: TKeyboard;
    408   Console: TConsole;
    409   Storage: TStorage;
    410   Mouse: TMouse;
     436  Screen: TDeviceScreen;
     437  Keyboard: TDeviceKeyboard;
     438  Console: TDeviceConsole;
     439  Storage: TDeviceStorage;
     440  Mouse: TDeviceMouse;
    411441  DeviceManager: TDeviceManager;
     442  Timer: TDeviceTimer;
    412443begin
    413444  DeviceManager := TDeviceManager.Create;
     
    415446  RegisterDevice(DeviceManager);
    416447
    417   Console := TConsole.Create;
     448  Console := TDeviceConsole.Create;
    418449  RegisterDevice(Console);
    419450  Console.InterruptVector := 1;
    420451
    421   Keyboard := TKeyboard.Create;
     452  Keyboard := TDeviceKeyboard.Create;
     453  Keyboard.InterruptVector := 4;
    422454  RegisterDevice(Keyboard);
    423455
    424   Screen := TScreen.Create;
     456  Screen := TDeviceScreen.Create;
    425457  RegisterDevice(Screen);
    426458
    427   Storage := TStorage.Create;
     459  Storage := TDeviceStorage.Create;
    428460  RegisterDevice(Storage);
    429461
    430   Storage := TStorage.Create;
     462  Storage := TDeviceStorage.Create;
    431463  RegisterDevice(Storage);
    432464
    433   Mouse := TMouse.Create;
     465  Mouse := TDeviceMouse.Create;
     466  Mouse.InterruptVector := 3;
    434467  RegisterDevice(Mouse);
     468
     469  Timer := TDeviceTimer.Create;
     470  Timer.InterruptVector := 2;
     471  RegisterDevice(Timer);
    435472end;
    436473
    437474procedure TMachine.PowerOn;
    438475begin
    439   Cpu.Data := Memory.Data;
     476  Cpu.Memory := Memory.Data;
    440477  Cpu.Start;
    441478end;
Note: See TracChangeset for help on using the changeset viewer.