Changeset 7 for branches/bigint
- Timestamp:
- Aug 1, 2024, 11:47:03 PM (4 months ago)
- Location:
- branches/bigint
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/bigint/Assembler.pas
r4 r7 213 213 ParseNumParam(Token); 214 214 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 215 225 if InstructionInfo.Params[I] = ptIndirect then begin 216 226 Parser.Expect(tkSpecialSymbol, '('); -
branches/bigint/Cpu.pas
r3 r7 10 10 11 11 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); 13 14 14 15 { TCpu } … … 25 26 procedure InstructionLoad; 26 27 procedure InstructionLoadConst; 28 procedure InstructionLoadIndex; 29 procedure InstructionLoadMem; 30 procedure InstructionStoreMem; 27 31 procedure InstructionInput; 28 32 procedure InstructionOutput; … … 32 36 procedure InstructionJumpZero; 33 37 procedure InstructionJumpNotZero; 38 procedure InstructionPush; 39 procedure InstructionPop; 40 procedure InstructionCall; 41 procedure InstructionRet; 34 42 procedure WriteMem(Address, Data: TInt); 35 43 function ReadMem(Address: TInt): TInt; … … 37 45 procedure WriteIo(Address, Data: TInt); 38 46 function ReadIo(Address: TInt): TInt; 47 procedure Push(Data: TInt); 48 function Pop: TInt; 39 49 public 40 50 Halted: Boolean; … … 83 93 end; 84 94 95 procedure TCpu.InstructionLoadIndex; 96 var 97 Dst, Src, Index: TInt; 98 begin 99 Dst := ReadMemPc; 100 Src := ReadMemPc; 101 Index := ReadMemPc; 102 WriteMem(Dst, ReadMem(Src + Index)); 103 end; 104 105 procedure TCpu.InstructionLoadMem; 106 var 107 Dst, Src: TInt; 108 begin 109 Dst := ReadMemPc; 110 Src := ReadMemPc; 111 WriteMem(Dst, ReadMem(ReadMem(Src))); 112 end; 113 114 procedure TCpu.InstructionStoreMem; 115 var 116 Dst, Src: TInt; 117 begin 118 Dst := ReadMemPc; 119 Src := ReadMemPc; 120 WriteMem(ReadMem(Dst), ReadMem(Src)); 121 end; 122 85 123 procedure TCpu.InstructionInput; 86 124 var … … 119 157 procedure TCpu.InstructionJump; 120 158 begin 121 PC := ReadMem (ReadMemPc);159 PC := ReadMemPc; 122 160 end; 123 161 … … 140 178 Addr := ReadMemPc; 141 179 if ReadMem(Condition) <> 0 then PC := Addr; 180 end; 181 182 procedure TCpu.InstructionPush; 183 begin 184 Push(ReadMem(ReadMemPc)); 185 end; 186 187 procedure TCpu.InstructionPop; 188 begin 189 WriteMem(ReadMemPc, Pop); 190 end; 191 192 procedure TCpu.InstructionCall; 193 var 194 Addr: TInt; 195 begin 196 Addr := ReadMemPc; 197 Push(PC); 198 PC := Addr; 199 end; 200 201 procedure TCpu.InstructionRet; 202 begin 203 PC := Pop; 142 204 end; 143 205 … … 170 232 end; 171 233 234 procedure TCpu.Push(Data: TInt); 235 begin 236 Dec(SP); 237 WriteMem(SP, Data); 238 end; 239 240 function TCpu.Pop: TInt; 241 begin 242 Result := ReadMem(SP); 243 Inc(SP); 244 end; 245 172 246 procedure TCpu.Reset; 173 247 begin 174 248 PC := 0; 175 SP := 0;249 SP := 1000; 176 250 Halted := False; 177 251 end; … … 198 272 Instructions[inHalt] := InstructionHalt; 199 273 Instructions[inLoad] := InstructionLoad; 274 Instructions[inLoadIndex] := InstructionLoadIndex; 200 275 Instructions[inLoadConst] := InstructionLoadConst; 276 Instructions[inLoadMem] := InstructionLoadMem; 277 Instructions[inStoreMem] := InstructionStoreMem; 201 278 Instructions[inInput] := InstructionInput; 202 279 Instructions[inOutput] := InstructionOutput; … … 206 283 Instructions[inInc] := InstructionInc; 207 284 Instructions[inDec] := InstructionDec; 285 Instructions[inPush] := InstructionPush; 286 Instructions[inPop] := InstructionPop; 287 Instructions[inCall] := InstructionCall; 288 Instructions[inRet] := InstructionRet; 208 289 end; 209 290 -
branches/bigint/Disassembler.pas
r6 r7 54 54 if InstructionInfo.Params[I] = ptIndirect then begin 55 55 InstText := InstText + '(' + IntToStr(Value) + ')'; 56 end else 57 if InstructionInfo.Params[I] = ptIndirect2 then begin 58 InstText := InstText + '((' + IntToStr(Value) + '))'; 56 59 end else raise Exception.Create('Unsupported instruction parameter type'); 57 60 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 10 PrintInc: 1 11 LDC (1000), 65 2 12 LDC (1001), 30 … … 6 16 DEC (1001) 7 17 JPNZ (1001), Label 8 HALT 18 RET 19 20 PrintText: 21 LDM (1001), ((1000)) 22 JPZ (1001), Label2 23 OUT (0), (1001) 24 INC (1000) 25 JP PrintText 26 Label2: 27 RET 28 29 Text: 30 DB 'Text ', 0 -
branches/bigint/Instructions.pas
r4 r7 7 7 8 8 type 9 TParamType = (ptNone, ptNumber, ptIndirect );9 TParamType = (ptNone, ptNumber, ptIndirect, ptIndirect2); 10 10 TParamTypeArray = array of TParamType; 11 11 … … 74 74 AddNew(inHalt, 'HALT', [], 'It terminates program execution and halts processor. Processor can be waked up by interrupt.'); 75 75 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.'); 77 80 AddNew(inInc, 'INC', [ptIndirect], 'Increments value in specified register.'); 78 81 AddNew(inDec, 'DEC', [ptIndirect], 'Decrements value in specified register.'); … … 82 85 AddNew(inJumpZero, 'JPZ', [ptIndirect, ptNumber], 'Jumps to given address if value of register is zero'); 83 86 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.'); 84 91 end; 85 92
Note:
See TracChangeset
for help on using the changeset viewer.