Ignore:
Timestamp:
Jan 4, 2018, 2:00:49 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Preparation for support for multiple memory types.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/virt simple/UMachine.pas

    r89 r138  
    2424  TParamType = (ptNone, ptConst, ptRegister, ptSpecialRegister);
    2525  TCondition = (cdZero, cdNotZero, cdNegative, cdPositive);
     26  TMemoryType = (mtNone, mtData);
    2627
    2728  TValue = Integer;
     
    3334  TParam = class
    3435    ParamType: TParamType;
    35     Memory: Boolean;
     36    MemoryType: TMemoryType;
    3637    Value: TValue;
    3738    function GetString: string;
     39    procedure SetParam(ParamType: TParamType; MemoryType: TMemoryType; Value: TValue);
    3840  end;
    3941
     
    5153    procedure AddInst(Opcode: TOpcode); overload;
    5254    procedure AddInst(Opcode: TOpcode;
    53       ParamType1: TParamType; Indirect1: Boolean; Value1: TValue);  overload;
     55      ParamType1: TParamType; Indirect1: TMemoryType; Value1: TValue);  overload;
    5456    procedure AddInst(Opcode: TOpcode;
    55       ParamType1: TParamType; Indirect1: Boolean; Value1: TValue;
    56       ParamType2: TParamType; Indirect2: Boolean; Value2: TValue);  overload;
     57      ParamType1: TParamType; Indirect1: TMemoryType; Value1: TValue;
     58      ParamType2: TParamType; Indirect2: TMemoryType; Value2: TValue);  overload;
    5759  end;
    5860
     
    146148      else Result := 'S' + IntToStr(Value);
    147149  end;
    148   if Memory then Result := '[' + Result + ']';
     150  if MemoryType = mtData then Result := '[' + Result + ']';
     151end;
     152
     153procedure TParam.SetParam(ParamType: TParamType; MemoryType: TMemoryType;
     154  Value: TValue);
     155begin
     156  Self.ParamType := ParamType;
     157  Self.MemoryType := MemoryType;
     158  Self.Value := Value;
    149159end;
    150160
     
    183193  NewInst := TInstruction.Create;
    184194  NewInst.Opcode := Opcode;
    185   NewInst.Params[0].ParamType := ptNone;
    186   NewInst.Params[0].Memory := False;
    187   NewInst.Params[0].Value := 0;
    188   NewInst.Params[1].ParamType := ptNone;
    189   NewInst.Params[1].Memory := False;
    190   NewInst.Params[1].Value := 0;
     195  NewInst.Params[0].SetParam(ptNone, mtNone, 0);
     196  NewInst.Params[1].SetParam(ptNone, mtNone, 0);
    191197  Add(NewInst);
    192198end;
    193199
    194200procedure TInstructions.AddInst(Opcode: TOpcode; ParamType1: TParamType;
    195   Indirect1: Boolean; Value1: TValue);
     201  Indirect1: TMemoryType; Value1: TValue);
    196202var
    197203  NewInst: TInstruction;
     
    199205  NewInst := TInstruction.Create;
    200206  NewInst.Opcode := Opcode;
    201   NewInst.Params[0].ParamType := ParamType1;
    202   NewInst.Params[0].Memory := Indirect1;
    203   NewInst.Params[0].Value := Value1;
    204   NewInst.Params[1].ParamType := ptNone;
    205   NewInst.Params[1].Memory := False;
    206   NewInst.Params[1].Value := 0;
     207  NewInst.Params[0].SetParam(ParamType1, Indirect1, Value1);
     208  NewInst.Params[1].SetParam(ptNone, mtNone, 0);
    207209  Add(NewInst);
    208210end;
    209211
    210212procedure TInstructions.AddInst(Opcode: TOpcode;
    211   ParamType1: TParamType; Indirect1: Boolean; Value1: TValue;
    212   ParamType2: TParamType; Indirect2: Boolean; Value2: TValue);
     213  ParamType1: TParamType; Indirect1: TMemoryType; Value1: TValue;
     214  ParamType2: TParamType; Indirect2: TMemoryType; Value2: TValue);
    213215var
    214216  NewInst: TInstruction;
     
    216218  NewInst := TInstruction.Create;
    217219  NewInst.Opcode := Opcode;
    218   NewInst.Params[0].ParamType := ParamType1;
    219   NewInst.Params[0].Memory := Indirect1;
    220   NewInst.Params[0].Value := Value1;
    221   NewInst.Params[1].ParamType := ParamType2;
    222   NewInst.Params[1].Memory := Indirect2;
    223   NewInst.Params[1].Value := Value2;
     220  NewInst.Params[0].SetParam(ParamType1, Indirect1, Value1);
     221  NewInst.Params[1].SetParam(ParamType2, Indirect2, Value2);
    224222  Add(NewInst);
    225223end;
     
    469467  end else if Param.ParamType = ptNone then raise Exception.Create('Read from not set parameter')
    470468  else raise Exception.Create('Unsupported parameter type ' + IntToStr(Integer(Param.ParamType)));
    471   if Param.Memory then Result := Memory[Result];
     469  if Param.MemoryType = mtData then Result := Memory[Result];
    472470end;
    473471
     
    475473begin
    476474  if Param.ParamType = ptRegister then begin
    477     if Param.Memory then Memory[Registers[Param.Value]] := Value
     475    if Param.MemoryType = mtData then Memory[Registers[Param.Value]] := Value
    478476      else Registers[Param.Value] := Value;
    479   end else if Param.ParamType = ptConst then begin
    480     if Param.Memory then Memory[Param.Value] := Value
     477  end else
     478  if Param.ParamType = ptConst then begin
     479    if Param.MemoryType = mtData then Memory[Param.Value] := Value
    481480      else raise Exception.Create('Cannot assign to constant parameter');
    482   end else if Param.ParamType = ptSpecialRegister then begin
     481  end else
     482  if Param.ParamType = ptSpecialRegister then begin
    483483    if Param.Value = Integer(srIP) then begin
    484       if Param.Memory then Memory[IP] := Value
     484      if Param.MemoryType = mtData then Memory[IP] := Value
    485485        else IP := Value;
    486486    end else if Param.Value = Integer(srSP) then begin
    487       if Param.Memory then Memory[SP] := Value
     487      if Param.MemoryType = mtData then Memory[SP] := Value
    488488        else SP := Value;
    489489    end else raise Exception.Create('Unsupported special register ' + IntToStr(Integer(Param.Value)));
    490   end else if Param.ParamType = ptNone then raise Exception.Create('Write to not set parameter')
    491   else raise Exception.Create('Unsupported parameter type ' + IntToStr(Integer(Param.ParamType)));
     490  end else
     491  if Param.ParamType = ptNone then begin
     492    raise Exception.Create('Write to not set parameter')
     493  end else
     494  raise Exception.Create('Unsupported parameter type ' + IntToStr(Integer(Param.ParamType)));
    492495end;
    493496
Note: See TracChangeset for help on using the changeset viewer.