How does the code control the hardware?

The following is my answer: Let me
talk about the code first:
we use the computer keyboard to input instructions, each instruction corresponds to an ASCII code, and the ASCII code here is the orderly voltage level (or the presence or absence of current, as follows Only mention the level of voltage), that is, what we input is the level of voltage, and the code you see is the image displayed on the control display of these voltage levels. In fact, the computer does not know what it is, but only knows how to display it.
Conclusion: The code is actually the orderly voltage level stored in the memory (memory, hard disk or flash memory, etc.).
Let’s talk about compiling:
compiling is a conversion process from an ordered voltage level to another ordered voltage level. Let’s take the 52 single-chip microcomputer as an example. We compile from the orderly voltage level that represents the ASCII code to Another prescribed orderly voltage level that can be recognized by the 52 single-chip microcomputer means the voltage level of the HEX file.
Conclusion: The compiled result is still the ordered voltage level stored in the computer.
To MCU programming:
The next two are programming. After understanding the above two points, it is easy to understand the following content. The programming is to transmit the orderly voltage level in the computer to the ROM in the MCU through the data line.
Then the ROM can release the voltage in it to control the peripheral circuits.
Summary: From the editing of the code to the final control of the circuit, the voltage is at work, but the form shown to us is different for us, and its essence is voltage, so there is no conversion.

Understand this sentence: there is no software in the world, software is only a reflection of hardware, just like consciousness is a reflection of the world!
I believe this is easy to understand.

I am very happy to see that someone agrees with my point of view. I will add one more point to the topic:
as long as you mention 0/1 and software, this question cannot be understood...because software [including 0/1] and hardware There is always a gap that cannot be crossed;
you said you wrote 0 in the microcontroller, how did you write 0? Hit a 0 on the keyboard? Actually it is the level [it has nothing to do with the number we understand], the 0 is just the presentation form of your level on the computer monitor, the so-called 0 [essentially the level] can be transmitted to the ROM in the microcontroller, the level control circuit There is no doubt about it, so the output is low...
 

Since the poster mentioned "low level", it seems that he has a little understanding of digital circuits.

Then, open the digital circuit related textbooks, the first few pages.

Generally it will tell you that a triode/FET is similar to a relay (a device that generates a magnetic field through a coil and then uses the magnetic field to control whether the physical switch is on or off); input/cut off the voltage signal on one of its pins, and the other The pin will appear high/low level.

PS: A relay is a switch controlled by an electromagnet; when the electromagnet is energized, a magnetic field is generated, and this magnetic field will attract or separate the switch, thereby realizing the function of "controlling the on-off of another circuit with a weak current" .
Among them, the kind of relay that makes the controlled circuit conduction by ordinary contact contact and turns off the switch after energizing the electromagnet that controls it is equivalent to a NOT gate. When the triode is used as a switch, it has almost the same effect as this relay.

 

The above is the basis of digital circuits.

Anything you type in is ultimately stored through a similar thing/mechanism; the so-called "instruction" is actually "a certain command code" (generally called machine code), this "command code" will change a bunch of "command codes" inside the CPU. switch" state to activate different circuits; then data (as mentioned earlier, it is also "memorized" by the conduction or non-conduction of the triode/FET) is sent to this "command" using a similar mechanism. Activated circuits - these circuits are combined by engineers using the most basic triode control principle with a large number of triodes: when data (a combination of high and low levels) passes through these circuits, it will become another Combination of high and low levels: This combination is exactly the same as the result that the function represented by the "instruction" should give.

This passage may be a bit difficult to understand. Then, let's look at the simplest AND gate: there are two data, which enter the AND gate through two different lines; there is only one output, and it must be input with two high levels before it will output a high level; otherwise Just output a low level (this is generally simplified as: only two 1s are input, it outputs 1, otherwise it outputs 0).

— This is the so-called "AND" logic; a set of such "AND" logics directly correspond to "bitwise AND" in computer instructions/high-level languages.
——The bitwise AND instruction means to select a set of lines and connect the data to this set of "AND" logic circuits; then this set of AND logic circuits will output the result of the bitwise AND of the two sets of data.


 

-Similarly, binary addition, 1+1=0 (simultaneous carry); 1+0=1; 0+1=1; 0+0=0: this can be simulated with an XOR circuit (because the XOR circuit The rule is 1+1=0, 1+0=1, 0+1=1, 0+0=0); but in this way (simultaneous carry) this description will be lost, so it is necessary to use an AND gate to simulate the high bit at the same time Carry (As mentioned earlier, the AND gate only outputs 1 if there are only two 1s, and the others output 0; the description of comprehensive XOR: Is this just consistent with the rules of binary addition?)

Then the higher bit becomes the addition of the data on the two input lines, plus the carry data...and so on: This is the idea of ​​using switches for addition.

The addition of numbers with more digits is nothing more than the binary addition of the corresponding bits plus the carry bit of the previous bit, nothing special—a set of switches stacked up like this is called an adder.

——The add command is to select the bunch of switches used for addition made above; and then input data to them (don’t forget, two sets of high and low levels), and these data drive the ones that make up the adder Switches, after a crackling sound (well, if it is an old relay computer: remember the story of BUG?), the circuit is stable in a certain state: at this time, the output of the adder is exactly the input data. And (of course this is the case. As mentioned earlier, we deliberately use XOR gates and AND gates to carefully combine them, so that they just have the same effect as addition).


 

——Other instructions are all the same (more complex/advanced clocks, pipelines, etc... just ignore it for now)

 

You can flip through textbooks. The realization of the adder was mentioned above.

The addition of the adder and some other logic circuits is the so-called ALU (arithmetic logic unit, which suddenly becomes taller). (Of course, it’s actually not that simple. For example, at least a clock signal should be added to coordinate the actions of the switches, and a latch should be added to temporarily store data. As mentioned earlier, input data to the adder , the bunch of switches that make up the adder need to crackle for a while to reach a stable state, and then you can read the answer: the clock signal is used to coordinate these switches to ensure that they all get enough time to reach a stable state)

 

In short, the code inside the computer itself is a set of specific high and low level combinations; and the computer is a well-designed, massive switch group that uses high and low levels to control on-off; when the switch group is input with different When the level is combined, it will cause complex switching actions inside it, and finally produce another set of high and low level combinations as output; these switching actions are carefully designed to make its behavior explainable and predictable——Explanation The rule of /prediction is the instruction set of the CPU.

——In other words, inside the machine, everything is high and low, and there is no conversion problem.
——On the contrary, the input of keyboard/mouse/mic needs to be converted into digital signal through a mechanical process; while the output of video, audio, etc. needs to be converted into digital-to-analog conversion and then through other mechanisms to become human-recognizable information



 

———————————————————————————————————————————————————————
I know that many people are confused, maybe not the principle of the switch; but: If the CPU is just a bunch of switches, why can it "understand" tall and complex instructions like "addition" and "do...while", and even make magical things like office, photoshop and even artificial intelligence? How are these high-level semantics understood by the circuit?


 

Simple instructions such as addition have been introduced earlier; when it comes to more complex things... this has to talk about Turing's contribution.

 

Or start with the smallest pediatric topic. Let's say you've never heard of a multiplication table; so, how do you do 8×9?

We know that A x B means the addition of B A or A B addition. To calculate 8×9, then, we need only add eight nines: seven additions.

 

In other words, there is a good idea here, that is: Many "high-level" mathematical calculations (such as multiplication) can actually be calculated with "low-level" methods (such as addition).

 

Turing's contribution is that he proved that if a machine can accept a series of inputs and complete operations according to the input instructions; then, when the operations supported by this machine meet the requirements of "Turing completeness", it Just simulate any other math/logic operation!

This is so critical. You know, humans have long wanted to use mechanical devices to replace some mental work. For example, the abacus, according to the formula, the answer will come out after a while of mechanical fiddling; there are also various mechanical calculators for foreigners, such as hand-cranked computers, ballistic computers for artillery, and mechanical encryption/decryption machines for the German army, etc. Such attempts can be said to be countless.

But, no matter what, these things can only solve specific problems. Want to be a universal machine that can solve all problems? God, how complicated that must be.

And Turing, at this time, pointed out a feasible path to machine intelligence for mankind...

 

——A machine that can only do addition can simulate a multiplication machine as long as it can find a way to achieve "continuous addition for a specified number of times" (simulating binary multiplication will be easier). A machine capable of simulating any mathematical/logic operation is not much more complicated than an adding machine.

In other words, to create an "omnipotent" computing machine, you don't need to exhaust all possibilities, but as long as you support program input and a few outrageous instructions, you can do it.


 

For example, the CPU, it basically only has three tricks: and, or, and not.

And means that both are true, then the output is true; or as long as one is true, then the output is true; if the input is true, it will output false, and the input is false, then the output will be true-the so-called true and false, generally written as 1, 0, in Inside the computer are high and low levels.

 

Regardless of the fact that the CPU only knows these three tricks; but when they are cleverly combined (constructed into a counter, instruction register, etc., etc., and then combined into a CPU), it meets the requirements of Turing's completeness and produces a qualitative change—for example, The adder mentioned earlier is an example of "how to use this kind of basic logic to simulate the addition of multi-bit binary numbers".


 

More specifically how to do it, this is not clear in a few words. Or take a closer look at your own digital circuit book.

——The study of digital circuits is how to use the three axes of AND or NOT to implement various advanced operations and even complex things such as CPU instruction sets (even directly implement certain algorithms, such as encryption, video encoding, etc.)
—— As for the CPU instruction set, it forms another much more powerful Turing machine (reflected in being able to support more "higher-order" operations than the original and or not): this is the machine code (almost the same as the assembly instruction). One-to-one correspondence)
——Then, high-level languages ​​such as c/c++, java, etc., are another more powerful Turing machine formed by using the CPU instruction set (the compiler/interpreter is responsible for the translation between the two Turing machines) .
——What programmers study is how to use a powerful Turing machine like a programming language to realize complex things like office, photoshop, wow, and even artificial intelligence.

This is a layer-by-layer simulation process.



 

——————————————————————————————
In short, the on-off of the switch is the basis; and how to use such a simple thing for various magical functions If it is combined, it is necessary to understand the principle of "program" (that is, the principle of Turing machine).

If a computer is a person, then software is the knowledge he has mastered. This knowledge enables him not only to count with his fingers (equivalent to the basic functions directly provided by the hardware), but also to gain insight into the mysteries of the universe (equivalent to the endless extended functions "simulated" by software).

 

———————————————————————————————————
To be more specific, how do people drive?

First of all, he needs to know the control principle of the car (knowledge/software); then, based on this knowledge, the brain sends nerve impulses to the muscles of his limbs, driving him to complete various actions such as turning the steering wheel, putting in gear, stepping on the clutch/accelerator, and finally achieving Drive for this purpose.

 

The principle of software controlling hardware is also similar.

As mentioned earlier, the program itself is a combination of high and low levels; it simulates various decision-making processes by executing on the CPU; at the same time, the computer is a bunch of switches; then, write data to certain addresses through instructions (accessing specific addresses is Completed by various addressing mechanisms/instructions, in the final analysis, it can also be said that the circuit topology is changed through switch switching), which is equivalent to turning on/off a certain switch on the corresponding address; this switch can be similar to that inside the CPU. A group of triodes, or a signal line to another relay - this signal causes the relay to close, so the motor turns on...

Just like a person drives a car, the tiny electrical impulses sent by the nerves are amplified by the muscles, affecting the operation of the engine/transmission involving a torrent of energy of hundreds or even thousands of horsepower, and then the car drives away.

The same is true for the computer: by outputting 0/1 (high and low levels) to the switch on the control specific address, it can drive mechanisms such as aircraft model motors, steering gears, etc. through pre-prepared physical facilities, which completes the aircraft model control.

 

The complete control loop can even be:
the sensor on the aircraft model collects data such as flight attitude, terrain, position, etc. (finally converted into a signal composed of high and low levels)----the signal is sent to the CPU through some ports----- The CPU executes the program, the program reads the signal sent by the sensor, and decides the next action ----- after the intelligent judgment of the program, by controlling the switch on the specific address (as mentioned earlier, sending a set of high and low power to this address Only the data composed by the platform can be used), to drive mechanisms such as aircraft model motors, steering gears, etc., to complete the control of the aircraft model.

This is the so-called "robot" (of course, it's just the most simplified robot principle).

people who use Zhihu

Little code farmer, personal blog: http://blog.gavinzh.com

Just finished the computer composition principle

Know a thing or two about microcontrollers

So here I will show you my answer to the main question

First of all, the subject said that writing 0 will output a low level

Then we will use code to show how to display low level

I first write out the meaning of the subject in the C language of the single-chip microcomputer

can be run in keil

#include “reg52.h”
sbit p1.0 = P1^0
void main()
{
    while(1)
    {
         p1.0 = 0;
    }
}

Well, the subject said that in the microcontroller control, writing 0 will output a low level, that's it.

First look at what is a single-chip microcomputer:

 

 

Figure 1

The output low level mentioned by the subject is to output a low level on one of the pins

I want people who can't read the code to be able to see

In the seventh line of code, the variable p1.0 is assigned a value of 0

Then let's take a deep look at what has happened inside the MCU that assigned him a value of 0.

First of all, let me show you what is inside a pin of the microcontroller.

 


Figure 2

Don't look at the people on the left

Let me explain to you on the right, the one on the far right is the pin

Although the pin is one, you can see

There are two devices on the right , and the upper device is used to keep the level of the internal output to the pin from being disturbed by external signals. The device below will temporarily store the signal collected from the outside, where either 0 or 1 is stored. How to judge? If it is greater than a certain voltage, it is 1, and if it is less than a certain voltage, it is 0. The two devices do not interfere with each other .

The code on the seventh line is to output a low level of a pin and maintain it to a low level with the upper holding element.

Then, some people want to ask, why does the MCU recognize it when it is written like this? Still wondering why the language recognized by the microcontroller is the same as the language recognized by the programmer?

Here it involves the principle of computer composition. I will briefly introduce:

First of all, the code I wrote will run in a software that compiles my code to form a boring and difficult assembly language that would be considered high-end in the 1970s, like this (except for the part in green, explain use):

Figure 3

This is not enough, forming such a language will make the low-level compiler in the computer understand, and the low-level compiler will translate the code into something as shown in the figure below

 

 

Figure 4

Note that this is a hexadecimal number, and I won't go into details about how to convert it into binary. Why compile it into the language in Figure 3 and then compile it? To put it bluntly, I feel like a water factory. The water factory puts the water I make into a common large water pipe and then passes it to the homes of different single-chip microcomputers. The single-chip microcomputer leads the water to the kitchen and other places according to the conditions of their own homes. (That's it--)

Then, we can let the single-chip microcomputer or computer execute this code.

Sorry, I just entered into the principle of computer composition now (sorry, Mr. Ji)

Today, the computers that everyone uses are von Neumann computers.

What is a von Neumann computer? The book explains:

Take the method of storing the program to let the controller read the binary from the memory and interpret it, and then let the arithmetic unit calculate the value.

Let me explain again, first let us understand what a calculator is

 

Figure 5

The bottom one is the arithmetic unit , which can perform logic operations of addition, subtraction, multiplication, and division. The controller will read data from the memory and put them in the boxes above the arithmetic unit in the above figure , and each box puts a piece of data.

how to put it?

See the two roads on the left and right? The data will be put into these boxes under the control of the controller . Of course, the controller will control the bottom calculator to perform various calculations and put them back into the upper box.

So how does the data go back?

Nonsense, of course, you can get out of the way, pass the left and right paths, dear

Let's explain the low level output that the poster said at the beginning. Some of the boxes on the top can't put data casually. These boxes are used to lead out the pins, that is, the data in some boxes is connected to the pins.

Speaking of this, I think I have clearly explained how 0 controls the low level.

If there are some places that are not clear, you can share them, and I will explain some of the content I understand in detail.

First look at how the "low level" is formed.

It can be known that the level output by the pin comes from the pair of complementary output stages on the lower right .
So when the PMOS is turned off and the NMOS is turned on, then the I/O port outputs a low level .

 

This " control signal " comes from the output register (output control) of the microcontroller.
So where did the source of this signal come from?

Take the STM32 controller as an example, which is a microprocessor of the ARM series RISC.

We see that part of the I/O is linked on the APB (peripheral bus peripheral bus).
It seems that this signal is coming from this bus.
It is not difficult to know that all data scheduling comes from the core of STM32-Cortex-M3.
We can look at it from the perspective of how the microprocessor executes instructions.

In fact, each program is stored in ROM, which stores the "information" given by our software. The microprocessor extracts the required instructions from the ROM through the bus, and then adjusts the clock at a certain time. Next, the last to execute the command.
The essence of the instruction is actually

That's right, the address is expressed in mem[], and the binary code "16'hd000" on the right is an essential combination of 0 and 1, which can be recognized by the machine, so it is called the machine code.
What I note here is the meaning they represent respectively, that is, assembly language.
Specifically, the order of 01 in different positions of this "machine code" represents different meanings.
Of course, this meaning is a convention, that is, the instruction set!

 

In this way, we know that, in fact, the 01 sequence stored in the memory, we can understand the different meanings through machine recognition and fetch instructions, and then perform different operations.
So how did the 01 sequence of this memory come from?


Generally speaking, in terms of storage principles, each ROM is a small room, and the arrangement and combination of rooms is information, and they are in order. It is "programmed into" through a certain mode or condition. Even if there is no external trigger, the original potential can still be maintained.

 

EEPROM storage principle

The working principle of the EEPROM basic storage unit circuit is shown in the figure below. Similar to EPROM, it regenerates a floating gate on top of the floating gate of the EPROM basic unit circuit. The former is called the first-level floating gate, and the latter is called the second-level floating gate. An electrode can be drawn out to the second-stage floating gate, so that the second-stage floating gate is connected to a certain voltage VG. If VG is a positive voltage, a tunnel effect is generated between the first floating gate and the drain, so that electrons are injected into the first floating gate, that is, programming and writing. If VG is made a negative voltage, the electrons in the first-level floating gate are forced to dissipate, that is, to be erased. Can be rewritten after erasing.

Tunneling effect: Quantum mechanics believes that even if the particle energy is less than the threshold energy, many particles rush to the potential barrier, some particles bounce back, and some particles can pass through, as if there is a tunnel, called "quantum tunneling" .

Then this ordered information is a combination of "0" and "1".

 

How to burn this sequence into the MCU, of course, there are peripheral circuits. Specifically, open the storage path of the MCU through a certain timing, and then burn the information into it.

How to burn it?
JTAG (Joint Test Action Group; Joint Test Working Group) is an international standard test protocol (IEEE 1149.1 compatible), mainly used for chip internal testing. Now most advanced devices support JTAG protocol, such as DSP , FPGA devices and so on. The standard JTAG interface is 4 lines: TMS, TCK, TDI, TDO, which are mode selection, clock, data input and data output lines respectively.



Then those high and low 01 information are output and received in the timing.

 

Next we talk about software.

The software we write is all high-level language, which is far away from the machine, but it is easy to be understood by human logic. We are smart enough to use some translators to complete more complicated work: these translators are compilers, such as KEIL, which translates
C The language is translated into assembly language, and then through the assembler, the assembly language is converted into machine code, and then the machine code is burned into the ROM of the single-chip microcomputer. After the single-chip microcomputer is powered on, it runs the program, reads the instructions, that is, the information, and then executes and controls The register of the IO port, and finally ground the IO port, haha, the low level is completed in this way.


 

 The software we write goes through these steps high-level language --> assembly language ---> machine language. Machine language is binary, and each instruction operation has a corresponding binary code. For example, when we execute ADD R1, R2 instructions, ADD has a unique binary code, which is assumed to be code 1, and R1 and R2 are CPU register addresses. Code 2 and code 3. The specific format and value of these codes are determined according to the instruction format and the specific cpu architecture. For example, the instruction word length of arm is fixed at 32 bits, and specific bits represent condition code opcodes, etc. For the instructions of arm, please refer to "Arm Architecture and Programming" edited by Du Chunlei
Our program is stored in the memory of the CPU in this binary encoding format.
With these unique codes? The CPU starts the decoding operation, and performs some combination operations of digital circuits. Assume that the code 1 is 0x10 (just assuming that the actual number of each instruction set is different), and when the decoding circuit finds that the operation code of the instruction is 0x10, it knows that it is an addition operation. , at this time, a valid signal will be output to strobe the adder; at the same time, code 2 and code 3 will be decoded, and the corresponding register will be gated (which one is the source register and which one is the target register is specified by the instruction set format), Then the data output by the register is sent to the adder through the data line inside the CPU for addition operation, and the result of the operation is sent to the target register. This performs an addition operation.


To directly answer the question of the subject, when you write 0 to the register of the IO pin in the program, the microcontroller will decode the instruction through steps similar to the above, and then write the data of 0 into the IO pin register. How to send the value of the register to the corresponding IO pin? Generally through the D flip-flop (as shown in the figure):

The data port of the internal IO register of the microcontroller is connected to the D pin of the D flip-flop (in fact, there are other circuits to increase the driving capacity, etc.), and the pin with a small triangle under the D pin is the clock signal pin. When the rising edge of the clock signal comes, the data at the D port of the D flip-flop will be output to the Q port, and the Q port is connected to an external pin. So as long as the IO register does not change, the Q pin will remain high or low, that is, writing 0 as shown by your program will make the pin output a low level.

To sum up: After your program is written, the compiler will turn it into a bunch of binary machine codes -----> the MCU decodes these codes, and knows which register you want to perform what kind of operation --- -->The corresponding register is written with the correct value. If it is an IO pin, the value of the register will be output to the external IO pin according to the clock. So in fact, the single-chip microcomputer is just a combination of a bunch of digital circuits, but we artificially stipulate what kind of code to perform what kind of operation.



Author: Dragonfly Yeast
Link: https://www.zhihu.com/question/20492284/answer/43902299
Source: Zhihu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.
 

There are a bunch of gate circuits inside the cpu. The on and off of the gate circuit corresponds to the output being 1 or 0;
how to make it conduct? Use voltage to make it conduct, you can think that this voltage is an energy, the principle of driving with energy is very common; then
you may have to say again, when the voltage is turned on, its output is a specific voltage, it is not 1, this is the connection and difference between digital and analog circuits: the reason why we manufacture digital circuits is to obtain a logic implementation through digital circuits , while analog circuits want to obtain a voltage output, which is analog circuits It's a world of difference from the number of electricity. So what is logic implementation? To put it simply, it is actually mathematical realization. The so-called programming is to turn our needs into mathematical problems, edit them in a programming language, give them to the CPU, let them calculate and drive the terminal, and finally display our logic.
As for the connection between digital and analog, the connection between them is that devices are driven by voltage, then you have to ask again, why can voltage drive semiconductor devices? Well, you have to read electromagnetic field + semiconductor physics, maybe you have to read a little bit of quantum mechanics, and I haven't read it
. A tool that allows us to see the realization of our logic . As for whether this tool is a digital circuit, a quantum circuit, or an optical circuit, as long as you have good low power consumption performance, it doesn’t matter. It’s best to use a vacuum, yes. For human beings, it would be better if the air is smart.

Next, let's talk about the cpu architecture + instruction set.
We often hear that a cpu architecture corresponds to an instruction set, so why is this?
We say that the so-called digital system is actually very simple, you give me input, and I will give you output; what kind of output do you want, then you have to analyze and analyze what kind of input you want to give me so that I can output what you want The output I see is the process of programming; but here comes the question, can I hold any input you give me casually? Obviously it cannot be held, so I will not give this example. . . .
The next conclusion to this paragraph is: the so-called instruction set is actually a set of driver codes for the digital system of the CPU.

Speaking of this, the big framework is almost the same. The remaining important parts are the connection between Boolean algebra and mathematics, the connection between mathematics and actual needs, and then the display part, take your time

There is a direct mapping relationship from the high-level language network to the transistor switch, so the code controls the hardware in this way.
To elaborate, high-level language can be converted into assembly language by a compiler. Assembly language is the instruction of the hardware, which can be directly converted into 0101010101. And these 010101 are the low level and high level in the circuit. These levels control the opening and closing of switches, so various combinations produce complex logic circuits.


Author: Suifeng
Link: https://www.zhihu.com/question/20492284/answer/15279863
Source: Zhihu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

Guess you like

Origin blog.csdn.net/taoanbang/article/details/100169644