Changeset 42


Ignore:
Timestamp:
Sep 8, 2023, 11:20:27 PM (15 months ago)
Author:
chronos
Message:
  • Modified: Improved simple virtual machine.
Location:
branches/simple
Files:
20 added
8 edited
3 moved

Legend:

Unmodified
Added
Removed
  • branches/simple/Cpu.pas

    r41 r42  
    44
    55uses
    6   Classes, SysUtils, DataBus;
     6  Classes, SysUtils, Channel;
    77
    88type
    9   TInstruction = (inLoadImmediate8, inLoadImmediate16, inLoad8, inLoad16,
    10     inStore8, inStore16, inInput8, inInput16, inOutput8, inOutput16,
    11     inHalt, inJump);
     9  TInstruction = (inLoadImmediate, inLoadImmediate8, inLoadImmediate16,
     10    inLoad, inLoad8, inLoad16, inLoad32, inLoad64,
     11    inStore, inStore8, inStore16, inStore32, inStore64,
     12    inInput, inInput8, inInput16, inInput32, inInput64,
     13    inOutput, inOutput8, inOutput16, inOutput32, inOutput64,
     14    inNop, inHalt, inJump);
    1215
    1316  { TCpu8 }
     
    1619  private
    1720  public
    18     IO: TDataBus8;
    19     Memory: TDataBus8;
     21    IO: TAddressableChannel8;
     22    Memory: TAddressableChannel8;
    2023    Terminated: Boolean;
    2124    A: Byte;
     
    2528    procedure Run;
    2629    procedure Step;
     30    constructor Create;
     31    destructor Destroy; override;
    2732  end;
    2833
     
    3237  private
    3338  public
    34     IO: TDataBus16;
    35     Memory: TDataBus16;
     39    IO: TAddressableChannel16;
     40    Memory: TAddressableChannel16;
    3641    Terminated: Boolean;
    3742    A: Word;
     
    4247    procedure Run;
    4348    procedure Step;
     49    constructor Create;
     50    destructor Destroy; override;
     51  end;
     52
     53  TCpu = class
     54    BitWidth: TBitWidth;
     55    Cpu8: TCpu8;
     56    Cpu16: TCpu16;
     57  end;
     58
     59  TRegMulti = record
     60    case Byte of
     61      0: (ValueByte: Byte);
     62      1: (ValueWord: Word);
     63  end;
     64
     65  { TCpuMulti }
     66
     67  TCpuMulti = class
     68  private
     69  public
     70    IO: TAddressableChannelMulti;
     71    Memory: TAddressableChannelMulti;
     72    Terminated: Boolean;
     73    A: TRegMulti;
     74    PC: TRegMulti;
     75    BitWidth: TBitWidth;
     76    function ReadProg8: Byte;
     77    function ReadProg16: Word;
     78    procedure Reset;
     79    procedure Run;
     80    procedure Step;
     81    constructor Create;
     82    destructor Destroy; override;
    4483  end;
    4584
    4685
    4786implementation
     87
     88{ TCpuMulti }
     89
     90function TCpuMulti.ReadProg8: Byte;
     91begin
     92  case BitWidth of
     93    bw8: begin
     94      Result := Memory.ReadA8D8(PC.ValueByte);
     95      Inc(PC.ValueByte);
     96    end;
     97    bw16: begin
     98      Result := Memory.ReadA16D8(PC.ValueWord);
     99      Inc(PC.ValueWord);
     100    end;
     101  end;
     102end;
     103
     104function TCpuMulti.ReadProg16: Word;
     105begin
     106  case BitWidth of
     107    //bw8: begin
     108    //  Result := Memory.ReadA8D16(PC.ValueByte);
     109    //  Inc(PC.ValueByte);
     110    //end;
     111    bw16: begin
     112      Result := Memory.ReadA16D16(PC.ValueWord);
     113      Inc(PC.ValueWord);
     114    end;
     115  end;
     116end;
     117
     118procedure TCpuMulti.Reset;
     119begin
     120  Terminated := False;
     121  PC.ValueWord := 0;
     122end;
     123
     124procedure TCpuMulti.Run;
     125begin
     126  while not Terminated do
     127    Step;
     128end;
     129
     130procedure TCpuMulti.Step;
     131var
     132  Instruction: TInstruction;
     133begin
     134  Instruction := TInstruction(ReadProg8);
     135  case Instruction of
     136    inNop: ;
     137    inHalt: Terminated := True;
     138    inLoadImmediate8: A.ValueByte := ReadProg8;
     139    inLoadImmediate16: A.ValueWord := ReadProg16;
     140    inLoadImmediate: case BitWidth of
     141      bw8: A.ValueByte := ReadProg8;
     142      bw16: A.ValueWord := ReadProg16;
     143    end;
     144    inLoad8: case BitWidth of
     145      bw8: A.ValueByte := Memory.ReadA8D8(ReadProg8);
     146      bw16: A.ValueByte := Memory.ReadA16D8(ReadProg16);
     147    end;
     148    inLoad16: case BitWidth of
     149      //bw8: A.ValueWord := Memory.ReadA8D16(ReadProg8);
     150      bw16: A.ValueWord := Memory.ReadA16D16(ReadProg16);
     151    end;
     152    inLoad: case BitWidth of
     153      bw8: A.ValueByte := Memory.ReadA8D8(ReadProg8);
     154      bw16: A.ValueWord := Memory.ReadA16D16(ReadProg16);
     155    end;
     156    inStore8: case BitWidth of
     157      bw8: Memory.WriteA8D8(ReadProg8, A.ValueByte);
     158      bw16: Memory.WriteA16D8(ReadProg16, A.ValueByte);
     159    end;
     160    inStore16: case BitWidth of
     161      //bw8: Memory.WriteA8D16(ReadProg8, A.ValueWord);
     162      bw16: Memory.WriteA16D16(ReadProg16, A.ValueWord);
     163    end;
     164    inStore: case BitWidth of
     165      bw8: Memory.WriteA8D8(ReadProg8, A.ValueByte);
     166      bw16: Memory.WriteA16D16(ReadProg16, A.ValueWord);
     167    end;
     168    inInput8: case BitWidth of
     169      bw8: A.ValueByte := IO.ReadA8D8(ReadProg8);
     170      bw16: A.ValueByte := IO.ReadA16D8(ReadProg16);
     171    end;
     172    inInput16: case BitWidth of
     173      //bw8: A.ValueWord := IO.ReadA8D16(ReadProg8);
     174      bw16: A.ValueWord := IO.ReadA16D16(ReadProg16);
     175    end;
     176    inInput: case BitWidth of
     177      bw8: A.ValueByte := IO.ReadA8D8(ReadProg8);
     178      bw16: A.ValueWord := IO.ReadA16D16(ReadProg16);
     179    end;
     180    inOutput8: case BitWidth of
     181      bw8: IO.WriteA8D8(ReadProg8, A.ValueByte);
     182      bw16: IO.WriteA16D8(ReadProg16, A.ValueWord);
     183    end;
     184    inOutput16: case BitWidth of
     185      //bw8: IO.WriteA8D16(ReadProg8, A.ValueWord);
     186      bw16: IO.WriteA16D16(ReadProg16, A.ValueWord);
     187    end;
     188    inOutput: case BitWidth of
     189      bw8: IO.WriteA8D8(ReadProg8, A.ValueByte);
     190      bw16: IO.WriteA16D16(ReadProg16, A.ValueWord);
     191    end;
     192    inJump: case BitWidth of
     193      bw8: PC.ValueByte := ReadProg8;
     194      bw16: PC.ValueWord := ReadProg16;
     195    end;
     196  end;
     197end;
     198
     199constructor TCpuMulti.Create;
     200begin
     201  IO := TAddressableChannelMulti.Create;
     202  Memory := TAddressableChannelMulti.Create;
     203end;
     204
     205destructor TCpuMulti.Destroy;
     206begin
     207  FreeAndNil(IO);
     208  FreeAndNil(Memory);
     209  inherited;
     210end;
    48211
    49212{ TCpu16 }
     
    58221begin
    59222  Result := Memory.Read16(PC);
    60   Inc(PC, 2);
     223  Inc(PC, SizeOf(Word));
    61224end;
    62225
     
    79242  Instruction := TInstruction(ReadProg8);
    80243  case Instruction of
     244    inNop: ;
    81245    inHalt: Terminated := True;
    82     inLoadImmediate8: A := ReadProg8;
     246    inLoadImmediate8: A := (A and $ff00) or ReadProg8;
    83247    inLoadImmediate16: A := ReadProg16;
    84     inLoad8: A := Memory.Read8(ReadProg16);
     248    inLoadImmediate: A := ReadProg16;
     249    inLoad8: A := (A and $ff00) or Memory.Read8(ReadProg16);
    85250    inLoad16: A := Memory.Read16(ReadProg16);
     251    inLoad: A := Memory.Read16(ReadProg16);
    86252    inStore8: Memory.Write8(ReadProg16, A);
    87253    inStore16: Memory.Write16(ReadProg16, A);
    88     inInput8: A := IO.Read8(ReadProg16);
     254    inStore: Memory.Write16(ReadProg16, A);
     255    inInput8: A := (A and $ff00) or IO.Read8(ReadProg16);
    89256    inInput16: A := IO.Read16(ReadProg16);
     257    inInput: A := IO.Read16(ReadProg16);
    90258    inOutput8: IO.Write8(ReadProg16, A);
    91259    inOutput16: IO.Write16(ReadProg16, A);
     260    inOutput: IO.Write16(ReadProg16, A);
    92261    inJump: PC := ReadProg16;
    93262  end;
     263end;
     264
     265constructor TCpu16.Create;
     266begin
     267  IO := TAddressableChannel16.Create;
     268  Memory := TAddressableChannel16.Create;
     269end;
     270
     271destructor TCpu16.Destroy;
     272begin
     273  FreeAndNil(IO);
     274  FreeAndNil(Memory);
     275  inherited;
    94276end;
    95277
     
    120302  Instruction := TInstruction(ReadProg8);
    121303  case Instruction of
    122     inLoadImmediate8: A := ReadProg8;
    123     inLoad8: A := Memory.Read8(ReadProg8);
    124     inStore8: Memory.Write8(ReadProg8, A);
    125     inInput8: A := IO.Read8(ReadProg8);
    126     inOutput8: IO.Write8(ReadProg8, A);
     304    inNop: ;
     305    inLoadImmediate, inLoadImmediate8: A := ReadProg8;
     306    inLoad, inLoad8: A := Memory.Read8(ReadProg8);
     307    inStore, inStore8: Memory.Write8(ReadProg8, A);
     308    inInput, inInput8: A := IO.Read8(ReadProg8);
     309    inOutput, inOutput8: IO.Write8(ReadProg8, A);
    127310    inHalt: Terminated := True;
    128311    inJump: PC := ReadProg8;
     
    130313end;
    131314
     315constructor TCpu8.Create;
     316begin
     317  IO := TAddressableChannel8.Create;
     318  Memory := TAddressableChannel8.Create;
     319end;
     320
     321destructor TCpu8.Destroy;
     322begin
     323  FreeAndNil(IO);
     324  FreeAndNil(Memory);
     325  inherited;
     326end;
     327
    132328end.
    133329
  • branches/simple/Devices/Console.pas

    r41 r42  
    44
    55uses
    6   Classes, SysUtils, Device, DeviceMapper;
     6  Classes, SysUtils, Device, DeviceManager, Channel;
    77
    88type
    9   { TConsole }
    109
    11   TConsole = class(TDevice16)
     10  { TConsole8 }
     11
     12  TConsole8 = class(TDevice8)
    1213  private
    1314    FOnRead: TReadEvent8;
     
    1617    function Read8: Byte;
    1718  public
    18     procedure RegisterMapper8(Mapper: TDeviceMapper8); override;
    19     procedure RegisterMapper16(Mapper: TDeviceMapper16); override;
     19    function GetHandlerCount: Integer; override;
     20    function GetHandler(Address: Integer): TChannel8; override;
     21    property OnWrite: TWriteEvent8 read FOnWrite write FOnWrite;
     22    property OnRead: TReadEvent8 read FOnRead write FOnRead;
     23  end;
     24
     25  { TConsole16 }
     26
     27  TConsole16 = class(TDevice16)
     28  private
     29    FOnRead: TReadEvent8;
     30    FOnWrite: TWriteEvent8;
     31    procedure Write8(Data: Byte);
     32    function Read8: Byte;
     33  public
     34    function GetHandlerCount: Integer; override;
     35    function GetHandler(Address: Integer): TChannel16; override;
    2036    property OnWrite: TWriteEvent8 read FOnWrite write FOnWrite;
    2137    property OnRead: TReadEvent8 read FOnRead write FOnRead;
     
    2541implementation
    2642
    27 { TConsole }
     43{ TConsole8 }
    2844
    29 procedure TConsole.RegisterMapper8(Mapper: TDeviceMapper8);
    30 begin
    31   Mapper.RegisterReadHandler(Read8);
    32   Mapper.RegisterWriteHandler(Write8);
    33 end;
    34 
    35 procedure TConsole.RegisterMapper16(Mapper: TDeviceMapper16);
    36 begin
    37   Mapper.RegisterReadHandler(Read8, nil);
    38   Mapper.RegisterWriteHandler(Write8, nil);
    39 end;
    40 
    41 procedure TConsole.Write8(Data: Byte);
     45procedure TConsole8.Write8(Data: Byte);
    4246begin
    4347  if Assigned(FOnWrite) then FOnWrite(Data);
    4448end;
    4549
    46 function TConsole.Read8: Byte;
     50function TConsole8.Read8: Byte;
    4751begin
    4852  if Assigned(FOnRead) then Result := FOnRead
     
    5054end;
    5155
     56function TConsole8.GetHandlerCount: Integer;
     57begin
     58  Result := 1;
     59end;
     60
     61function TConsole8.GetHandler(Address: Integer): TChannel8;
     62begin
     63  if Address = 0 then begin
     64    Result := TChannel8.Create;
     65    Result.Read8 := Read8;
     66    Result.Write8 := Write8;
     67  end;
     68end;
     69
     70{ TConsole16 }
     71
     72procedure TConsole16.Write8(Data: Byte);
     73begin
     74  if Assigned(FOnWrite) then FOnWrite(Data);
     75end;
     76
     77function TConsole16.Read8: Byte;
     78begin
     79  if Assigned(FOnRead) then Result := FOnRead
     80    else Result := 0;
     81end;
     82
     83function TConsole16.GetHandlerCount: Integer;
     84begin
     85  Result := 1;
     86end;
     87
     88function TConsole16.GetHandler(Address: Integer): TChannel16;
     89begin
     90  if Address = 0 then begin
     91    Result := TChannel16.Create;
     92    Result.Read8 := Read8;
     93    Result.Write8 := Write8;
     94  end;
     95end;
    5296
    5397end.
  • branches/simple/Devices/Device.pas

    r41 r42  
    44
    55uses
    6   Classes, SysUtils, DeviceMapper;
     6  Classes, SysUtils, Channel, Generics.Collections, Forms;
    77
    88type
     9  TDeviceClass = (dcNone, dcKeyboard, dcMouse, dcStorage, dcScreen, dcConsole,
     10    dcTimer);
     11  TDeviceClassSet = set of TDeviceClass;
     12
    913  { TDevice8 }
    1014
    1115  TDevice8 = class
    12     procedure RegisterMapper8(Mapper: TDeviceMapper8); virtual; abstract;
     16    BaseAddress: Byte;
     17    procedure SetDataBus(Channel: TAddressableChannel8); virtual;
     18    function GetHandlerCount: Integer; virtual;
     19    function GetHandler(Address: Integer): TChannel8; virtual;
    1320  end;
    1421
     
    1623
    1724  TDevice16 = class
    18     procedure RegisterMapper8(Mapper: TDeviceMapper8); virtual; abstract;
    19     procedure RegisterMapper16(Mapper: TDeviceMapper16); virtual; abstract;
     25    BaseAddress: Word;
     26    procedure SetDataBus(Channel: TAddressableChannel16); virtual;
     27    function GetHandlerCount: Integer; virtual;
     28    function GetHandler(Address: Integer): TChannel16; virtual;
    2029  end;
     30
     31  { TDevice32 }
     32
     33  TDevice32 = class
     34    BaseAddress: DWord;
     35    procedure SetDataBus(Channel: TAddressableChannel32); virtual;
     36    function GetHandlerCount: Integer; virtual;
     37    function GetHandler(Address: Integer): TChannel32; virtual;
     38  end;
     39
     40  { TDevice64 }
     41
     42  TDevice64 = class
     43    BaseAddress: QWord;
     44    procedure SetDataBus(Channel: TAddressableChannel64); virtual;
     45    function GetHandlerCount: Integer; virtual;
     46    function GetHandler(Address: Integer): TChannel64; virtual;
     47  end;
     48
     49  TDevice = class;
     50
     51  { TFormDevice }
     52
     53  TFormDevice = class(TForm)
     54  protected
     55    function GetDevice: TDevice; virtual;
     56    procedure SetDevice(AValue: TDevice); virtual;
     57  public
     58    property Device: TDevice read GetDevice write SetDevice;
     59  end;
     60
     61  TFormDeviceClass = class of TFormDevice;
     62
     63  { TDevice }
     64
     65  TDevice = class
     66    Name: string;
     67    DeviceClass: TDeviceClass;
     68    Form: TFormDevice;
     69  end;
     70
     71  { TDevices }
     72
     73  TDevices = class(TObjectList<TDevice>)
     74    function GetDevicesCountByClass(DeviceClass: TDeviceClass): Integer;
     75    function GetDevicesByClass(DeviceClass: TDeviceClass): TDevices;
     76    function GetClasses: TDeviceClassSet;
     77  end;
     78
     79const
     80  DeviceClassText: array[TDeviceClass] of string = ('None', 'Keyboard', 'Mouse', 'Storage', 'Screen', 'Console', 'Timer');
    2181
    2282
    2383implementation
    2484
     85{ TFormDevice }
     86
     87function TFormDevice.GetDevice: TDevice;
     88begin
     89  Result := nil;
     90end;
     91
     92procedure TFormDevice.SetDevice(AValue: TDevice);
     93begin
     94end;
     95
     96{ TDevices }
     97
     98function TDevices.GetDevicesCountByClass(DeviceClass: TDeviceClass): Integer;
     99var
     100  I: Integer;
     101begin
     102  Result := 0;
     103  for I := 0 to Count - 1 do
     104    if Items[I].DeviceClass = DeviceClass then Inc(Result);
     105end;
     106
     107function TDevices.GetDevicesByClass(DeviceClass: TDeviceClass): TDevices;
     108var
     109  I: Integer;
     110begin
     111  Result := TDevices.Create(False);
     112  for I := 0 to Count - 1 do
     113    if Items[I].DeviceClass = DeviceClass then Result.Add(Items[I])
     114end;
     115
     116function TDevices.GetClasses: TDeviceClassSet;
     117var
     118  I: Integer;
     119begin
     120  Result := [];
     121  for I := 0 to Count - 1 do
     122    if not (Items[I].DeviceClass in Result) then
     123      Result := Result + [Items[I].DeviceClass];
     124end;
     125
     126{ TDevice32 }
     127
     128procedure TDevice32.SetDataBus(Channel: TAddressableChannel32);
     129begin
     130end;
     131
     132function TDevice32.GetHandlerCount: Integer;
     133begin
     134  Result := 0;
     135end;
     136
     137function TDevice32.GetHandler(Address: Integer): TChannel32;
     138begin
     139end;
     140
     141{ TDevice64 }
     142
     143procedure TDevice64.SetDataBus(Channel: TAddressableChannel64);
     144begin
     145end;
     146
     147function TDevice64.GetHandlerCount: Integer;
     148begin
     149  Result := 0;
     150end;
     151
     152function TDevice64.GetHandler(Address: Integer): TChannel64;
     153begin
     154end;
     155
     156{ TDevice8 }
     157
     158procedure TDevice8.SetDataBus(Channel: TAddressableChannel8);
     159begin
     160end;
     161
     162function TDevice8.GetHandlerCount: Integer;
     163begin
     164  Result := 0;
     165end;
     166
     167function TDevice8.GetHandler(Address: Integer): TChannel8;
     168begin
     169end;
     170
     171{ TDevice16 }
     172
     173procedure TDevice16.SetDataBus(Channel: TAddressableChannel16);
     174begin
     175end;
     176
     177function TDevice16.GetHandlerCount: Integer;
     178begin
     179  Result := 0;
     180end;
     181
     182function TDevice16.GetHandler(Address: Integer): TChannel16;
     183begin
     184end;
     185
    25186end.
    26187
  • branches/simple/Devices/Screen.pas

    r41 r42  
    44
    55uses
    6   Classes, SysUtils, Device, DeviceMapper, Memory;
     6  Classes, SysUtils, Device, DeviceManager, Memory, Channel;
    77
    88type
     
    3131    constructor Create;
    3232    destructor Destroy; override;
    33     procedure RegisterMapper8(Mapper: TDeviceMapper8); override;
     33    function GetHandlerCount: Integer; override;
     34    function GetHandler(Address: Integer): TChannel8; override;
    3435    property OnChange: TNotifyEvent read FOnChange write FOnChange;
    3536  end;
     
    5960    constructor Create;
    6061    destructor Destroy; override;
    61     procedure RegisterMapper8(Mapper: TDeviceMapper8); override;
    62     procedure RegisterMapper16(Mapper: TDeviceMapper16); override;
     62    function GetHandlerCount: Integer; override;
     63    function GetHandler(Address: Integer): TChannel16; override;
    6364    property OnChange: TNotifyEvent read FOnChange write FOnChange;
    6465  end;
    6566
     67  TScreen = class(TDevice)
     68    BitWidth: TBitWidth;
     69    Screen8: TScreen8;
     70    Screen16: TScreen16;
     71    constructor Create(BitWidth: TBitWidth; Screen8: TScreen8; Screen16: TScreen16);
     72  end;
     73
    6674
    6775implementation
     76
     77{ TScreen }
     78
     79constructor TScreen.Create(BitWidth: TBitWidth; Screen8: TScreen8;
     80  Screen16: TScreen16);
     81begin
     82  Self.BitWidth := BitWidth;
     83  Self.Screen8 := Screen8;
     84  Self.Screen16 := Screen16;
     85end;
    6886
    6987{ TScreen16 }
     
    149167end;
    150168
    151 procedure TScreen16.RegisterMapper8(Mapper: TDeviceMapper8);
    152 begin
    153 end;
    154 
    155 procedure TScreen16.RegisterMapper16(Mapper: TDeviceMapper16);
    156 begin
    157   Mapper.RegisterReadHandler(ReadData8, ReadData16);
    158   Mapper.RegisterWriteHandler(WriteData8, WriteData16);
    159   Mapper.RegisterReadHandler(nil, ReadAddr16);
    160   Mapper.RegisterWriteHandler(nil, WriteAddr16);
    161   Mapper.RegisterReadHandler(nil, ReadWidth16);
    162   Mapper.RegisterWriteHandler(nil, WriteWidth16);
    163   Mapper.RegisterReadHandler(nil, ReadHeight16);
    164   Mapper.RegisterWriteHandler(nil, WriteHeight16);
     169function TScreen16.GetHandlerCount: Integer;
     170begin
     171  Result := 4;
     172end;
     173
     174function TScreen16.GetHandler(Address: Integer): TChannel16;
     175begin
     176  if Address = 0 then begin
     177    Result := TChannel16.Create;
     178    Result.Read16 := ReadData16;
     179    Result.Write16 := WriteData16;
     180  end else
     181  if Address = 1 then begin
     182    Result := TChannel16.Create;
     183    Result.Read16 := ReadAddr16;
     184    Result.Write16 := WriteAddr16;
     185  end else
     186  if Address = 2 then begin
     187    Result := TChannel16.Create;
     188    Result.Read16 := ReadWidth16;
     189    Result.Write16 := WriteWidth16;
     190  end else
     191  if Address = 3 then begin
     192    Result := TChannel16.Create;
     193    Result.Read16 := ReadHeight16;
     194    Result.Write16 := WriteHeight16;
     195  end;
    165196end;
    166197
     
    236267end;
    237268
    238 procedure TScreen8.RegisterMapper8(Mapper: TDeviceMapper8);
    239 begin
    240   Mapper.RegisterReadHandler(ReadData8);
    241   Mapper.RegisterWriteHandler(WriteData8);
    242   Mapper.RegisterReadHandler(ReadAddr8);
    243   Mapper.RegisterWriteHandler(WriteAddr8);
    244   Mapper.RegisterReadHandler(ReadWidth8);
    245   Mapper.RegisterWriteHandler(WriteWidth8);
    246   Mapper.RegisterReadHandler(ReadHeight8);
    247   Mapper.RegisterWriteHandler(WriteHeight8);
     269function TScreen8.GetHandlerCount: Integer;
     270begin
     271  Result := 4;
     272end;
     273
     274function TScreen8.GetHandler(Address: Integer): TChannel8;
     275begin
     276  if Address = 0 then begin
     277    Result := TChannel8.Create;
     278    Result.Read8 := ReadData8;
     279    Result.Write8 := WriteData8;
     280  end else
     281  if Address = 1 then begin
     282    Result := TChannel8.Create;
     283    Result.Read8 := ReadAddr8;
     284    Result.Write8 := WriteAddr8;
     285  end else
     286  if Address = 2 then begin
     287    Result := TChannel8.Create;
     288    Result.Read8 := ReadWidth8;
     289    Result.Write8 := WriteWidth8;
     290  end else
     291  if Address = 3 then begin
     292    Result := TChannel8.Create;
     293    Result.Read8 := ReadHeight8;
     294    Result.Write8 := WriteHeight8;
     295  end;
    248296end;
    249297
  • branches/simple/Forms/FormConsole.pas

    r41 r42  
    44
    55uses
    6   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
     6  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Device;
    77
    88type
     
    1010  { TFormConsole }
    1111
    12   TFormConsole = class(TForm)
     12  TFormConsole = class(TFormDevice)
    1313    Memo1: TMemo;
    1414  public
  • branches/simple/Forms/FormMain.pas

    r41 r42  
    4343{$R *.lfm}
    4444
     45uses
     46  Screen, Channel;
     47
    4548{ TFormMain }
    4649
     
    4952  FormScreen := TFormScreen.Create(nil);
    5053  FormConsole := TFormConsole.Create(nil);
    51   InitMachine16;
     54  InitMachine8;
    5255end;
    5356
     
    6669  FreeAndNil(FormScreen);
    6770  FreeAndNil(FormConsole);
    68   FreeAndNil(Machine8);
     71  if Assigned(Machine8) then FreeAndNil(Machine8);
     72  if Assigned(Machine16) then FreeAndNil(Machine16);
    6973end;
    7074
     
    8084    Console.OnWrite := FormConsole.ConsoleWrite;
    8185    Screen.OnChange := FormScreen.ScreenChange;
    82     FormScreen.Machine := Machine8;
     86    FormScreen.Screen := TScreen.Create(bw8, Machine8.Screen, nil);
    8387    with Memory do begin
    8488      // LD A8, 'A'
     
    9094      // LD A8, 'A'
    9195      WritePos8(Byte(inLoadImmediate8));
    92       WritePos8(Ord('H'));
    93       // OUT (0), A8
    94       WritePos8(Byte(inOutput8));
    95       WritePos8(1);
     96      WritePos8(Ord('B'));
    9697      // OUT (0), A8
    9798      WritePos8(Byte(inOutput8));
     
    110111    Console.OnWrite := FormConsole.ConsoleWrite;
    111112    Screen.OnChange := FormScreen.ScreenChange;
    112     FormScreen.Machine := Machine16;
     113    FormScreen.Screen := TScreen.Create(bw16, nil, Machine16.Screen);
    113114    with Memory do begin
    114115      // LD A8, 'A'
     
    122123      WritePos16(Ord('H'));
    123124      // OUT8 (0), A8
    124       WritePos8(Byte(inOutput8));
     125      WritePos8(Byte(inOutput16));
    125126      WritePos16(1);
    126127      // HALT
  • branches/simple/Forms/FormScreen.pas

    r41 r42  
    44
    55uses
    6   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Machine;
     6  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Screen,
     7  Channel, Device;
    78
    89type
     
    1011  { TFormScreen }
    1112
    12   TFormScreen = class(TForm)
     13  TFormScreen = class(TFormDevice)
    1314    Image1: TImage;
    1415    TimerDraw: TTimer;
     
    1718    DrawPending: Boolean;
    1819  public
    19     Machine: TMachine;
     20    Screen: TScreen;
    2021    procedure ScreenChange(Sender: TObject);
    2122  end;
     
    3637  if DrawPending then begin
    3738    DrawPending := False;
    38     if Machine is TMachine16 then
    39     with TMachine16(Machine).Screen do begin
     39    if Screen.BitWidth = bw8 then
     40    with Screen.Screen8 do begin
    4041      Image1.Picture.Bitmap.SetSize(Width * CharSize, Height * CharSize);
    4142      Image1.Picture.Bitmap.Canvas.FillRect(0, 0, Image1.Picture.Bitmap.Width,
     
    4647            Chr(Memory.Read8(Y * Width + X)));
    4748    end else
    48     if Machine is TMachine8 then
    49     with TMachine8(Machine).Screen do begin
     49    if Screen.BitWidth = bw16 then
     50    with Screen.Screen16 do begin
    5051      Image1.Picture.Bitmap.SetSize(Width * CharSize, Height * CharSize);
    5152      Image1.Picture.Bitmap.Canvas.FillRect(0, 0, Image1.Picture.Bitmap.Width,
  • branches/simple/Machine.pas

    r41 r42  
    44
    55uses
    6   Classes, SysUtils, Cpu, Memory, DeviceMapper, Console, Screen;
     6  Classes, SysUtils, Cpu, Memory, DeviceManager, Console, Screen;
    77
    88type
     
    2525    procedure SetPoweredOn(AValue: Boolean); override;
    2626  public
    27     DeviceMapper: TDeviceMapper8;
     27    DeviceManager: TDeviceManager8;
    2828    Memory: TMemory8;
    29     Console: TConsole;
     29    Console: TConsole8;
    3030    Cpu: TCpu8;
    3131    Screen: TScreen8;
     
    4141    procedure SetPoweredOn(AValue: Boolean); override;
    4242  public
    43     DeviceMapper: TDeviceMapper16;
     43    DeviceManager: TDeviceManager16;
    4444    Memory: TMemory16;
    45     Console: TConsole;
     45    Console: TConsole16;
    4646    Cpu: TCpu16;
    4747    Screen: TScreen16;
     
    7272constructor TMachine16.Create;
    7373begin
    74   DeviceMapper := TDeviceMapper16.Create;
    75   Console := TConsole.Create;
    76   Console.RegisterMapper16(DeviceMapper);
     74  Cpu := TCpu16.Create;
     75  Console := TConsole16.Create;
    7776  Screen := TScreen16.Create;
    78   Screen.RegisterMapper16(DeviceMapper);
     77  DeviceManager := TDeviceManager16.Create;
     78  DeviceManager.RegisterDevice(Console);
     79  DeviceManager.RegisterDevice(Screen);
     80  DeviceManager.SetDataBus(Cpu.IO);
    7981  Memory := TMemory16.Create;
    8082  Memory.Size := 65535;
    81   Cpu := TCpu16.Create;
    82   Cpu.IO := DeviceMapper;
    83   Cpu.Memory := Memory;
     83  Memory.SetDataBus(Cpu.Memory);
    8484end;
    8585
     
    8787begin
    8888  FreeAndNil(Cpu);
    89   FreeAndNil(DeviceMapper);
     89  FreeAndNil(DeviceManager);
    9090  FreeAndNil(Console);
    9191  FreeAndNil(Memory);
     
    105105constructor TMachine8.Create;
    106106begin
    107   DeviceMapper := TDeviceMapper8.Create;
    108   Console := TConsole.Create;
    109   Console.RegisterMapper8(DeviceMapper);
     107  Cpu := TCpu8.Create;
     108  Console := TConsole8.Create;
    110109  Screen := TScreen8.Create;
    111   Screen.RegisterMapper8(DeviceMapper);
     110  DeviceManager := TDeviceManager8.Create;
     111  DeviceManager.RegisterDevice(Console);
     112  DeviceManager.RegisterDevice(Screen);
     113  DeviceManager.SetDataBus(Cpu.IO);
    112114  Memory := TMemory8.Create;
    113115  Memory.Size := 255;
    114   Cpu := TCpu8.Create;
    115   Cpu.IO := DeviceMapper;
    116   Cpu.Memory := Memory;
     116  Memory.SetDataBus(Cpu.Memory);
    117117end;
    118118
     
    120120begin
    121121  FreeAndNil(Cpu);
    122   FreeAndNil(DeviceMapper);
     122  FreeAndNil(DeviceManager);
    123123  FreeAndNil(Console);
    124124  FreeAndNil(Memory);
  • branches/simple/Memory.pas

    r40 r42  
    44
    55uses
    6   Classes, SysUtils, DataBus;
     6  Classes, SysUtils, Channel, Device;
    77
    88type
     9
     10  { TMemoryMulti }
     11
     12  TMemoryMulti = class
     13  private
     14    Memory: array of Byte;
     15    procedure SetSize(Value: QWord);
     16    function GetSize: QWord;
     17  public
     18    Position: QWord;
     19    property Size: QWord read GetSize write SetSize;
     20  end;
     21
    922  { TMemory8 }
    1023
    11   TMemory8 = class(TDataBus8)
     24  TMemory8 = class(TDevice8)
    1225  private
    1326    Memory: array of Byte;
     
    1831    procedure WritePos8(Data: Byte);
    1932    function ReadPos8: Byte;
    20     procedure Write8(Address: Byte; Data: Byte); override;
    21     function Read8(Address: Byte): Byte; override;
     33    procedure Write8(Address: Byte; Data: Byte);
     34    function Read8(Address: Byte): Byte;
     35    procedure SetDataBus(Channel: TAddressableChannel8); override;
    2236    property Size: Byte read GetSize write SetSize;
    2337  end;
     
    2539  { TMemory16 }
    2640
    27   TMemory16 = class(TDataBus16)
     41  TMemory16 = class(TDevice16)
    2842  private
    2943    Memory: array of Byte;
     
    3650    procedure WritePos8(Data: Byte);
    3751    procedure WritePos16(Data: Word);
    38     function Read8(Address: Word): Byte; override;
    39     function Read16(Address: Word): Word; override;
    40     procedure Write8(Address: Word; Data: Byte); override;
    41     procedure Write16(Address: Word; Data: Word); override;
     52    function Read8(Address: Word): Byte;
     53    function Read16(Address: Word): Word;
     54    procedure Write8(Address: Word; Data: Byte);
     55    procedure Write16(Address: Word; Data: Word);
     56    procedure SetDataBus(Channel: TAddressableChannel16); override;
    4257    property Size: Word read GetSize write SetSize;
    4358  end;
    4459
     60  { TMemory64 }
     61
     62  TMemory64 = class(TDevice64)
     63  private
     64    Memory: array of Byte;
     65    procedure SetSize(Value: QWord);
     66    function GetSize: QWord;
     67  public
     68    Position: QWord;
     69    function ReadPos8: Byte;
     70    function ReadPos16: Word;
     71    function ReadPos32: DWord;
     72    function ReadPos64: QWord;
     73    procedure WritePos8(Data: Byte);
     74    procedure WritePos16(Data: Word);
     75    procedure WritePos32(Data: Word);
     76    procedure WritePos64(Data: Word);
     77    function Read8(Address: QWord): Byte;
     78    function Read16(Address: QWord): Word;
     79    function Read32(Address: QWord): DWord;
     80    function Read64(Address: QWord): QWord;
     81    procedure Write8(Address: QWord; Data: Byte);
     82    procedure Write16(Address: QWord; Data: Word);
     83    procedure Write32(Address: QWord; Data: DWord);
     84    procedure Write64(Address: QWord; Data: QWord);
     85    procedure SetDataBus(Channel: TAddressableChannel64); override;
     86    property Size: QWord read GetSize write SetSize;
     87  end;
     88
     89  { TMemory }
     90
     91  TMemory = class(TDevice)
     92    BitWidth: TBitWidth;
     93    Memory8: TMemory8;
     94    Memory16: TMemory16;
     95  end;
     96
    4597
    4698implementation
     99
     100{ TMemory64 }
     101
     102procedure TMemory64.SetSize(Value: QWord);
     103begin
     104
     105end;
     106
     107function TMemory64.GetSize: QWord;
     108begin
     109
     110end;
     111
     112function TMemory64.ReadPos8: Byte;
     113begin
     114
     115end;
     116
     117function TMemory64.ReadPos16: Word;
     118begin
     119
     120end;
     121
     122function TMemory64.ReadPos32: DWord;
     123begin
     124
     125end;
     126
     127function TMemory64.ReadPos64: QWord;
     128begin
     129
     130end;
     131
     132procedure TMemory64.WritePos8(Data: Byte);
     133begin
     134
     135end;
     136
     137procedure TMemory64.WritePos16(Data: Word);
     138begin
     139
     140end;
     141
     142procedure TMemory64.WritePos32(Data: Word);
     143begin
     144
     145end;
     146
     147procedure TMemory64.WritePos64(Data: Word);
     148begin
     149
     150end;
     151
     152function TMemory64.Read8(Address: QWord): Byte;
     153begin
     154
     155end;
     156
     157function TMemory64.Read16(Address: QWord): Word;
     158begin
     159
     160end;
     161
     162function TMemory64.Read32(Address: QWord): DWord;
     163begin
     164
     165end;
     166
     167function TMemory64.Read64(Address: QWord): QWord;
     168begin
     169
     170end;
     171
     172procedure TMemory64.Write8(Address: QWord; Data: Byte);
     173begin
     174
     175end;
     176
     177procedure TMemory64.Write16(Address: QWord; Data: Word);
     178begin
     179
     180end;
     181
     182procedure TMemory64.Write32(Address: QWord; Data: DWord);
     183begin
     184
     185end;
     186
     187procedure TMemory64.Write64(Address: QWord; Data: QWord);
     188begin
     189
     190end;
     191
     192procedure TMemory64.SetDataBus(Channel: TAddressableChannel64);
     193begin
     194  inherited SetDataBus(Channel);
     195end;
     196
     197{ TMemoryMulti }
     198
     199procedure TMemoryMulti.SetSize(Value: QWord);
     200begin
     201  SetLength(Memory, Size);
     202end;
     203
     204function TMemoryMulti.GetSize: QWord;
     205begin
     206  Result := Length(Memory);
     207end;
    47208
    48209{ TMemory16 }
     
    103264end;
    104265
     266procedure TMemory16.SetDataBus(Channel: TAddressableChannel16);
     267begin
     268  Channel.Read8 := Read8;
     269  Channel.Read16 := Read16;
     270  Channel.Write8 := Write8;
     271  Channel.Write16 := Write16;
     272end;
     273
    105274{ TMemory8 }
    106275
     
    137306end;
    138307
     308procedure TMemory8.SetDataBus(Channel: TAddressableChannel8);
     309begin
     310  Channel.Write8 := Write8;
     311  Channel.Read8 := Read8;
     312end;
    139313
    140314end.
  • branches/simple/simple.lpi

    r41 r42  
    2626    <RequiredPackages>
    2727      <Item>
     28        <PackageName Value="SynEdit"/>
     29      </Item>
     30      <Item>
    2831        <PackageName Value="LCL"/>
    2932      </Item>
     
    5053      </Unit>
    5154      <Unit>
    52         <Filename Value="DataBus.pas"/>
     55        <Filename Value="Channel.pas"/>
    5356        <IsPartOfProject Value="True"/>
    5457      </Unit>
     
    5861      </Unit>
    5962      <Unit>
    60         <Filename Value="DeviceMapper.pas"/>
     63        <Filename Value="DeviceManager.pas"/>
    6164        <IsPartOfProject Value="True"/>
    6265      </Unit>
    6366      <Unit>
    64         <Filename Value="Device.pas"/>
     67        <Filename Value="Devices/Device.pas"/>
    6568        <IsPartOfProject Value="True"/>
    6669      </Unit>
    6770      <Unit>
    68         <Filename Value="Console.pas"/>
     71        <Filename Value="Devices/Console.pas"/>
    6972        <IsPartOfProject Value="True"/>
    7073      </Unit>
    7174      <Unit>
    72         <Filename Value="Screen.pas"/>
     75        <Filename Value="Devices/Screen.pas"/>
    7376        <IsPartOfProject Value="True"/>
    7477      </Unit>
     
    8790        <ResourceBaseClass Value="Form"/>
    8891      </Unit>
     92      <Unit>
     93        <Filename Value="Assembler.pas"/>
     94        <IsPartOfProject Value="True"/>
     95      </Unit>
     96      <Unit>
     97        <Filename Value="Parser.pas"/>
     98        <IsPartOfProject Value="True"/>
     99      </Unit>
     100      <Unit>
     101        <Filename Value="Instructions.pas"/>
     102        <IsPartOfProject Value="True"/>
     103      </Unit>
     104      <Unit>
     105        <Filename Value="Message.pas"/>
     106        <IsPartOfProject Value="True"/>
     107      </Unit>
     108      <Unit>
     109        <Filename Value="Forms/FormAssembler.pas"/>
     110        <IsPartOfProject Value="True"/>
     111        <ComponentName Value="FormAssembler"/>
     112        <HasResources Value="True"/>
     113        <ResourceBaseClass Value="Form"/>
     114      </Unit>
     115      <Unit>
     116        <Filename Value="Forms/FormDevices.pas"/>
     117        <IsPartOfProject Value="True"/>
     118        <ComponentName Value="FormDevices"/>
     119        <HasResources Value="True"/>
     120        <ResourceBaseClass Value="Form"/>
     121      </Unit>
     122      <Unit>
     123        <Filename Value="Forms/FormDisassembler.pas"/>
     124        <IsPartOfProject Value="True"/>
     125        <ComponentName Value="FormDisassembler"/>
     126        <ResourceBaseClass Value="Form"/>
     127      </Unit>
     128      <Unit>
     129        <Filename Value="Forms/FormMessages.pas"/>
     130        <IsPartOfProject Value="True"/>
     131        <ComponentName Value="FormMessages"/>
     132        <ResourceBaseClass Value="Form"/>
     133      </Unit>
     134      <Unit>
     135        <Filename Value="Forms/FormStorage.pas"/>
     136        <IsPartOfProject Value="True"/>
     137        <ComponentName Value="FormStorage"/>
     138        <HasResources Value="True"/>
     139        <ResourceBaseClass Value="Form"/>
     140      </Unit>
     141      <Unit>
     142        <Filename Value="Pin.pas"/>
     143        <IsPartOfProject Value="True"/>
     144      </Unit>
     145      <Unit>
     146        <Filename Value="Forms/FormDevice.pas"/>
     147        <IsPartOfProject Value="True"/>
     148      </Unit>
     149      <Unit>
     150        <Filename Value="Devices/Storage.pas"/>
     151        <IsPartOfProject Value="True"/>
     152      </Unit>
    89153    </Units>
    90154  </ProjectOptions>
     
    96160    <SearchPaths>
    97161      <IncludeFiles Value="$(ProjOutDir)"/>
    98       <OtherUnitFiles Value="Forms"/>
     162      <OtherUnitFiles Value="Forms;Devices"/>
    99163      <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
    100164    </SearchPaths>
  • branches/simple/simple.lpr

    r41 r42  
    1111  {$ENDIF}
    1212  Interfaces, // this includes the LCL widgetset
    13   Forms, FormMain, Cpu, Machine, DataBus, Memory, DeviceMapper, Device, Console,
    14   Screen, FormScreen, FormConsole;
     13  Forms, FormMain, Cpu, Machine, Channel, Memory,
     14DeviceManager, Device, Console,
     15  Screen, Assembler, Parser, Instructions, Message, FormScreen, FormConsole,
     16  FormAssembler, FormDevices, FormDisassembler, FormMessages, FormStorage,
     17  Pin, FormDevice, Storage;
    1518
    1619{$R *.res}
Note: See TracChangeset for help on using the changeset viewer.