|
1 | 1 | # Week 3: Basic CPU Components |
2 | 2 |
|
3 | | -<<<<<<< HEAD |
4 | 3 | ### 1. ALU Class |
5 | 4 | ```cpp |
6 | 5 | class ALU { |
@@ -221,78 +220,3 @@ int main() { |
221 | 220 | - Displays the converted machine code. |
222 | 221 | - Displays the initial state of the registers. |
223 | 222 | - Loads the machine code into the |
224 | | -======= |
225 | | -## Objective |
226 | | -The goal of this week is to implement the core components of a CPU. This includes building the Arithmetic Logic Unit (ALU), implementing general-purpose registers, and creating the program counter and instruction register. |
227 | | - |
228 | | -## Program Counter and Instruction Register |
229 | | - |
230 | | -### Overview |
231 | | -The **Program Counter (PC)** and **Instruction Register (IR)** are critical components in the design of a CPU. They work together to ensure that instructions are fetched, decoded, and executed in a sequential manner. |
232 | | - |
233 | | -### Program Counter (PC) |
234 | | -The PC holds the memory address of the next instruction to be executed. It increments automatically after each instruction fetch, ensuring the CPU processes instructions sequentially unless a jump or branch instruction modifies the sequence. |
235 | | - |
236 | | -### Instruction Register (IR) |
237 | | -The IR holds the currently executing instruction fetched from memory. It temporarily stores the instruction so that the CPU can decode and execute it. |
238 | | - |
239 | | -## Program Structure |
240 | | - |
241 | | -[Source Code](/Week%203/Register.cpp) |
242 | | - |
243 | | -## Explanation |
244 | | - |
245 | | -### Program Counter (PC) |
246 | | -- **Initialization**: The PC is initialized to `0` in the CPU class constructor. |
247 | | -- **Fetching**: In the `fetchInstruction` method, the PC points to the memory address of the next instruction. |
248 | | -- **Incrementing**: After fetching an instruction, the PC is incremented to point to the next instruction. |
249 | | - |
250 | | -### Instruction Register (IR) |
251 | | -- **Holding Instructions**: The IR holds the instruction fetched from memory. |
252 | | -- **Fetching**: In the `fetchInstruction` method, the IR is set to the value of the memory at the address pointed to by the PC. |
253 | | -- **Decoding and Executing**: The fetched instruction can then be decoded and executed by the CPU. |
254 | | - |
255 | | -### Simulated CPU Execution |
256 | | -- **Memory Representation**: A vector `memory` is used to represent the instruction memory. |
257 | | -- **Fetching and Storing**: The CPU fetches instructions from this memory, stores them in the IR, and increments the PC. |
258 | | -- **Execution**: The ALU performs operations based on the fetched instructions, demonstrating how arithmetic and logical operations would be executed. |
259 | | - |
260 | | -## Arithmetic Logic Unit (ALU) |
261 | | - |
262 | | -The **Arithmetic Logic Unit (ALU)** is a critical component of the CPU that performs arithmetic and logic operations. It's the execution core of the CPU, where all the actual computation happens. |
263 | | - |
264 | | -### Key Functions of the ALU |
265 | | - |
266 | | -#### Arithmetic Operations |
267 | | -- **Addition**: Adds two numbers. |
268 | | -- **Subtraction**: Subtracts one number from another. |
269 | | -- **Multiplication**: Multiplies two numbers (though sometimes handled by a separate multiplier unit). |
270 | | -- **Division**: Divides one number by another (though often handled by a separate division unit). |
271 | | - |
272 | | -#### Logical Operations |
273 | | -- **AND**: Performs a bitwise AND operation. |
274 | | -- **OR**: Performs a bitwise OR operation. |
275 | | -- **NOT**: Performs a bitwise NOT operation, inverting each bit. |
276 | | -- **XOR**: Performs a bitwise XOR operation. |
277 | | - |
278 | | -#### Comparison Operations |
279 | | -- **Equality**: Checks if two numbers are equal. |
280 | | -- **Inequality**: Checks if two numbers are not equal. |
281 | | -- **Greater Than**: Checks if one number is greater than another. |
282 | | -- **Less Than**: Checks if one number is less than another. |
283 | | - |
284 | | -#### Shift Operations |
285 | | -- **Left Shift**: Shifts bits to the left, filling with zeros. |
286 | | -- **Right Shift**: Shifts bits to the right, with the option of filling with zeros (logical shift) or preserving the sign bit (arithmetic shift). |
287 | | - |
288 | | -### ALU Structure |
289 | | -An ALU is typically composed of: |
290 | | -- **Input Registers**: These hold the operands (the numbers to be operated on). |
291 | | -- **Operation Decoder**: This decodes the operation to be performed (e.g., add, subtract). |
292 | | -- **Arithmetic Circuitry**: Performs arithmetic operations. |
293 | | -- **Logic Circuitry**: Performs logic operations. |
294 | | -- **Output Register**: Holds the result of the operation. |
295 | | - |
296 | | -## Conclusion |
297 | | -The Program Counter (PC) and Instruction Register (IR) are essential for the sequential execution of instructions in a CPU, while the ALU is responsible for performing the core computations. Together, these components form the backbone of CPU operations, enabling the execution of programs by processing instructions methodically and efficiently. This project lays the foundation for understanding how modern processors handle instruction cycles and perform computations, providing a solid base for further exploration into CPU architecture and design. 🚀 |
298 | | ->>>>>>> c70d70303a5d72c4a39d48491cf8e55b7bd81225 |
0 commit comments