Ignore:
Timestamp:
Jul 6, 2022, 1:05:27 AM (23 months ago)
Author:
chronos
Message:
  • Modified: Code parsing and execution improvements.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/UltimatOS/UInstructionWriter.pas

    r29 r31  
    88type
    99  TInstructionParam = (ipNone, ipRegIndex, ipAddress, ipData, ipIndex,
    10     ipRegIndexRel);
     10    ipRegIndexRel, ipAddressRel);
    1111
    1212  TInstructionDef = class
     
    7272    procedure WriteReg(Index: Byte);
    7373    procedure WriteByte(Value: Byte);
     74    procedure WriteWord(Value: Word);
     75    procedure WriteCardinal(Value: Cardinal);
    7476    constructor Create;
    7577    destructor Destroy; override;
     
    190192        end;
    191193      end;
     194    end;
     195    ipAddressRel: begin
     196      Address := Trim(ParseText(Text, ','));
     197      if Address.StartsWith('(') and Address.EndsWith(')') then begin
     198        Address := Copy(Address, 2, Length(Address) - 2);
     199        if TryStrToInt(Address, Value) then begin
     200          WriteAddress(Value);
     201        end else begin
     202          FoundConstant := Constants.SearchByName(UpperCase(Address));
     203          if Assigned(FoundConstant) then begin
     204            WriteAddress(FoundConstant.Value);
     205            Exit;
     206          end;
     207          FoundLabel := Labels.SearchByName(UpperCase(Address));
     208          if Assigned(FoundLabel) then begin
     209            // Existing label
     210            WriteAddress(FoundLabel.Address);
     211          end else begin
     212            // Forward label reference
     213            with Labels.AddNew(UpperCase(Address), -1) do begin
     214              SetLength(ForwardRefs, Length(ForwardRefs) + 1);
     215              ForwardRefs[Length(ForwardRefs) - 1] := IP;
     216            end;
     217            WriteAddress(0);
     218          end;
     219        end;
     220      end else Error('Expected indirect address ' + Address);
    192221    end;
    193222    ipRegIndex: begin
     
    333362        end else WriteByte(StrToInt(Param));
    334363      end;
     364    end else
     365    if InstructionName = 'dw' then begin
     366      while Text <> '' do begin
     367        Param := ParseText(Text, ',');
     368        if Param.StartsWith('"') and Param.EndsWith('"') then begin
     369          Param := Copy(Param, 2, Length(Param) - 2);
     370          for I := 1 to Length(Param) do
     371            WriteWord(Ord(Param[I]));
     372        end else WriteWord(StrToInt(Param));
     373      end;
     374    end else
     375    if InstructionName = 'dd' then begin
     376      while Text <> '' do begin
     377        Param := ParseText(Text, ',');
     378        if Param.StartsWith('"') and Param.EndsWith('"') then begin
     379          Param := Copy(Param, 2, Length(Param) - 2);
     380          for I := 1 to Length(Param) do
     381            WriteCardinal(Ord(Param[I]));
     382        end else WriteCardinal(StrToInt(Param));
     383      end;
    335384    end else Error('Unsupported directive name ' + InstructionName);
    336385  end else begin
     
    383432end;
    384433
     434procedure TInstructionWriter.WriteWord(Value: Word);
     435begin
     436  PWord(@Memory.Data[IP])^ := Value;
     437  Inc(IP, SizeOf(Word));
     438end;
     439
     440procedure TInstructionWriter.WriteCardinal(Value: Cardinal);
     441begin
     442  PCardinal(@Memory.Data[IP])^ := Value;
     443  Inc(IP, SizeOf(Cardinal));
     444end;
     445
    385446constructor TInstructionWriter.Create;
    386447begin
     
    392453    AddNew(inHalt, 'HALT');
    393454    AddNew(inSet, 'SET', ipRegIndex, ipData);
    394     AddNew(inInput, 'IN', ipRegIndex, ipAddress);
    395     AddNew(inOutput, 'OUT', ipAddress, ipRegIndex);
     455    AddNew(inInput, 'IN', ipRegIndex, ipAddressRel);
     456    AddNew(inOutput, 'OUT', ipAddressRel, ipRegIndex);
    396457    AddNew(inInc, 'INC', ipRegIndex);
    397458    AddNew(inDec, 'DEC', ipRegIndex);
     
    409470    AddNew(inShr, 'SHR', ipRegIndex, ipIndex);
    410471    AddNew(inLoad, 'LD', ipRegIndex, ipRegIndexRel);
     472    AddNew(inLoadi, 'LDI', ipRegIndex, ipAddressRel);
    411473    AddNew(inStore, 'ST', ipRegIndexRel, ipRegIndex);
    412474    AddNew(inMul, 'MUL', ipRegIndex, ipRegIndex);
    413475    AddNew(inAnd, 'AND', ipRegIndex, ipRegIndex);
     476    AddNew(inAndi, 'ANDI', ipRegIndex, ipData);
    414477    AddNew(inOr, 'OR', ipRegIndex, ipRegIndex);
    415478    AddNew(inXor, 'XOR', ipRegIndex, ipRegIndex);
Note: See TracChangeset for help on using the changeset viewer.