Ignore:
Timestamp:
Nov 25, 2023, 11:47:52 PM (6 months ago)
Author:
chronos
Message:
  • Fixed: Assembler and disassembler to work correctly with supported instructions.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ByteArray/Instructions.pas

    r56 r59  
    77
    88type
    9   TParamType = (ptNone, ptNumber, ptReg, ptRegIndirect, ptRegIndirectIndex,
    10     ptSize);
     9  TParamType = (ptNone, ptRegIndirectIndex, ptRegIndirect, ptReg,
     10    ptDataWidth, ptAddressWidth, ptData, ptAddress);
    1111  TParamTypeArray = array of TParamType;
    1212
     
    1818  end;
    1919
    20   { TInstructionSet }
     20  { TInstructionInfos }
    2121
    22   TInstructionSet = class
    23     Items: TObjectList<TInstructionInfo>;
    24     function SearchName(Name: string): TInstructionInfo;
     22  TInstructionInfos = class(TObjectList<TInstructionInfo>)
     23    function SearchByName(Name: string): TInstructionInfo;
     24    function SearchByNameMultiple(Name: string): TInstructionInfos;
    2525    function SearchInstruction(Instruction: TInstruction): TInstructionInfo;
    2626    function AddNew(Instruction: TInstruction; Name: string;
    2727      Params: TParamTypeArray; Description: string): TInstructionInfo;
    28     constructor Create;
    29     destructor Destroy; override;
     28    procedure Init;
    3029  end;
     30
     31const
     32  ParamTypeString: array[TParamType] of string = ('None', 'Data', 'Address',
     33    'Register', 'Indirect register', 'Indirect indexed register',
     34    'Data width', 'Address width');
    3135
    3236
    3337implementation
    3438
    35 { TInstructionSet }
     39{ TInstructionInfos }
    3640
    37 function TInstructionSet.SearchName(Name: string): TInstructionInfo;
     41function TInstructionInfos.SearchByName(Name: string): TInstructionInfo;
    3842var
    3943  I: Integer;
    4044begin
    4145  I := 0;
    42   while (I < Items.Count) and (Items[I].Name <> Name) do Inc(I);
    43   if I < Items.Count then Result := Items[I]
     46  while (I < Count) and (Items[I].Name <> Name) do Inc(I);
     47  if I < Count then Result := Items[I]
    4448    else Result := nil;
    4549end;
    4650
    47 function TInstructionSet.SearchInstruction(Instruction: TInstruction
     51function TInstructionInfos.SearchByNameMultiple(Name: string
     52  ): TInstructionInfos;
     53var
     54  I: Integer;
     55begin
     56  Result := TInstructionInfos.Create(False);
     57  for I := 0 to Count - 1 do
     58    if Items[I].Name = Name then Result.Add(Items[I]);
     59end;
     60
     61function TInstructionInfos.SearchInstruction(Instruction: TInstruction
    4862  ): TInstructionInfo;
    4963var
     
    5165begin
    5266  I := 0;
    53   while (I < Items.Count) and (Items[I].Instruction <> Instruction) do Inc(I);
    54   if I < Items.Count then Result := Items[I]
     67  while (I < Count) and (Items[I].Instruction <> Instruction) do Inc(I);
     68  if I < Count then Result := Items[I]
    5569    else Result := nil;
    5670end;
    5771
    58 function TInstructionSet.AddNew(Instruction: TInstruction; Name: string;
     72function TInstructionInfos.AddNew(Instruction: TInstruction; Name: string;
    5973  Params: TParamTypeArray; Description: string): TInstructionInfo;
    6074begin
     
    6478  Result.Params := Params;
    6579  Result.Description := Description;
    66   Items.Add(Result);
     80  Add(Result);
    6781end;
    6882
    69 constructor TInstructionSet.Create;
     83procedure TInstructionInfos.Init;
    7084begin
    71   Items := TObjectList<TInstructionInfo>.Create;
     85  Clear;
    7286  AddNew(inNop, 'NOP', [], 'No operation - The instruction doesn''t do anything.');
    7387  AddNew(inHalt, 'HALT', [], 'It terminates program execution and halts processor. Processor can be waked up by interrupt.');
    74   AddNew(inLoadConst, 'LD', [ptReg, ptNumber], 'Sets register to immediate constant value.');
    75   AddNew(inLoadConstSize, 'LD', [ptSize, ptReg, ptNumber], 'Sets register to immediate constant value.');
     88  AddNew(inLoadConst, 'LD', [ptReg, ptData], 'Sets register to immediate constant value.');
     89  AddNew(inLoadConstSize, 'LD', [ptDataWidth, ptReg, ptData], 'Sets register to immediate constant value.');
    7690  AddNew(inLoad, 'LD', [ptReg, ptReg], 'Copies value from one register to another.');
     91  AddNew(inLoadSize, 'LD', [ptDataWidth, ptReg, ptReg], 'Copies value from one register to another.');
     92  AddNew(inLoadMem, 'LD', [ptReg, ptRegIndirect], 'Loads value from memory to register.');
     93  AddNew(inLoadMemSize, 'LD', [ptDataWidth, ptReg, ptRegIndirect], 'Loads value from memory to register.');
     94  AddNew(inStoreMem, 'LD', [ptRegIndirect, ptReg], 'Stores value from register to memory.');
     95  AddNew(inStoreMemSize, 'LD', [ptDataWidth, ptRegIndirect, ptReg], 'Stores value from register to memory.');
     96  AddNew(inLoadMemIndex, 'LD', [ptReg, ptRegIndirectIndex], 'Loads value from memory with numeric index to register.');
     97  AddNew(inLoadMemIndexSize, 'LD', [ptDataWidth, ptReg, ptRegIndirectIndex], 'Loads value from memory with numeric index to register.');
     98  AddNew(inStoreMemIndex, 'LD', [ptRegIndirectIndex, ptReg], 'Stores value from register to memory with numeric index.');
     99  AddNew(inStoreMemIndexSize, 'LD', [ptDataWidth, ptRegIndirectIndex, ptReg], 'Stores value from register to memory with numeric index.');
    77100  AddNew(inInc, 'INC', [ptReg], 'Increments value in specified register.');
     101  AddNew(inIncSize, 'INC', [ptDataWidth, ptReg], 'Increments value in specified register.');
    78102  AddNew(inDec, 'DEC', [ptReg], 'Decrements value in specified register.');
    79   AddNew(inLoadMem, 'LD', [ptReg, ptRegIndirect], 'Loads value from memory to register.');
    80   AddNew(inStoreMem, 'ST', [ptRegIndirect, ptReg], 'Stores value from register to memory.');
     103  AddNew(inDecSize, 'DEC', [ptDataWidth, ptReg], 'Decrements value in specified register.');
    81104  AddNew(inAdd, 'ADD', [ptReg, ptReg], 'Adds second register to first register.');
     105  AddNew(inAddSize, 'ADD', [ptDataWidth, ptReg, ptReg], 'Adds second register to first register.');
    82106  AddNew(inSub, 'SUB', [ptReg, ptReg], 'Subtracts second register from first register.');
     107  AddNew(inSubSize, 'SUB', [ptDataWidth, ptReg, ptReg], 'Subtracts second register from first register.');
    83108  AddNew(inInput, 'IN', [ptReg, ptRegIndirect], 'Reads value from input port to register.');
    84109  AddNew(inOutput, 'OUT', [ptRegIndirect, ptReg], 'Writes value from register to output port.');
    85   AddNew(inJumpZero, 'JZ', [ptReg, ptNumber], 'Jumps to given address if value of register is zero');
    86   AddNew(inJumpNotZero, 'JNZ', [ptReg, ptNumber], 'Jumps to given address if value of register is not zero');
     110  AddNew(inJumpZero, 'JZ', [ptReg, ptAddress], 'Jumps to given address if value of register is zero');
     111  AddNew(inJumpNotZero, 'JNZ', [ptReg, ptAddress], 'Jumps to given address if value of register is not zero');
    87112  AddNew(inPush, 'PUSH', [ptReg], 'Pushes value of register to top of the stack.');
     113  AddNew(inPushSize, 'PUSH', [ptDataWidth, ptReg], 'Pushes value of register to top of the stack.');
    88114  AddNew(inPop, 'POP', [ptReg], 'Pops value of register from top of the stack.');
    89   AddNew(inCall, 'CALL', [ptNumber], 'Calls subroutine at given address. Stores return address to the stack.');
     115  AddNew(inPopSize, 'POP', [ptDataWidth, ptReg], 'Pops value of register from top of the stack.');
     116  AddNew(inCall, 'CALL', [ptAddress], 'Calls subroutine at given address. Stores return address to the stack.');
     117  AddNew(inCallSize, 'CALL', [ptAddressWidth, ptAddress], 'Calls subroutine at given address. Stores return address to the stack.');
    90118  AddNew(inRet, 'RET', [], 'Return from subroutine. Reads return address from top of the stack to IP register.');
     119  AddNew(inRetSize, 'RET', [ptAddressWidth], 'Return from subroutine. Reads return address from top of the stack to IP register.');
    91120  AddNew(inAnd, 'AND', [ptReg, ptReg], 'Bitwise AND operation. Result is stored in first parameter.');
     121  AddNew(inAndSize, 'AND', [ptDataWidth, ptReg, ptReg], 'Bitwise AND operation. Result is stored in first parameter.');
    92122  AddNew(inOr, 'OR', [ptReg, ptReg], 'Bitwise OR operation. Result is stored in first parameter.');
     123  AddNew(inOrSize, 'OR', [ptDataWidth, ptReg, ptReg], 'Bitwise OR operation. Result is stored in first parameter.');
    93124  AddNew(inXor, 'XOR', [ptReg, ptReg], 'Bitwise XOR operation. Result is stored in first parameter.');
    94   AddNew(inXor, 'SHL', [ptReg, ptReg], 'Shifts all bits to the left in first register by n bits according value of second register.');
    95   AddNew(inXor, 'SHR', [ptReg, ptReg], 'Shifts all bits to the right in first register by n bits according value of second register.');
     125  AddNew(inXorSize, 'XOR', [ptDataWidth, ptReg, ptReg], 'Bitwise XOR operation. Result is stored in first parameter.');
     126  AddNew(inShl, 'SHL', [ptReg, ptReg], 'Shifts all bits to the left in first register by n bits according value of second register.');
     127  AddNew(inShlSize, 'SHL', [ptDataWidth, ptReg, ptReg], 'Shifts all bits to the left in first register by n bits according value of second register.');
     128  AddNew(inShr, 'SHR', [ptReg, ptReg], 'Shifts all bits to the right in first register by n bits according value of second register.');
     129  AddNew(inShrSize, 'SHR', [ptDataWidth, ptReg, ptReg], 'Shifts all bits to the right in first register by n bits according value of second register.');
    96130  AddNew(inMul, 'MUL', [ptReg, ptReg], 'Multiplies first register with second register value.');
     131  AddNew(inMulSize, 'MUL', [ptDataWidth, ptReg, ptReg], 'Multiplies first register with second register value.');
    97132  AddNew(inDiv, 'DIV', [ptReg, ptReg], 'Diviedes first register with second register value.');
     133  AddNew(inDivSize, 'DIV', [ptDataWidth, ptReg, ptReg], 'Diviedes first register with second register value.');
    98134  //AddNew(inMod, 'MOD', [ptReg, ptReg], 'Returns modulo in first registr after division of values.');
    99   AddNew(inJump, 'JP', [ptNumber], 'Unconditional absolute jump to defined address.');
    100   AddNew(inJumpRel, 'JR', [ptNumber], 'Unconditional relative jump to defined address.');
    101   //AddNew(inLoadIndex, 'LDI', [ptReg, ptRegIndirectIndex], 'Loads value from memory with numeric index to register.');
    102   //AddNew(inStoreIndex, 'STI', [ptRegIndirectIndex, ptReg], 'Stores value from register to memory with numeric index.');
     135  AddNew(inJump, 'JP', [ptAddress], 'Unconditional absolute jump to defined address.');
     136  AddNew(inJumpSize, 'JP', [ptAddressWidth, ptAddress], 'Unconditional absolute jump to defined address.');
     137  AddNew(inJumpRel, 'JR', [ptAddress], 'Unconditional relative jump to defined address.');
     138  AddNew(inJumpRelSize, 'JR', [ptAddressWidth, ptAddress], 'Unconditional relative jump to defined address.');
    103139  //AddNew(inLoadCpu, 'LDC', [], 'Loads value from system register.');
    104140  //AddNew(inStoreCpu, 'STC', [], 'Stores value to system register.');
     
    107143end;
    108144
    109 destructor TInstructionSet.Destroy;
    110 begin
    111   FreeAndNil(Items);
    112   inherited;
    113 end;
    114 
    115145end.
    116146
Note: See TracChangeset for help on using the changeset viewer.