Changeset 177 for branches/virtualcpu4/UInstructionWriter.pas
- Timestamp:
- Apr 12, 2019, 2:03:51 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/virtualcpu4/UInstructionWriter.pas
r174 r177 13 13 14 14 TInstructionWriter = class 15 private 16 DataSizeLast: TBitWidth; 17 DataSizePrefix: TBitWidth; 18 AddrSizeLast: TBitWidth; 19 AddrSizePrefix: TBitWidth; 20 procedure PrefixBegin; 21 procedure PrefixEnd; 22 public 15 23 Cpu: TCpu; 16 24 IP: Integer; 25 DataSize: TBitWidth; 26 AddrSize: TBitWidth; 27 procedure Init; 17 28 procedure Write8(Value: Byte); 18 29 procedure Write16(Value: Word); … … 20 31 procedure Write64(Value: QWord); 21 32 procedure WriteAddress(Value: TAddress); 33 procedure WriteAddressSigned(Value: TAddressSigned); 22 34 procedure WriteData(Value: QWord); 23 35 procedure WriteString(Text: string); … … 27 39 procedure Increment(Reg: TRegIndex); 28 40 procedure Decrement(Reg: TRegIndex); 29 procedure Jump(Value: QWord); 30 procedure JumpNotZero(Value: QWord); 41 procedure Jump(Addr: QWord); 42 procedure JumpNotZero(Addr: QWord); 43 procedure JumpZero(Addr: QWord); 44 procedure JumpRel(Addr: QWord); 45 procedure JumpRelNotZero(Addr: QWord); 46 procedure JumpRelZero(Addr: QWord); 31 47 procedure DataPrefix8; 32 48 procedure DataPrefix16; 33 49 procedure DataPrefix32; 34 50 procedure DataPrefix64; 51 procedure AddrPrefix8; 52 procedure AddrPrefix16; 53 procedure AddrPrefix32; 54 procedure AddrPrefix64; 35 55 procedure StoreMem(RegAddr, RegSrc: TRegIndex); 36 56 procedure LoadMem(RegDst, RegAddr: TRegIndex); … … 40 60 end; 41 61 62 { TAssembler } 63 64 TAssembler = class 65 Source: TStringList; 66 Dest: array of Byte; 67 procedure Compile; 68 end; 69 70 TDisassembler = class 71 72 end; 73 74 const 75 OpcodeName: array[TOpcode] of string = ('NOP', 'HALT', 'LD', 'LDI', 'JP', 'JPZ', 76 'JPNZ', 'JR', 'JRZ', 'JRNZ', 'NEG', 'CLR', 'LDM', 'STM', 'EX', 'PUSH', 'POP', 77 'CALL', 'RET', 'ADD', 'ADDI', 'SUB', 'SUBI', 'INC', 'DEC', 'IN', 'OUT', 78 'SHL', 'SHR', 'DP8', 'DP16', 'DP32', 'DP64', 'DS', 'AS', 'TEST', 'AND', 'OR', 79 'XOR', 'LDDR', 'LDIR', 'MUL', 'DIV', 'MOD', 'AP8', 'AP16', 'AP32', 'AP64'); 42 80 43 81 implementation 44 82 83 { TAssembler } 84 85 procedure TAssembler.Compile; 86 begin 87 88 end; 89 45 90 { TInstructionWriter } 46 91 47 92 procedure TInstructionWriter.Nop; 48 93 begin 94 PrefixBegin; 49 95 Write8(Byte(opNop)); 96 PrefixEnd; 50 97 end; 51 98 52 99 procedure TInstructionWriter.Halt; 53 100 begin 101 PrefixBegin; 54 102 Write8(Byte(opHalt)); 103 PrefixEnd; 55 104 end; 56 105 57 106 procedure TInstructionWriter.Loadi(Reg: TRegIndex; Value: QWord); 58 107 begin 108 PrefixBegin; 59 109 Write8(Byte(opLoadi)); 60 110 Write8(Reg); 61 111 WriteData(Value); 112 PrefixEnd; 62 113 end; 63 114 64 115 procedure TInstructionWriter.Increment(Reg: TRegIndex); 65 116 begin 117 PrefixBegin; 66 118 Write8(Byte(opInc)); 67 119 Write8(Reg); 120 PrefixEnd; 68 121 end; 69 122 70 123 procedure TInstructionWriter.Decrement(Reg: TRegIndex); 71 124 begin 125 PrefixBegin; 72 126 Write8(Byte(opDec)); 73 127 Write8(Reg); 74 end; 75 76 procedure TInstructionWriter.Jump(Value: QWord); 77 begin 128 PrefixEnd; 129 end; 130 131 procedure TInstructionWriter.Jump(Addr: QWord); 132 begin 133 PrefixBegin; 78 134 Write8(Byte(opJump)); 79 WriteAddress(Value); 80 end; 81 82 procedure TInstructionWriter.JumpNotZero(Value: QWord); 83 begin 135 WriteAddress(Addr); 136 PrefixEnd; 137 end; 138 139 procedure TInstructionWriter.JumpNotZero(Addr: QWord); 140 begin 141 PrefixBegin; 84 142 Write8(Byte(opJumpNotZero)); 85 WriteAddress(Value); 143 WriteAddress(Addr); 144 PrefixEnd; 145 end; 146 147 procedure TInstructionWriter.JumpZero(Addr: QWord); 148 begin 149 PrefixBegin; 150 Write8(Byte(opJumpZero)); 151 WriteAddress(Addr); 152 PrefixEnd; 153 end; 154 155 procedure TInstructionWriter.JumpRel(Addr: QWord); 156 var 157 NextIP: QWord; 158 begin 159 PrefixBegin; 160 NextIP := IP + 1 + BitWidthBytes[Cpu.AddrSize]; 161 Write8(Byte(opJumpRel)); 162 WriteAddressSigned(Int64(Addr) - Int64(NextIP)); 163 PrefixEnd; 164 end; 165 166 procedure TInstructionWriter.JumpRelNotZero(Addr: QWord); 167 var 168 NextIP: QWord; 169 begin 170 PrefixBegin; 171 NextIP := IP + 1 + BitWidthBytes[Cpu.AddrSize]; 172 Write8(Byte(opJumpRelNotZero)); 173 WriteAddressSigned(Int64(Addr) - Int64(NextIP)); 174 PrefixEnd; 175 end; 176 177 procedure TInstructionWriter.JumpRelZero(Addr: QWord); 178 var 179 NextIP: QWord; 180 begin 181 PrefixBegin; 182 NextIP := IP + 1 + BitWidthBytes[Cpu.AddrSize]; 183 Write8(Byte(opJumpRelZero)); 184 WriteAddressSigned(Int64(Addr) - Int64(NextIP)); 185 if AddrSizeLast <> bwNone then AddrSize := AddrSizeLast; 186 PrefixEnd; 86 187 end; 87 188 88 189 procedure TInstructionWriter.DataPrefix8; 89 190 begin 191 DataSizePrefix := bw8; 90 192 Write8(Byte(opDataPrefix8)); 91 193 end; … … 93 195 procedure TInstructionWriter.DataPrefix16; 94 196 begin 197 DataSizePrefix := bw16; 95 198 Write8(Byte(opDataPrefix16)); 96 199 end; … … 98 201 procedure TInstructionWriter.DataPrefix32; 99 202 begin 203 DataSizePrefix := bw32; 100 204 Write8(Byte(opDataPrefix32)); 101 205 end; … … 103 207 procedure TInstructionWriter.DataPrefix64; 104 208 begin 209 DataSizePrefix := bw64; 105 210 Write8(Byte(opDataPrefix64)); 211 end; 212 213 procedure TInstructionWriter.AddrPrefix8; 214 begin 215 AddrSizePrefix := bw8; 216 Write8(Byte(opAddrPrefix8)); 217 end; 218 219 procedure TInstructionWriter.AddrPrefix16; 220 begin 221 AddrSizePrefix := bw16; 222 Write8(Byte(opAddrPrefix16)); 223 end; 224 225 procedure TInstructionWriter.AddrPrefix32; 226 begin 227 AddrSizePrefix := bw32; 228 Write8(Byte(opAddrPrefix32)); 229 end; 230 231 procedure TInstructionWriter.AddrPrefix64; 232 begin 233 AddrSizePrefix := bw64; 234 Write8(Byte(opAddrPrefix64)); 106 235 end; 107 236 … … 115 244 procedure TInstructionWriter.LoadMem(RegDst, RegAddr: TRegIndex); 116 245 begin 246 PrefixBegin; 117 247 Write8(Byte(opLoadMem)); 118 248 Write8(RegDst); 119 249 Write8(RegAddr); 250 PrefixEnd; 120 251 end; 121 252 122 253 procedure TInstructionWriter.Output(Port: TAddress; Reg: TRegIndex); 123 254 begin 255 PrefixBegin; 124 256 Write8(Byte(opOut)); 125 257 WriteAddress(Port); 126 258 Write8(Reg); 259 PrefixEnd; 127 260 end; 128 261 129 262 procedure TInstructionWriter.Input(Reg: TRegIndex; Port: TAddress); 130 263 begin 264 PrefixBegin; 131 265 Write8(Byte(opIn)); 132 266 Write8(Reg); 133 267 WriteAddress(Port); 268 PrefixEnd; 134 269 end; 135 270 136 271 procedure TInstructionWriter.Test(Reg: TRegIndex); 137 272 begin 273 PrefixBegin; 138 274 Write8(Byte(opTest)); 139 275 Write8(Reg); 276 Prefixend; 140 277 end; 141 278 … … 148 285 end; 149 286 287 procedure TInstructionWriter.PrefixBegin; 288 begin 289 if DataSizePrefix <> bwNone then begin 290 DataSizeLast := DataSize; 291 DataSize := DataSizePrefix; 292 DataSizePrefix := bwNone; 293 end; 294 if AddrSizePrefix <> bwNone then begin 295 AddrSizeLast := AddrSize; 296 AddrSize := AddrSizePrefix; 297 AddrSizePrefix := bwNone; 298 end; 299 end; 300 301 procedure TInstructionWriter.PrefixEnd; 302 begin 303 if DataSizeLast <> bwNone then begin 304 DataSize := DataSizeLast; 305 DataSizeLast := bwNone; 306 end; 307 if AddrSizeLast <> bwNone then begin 308 AddrSize := AddrSizeLast; 309 AddrSizeLast := bwNone; 310 end; 311 end; 312 313 procedure TInstructionWriter.Init; 314 begin 315 DataSize := Cpu.DataSize; 316 AddrSize := Cpu.AddrSize; 317 end; 318 150 319 procedure TInstructionWriter.Write8(Value: Byte); 151 320 begin … … 174 343 procedure TInstructionWriter.WriteAddress(Value: TAddress); 175 344 begin 176 case Cpu.AddressSize of345 case AddrSize of 177 346 bw8: Write8(Value); 178 347 bw16: Write16(Value); … … 182 351 end; 183 352 353 procedure TInstructionWriter.WriteAddressSigned(Value: TAddressSigned); 354 begin 355 case AddrSize of 356 bw8: Write8(Byte(Value)); 357 bw16: Write16(Word(Value)); 358 bw32: Write32(DWord(Value)); 359 bw64: Write64(QWord(Value)); 360 end; 361 end; 362 184 363 procedure TInstructionWriter.WriteData(Value: QWord); 185 364 begin 186 case Cpu.DataSize of365 case DataSize of 187 366 bw8: Write8(Value); 188 367 bw16: Write16(Value);
Note:
See TracChangeset
for help on using the changeset viewer.