Changeset 7


Ignore:
Timestamp:
Aug 1, 2024, 11:47:03 PM (4 months ago)
Author:
chronos
Message:
  • Added: More instructions implemented.
Location:
branches/bigint
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/bigint/Assembler.pas

    r4 r7  
    213213        ParseNumParam(Token);
    214214      end else
     215      if InstructionInfo.Params[I] = ptIndirect2 then begin
     216        Parser.Expect(tkSpecialSymbol, '((');
     217        Parser.Expect(tkSpecialSymbol, '(');
     218        Token := Parser.ReadNext;
     219        if TryStrToInt(Token.Value, Number) then
     220          Memory.WritePos(Number)
     221          else Error('Expected numeric index error', Token.Pos);
     222        Parser.Expect(tkSpecialSymbol, '))');
     223        Parser.Expect(tkSpecialSymbol, '))');
     224      end else
    215225      if InstructionInfo.Params[I] = ptIndirect then begin
    216226        Parser.Expect(tkSpecialSymbol, '(');
  • branches/bigint/Cpu.pas

    r3 r7  
    1010
    1111  TInstruction = (inNop, inHalt, inLoad, inLoadConst, inInput, inOutput,
    12     inJump, inJumpZero, inJumpNotZero, inInc, inDec);
     12    inJump, inJumpZero, inJumpNotZero, inInc, inDec, inLoadIndex, inPush, inPop,
     13    inCall, inRet, inLoadMem, inStoreMem);
    1314
    1415  { TCpu }
     
    2526    procedure InstructionLoad;
    2627    procedure InstructionLoadConst;
     28    procedure InstructionLoadIndex;
     29    procedure InstructionLoadMem;
     30    procedure InstructionStoreMem;
    2731    procedure InstructionInput;
    2832    procedure InstructionOutput;
     
    3236    procedure InstructionJumpZero;
    3337    procedure InstructionJumpNotZero;
     38    procedure InstructionPush;
     39    procedure InstructionPop;
     40    procedure InstructionCall;
     41    procedure InstructionRet;
    3442    procedure WriteMem(Address, Data: TInt);
    3543    function ReadMem(Address: TInt): TInt;
     
    3745    procedure WriteIo(Address, Data: TInt);
    3846    function ReadIo(Address: TInt): TInt;
     47    procedure Push(Data: TInt);
     48    function Pop: TInt;
    3949  public
    4050    Halted: Boolean;
     
    8393end;
    8494
     95procedure TCpu.InstructionLoadIndex;
     96var
     97  Dst, Src, Index: TInt;
     98begin
     99  Dst := ReadMemPc;
     100  Src := ReadMemPc;
     101  Index := ReadMemPc;
     102  WriteMem(Dst, ReadMem(Src + Index));
     103end;
     104
     105procedure TCpu.InstructionLoadMem;
     106var
     107  Dst, Src: TInt;
     108begin
     109  Dst := ReadMemPc;
     110  Src := ReadMemPc;
     111  WriteMem(Dst, ReadMem(ReadMem(Src)));
     112end;
     113
     114procedure TCpu.InstructionStoreMem;
     115var
     116  Dst, Src: TInt;
     117begin
     118  Dst := ReadMemPc;
     119  Src := ReadMemPc;
     120  WriteMem(ReadMem(Dst), ReadMem(Src));
     121end;
     122
    85123procedure TCpu.InstructionInput;
    86124var
     
    119157procedure TCpu.InstructionJump;
    120158begin
    121   PC := ReadMem(ReadMemPc);
     159  PC := ReadMemPc;
    122160end;
    123161
     
    140178  Addr := ReadMemPc;
    141179  if ReadMem(Condition) <> 0 then PC := Addr;
     180end;
     181
     182procedure TCpu.InstructionPush;
     183begin
     184  Push(ReadMem(ReadMemPc));
     185end;
     186
     187procedure TCpu.InstructionPop;
     188begin
     189  WriteMem(ReadMemPc, Pop);
     190end;
     191
     192procedure TCpu.InstructionCall;
     193var
     194  Addr: TInt;
     195begin
     196  Addr := ReadMemPc;
     197  Push(PC);
     198  PC := Addr;
     199end;
     200
     201procedure TCpu.InstructionRet;
     202begin
     203  PC := Pop;
    142204end;
    143205
     
    170232end;
    171233
     234procedure TCpu.Push(Data: TInt);
     235begin
     236  Dec(SP);
     237  WriteMem(SP, Data);
     238end;
     239
     240function TCpu.Pop: TInt;
     241begin
     242  Result := ReadMem(SP);
     243  Inc(SP);
     244end;
     245
    172246procedure TCpu.Reset;
    173247begin
    174248  PC := 0;
    175   SP := 0;
     249  SP := 1000;
    176250  Halted := False;
    177251end;
     
    198272  Instructions[inHalt] := InstructionHalt;
    199273  Instructions[inLoad] := InstructionLoad;
     274  Instructions[inLoadIndex] := InstructionLoadIndex;
    200275  Instructions[inLoadConst] := InstructionLoadConst;
     276  Instructions[inLoadMem] := InstructionLoadMem;
     277  Instructions[inStoreMem] := InstructionStoreMem;
    201278  Instructions[inInput] := InstructionInput;
    202279  Instructions[inOutput] := InstructionOutput;
     
    206283  Instructions[inInc] := InstructionInc;
    207284  Instructions[inDec] := InstructionDec;
     285  Instructions[inPush] := InstructionPush;
     286  Instructions[inPop] := InstructionPop;
     287  Instructions[inCall] := InstructionCall;
     288  Instructions[inRet] := InstructionRet;
    208289end;
    209290
  • branches/bigint/Disassembler.pas

    r6 r7  
    5454          if InstructionInfo.Params[I] = ptIndirect then begin
    5555            InstText := InstText + '(' + IntToStr(Value) + ')';
     56          end else
     57          if InstructionInfo.Params[I] = ptIndirect2 then begin
     58            InstText := InstText + '((' + IntToStr(Value) + '))';
    5659          end else raise Exception.Create('Unsupported instruction parameter type');
    5760        end;
  • branches/bigint/Example.asm

    r4 r7  
     1  LDC  (1000), Text
     2  CALL PrintText
     3  LDC  (1000), Text
     4  CALL PrintText
     5  CALL PrintInc
     6  CALL PrintInc
     7  HALT
     8
     9
     10PrintInc:
    111  LDC  (1000), 65
    212  LDC  (1001), 30
     
    616  DEC  (1001)
    717  JPNZ (1001), Label
    8   HALT
     18  RET
     19
     20PrintText:
     21  LDM  (1001), ((1000))
     22  JPZ  (1001), Label2
     23  OUT  (0), (1001)
     24  INC  (1000)
     25  JP   PrintText
     26Label2:
     27  RET
     28
     29Text:
     30  DB 'Text ', 0
  • branches/bigint/Instructions.pas

    r4 r7  
    77
    88type
    9   TParamType = (ptNone, ptNumber, ptIndirect);
     9  TParamType = (ptNone, ptNumber, ptIndirect, ptIndirect2);
    1010  TParamTypeArray = array of TParamType;
    1111
     
    7474  AddNew(inHalt, 'HALT', [], 'It terminates program execution and halts processor. Processor can be waked up by interrupt.');
    7575  AddNew(inLoadConst, 'LDC', [ptIndirect, ptNumber], 'Loads constant into address location.');
    76   AddNew(inLoad, 'LD', [ptIndirect, ptIndirect], 'Copies value from one register to another.');
     76  AddNew(inLoadIndex, 'LDI', [ptIndirect, ptIndirect, ptNumber], 'Loads value from one memory location with added relative index to another.');
     77  AddNew(inLoad, 'LD', [ptIndirect, ptIndirect], 'Loads value from one memory location to another.');
     78  AddNew(inLoadMem, 'LDM', [ptIndirect, ptIndirect2], 'Loads value from one indirect memory location to another.');
     79  AddNew(inStoreMem, 'STM', [ptIndirect2, ptIndirect], 'Stotes value to indirect memory location from source location.');
    7780  AddNew(inInc, 'INC', [ptIndirect], 'Increments value in specified register.');
    7881  AddNew(inDec, 'DEC', [ptIndirect], 'Decrements value in specified register.');
     
    8285  AddNew(inJumpZero, 'JPZ', [ptIndirect, ptNumber], 'Jumps to given address if value of register is zero');
    8386  AddNew(inJumpNotZero, 'JPNZ', [ptIndirect, ptNumber], 'Jumps to given address if value of register is not zero');
     87  AddNew(inPush, 'PUSH', [ptIndirect], 'Push memory location onto stack.');
     88  AddNew(inPop, 'POP', [ptIndirect], 'Pop item from stack and store it into memory location.');
     89  AddNew(inCall, 'CALL', [ptNumber], 'Call subroutine.');
     90  AddNew(inRet, 'RET', [], 'Return from subrotine.');
    8491end;
    8592
Note: See TracChangeset for help on using the changeset viewer.