Changeset 7 for branches/bigint/Cpu.pas


Ignore:
Timestamp:
Aug 1, 2024, 11:47:03 PM (3 months ago)
Author:
chronos
Message:
  • Added: More instructions implemented.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.