Changeset 12 for branches/bigint


Ignore:
Timestamp:
Apr 24, 2025, 10:12:32 PM (2 weeks ago)
Author:
chronos
Message:
  • Added: More CPU instructions.
  • Added: Mouse device.
Location:
branches/bigint
Files:
4 added
2 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • branches/bigint/BigIntVM.lpi

    r8 r12  
    2424          <SearchPaths>
    2525            <IncludeFiles Value="$(ProjOutDir)"/>
    26             <OtherUnitFiles Value="Forms"/>
     26            <OtherUnitFiles Value="Forms;Devices"/>
    2727            <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)-$(BuildMode)"/>
    2828          </SearchPaths>
     
    6060      </Item>
    6161      <SharedMatrixOptions Count="2">
    62         <Item1 Targets="Common" Modes="Debug" Value="-g -gl -gh -CirotR -O1"/>
     62        <Item1 ID="496997618487" Targets="Common" Modes="Debug" Value="-g -gl -gh -CirotR -O1"/>
    6363        <Item2 ID="202440807568" Targets="Common" Modes="Release" Value="-CX -XX -O3"/>
    6464      </SharedMatrixOptions>
     
    113113      </Unit>
    114114      <Unit>
    115         <Filename Value="Screen.pas"/>
    116         <IsPartOfProject Value="True"/>
    117       </Unit>
    118       <Unit>
    119         <Filename Value="Console.pas"/>
     115        <Filename Value="Devices/Screen.pas"/>
     116        <IsPartOfProject Value="True"/>
     117      </Unit>
     118      <Unit>
     119        <Filename Value="Devices/Console.pas"/>
    120120        <IsPartOfProject Value="True"/>
    121121      </Unit>
     
    158158        <IsPartOfProject Value="True"/>
    159159        <ComponentName Value="FormMemory"/>
     160      </Unit>
     161      <Unit>
     162        <Filename Value="Devices/Mouse.pas"/>
     163        <IsPartOfProject Value="True"/>
    160164      </Unit>
    161165    </Units>
     
    168172    <SearchPaths>
    169173      <IncludeFiles Value="$(ProjOutDir)"/>
    170       <OtherUnitFiles Value="Forms"/>
     174      <OtherUnitFiles Value="Forms;Devices"/>
    171175      <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)-$(BuildMode)"/>
    172176    </SearchPaths>
  • branches/bigint/BigIntVM.lpr

    r8 r12  
    1111  Forms, FormMain, FormDisassembler, FormMemory, Cpu, IntMemory, Int, Machine,
    1212  DeviceManager, Screen, Console, Device, Assembler, Instructions, Parser,
    13   Message, Disassembler, CommonPackage
     13  Message, Disassembler, CommonPackage, Mouse
    1414  { you can add units after this };
    1515
  • branches/bigint/Cpu.pas

    r7 r12  
    1010
    1111  TInstruction = (inNop, inHalt, inLoad, inLoadConst, inInput, inOutput,
    12     inJump, inJumpZero, inJumpNotZero, inInc, inDec, inLoadIndex, inPush, inPop,
    13     inCall, inRet, inLoadMem, inStoreMem);
     12    inJump, inJumpZero, inJumpNotZero, inIncrement, inDecrement, inLoadIndex,
     13    inPush, inPop, inCall, inReturn, inLoadMem, inStoreMem, inAdd, inSubtract,
     14    inAnd, inOr, inXor, inShiftLeft, inShiftRight, inJumpRel, inJumpRelZero,
     15    inJumpRelNotZero, inBitSet, inBitReset, inBitTest, inGetMaxInt, inGetMinInt);
    1416
    1517  { TCpu }
     
    3638    procedure InstructionJumpZero;
    3739    procedure InstructionJumpNotZero;
     40    procedure InstructionJumpRel;
     41    procedure InstructionJumpRelZero;
     42    procedure InstructionJumpRelNotZero;
    3843    procedure InstructionPush;
    3944    procedure InstructionPop;
    4045    procedure InstructionCall;
    4146    procedure InstructionRet;
     47    procedure InstructionAdd;
     48    procedure InstructionSub;
     49    procedure InstructionAnd;
     50    procedure InstructionOr;
     51    procedure InstructionXor;
     52    procedure InstructionShl;
     53    procedure InstructionShr;
     54    procedure InstructionBitSet;
     55    procedure InstructionBitReset;
     56    procedure InstructionBitTest;
     57    procedure InstructionGetMaxInt;
     58    procedure InstructionGetMinInt;
    4259    procedure WriteMem(Address, Data: TInt);
    4360    function ReadMem(Address: TInt): TInt;
     
    180197end;
    181198
     199procedure TCpu.InstructionJumpRel;
     200var
     201  Addr: TInt;
     202begin
     203  Addr := ReadMemPc;
     204  PC := PC + Addr;
     205end;
     206
     207procedure TCpu.InstructionJumpRelZero;
     208var
     209  Condition: TInt;
     210  Addr: TInt;
     211begin
     212  Condition := ReadMemPc;
     213  Addr := ReadMemPc;
     214  if ReadMem(Condition) = 0 then PC := PC + Addr;
     215end;
     216
     217procedure TCpu.InstructionJumpRelNotZero;
     218var
     219  Condition: TInt;
     220  Addr: TInt;
     221begin
     222  Condition := ReadMemPc;
     223  Addr := ReadMemPc;
     224  if ReadMem(Condition) <> 0 then PC := PC + Addr;
     225end;
     226
    182227procedure TCpu.InstructionPush;
    183228begin
     
    202247begin
    203248  PC := Pop;
     249end;
     250
     251procedure TCpu.InstructionAdd;
     252var
     253  Dst, Src1, Src2: TInt;
     254begin
     255  Dst := ReadMemPc;
     256  Src1 := ReadMemPc;
     257  Src2 := ReadMemPc;
     258  WriteMem(Dst, ReadMem(Src1) + ReadMem(Src2));
     259end;
     260
     261procedure TCpu.InstructionSub;
     262var
     263  Dst, Src1, Src2: TInt;
     264begin
     265  Dst := ReadMemPc;
     266  Src1 := ReadMemPc;
     267  Src2 := ReadMemPc;
     268  WriteMem(Dst, ReadMem(Src1) - ReadMem(Src2));
     269end;
     270
     271procedure TCpu.InstructionAnd;
     272var
     273  Dst, Src1, Src2: TInt;
     274begin
     275  Dst := ReadMemPc;
     276  Src1 := ReadMemPc;
     277  Src2 := ReadMemPc;
     278  WriteMem(Dst, ReadMem(Src1) and ReadMem(Src2));
     279end;
     280
     281procedure TCpu.InstructionOr;
     282var
     283  Dst, Src1, Src2: TInt;
     284begin
     285  Dst := ReadMemPc;
     286  Src1 := ReadMemPc;
     287  Src2 := ReadMemPc;
     288  WriteMem(Dst, ReadMem(Src1) or ReadMem(Src2));
     289end;
     290
     291procedure TCpu.InstructionXor;
     292var
     293  Dst, Src1, Src2: TInt;
     294begin
     295  Dst := ReadMemPc;
     296  Src1 := ReadMemPc;
     297  Src2 := ReadMemPc;
     298  WriteMem(Dst, ReadMem(Src1) xor ReadMem(Src2));
     299end;
     300
     301procedure TCpu.InstructionShl;
     302var
     303  Dst, Src, Shift: TInt;
     304begin
     305  Dst := ReadMemPc;
     306  Src := ReadMemPc;
     307  Shift := ReadMemPc;
     308  WriteMem(Dst, ReadMem(Src) shl ReadMem(Shift));
     309end;
     310
     311procedure TCpu.InstructionShr;
     312var
     313  Dst, Src, Shift: TInt;
     314begin
     315  Dst := ReadMemPc;
     316  Src := ReadMemPc;
     317  Shift := ReadMemPc;
     318  WriteMem(Dst, ReadMem(Src) shr ReadMem(Shift));
     319end;
     320
     321procedure TCpu.InstructionBitSet;
     322var
     323  Dst, Src, Bit: TInt;
     324begin
     325  Dst := ReadMemPc;
     326  Src := ReadMemPc;
     327  Bit := ReadMemPc;
     328  WriteMem(Dst, ReadMem(Src) or (1 << ReadMem(Bit)));
     329end;
     330
     331procedure TCpu.InstructionBitReset;
     332var
     333  Dst, Src, Bit: TInt;
     334begin
     335  Dst := ReadMemPc;
     336  Src := ReadMemPc;
     337  Bit := ReadMemPc;
     338  WriteMem(Dst, ReadMem(Src) and (-1 xor (1 << ReadMem(Bit))));
     339end;
     340
     341procedure TCpu.InstructionBitTest;
     342var
     343  Dst, Src, Bit: TInt;
     344begin
     345  Dst := ReadMemPc;
     346  Src := ReadMemPc;
     347  Bit := ReadMemPc;
     348  WriteMem(Dst, (ReadMem(Src) >> ReadMem(Bit)) and 1);
     349end;
     350
     351procedure TCpu.InstructionGetMaxInt;
     352var
     353  Dst: TInt;
     354begin
     355  Dst := ReadMemPc;
     356  WriteMem(Dst, SizeOf(TInt) * 8);
     357end;
     358
     359procedure TCpu.InstructionGetMinInt;
     360var
     361  Dst: TInt;
     362begin
     363  Dst := ReadMemPc;
     364//  WriteMem(Dst, SizeOf(TInt) * 8);
    204365end;
    205366
     
    281442  Instructions[inJumpZero] := InstructionJumpZero;
    282443  Instructions[inJumpNotZero] := InstructionJumpNotZero;
    283   Instructions[inInc] := InstructionInc;
    284   Instructions[inDec] := InstructionDec;
     444  Instructions[inJumpRel] := InstructionJumpRel;
     445  Instructions[inJumpRelZero] := InstructionJumpRelZero;
     446  Instructions[inJumpRelNotZero] := InstructionJumpRelNotZero;
     447  Instructions[inIncrement] := InstructionInc;
     448  Instructions[inDecrement] := InstructionDec;
    285449  Instructions[inPush] := InstructionPush;
    286450  Instructions[inPop] := InstructionPop;
    287451  Instructions[inCall] := InstructionCall;
    288   Instructions[inRet] := InstructionRet;
     452  Instructions[inReturn] := InstructionRet;
     453  Instructions[inAdd] := InstructionAdd;
     454  Instructions[inSubtract] := InstructionSub;
     455  Instructions[inAnd] := InstructionAnd;
     456  Instructions[inOr] := InstructionOr;
     457  Instructions[inXor] := InstructionXor;
     458  Instructions[inShiftLeft] := InstructionShl;
     459  Instructions[inShiftRight] := InstructionShr;
     460  Instructions[inBitSet] := InstructionBitSet;
     461  Instructions[inBitReset] := InstructionBitReset;
     462  Instructions[inBitTest] := InstructionBitTest;
     463  Instructions[inGetMaxInt] := InstructionGetMaxInt;
     464  Instructions[inGetMinInt] := InstructionGetMinInt;
    289465end;
    290466
  • branches/bigint/Example.asm

    r7 r12  
     1  VAR  Count, 30
     2
    13  LDC  (1000), Text
    24  CALL PrintText
  • branches/bigint/Forms/FormMain.lfm

    r6 r12  
    1111  OnDestroy = FormDestroy
    1212  OnShow = FormShow
    13   LCLVersion = '3.4.0.0'
     13  LCLVersion = '3.6.0.0'
    1414  object Memo1: TMemo
    1515    Left = 0
  • branches/bigint/Forms/FormMain.pas

    r8 r12  
    1818  private
    1919    procedure ConsoleWrite(Sender: TObject);
     20    function MouseGetPosition: TPoint;
    2021  public
    2122    SubFormDisassembler: TFormDisassembler;
     
    3839  Machine := TMachine.Create;
    3940  Machine.Console.OnWrite := ConsoleWrite;
     41  Machine.Mouse.OnGetPosition := MouseGetPosition;
    4042end;
    4143
     
    8183end;
    8284
     85function TFormMain.MouseGetPosition: TPoint;
     86begin
     87  Result := Mouse.CursorPos;
     88end;
     89
    8390end.
    8491
  • branches/bigint/Instructions.pas

    r7 r12  
    7878  AddNew(inLoadMem, 'LDM', [ptIndirect, ptIndirect2], 'Loads value from one indirect memory location to another.');
    7979  AddNew(inStoreMem, 'STM', [ptIndirect2, ptIndirect], 'Stotes value to indirect memory location from source location.');
    80   AddNew(inInc, 'INC', [ptIndirect], 'Increments value in specified register.');
    81   AddNew(inDec, 'DEC', [ptIndirect], 'Decrements value in specified register.');
     80  AddNew(inIncrement, 'INC', [ptIndirect], 'Increments value in specified register.');
     81  AddNew(inDecrement, 'DEC', [ptIndirect], 'Decrements value in specified register.');
    8282  AddNew(inInput, 'IN', [ptIndirect, ptIndirect], 'Reads value from input port to register.');
    8383  AddNew(inOutput, 'OUT', [ptIndirect, ptIndirect], 'Writes value from register to output port.');
    8484  AddNew(inJump, 'JP', [ptNumber], 'Unconditional absolute jump to defined address.');
    85   AddNew(inJumpZero, 'JPZ', [ptIndirect, ptNumber], 'Jumps to given address if value of register is zero');
    86   AddNew(inJumpNotZero, 'JPNZ', [ptIndirect, ptNumber], 'Jumps to given address if value of register is not zero');
     85  AddNew(inJumpZero, 'JPZ', [ptIndirect, ptNumber], 'Jumps to absolute address if value of register is zero');
     86  AddNew(inJumpNotZero, 'JPNZ', [ptIndirect, ptNumber], 'Jumps to absolute address if value of register is not zero');
     87  AddNew(inJumpRel, 'JR', [ptNumber], 'Unconditional relative jump to defined address.');
     88  AddNew(inJumpRelZero, 'JRZ', [ptIndirect, ptNumber], 'Jumps to relative address if value of register is zero');
     89  AddNew(inJumpRelNotZero, 'JRNZ', [ptIndirect, ptNumber], 'Jumps to relative address if value of register is not zero');
    8790  AddNew(inPush, 'PUSH', [ptIndirect], 'Push memory location onto stack.');
    8891  AddNew(inPop, 'POP', [ptIndirect], 'Pop item from stack and store it into memory location.');
    8992  AddNew(inCall, 'CALL', [ptNumber], 'Call subroutine.');
    90   AddNew(inRet, 'RET', [], 'Return from subrotine.');
     93  AddNew(inReturn, 'RET', [], 'Return from subrotine.');
     94  AddNew(inAdd, 'ADD', [ptIndirect, ptIndirect, ptIndirect], 'Addition of two numbers.');
     95  AddNew(inSubtract, 'Sub', [ptIndirect, ptIndirect, ptIndirect], 'Subtraction of two numbers.');
     96  AddNew(inAnd, 'AND', [ptIndirect, ptIndirect, ptIndirect], 'Logical AND operation.');
     97  AddNew(inOr, 'OR', [ptIndirect, ptIndirect, ptIndirect], 'Logical OR operation.');
     98  AddNew(inXor, 'XOR', [ptIndirect, ptIndirect, ptIndirect], 'Logical XOR operation.');
     99  AddNew(inShiftLeft, 'SHL', [ptIndirect, ptIndirect, ptIndirect], 'Bitwise shift to the left.');
     100  AddNew(inShiftRight, 'SHR', [ptIndirect, ptIndirect, ptIndirect], 'Bitwise shift to the right.');
     101  AddNew(inBitSet, 'SET', [ptIndirect, ptIndirect, ptIndirect], 'Set specific bit.');
     102  AddNew(inBitReset, 'RES', [ptIndirect, ptIndirect, ptIndirect], 'Reset specific bit.');
     103  AddNew(inBitTest, 'BIT', [ptIndirect, ptIndirect, ptIndirect], 'Test of specific bit.');
    91104end;
    92105
  • branches/bigint/Machine.pas

    r8 r12  
    44
    55uses
    6   Classes, SysUtils, Cpu, IntMemory, Console, DeviceManager;
     6  Classes, SysUtils, Cpu, IntMemory, Console, DeviceManager, Mouse;
    77
    88type
     
    1111
    1212  TMachine = class
     13  public
    1314    Cpu: TCpu;
    1415    Memory: TIntMemory;
    1516    Console: TConsole;
     17    Mouse: TMouse;
    1618    DeviceManager: TDeviceManager;
    1719    constructor Create;
     
    2931  Memory.Size := 10000;
    3032  Console := TConsole.Create;
     33  Mouse := TMouse.Create;
    3134  DeviceManager := TDeviceManager.Create;
    3235  DeviceManager.RegisterDevice(Console);
     36  DeviceManager.RegisterDevice(Mouse);
    3337  Cpu := TCpu.Create;
    3438  Cpu.OnWriteMem := Memory.Write;
     
    4448  FreeAndNil(DeviceManager);
    4549  FreeAndNil(Console);
     50  FreeAndNil(Mouse);
    4651  inherited;
    4752end;
Note: See TracChangeset for help on using the changeset viewer.