Startup code:啟動代碼。在Keil中,啟動代碼在復位目標系統后立即被執行。啟動代碼主要實現以下功能:
(1) 清除內部數據存儲器
(2) 清除外部數據存儲器
(3) 清除外部頁存儲器
(4) 初始化small模式下的可重入棧和指針
(5) 初始化large模式下的可重入棧和指針
(6) 初始化compact模式下的可重入棧和指針
(7) 初始化8051硬件棧指針
(8) 傳遞初始化全局變量的控制命令或者在沒有初始化全局變量時給main函數傳遞命令。
在每一個啟動文件中,提供了可供用戶自己修改有來控制程序執行的匯編常量。見表1
表1
Name
|
Description
|
IDATALEN
|
Specifies the number of bytes of idata to clear to 0. The default is 80h because most 8051 derivatives contain at least 128 bytes of internal data memory. Use a value of 100h for the 8052 and other derivatives that have 256 bytes of internal data memory.
|
XDATASTART
|
Specifies the initial xdata address to clear to 0.
|
XDATALEN
|
Indicates the number of bytes of xdata to clear to 0. The default is 0.
|
PDATASTART
|
Specifies the initial pdata address to clear to 0.
|
PDATALEN
|
Specifies the number of bytes of pdata to clear to 0. The default is 0.
|
IBPSTACK
|
Specifies whether or not the small model reentrant stack pointer (?C_IBP) should be initialized. A value of 1 causes this pointer to be initialized. A value of 0 prevents initialization of this pointer. The default is 0.
|
IBPSTACKTOP
|
Specifies the top of the small model reentrant stack. The default is 0xFF in idata memory.
The Cx51 Compiler does not check to see if the stack area available satisfies the requirements of the application. It is your responsibility to perform such a test. |
XBPSTACK
|
Specifies whether or not the large model reentrant stack pointer (?C_XBP) should be initialized. A value of 1 causes this pointer to be initialized. A value of 0 prevents initialization of this pointer. The default is 0.
|
XBPSTACKTOP
|
Specifies the top of the large model reentrant stack. The default is 0xFFFF in xdata memory.
The Cx51 Compiler does not check to see if the stack area available satisfies the requirements of the application. It is your responsibility to perform such a test. |
PBPSTACK
|
Specifies whether the compact model reentrant stack pointer (?C_PBP) should be initialized. A value of 1 causes this pointer to be initialized. A value of 0 prevents initialization of this pointer. The default is 0.
|
PBPSTACKTOP
|
Specifies the top of the compact model reentrant stack. The default is 0xFF in pdata memory.
The Cx51 Compiler does not check to see if the stack area available satisfies the requirements of the application. It is your responsibility to perform such a test. |
PPAGEENABLE
|
Enables (a value of 1) or disables (a value of 0) Port 2 initialization for pdata memory access. The default is 0. pdata addressing uses Port 2 for the upper address (or page) byte.
|
PPAGE
|
Specifies the value to write to Port 2 of the 8051 for pdata memory access. This value represents the xdata memory page to use for pdata. This is the upper 8 bits of the absolute address range to use for pdata. For example, if the pdata area begins at address 1000h (page 10h) in xdata memory,
BL51 input modules PDATA (1050H) Neither the BL51 Linker nor the Cx51 Compiler checks to see if the
|
上面這些只是標號,如果愿意,自己可以換成其他的名字。這樣寫意義更直觀。
$NOMOD51
;不使用預先定義的SFR,The NOMOD51 directive suppresses pre-definition of 8051 SFR
;names. This directive must be used when a customer-specific SFR definition file is included.
;------------------------------------------------------------------------------
; This file is part of the C51 Compiler package
; Copyright (c) 1988-2005 Keil Elektronik GmbH and Keil Software, Inc.
; Version 8.01
;
; *** <<< Use Configuration Wizard in Context Menu >>> ***
;------------------------------------------------------------------------------
; STARTUP.A51: This code is executed after processor reset.
;代碼在處理器復位后執行
; To translate this file use A51 with the following invocation:
;通過A51用下面的命令傳遞該文件
; A51 STARTUP.A51
;
; To link the modified STARTUP.OBJ file to your application use the following
; Lx51 invocation:
;利用下面Lx51命令來鏈接修改的STARTUP.OBJ文件到實際的應用
; Lx51 your object file list, STARTUP.OBJ controls
;
;------------------------------------------------------------------------------
;
; User-defined Power-On Initialization of Memory
;用戶自定義上電初始化內存
; With the following EQU statements the initialization of memory
; at processor reset can be defined:
;在處理器復位時通過EQU來初始化內存
; IDATALEN: IDATA memory size <0x0-0x100>
;IDATALEN:IDATA存儲區的大小<0-256>,可以根據自己的選擇修改
; Note: The absolute start-address of IDATA memory is always 0
; The IDATA space overlaps physically the DATA and BIT areas.
;IDATA絕對的起始地址總是0
;IDATA空間涵蓋物理上的DATA和BIT區
;需用0來初始化idata區的字節數
IDATALEN EQU 80H
;
; XDATASTART: XDATA memory start address <0x0-0xFFFF>
; The absolute start address of XDATA memory
;XDATA存儲區的起始地址,XDATA內存的絕對起始地址。
XDATASTART EQU 0
;指定初始的XDATA地址清0
; XDATALEN: XDATA memory size <0x0-0xFFFF>
; The length of XDATA memory in bytes.
;XDATA空間的長度,以字節為單位
XDATALEN EQU 0
;說明xdata的字節數清0,該值默認為0
; PDATASTART: PDATA memory start address <0x0-0xFFFF>
; The absolute start address of PDATA memory
PDATASTART EQU 0H
;
; PDATALEN: PDATA memory size <0x0-0xFF>
; The length of PDATA memory in bytes.
PDATALEN EQU 0H
;
;
;------------------------------------------------------------------------------
;
; Reentrant Stack Initialization
;重入棧的初始化
; The following EQU statements define the stack pointer for reentrant
; functions and initialized it:
;EQU語句定義了重入函數的棧指針并初始化
; Stack Space for reentrant functions in the SMALL model.
;SMALL模式下的重入函數棧
;
IBPSTACK: Enable SMALL model reentrant stack
; IBPSTACK = 1使能模擬棧
; Stack space for reentrant functions in the SMALL model.
IBPSTACK EQU 0 ; set to 1 if small reentrant is used.
; IBPSTACKTOP: End address of SMALL model stack <0x0-0xFF>
; IBPSTACKTOP:棧的結束地址<0x0-0xFF>
; Set the top of the stack to the highest location.
;設置為最高地址(向下生長的)
;默認FFH+1(其實就是0,使用時進行操作(+0xFF)使其變為0xFF)
IBPSTACKTOP EQU 0xFF +1 ; default 0FFH+1
;
;下面是LARGE模式下的模擬棧,和SMALL相同不做特別說明
; Stack Space for reentrant functions in the LARGE model.
;
XBPSTACK: Enable LARGE model reentrant stack
; Stack space for reentrant functions in the LARGE model.
XBPSTACK EQU 0 ; set to 1 if large reentrant is used.
; XBPSTACKTOP: End address of LARGE model stack <0x0-0xFFFF>
; Set the top of the stack to the highest location.
XBPSTACKTOP EQU 0xFFFF +1 ; default 0FFFFH+1
;
;COMPACT模式下
; Stack Space for reentrant functions in the COMPACT model.
;
PBPSTACK: Enable COMPACT model reentrant stack
; Stack space for reentrant functions in the COMPACT model.
PBPSTACK EQU 0 ; set to 1 if compact reentrant is used.
;
; PBPSTACKTOP: End address of COMPACT model stack <0x0-0xFFFF>
; Set the top of the stack to the highest location.
PBPSTACKTOP EQU 0xFF +1 ; default 0FFH+1
;
;
;------------------------------------------------------------------------------
;COMPACT模式下64K xdata的頁存儲器
; Memory Page for Using the Compact Model with 64 KByte xdata RAM
; Compact Model Page Definition
;通過PDATA變量定義XDATA頁
; Define the XDATA page used for PDATA variables.
; PPAGE must conform with the PPAGE set in the linker invocation.
;PPAGE必須與鏈接器引用設置一致
; Enable pdata memory page initalization
PPAGEENABLE EQU 0 ; set to 1 if pdata object are used.
;設置1使能
; PPAGE number <0x0-0xFF>
; uppermost 256-byte address of the page used for PDATA variables.
;頁號,PDATA變量高256字節地址(高8位地址)
;
; SFR address which supplies uppermost address byte <0x0-0xFF>
; most 8051 variants use P2 as uppermost address byte
;提供高字節地址的SFR的地址(高8位地址的存放位置),一般的8051單片機用P2口
PPAGE_SFR DATA 0A0H
;
;
;------------------------------------------------------------------------------
; Standard SFR Symbols
;使用偽指令DATA為寄存器分配地址或者為響應的地址起一個別名
ACC DATA 0E0H
B DATA 0F0H
SP DATA 81H
DPL DATA 82H
DPH DATA 83H
;NAME:
; NAME (modulename)
;NAME為輸出文件定義一個模塊名稱(modulename),如果在文件中沒有特定的
; (modulename),默認的應用第一個輸入文件的名稱。
?C_C51STARTUP SEGMENT CODE
;定義一個代碼段,名稱?C_C51STARTUP
?STACK SEGMENT IDATA ;堆棧
?C_STARTUP: LJMP STARTUP1 ;芯片上電復位后,執行的第一句就是該句,該句往下是開始執行啟動代碼
STARTUP1: IF IDATALEN <> 0 ;如果IDATALEN不為0,則將長度-1送R0
IDATALOOP: MOV @R0,A ;將IDATA區清0
ENDIF
IF XDATALEN <> 0 ;如果有外部數據存儲區
XDATALOOP: MOVX @DPTR,A
ENDIF
IF PPAGEENABLE <> 0 ;使能外部頁編址
ENDIF
IF PDATALEN <> 0 ;0-FF之間
PDATALOOP: MOVX @R0,A ;清0
ENDIF
IF IBPSTACK <> 0 ;使能SMALL模式下的模擬棧
EXTRN DATA (?C_IBP) ;使用其他目標模塊中定義的?C_IBP(模擬棧指針)
ENDIF
IF XBPSTACK <> 0 ;Large模式下使能模擬棧
EXTRN DATA (?C_XBP)
ENDIF
IF PBPSTACK <> 0 ;COMPACT模式下的模擬棧
EXTRN DATA (?C_PBP)
ENDIF
;硬件棧與模擬棧相區別,硬件棧是向上的,模擬棧是向下的
;在M51中可以看到具體的?STACK的值
;模擬棧的指針指向所在模式下,存儲區最大地址+1(0xFF+1或0xFFFF+1)
;硬件棧初始化指向棧底-1;
;因為C51中棧操作是先操作棧指針(加或減),然后在寫數
; This code is required if you use L51_BANK.A51 with Banking Mode 4
; Code Banking
;
Select Bank 0 for L51_BANK.A51 Mode 4
#if 0
; Initialize bank mechanism to code bank 0 when using L51_BANK.A51 with Banking Mode 4.
EXTRN CODE (?B_SWITCH0)
#endif
;
;該文件中的一些符號的值的大小,可以在M51中看到,此外,可以通過系統上電復位后反
;匯編程序看到詳細的執行過程
;文件中的一些命令可以查閱KeilC幫助文檔