;; Read the banner string from flash and output it over the serial port. Since
;; we are running from RAM, we can read from the flash directly without having
;; to use the Utility ROM data transfer functions (moveDP0inc, etc...).
move SC.4, #0
move DPC, #0 ; Set pointers to byte mode.
move DP[0], #(stringData * 2) ; Point to byte address of string data.
stringLoop:
move Acc, @DP[0]++
sjump Z, stringEnd
lcall #TxChar
sjump stringLoop
stringEnd:
move DPC, #1Ch ; Set pointers to word mode.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This portion of the code (addresses 200h and higher) will remain in flash.
org 0200h
stringData:
db 0Dh, 0Ah, "Executing code from RAM....", 00h
请注意,如图1所示,SC.4 (CDA0)位影响将哪一半程序闪存(上半页或下半页)以字节模式映射至数据内存。当使用字模式指针时,整个程序闪存被一次性映射至数据内存。
第二,现在虽然闪存在数据空间可存取,但SRAM不可直接存取。这意味着不能对SRAM的存储单元进行读或写操作,应用程序必须采取迂回的方法。从SRAM存储单元读取数据可按照在闪存中运行的代码从闪存存储单元读取数据相同的方式实现—利用应用ROM数据传递函数(moveDP0inc等)。然而,由于在应用ROM中没有类似的函数可实现直接写操作,所以应用程序必须提供一个小函数驻留在闪存中,该函数可被RAM中驻留的代码直接调用来执行写操作。
以下的代码演示用来读和写RAM变量varA的方法,其初始值随其它部分的应用程序被从闪存复制到RAM,地址范围为0000h-01FFh。
scall printVar
scall incrVar
scall printVar
scall incrVar
scall printVar
scall incrVar
move Acc, #0Dh
lcall #TxChar
move Acc, #0Ah
lcall #TxChar
sjump $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Variables stored in RAM (program) space. They can be read using the
;; Utility ROM data transfer functions (such as UROM_moveDP0) and written
;; using the writeDP0 function which remains in flash.
;;
varA:
dw 'A'
;================================================================
;=
;= printVar
;=
;= Reads the varA RAM variable value and sends it over the serial port.
;=
printVar:
move DPC, #1Ch ; Word mode
move DP[0], #varA ; Variable's location in UROM data space
lcall UROM_moveDP0 ; Moves variable value into GR.
move Acc, GR
lcall #TxChar
ret
;==============================================================
;=
;= incrVar
;=
;= Reads the varA RAM variable value, adds 1 to it, and stores it back in RAM.
;=
incrVar:
move DPC, #1Ch ; Word mode
move DP[0], #varA ; Variable's location in UROM data space
lcall UROM_moveDP0 ; Moves variable value into GR.
move Acc, GR
add #1
move GR, Acc
lcall writeDP0
ret
;==================================================================
;=
;= TxChar
;=
;= Outputs a character to the serial port.
;=
;= Inputs : Acc.L - Character to send.
;=
org 01F0h
move SBUF, Acc ; Send character.
TxChar_Loop:
move C, SCON.1 ; Check transmit flag.
sjump NC, TxChar_Loop ; Stall until last transmit has completed.
move SCON.1, #0 ; Clear the transmit flag.
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This portion of the code (addresses 200h and higher) will remain in flash.
org 0200h
stringData:
db 0Dh, 0Ah, "Executing code from RAM....", 00h
;===============================================================
;=
;= WriteRAM
;=
;= This is a routine that can be called by code running in the RAM to load
;= a new value into a byte or word location in the RAM.
;=
;= Inputs : DP[0] - Location to write (absolute starting at 0000h) in RAM.
;= GR - Value to write to the RAM location.
;=
;= Notes : DP[0] must be configured to operate in word or byte mode as
;= desired before calling this function. Following a call to this
;= function, DP[0] must be refreshed before it is used to read data.
;=
writeDP0:
move @DP[0], GR
ret
在执行时,示例代码通过串口输出以下的文字(图2)。
图2. 示例代码通过串口的输出文字
结论
利用MAXQ8913及其它MAXQ微控制器采用的Harvard内存映射架构,可以将不同的物理内存段(例如数据SRAM)映射为程序或数据内存空间。在数据SRAM中执行部分应用程序为性能提升和降低功耗提供了潜力。不过该过程也增加了应用程序的复杂性。