CodeStart:                               
    DisableInts
    move.b  ($A10001), d0               ; Initialization for TMSS
    and.b   #$0F, d0
    beq.s   @NoTMSS  ; initialize blast processor
    move.l  #"SEGA", ($A14000)    
@NoTMSS:  
    move.l #ClearMethods, -(a7)
SetVDPDefaults:
    lea     (VDPControl), a0            ; Init VDP
    move.w  #$8014, (a0)                ; No HINT, no HV latch
    move.w  #$8174, (a0)                ; Disable display
    move.w  #$8230, (a0)                ; Scroll A: $C000
    move.w  #$8407, (a0)                ; Scroll B: $E000
    move.w  #$8578, (a0)                ; Sprites: $F000
    move.w  #$8700, (a0)                ; Background: pal 0, color 0
    move.w  #$8A00, (a0)                ; HInt every scanline
    move.w  #$8B00, (a0)                ; No VINT, full scroll for H+V
    move.w  #$8C81, (a0)                ; H40, no S/H, no interlace
    move.w  #$8D3E, (a0)                ; HScroll: $F800
    move.w  #$8F02, (a0)                ; Autoincrement: 2 bytes
    move.w  #$9001, (a0)                ; Scroll size: 64x32
    move.w  #$9100, (a0)                ; Hide window plane
    move.w  #$9200, (a0)                ;  "     "      "
    rts
    
ClearMethods:
    move.l #Demo_Main, -(a7)
ClearEverything:
	lea VDPCONTROL, a0
	lea VDPDATA, a1
	move.w #$8114, VDPCONTROL
    moveq   #0, d0                      ; Clear VRAM, necessary to do for real hardware
    move.l  #$40000000, (a0)
    lea     (VDPData), a1
    move.w  #$800-1, d1
@ClearVRAMLoop:
    rept 8
    move.l  d0, (a1)
    endr
    dbf     d1, @ClearVRAMLoop

    move.l  #$C0000000, (a0)            ; Clear CRAM
    moveq   #8-1, d1
@ClearCRAM:
    rept 4
    move.l  d0, (a1)
    endr
    dbf     d1, @ClearCRAM

;    move.l  #$40000010, (a0)            ; Clear VSRAM
	setvsramaddr 0, a0
    moveq #32-1, d1
@ClearVSRAM
	move.w #0, (a1)
	dbf d1, @ClearVSRAM
	
    
    move.l #65536/4-3, d1
    lea ($FF0000), a0
@ClrRAM
    move.l d0, (a0)+
    dbf d1, @ClrRAM

    move.b #$40, ($A10009)              ; Initialize Joypads
    move.b #$40, ($A1000B)
    move.w #$4e73, (RAM_HINT)
    move.w (JMPOPCODE), (RAM_VINTLOC)
    move.l #INTERRUPT, (RAM_VINTLoc+2)
    move.w  #$8174, (VDPControl)        ; Turn on display                      
    
;    EnableInts
    rts
    jmp Demo_Main
    
   
EmuDetect:

    disableints
    lea (font), a0
    lea ($FF8000), a1
    
    jsr RunLengthDecoding
    
    singledmavram $FF8000, 0, 127*32, vdpcontrol
    setcramaddr 0, vdpcontrol
    move.w #$EEE, vdpdata
    
    moveq #2, d4
@peter
    moveq #0, d7
    moveq #0, d6
    waitframe
    jmp @p
@q
    addq.l #1, d7    
@p   btst #3, (VDPControl+1)
    
    bne.s @q
    jmp @r
    
@s
    addq.l #1, d6    
@r   btst #3, (VDPControl+1)
    
    beq.s @s  
    
    
    dbf d4, @peter
    
    cmp.w #$464, d7
    blt.s Emulator
    cmp.w #$466, d7
    bgt.s Emulator    
    cmp.w #$B2D, d6
    blt.s Emulator    
    cmp.w #$B2E, d6
    bgt.s Emulator
    
    
EMURTS:
    rts
    
Emulator:
    lea (Emustring), a0
    ;r ClearEverything
    moveq #6, d1
    moveq #0, d0
    
    moveq #14-1, d6
@writestring:
    addq.w #1, d1
    jsr Print_String
    lea 42(a0), a0
    dbf d6, @writestring
    move.b #$00, ($A10003)
@TstBtn:
    btst #5, ($A10003)
    bne.s @TstBtn
    
    move.b #$40, ($A10003)
    bra.w EMURTS

Emustring:
   ;      0123456789012345678901234567890123456789
    dc.b "   You're using an Emulator. Please be  ",0,0
    dc.b "     aware that some of the features    ",0,0
    dc.b "     of this demo are currently NOT     ",0,0
    dc.b " emulated by any emulators. It has been ",0,0
    dc.b "       thoroughly verified on:          ",0,0
    dc.b "                                        ",0,0
    dc.b "       Genesis Plus GX      v1.6        ",0,0
    dc.b "       Gens/GS                r7        ",0,0
    dc.b "       Gens                v2.14        ",0,0
    dc.b "       KEGA Fusion         v3.64        ",0,0
    dc.b "       PicoDrive          v1.45a        ",0,0
    dc.b "       Regen             v0.972d        ",0,0
    dc.b "                                        ",0,0
    dc.b "   ** Please press the START button **  ",0,0
    even