1 | unit UInstructionReader;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UCpu;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TInstructionReader }
|
---|
13 |
|
---|
14 | TInstructionReader = class
|
---|
15 | protected
|
---|
16 | public
|
---|
17 | Cpu: TCpu;
|
---|
18 | IP: Integer;
|
---|
19 | Prefix: Boolean;
|
---|
20 | DataSize: TBitWidth;
|
---|
21 | DataSizeBase: TBitWidth;
|
---|
22 | AddrSize: TBitWidth;
|
---|
23 | AddrSizeBase: TBitWidth;
|
---|
24 | procedure Init;
|
---|
25 | function Read8: Byte; inline;
|
---|
26 | function Read16: Word; inline;
|
---|
27 | function Read32: DWord; inline;
|
---|
28 | function Read64: QWord; inline;
|
---|
29 | function ReadAddress: TAddress; inline;
|
---|
30 | function ReadAddressSigned: TAddressSigned; inline;
|
---|
31 | function ReadData: QWord;
|
---|
32 | end;
|
---|
33 |
|
---|
34 | implementation
|
---|
35 |
|
---|
36 | { TInstructionReader }
|
---|
37 |
|
---|
38 | procedure TInstructionReader.Init;
|
---|
39 | begin
|
---|
40 | DataSizeBase := Cpu.DataSizeBase;
|
---|
41 | DataSize := DataSizeBase;
|
---|
42 | AddrSizeBase := Cpu.AddrSizeBase;
|
---|
43 | AddrSize := AddrSizeBase;
|
---|
44 | IP := 0;
|
---|
45 | Prefix := False;
|
---|
46 | end;
|
---|
47 |
|
---|
48 | function TInstructionReader.Read8: Byte;
|
---|
49 | begin
|
---|
50 | Result := PByte(Cpu.Memory + IP)^;
|
---|
51 | Inc(IP);
|
---|
52 | end;
|
---|
53 |
|
---|
54 | function TInstructionReader.Read16: Word;
|
---|
55 | begin
|
---|
56 | Result := PWord(Cpu.Memory + IP)^;
|
---|
57 | Inc(IP, SizeOf(Word));
|
---|
58 | end;
|
---|
59 |
|
---|
60 | function TInstructionReader.Read32: DWord;
|
---|
61 | begin
|
---|
62 | Result := PDWord(Cpu.Memory + IP)^;
|
---|
63 | Inc(IP, SizeOf(DWord));
|
---|
64 | end;
|
---|
65 |
|
---|
66 | function TInstructionReader.Read64: QWord;
|
---|
67 | begin
|
---|
68 | Result := PQWord(Cpu.Memory + IP)^;
|
---|
69 | Inc(IP, SizeOf(QWord));
|
---|
70 | end;
|
---|
71 |
|
---|
72 | function TInstructionReader.ReadAddress: TAddress;
|
---|
73 | begin
|
---|
74 | case AddrSize of
|
---|
75 | bw8: Result := Read8;
|
---|
76 | bw16: Result := Read16;
|
---|
77 | bw32: Result := Read32;
|
---|
78 | bw64: Result := Read64;
|
---|
79 | end;
|
---|
80 | end;
|
---|
81 |
|
---|
82 | function TInstructionReader.ReadAddressSigned: TAddressSigned;
|
---|
83 | begin
|
---|
84 | case AddrSize of
|
---|
85 | bw8: Result := ShortInt(Read8);
|
---|
86 | bw16: Result := SmallInt(Read16);
|
---|
87 | bw32: Result := Integer(Read32);
|
---|
88 | bw64: Result := Int64(Read64);
|
---|
89 | end;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | function TInstructionReader.ReadData: QWord;
|
---|
93 | begin
|
---|
94 | case DataSize of
|
---|
95 | bw8: Result := Read8;
|
---|
96 | bw16: Result := Read16;
|
---|
97 | bw32: Result := Read32;
|
---|
98 | bw64: Result := Read64;
|
---|
99 | end;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | end.
|
---|
103 |
|
---|