| 1 | unit CpuBitWidth;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, AddressableArea;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | TInstruction = (inNop, inHalt,
|
|---|
| 10 | inLoad1, inLoad2, inLoad4, inLoad8,
|
|---|
| 11 | inLoadMem1, inLoadMem2, inLoadMem4, inLoadMem8,
|
|---|
| 12 | inStoreMem1, inStoreMem2, inStoreMem4, inStoreMem8,
|
|---|
| 13 | inInput1, inInput2, inInput4, inInput8,
|
|---|
| 14 | inOutput1, inOutput2, inOutput4, inOutput8);
|
|---|
| 15 |
|
|---|
| 16 | { TCpuBitWidth1 }
|
|---|
| 17 |
|
|---|
| 18 | TCpuBitWidth1 = class
|
|---|
| 19 | Memory: TAddressableArea1;
|
|---|
| 20 | IO: TAddressableArea1;
|
|---|
| 21 | Halted: Boolean;
|
|---|
| 22 | PC: Byte;
|
|---|
| 23 | A: Byte;
|
|---|
| 24 | function Read1: Byte;
|
|---|
| 25 | procedure Write1(Data: Byte);
|
|---|
| 26 | procedure Reset;
|
|---|
| 27 | procedure Run;
|
|---|
| 28 | procedure Step;
|
|---|
| 29 | end;
|
|---|
| 30 |
|
|---|
| 31 | { TCpuBitWidth2 }
|
|---|
| 32 |
|
|---|
| 33 | TCpuBitWidth2 = class
|
|---|
| 34 | Memory: TAddressableArea2;
|
|---|
| 35 | IO: TAddressableArea2;
|
|---|
| 36 | Halted: Boolean;
|
|---|
| 37 | PC: Word;
|
|---|
| 38 | A: Word;
|
|---|
| 39 | function Read1: Byte;
|
|---|
| 40 | function Read2: Word;
|
|---|
| 41 | procedure Write1(Data: Byte);
|
|---|
| 42 | procedure Write2(Data: Word);
|
|---|
| 43 | procedure Reset;
|
|---|
| 44 | procedure Run;
|
|---|
| 45 | procedure Step;
|
|---|
| 46 | end;
|
|---|
| 47 |
|
|---|
| 48 | { TCpuBitWidth4 }
|
|---|
| 49 |
|
|---|
| 50 | TCpuBitWidth4 = class
|
|---|
| 51 | Memory: TAddressableArea4;
|
|---|
| 52 | IO: TAddressableArea4;
|
|---|
| 53 | Halted: Boolean;
|
|---|
| 54 | PC: Cardinal;
|
|---|
| 55 | A: Cardinal;
|
|---|
| 56 | function Read1: Byte;
|
|---|
| 57 | function Read2: Word;
|
|---|
| 58 | function Read4: Cardinal;
|
|---|
| 59 | procedure Reset;
|
|---|
| 60 | procedure Run;
|
|---|
| 61 | procedure Step;
|
|---|
| 62 | end;
|
|---|
| 63 |
|
|---|
| 64 | { TCpuBitWidth8 }
|
|---|
| 65 |
|
|---|
| 66 | TCpuBitWidth8 = class
|
|---|
| 67 | Memory: TAddressableArea8;
|
|---|
| 68 | IO: TAddressableArea8;
|
|---|
| 69 | Halted: Boolean;
|
|---|
| 70 | PC: DWord;
|
|---|
| 71 | A: DWord;
|
|---|
| 72 | function Read1: Byte;
|
|---|
| 73 | function Read2: Word;
|
|---|
| 74 | function Read4: Cardinal;
|
|---|
| 75 | function Read8: DWord;
|
|---|
| 76 | procedure Reset;
|
|---|
| 77 | procedure Run;
|
|---|
| 78 | procedure Step;
|
|---|
| 79 | end;
|
|---|
| 80 |
|
|---|
| 81 | implementation
|
|---|
| 82 |
|
|---|
| 83 | { TCpuBitWidth4 }
|
|---|
| 84 |
|
|---|
| 85 | function TCpuBitWidth4.Read1: Byte;
|
|---|
| 86 | begin
|
|---|
| 87 | Result := Memory.Read1(PC);
|
|---|
| 88 | Inc(PC);
|
|---|
| 89 | end;
|
|---|
| 90 |
|
|---|
| 91 | function TCpuBitWidth4.Read2: Word;
|
|---|
| 92 | begin
|
|---|
| 93 | Result := Memory.Read2(PC);
|
|---|
| 94 | Inc(PC, 2);
|
|---|
| 95 | end;
|
|---|
| 96 |
|
|---|
| 97 | function TCpuBitWidth4.Read4: Cardinal;
|
|---|
| 98 | begin
|
|---|
| 99 | Result := Memory.Read4(PC);
|
|---|
| 100 | Inc(PC, 4);
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 | procedure TCpuBitWidth4.Reset;
|
|---|
| 104 | begin
|
|---|
| 105 | PC := 0;
|
|---|
| 106 | A := 0;
|
|---|
| 107 | end;
|
|---|
| 108 |
|
|---|
| 109 | procedure TCpuBitWidth4.Run;
|
|---|
| 110 | begin
|
|---|
| 111 | Reset;
|
|---|
| 112 | while not Halted do
|
|---|
| 113 | Step;
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | procedure TCpuBitWidth4.Step;
|
|---|
| 117 | var
|
|---|
| 118 | Opcode: TInstruction;
|
|---|
| 119 | begin
|
|---|
| 120 | Opcode := TInstruction(Read1);
|
|---|
| 121 | case Opcode of
|
|---|
| 122 | inNop: ;
|
|---|
| 123 | inHalt: Halted := True;
|
|---|
| 124 | inLoad1: A := Read1;
|
|---|
| 125 | inLoad2: A := Read2;
|
|---|
| 126 | inLoad4: A := Read4;
|
|---|
| 127 | inLoadMem1: A := Memory.Read1(Read4);
|
|---|
| 128 | inLoadMem2: A := Memory.Read2(Read4);
|
|---|
| 129 | inLoadMem4: A := Memory.Read4(Read4);
|
|---|
| 130 | inStoreMem1: Memory.Write1(Read4, A);
|
|---|
| 131 | inStoreMem2: Memory.Write2(Read4, A);
|
|---|
| 132 | inStoreMem4: Memory.Write4(Read4, A);
|
|---|
| 133 | inInput1: A := IO.Read1(Read4);
|
|---|
| 134 | inInput2: A := IO.Read2(Read4);
|
|---|
| 135 | inInput4: A := IO.Read4(Read4);
|
|---|
| 136 | inOutput1: IO.Write1(Read4, A);
|
|---|
| 137 | inOutput2: IO.Write2(Read4, A);
|
|---|
| 138 | inOutput4: IO.Write4(Read4, A);
|
|---|
| 139 | end;
|
|---|
| 140 | end;
|
|---|
| 141 |
|
|---|
| 142 | { TCpuBitWidth2 }
|
|---|
| 143 |
|
|---|
| 144 | function TCpuBitWidth2.Read1: Byte;
|
|---|
| 145 | begin
|
|---|
| 146 | Result := Memory.Read1(PC);
|
|---|
| 147 | Inc(PC);
|
|---|
| 148 | end;
|
|---|
| 149 |
|
|---|
| 150 | function TCpuBitWidth2.Read2: Word;
|
|---|
| 151 | begin
|
|---|
| 152 | Result := Memory.Read2(PC);
|
|---|
| 153 | Inc(PC, 2);
|
|---|
| 154 | end;
|
|---|
| 155 |
|
|---|
| 156 | procedure TCpuBitWidth2.Write1(Data: Byte);
|
|---|
| 157 | begin
|
|---|
| 158 | Memory.Write1(PC, Data);
|
|---|
| 159 | Inc(PC);
|
|---|
| 160 | end;
|
|---|
| 161 |
|
|---|
| 162 | procedure TCpuBitWidth2.Write2(Data: Word);
|
|---|
| 163 | begin
|
|---|
| 164 | Memory.Write2(PC, Data);
|
|---|
| 165 | Inc(PC, 2);
|
|---|
| 166 | end;
|
|---|
| 167 |
|
|---|
| 168 | procedure TCpuBitWidth2.Reset;
|
|---|
| 169 | begin
|
|---|
| 170 | PC := 0;
|
|---|
| 171 | A := 0;
|
|---|
| 172 | end;
|
|---|
| 173 |
|
|---|
| 174 | procedure TCpuBitWidth2.Run;
|
|---|
| 175 | begin
|
|---|
| 176 | Reset;
|
|---|
| 177 | while not Halted do
|
|---|
| 178 | Step;
|
|---|
| 179 | end;
|
|---|
| 180 |
|
|---|
| 181 | procedure TCpuBitWidth2.Step;
|
|---|
| 182 | var
|
|---|
| 183 | Opcode: TInstruction;
|
|---|
| 184 | begin
|
|---|
| 185 | Opcode := TInstruction(Read1);
|
|---|
| 186 | case Opcode of
|
|---|
| 187 | inNop: ;
|
|---|
| 188 | inHalt: Halted := True;
|
|---|
| 189 | inLoad1: A := Read1;
|
|---|
| 190 | inLoad2: A := Read2;
|
|---|
| 191 | inLoadMem1: A := Memory.Read1(Read2);
|
|---|
| 192 | inLoadMem2: A := Memory.Read2(Read2);
|
|---|
| 193 | inStoreMem1: Memory.Write1(Read2, A);
|
|---|
| 194 | inStoreMem2: Memory.Write2(Read2, A);
|
|---|
| 195 | inInput1: A := IO.Read1(Read2);
|
|---|
| 196 | inInput2: A := IO.Read2(Read2);
|
|---|
| 197 | inOutput1: IO.Write1(Read2, A);
|
|---|
| 198 | inOutput2: IO.Write2(Read2, A);
|
|---|
| 199 | end;
|
|---|
| 200 | end;
|
|---|
| 201 |
|
|---|
| 202 | { TCpuBitWidth8 }
|
|---|
| 203 |
|
|---|
| 204 | function TCpuBitWidth8.Read1: Byte;
|
|---|
| 205 | begin
|
|---|
| 206 | Result := Memory.Read1(PC);
|
|---|
| 207 | Inc(PC);
|
|---|
| 208 | end;
|
|---|
| 209 |
|
|---|
| 210 | function TCpuBitWidth8.Read2: Word;
|
|---|
| 211 | begin
|
|---|
| 212 | Result := Memory.Read2(PC);
|
|---|
| 213 | Inc(PC, 2);
|
|---|
| 214 | end;
|
|---|
| 215 |
|
|---|
| 216 | function TCpuBitWidth8.Read4: Cardinal;
|
|---|
| 217 | begin
|
|---|
| 218 | Result := Memory.Read4(PC);
|
|---|
| 219 | Inc(PC, 4);
|
|---|
| 220 | end;
|
|---|
| 221 |
|
|---|
| 222 | function TCpuBitWidth8.Read8: DWord;
|
|---|
| 223 | begin
|
|---|
| 224 | Result := Memory.Read8(PC);
|
|---|
| 225 | Inc(PC, 8);
|
|---|
| 226 | end;
|
|---|
| 227 |
|
|---|
| 228 | procedure TCpuBitWidth8.Reset;
|
|---|
| 229 | begin
|
|---|
| 230 | PC := 0;
|
|---|
| 231 | A := 0;
|
|---|
| 232 | end;
|
|---|
| 233 |
|
|---|
| 234 | procedure TCpuBitWidth8.Run;
|
|---|
| 235 | begin
|
|---|
| 236 | Reset;
|
|---|
| 237 | while not Halted do
|
|---|
| 238 | Step;
|
|---|
| 239 | end;
|
|---|
| 240 |
|
|---|
| 241 | procedure TCpuBitWidth8.Step;
|
|---|
| 242 | var
|
|---|
| 243 | Opcode: TInstruction;
|
|---|
| 244 | begin
|
|---|
| 245 | Opcode := TInstruction(Read1);
|
|---|
| 246 | case Opcode of
|
|---|
| 247 | inNop: ;
|
|---|
| 248 | inHalt: Halted := True;
|
|---|
| 249 | inLoad1: A := Read1;
|
|---|
| 250 | inLoad2: A := Read2;
|
|---|
| 251 | inLoad4: A := Read4;
|
|---|
| 252 | inLoad8: A := Read8;
|
|---|
| 253 | inLoadMem1: A := Memory.Read1(Read8);
|
|---|
| 254 | inLoadMem2: A := Memory.Read2(Read8);
|
|---|
| 255 | inLoadMem4: A := Memory.Read4(Read8);
|
|---|
| 256 | inLoadMem8: A := Memory.Read8(Read8);
|
|---|
| 257 | inStoreMem1: Memory.Write1(Read8, A);
|
|---|
| 258 | inStoreMem2: Memory.Write2(Read8, A);
|
|---|
| 259 | inStoreMem4: Memory.Write4(Read8, A);
|
|---|
| 260 | inStoreMem8: Memory.Write8(Read8, A);
|
|---|
| 261 | inInput1: A := IO.Read1(Read8);
|
|---|
| 262 | inInput2: A := IO.Read2(Read8);
|
|---|
| 263 | inInput4: A := IO.Read4(Read8);
|
|---|
| 264 | inInput8: A := IO.Read8(Read8);
|
|---|
| 265 | inOutput1: IO.Write1(Read8, A);
|
|---|
| 266 | inOutput2: IO.Write2(Read8, A);
|
|---|
| 267 | inOutput4: IO.Write4(Read8, A);
|
|---|
| 268 | inOutput8: IO.Write8(Read8, A);
|
|---|
| 269 | end;
|
|---|
| 270 | end;
|
|---|
| 271 |
|
|---|
| 272 | { TCpuBitWidth1 }
|
|---|
| 273 |
|
|---|
| 274 | function TCpuBitWidth1.Read1: Byte;
|
|---|
| 275 | begin
|
|---|
| 276 | Result := Memory.Read1(PC);
|
|---|
| 277 | Inc(PC);
|
|---|
| 278 | end;
|
|---|
| 279 |
|
|---|
| 280 | procedure TCpuBitWidth1.Write1(Data: Byte);
|
|---|
| 281 | begin
|
|---|
| 282 | Memory.Write1(PC, Data);
|
|---|
| 283 | Inc(PC);
|
|---|
| 284 | end;
|
|---|
| 285 |
|
|---|
| 286 | procedure TCpuBitWidth1.Reset;
|
|---|
| 287 | begin
|
|---|
| 288 | PC := 0;
|
|---|
| 289 | A := 0;
|
|---|
| 290 | end;
|
|---|
| 291 |
|
|---|
| 292 | procedure TCpuBitWidth1.Run;
|
|---|
| 293 | begin
|
|---|
| 294 | Reset;
|
|---|
| 295 | while not Halted do
|
|---|
| 296 | Step;
|
|---|
| 297 | end;
|
|---|
| 298 |
|
|---|
| 299 | procedure TCpuBitWidth1.Step;
|
|---|
| 300 | var
|
|---|
| 301 | Opcode: TInstruction;
|
|---|
| 302 | begin
|
|---|
| 303 | Opcode := TInstruction(Read1);
|
|---|
| 304 | case Opcode of
|
|---|
| 305 | inNop: ;
|
|---|
| 306 | inHalt: Halted := True;
|
|---|
| 307 | inLoad1: A := Read1;
|
|---|
| 308 | inLoadMem1: A := Memory.Read1(Read1);
|
|---|
| 309 | inStoreMem1: Memory.Write1(Read1, A);
|
|---|
| 310 | inInput1: A := IO.Read1(Read1);
|
|---|
| 311 | inOutput1: IO.Write1(Read1, A);
|
|---|
| 312 | end;
|
|---|
| 313 | end;
|
|---|
| 314 |
|
|---|
| 315 | end.
|
|---|
| 316 |
|
|---|