; ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
;   crtxfix.asm: C Runtime XRAM Operations Fix For STC-51 Only
;
;   Copyright (C) 2025 TechQI<techqi@126.com>
;
;   Licensed under the Apache License, Version 2.0 (the "License");
;   you may not use this file except in compliance with the License.
;   You may obtain a copy of the License at
;
;       http://www.apache.org/licenses/LICENSE-2.0
;
;   Unless required by applicable law or agreed to in writing, software
;   distributed under the License is distributed on an "AS IS" BASIS,
;   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;   See the License for the specific language governing permissions and
;   limitations under the License.
; ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓


.area GSINIT3 (CODE)
.globl _DPS
; -------------------------------------------------------------------------
; copy XINIT to XISEG, with auto-incremented DUAL-DPTR
; -------------------------------------------------------------------------
__mcs51_genXINIT::
	mov     R0,#l_XINIT
	mov     A, R0
	orl     A, #(l_XINIT >> 8)
	jz	    1001$
	mov     R1, #((l_XINIT+255) >> 8)
	mov     _DPS, #0x38     ; ID1=0, ID0=0, TSL=1, AU1=1, AU0=1, SEL=0
	mov	    DPTR, #s_XINIT	; DPTR0 for code
	mov     DPTR, #s_XISEG	; DPTR1 for xdata
1000$:
    clr     A
	movc	A, @A+DPTR      ; read code byte (increment DPTR0 automatically) 
	movx	@DPTR, A        ; write to xdata (increment DPTR1 automatically)
	djnz	R0, 1000$
	djnz	R1, 1000$
    mov     _DPS, #0x00
1001$:


.area GSINIT4 (CODE)
; -------------------------------------------------------------------------
; Set uninitialized XRAM to zero
; -------------------------------------------------------------------------
__mcs51_genXRAMCLEAR::
; PDATA
    mov     R0, #l_PSEG
    mov     A, R0
    jz      2001$
    mov     DPH, #0
    mov     DPL, #s_PSEG
    clr     A
2000$:
    mov     _DPS, #0x18     ; ID1=0, ID0=0, TSL=0, AU1=1, AU0=1, SEL=0
	movx	@DPTR, A        ; write to xdata (increment DPTR0 automatically)
    djnz	R0, 2000$

; XDATA
2001$:
    mov     R0, #l_XSEG
    mov	    A, R0
    orl	    A, #(l_XSEG >> 8)
    jz	    2003$
    mov	    R1, #((l_XSEG + 255) >> 8)
    mov	    DPTR, #s_XSEG
    clr     A
2002$:
    mov     _DPS, #0x18     ; ID1=0, ID0=0, TSL=0, AU1=1, AU0=1, SEL=0
    movx	@DPTR, A        ; write to xdata (increment DPTR0 automatically)
    djnz	R0, 2002$
    djnz	R1, 2002$
2003$:
    mov     _DPS, #0x00
