Introduction
This series has not been updated for a long time. I am sorry to remember that the previous blog post was in February. I haven't had enough time in my spare time recently, so updating a new blog has become a luxury. I remember that when I first started writing, LZ wrote three blog posts a day at most. Now, let's think about the past as a dream.
Well, it's hard to write it once, so I won't talk nonsense. This article mainly introduces the hardware and the content of the HCl language.
Start with question
First of all, before introducing the content of this article, let's first consider a seemingly simple but actually "advanced" question. As we all know, computers are dealing with 0 and 1 in the final sense. How do computers remember 0 and 1?
How is this question a bit like why 1 + 1 in the field of mathematics equals 2? Do you instantly feel like you are very tall?
Please be patient. In fact, the answer to this question is much simpler than 1 + 1 = 2. The answer is that the computer records 0 and 1 by voltage. When learning physics, everyone must have heard of the overvoltage. Its unit is generally volt (V ). For example, the common voltage is 220 V. 1 V indicates 1 in the computer, and 0 V indicates 0.
After answering this question, we can continue our design.
Design Basics
To talk about design, we need to know the basis of design. Just now, we have learned the most basic basics: how to express 0 and 1, but this is just the most fundamental foundation. We still need some basic elements. Just as if you want to draw an object, the first thing is a point, and then you can click it to form a line, and then wire it to form a surface, and the final surface will become an adult.
The values 0 and 1 are just vertices. Next, we need to convert the points into lines. This is called a logic gate.
Logical gates are the basic computing elements of digital circuits. They can also be seen as the ing between physical structures and logical structures. They are actually composed of transistors. The logic gate accepts the input of the signal and generates a certain amount of output based on the signal. The output is a Boolean function of the input, that is, the output can only be 0 or 1. It is the standard symbol of and door ('and' DOOR), or door ('or' DOOR), and not door ('non-' door, you can see that they represent the composition of a group of lines.
And: Or: not:
We can see that and or are both two inputs, and not is one input. We mentioned the HCl hardware design language. The above three figures are represented by HCl, A & B, A | B, and! A.
For example, for the and gate, it accepts two inputs. If both inputs are high voltage, the output is high voltage, that is, the output is 1. Otherwise, all are output low voltage, that is, the output is 0. These are based on the fact that 1 V is 1 and 0 V is 0.
Advanced Design
Now that we have mastered the basics, we can have some tricks on it. For example, the logic gates mentioned above can only accept one-bit input and one-bit output. For example, we can calculate 0 & 1 as 0. What should we do if we want to calculate more complex expressions, such as 10001 & 11111 =? (Note that bitwise AND operators are not logical operations ).
The answer is a combination circuit. The principle is very simple. A logic gate can calculate one bit. If we get 32 logic gates, can we compute 32 bits? As with the 10001 & 11111 operation above, we can use five logic gates for calculation. Of course, this is not the case. We will introduce it in detail later.
However, the circuit is often complex and cannot be connected randomly. Otherwise, who is responsible for the death of electricity? Therefore, the circuit combination must follow the following two principles.
1. The outputs of two logic gates cannot be connected together. Otherwise, they may cause a signal conflict on the line. Therefore, an invalid voltage or fault may occur. For example, if one 0 V and one 1 V are connected together, will it generate a 0.5 V, or is it similar to connecting the positive and negative poles, and it will be directly short-circuited.
2. The combined circuit must be non-circular. That is to say, the output cannot be used as the input. Otherwise, this function may be ambiguous. This principle is very simple, for example, in mathematics, if C = a + B. Here we can regard C as output, and A and B as input. If a is equal to C, is B equal to 0? Of course not, because a is actually a + B at this time, and C in the second round will be equal to a + 2B. Similarly, C in the third round will be equal to a + 3B. So C = a + B, c = a + 2B, c = a + 3B? (This is a bit difficult. Please taste it carefully)
Following the above principles, we can assemble the circuit at will. For example, the circuit in the book.
It indicates whether the output is equal to a and B. If you describe the circuit with HCl, It is (A & B) | (! A &&! B ). The expression is a = B in the programming language.
The following figure shows an example in another book, which is translated as a multiplex. In fact, this is because the control bit s can be reused. For example.
This is not particularly obvious in this example, but there are more obvious examples to illustrate the meaning of "reuse. If the above circuit is expressed in HCl, It is (A & S) | (B &&! S ). This circuit means that if S is 1, the result is a; otherwise, the result is B.
In the book, the expressions of HCl and C are distinguished by the following three points.
1. logic gate is output continuously, but it is evaluated only when the C language expression is executed. The difference is that the logic gate can be regarded as a circuit. The circuit cannot be powered off, and the current will always exist.
2. The input in C can be any integer, while the HCl can only be 1 and 0. This is easy to understand.
3. For the symbol A & B, the C language stipulates that if the former is false, the latter will not be calculated. But there is no such statement in HCl.
Calculate by bit
As mentioned above, even the advanced design still operates on a single digit. So how can we really operate on multiple locations, such as commonly used 32-bit or 64-bit.
The principle is simple, that is, multiple one-bit operations are performed together, as shown in the figure below.
In this figure, the judgment of equal bits is a combination circuit composed of two not doors, two and doors, and one or gate. They are done in parallel, and finally a and door is used to determine whether two 32-bit numbers are equal. That is, expression A = B. It is worth noting that both A and B are 32 bits.
There is also an expression similar to the switch statement in C language, which is in the following form.
It indicates that if S is 1, A is output; otherwise, B is output. Similarly, both A and B are 32 bits. How do I express the circuit? It is actually the 32-bit version of the last multiplexing circuit. For example.
We can see that the not value of S is reused. Otherwise, 32 Not doors are required. It is worth mentioning that the conditions in the Condition Selection expression in HCl do not need to be mutually exclusive, but are selected in order of priority, which is different from the switch in C language.
The last HCl expression is in the form of a set. It indicates that an input signal needs to be matched with some values. For example, S1 and S0.
If S1 is code = 2 | code = 3, S0 is code = 1 | code = 0. Therefore, HCl can be expressed in another way, namely, code in {2, 3} and code in {1, 0 }. It can be seen that the HCl expression is actually a hardware expression.
Memory and clock
All of the above are combined circuits. Their function is to generate a value based on input. However, as we have said just now, the combined circuit is continuously output, so it cannot remain unchanged. However, our computer needs to store data, so we need storage devices that can save the status. A storage device is controlled by a clock. The clock is like a switch, which controls when the storage device updates the value of the device.
There are two common storage devices.
1. clock register: stores a single bit or word. Clock signals to control whether registers need to load input values.
2. Access the memory immediately: store multiple words. Use the address to select the word to read and write.
A typical application of clock registers is PC, condition code register, and program status. They all have clear input, which means that their values are actually a function of a certain number of values. For example, the input of the condition code register is mainly the value of the logical computing unit, therefore, the value of the condition code register can be considered as a function of a logical computing unit.
The clock register updates the status based on the clock signal, and the clock signal is like a table. For example, the value of the Register is updated every 12 o'clock. As shown in the following figure, when the clock changes, the value changes to y.
The most typical example of instant access to memory is our register file (eight program registers) and instant access to memory (also known as memory ). They do not have clear input values, so there is no function relationship. Both register files and memory have read and write operations. For clock registers, there is no need to read or write them, because it only changes the output value according to the input changes, it can be directly connected to the circuit.
Register files generally have two read ports and one write port. Each port carries an address to identify which register to operate. For the write port, there is an input data, and for the read port, there is an output data. The specific illustration is as follows.
We can see that there is a clock (clock) Controlling write operations at the write port of the register file. When the clock changes, the input data value is updated to the corresponding register. For read data, it is similar to a combination circuit. According to the input address value (SRC), the register file will output the corresponding data.
The access to memory is very similar to the register file. The difference is that there is only one address input, not three, but only one data output, rather than two. The specific illustration is as follows.
When address is input and data in is input, if the write input is 1 and the clock (clock) changes, the value of the address in the memory is the value of the input address. The value is the value of the input data. Similarly, if the address is input and the write input is 0, the output data (data out) is the value of the input address. In addition, when reading data, if the address is out of the range, the error signal is output as 1. Recall the HCl language. The error is actually the output of a combined circuit, which is equal to (address in> MAX address) | (address in <0 ), that is, when the input address value is not within the valid range, its output is 1.
Summary
Recall the content of this chapter, which is actually made up of the hardware in combination with the HCl language. These hardware are required by the y86 processor. Such as a combination of circuits and memory. At the end of this chapter, we are very close to our programming field. We can see the emergence of registers and memory. However, it should be noted that the Register File here is not equal to the register, and the access to the memory is not equal to the memory. LZ just wants you to understand it more vividly.
In the next chapter, y86 processor is implemented in sequence with everyone. This is a very low-level, but most basic processor. If you enter the world slowly, you will think this is a very interesting thing. As far as LZ is concerned, the biggest feeling of reading this book is that each chapter is boring at the beginning, but every time I read a few sections, there is a sense of openness. I believe you will also like it very much.
Finally, LZ explained that this series of blog posts are written by LZ based on their own understanding. Therefore, although the core content is similar to the original content, the language description is quite different, if you don't get used to it, you may have to look at the original work.