Changeset 56
- Timestamp:
- Nov 22, 2023, 11:37:44 PM (12 months ago)
- Location:
- branches/ByteArray
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ByteArray/Assembler.pas
r55 r56 29 29 function ParseLabel: Boolean; 30 30 procedure UpdateLabelRefs; 31 procedure ParseNumParam(Token: TToken); 31 function ParseNumParam(out Number: TBigInt): Boolean; 32 function ParseReg(out RegIndex: TRegIndex): Boolean; 32 33 public 33 34 InstructionSet: TInstructionSet; … … 36 37 LabelRefs: TList<TLabelRef>; 37 38 Variables: TDictionary<string, TBigInt>; 38 Message : TMessages;39 Messages: TMessages; 39 40 procedure Error(Text: string; Pos: TPoint); 40 41 procedure Compile(Source: string); … … 49 50 implementation 50 51 52 resourcestring 53 SCompilationFinished = 'Compilation finished.'; 54 SExpectedNumber = 'Expected number.'; 55 SExpectedRegisterName = 'Expected register name.'; 56 SExpectedNumericIndex = 'Expected numeric register index.'; 57 SExpectedVariableName = 'Expected variable name.'; 58 SExpectedVariableValue = 'Expected variable value.'; 59 SUnsupportedParameterType = 'Unsupported parameter type'; 60 51 61 { TLabelRef } 52 62 … … 71 81 end; 72 82 73 procedure TAssembler.ParseNumParam(Token: TToken); 74 var 75 Addr: TBigInt; 76 begin 83 function TAssembler.ParseNumParam(out Number: TBigInt): Boolean; 84 var 85 Token: TToken; 86 begin 87 Result := False; 88 Token := Parser.ReadNext; 77 89 if Token.Kind = tkNumber then begin 78 if TryStrToBigInt(Token.Value, Addr) then begin79 Memory.WritePos(1, Addr);90 if TryStrToBigInt(Token.Value, Number) then begin 91 Result := True; 80 92 end; 81 93 end else 82 94 if Token.Kind = tkIdentifier then begin; 83 if Variables.TryGetValue(Token.Value, Addr) then begin84 Memory.WritePos(1, Addr);95 if Variables.TryGetValue(Token.Value, Number) then begin 96 Result := True; 85 97 end else 86 if Labels.TryGetValue(Token.Value, Addr) then begin87 Memory.WritePos(1, Addr);98 if Labels.TryGetValue(Token.Value, Number) then begin 99 Result := True; 88 100 end else begin 89 101 LabelRefs.Add(TLabelRef.Create(Token.Value, Memory.Position)); 90 Memory.WritePos(1, 0); 102 Number := 0; 103 Result := True; 91 104 end; 92 105 end else Error('Unexpected token ' + Token.Value + '.', Token.Pos); 93 106 end; 94 107 108 function TAssembler.ParseReg(out RegIndex: TRegIndex): Boolean; 109 var 110 LastPos: TParserPos; 111 Token: TToken; 112 begin 113 Result := False; 114 LastPos := Parser.Pos; 115 Token := Parser.ReadNext; 116 if (Length(Token.Value) = 1) and (Token.Value[1] in ['A'..'H']) then begin 117 RegIndex := TRegIndex(Ord(Token.Value[1]) - Ord('A')); 118 Result := True; 119 end else Parser.Pos := LastPos; 120 end; 121 95 122 procedure TAssembler.Error(Text: string; Pos: TPoint); 96 123 begin 97 Message .AddMessage(Text, Pos);124 Messages.AddMessage(Text, Pos); 98 125 if Assigned(FOnError) then 99 126 FOnError(Text, Pos); … … 102 129 procedure TAssembler.Compile(Source: string); 103 130 begin 104 Message.Clear; 105 Memory.Size := 0; 131 Messages.Clear; 132 Memory.Size := 1000; 133 Memory.Position := 0; 106 134 Labels.Clear; 107 135 LabelRefs.Clear; … … 123 151 Parser.Expect(tkEof); 124 152 UpdateLabelRefs; 125 Error('Compilation finished.', Point(0, 0)); 153 Error(SCompilationFinished, Point(0, 0)); 154 Memory.Size := Memory.Position; 126 155 end; 127 156 … … 145 174 else Error('Expected number', TokenValue.Pos); 146 175 end else Error('Duplicate variable name ' + TokenName.Value, TokenName.Pos); 147 end else Error( 'Expected variable value.', TokenValue.Pos);148 end else Error( 'Expected variable name.', TokenName.Pos);176 end else Error(SExpectedVariableValue, TokenValue.Pos); 177 end else Error(SExpectedVariableName, TokenName.Pos); 149 178 if Parser.CheckNextAndRead(tkSpecialSymbol, ',') then begin 150 179 Continue; … … 158 187 var 159 188 Token: TToken; 189 Number: TBigInt; 160 190 begin 161 191 Result := False; … … 163 193 Result := True; 164 194 while True do begin 165 Token := Parser.ReadNext; 166 if Token.Kind = tkString then begin 167 Memory.WriteStringPos(Token.Value); 168 end else 169 ParseNumParam(Token); 195 if ParseNumParam(Number) then begin 196 Memory.WritePos(1, Number); 197 end else begin 198 Token := Parser.ReadNext; 199 if Token.Kind = tkString then begin 200 Memory.WriteStringPos(Token.Value); 201 end else raise Exception.Create('Expected parameter'); 202 end; 170 203 if Parser.CheckNextAndRead(tkSpecialSymbol, ',') then begin 171 204 Continue; … … 196 229 Token: TToken; 197 230 LastPos: TParserPos; 198 Number: Integer; 231 RegIndex: TRegIndex; 232 Number: TBigInt; 199 233 begin 200 234 Result := False; … … 209 243 Parser.Expect(tkSpecialSymbol, ','); 210 244 if InstructionInfo.Params[I] = ptNumber then begin 211 Token := Parser.ReadNext;212 ParseNumParam(Token);245 if ParseNumParam(Number) then Memory.WritePos(1, Number) 246 else Error(SExpectedNumber, Token.Pos); 213 247 end else 214 248 if InstructionInfo.Params[I] = ptReg then begin 215 Token := Parser.ReadNext; 216 if (Token.Value <> '') and (Token.Value[1] = 'R') then begin 217 Token.Value := Copy(Token.Value, 2, MaxInt); 218 if TryStrToInt(Token.Value, Number) then 219 Memory.WritePos(1, Number) 220 else Error('Expected numeric register index error', Token.Pos); 221 end else Error('Expected register name starting with R character.', Token.Pos); 249 if ParseReg(RegIndex) then Memory.WritePos(1, Byte(RegIndex)) 250 else Error(SExpectedRegisterName, Token.Pos); 222 251 end else 223 252 if InstructionInfo.Params[I] = ptRegIndirect then begin 224 253 Parser.Expect(tkSpecialSymbol, '('); 225 Token := Parser.ReadNext; 226 if (Token.Value <> '') and (Token.Value[1] = 'R') then begin 227 Token.Value := Copy(Token.Value, 2, MaxInt); 228 if TryStrToInt(Token.Value, Number) then 229 Memory.WritePos(1, Number) 230 else Error('Expected numeric register index error', Token.Pos); 231 end else Error('Expected register name starting with R character.', Token.Pos); 254 if ParseReg(RegIndex) then Memory.WritePos(1, Byte(RegIndex)) 255 else Error(SExpectedRegisterName, Token.Pos); 232 256 Parser.Expect(tkSpecialSymbol, ')'); 233 257 end else 234 258 if InstructionInfo.Params[I] = ptRegIndirectIndex then begin 235 259 Parser.Expect(tkSpecialSymbol, '('); 236 Token := Parser.ReadNext; 237 if (Token.Value <> '') and (Token.Value[1] = 'R') then begin 238 Token.Value := Copy(Token.Value, 2, MaxInt); 239 if TryStrToInt(Token.Value, Number) then begin 240 Memory.WritePos(1, Number); 241 Parser.Expect(tkSpecialSymbol, '+'); 242 Token := Parser.ReadNext; 243 ParseNumParam(Token); 244 end else Error('Expected numeric register index error', Token.Pos); 245 end else Error('Expected register name starting with R character.', Token.Pos); 260 if ParseReg(RegIndex) then begin 261 Memory.WritePos(1, Byte(RegIndex)); 262 Parser.Expect(tkSpecialSymbol, '+'); 263 Token := Parser.ReadNext; 264 if ParseNumParam(Number) then Memory.WritePos(1, Number) 265 else Error(SExpectedNumericIndex, Token.Pos); 266 end else Error(SExpectedRegisterName, Token.Pos); 246 267 Parser.Expect(tkSpecialSymbol, ')'); 247 end else Error( 'Unsupported parameter type', Token.Pos);268 end else Error(SUnsupportedParameterType, Token.Pos); 248 269 end; 249 270 end; … … 300 321 Parser := TParser.Create; 301 322 Parser.OnError := Error; 302 Message := TMessages.Create;323 Messages := TMessages.Create; 303 324 Memory := TMemory.Create; 304 325 InstructionSet := TInstructionSet.Create; … … 315 336 FreeAndNil(InstructionSet); 316 337 FreeAndNil(Memory); 317 FreeAndNil(Message );338 FreeAndNil(Messages); 318 339 FreeAndNil(Parser); 319 340 inherited; -
branches/ByteArray/Cpu.pas
r55 r56 88 88 procedure InstructionDec; 89 89 procedure InstructionDecSize; 90 procedure InstructionXor; 91 procedure InstructionXorSize; 92 procedure InstructionAnd; 93 procedure InstructionAndSize; 94 procedure InstructionOr; 95 procedure InstructionOrSize; 90 96 procedure InitInstructions; 91 97 procedure SetRunning(AValue: Boolean); … … 469 475 RegIndex := ReadRegIndex; 470 476 Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) - 1; 477 end; 478 479 procedure TCpu.InstructionXor; 480 var 481 RegIndex: TRegIndex; 482 RegIndex2: TRegIndex; 483 begin 484 RegIndex := ReadRegIndex; 485 RegIndex2 := ReadRegIndex; 486 Regs[RegIndex] := Regs[RegIndex] xor Regs[RegIndex2]; 487 end; 488 489 procedure TCpu.InstructionXorSize; 490 var 491 DataSize: TBigIntSize; 492 RegIndex: TRegIndex; 493 RegIndex2: TRegIndex; 494 begin 495 DataSize := ReadSize; 496 RegIndex := ReadRegIndex; 497 RegIndex2 := ReadRegIndex; 498 Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) xor Regs[RegIndex2].Copy(DataSize); 499 end; 500 501 procedure TCpu.InstructionAnd; 502 var 503 RegIndex: TRegIndex; 504 RegIndex2: TRegIndex; 505 begin 506 RegIndex := ReadRegIndex; 507 RegIndex2 := ReadRegIndex; 508 Regs[RegIndex] := Regs[RegIndex] and Regs[RegIndex2]; 509 end; 510 511 procedure TCpu.InstructionAndSize; 512 var 513 DataSize: TBigIntSize; 514 RegIndex: TRegIndex; 515 RegIndex2: TRegIndex; 516 begin 517 DataSize := ReadSize; 518 RegIndex := ReadRegIndex; 519 RegIndex2 := ReadRegIndex; 520 Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) and Regs[RegIndex2].Copy(DataSize); 521 end; 522 523 procedure TCpu.InstructionOr; 524 var 525 RegIndex: TRegIndex; 526 RegIndex2: TRegIndex; 527 begin 528 RegIndex := ReadRegIndex; 529 RegIndex2 := ReadRegIndex; 530 Regs[RegIndex] := Regs[RegIndex] or Regs[RegIndex2]; 531 end; 532 533 procedure TCpu.InstructionOrSize; 534 var 535 DataSize: TBigIntSize; 536 RegIndex: TRegIndex; 537 RegIndex2: TRegIndex; 538 begin 539 DataSize := ReadSize; 540 RegIndex := ReadRegIndex; 541 RegIndex2 := ReadRegIndex; 542 Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) or Regs[RegIndex2].Copy(DataSize); 471 543 end; 472 544 … … 507 579 FInstructions[inDec] := InstructionDec; 508 580 FInstructions[inDecSize] := InstructionDecSize; 581 FInstructions[inXor] := InstructionXor; 582 FInstructions[inXorSize] := InstructionXorSize; 583 FInstructions[inAnd] := InstructionAnd; 584 FInstructions[inAndSize] := InstructionAndSize; 585 FInstructions[inOr] := InstructionOr; 586 FInstructions[inOrSize] := InstructionOrSize; 509 587 end; 510 588 -
branches/ByteArray/Devices/Memory.pas
r55 r56 27 27 function GetAddressCount: Integer; override; 28 28 procedure SetChannel(Channel: TChannel); override; 29 procedure SaveToFile(FileName: string); 29 30 procedure Clean; 30 31 property Size: Integer read FSize write SetSize; … … 136 137 end; 137 138 139 procedure TMemory.SaveToFile(FileName: string); 140 var 141 F: TFileStream; 142 begin 143 if FileExists(FileName) then 144 F := TFileStream.Create(FileName, fmOpenWrite) 145 else F := TFileStream.Create(FileName, fmCreate); 146 try 147 if FSize > 0 then F.Write(FData[0], FSize); 148 finally 149 F.Free; 150 end; 151 end; 152 138 153 end. 139 154 -
branches/ByteArray/Forms/FormMain.pas
r50 r56 170 170 Reset; 171 171 172 // LD A, 'A' 172 173 WriteInstruction(inLoadConst); 173 174 WriteRegister(riA); 174 175 Write(DataWidth, Ord('A')); 175 176 177 // LD B, 8 176 178 WriteInstruction(inLoadConst); 177 179 WriteRegister(riB); 178 180 Write(DataWidth, Machine.Serial.BaseAddress); 179 181 182 // OUT (B), A 180 183 WriteInstruction(inOutput); 181 184 WriteRegister(riB); 182 185 WriteRegister(riA); 183 186 187 // LD B, 184 188 WriteInstruction(inLoadConst); 185 189 WriteRegister(riB); -
branches/ByteArray/Forms/FormSourceEditor.lfm
r50 r56 133 133 Caption = 'Run' 134 134 OnExecute = ARunExecute 135 ShortCut = 120 135 136 end 136 137 object ACompile: TAction 137 138 Caption = 'Compile' 138 139 OnExecute = ACompileExecute 140 ShortCut = 16504 139 141 end 140 142 object ARunToCursor: TAction 141 143 Caption = 'Run to cursor' 144 ShortCut = 115 142 145 end 143 146 object AStepIn: TAction 144 147 Caption = 'Step in' 148 ShortCut = 118 145 149 end 146 150 object AStepOut: TAction 147 151 Caption = 'Step out' 152 ShortCut = 8311 148 153 end 149 154 object AStepOver: TAction 150 155 Caption = 'Step over' 156 ShortCut = 119 151 157 end 152 158 object APause: TAction … … 156 162 Caption = 'Stop' 157 163 OnExecute = AStopExecute 164 ShortCut = 16497 158 165 end 159 166 end -
branches/ByteArray/Forms/FormSourceEditor.pas
r50 r56 113 113 with Assembler do begin 114 114 Compile(FormAssembler.SynEdit1.Lines.Text); 115 //Memory.SaveToFile('compiled.bin');115 Memory.SaveToFile('compiled.bin'); 116 116 CompiledProgram.Assign(Memory); 117 117 Machine.Memory.Position := 0; 118 118 Machine.Memory.WriteMemoryPos(CompiledProgram); 119 FormMessages.Messages := Messages; 119 120 end; 120 121 if FormMessages.Visible then -
branches/ByteArray/Instructions.pas
r55 r56 7 7 8 8 type 9 TParamType = (ptNone, ptNumber, ptReg, ptRegIndirect, ptRegIndirectIndex); 9 TParamType = (ptNone, ptNumber, ptReg, ptRegIndirect, ptRegIndirectIndex, 10 ptSize); 10 11 TParamTypeArray = array of TParamType; 11 12 … … 72 73 AddNew(inHalt, 'HALT', [], 'It terminates program execution and halts processor. Processor can be waked up by interrupt.'); 73 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.'); 74 76 AddNew(inLoad, 'LD', [ptReg, ptReg], 'Copies value from one register to another.'); 75 77 AddNew(inInc, 'INC', [ptReg], 'Increments value in specified register.'); -
branches/ByteArray/Languages/ByteArray.cs.po
r55 r56 12 12 "X-Generator: Poedit 3.0.1\n" 13 13 14 #: assembler.scompilationfinished 15 msgid "Compilation finished." 16 msgstr "Překlad dokončen." 17 18 #: assembler.sexpectednumber 19 msgid "Expected number." 20 msgstr "Očekáváno číslo." 21 22 #: assembler.sexpectednumericindex 23 msgid "Expected numeric register index." 24 msgstr "Očekáván číselný index registru." 25 26 #: assembler.sexpectedregistername 27 msgid "Expected register name." 28 msgstr "Očekáváno jméno registru." 29 30 #: assembler.sexpectedvariablename 31 msgid "Expected variable name." 32 msgstr "Očekáváno jméno proměnné." 33 34 #: assembler.sexpectedvariablevalue 35 msgid "Expected variable value." 36 msgstr "Očekávána hodnota proměnné." 37 38 #: assembler.sunsupportedparametertype 39 msgid "Unsupported parameter type" 40 msgstr "Nepodporovaný typ parametru" 41 14 42 #: bigint.soutofrange 15 43 msgctxt "bigint.soutofrange" -
branches/ByteArray/Languages/ByteArray.pot
r55 r56 1 1 msgid "" 2 2 msgstr "Content-Type: text/plain; charset=UTF-8" 3 4 #: assembler.scompilationfinished 5 msgid "Compilation finished." 6 msgstr "" 7 8 #: assembler.sexpectednumber 9 msgid "Expected number." 10 msgstr "" 11 12 #: assembler.sexpectednumericindex 13 msgid "Expected numeric register index." 14 msgstr "" 15 16 #: assembler.sexpectedregistername 17 msgid "Expected register name." 18 msgstr "" 19 20 #: assembler.sexpectedvariablename 21 msgid "Expected variable name." 22 msgstr "" 23 24 #: assembler.sexpectedvariablevalue 25 msgid "Expected variable value." 26 msgstr "" 27 28 #: assembler.sunsupportedparametertype 29 msgid "Unsupported parameter type" 30 msgstr "" 3 31 4 32 #: bigint.soutofrange -
branches/ByteArray/Sample.asm
r46 r56 1 # test 1 NOP 2 3 LD A, 64 4 LD B, 8 5 OUT (B), A 6 LD B, 0 7 LD C, 10 8 OUT (B), A 9 Loop: 10 INC A 11 DEC C 12 JNZ C, Loop 13 HALT
Note:
See TracChangeset
for help on using the changeset viewer.