summaryrefslogblamecommitdiffstats
path: root/src/mame/drivers/raiden2.cpp
blob: c1d61acb00b60a21f4880e4aa1e12c972ee8768f (plain) (tree)
1
2
3
4
5
6
7
8
9
                    
                                                                                   

                                                                                                         
                                                                                   
 

                                                                                                       
 
                                                                                       
 
                                                                                               
 


                                                                                                          



                                                                                                           





                                                                                 
 
 

                                                                                                           







































                                                                                            
                                                                                        





































                                                                                                  
 

                                
          
          
              
 
           




                                                      



































                                                                                              







                                                                          
                                                                 
 
                                                                                                         
 
                

                             
                        
                        
                              
                           
                           
                         
                            
 




                     




                                   
                                 


                                                
 




                                       
 
 
                                    
 

                                              

 
  
                             
 
                                        

 
                             
 
                                        
 
  
 
                     
 
 
                                                   
 



                                                                                                              




















                                                                  
 
 
 
                                                                                          
 
                                                               


                           
 


                                         
                                                           
 
                                                  
                                          
 
                                               
 




                                                                                                
           
 
                                   







                                            
                        

                                                
                                                





                                                
                                           

                                      
 


                           


















                                                                  





                                                                                                                                                                                                                                          
 

                                                              
                                                         


                                                            
                                                                                                                         
 
 

                                                              
                                                         


                                                            
                                                                                                                                 
 
 

                                                              
                                                         


                                                            
                                                                                                                                 
 
 

                                                              
                                                         


                                                            
                                                                                                                                         
 




                                              
                            





                                                       
                                                   
 
                                         
                                                  

 
                                                  
 
                                        
                                                 

 
                                                   
 
                                         
                                                  

 
                                             
 
                                         
                                            

 




                                               

                                            
                                



                                             
                                  
         



                                          
                      
                                         
            
                                         





                                             
                                               

                                           
                                                           

                 
                                          

                                            
                                                          



                 




                                               

                                                

                                



                                                
                                                           



                 



                                                    



                                                   
         




                                                 



 

                                          
                                                       
 
                                         
                                            
 
                                                
 
                                                    

 
                                                      
 
                                        
                                            
 
                                                 
 
                                             

 
                                                       
 
                                         
                                            
 
                                                
 
                                             

 
                                                       
 
                                         



                                   
                                                                

 

                                      
 
                                         
 



                                                             




                                                      
 



                                                                                                                                                                                

 
                                                
 
                                                                                                                 
 


                       


                                                               

                                                                       
                                                                       
                                              

                                                                                 
 
                                                     









                                                                                                                                      
 



                                                           
                                                                                                                     
 
                                                      

                                             
 

                                                                          
 
                                          

                                                                                   

                                                                          
 
                                          

                                                                                  

                                                                          
 
                                          
                                                                                   
 

                                                                          
 

                                                                             
 

                                                                          
 

                                                             
 











                                       
                                                      
 
                                                                                     



 


                               
                                       
 


                                                                                              
 
                                     
 



                                                        
 

                       


 
                                           
 
                                                       

 
                                              
 
                                                                    

 
                                              
 
                                                                    

 
                                              
 
                                                                    
                         


                                       


         
                                              
 
                                                                    
                         


                                       


         
                                             
 
                                                        

 
                                             
 
                                                        

 
                                               
 
                                                          


                                                         
                                                    


                                          

                                                                                

                                                             
                                                                            




                                                               
                                                                            







                                                               
                                               
 
                                     
                         
                                            






                                                      
                                  
 


                   
                    

 
                                           
 
                       
                      
 

                                           
 
                     
                     

 
                                            
 
                       
                      
 

                                            
 
                        
 


                     
                                            
 


                     
                    
                      
 

                                           
 
                     


                     
                                          
 


                     
                    
                      

 


                                             
                                           
                                                                          

                                                        
                                              

         
 
 


                                              
                                                              




                                              
                                                              

 




                                                    




                                                   

                                                
                                       
                                                                            
 

                                                                              
 

                                                            
 


                                               
                                                                                                    
 


                                                      
                    
         

                                                

                                                
 
                          
         
                                                                     
                                  

 




                                                
                                                
 


                            
                                               
 
                           






                                                 




                                                 
                                                
 
                           
 
 
                 































































                                                                                                                                                                                       

                             










                                                                                                                
 
                                                                   
 






                                                                                                                              
 


                                              
 
                                    
 





                                                                                                               
 






                                                                                             
 


























                                                                                                                              
 





















                                                                                                               
 







































































                                                                                                                              
 
 


                                   
                                                                            
 
                                       
















                                                                                      
                                               
                                                                                          







                                                            
                                                                                          







                                                            
                                                                                  

                                                         
                                                                                         

                                                      
                                                                                           
                                                         

                                                                                                                   
                                                            
                                                                                      



                                           
                                                                                           



                                                         
                                                                               

                                                      
                                                                                        
 
                                                   




                                                       


                                    
                               
 
                                               
                                                                                                                   

                                                      
                                                                                                                   

                                                      
 


                                                                     

               


                                    



                                                                  











                                                                                      
                                                                                                       






                                                                                      
                                                                                                       
 













































                                                                                                              




                                                     

                                                                                          

                                                
                                                
                                             




                                                                                                         

                                                    
                                                                                                         

                                                    
                                                                                                         

                                                    
                                                                                                         

                                                    
               
 


                                  


                                                     


















































                                                          












































                                                                        
                                     

                                                                            
                                                                                                                                

             
 

                     
                                            

                                    


                                                                                  
 
                                                          
 

                                                            
 
                            
                                         
                                                                
                                                                                                         
                                                                       
 
                                                                       
                                         
                                               
 
                                              

                                                                                     
 
                                                    
                                                                                            
 
                                                        
 
                            
                                               
 
                                                            
                                                                                            

                                         
 
                                                                                          
                                                   
 
                                                                                          



                                                      
                                               

                                                                           
                  
 

                                           

                                           
 
                                                         
 
                                    
                                                                
                                                        

                  

                                             

                                             
 
                                                           


                  
                                             

                                    


                                                                                  
 
                                                           
 

                                                            
 
                            
                                         
                                                               
                                                            
                                                                                                         
                                                                       
 
                                                                       
                                         
                                               
 
                                              

                                                                                     
 
                                                    
                                                                                            
 
                                                        

                            
                                               
 

                                                                                            

                                                  
                                                                                   



                                                      
                                               

                                                                           
                  
 
                 

         
 









































































                                                                                                               


  

                                                                                                                
                                                     

                                                                                                             
 



                                                                                                                   


                                                                                                                   
                                                         



                                                                                                                                            

                                                                                                                                                                                                       















                                                                                                                                                              

  
                    
                                                               
                                                                                                                         
                                     
                                                                                                                         
                                     
 
                                                      
                                                                                                                                           
 
                                                                                         
                                                                                                                 
                                    
                                                           
 
                                                     
                                                                                                                                               
 
                                                              

                                                                                                                                                         
 
                                                                           



                                                                                                                                                                 
 
                                                               
                                                                                                                                                
 
                                                               
                                                                                                                                                
 
                                                      
                                                                          
                                                                                                                                  

       
                     
                                                               
                                                                                                                                                             
                                     
                                                                                                                          





                                                                                                                                           
                                                                                                                 
                                    
                                                           

                                                     
                                                                                                                                                                 




                                                                                                                                                         
                                                                           





                                                                                                                                                                 
                                                                                                                                                





                                                                                                                                                
                                                                                                                                  

       






                                       










                                                         
                      
                                                               
                                                                                                                                                             
                                     
                                                                                                                         
                                     
 
                                                      
                                                                                                                                           
 
                                                                                         
                                                                                                                                       
                                    
                                                           

                                                     
                                                                                                                                               

                                                              

                                                                                                                                                         
 
                                                                           



                                                                                                                                                                 
 
                                                               
                                                                                                                                                
 
                                                               
                                                                                                                                                
 
                                                      
                                                                          

                                                                                                                                  



































                                                   
                     
                                                               
                                                                                                                                                             
                                     
                                                                                                                         
                                     
 
                                                      
                                                                                                                                           
 
                                                                                         

                                                                                                                    
                                                           
 
                                                     
                                                                                                                                               
 
                                                              

                                                                                                                                                         
 
                                                                           



                                                                                                                                                                 
 
                                                               
                                                                                                                                                                  
 
                                                               
                                                                                                                                                
 
                                                      
                                                                          

                                                                                                                                  
 
                                                              
                                                               
                                                                                                                                                                                   
                                     
                                                                                                                                                
                                     
 
                                                      
                                                                                                                                           
 
                                                                                         
                                                                                                                                         
                                    
                                                           
 
                                                     
                                                                                                                                                
 
                                                              

                                                                                                                                                         
 
                                                                           



                                                                                                                                                                 
 
                                                               
                                                                                                                                                 
 
                                                               
                                                                                                                                                
 
                                                      
                                                                          

                                                                                                                                  
 





                                                                                                                                                                             
 

                                                                                                                                           
 



                                                                                                                                        
 

                                                                                                                                              
 


                                                                                                                                                         
 




                                                                                                                                                                 
 

                                                                                                                                               
 








                                                                                                                                                
                                                               
                                                                                                                                                                             
                                     
                                                                                                                         
                                     
 
                                                      
                                                                                                                                           
 
                                                                                         
                                                                                                                   
                                    
                                                           
 
                                                     
                                                                                                                                              
 
                                                              

                                                                                                                                                         
 
                                                                           



                                                                                                                                                                 
 
                                                               
                                                                                                                                               
 
                                                               
                                                                                                                                                
 
                                                      
                                                                          
                                                                                                                                  

       
                     
                                                               
                                                                                                                                                 
                                     
                                                                                                                     

                                     
                                                      
                                                                                                                                           
 
                                                                         
                                                                                                                                       
                                    
                                                           
 
                                                     
                                                                                                                                       
 
                                                              

                                                                                                                                                         
 
                                                                           



                                                                                                                                                                 
 
                                                               
                                                                                                                                     
 
                                                               
                                                                                                                                                
 
                                                      
                                                                          
                                                                                                                                  
       
 
                     
                                                               
                                                                                                                                                                         
                                     
                                                                                                                           

                                     
                                                      

                                                                                                                                           

                                                                                                                      
                                    
                                                           

                                                     
                                                                                                                                               




                                                                                                                                                         
                                                                           




                                                                                                                                                                 
                                                               
                                                                                                                                                
 
                                                               
                                                                                                                                                
 
                                                      
                                                                          
                                                                                                                                  
       
 


















                                                               
                                                               



                                                                                                                             
 
                                                      

                                                                                                                                           

                                                                                                                   
                                    
                                                           

                                                     
                                                                                                                      




                                                                                                                                                         
                                                                           




                                                                                                                                                                 
                                                               
                                                                                                                   
 
                                                               

                                                                                                                                                
                                                      
                                                                          
                                                                                                                                  

       
                      
                                                               



                                                                                                                        




                                                                                                                                           
                                                                                                                 
                                    
                                                           

                                                     
                                                                                                                  




                                                                                                                                                         
                                                                           





                                                                                                                                                                 
                                                                                                                





                                                                                                                                                
                                                                                                                                  
       
 
                                                             
                                                               
                                                                                                                                               
                                     
                                                                                                                                               




                                                                                                                                           

                                                                                                                 
                                    
                                                           

                                                     
                                                                                                                  




                                                                                                                                                         
                                                                           





                                                                                                                                                                 
                                                                                                                





                                                                                                                                                
                                                                                                                                  

       
                                                                                                                           
                                                               







                                                                                                                                     



                                                                                                                                           

                                                                                                                            
                                    
                                                           

                                                     
                                                                                                                                 











                                                                                                                                                                 
                                                                                                                                  





                                                                                                                                                
                                                                                                                                  
       
 
                                                                                          
                                                               







                                                                                                                             




                                                                                                                                           
                                                                                                                     
                                    
                                                           

                                                     
                                                                                                                      




                                                                                                                                                         
                                                                           





                                                                                                                                                                 
                                                                                                                    





                                                                                                                                                
                                                                                                                                  

       
                                                                                                                                                                
                                                               










                                                                                                                                                      
                                                           








                                                                                                                                              
                                                                           











                                                                                                                                                              

                    
                     
                                                               



                                                                                                                    
 
                                                      
                                                                                                                                                      
 
                                                                                         

                                                                                                                
                                                           
 
                                                     
                                                                                                                      
 
                                                              


                                                                                                                     
                                                                           




                                                                                                                                                              
                                                               
                                                                                                               
 
                                                               
                                                                                                                                                   
       
 
                       
                                                               



                                                                                                                                       
 
                                                      

                                                                                                                  
                                                                                         

                                                                                                                
                                                           
 
                                                     
                                                                                                                      
 
                                                              


                                                                                                                     
                                                                           




                                                                                                                                                              
                                                               
                                                                                                               
 
                                                               
                                                                                                               

       
                       
                                                               



                                                                                                                     
 
                                                      
                                                                                                                                                      
 
                                                                                         

                                                                                                                
                                                           

                                                     
                                                                                                                      




                                                                                                                     
                                                                           




                                                                                                                                                              
                                                               

                                                                                                               
                                                               
                                                                                                                                                   

       
                      
                                                               



                                                                                                                         
 
                                                      
                                                                                                                                                      
 
                                                                                         

                                                                                                                
                                                           
 
                                                     
                                                                                                                      
 
                                                              

                                                                                                                     
 
                                                                           



                                                                                                                                                              
 
                                                               
                                                                                                               
 
                                                               
                                                                                                                                                   

       
                      
                                                               



                                                                                                                     
 
                                                      
                                                                                                                                                      
 
                                                                                         

                                                                                                                
                                                           
 
                                                     
                                                                                                                      
 
                                                              


                                                                                                                     
                                                                           




                                                                                                                                                              
                                                               
                                                                                                               
 
                                                               
                                                                                                                                                   

       
                      
                                                               



                                                                                                                     
 
                                                      
                                                                                                                                                       
 
                                                                                         

                                                                                                              
                                                           

                                                     
                                                                                                               

                                                              

                                                                                                                       
 
                                                                           

                                                                                                                                                                             

                                                                                                                             
 
                                                               
                                                                                                             
 
                                                               
                                                                                                                                                   
       
 
 
                       
                                                               










                                                                                                                                                       
                                                           







                                                                                                                         
                                                                           











                                                                                                                                                                             

                      
                                                               










                                                                                                                                                       
                                                           







                                                                                                                       
                                                                           











                                                                                                                                                                             
 

































                                                                                                                                                                             

                       
                                                               











                                                                                                                                                      
                                                           







                                                                                                                      
                                                                           













                                                                                                                                                              
                    

                                                                         










                                                                                                  

                                







































































                                                                                                            
  
 
 
                                                                                                                                                               
                                                               



                                                                                                                                                      
 
                                                      
                                                                                                                        
 
                                                                                         
                                                                                                                                                   
                                    
                                                           
 
                                                     

                                                                                                                                                             
 
                                                              

                                                                                                                               
 
                                                                                                                       

                                                                                                                                    
 
                                                               

                                                                                                                                              
                                                      



                                                                                                                                                

       
                                                      
                                                               



                                                                                                                        
 
                                                      
                                                                                                                        
 
                                                                                         
                                                                                                                    
                                    
                                                           
 
                                                     

                                                                                                                            
 
                                                              

                                                                                                                               
 
                                                                                                                       

                                                                                                                                    
 
                                                               

                                                                                                                         
                                                      



                                                                                                                                                

       




                                                                                                                    

                                                                                                                        
                                                   
                                                               



                                                                                                                         
 
                                                      
                                                                                                                        
 
                                                                                         
                                                                                                                          
                                    
                                                           
 
                                                     

                                                                                                                              
 
                                                              

                                                                                                                               
 
                                                                                                                       

                                                                                                                                    
 
                                                               

                                                                                                                        
                                                      



                                                                                                                                                

       
                                                                                         
                                                               



                                                                                                                         
 
                                                      
                                                                                                                        
 
                                                                                         
                                                                                                                    
                                    
                                                           
 
                                                     

                                                                                                                            
 
                                                              

                                                                                                                               
 
                                                                                                                       

                                                                                                                                    
 
                                                               

                                                                                                                  
                                                      



                                                                                                                                                

       

                                                                                                                                                               
                                                               



                                                                                                                          
 
                                                      
                                                                                                                        
 
                                                                                         
                                                                                                                                                                                                                                                 
                                    
                                                           
 
                                                     

                                                                                                                                                                                                                                                           
 
                                                              

                                                                                                                               
 
                                                                                                                       

                                                                                                                                    
 
                                                               

                                                                                                                                                                                                      
                                                      







                                                                                                                                                
                                                               




                                                                                                                        
                                                      




                                                                                                                        
                                                           

                                                     

                                                                                                                              




                                                                                                                               
                                                                                                                       


                                                                                                                                    
                                                               

                                                                                                                         
                                                      



                                                                                                                                                

       
  

                               

                                                                                              
                  



                                                                                                 


                       
                                                               




                                                                                                                         
                                                      
                                                                                                                        

                                                                                         
                                                                                                                    
                                    
                                                           

                                                     

                                                                                                                              

                                                              

                                                                                                                               
 
                                                                                                                       

                                                                                                                                    
 
                                                               

                                                                                                                        
                                                      



                                                                                                                                                



       



































                                                                                 
                   
                                                               
                                                                                                                     
                                                                                                                     
                                                                                                                     

                                                                                                                     
                                                                      

                           
                                                                                         


                                                                                                               
                                                           
 
                                                     

                                                                                                                            
 


                                                                                                                   
 
                                                                                               

                                                                                                                           
 
                                                               
                                                                                                               
 
                                                                               
       
 
                                                         


















                                                                                                                


                                                        

                                                        






                                                        
                                                          
                     
                            



                     
                            







                                                        
                                                        
 

                                       



                                              
                                  
 
                                             

                                                      

                                                                                             
                                           

 
                                   
 
                                             

                                                      

                                                                                                
                                           

 
                                                         


               
                                 
 

                                                      
                        
                                  
 
 
                                                           

                           

                            





                             


  
                                   
 
                                               

                                                      

                                                                                             
                                            

 

                  
                                                                                                                                                                  





































                                                                                                                                                                                                                                                                                                    
// license:LGPL-2.1+
// copyright-holders:Olivier Galibert, Angelo Salese, David Haywood, Tomasz Slanina
/********************************************************************************************************

    Seibu Protected 1993-94 era hardware, V30 based (sequel to the SYS68C hardware)

    TODO:
    * zeroteam - sort-DMA doesn't seem to work too well, sprite-sprite priorities are broken as per now

    * xsedae - it does an "8-liner"-style scroll during attract, doesn't work too well.

    * sprite chip is the same as seibuspi.c and feversoc.c, needs device-ification and merging.

    * sprite chip also uses first entry for "something" that isn't sprite, some of them looks clipping
      regions (150 - ff in zeroteam, 150 - 0 and 150 - 80 in raiden2). Latter probably do double buffering
      on odd/even frames, by updating only top or bottom part of screen.

===========================================================================================================

raiden 2 board test note 17/04/08 (based on test by dox)

 rom banking is at 6c9, bit 0x80
  -- the game only writes this directly at startup, must be written indirectly by
     one of the protection commands? or mirrored?
  value of 0x80 puts 0x00000-0x1ffff at 0x20000 - 0x3ffff
  value of 0x00 puts 0x20000-0x3ffff at 0x20000 - 0x3ffff


===========================================================================================================

Raiden DX
Seibu Kaihatsu, 1994

This readme covers Raiden DX and to some extent Raiden II
which uses an almost identical PCB.

PCB Layout
----------

(C) 1993 RAIDEN II DX SEIBU KAIHATSU INC.,o
|----------------------------------------------------------|
|      1    2   3   4   5   6    7      8      9     10    |
|LA4460    M6295  PCM  Z80     6116                       A|
|   YM2151 M6295   6    5      6116    28.63636MHz        B|
|     VOL   YM3012                                         |
|HB-45A            |------|                               C|
|HB-2       4560   |SIE150| 6116      |---------|          |
|RC220             |      | 6116      | SEI252  |         D|
|                  |------| 6116      |SB05-106 |          |
|                           6116      |(QFP208) |         E|
|J                                    |         |         F|
|A    DSW2(8)                         |---------|          |
|M                                                        G|
|M    DSW1(8)                                   CXK58258   |
|A          |---------|OBJ-1    OBJ-2           CXK58258  H|
|           | SEI360  |                         CXK58258  J|
|           |SB06-1937|DX_OBJ-3 DX_OBJ-4        CXK58258  K|
|           |(QFP160) |  PAL1               |---------|   L|
|           |         |                     |SEI1000  |   M|
| |------|  |---------|  1H      3H         |SB01-001 |   N|
| |SEI200|         32MHz                    |(QFP184) |    |
| |      |CY7C185        2H      4H         |         |   P|
| |------|CY7C185                           |---------|    |
|                                                         Q|
|                        PAL2 PAL3             |----|     R|
|                                              |V30 |      |
| DX_BACK-1  DX_BACK-2   7   COPX-D2           |----|     S|
|----------------------------------------------------------|
Notes:
      V30 clock    - 16.000MHz [32/2]. Chip is stamped "NEC D70116HG-16 V30 NEC '84" (QFP52)
      Z80 clock    - 3.579545MHz [28.63636/8]. /NMI, /BUSREQ and /WAIT tied high/unused.
      YM2151 clock - 3.579545MHz [28.63636/8]
      M6295 clocks - 1.022MHz [28.63636/28] and pin 7 HIGH (both)
      CXK58258     - Sony CXK58258 32k x8 SRAM (= 62256)
      CY7C185      - Cypress CY7C185 8k x8 SRAM (= 6264)
      6116         - 2k x8 SRAM
      HB-45A       - Seibu custom ceramic module sound DAC (SIP20)
      HB-2         - Seibu custom ceramic module connected to coin counters (SIP10)
      RC220        - Custom resistor network module used for inputs (SIP14)
      VSync        - 55.4859Hz  \
      HSync        - 15.5586kHz / measured via EL4583
      PAL1         - AMI 18CV8 stamped 'JJ5004' (DIP20)
      PAL2         - AMI 18CV8 stamped 'JJ5002' (DIP20)
      PAL3         - AMI 18CV8 stamped 'JJ5001' (DIP20)
      ROMs         - *PCM      - 2M MaskROM stamped 'RAIDEN 2 PCM' at location U1018 (DIP32)
                     6         - 27C020 EPROM labelled 'SEIBU 6' at location U1017 (DIP32)
                     5         - 27C512 EPROM labelled 'SEIBU 5' at location U1110 (DIP28)
                     *OBJ-1    - 16M MaskROM stamped 'RAIDEN 2 OBJ-1' at location U0811 (DIP42)
                     *OBJ-2    - 16M MaskROM stamped 'RAIDEN 2 OBJ-2' at location U082 (DIP42)
                     DX_OBJ-3  - 16M MaskROM stamped 'DX OBJ-3' at location U0837 (DIP42)
                     DX_OBJ-4  - 16M MaskROM stamped 'DX OBJ-4' at location U0836 (DIP42)
                     1H        - 27C4001 EPROM labelled 'SEIBU 1H' at location U1210 (DIP32)
                     2H        - 27C4001 EPROM labelled 'SEIBU 2H' at location U1211 (DIP32)
                     3H        - 27C4001 EPROM labelled 'SEIBU 3H' at location U129 (DIP32)
                     4H        - 27C4001 EPROM labelled 'SEIBU 4H' at location U1212 (DIP32)
                     DX_BACK-1 - 16M MaskROM stamped 'DX BACK-1' at location U075 (DIP42)
                     DX_BACK-2 - 16M MaskROM stamped 'DX BACK-2' at location U0714 (DIP42)
                     7         - 27C210 EPROM labelled 'SEIBU 7' at location U0724 (DIP40)
                     *COPX-D2  - 2M MaskROM stamped 'COPX-D2' at location U0313 (DIP40)

                     * = these ROMs are soldered-in and match ROMs from the original Raiden II PCB

      SEIBU Custom ICs -
                        SIE150 (QFP100)
                        SEI252 SB05-106 (QFP208)
                        SEI0200 TC110G21AF 0076 (QFP100)
                        SEI360 SB06-1937 (QFP160)
                        SEI1000 SB01-001 (QFP184)


Games on this PCB / Similar PCBs
 Raiden 2
 Raiden DX
 Zero Team
 X Se Dae Quiz

 + variants

Some of these games were also released on updated PCBs
which usually featured vastly inferior sound hardware
 (see the V33 based version of Raiden II/DX New)

All games on this hardware have a startup routine that reads a few of the highest bytes of ROM
and stores their adjusted values in RAM. Which bytes are read differs from set to set, but one
is always FFFFB, which determines the region and/or licensee.

             Raiden II/DX       Zero Team
             -------------      ----------
    00/FF    Japan/Default      Japan/Default
    01       U.S. (Fabtek)      U.S. (Fabtek)
    02       Taiwan (Liang Hwa) Korea (Dream Soft)
    03       HK (Metrotainment) Taiwan (Liang Hwa)
    04       Korea?             "Selection"
    05       Germany (Tuning)
    06       Austria
    07       Belgium
    08       Denmark
    09       Finland
    0A       France
    0B       Great Britain
    0C       Greece
    0D       Holland
    0E       Italy
    0F       Norway
    10       Portugal
    11       Spain
    12       Sweden
    13       Switzerland
    14       Australia
    15       New Zealand

Most sets of Raiden II and Raiden DX display "USE IN JAPAN ONLY" when the region byte is set
to 00 or FF. This string is absent from the 'easier' sets of Raiden II, and Zero Team has
nothing of the sort. (The obviously Korean-only X Se Dae Quiz still reads the byte.)

Region 04 for Raiden II and Raiden DX is presumably Korea, but the notice that would confirm
this does not seem to be present in any of the sets.


Protection Notes:
 These games use the 2nd (and 3rd) generation of Seibu's 'COP' protection,
 utilizing the external 'COPX_D2' and 'COPX_D3' lookup roms (probably for
 math operations)  These chips, marked (c)1992 RISE Corp. are not thought
 to be the actual MCU which is probably internal to one of the Seibu
 customs.

 The games in legionna.c use (almost?) the same protection chips.

********************************************************************************************************/

#include "emu.h"
#include "includes/raiden2.h"

#include "cpu/nec/nec.h"
#include "cpu/z80/z80.h"
#include "machine/eepromser.h"
#include "sound/3812intf.h"
#include "sound/okim6295.h"
#include "sound/ym2151.h"
#include "machine/r2crypt.h"

#include "debugger.h"
#include "screen.h"
#include "speaker.h"


void raiden2_state::machine_start()
{
	save_item(NAME(bg_bank));
	save_item(NAME(fg_bank));
	save_item(NAME(mid_bank));
	save_item(NAME(tx_bank));
	save_item(NAME(raiden2_tilemap_enable));
	save_item(NAME(prg_bank));
	save_item(NAME(cop_bank));

	save_item(NAME(sprite_prot_x));
	save_item(NAME(sprite_prot_y));
	save_item(NAME(dst1));
	save_item(NAME(cop_spr_maxx));
	save_item(NAME(cop_spr_off));


	save_item(NAME(scrollvals));

	save_item(NAME(sprite_prot_src_addr));

}

/*
uint16_t raiden2_state::rps()
{
    return m_maincpu->state_int(NEC_CS);
}

uint16_t raiden2_state::rpc()
{
    return m_maincpu->state_int(NEC_IP);
}
*/

int cnt=0, ccol = -1;


WRITE16_MEMBER(raiden2_state::m_videoram_private_w)
{
	//  map(0x0d000, 0x0d7ff).ram().w(this, FUNC(raiden2_state::raiden2_background_w)).share("back_data");
	//  map(0x0d800, 0x0dfff).ram().w(this, FUNC(raiden2_state::raiden2_foreground_w).share("fore_data");
	//  map(0x0e000, 0x0e7ff).ram().w(this, FUNC(raiden2_state::raiden2_midground_w).share("mid_data");
	//  map(0x0e800, 0x0f7ff).ram().w(this, FUNC(raiden2_state::raiden2_text_w).share("text_data");

	if (offset < 0x800 / 2)
	{
		raiden2_background_w(space, offset, data, 0xffff);
	}
	else if (offset < 0x1000 /2)
	{
		offset -= 0x800 / 2;
		raiden2_foreground_w(space, offset, data, 0xffff);
	}
	else if (offset < 0x1800/2)
	{
		offset -= 0x1000 / 2;
		raiden2_midground_w(space, offset, data, 0xffff);
	}
	else if (offset < 0x2800/2)
	{
		offset -= 0x1800 / 2;
		raiden2_text_w(space, offset, data, 0xffff);
	}
}



void raiden2_state::combine32(uint32_t *val, int offset, uint16_t data, uint16_t mem_mask)
{
	uint16_t *dest = (uint16_t *)val + BYTE_XOR_LE(offset);
	COMBINE_DATA(dest);
}



/* SPRITE DRAWING (move to video file) */

void raiden2_state::draw_sprites(const rectangle &cliprect)
{
	uint16_t *source = sprites + (0x1000/2)-4;
	sprite_buffer.fill(0xf, cliprect);

	gfx_element *gfx = m_gfxdecode->gfx(2);

	/*
	  00 fhhh Fwww ppcc cccc   h = height f=flipy w = width F = flipx p = priority c = color
	  02 nnnn nnnn nnnn nnnn   n = tileno
	  04 xxxx xxxx xxxx xxxx   x = xpos
	  06 yyyy yyyy yyyy yyyy   y = ypos
	 */

	while( source >= sprites ){
		int tile_number = source[1];
		int sx = source[2];
		int sy = source[3];
		int colr;
		int xtiles, ytiles;
		int ytlim, xtlim;
		int xflip, yflip;
		int xstep, ystep;
		int pri;

		ytlim = (source[0] >> 12) & 0x7;
		xtlim = (source[0] >> 8 ) & 0x7;

		xflip = (source[0] >> 15) & 0x1;
		yflip = (source[0] >> 11) & 0x1;

		colr = source[0] & 0x3f;

		pri = (source[0] >> 6) & 3;

		colr |= pri << (14-4);

		ytlim += 1;
		xtlim += 1;

		xstep = 16;
		ystep = 16;

		if (xflip)
		{
			ystep = -16;
			sy += ytlim*16-16;
		}

		if (yflip)
		{
			xstep = -16;
			sx += xtlim*16-16;
		}

		for (xtiles = 0; xtiles < xtlim; xtiles++)
		{
			for (ytiles = 0; ytiles < ytlim; ytiles++)
			{
				/* note this wraparound handling could be wrong if some of the COP maths is wrong */

#define ZEROTEAM_MASK_X (0x1ff) // causes a blank square in the corner of zero team, but otherwise the thrusters of the ship in the r2 intro are clipped, using 0x8000 as a sign bit instead of this logic works for r2, but not zero team
#define ZEROTEAM_MASK_Y (0x1ff)



						gfx->transpen(
						sprite_buffer,
						cliprect,
						tile_number,
						colr,
						yflip,xflip,
						(sx+xstep*xtiles)&ZEROTEAM_MASK_X,(sy+ystep*ytiles)&ZEROTEAM_MASK_Y, 15);


						gfx->transpen(
						sprite_buffer,
						cliprect,
						tile_number,
						colr,
						yflip,xflip,
						((sx+xstep*xtiles)&ZEROTEAM_MASK_X)-0x200,(sy+ystep*ytiles)&ZEROTEAM_MASK_Y, 15);


						gfx->transpen(
						sprite_buffer,
						cliprect,
						tile_number,
						colr,
						yflip,xflip,
						(sx+xstep*xtiles)&ZEROTEAM_MASK_X,((sy+ystep*ytiles)&ZEROTEAM_MASK_Y)-0x200, 15);


						gfx->transpen(
						sprite_buffer,
						cliprect,
						tile_number,
						colr,
						yflip,xflip,
						((sx+xstep*xtiles)&ZEROTEAM_MASK_X)-0x200,((sy+ystep*ytiles)&ZEROTEAM_MASK_Y)-0x200, 15);


				tile_number++;
			}
		}

		source -= 4;
	}

}

/* VIDEO RELATED WRITE HANDLERS (move to video file) */

WRITE16_MEMBER(raiden2_state::raiden2_background_w)
{
	COMBINE_DATA(&back_data[offset]);
	background_layer->mark_tile_dirty(offset);
}

WRITE16_MEMBER(raiden2_state::raiden2_midground_w)
{
	COMBINE_DATA(&mid_data[offset]);
	midground_layer->mark_tile_dirty(offset);
}

WRITE16_MEMBER(raiden2_state::raiden2_foreground_w)
{
	COMBINE_DATA(&fore_data[offset]);
	foreground_layer->mark_tile_dirty(offset);
}

WRITE16_MEMBER(raiden2_state::raiden2_text_w)
{
	COMBINE_DATA(&text_data[offset]);
	text_layer->mark_tile_dirty(offset);
}

WRITE16_MEMBER(raiden2_state::tilemap_enable_w)
{
	COMBINE_DATA(&raiden2_tilemap_enable);
}

WRITE16_MEMBER(raiden2_state::tile_scroll_w)
{
	tilemap_t *tm = nullptr;
	switch(offset/2) {
	case 0: tm = background_layer; break;
	case 1: tm = midground_layer; break;
	case 2: tm = foreground_layer; break;
	default: assert(0); break;
	}

	COMBINE_DATA(scrollvals + offset);
	data = scrollvals[offset];

	if(offset & 1)
		tm->set_scrolly(0, data);
	else
		tm->set_scrollx(0, data);
}

WRITE16_MEMBER(raiden2_state::tile_bank_01_w)
{
	if(ACCESSING_BITS_0_7) {
		int new_bank;
		new_bank = 0 | ((data & 1)<<1);
		if(new_bank != bg_bank) {
			bg_bank = new_bank;
			background_layer->mark_all_dirty();
		}

		new_bank = 1 | (data & 2);
		if(new_bank != mid_bank) {
			mid_bank = new_bank;
			midground_layer->mark_all_dirty();
		}
	}
}

READ16_MEMBER(raiden2_state::cop_tile_bank_2_r)
{
	return cop_bank;
}

WRITE16_MEMBER(raiden2_state::cop_tile_bank_2_w)
{
	COMBINE_DATA(&cop_bank);

	if(ACCESSING_BITS_8_15) {
		int new_bank = 4 | (data >> 14);
		if(new_bank != fg_bank) {
			fg_bank = new_bank;
			foreground_layer->mark_all_dirty();
		}
	}
}

WRITE16_MEMBER(raiden2_state::raidendx_cop_bank_2_w)
{
	COMBINE_DATA(&cop_bank);

	int new_bank = 4 | ((cop_bank >> 4) & 3);
	if(new_bank != fg_bank) {
		fg_bank = new_bank;
		foreground_layer->mark_all_dirty();
	}

	/* mainbank2 coming from 6c9 ? */
	int bb = cop_bank >> 12;
	membank("mainbank1")->set_entry(bb + 16);
	membank("mainbank2")->set_entry(3);
}



/* TILEMAP RELATED (move to video file) */

TILE_GET_INFO_MEMBER(raiden2_state::get_back_tile_info)
{
	int tile = back_data[tile_index];
	int color = (tile >> 12) | (0 << 4);

	tile = (tile & 0xfff) | (bg_bank << 12);

	SET_TILE_INFO_MEMBER(1,tile+0x0000,color,0);
}

TILE_GET_INFO_MEMBER(raiden2_state::get_mid_tile_info)
{
	int tile = mid_data[tile_index];
	int color = (tile >> 12) | (2 << 4);

	tile = (tile & 0xfff) | (mid_bank << 12);

	SET_TILE_INFO_MEMBER(1,tile,color,0);
}

TILE_GET_INFO_MEMBER(raiden2_state::get_fore_tile_info)
{
	int tile = fore_data[tile_index];
	int color = (tile >> 12) | (1 << 4);

	tile = (tile & 0xfff) | (fg_bank << 12);

	SET_TILE_INFO_MEMBER(1,tile,color,0);
}

TILE_GET_INFO_MEMBER(raiden2_state::get_text_tile_info)
{
	int tile = text_data[tile_index];
	int color = (tile>>12)&0xf;

	tile &= 0xfff;

	SET_TILE_INFO_MEMBER(0,tile + tx_bank * 0x1000,color,0);
}

/* VIDEO START (move to video file) */


VIDEO_START_MEMBER(raiden2_state,raiden2)
{
	back_data = make_unique_clear<uint16_t[]>(0x800/2);
	fore_data =  make_unique_clear<uint16_t[]>(0x800/2);
	mid_data =  make_unique_clear<uint16_t[]>(0x800/2);
	text_data =  make_unique_clear<uint16_t[]>(0x1000/2);

	save_pointer(NAME(back_data.get()), 0x800/2);
	save_pointer(NAME(fore_data.get()), 0x800/2);
	save_pointer(NAME(mid_data.get()), 0x800/2);
	save_pointer(NAME(text_data.get()), 0x1000/2);

	text_layer       = &machine().tilemap().create(*m_gfxdecode, tilemap_get_info_delegate(FUNC(raiden2_state::get_text_tile_info),this), TILEMAP_SCAN_ROWS,  8, 8, 64,32 );
	background_layer = &machine().tilemap().create(*m_gfxdecode, tilemap_get_info_delegate(FUNC(raiden2_state::get_back_tile_info),this), TILEMAP_SCAN_ROWS, 16,16, 32,32 );
	midground_layer  = &machine().tilemap().create(*m_gfxdecode, tilemap_get_info_delegate(FUNC(raiden2_state::get_mid_tile_info),this),  TILEMAP_SCAN_ROWS, 16,16, 32,32 );
	foreground_layer = &machine().tilemap().create(*m_gfxdecode, tilemap_get_info_delegate(FUNC(raiden2_state::get_fore_tile_info),this), TILEMAP_SCAN_ROWS, 16,16, 32,32 );
}

/* screen_update_raiden2 (move to video file) */

void raiden2_state::blend_layer(bitmap_rgb32 &bitmap, const rectangle &cliprect, bitmap_ind16 &source, int layer)
{
	if(layer == -1)
		return;

	const pen_t *pens = &m_palette->pen(0);
	layer <<= 14;
	for(int y = cliprect.min_y; y <= cliprect.max_y; y++) {
		const uint16_t *src = &source.pix16(y, cliprect.min_x);
		uint32_t *dst = &bitmap.pix32(y, cliprect.min_x);
		for(int x = cliprect.min_x; x <= cliprect.max_x; x++) {
			uint16_t val = *src++;
			if((val & 0xc000) == layer && (val & 0x000f) != 0x000f) {
				val &= 0x07ff;

				if(blend_active[val])
					*dst = alpha_blend_r32(*dst, pens[val], 0x7f);
				else
					*dst = pens[val];
			}
			dst++;
		}
	}
}

void raiden2_state::tilemap_draw_and_blend(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect, tilemap_t *tilemap)
{
	tilemap->draw(screen, tile_buffer, cliprect, 0, 0);
	blend_layer(bitmap, cliprect, tile_buffer, 0);
}

uint32_t raiden2_state::screen_update_raiden2(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
{
	bitmap.fill(m_palette->black_pen(), cliprect);
	if (!(raiden2_tilemap_enable & 16)) {
		draw_sprites(cliprect);

		blend_layer(bitmap, cliprect, sprite_buffer, cur_spri[0]);
	}

	if (!(raiden2_tilemap_enable & 1))
		tilemap_draw_and_blend(screen, bitmap, cliprect, background_layer);

	if (!(raiden2_tilemap_enable & 16))
		blend_layer(bitmap, cliprect, sprite_buffer, cur_spri[1]);

	if (!(raiden2_tilemap_enable & 2))
		tilemap_draw_and_blend(screen, bitmap, cliprect, midground_layer);

	if (!(raiden2_tilemap_enable & 16))
		blend_layer(bitmap, cliprect, sprite_buffer, cur_spri[2]);

	if (!(raiden2_tilemap_enable & 4))
		tilemap_draw_and_blend(screen, bitmap, cliprect, foreground_layer);

	if (!(raiden2_tilemap_enable & 16))
		blend_layer(bitmap, cliprect, sprite_buffer, cur_spri[3]);

	if (!(raiden2_tilemap_enable & 8))
		tilemap_draw_and_blend(screen, bitmap, cliprect, text_layer);

	if (!(raiden2_tilemap_enable & 16))
		blend_layer(bitmap, cliprect, sprite_buffer, cur_spri[4]);

	if (machine().input().code_pressed_once(KEYCODE_Z))
		if (m_raiden2cop) m_raiden2cop->dump_table();

	return 0;
}




/*************************************
 *
 *  Interrupts
 *
 *************************************/

INTERRUPT_GEN_MEMBER(raiden2_state::raiden2_interrupt)
{
	device.execute().set_input_line_and_vector(0, HOLD_LINE, 0xc0/4);   /* VBL */
}




// Sprite encryption key upload

static uint32_t sprcpt_adr, sprcpt_idx;

static uint16_t sprcpt_flags2;
static uint32_t sprcpt_val[2], sprcpt_flags1;
static uint32_t sprcpt_data_1[0x100], sprcpt_data_2[0x40], sprcpt_data_3[6], sprcpt_data_4[4];

void raiden2_state::sprcpt_init(void)
{
	memset(sprcpt_data_1, 0, sizeof(sprcpt_data_1));
	memset(sprcpt_data_2, 0, sizeof(sprcpt_data_2));
	memset(sprcpt_data_3, 0, sizeof(sprcpt_data_3));
	memset(sprcpt_data_4, 0, sizeof(sprcpt_data_4));

	sprcpt_adr = 0;
	sprcpt_idx = 0;
}


WRITE16_MEMBER(raiden2_state::sprcpt_adr_w)
{
	combine32(&sprcpt_adr, offset, data, mem_mask);
}

WRITE16_MEMBER(raiden2_state::sprcpt_data_1_w)
{
	combine32(sprcpt_data_1+sprcpt_adr, offset, data, mem_mask);
}

WRITE16_MEMBER(raiden2_state::sprcpt_data_2_w)
{
	combine32(sprcpt_data_2+sprcpt_adr, offset, data, mem_mask);
}

WRITE16_MEMBER(raiden2_state::sprcpt_data_3_w)
{
	combine32(sprcpt_data_3+sprcpt_idx, offset, data, mem_mask);
	if(offset == 1) {
		sprcpt_idx ++;
		if(sprcpt_idx == 6)
			sprcpt_idx = 0;
	}
}

WRITE16_MEMBER(raiden2_state::sprcpt_data_4_w)
{
	combine32(sprcpt_data_4+sprcpt_idx, offset, data, mem_mask);
	if(offset == 1) {
		sprcpt_idx ++;
		if(sprcpt_idx == 4)
			sprcpt_idx = 0;
	}
}

WRITE16_MEMBER(raiden2_state::sprcpt_val_1_w)
{
	combine32(sprcpt_val+0, offset, data, mem_mask);
}

WRITE16_MEMBER(raiden2_state::sprcpt_val_2_w)
{
	combine32(sprcpt_val+1, offset, data, mem_mask);
}

WRITE16_MEMBER(raiden2_state::sprcpt_flags_1_w)
{
	combine32(&sprcpt_flags1, offset, data, mem_mask);
	if(offset == 1) {
		// bit 31: 1 = allow write on sprcpt data

		if(!(sprcpt_flags1 & 0x80000000U)) {
			// Upload finished
			if(1) {
				int i;
				logerror("sprcpt_val 1: %08x\n", sprcpt_val[0]);
				logerror("sprcpt_val 2: %08x\n", sprcpt_val[1]);
				logerror("sprcpt_data 1:\n");
				for(i=0; i<0x100; i++) {
					logerror(" %08x", sprcpt_data_1[i]);
					if(!((i+1) & 7))
						logerror("\n");
				}
				logerror("sprcpt_data 2:\n");
				for(i=0; i<0x40; i++) {
					logerror(" %08x", sprcpt_data_2[i]);
					if(!((i+1) & 7))
						logerror("\n");
				}
			}
		}
	}
}

WRITE16_MEMBER(raiden2_state::sprcpt_flags_2_w)
{
	COMBINE_DATA(&sprcpt_flags2);
	if(offset == 0) {
		if(sprcpt_flags2 & 0x8000) {
			// Reset decryption -> redo it
		}
	}
}



void raiden2_state::common_reset()
{
	bg_bank=0;
	fg_bank=6;
	mid_bank=1;
	tx_bank = 0;
}

MACHINE_RESET_MEMBER(raiden2_state,raiden2)
{
	common_reset();
	sprcpt_init();

	membank("mainbank1")->set_entry(2);
	membank("mainbank2")->set_entry(3);

	prg_bank = 0;
	//cop_init();
}

MACHINE_RESET_MEMBER(raiden2_state,raidendx)
{
	common_reset();
	sprcpt_init();

	membank("mainbank1")->set_entry(16);
	membank("mainbank2")->set_entry(3);

	prg_bank = 0x08;

	//cop_init();
}

MACHINE_RESET_MEMBER(raiden2_state,zeroteam)
{
	bg_bank = 0;
	fg_bank = 2;
	mid_bank = 1;
	tx_bank = 0;
	sprcpt_init();

	membank("mainbank1")->set_entry(2);
	membank("mainbank2")->set_entry(3);

	prg_bank = 0;
	//cop_init();
}

MACHINE_RESET_MEMBER(raiden2_state,xsedae)
{
	bg_bank = 0;
	fg_bank = 2;
	mid_bank = 1;
	tx_bank = 0;
	sprcpt_init();
}

WRITE16_MEMBER(raiden2_state::raiden2_bank_w)
{
	if(ACCESSING_BITS_8_15) {
		int bb = (~data >> 15) & 1;
		logerror("select bank %d %04x\n", (data >> 15) & 1, data);
		membank("mainbank1")->set_entry(bb*2);
		membank("mainbank2")->set_entry(bb*2+1);
		prg_bank = ((data >> 15) & 1);
	}
}


WRITE16_MEMBER(raiden2_state::sprite_prot_x_w)
{
	sprite_prot_x = data;
	//popmessage("%04x %04x",sprite_prot_x,sprite_prot_y);
}

WRITE16_MEMBER(raiden2_state::sprite_prot_y_w)
{
	sprite_prot_y = data;
	//popmessage("%04x %04x",sprite_prot_x,sprite_prot_y);
}

WRITE16_MEMBER(raiden2_state::sprite_prot_src_seg_w)
{
	sprite_prot_src_addr[0] = data;
}

READ16_MEMBER(raiden2_state::sprite_prot_src_seg_r)
{
	return sprite_prot_src_addr[0];
}

WRITE16_MEMBER(raiden2_state::sprite_prot_src_w)
{
	sprite_prot_src_addr[1] = data;
	uint32_t src = (sprite_prot_src_addr[0]<<4)+sprite_prot_src_addr[1];

	int x = int16_t((space.read_dword(src+0x08) >> 16) - (sprite_prot_x));
	int y = int16_t((space.read_dword(src+0x04) >> 16) - (sprite_prot_y));

	uint16_t head1 = space.read_word(src+cop_spr_off);
	uint16_t head2 = space.read_word(src+cop_spr_off+2);

	int w = (((head1 >> 8 ) & 7) + 1) << 4;
	int h = (((head1 >> 12) & 7) + 1) << 4;

	uint16_t flag = x-w/2 > -w && x-w/2 < cop_spr_maxx+w && y-h/2 > -h && y-h/2 < 256+h ? 1 : 0;

	flag = (space.read_word(src) & 0xfffe) | flag;
	space.write_word(src, flag);

	if(flag & 1)
	{
		space.write_word(dst1,   head1);
		space.write_word(dst1+2, head2);
		space.write_word(dst1+4, x-w/2);
		space.write_word(dst1+6, y-h/2);

		dst1 += 8;
	}
	//printf("[%08x] %08x %08x %04x %04x\n",src,dx,dy,dst1,dst2);
	//machine().debug_break();
}

READ16_MEMBER(raiden2_state::sprite_prot_dst1_r)
{
	return dst1;
}

READ16_MEMBER(raiden2_state::sprite_prot_maxx_r)
{
	return cop_spr_maxx;
}

READ16_MEMBER(raiden2_state::sprite_prot_off_r)
{
	return cop_spr_off;
}

WRITE16_MEMBER(raiden2_state::sprite_prot_dst1_w)
{
	dst1 = data;
}

WRITE16_MEMBER(raiden2_state::sprite_prot_maxx_w)
{
	cop_spr_maxx = data;
}

WRITE16_MEMBER(raiden2_state::sprite_prot_off_w)
{
	cop_spr_off = data;
}

/* MEMORY MAPS */
void raiden2_state::raiden2_cop_mem(address_map &map)
{
	map(0x0041c, 0x0041d).w(m_raiden2cop, FUNC(raiden2cop_device::cop_angle_target_w)); // angle target (for 0x6200 COP macro)
	map(0x0041e, 0x0041f).w(m_raiden2cop, FUNC(raiden2cop_device::cop_angle_step_w));   // angle step   (for 0x6200 COP macro)
	map(0x00420, 0x00421).w(m_raiden2cop, FUNC(raiden2cop_device::cop_itoa_low_w));
	map(0x00422, 0x00423).w(m_raiden2cop, FUNC(raiden2cop_device::cop_itoa_high_w));
	map(0x00424, 0x00425).w(m_raiden2cop, FUNC(raiden2cop_device::cop_itoa_mode_w));
	map(0x00428, 0x00429).w(m_raiden2cop, FUNC(raiden2cop_device::cop_dma_v1_w));
	map(0x0042a, 0x0042b).w(m_raiden2cop, FUNC(raiden2cop_device::cop_dma_v2_w));
	map(0x0042c, 0x0042d).w(m_raiden2cop, FUNC(raiden2cop_device::cop_prng_maxvalue_w));
	map(0x00432, 0x00433).w(m_raiden2cop, FUNC(raiden2cop_device::cop_pgm_data_w));
	map(0x00434, 0x00435).w(m_raiden2cop, FUNC(raiden2cop_device::cop_pgm_addr_w));
	map(0x00436, 0x00437).w(m_raiden2cop, FUNC(raiden2cop_device::cop_hitbox_baseadr_w));
	map(0x00438, 0x00439).w(m_raiden2cop, FUNC(raiden2cop_device::cop_pgm_value_w));
	map(0x0043a, 0x0043b).w(m_raiden2cop, FUNC(raiden2cop_device::cop_pgm_mask_w));
	map(0x0043c, 0x0043d).w(m_raiden2cop, FUNC(raiden2cop_device::cop_pgm_trigger_w));
	map(0x00444, 0x00445).w(m_raiden2cop, FUNC(raiden2cop_device::cop_scale_w));
	map(0x00450, 0x00451).w(m_raiden2cop, FUNC(raiden2cop_device::cop_sort_ram_addr_hi_w));
	map(0x00452, 0x00453).w(m_raiden2cop, FUNC(raiden2cop_device::cop_sort_ram_addr_lo_w));
	map(0x00454, 0x00455).w(m_raiden2cop, FUNC(raiden2cop_device::cop_sort_lookup_hi_w));
	map(0x00456, 0x00457).w(m_raiden2cop, FUNC(raiden2cop_device::cop_sort_lookup_lo_w));
	map(0x00458, 0x00459).w(m_raiden2cop, FUNC(raiden2cop_device::cop_sort_param_w));
	map(0x0045a, 0x0045b).w(m_raiden2cop, FUNC(raiden2cop_device::cop_pal_brightness_val_w)); //palette DMA brightness val, used by X Se Dae / Zero Team
	map(0x0045c, 0x0045d).w(m_raiden2cop, FUNC(raiden2cop_device::cop_pal_brightness_mode_w));  //palette DMA brightness mode, used by X Se Dae / Zero Team (sets to 5)
	map(0x00470, 0x00471).rw(this, FUNC(raiden2_state::cop_tile_bank_2_r), FUNC(raiden2_state::cop_tile_bank_2_w)); // implementaton of this varies between games, external hookup?

	map(0x00476, 0x00477).w(m_raiden2cop, FUNC(raiden2cop_device::cop_dma_adr_rel_w));
	map(0x00478, 0x00479).w(m_raiden2cop, FUNC(raiden2cop_device::cop_dma_src_w));
	map(0x0047a, 0x0047b).w(m_raiden2cop, FUNC(raiden2cop_device::cop_dma_size_w));
	map(0x0047c, 0x0047d).w(m_raiden2cop, FUNC(raiden2cop_device::cop_dma_dst_w));
	map(0x0047e, 0x0047f).rw(m_raiden2cop, FUNC(raiden2cop_device::cop_dma_mode_r), FUNC(raiden2cop_device::cop_dma_mode_w));
	map(0x004a0, 0x004ad).rw(m_raiden2cop, FUNC(raiden2cop_device::cop_reg_high_r), FUNC(raiden2cop_device::cop_reg_high_w));
	map(0x004c0, 0x004cd).rw(m_raiden2cop, FUNC(raiden2cop_device::cop_reg_low_r), FUNC(raiden2cop_device::cop_reg_low_w));
	map(0x00500, 0x00505).w(m_raiden2cop, FUNC(raiden2cop_device::cop_cmd_w));
	map(0x00580, 0x00581).r(m_raiden2cop, FUNC(raiden2cop_device::cop_collision_status_r));
	map(0x00582, 0x00587).r(m_raiden2cop, FUNC(raiden2cop_device::cop_collision_status_val_r));
	map(0x00588, 0x00589).r(m_raiden2cop, FUNC(raiden2cop_device::cop_collision_status_stat_r));
	map(0x00590, 0x00599).r(m_raiden2cop, FUNC(raiden2cop_device::cop_itoa_digits_r));
	map(0x005a0, 0x005a7).r(m_raiden2cop, FUNC(raiden2cop_device::cop_prng_r)); // zeroteam reads from 5a4
	map(0x005b0, 0x005b1).r(m_raiden2cop, FUNC(raiden2cop_device::cop_status_r));
	map(0x005b2, 0x005b3).r(m_raiden2cop, FUNC(raiden2cop_device::cop_dist_r));
	map(0x005b4, 0x005b5).r(m_raiden2cop, FUNC(raiden2cop_device::cop_angle_r));

	map(0x00600, 0x0063f).rw("crtc", FUNC(seibu_crtc_device::read), FUNC(seibu_crtc_device::write));
	//map(0x00640, 0x006bf).rw("obj", FUNC(seibu_encrypted_sprite_device::read), FUNC(seibu_encrypted_sprite_device::write));
	map(0x006a0, 0x006a3).w(this, FUNC(raiden2_state::sprcpt_val_1_w));
	map(0x006a4, 0x006a7).w(this, FUNC(raiden2_state::sprcpt_data_3_w));
	map(0x006a8, 0x006ab).w(this, FUNC(raiden2_state::sprcpt_data_4_w));
	map(0x006ac, 0x006af).w(this, FUNC(raiden2_state::sprcpt_flags_1_w));
	map(0x006b0, 0x006b3).w(this, FUNC(raiden2_state::sprcpt_data_1_w));
	map(0x006b4, 0x006b7).w(this, FUNC(raiden2_state::sprcpt_data_2_w));
	map(0x006b8, 0x006bb).w(this, FUNC(raiden2_state::sprcpt_val_2_w));
	map(0x006bc, 0x006bf).w(this, FUNC(raiden2_state::sprcpt_adr_w));
	map(0x006c0, 0x006c1).rw(this, FUNC(raiden2_state::sprite_prot_off_r), FUNC(raiden2_state::sprite_prot_off_w));
	map(0x006c2, 0x006c3).rw(this, FUNC(raiden2_state::sprite_prot_src_seg_r), FUNC(raiden2_state::sprite_prot_src_seg_w));
	map(0x006c4, 0x006c5).nopw(); // constant value written along with 0x6c0
	map(0x006c6, 0x006c7).w(this, FUNC(raiden2_state::sprite_prot_dst1_w));
	map(0x006ca, 0x006cb).w(this, FUNC(raiden2_state::raiden2_bank_w));
	map(0x006cc, 0x006cd).w(this, FUNC(raiden2_state::tile_bank_01_w));
	map(0x006ce, 0x006cf).w(this, FUNC(raiden2_state::sprcpt_flags_2_w));
	map(0x006d8, 0x006d9).w(this, FUNC(raiden2_state::sprite_prot_x_w));
	map(0x006da, 0x006db).w(this, FUNC(raiden2_state::sprite_prot_y_w));
	map(0x006dc, 0x006dd).rw(this, FUNC(raiden2_state::sprite_prot_maxx_r), FUNC(raiden2_state::sprite_prot_maxx_w));
	map(0x006de, 0x006df).w(this, FUNC(raiden2_state::sprite_prot_src_w));
	/* end video block */

	map(0x006fc, 0x006fd).w(m_raiden2cop, FUNC(raiden2cop_device::cop_dma_trigger_w));
	map(0x006fe, 0x006ff).w(m_raiden2cop, FUNC(raiden2cop_device::cop_sort_dma_trig_w)); // sort-DMA trigger

	map(0x00762, 0x00763).r(this, FUNC(raiden2_state::sprite_prot_dst1_r));
}

void raiden2_state::raiden2_mem(address_map &map)
{
	map(0x00000, 0x003ff).ram();

	raiden2_cop_mem(map);

	map(0x0068e, 0x0068f).nopw(); //irq ack / sprite buffering?

	map(0x00700, 0x0071f).lrw8("seibu_sound_rw",
							   [this](address_space &space, offs_t offset, u8 mem_mask) {
								   return m_seibu_sound->main_r(space, offset >> 1, mem_mask);
							   },
							   [this](address_space &space, offs_t offset, u8 data, u8 mem_mask) {
								   m_seibu_sound->main_w(space, offset >> 1, data, mem_mask);
							   }).umask16(0x00ff);

	map(0x00740, 0x00741).portr("DSW");
	map(0x00744, 0x00745).portr("P1_P2");
	map(0x0074c, 0x0074d).portr("SYSTEM");

	map(0x00800, 0x0bfff).ram();

	map(0x0c000, 0x0cfff).ram().share("sprites");
	map(0x0d000, 0x0d7ff).ram(); // .w(this, FUNC(raiden2_state::raiden2_background_w)).share("back_data");
	map(0x0d800, 0x0dfff).ram(); // .w(this, FUNC(raiden2_state::raiden2_foreground_w).share("fore_data");
	map(0x0e000, 0x0e7ff).ram(); // .w(this, FUNC(raiden2_state::raiden2_midground_w).share("mid_data");
	map(0x0e800, 0x0f7ff).ram(); // .w(this, FUNC(raiden2_state::raiden2_text_w).share("text_data");
	map(0x0f800, 0x0ffff).ram(); /* Stack area */

	map(0x10000, 0x1efff).ram();
	map(0x1f000, 0x1ffff).ram(); //.w("palette", palette_device, write).share("palette");

	map(0x20000, 0x2ffff).bankr("mainbank1");
	map(0x30000, 0x3ffff).bankr("mainbank2");
	map(0x40000, 0xfffff).rom().region("maincpu", 0x40000);
}

void raiden2_state::raidendx_mem(address_map &map)
{
	raiden2_mem(map);
	map(0x00470, 0x00471).rw(this, FUNC(raiden2_state::cop_tile_bank_2_r), FUNC(raiden2_state::raidendx_cop_bank_2_w));
	map(0x004d0, 0x004d7).ram(); //???
	map(0x00600, 0x0063f).rw("crtc", FUNC(seibu_crtc_device::read_alt), FUNC(seibu_crtc_device::write_alt));
//  map(0x006ca, 0x006cb).nopw();
}

void raiden2_state::zeroteam_mem(address_map &map)
{
	map(0x00000, 0x003ff).ram();

	raiden2_cop_mem(map);

	map(0x00470, 0x00471).nopw();
	map(0x006cc, 0x006cd).nopw();

	map(0x0068e, 0x0068f).nopw(); // irq ack / sprite buffering?

	map(0x00700, 0x0071f).lrw8("seibu_sound_rw",
							   [this](address_space &space, offs_t offset, u8 mem_mask) {
								   return m_seibu_sound->main_r(space, offset >> 1, mem_mask);
							   },
							   [this](address_space &space, offs_t offset, u8 data, u8 mem_mask) {
								   m_seibu_sound->main_w(space, offset >> 1, data, mem_mask);
							   }).umask16(0x00ff);

	map(0x00740, 0x00741).portr("DSW");
	map(0x00744, 0x00745).portr("P1_P2");
	map(0x00748, 0x00749).portr("P3_P4");
	map(0x0074c, 0x0074d).portr("SYSTEM");

	map(0x00800, 0x0b7ff).ram();
	map(0x0b800, 0x0bfff).ram(); // .w(this, FUNC(raiden2_state::raiden2_background_w)).share("back_data");
	map(0x0c000, 0x0c7ff).ram(); // .w(this, FUNC(raiden2_state::raiden2_foreground_w).share("fore_data");
	map(0x0c800, 0x0cfff).ram(); // .w(this, FUNC(raiden2_state::raiden2_midground_w).share("mid_data");
	map(0x0d000, 0x0dfff).ram(); // .w(this, FUNC(raiden2_state::raiden2_text_w).share("text_data");
	map(0x0e000, 0x0efff).ram(); // .w("palette", palette_device, write).share("palette");
	map(0x0f000, 0x0ffff).ram().share("sprites");
	map(0x10000, 0x1ffff).ram();

	map(0x20000, 0x2ffff).bankr("mainbank1");
	map(0x30000, 0x3ffff).bankr("mainbank2");
	map(0x40000, 0xfffff).rom().region("maincpu", 0x40000);
}

void raiden2_state::xsedae_mem(address_map &map)
{
	map(0x00000, 0x003ff).ram();

	raiden2_cop_mem(map);

	map(0x00470, 0x00471).nopw();
	map(0x006cc, 0x006cd).nopw();

	map(0x0068e, 0x0068f).nopw(); //irq ack / sprite buffering?

	map(0x00700, 0x0071f).lrw8("seibu_sound_rw",
							   [this](address_space &space, offs_t offset, u8 mem_mask) {
								   return m_seibu_sound->main_r(space, offset >> 1, mem_mask);
							   },
							   [this](address_space &space, offs_t offset, u8 data, u8 mem_mask) {
								   m_seibu_sound->main_w(space, offset >> 1, data, mem_mask);
							   }).umask16(0x00ff);

	map(0x00740, 0x00741).portr("DSW");
	map(0x00744, 0x00745).portr("P1_P2");
	map(0x00748, 0x00749).portr("P3_P4");
	map(0x0074c, 0x0074d).portr("SYSTEM");

	map(0x00800, 0x0b7ff).ram();
	map(0x0b800, 0x0bfff).ram(); // .w(this, FUNC(raiden2_state::raiden2_background_w)).share("back_data");
	map(0x0c000, 0x0c7ff).ram(); // .w(this, FUNC(raiden2_state::raiden2_foreground_w).share("fore_data");
	map(0x0c800, 0x0cfff).ram(); // .w(this, FUNC(raiden2_state::raiden2_midground_w).share("mid_data");
	map(0x0d000, 0x0dfff).ram(); // .w(this, FUNC(raiden2_state::raiden2_text_w).share("text_data");
	map(0x0e000, 0x0efff).ram(); // .w("palette", palette_device, write).share("palette");
	map(0x0f000, 0x0ffff).ram().share("sprites");

	map(0x10000, 0x1ffff).ram();

	map(0x20000, 0xfffff).rom().region("maincpu", 0x20000);
}

void raiden2_state::raiden2_sound_map(address_map &map)
{
	map(0x0000, 0x1fff).rom();
	map(0x2000, 0x27ff).ram();
	map(0x4000, 0x4000).w(m_seibu_sound, FUNC(seibu_sound_device::pending_w));
	map(0x4001, 0x4001).w(m_seibu_sound, FUNC(seibu_sound_device::irq_clear_w));
	map(0x4002, 0x4002).w(m_seibu_sound, FUNC(seibu_sound_device::rst10_ack_w));
	map(0x4003, 0x4003).w(m_seibu_sound, FUNC(seibu_sound_device::rst18_ack_w));
	map(0x4004, 0x4004).noprw();
	map(0x4008, 0x4009).rw(m_seibu_sound, FUNC(seibu_sound_device::ym_r), FUNC(seibu_sound_device::ym_w));
	map(0x4010, 0x4011).r(m_seibu_sound, FUNC(seibu_sound_device::soundlatch_r));
	map(0x4012, 0x4012).r(m_seibu_sound, FUNC(seibu_sound_device::main_data_pending_r));
	map(0x4013, 0x4013).portr("COIN");
	map(0x4018, 0x4019).w(m_seibu_sound, FUNC(seibu_sound_device::main_data_w));
	map(0x401a, 0x401a).w(m_seibu_sound, FUNC(seibu_sound_device::bank_w));
	map(0x401b, 0x401b).w(m_seibu_sound, FUNC(seibu_sound_device::coin_w));
	map(0x6000, 0x6000).rw("oki1", FUNC(okim6295_device::read), FUNC(okim6295_device::write));
	map(0x6002, 0x6002).rw("oki2", FUNC(okim6295_device::read), FUNC(okim6295_device::write));
	map(0x8000, 0xffff).bankr("seibu_bank1");
}

void raiden2_state::zeroteam_sound_map(address_map &map)
{
	map(0x0000, 0x1fff).rom();
	map(0x2000, 0x27ff).ram();
	map(0x4000, 0x4000).w(m_seibu_sound, FUNC(seibu_sound_device::pending_w));
	map(0x4001, 0x4001).w(m_seibu_sound, FUNC(seibu_sound_device::irq_clear_w));
	map(0x4002, 0x4002).w(m_seibu_sound, FUNC(seibu_sound_device::rst10_ack_w));
	map(0x4003, 0x4003).w(m_seibu_sound, FUNC(seibu_sound_device::rst18_ack_w