CPU is an important part of digital processing systems. In my view, microcontrollers, microprocessors, and DSPs can all be referred to as CPUs, with their emphasis differing. Specifically, traditional microcontrollers are more focused on embedded computing, such as the commonly used 51, AVR, and ARM chips, which not only contain computational and control functions but also cover external resources like timers, serial ports, parallel ports, USB, and I2C buses. The CPU generally exists as a core of the DSP, which usually includes another core dedicated to digital signal processing. On the other hand, the microprocessor, which we often refer to as the processor on a PC, has a more singular focus, concentrating on computational and control function processing. Therefore, in terms of performance in this area, microcontrollers and DSPs cannot compare to it. With the help of the southbridge and northbridge chips, the PC’s microprocessor can focus on its primary tasks, significantly improving efficiency.
For friends, the most encountered CPU in life is actually the x86 CPU. Of course, if any friend likes toys like Apple, they might know something about ARM. The rest are specialized CPUs used in specific fields, such as the PowerPC chips commonly used in the communications industry, the Sun SPARC chips used in high-performance servers, and the MIPS chips used in scientific computing. Therefore, regardless of the chip encountered, it is the same for application layer developers, just that there are some small details that need attention. For example:
(1) Data alignment methods
(2) Byte order issues
(3) Function parameter stacking issues
(4) CPU out-of-order execution issues
(5) Cache and memory consistency issues in the CPU
Of course, if we are only considering simple application layer design, considering these contents is already quite challenging. However, we are considering how to design embedded operating systems, so we also need to look at what is generally included under a CPU. As long as one is proficient in the design and implementation of a CPU, knowledge of other CPUs will also follow suit.
Any CPU, regardless of its completed functions, usually has the following basic designs:
1. Registers
-
Stack register
-
PC register
-
Status register
-
Arithmetic register
Registers are the basic resources within the CPU. No matter when the CPU code is executed, these resources are shared. Therefore, when an interrupt, function call, or thread switch occurs, these registers need to be protected. A common basic measure is to save them to a temporary stack. The stack register records where the current stack is being used, the PC register records where the current code is running, and where the next instruction is located. The status register saves the current execution state of the CPU, including whether there has been an overflow, whether interrupts are enabled or disabled, and whether there is a division by zero exception, etc. As for the arithmetic register, it varies by CPU; some CPUs have fewer registers, such as the x86 registers, while others have more, such as PowerPC. The uses of arithmetic registers are many, including data access, computation, shifting, returning computation results, etc.
2. Instruction Set
-
Addressing instructions
-
Mathematical operation instructions
-
Logical operation instructions
-
Software interrupt instructions
-
Jump instructions
-
Remote call instructions
-
IO access instructions
-
Stack operation instructions
The instruction set directly determines the type of a specific CPU to some extent. For example, although there are differences between the CPUs produced by Intel and AMD, they both use the x86 instruction set. Similarly, CPUs produced by Marvell, Samsung, and Qualcomm are different, but they all use the ARM instruction set. Therefore, if software runs on Marvell, it can generally also run on Samsung. The instruction set is complex and contains a lot of content. However, usually, these contents are the types of instructions that a CPU must complete. Of course, the most important are interrupt and stack handling instructions.
3. Interrupt and Exception Handling Mechanism
No matter what CPU, the interrupt part is indispensable. Imagine if a CPU only knows to keep running, its execution efficiency would actually be very low. A CPU with interrupts not only allows the CPU to interact with more external I/O devices but also greatly improves its own running efficiency. Different CPUs handle interrupts in similar ways. Typically, in the entire address space of the CPU, there is usually an interrupt vector table in the low address area, where each entry records the handling function corresponding to each interrupt.
Therefore, whenever an interrupt occurs, the CPU will first push the next PC address onto the stack, then jump to the corresponding interrupt address in the interrupt vector table to execute the corresponding handling function. This process is completed automatically by the CPU, and we do not need to worry about it. For us, it is actually no different from a function call within the CPU. After the interrupt handling is completed, we use the ret instruction to return to the previously pushed IP address to continue with the following code. The entire process is as if the interrupt never occurred.
Therefore, understanding a CPU mainly involves understanding registers, instruction sets, and interrupts. With an in-depth understanding of interrupts and stacks, there is actually nothing difficult. Here, we can all consider a question: how to simulate interrupts under Windows or Linux to complete our operating system development? Everyone can think about it first; we will continue to introduce it in subsequent blogs. In this entire article, we did not introduce the related content of coding. In fact, as long as the basic concepts here are clarified, the rest is just some procedural work. In software development, design is actually the most difficult part; the rest is development and debugging.
1.Detailed Analysis of the Growth Experience of Embedded Linux Engineers
2.19 Methods and Techniques for Converting 5V to 3.3V in Microcontrollers!
3.What is the Difference Between Embedded WiFi and Regular WiFi?
4.Key Knowledge Points Every Linux Developer Should Know~
5.After Studying the Bitcoin Code, I Discovered an Amazing Secret!
6.Things Developers Should Avoid: Five Programming Languages Not Worth Learning in 2018
Disclaimer: This article is a network reprint, and the copyright belongs to the original author. If there are copyright issues regarding the work, please contact us, and we will confirm the copyright based on the copyright certificate you provide and pay remuneration or delete the content.