An SFR access method for 8051 kernel
By remapping SFR data in the xdata region of the 8051 kernel, indirect access to SFRs is achieved, solving the problem that SFR data cannot be accessed under the traditional direct addressing method. This improves the readability and portability of the program and simplifies the library development process.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-27
- Publication Date
- 2026-04-03
AI Technical Summary
Traditional 8051 core microcontrollers can only access data values in RAM areas through direct addressing, and cannot directly access data in SFRs, affecting program readability and portability.
By allocating a portion of the xdata region for remapping SFR data, indirect access to SFRs is achieved. Real-time synchronous updates of data are realized through internal hardware connections, and indirect access is achieved by pointing to the remapped data through pointers.
It improves the SFR access efficiency of the 8051 kernel, enhances program readability and portability, simplifies library development, and improves development efficiency.
Smart Images

Figure CN115203068B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of microcontroller technology, specifically relating to an SFR access method for the 8051 core. Background Technology
[0002] The 8051 core microcontroller is a very common 8-bit microcontroller on the market, favored by developers for its low cost, simple control, and ease of development. With continuous technological advancements and increasingly complex application designs, the peripheral functions of the 8051 are constantly being added and improved, and the number of Special Function Registers (SFRs) is also increasing. This places higher demands on program readability and portability. More and more original equipment manufacturers (OEMs) are now providing their own chip development libraries for customers to use, improving development efficiency. However, the traditional 8051 core's SFRs can only be accessed through direct addressing. In the traditional pointer addressing method, the accessed address in the 8051 chip will not be the corresponding SFR value but rather the data value in the RAM area. Therefore, traditional addressing methods cannot access the data in the SFRs. Summary of the Invention
[0003] To address the aforementioned problems, this invention provides an SFR access method for the 8051 kernel. This method involves re-partitioning a portion of the xdata region to remap the data within the SFR, and indirectly accessing the SFR by reading and writing the SFR value through xdata.
[0004] To achieve the above-mentioned objectives, the technical solution adopted by this invention is: an SFR access method for an 8051 kernel, comprising the following steps:
[0005] S1: Design a remapping function for the SFRs within the 8051 and redivide the xdata region of the 8051. Allocate a portion of the xdata region of the 8051 for SFR remapping. Select the starting address range for remapping in the xdata region based on the size of the remapping data.
[0006] S2: Remap the data in the Special Function Register (SFR) to the pre-defined xdata region. The data remapped to the xdata region is updated synchronously in real time according to the data in the SFR. Modify the data remapped to the xdata region, and the data in the SFR will be updated synchronously in real time accordingly.
[0007] S3: By designing pointers that point to data remapped to the xdata region, indirect access to SFRs is achieved.
[0008] Preferably, in step S1, the main method for dividing a portion of the xdata region of the 8051 is as follows: based on the size of the data during remapping, a segment is selected within the address region from 0 to 0xFFFF, and the selected segment is larger than the data size during remapping.
[0009] Preferably, the SFR data content is remapped in hardware within a 128-byte space allocated in xdata, allowing indirect access to the SFR by reading and writing the SFR value through xdata.
[0010] Preferably, the end address region of xdata is selected as the remapping space of SFR.
[0011] Preferably, in step S2, the specific method of remapping is as follows: the remapping area of the xdata of the 8051 and the SFR are connected through internal hardware to achieve real-time synchronous updates.
[0012] Preferably, the main method of internal hardware connection is as follows: when the CPU address bus of the 8051 accesses the SFR, it first goes through the internal address translation unit to convert the address into the target address before connecting to the SFR; the data bus is directly connected to the SFR, and the CPU controls the address converter to switch the physical address of the accessed SFR through the control bus according to the access address.
[0013] Preferably, specific address information is input to the CPU of the 8051. This specific address information is the address of the region that needs to be remapped. The input specific address information is converted into a target SFR by the address translation unit inside the 8051. The data at the corresponding address in the SFR is accessed, and the accessed data is transmitted to the newly partitioned xdata region in the CPU through the data bus inside the 8051.
[0014] Preferably, the xdata region is repartitioned for remapping the 8051 kernel's SFRs and provided to customers through a library encapsulation.
[0015] The present invention has the following beneficial effects:
[0016] 1) This invention expands the access path of SFR by improving the 8051 structure. A part of the xdata space is allocated for remapping the SFR space. The data remapped to xdata is updated in real time through the internal hardware remapping connection design. That is, the same operation performed in the mapped area and the remapped area will be immediately reflected in the other space. Thus, the indirect access to SFR is achieved by accessing the xdata to read and write the SFR value. It can efficiently and quickly access the chip peripherals, has good readability, and provides many conveniences for the design and development of libraries.
[0017] 2) The modified 8051 structure obtained by the method of this invention can be aligned with 32-bit machines when developing libraries, which increases the readability and portability of the code. The SFR of the 8051 kernel of this invention can be encapsulated into a library for customers to call, thereby improving development efficiency and laying the foundation for more complex 8051 structures in the future. Attached Figure Description
[0018] Figure 1 A schematic diagram for the invention remapping;
[0019] Figure 2 A schematic diagram of the internal hardware connections;
[0020] Figure 3 This is a diagram illustrating remapped address access. Detailed Implementation
[0021] The technical solutions of the embodiments of the present invention / utility model will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention / utility model, and not all embodiments. Unless otherwise specified, the technical means used in the embodiments are conventional means well known to those skilled in the art.
[0022] In the description of this invention / utility model, it should be understood that the terms "longitudinal," "lateral," "upper," "lower," "front," "rear," "left," "right," "vertical," "horizontal," "top," "bottom," "inner," and "outer," etc., indicating the orientation or positional relationship, are based on the orientation or positional relationship shown in the accompanying drawings and are only for the convenience of describing this invention / utility model, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation, and therefore should not be construed as a limitation on this invention / utility model. Figure 1 As shown, an SFR access method for the 8051 kernel specifically includes the following steps:
[0023] S1: Design a remapping function for the SFRs within the 8051 and redivide the xdata region of the 8051. Allocate a portion of the xdata region of the 8051 for SFR remapping. This region is used only for remapping and can remap the data in the SFRs to the xdata region. The selection of the region in xdata for remapping depends on the size of the remapped data, that is, it needs to be determined based on the size of the data in the SFRs. This will select the starting address range for remapping in the xdata region, ensuring that all the data in the SFRs can be remapped to the region allocated in xdata.
[0024] By employing a design remapping method, the SFR is designed to have remapping functionality. The design remapping method can adopt conventional chip design remapping methods. The main method for dividing a portion of the xdata region of the 8051 is as follows: based on the amount of data during remapping, a segment is selected within the address region 0 to 0xFFFF. This selected segment is larger than the amount of data during remapping, that is, the area divided from xdata must be larger than the amount of data in the SFR, thereby ensuring that after remapping, all the data in the SFR can be remapped to the area divided from xdata.
[0025] The xdata space is divided into 128 bytes (for SFRs with multi-BANK structures, a larger xdata space can be designed for remapping) to hardware remap the SFR data content, so that the SFR can be indirectly accessed by accessing xdata to read and write the SFR value.
[0026] The end address region of xdata is selected as the remapping space for the SFR. Considering compatibility, the end address region of xdata (such as 0xff00~0xffff) can be allocated as the remapping space. The principle of address selection is that the selected region needs enough space for remapping. For example, if a 1K space range needs to be remapped, then the range 0xfc00~0xffff is selected as the remapping region. The starting address range of xdata remapping is selected as needed. This address is the basis for subsequent programming operations on the remapped SFR.
[0027] S2: Remap the data in the Special Function Register (SFR) to the pre-defined xdata region. The data remapped to the xdata region is updated synchronously in real time according to the data in the SFR. Modify the data remapped to the xdata region, and the data in the SFR will be updated synchronously in real time accordingly. That is, when the data in the SFR changes, the remapped data in the xdata region also changes accordingly, so that the remapped data in the xdata region is consistent with the data in the SFR. When the data in the xdata remapped region changes, the data in the SFR also changes accordingly, so that the data in the two regions are always consistent, achieving real-time synchronous updates.
[0028] like Figure 3As shown, the specific remapping method is as follows: the remapping area of the xdata of the 8051 is connected to the SFR through internal hardware. The remapping function is embedded in the chip hardware. That is, when remapping is performed, specific address information is input to the CPU of the 8051. This specific address information is the address of the area to be remapped. The input specific address information is converted into a target SFR by the address translation unit inside the 8051. The data at the corresponding address in the SFR is accessed, and the accessed data is transmitted to the newly partitioned xdata area in the CPU through the data bus inside the 8051. For example, if the instruction MOVX FF00 is input to the CPU, the address translation unit will convert the input specific address into address 0080, access the SFR, obtain the SFR value at address 80 in the SFR, and transmit the data to the CPU through the internal data bus, storing it in the newly partitioned xdata area.
[0029] When the address bus accesses an SFR, it first goes through an internal address translation unit to convert the address into a target address before connecting to the SFR. The data bus is directly connected to the SFR. The CPU controls the address converter to switch the physical address of the accessed SFR through the control bus according to the access address.
[0030] This ensures data communication between the two spaces. Data remapped to xdata is synchronized in real-time through an internal hardware remapping connection design; that is, identical operations performed in both the mapped and remapped areas are immediately reflected in the other space. Figure 2 As shown, the main method of internal hardware connection is as follows: when the CPU address bus of the 8051 accesses the SFR, it first goes through the internal address translation unit to convert the address into the target address before connecting to the SFR; the data bus is directly connected to the SFR, and the CPU controls the address converter to switch the physical address of the accessed SFR through the control bus according to the access address.
[0031] In this embodiment, remapping is preferably performed using a chip hardware-based method.
[0032] S3: By designing pointers that point to data remapped to the xdata region, indirect access to SFRs is achieved.
[0033] The 8051 chip can access SFRs indirectly through pointers, as shown in the following instructions:
[0034] #define PORT((PORT_Typexdata*)PORT_BASE)
[0035] typedef struct{
[0036] uint8_t P0;
[0037] uint8_t P1;
[0038] uint8_t P2;
[0039] uint8_t PSET0;
[0040] uint8_t PSET1;
[0041] uint8_t PSET2;
[0042] }PORT_Type
[0043] *((volatile uint8_t xdata*)(&PORT->PSET0+GPIOx))=(1< <currentpin);
[0044] The 8051 architecture using the method of the present invention for indirect addressing design of SFRs can be directly packaged into a library and provided to customers. In other words, the 8051 kernel SFRs that are re-divided in the xdata region for remapping are provided to customers through library packaging, thereby improving the development efficiency of customers.
[0045] The embodiments described above are merely preferred embodiments of the present invention / utility model and are not intended to limit the scope of the present invention / utility model. Any modifications, alterations, alterations, or substitutions made by those skilled in the art to the technical solutions of the present invention / utility model without departing from its design spirit should fall within the protection scope defined by the claims of the present invention / utility model.
Claims
1. A method for accessing SFRs in an 8051 kernel, characterized in that, Includes the following steps: S1: Design a remapping function for the SFRs within the 8051 and redivide the xdata region of the 8051. Allocate a portion of the xdata region of the 8051 for SFR remapping. Select the starting address range for remapping in the xdata region based on the size of the remapping data. S2: Remap the data in the Special Function Register (SFR) to the pre-defined xdata region. The data remapped to the xdata region is updated synchronously in real time according to the data in the SFR. Modify the data remapped to the xdata region, and the data in the SFR will be updated synchronously in real time accordingly. The specific method of remapping is as follows: the remapping area of xdata of the 8051 is connected to the SFR through internal hardware. When the CPU address bus of the 8051 accesses the SFR, it first goes through the internal address translation unit to convert the address into the target address and then connects to the SFR. The data bus is directly connected to the SFR. The CPU controls the address converter to switch the physical address of the accessed SFR through the control bus according to the access address. Alternatively, the specific method of remapping is as follows: input specific address information into the CPU of 8051. The specific address information is the address of the region to be remapped. The input specific address information is converted into a target SFR by the address translation unit inside 8051. The data at the corresponding address in the SFR is accessed. The accessed data is transmitted to the newly partitioned xdata region in the CPU through the data bus inside 8051. S3: By designing pointers that point to data remapped to the xdata region, indirect access to SFRs is achieved.
2. The SFR access method for the 8051 kernel according to claim 1, characterized in that: In step S1, the main method for dividing a portion of the xdata region of the 8051 is as follows: based on the size of the data during remapping, a segment is selected from the address region 0~0xFFFF, and the selected segment is larger than the data size during remapping.
3. The SFR access method for the 8051 kernel according to claim 2, characterized in that: The SFR data content is remapped in hardware by allocating 128 bytes of space in xdata, so that indirect access to the SFR can be achieved by accessing xdata to read and write the SFR value.
4. The SFR access method for the 8051 kernel according to claim 3, characterized in that: The end address region of xdata is selected as the remapping space of SFR.
5. The SFR access method for the 8051 kernel according to claim 1, characterized in that: The xdata region is repartitioned for remapping the 8051 kernel's SFRs, and provided to customers through a library encapsulation.
Citation Information
Patent Citations
System for supporting virtual machine and method for managing access of physical address space in corresponding system
CN114625476A
Addressing space extending method of 16M syllable data storage based on MCS-51 structure
CN1719421A