Calling the Main ROM from Option ROM

From Bitchin100 DocGarden
Jump to: navigation, search

The Main (BASIC or OS) ROM contains many routines that come in handy when writing code designed to run from the Option ROM. For example, extensive routines for doing text I/O to the LCD screen are available in the Main ROM.

The trampoline code to transit to the Main ROM and back is tricky to get right.

Various material is available in the Compuserve archives, and Mo Budlong's book "Secrets of the ROM Revealed."

In developing the utility ROM image that ships with REX, Steve Adolph put this old code through its paces and found room for improvement. Included here are the routines needed for leveraging the Main ROM from the Option ROM

Source Code


STDCALL:
       DI
       SHLD    HOLDH           ;Caller's HL
       XCHG
       SHLD    HOLDD           ;Caller's DE
       POP     H               ;(HL)=Routine to Call
       MOV     E,M
       INX     H
       MOV     D,M             ;DE=Routine to Call
       INX     H               ;HL=Return Address
       PUSH    H
       LXI     H,OPON          ;return through OPON
       PUSH    H
       PUSH    D               ;Return Address
       LHLD    HOLDD           ;Caller's DE
       XCHG
       LHLD    HOLDH           ;Caller's HL

       PUSH    H               ; 0363, FB C9  EI, RET
       lxi     h,0363h         ; return here ---> EI; RET
       XTHL                    ; works in T200 and M100

       jmp     STD_ON



INTCALL:
       SHLD    INTH            ;Caller's HL
       XCHG
       SHLD    INTD            ;Caller's DE
       POP     H               ;(HL)=Routine to Call
       MOV     E,M
       INX     H
       MOV     D,M             ;DE=Routine to Call
       INX     H               ;HL=Return Address
       PUSH    H
       LXI     H,OPON          ;return through OPON
       PUSH    H
       PUSH    D               ;Return Address
       LHLD    INTD            ;Caller's DE
       XCHG
       LHLD    INTH            ;Caller's HL

       JMP     STD_ON                          ;make sure STD_ON does not enable interrupts!



;---------------------------------------------------------
;STD_ON: Turns off the Option ROM (or turns on Standard ROM)
;dedicated to rom calls and interrupts
;---------------------------------------------------------


                                            ; modification to STDCALL to delay the
EI till the end.
       .ORG    007EH
STD_ON:
       PUSH    PSW
       PUSH    H               ; 26C8, F1 C9  POP PSW, RET
       LXI     H,26C8H         ; it returns to this location --> pop psw; ret
       XTHL
                               ; return to location pushed on stack
       lda     0FF45h
       ani     0FEh            ; manage FF45 properly
       sta     0FF45h
       OUT     0E8H
       RET                     ; RET can be found at 008EH in stdrom
                               ; in both M100 and T200



OPON:          ; this code in upper ram somewhere safe, like ALTLCD
                    ; this routine critical for proper return from
STDCALL and INTCALL

       di
       PUSH    PSW             ; Model 100 logic.
       lda     0FF45h
       ori     001h            ; manage FF45 properly
       sta     0FF45h          ; sensitive - cannot take an interrupt here
                               ; either the rom switch is screwed up, or
                               ; PSW gets trashed, or FF45 gets trashed

                               ; if the interrupt occurs after sta 0FF45 but before
                               ; out E8, then we might not return to REXROM (and the
                               ; rest of port E8 gets screwed up)
                               ; we would return to somewhere in main rom.
       OUT     0E8H

       POP     PSW
       ei
       RET