;************************************
; \\|//
; (@ @)
; ASHLEY4.
; Entry for the 512b compo.
;
; Assemble with fasm 
; c:\fasm CdPod.asm CdPod.bin
;
; Keys = (UpArrow + Enter = Play).
; Keys = (DownArrow + Enter = Stop).
;
;************************************
org 0x7C00 

use16
;****************************
; Realmode startup code.
;****************************

start:
        xor   ax,ax
        mov   ds,ax
        mov   es,ax
        mov   ss,ax
        mov   sp,0x7C00 

;****************************
; Vesa start code.
;****************************

        mov  bx,4112h
        mov  ax,4f01h
        mov  di,Mode_Info	
        mov  cx,bx
        int  10h 
        
        mov  ax,4f02h
        int  10h

;*****************************
; Setting up, to enter pmode.
;*****************************

        cli 
        lgdt  [gdtr]
        
        mov   eax, cr0
        or    al,0x1 
        mov   cr0,eax
 
        jmp   0x10: protected

;*****************************
; Pmode. ;-)
;*****************************

use32
protected:
        mov   ax,0x8 
        mov   ds,ax
        mov   es,ax
        mov   ss,ax
        mov   esp,0x7C00
;*****************************
; Turn floppy off (if space).
;*****************************

        mov   dx,3F2h
        mov   al,0
        out   dx,al

;*****************************
; Do we have 32 BitsPerPixel.
;*****************************

       ; cmp   byte[ModeInfo_BitsPerPixel],32
       ; jne   KeyBoardStart 

;*****************************
; fade background screen.
;*****************************

fade_screen:
        mov   edx,[ModeInfo_PhysBasePtr]
        mov   edi,edx
        xor   eax,eax
        mov   al,0xc5          
        xor   ebx,ebx
        mov   bl,195 
DoLoop:    
        mov   cx,640*2 
        dec   eax    

        rep   stosd
       
        dec   ebx
        jnz   DoLoop



;***********************************
; Draws the Pod.
;***********************************

        mov   edi,236*4+640*4*125 
        add   edi,edx
Pod:
        xor   ecx,ecx
        mov   ebx,245
button2:
        mov   cl,14
        mov   al,0x2d
letsloop1:
        stosd
        add   eax,15
        loop  letsloop1
        mov   cl,130
        rep   stosd
        mov   cl,14
letsloop2:
        stosd
        sub   eax,15
        loop  letsloop2
        add   edi,640*4-158*4
        dec   ebx
        jnz   button2
       
;***********************************
; Draws the Pod window.  :-(
;***********************************

        mov   edi,263*4+640*4*143
        add   edi,edx 
PodWindow:
        mov   eax,0xffa6ffff
        xor   edx,edx
        mov   dl,65
DrawSomePixals:
        mov   cl,104
        rep   stosd
        add   edi,640*4-104*4 
        dec   edx
        jnz   DrawSomePixals

        xor   eax,eax
        mov   dl,65
DrawaLine:
        sub   edi,641*4
        stosd
        dec   edx
        jnz   DrawaLine

        mov   cl,104
        rep   stosd
     
;********************************
; Take's user input.
;********************************

KeyBoardStart:
       mov   al,0x48
       jmp   OverXor

;********************************* 
; Main keyboard loop.
;********************************* 

NoKey:
        xor   eax,eax 
        in    AL,60h 
OverXor:   
        cmp   al,0x50      
        jne   NoKey1
       
        mov   si,Stop
        call  DrawImage
NoKey1:
        cmp   al,0x48   
        jne   NoKey1A
      
        mov   si,Play
        call  DrawImage
        
NoKey1A:
        cmp   al,0x1c   
        jne   NoKey
        
        cmp   si,stop
        je    StopCdPlayer

;***********************************
; Seting up for playing cd.
;***********************************

SetUpForPlayCd: 
        pushad    
        mov   si,packet1
        call  Send_Atapi_Packet
        popad

        jmp   NoKey

;***********************************
; Seting up for stoping cd.
;***********************************

StopCdPlayer:
        pushad
        mov   si,packet2                                       
        call  Send_Atapi_Packet
        popad

        jmp   NoKey


;***********************************
; Draws text.
;***********************************

DrawImage:
      
      mov   edi,[ModeInfo_PhysBasePtr]
      add   edi,270*4+640*4*148 
      xor   edx,edx
      mov   dl,7 
      cld            
GetData:             
      lodsd                
      MOV     ebx,eax           
      MOV     CL,32          
CheckBit:                
      test    ebx,80000000h        
      JZ      ZeroBit      
     
      xor    eax,eax           
      stosd                  
      jmp     Skip           
ZeroBit:             
      mov     eax,0xffa6ffff
      stosd
Skip:                 
      shl     ebx,1             
      loop    CheckBit        
      add     edi,640*4-32*4
      dec     edx              
      jne     GetData
      
      ret

;***********************************
; Include file for atapi driver.
;***********************************
       
include 'Ata_Atapi1.inc'


;*************************************
; GDT. 
;*************************************

gdt:        dw    0x0000, 0x0000, 0x0000, 0x0000
sys_data:   dw    0xFFFF, 0x0000, 0x9200, 0x00CF
sys_code:   dw    0xFFFF, 0x0000, 0x9800, 0x00CF
gdt_end:

gdtr:	    dw gdt_end - gdt - 1	                                  
	    dd gdt 

;*************************************
; Put initialized data here.
;*************************************
Play:
db     44h,38h,40h,78h       
db     44h,44h,40h,44h
db     28h,44h,40h,44h
db     10h,7ch,40h,78h           
db     10h,44h,40h,40h
db     10h,44h,40h,40h
db     10h,44h,7ch,40h
play:

Stop:
db     78h,38h,7ch,38h
db     44h,44h,10h,44h
db     44h,44h,10h,40h
db     78h,44h,10h,38h
db     40h,44h,10h,04h
db     40h,44h,10h,44h
db     40h,38h,10h,38h
stop:
packet1:
            db 0x45                     
	    db 0x0                    
	    db 0x0 ;ff                     
	    db 0x0 ;ff                     
            db 0x0 ;ff                    
	    db 0x0 ;ff                     
	    db 0x0
	    db 0xff                             
	    db 0xff                 
	   ; db 0x0        
	   ; db 0x0
	   ; db 0x0

packet2:
            db 0x4b      ;1b                     
	    db 0x1                     
       	    db 0x0                                 ;3=close,2=eject,1=start,0=stop    
	    db 0x0                     
        ;    db 0x0                    
	;    db 0x0                     
	;    db 0x0
	;    db 0x0                             
	;    db 0x0                 
	;    db 0x0        
	;    db 0x0
	;    db 0x0


;*************************************
; Make program 510 byte's + 0xaa55
;*************************************
            
times 510- ($-start)  db 0  
dw 0xaa55

;*************************************
; Put uninitialized data here.
;*************************************

include 'vesa.inc'

;*************************************
; $$$$ŁŁŁŁ( vote for me ;-))ŁŁŁŁ$$$$.
;*************************************