Everyone knows that we can use C to write a program to control hardware work, but do you know how it works?
Netizen
In the actual operation of the c language, it runs in the form of assembly instructions. The compiler compiles the C language into assembly instructions, and the CPU directly executes the assembly instructions.
So this question becomes, how does the assembly instruction operate the hardware?
If the hardware platform is limited to the x86 environment, there are basically two ways to assemble the instruction hardware:
method one:
By writing data to the memory space. The hardware will map various registers on the hardware (the external line can be understood as the interface to access the hardware or the tool to operate the hardware) to a certain memory address space, and then just use the assembly instructions, even C language to read and write this memory address space (Not really operating physical memory), you can achieve the purpose of operating the hardware.
If the subject also has a Windows XP environment (a virtual machine is also available), you can directly manipulate the video memory with assembly instructions:
MOV AX, B800
MOV ES, AX
XOR DI, DI
MOV CX, 0800
MOV AX, 5555
REPZ STOSB
The various registers of the hardware will be mapped to a certain piece of physical memory. This method is called MMIO. In the device manager of Windows, right click on the device and see the properties - "Resources", many hardware devices have "memory range The parameter, the memory range here means that the hardware resources can be controlled by accessing this segment of memory.
Method 2:
In x86 assembly, there are two special instructions, IN and OUT, which are unique to the x86 platform. The I/O range in the above figure is accessed and controlled by the IN/OUT commands.
The above two ways of accessing the hardware, the first one can be implemented in C language, the above compilation is essentially similar to the C language code:
Char ptr = 0xB8000;
Int i;
For (i = 0; i "0x800; i++)
{ptr + i = 0x55;
}
The second IN/OUT method does not have a direct C language syntax correspondence, and you need to encapsulate the assembly yourself.
So why is it difficult to operate the hardware in C language? This is because most of the code written in normal mode is in protected mode. In protected mode, direct access to physical addresses is restricted. The addresses of C language operations are virtual addresses.
For Windows, to access the physical address, you need to work in kernel mode, that is, the write driver.
In terms of memory, first of all, the subject first understands the concept of physical address and virtual address.
When the original 8086cpu was designed, the address space had an area (640K-1M) and one piece was used as video memory.
The reserved address you mentioned here refers to the physical address. The exact range of this address is 000A0000-000BFFFF. Regardless of whether it is a 32-bit or 64-bit CPU, this physical memory address is always reserved for video memory, and does not distinguish between 32 bits. Still 64-bit, does not distinguish between protected mode or real mode.
It can be seen that this segment of memory is still reserved for the graphics card.
So why not use this memory directly now?
Because the current software is running in protected mode, the addresses accessed are virtual addresses, not physical addresses, including the environment you open using the cmd command, which are virtual addresses, although 32-bit XP can use the debug command to 000B8000. Write data and display it in the cmd interface, but in essence, this is virtual.
What if I want to use this video memory?
Write a simple operating system yourself, do not start the graphics acceleration function of the graphics card, the CPU enters the protection mode and maps a 4G data segment in the GDT, which is consistent with the physical address, then write data to 000B8000, it will be like DOS in the past It is displayed on the screen as well, so this memory can also be accessed in protected mode. So, in protected mode, you can also use it.
How is the graphics card so much memory mapped?
There are a lot of memory addresses mapped to the video memory. This kind of mapping relationship is used to leave some physical addresses to the memory, so that the CPU can access the memory resources as if it were accessing the memory.
Of course, the actual situation is that 2G video memory may not be completely mapped, but only a part of the address is mapped. The video card has some open registers to control which part of the memory is mapped, so that the CPU can use a relatively small physical address range. Access all video memory.
Another interesting thing: in the virtual machine, find the first memory area of ​​the mapped high address part, write a program that can directly access the physical address (such as a driver), read the memory, and then write to In the file, use the screenshot and write it to the file. You will find that the content of the screenshot is basically the same as the content read in the video memory.
Netizen awayisblue
To answer your question, we need to know:
What kind of existence is hardware?
What is the driver.
How to operate hardware in C language
I don't strictly define these concepts. Let me explain them in an informal way.
First talk about hardware:
First introduce a SMC8 microcontroller chip.
This chip has cpu, memory, registers (don't feel the pressure of seeing new terms, continue to look down), etc., equivalent to our computer, but also external hardware.
The concepts you need to know here are:
The chip's pin corresponds to the register. The register is an 8-bit memory unit (pair, which exists on the memory). When you write data to this memory unit, the voltage of the chip's pin changes. Say I wrote 01100001, the voltage status of the 8 pins corresponding to the chip (divided into high level and low level) will output: low high high low low low low high.
The cpu can execute code instructions that can manipulate memory.
Conclusion: So from the above two points we can know that the cpu can execute instructions to change the chip's pin level (voltage).
What we need to know about this display is:
It is pinned and these pins can be connected to the pins of the microcontroller chip described earlier.
The display has its own memory for storing the characters to be displayed, and the display reads characters from the memory for display.
After the MCU chip is connected to the display, the data can be written into the memory of the display through the pin (the different data levels are represented by different pin levels, for example, low, high, low, low, low, high, and representing 01100001, This data is written in the memory of the display and displayed by the display. Of course, the character corresponding to the number is displayed according to ASCII. The character corresponding to 01100001 is 'a'). In addition to the pin for receiving data, there is also a reference for controlling the display. Feet (this we will introduce at the driver, continue to look down).
Conclusion: The MCU chip is connected to the display, and the display of the character can be controlled by the level of the pin output.
Then, the above, that is, the microcontroller chip cpu can control the display of the character of the display by executing instructions.
Here, the hardware mentioned by the subject refers to this display.
Next talk about the driver:
So, what is the driver? The driver is nothing more than the middle layer of hardware and software, but we don't entangle this relationship. Let's take a look at it. For our example, what is the driver? First we have to know:
The display supports a variety of operations, such as clearing the display, moving the cursor, reading data, writing data, and so on.
These are implemented by operating data pins and control pins.
The pin can be controlled by a microcontroller chip.
Conclusion: We can mask the hardware (display hardware) layer by writing the monitor's "driver" program inside the microcontroller chip.
So here the driver refers to the program representation of the operations supported by the display. For example, to clear the display, we can write a clear() function, the cursor moves, we write a move_cursor() function, read the data and write the data as read() and write() respectively, and then implement them separately (by The form of the data written in the register, which in turn controls the level change of the pin, and then controls the display, as described earlier. These functions are the drivers. Why is it that the driver can shield the hardware? Because the programmer can use the previous driver to directly operate the display (hardware) without knowing too much about the hardware, and the general driver can also be provided by the manufacturer.
To put it another way: Generally, these drivers can be written in assembly (for operational efficiency) or in C. For example, the above example can be written directly in C language. Of course, the form of C language inline assembly is also available.
Finally, how to operate the hardware in C language:
I believe that here, the C language is how to operate the hardware has been relatively clear.
Here is a summary:
The C language is run by the CPU (actually, it is compiled into the machine code and then executed in the chip), and the memory can be operated.
One section of the memory corresponds to the register, and the register corresponds to the pin of the chip, so the voltage change of the chip pin can be controlled by operating the segment of the memory.
Hardware (such as a display) has pins (or wires, which are the same thing), and these pins are connected to the pins of the chip to accept chip control.
You can make a series of operation functions for the operation of a certain hardware. These operation functions are the drivers.
So our C language can directly operate the hardware by calling this driver. (Of course, the driver can also be written in C language, so the C language operating hardware does not have to go through the driver).
Netizen Chow Anod
The Arctic has already said it is in place. I add some knowledge points:
1 On the language level, the “hardware†that C can operate directly has only the memory address. Although C supports the register keyword, it cannot specify a specific register, so only the memory address. The way to manipulate the memory address in C is the pointer. E.g:
Char p = . ..;p = . ..;
2 According to 1 reverse push, you can understand that if you want to open to C to operate a certain hardware, the most direct solution is to pre-allocate the use of some fixed addresses when designing the hardware, and then write the legal addresses to these fixed addresses in the actual project. data. This can be similar
Uint32_t p = SCREEN_ADDR; p = RGBA(0xff, 0xff, 0xff, 0xff);
Such code to achieve the operation of the hardware.
3 How do you get this address? What kind of data is legal? To answer these questions, you need to check the spec of the specific device. For example, this clear example can be seen at a glance (I don’t understand it at a glance, please read it repeatedly to fully understand the second point above):
PS: The code for the x86 architecture can't be written this way, for the reason you see the Arctic answer.
Netizen
We use the keyboard of the computer to input the instructions, each instruction corresponds to an ASCII code, and the ASCII code here is the order of the voltage level (or the presence or absence of current, only the voltage level below), that is, we input The voltage is the level of the voltage. The code you see is the level of these voltages. The computer displays the image displayed by the monitor. In fact, the computer does not know what it is. It only knows how to display it.
Conclusion: The code is actually the orderly voltage stored in the memory (memory, hard disk or flash memory, etc.).
Then compile:
Compilation is a conversion process from the high and low of an ordered voltage to another ordered voltage. The following is an example of a 52-chip microcomputer. The compilation is from the orderly voltage level of the ASCII code to 52 microcontrollers. Another specified well-ordered voltage level is the high or low voltage of the HEX file.
Conclusion: The compiled result is still the orderly voltage level stored in the computer.
To the microcontroller to burn:
The next two are burning, understand the above two points is very easy to understand the following content, burning is the order of the computer voltage in the computer through the data line to the ROM in the microcontroller.
The ROM can then release the voltage from it to control the peripheral circuitry.
Summary: From the editing of the code to the final control of the circuit, the voltage is working, but the form that we show to us is different, and the essence is voltage, so there is no conversion.
Understand this sentence: there is no software in the world, software is just a reflection of the hardware, just as consciousness is a reflection of the world is the same!
I believe this is easy to understand.
I am very happy to see someone agree with my point of view. I will add one more point to the topic:
As long as you mention 0/1 and mention software, this problem cannot be understood. .. because the software [including 0/1] and the hardware always have a gap that cannot be crossed;
You said that you write 0 in the MCU, how do you write 0? Kick a 0 on the keyboard? Actually level [I don't care about the numbers we understand], that 0 is just the level of your presentation on the computer monitor. The so-called 0 [substance is level] can be transferred to the ROM in the microcontroller, there is no doubt about the level control level, so the output is low.
UCOAX Custom Made HDMI 2.1 Cable with different connectors and length.
SUPPORTS THE LATEST 8K resolution at 60Hz. 4K supports at 120Hz. Max resolution up to 7680x4320. Supports 144Hz monitors.
HDR HIGH DYNAMIC RANGE for the best details and color depth. Wider range of colors, brighter whites, and deeper blacks.
HDMI 2.1 Cable for XBOX SERIES X, PLAYSTATION 5, 8K/4K BLU-RAY PLAYERS, Apple TV, PS4, XBox One X, XBox One S, Roku Ultra, High End Gaming PC's and other HDMI-enabled devices to 4K & 8K TVs, Monitors & Projectors.
AUDIO RETURN CHANNEL (ARC) and Ethernet. Best cable for gaming with features like Variable Refresh Rate (VRR), Quick Media Switching (QMS), Quick Frame Transport (QFT)
Hdmi 2.1 Cable,8K Hdmi Cable,Hdmi 2.1 Certified Cable,Best Hdmi 2.1 Cable
UCOAX , https://www.jsucoax.com