.bss
 
 SP_save:        .res    1
-SHFLOK_save     .res    1
+SHFLOK_save:    .res    1
 LMARGN_save:    .res    1
 .ifndef __ATARIXL__
 APPMHI_save:    .res    2
 
 
         .export         _get_ostype
 
+        .include "atari.inc"
+       .include "romswitch.inc"
+
 .ifdef __ATARIXL__
 
-        .include "atari.inc"
         .import __CHARGEN_START__
         .segment "LOWCODE"
-.macro  disable_rom
+
+.macro  disable_rom_save_a
         pha
-        lda     PORTB
-        and     #$fe
-        sta     PORTB
-        lda     #>__CHARGEN_START__
-        sta     CHBAS
-        sta     CHBASE
-        pla
-.endmacro
-.macro  enable_rom
-        lda     PORTB
-        ora     #1
-        sta     PORTB
-        lda     #$E0
-        sta     CHBAS
-        sta     CHBASE
+       disable_rom
+       pla
 .endmacro
 
 .else   ; above atarixl, below atari
 
-.macro  disable_rom
-.endmacro
-.macro  enable_rom
+.macro  disable_rom_save_a
 .endmacro
 
-.endif
+.endif  ; .ifdef __ATARIXL__
 
-.proc   _get_ostype
+
+; unknown ROM
+
+_unknown:
+        lda     #0
+        tax
+        disable_rom_save_a
+        rts
+
+
+_get_ostype:
 
         enable_rom
         lda     $fcd8
         and     #%00111000
         ora     #%11
 _fin:   ldx     #0
-        disable_rom
-        rts
-
-; unknown ROM
-
-_unknown:
-        lda     #0
-        tax
-        disable_rom
+        disable_rom_save_a
         rts
 
 ; 1200XL ROM
 
         lda     #%00010001
         bne     _fin
-
-.endproc
 
--- /dev/null
+;
+; Macros to disable and enable the ROM on Atari XL systems.
+;
+; Christian Groessler, chris@groessler.org, 19-Sep-2013
+;
+
+
+.ifdef __ATARIXL__
+
+USEWSYNC=      1
+
+.if USEWSYNC
+.macro  wsync
+        sta     WSYNC
+.endmacro
+.else
+.macro  wsync
+.endmacro
+.endif
+
+
+.macro  disable_rom
+        lda     PORTB
+        and     #$fe
+        wsync
+        sta     PORTB
+        lda     #>__CHARGEN_START__
+        sta     CHBAS
+        sta     CHBASE
+.endmacro
+.macro  disable_rom_quick
+        lda     PORTB
+        and     #$fe
+        sta     PORTB
+        lda     #>__CHARGEN_START__
+        sta     CHBAS
+        sta     CHBASE
+.endmacro
+.macro  disable_rom_val val
+        lda     val
+        wsync
+        sta     PORTB
+        lda     #>__CHARGEN_START__
+        sta     CHBAS
+        sta     CHBASE
+.endmacro
+
+.macro  enable_rom
+        lda     PORTB
+        ora     #1
+        wsync
+        sta     PORTB
+        lda     #$E0
+        sta     CHBAS
+        sta     CHBASE
+.endmacro
+.macro  enable_rom_quick
+        lda     PORTB
+        ora     #1
+        sta     PORTB
+        lda     #$E0
+        sta     CHBAS
+        sta     CHBASE
+.endmacro
+
+.else   ; above __ATARIXL__, below not
+
+.macro  disable_rom
+.endmacro
+.macro  enable_rom
+.endmacro
+
+.endif
 
 ;
 
 DEBUG  =       1
-USEWSYNC=      1
 CHKBUF =       1       ; check if bounce buffering is needed (bounce buffering is always done if set to 0)
 
 .ifdef __ATARIXL__
         .include        "atari.inc"
        .include        "save_area.inc"
         .include        "zeropage.inc"
+        .include        "romswitch.inc"
+
        .import         __CHARGEN_START__
 
        .export         sram_init
 BUFSZ          =       128             ; bounce buffer size
 BUFSZ_SIO      =       256
 
-.macro wsync
-.if USEWSYNC
-       sta     WSYNC
-.endif
-.endmacro
-
-.macro disable_rom
-       lda     PORTB
-       and     #$fe
-       wsync
-       sta     PORTB
-       lda     #>__CHARGEN_START__
-       sta     CHBAS
-       sta     CHBASE
-.endmacro
-.macro disable_rom_quick
-       lda     PORTB
-       and     #$fe
-       sta     PORTB
-       lda     #>__CHARGEN_START__
-       sta     CHBAS
-       sta     CHBASE
-.endmacro
-.macro disable_rom_val val
-       lda     val
-       wsync
-       sta     PORTB
-       lda     #>__CHARGEN_START__
-       sta     CHBAS
-       sta     CHBASE
-.endmacro
-
-.macro enable_rom
-       lda     PORTB
-       ora     #1
-       wsync
-       sta     PORTB
-       lda     #$E0
-       sta     CHBAS
-       sta     CHBASE
-.endmacro
-.macro enable_rom_quick
-       lda     PORTB
-       ora     #1
-       sta     PORTB
-       lda     #$E0
-       sta     CHBAS
-       sta     CHBASE
-.endmacro
-
 .segment "INIT"
 
 ; Turn off ROMs, install system and interrupt wrappers, set new chargen pointer