Self-modifying-code subroutine EXAMP EQU $03 ;Example table element (0 to N) to transfer from the SIZE EQU $0E ;For this example, all elements have length 14 (base 10). ;TABLE to TARGET ********************************************************************************* * Ram Table * ********************************************************************************* ORG $0050 SMCR@-{A}M RMB $4 ;Self-modifying-code subroutine TEMPX RMB $1 ;Temp IDX storage TEMPA RMB $1 ;Temp ACC storage TARGET RMB SIZE ;Target location of element length SIZE ********************************************************************************* * Initialization * ********************************************************************************* ORG $0400 ;Start of rom INIT LDA SMCROM ;Initialize the self-modifying-code. STA SMCR@-{A}M ;LDA instruction LDA SMCROM+$3 STA SMCR@-{A}M+$3 ;RTS instruction ********************************************************************************* * Program start * ********************************************************************************* BEGIN LDX #EXAMP ;Transfer the example element JSR XFER ;Transfer all bytes from the TABLE rom to TARGET ram STOP ********************************************************************************* * Subroutines * ********************************************************************************* * Subroutine XFER * This routine transfers an element of the array TABLE(X) to TARGET. * The length of each element, in bytes, is SIZE (1<=SIZE<=255). * The number of the element to be transferred is in IDX. * Both ACC and IDX are returned intact. XFER STX TEMPX ;Temporarily save IDX STA TEMPA ;Temporarily save ACC LDA #SIZE ;Calculate the effective address of the first ;byte for the desired element. MUL ADD SMCROM+$02 ;Add the element offset to the TABLE offset STA SMCR@-{A}M+$02 TXA ADC SMCROM+$01 STA SMCR@-{A}M+$01 CLRX ;Begin transferring the individual bytes, starting with the ;byte located at the effective address and ending with the byte ;located at the effective address + SIZE -1 GETBYTE JSR SMCR@-{A}M ;Get the byte by extended addressing STA TARGET,X ;Save in the TARGET ram INC SMCR@-{A}M+$02 ;Add 1 (double-precision) to the SMCR@-{A}M extended address BNE NOINC INC SMCR@-{A}M+$01 NOINC INCX ;Update the index counter CPX #SIZE ;Quit when SIZE bytes have been transferred BNE GETBYTE LDX TEMPX ;Restore IDX LDA TEMPA ;Restore ACC RTS * Subroutine SMCROM (Dummy) * This dummy SMCROM (Self-modifying-code ROM) routine is transferred * to SMCR@-{A}M during program initialization. Extended address TABLE, the starting * address of the data table, is used to calculate an absolute address of a byte * of data in the table which is then transferred into ACC. SMCROM LDA TABLE ;Absolute addressing pointing to the start of the RTS ;message table ********************************************************************************* * Data table * ********************************************************************************* * This is the actual data table which can contain any number of entries (up to * the limits of the HC05's rom space), with all entries of length SIZE. TABLE FCB 'TABLE_ENTRY_00' FCB 'TABLE_ENTRY_01' FCB 'TABLE_ENTRY_02' FCB 'TABLE_ENTRY_03';Selected element for this example * . * . * . FCB 'TABLE ENTRY 99' ********************************************************************************* * Vectors * ********************************************************************************* ORG $1FFE RES FDB INIT ;Reset vector