summaryrefslogblamecommitdiffstats
path: root/src/mame/drivers/apple2e.cpp
blob: ce0368f91f8470e1b2bac8a3dc414b6e591704ad (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12





                                                                            





                                                                                                           

                                                                                                  































                                                                                                          




                                                                               
 
                                                          




























                                                                                   




                                                                            

                                                             










                                                                            

                         

                             
                            


                              
                             

                            
                            

                             
                          
                           
                                 

                            





                                     
                               




















                                     
                            
                                 



                                   
                              
                               
                                
                               
                            
                                    
 

                                


                            
                     




                             
 







                                  
                             





                                  









                             





                                        

                          

                  
                  




                                          

                                                                                         
                                             
                                          
                                                

                                        
                                              
                                            



                                                    
                                          






















                                                     

                                               


                                              
                                                
                                                  

                                          
                                                         
                                          



                                                        
                                                       




















                                                             
                                                



                                                       

                                              
 
                                                                                                       












                                         

                                        
                                         




















                                              

                                              



                                              

                                              



                                              

                                              
                                      
                                          
                                     
                                         

                                              
                                            




                                      

                                              








                                                       
                                            
 





                                                 
                                         





                                              
















                                                  


                                          








                                             

                                      




















                                               
                                                              

                         
                              
                        
 
                                                                  
                                
                           
 


                           
                                                   

                       

                       
                                            









                                                          
                               
                           

                                 

                                      
                                            
                                   
                                    
                                                 


                                                                    
                              
                                


                                

                                     


                                                  

                                                                                                                                                                       

  
 




                                                                                                                                                                             

                                             
                                                 
                         
                                                  



                                                    

                                                             
         
                                                          
                                  













                                                   


                               
                            








                                                        





















                                                               
                         
                                                  




                                                    

                                                                          
         
                                                    
                                  
                                                        












                                                  


                               
                            




                              























                                                                                                       
 












                                                                                                 
                                                                                 






                                                                       
                                                                                 












                                                                                 
                                                 






                                       
                                            
 

                       
                                          

         



                                                    
                                                          















                                                                              




                       





























































                                                                                      
                                          










                                 


                                   





                                                             
                               

                                   
                                    


                                               

                                                                 







                                                             

                                 


                                                                      




                                                                            
         
                                                        
         
                                          


                                             
                                                        









                                                          

















                                                   

                                     
                                 
 
                            
                                                                                                      


                                               
                               














                                                                                             
 


                                                                                                        
                 
                                                                                          




                                                   
                                           


                                               
                                                 



                                                       


                                

                                    
         
 











































                                            

                                   




                                        

                                  
                                      
                                         
                                          
                                    
                                    




                                       





                                   
                                   
                                                  
                                              



                           










                                  
                                 
                           
                       

                              
 































                                                                    
                             




                                                  
                                                                                                                   









                                      
                                                                       









                                      
                                                                      


         
                                                                            






                                                                            

                    
                                   





















                                                                             
                                                                                                             









                                                                                                
                                                                                 






                                           
                                                                              
                                 
                                                                                               


                                    
                                                                                              




                                                                                         
                                                                              








                                                                                               

                                 

                                           
                                                                              











                                                                                                 
                                                                              
                                 
                                                                                                 


                                    
                                                                                                













                                                                       
                                                                            





                                                                            
                                        
         



                                                      
                 







                                                        


                    
                                                    
                 
 
                                            
 
                                                      
                 







                                                        


                    
                                                    
                 

                                            
         
                      
         



                                                                                      



                                                                     
         



                                          
                     
         




                                                                                        
                 























                                                                                                                                                    


                    
                                                
                 
 
                                                                
                 







                                                        


                    
                                                

                 
                                        
         
                                
                 
                                                

                                                


                    
                                                

                                                
                 








                                                   
         

 
                                                       
 
                                 
 





                                                        
 


                                                                      
         





                                                                      
                 




                                               
                 
         
 


                           


                                       

                              

                       
                       
                 
                                        
                              
                 
         
 
                         





                                
                                 
         
                            
                 
                                           


                    
                                    
                         
                                                      


                            







                                                              




                         
                                                         



                                                    


              







                                      







                                              


         
                                                                        
                                                  
 
                                                     








                                               
                                                    

                                                            
                                                    

                                                           
                                                     
                                                           

                                                                 
                                                    

                                                                
                                                        

                                                                
                                                        

                                                               
                                                        

                                                      
                                                                 


                                              
                                                        

                                                      
                                                                






                                              
                                       
                         
                                                        
                                                             

                                              
                                                        
                                                             


                                              
                                                                                                  

         






                                                                      




                                       
                                                      









                                                                                    







                                                                        




                                                        
                                                                      


                                            
                                                                      




                                         







                                                                    
                                                                         




                                                
                                          
                              

                                        
                                          
                              

                                     
                                          
                              

                                         
                                          
                              

                                         
                                        
                                          



                                         
                                       
                                          



                                           
                                          



                                           
                                          



                                         


                                           

                                    


                                           

                                     


                                           

                                    


                                           

                                     


                                           

                                    


                                           

                                     


                                           

                                    


                                           
 
                                                                        

                              
                                      







                                                                                                       



                                                                                                                 









                                                                            
                                                                        
                                                     
 
                                                                             
         

                                              
 

                       

                                                                       
                                                                                        



                                             

                                                                      
 
                                                           
                                                                     
 
                                         
                                                                     
 
                                          
                                                                      
 
                                            
                                                                        
 
                                         
                                                                     
 
                                             
                                                                         
 
                                           
                                                                       
 
                                          
                                                                                
 
                                        
                                                                                 
 
                                         
                                                                            
 
                                         
                                                                     
 
                                         
                                                                              
 
                                              
                                                                                   
 
                                         
                                                                              




                                         
                                                                                              
                         
                                             
 
                                                     
                          
                                                                                                                
 
                                                      
                          
                                                                                                                
 
                                                    
                          
                                                                                                                
 
                                           
                          
                                                                                                                
 
                                           
                          
                                                                                                                


                                          
                                                                                                                


                                          
                                                                                                                
 
                                          
                                                                        
 
                                          
                                                                                 

                        
                                             







                                    
                                                     
 





                                                                                   

                       
                                         



                                          
                                        



                                         
                                       



                                         
                                      



                                         
                                        



                                         
                                       



                                         
                                          



                                               
                                         



                                               
                                       

                                         
                                    


                                                   

                              
                                      

                                         
                                    


                                                   

                              
                                          



                                               
                                           



                                               
                                       


                                                 
                                      


                                                
                                            


                                                      
                                           


                                                     
                                              

                                       
                                                    







                                                                                                       
                                                                                



                                                                                      
                                                                                                                  

                              
                                        

                                               
                                        


                                                
                                             





                                       
                                                                        
                                                     
 
                                                                             
         

                                              
 

                       
                                                                       
                         
                                                                                        



                                             

                                                                                                     
 
                                                           
                                                                     
 
                                         
                                                                     
 
                                          
                                                                      
 
                                                                   
                                               
                                                                    
 
                                         
                                                                     
 
                                                                   
                                               
                                                                    
 
                                           
                                                                       
 
                                       
                                                                   
 
                                        
                                                                                 
 
                                         
                                                                            
 
                                         
                                                                     
 
                                         
                                                                              
 
                                              
                                                                                   
 
                                         
                                                                              
 
                                                     

                                                  
                                                       

                                                       
                                                     

                                                      
                                                     


                                                            
                                                                                         
 
                                                                       
                          
                                                                                                                
 
                                                      
                          
                                                                                                                
 
                                                                        
                          
                                                                             
 
                                           
                          
                                                                                                                
 
                                           
                          
                                                                                                                


                                                 
                                                                 


                                                 
                                                                 
 
                                          

                                           
                                                                        
 
                                          
                                                                                 

                        
                                            







                                        
                                                     
 





                                                                                   

                       
                                         



                                          
                                        



                                         
                                       



                                         
                                      



                                         
                                        



                                         
                                       



                                         
                                          



                                               
                                         



                                               
                                       



                                         
                                      



                                         
                                          



                                               
                                           



                                               
                                       


                                                 
                                      


                                                
                                            


                                                      
                                           


                                                     



























                                                                            





                                           

                                        

                              

                                         


                              
                                            

































































































                                                             
                                              







                                     
                                                       


                    




                                                                      
                                                          
                         
                                                                                    
                         

                            
                                                             



                                                          














                                    
                                              


            





                                                              
                                                  
                 
                                                                            
                 

                    

                                                                    



                                                          








                                                                           




                                                               


         
                                                              
 
                                                     
 
                                             
         
                                                                                                                                    




                                               
                                                                     




                                  
                                                                          


                                                     





                                                                          







                                           





                                               
                                             
         
                                                                                                                                    




                                               
                                                                     


         
                                                             
 
                                              
                                                 

 

                                                                        


                                                                                      

                                                                                    

                                                                         
 

                                       
                                                                 



                                       
                                           

 

                                            
                                                                 



                                       
                                            

 

                                    
                                                                 




                                       
                                        
 



                                                                                    
                                                                         

                                       

                                                   
                                                

         
                                           

 

                                            

                                                   
                                                

         
                                            

 

                                    




                                          
                                        
 
 


                                                                                    







                                                

                                   
                                                                    
         
                                             
                                   
                                       


                            
                                                                        
         
                                                                          




                                  

                                       
                                                                    
         


                                             

         




                                                  




                                          
                                                                                                                                   
         
                                           

         
                                                                    
         


                                             




                                          

                                    
                                                                                                                                   
         
                                          




                            
                                                       

                                                     
                                           


                                               


                       
                                                                        
         
                                                                          


         


                                                                                    



                                  
                                                                               

         
                                                                                   






                                   
                                                                               


         

                                           




                                            


                                  

                                 
                                    
         


                                            











                                                                                         



                                                  



                 
                                    





















                                                                            
                                    

                                   

                                            
















                                                                                         
                                    
















                                                                                                                                          
                                         





































                                                                                                
                                                                     


























                                                                                
                                                                                                                                      












































































                                                                                                    
                                                                            

















                                                                













                                                                                                                                                







                                                                                

                                                                                         














                                                                                 

                                                                                                 
















                                                                                                       

                                                                                                 

                                                                                                       
                                                                                             














                                                                                 

                                                                                         

                                                                                             

                                                                                                                 








                                                                                 

                                                                                                     



                                                   



                                                                                                                  



                                                   



                                                                                                                  



                                                   



                                                                                                     



                                                   



                                                                                                     



                                                   





                                                                                                       



                                                  




                                                                                         




                                                  




                                                                                         



                                                  


                                                                                                  

                                                                                                 
                                                                           



                                                  




                                                                                                




                                                                              
                                                                                       



                                                





                                                                                                           






                                                       
 




























                                                                            

                                                   





                                                                                                 









                                                                                                                 


                                                                                                              
                 

                                                       
                                                                               





                                            
                                                                              























                                                             
                                        







                                                                            










































































                                                                                                                     

                                                                                                         

                                                                                                         
                                                                                                         
                                                                                                         


                                                                                                         













                                                                                                                    
                                                                                                         




















































                                                                                                                  

                                                                                                                                        









                                                                                                               



                                                                                                                                           















                                                                                                                                    
                                                                                                         





                                                                                                                                    







                                                                                                               
                                                                                           
                                                                                              
                                                   







                                                                                                                 
                                                                                           
                                                   







                                                                                                             
                                                                                           
                                                                                                                     
                                                   








                                                                                                                    
                                                   







                                                                                                                  

                                                                                             
                                                                                                       
                                                   



                                                                                                                 
                                                                                              


                                                                                                         
                                                                                           
                                                                                                     
                                                   







                                                                                                                 
                                                                                           
                                                                                                                         
                                                   







                                                                                                                 
                                                                                           

                                                                                                                     
                                                   




















                                                                                                                                    
 


                                        












































































































                                                                                                                                    
 


                                        
                                  

















                                                                                                                 
                                                                                           





























                                                                                                                     
                                                   

























































                                                                                                                                    
 




                                        




                                        

































































































































                                                                                                                                               










































                                                                                                                                               
















                                                                                                                  
                                                                                                                                             
























                                                                                                                     
                                                                                                                                                              

































                                                                                                                                    


                                      
                                                                                                                       





                                                                                                                          
                                                                                                         
                                                                                                                          
                                                                                                                          













































                                                                                                                            
                                                                                                                                                                                        










                                                                                                                               
                                                                                                                                                                
















































                                                                                                                                    


                                      


                                                                                                                     

                                                                                                         

                                                                                                         
                                                                                                         
                                                                                                         


                                                                                                         













                                                                                                                    
                                                                                                         





















































                                                                                                                        
                                                                                                                                  










                                                                                                                        


                                                                                                                                           





















                                                                                                                                    


                                      





















                                                                                                                                  
                                                                               


                                                                                                        
                                                                                      

                                                                                                                             
                                                                                       



















                                                                                                                                   
                                                                                                
                                                                                   
                                                                                              



                                                           


                                                                                         
 
 

                                                   
                                    

                                                                        

                                                                           
                                      
                                                                                               
                                                          
 
                                                                             
                                                            
 


                                                                          
                                       

                            
                                               
                                                                                   
 
                                      
                                                                                           
 
                 
                                                                           

                          
                                                                                                                                  

                          
                                                                                                                                  

                          
                                                                                                                                  

                          
                                                                                                                                   

                          
                                                                                                                                   

                          
                                                                                                                                   

                          
                                                                                                                                 

                          
                                                                                                                                 

                          
                                                                                                                                 

                          
                                                                                                                                 

                                              
                                                                                                                                  

                          
                                                                                                                                      

                                 













                                                                        

                                                                                  
                                                         
                                                                                            

                          
                                  
                                                  


                                                               
                                                                   






                                                                         
 



                                                                               
                                                                                                                           
 

                                                                                        




                                                                                                   
 


                                                        
 

                                                  
                        
                          

                                        
 

                                                    
                        

                                                                                                                                  

                                                                        

                                                                           
 

                                                    
                        

                                                                                                   
 

                                                                             
 
 

                                                  
                        

                                                                        
 


                                                                                                                                                                      
 

                                                    
                        

                                                                        
 
 

                                                   
                         

                                                                                                                                 

                                                                        

                                                
                                              
 






                                                                        
 


















                                                                                                         

                                                                        

                                                                                                  
 
                                    
                                             
 
                                                                  
 
 













                                                                              









                                                                                                                 



                                                               





                                                 











































                                                                              

                                         
                                                   
                                                          
 


                                                   








                                                                  






                                                

                                                    
                        

                                                                               
                                                                           
 

                                    




                                                                           
 
                                                                                           
 
 

                                                       
                        
 
                                    
                                                                          
 
 

                                                       
                        

                                                                               
                                                                           
 
                                    
                                                                          
 
                                                                                           
 










                                                          

                                                    
                        

                                                                         
 


                                                                  
 

                                    
 






                                                                           
 
                                                                                           
 
 

                                                       
                        

                                                                         
 


                                                                  
 

                                    
 






                                                                           
 
                                                                                           
 
 
                                               

                        
 
                                    
                                    


                                                                           

                                                                     






                                                                                     










                                                                                                                 
                                       










                                                                                                                                  
                                       






                                                                                                                 

                                   
                                                                                                                     
 
                                       



                                                                                                                 
                                                                                                                      

       




                                                                                                           
                                       











                                                                                                                                                                 
                                       










                                                                                                                                                                 
                                       






                                                                                                                                                                 



                                                                                                                 
                                       

                                                                                                                 

                                                          
                                                                                                                  

       




                                                                                                                 
                                       











                                                                                                                                                     
                                       
                                                                                                                                       




                                                                                                                                  
                   

                                                                                                                                 
 
                                               

                                                                                                                                   
 

                                                                                                                                                        
 
                                                 


                                                                                                               




                                                                                                                    
                                       











                                                                                                                                                                   

                                       















                                                                                                                                                                 
                                       










                                                                                                                                  
                                       










                                                                                                                                  
                                       










                                                                                                                                                          
                                       
                                                                                                              









                                                                                                                                                          
                                       
                                                                                                              









                                                                                                                                                          
                                       










                                                                                                                                                        
                                       





                                                                                                                






                                                                                                                                    

                                                                                                                             

                                              

                                                                                                                                        

                                               

                                                                                                                                        

                                      

                                                                                                                                    







                                                                                                                                    

























































































                                                                                                                                                






















                                                                                                                                           


















                                                                                                                                                                                   
                                                                                                                                                                                      

                                                                                                                                                                                      
                                                                                                                                                                                      

                                                                                                                                                                                         
                                                                                                                                                         
// license:BSD-3-Clause
// copyright-holders:R. Belmont
/***************************************************************************

    apple2e.c - Apple IIe/IIc/IIc Plus and clones

    Next generation driver written in September/October 2014 by R. Belmont.
    Thanks to the original Apple II series driver's authors: Mike Balfour, Nathan Woods, and R. Belmont
    Special thanks to the Apple II Documentation Project/Antoine Vignau and Peter Ferrie.


    IIe: base of this driver.  64K RAM, slot 0 language card emulation without the double-read requirement,
         lowercase and SHIFT key on button 2, Open and Solid Apple buttons on joy buttons 0 and 1,
         auxiliary slot, built-in 80 column support if extra RAM added.

         Physical slot 0 was eliminated thanks to the built-in language card.

         Most of the write-only softswitches gained readback locations, necessary to make interrupt-driven
         software possible.

         Base 80-column card: 1K RAM, allows 80 columns and double-lo-res,
         no double-hi-res.

         Extended 80-column card: 64K RAM (including a second language card),
         allows 80 columns, double-lo-res, and double-hi-res.

         Revision A motherboards (very rare) don't support double-hi-res; it's unclear
         if double-lo-res works or not.  We emulate the much more common Rev B or later
         board.

    IIe enhanced: 65C02 CPU with more instructions, MouseText in the character generator.

    IIe platinum: Like enhanced but with added numeric keypad and extended 80-column card
         included in the box.  Keypad CLEAR generates ESC by default, one hardware mod
         made it generate CTRL-X instead.  (new keyboard encoder ROM?)

    NOTE: On real IIe and IIe enhanced h/w, pressing SHIFT and paddle button 2 will
    short out the power supply and cause a safety shutdown.  (We don't emulate
    this "feature", and it was relatively rare in real life as Apple joysticks only
    had buttons 0 and 1 normally).

    IIc: IIe enhanced shrunken into a pizzabox with a Disk II compatible
         half-height drive included in the case.

     No slots, but included functionality equivalent to the following slots
     on the motherboard:
     - 2 Super Serial Cards (modem and printer ports)
     - extended 80 column card / 128K RAM
     - Disk II IWM controller
     - Apple II Mouse card (firmware entry points are compatible,
       but the hardware implementation omits the 68705 and is quite different!)

     Has a 40/80 column switch and a QWERTY/DVORAK switch.

    IIc (UniDisk 3.5): IIc with ROM doubled to 32K and the ROMSWITCH register
         added to page between the original 16K ROM and the new added 16K.  The
         extra firmware space was dedicated to implementing the Protocol Converter,
         later renamed "SmartPort", which communicates with "smart" packet devices
         over the IWM bus.

         Partial AppleTalk code also exists in this ROM but it doesn't work and
         was not completed.

    IIc (Original Memory Expansion):
         Removes AppleTalk and adds support for a memory expansion card with up
         to 1 MB; this is identical both in hardware and firmware to the "Slinky"
         memory expansion card for the Apple IIe (a2bus/a2memexp.c).

    IIc (Revised Memory Expansion, Rev. 3):
        Fixes several nasty bugs in the Original Memory Expansion version.  Not
        currently dumped.

    IIc (Rev 4):
        Fixes memory size detection for memory cards with less than 1MB.  Fixes
        several screen hole errors introduced in Rev 3, and fixes Terminal Mode
        wherein the firmware can be put into a built-in terminal mode for simple
        tests with a modem.

    IIc Plus:
        Like IIc with memory expansion, but with licensed built-in Zip Chip which
        runs the 65C02 at 4 MHz turbo speed with a small cache RAM.

        The machine has an internal "Apple 3.5" drive plus a custom chip
        named "MIG" (Multidrive Interface Glue) which helps with the control
        of the drive.  This gets around the fact that 1 MHz isn't
        sufficient to handle direct Woz-style control of a double-density
        3.5" drive.

        External drive port allows IIgs-style daisy-chaining.

----------------------------------

TK3000 keyboard matrix

Data bus D0-D7 is X0-X7
Address bus A0-A11 is Y0-Y11

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

#include "emu.h"
#include "video/apple2.h"

#include "cpu/m6502/m6502.h"
#include "cpu/m6502/m65c02.h"
#include "cpu/mcs48/mcs48.h"
#include "cpu/z80/z80.h"
#include "imagedev/cassette.h"
#include "imagedev/flopdrv.h"
#include "machine/appldriv.h"
#include "machine/bankdev.h"
#include "machine/kb3600.h"
#include "machine/mos6551.h"
#include "machine/ram.h"
#include "machine/sonydriv.h"
#include "machine/timer.h"
#include "machine/ds1315.h"
#include "machine/apple2common.h"

#include "bus/a2bus/a2bus.h"
#include "bus/a2bus/a2diskii.h"
#include "bus/a2bus/a2diskiing.h"
#include "bus/a2bus/a2mockingboard.h"
#include "bus/a2bus/a2cffa.h"
#include "bus/a2bus/a2memexp.h"
#include "bus/a2bus/a2scsi.h"
#include "bus/a2bus/a2hsscsi.h"
#include "bus/a2bus/a2thunderclock.h"
#include "bus/a2bus/a2softcard.h"
#include "bus/a2bus/a2videoterm.h"
#include "bus/a2bus/a2ssc.h"
#include "bus/a2bus/a2swyft.h"
#include "bus/a2bus/a2themill.h"
#include "bus/a2bus/a2sam.h"
#include "bus/a2bus/a2alfam2.h"
#include "bus/a2bus/laser128.h"
#include "bus/a2bus/a2echoii.h"
#include "bus/a2bus/a2arcadebd.h"
#include "bus/a2bus/a2midi.h"
#include "bus/a2bus/a2zipdrive.h"
#include "bus/a2bus/a2applicard.h"
#include "bus/a2bus/a2ultraterm.h"
#include "bus/a2bus/a2pic.h"
#include "bus/a2bus/a2corvus.h"
#include "bus/a2bus/a2mcms.h"
#include "bus/a2bus/a2dx1.h"
#include "bus/a2bus/timemasterho.h"
#include "bus/a2bus/mouse.h"
#include "bus/a2bus/ezcgi.h"
#include "bus/a2bus/pc_xporter.h"
#include "bus/a2bus/a2eauxslot.h"
#include "bus/a2bus/a2estd80col.h"
#include "bus/a2bus/a2eext80col.h"
#include "bus/a2bus/a2eramworks3.h"
#include "bus/a2bus/ssprite.h"
#include "bus/a2bus/ssbapple.h"
#include "bus/a2bus/transwarp.h"
#include "bus/a2bus/a2vulcan.h"
#include "bus/a2bus/4play.h"
#include "bus/a2bus/computereyes2.h"

#include "bus/a2gameio/gameio.h"

#include "bus/rs232/rs232.h"

#include "screen.h"
#include "softlist.h"
#include "speaker.h"

#include "formats/ap2_dsk.h"
#include "formats/ap_dsk35.h"


#define A2_CPU_TAG "maincpu"
#define A2_KBDC_TAG "ay3600"
#define A2_BUS_TAG "a2bus"
#define A2_SPEAKER_TAG "speaker"
#define A2_CASSETTE_TAG "tape"
#define A2_UPPERBANK_TAG "inhbank"
#define IIC_ACIA1_TAG "acia1"
#define IIC_ACIA2_TAG "acia2"
#define IICP_IWM_TAG    "fdc"
#define LASER128_UDC_TAG "l128udc"
#define PRINTER_PORT_TAG "printer"
#define MODEM_PORT_TAG "modem"
#define A2_AUXSLOT_TAG "auxbus"
#define A2_VIDEO_TAG "a2video"

#define A2_0000_TAG "r00bank"
#define A2_0200_TAG "r02bank"
#define A2_0400_TAG "r04bank"
#define A2_0800_TAG "r08bank"
#define A2_2000_TAG "r20bank"
#define A2_4000_TAG "r40bank"
#define A2_C100_TAG "c1bank"
#define A2_C300_TAG "c3bank"
#define A2_C400_TAG "c4bank"
#define A2_C800_TAG "c8bank"
#define A2_LCBANK_TAG "lcbank"

#define MOUSE_BUTTON_TAG    "mse_button"
#define MOUSE_XAXIS_TAG     "mse_x"
#define MOUSE_YAXIS_TAG     "mse_y"

#define CNXX_UNCLAIMED  -1
#define CNXX_INTROM     -2

#define IRQ_SLOT 0
#define IRQ_VBL  1
#define IRQ_MOUSEXY 2

class apple2e_state : public driver_device
{
public:
	apple2e_state(const machine_config &mconfig, device_type type, const char *tag) :
		driver_device(mconfig, type, tag),
		m_maincpu(*this, A2_CPU_TAG),
		m_screen(*this, "screen"),
		m_scantimer(*this, "scantimer"),
		m_ram(*this, RAM_TAG),
		m_rom(*this, "maincpu"),
		m_a2common(*this, "a2common"),
		m_cecbanks(*this, "cecexp"),
		m_ay3600(*this, A2_KBDC_TAG),
		m_video(*this, A2_VIDEO_TAG),
		m_a2bus(*this, A2_BUS_TAG),
		m_a2eauxslot(*this, A2_AUXSLOT_TAG),
		m_gameio(*this, "gameio"),
		m_mouseb(*this, MOUSE_BUTTON_TAG),
		m_mousex(*this, MOUSE_XAXIS_TAG),
		m_mousey(*this, MOUSE_YAXIS_TAG),
		m_kbdrom(*this, "keyboard"),
		m_kbspecial(*this, "keyb_special"),
		m_sysconfig(*this, "a2_config"),
		m_speaker(*this, A2_SPEAKER_TAG),
		m_cassette(*this, A2_CASSETTE_TAG),
		m_upperbank(*this, A2_UPPERBANK_TAG),
		m_0000bank(*this, A2_0000_TAG),
		m_0200bank(*this, A2_0200_TAG),
		m_0400bank(*this, A2_0400_TAG),
		m_0800bank(*this, A2_0800_TAG),
		m_2000bank(*this, A2_2000_TAG),
		m_4000bank(*this, A2_4000_TAG),
		m_c100bank(*this, A2_C100_TAG),
		m_c300bank(*this, A2_C300_TAG),
		m_c400bank(*this, A2_C400_TAG),
		m_c800bank(*this, A2_C800_TAG),
		m_lcbank(*this, A2_LCBANK_TAG),
		m_acia1(*this, IIC_ACIA1_TAG),
		m_acia2(*this, IIC_ACIA2_TAG),
		m_laserudc(*this, LASER128_UDC_TAG),
		m_iicpiwm(*this, IICP_IWM_TAG),
		m_ds1315(*this, "nsc")
	{ }

	required_device<cpu_device> m_maincpu;
	required_device<screen_device> m_screen;
	required_device<timer_device> m_scantimer;
	required_device<ram_device> m_ram;
	required_memory_region m_rom;
	required_device<apple2_common_device> m_a2common;
	optional_memory_region m_cecbanks;
	required_device<ay3600_device> m_ay3600;
	required_device<a2_video_device> m_video;
	required_device<a2bus_device> m_a2bus;
	optional_device<a2eauxslot_device> m_a2eauxslot;
	required_device<apple2_gameio_device> m_gameio;
	optional_ioport m_mouseb, m_mousex, m_mousey;
	optional_memory_region m_kbdrom;
	required_ioport m_kbspecial;
	required_ioport m_sysconfig;
	required_device<speaker_sound_device> m_speaker;
	optional_device<cassette_image_device> m_cassette;
	required_device<address_map_bank_device> m_upperbank;
	required_device<address_map_bank_device> m_0000bank;
	required_device<address_map_bank_device> m_0200bank;
	required_device<address_map_bank_device> m_0400bank;
	required_device<address_map_bank_device> m_0800bank;
	required_device<address_map_bank_device> m_2000bank;
	required_device<address_map_bank_device> m_4000bank;
	required_device<address_map_bank_device> m_c100bank;
	required_device<address_map_bank_device> m_c300bank;
	required_device<address_map_bank_device> m_c400bank;
	required_device<address_map_bank_device> m_c800bank;
	required_device<address_map_bank_device> m_lcbank;
	optional_device<mos6551_device> m_acia1, m_acia2;
	optional_device<applefdc_base_device> m_laserudc;
	optional_device<iwm_device> m_iicpiwm;
	required_device<ds1315_device> m_ds1315;

	TIMER_DEVICE_CALLBACK_MEMBER(apple2_interrupt);
	TIMER_DEVICE_CALLBACK_MEMBER(ay3600_repeat);

	virtual void machine_start() override;
	virtual void machine_reset() override;

	uint32_t screen_update(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect);

	DECLARE_READ8_MEMBER(ram0000_r);
	DECLARE_WRITE8_MEMBER(ram0000_w);
	DECLARE_READ8_MEMBER(ram0200_r);
	DECLARE_WRITE8_MEMBER(ram0200_w);
	DECLARE_READ8_MEMBER(ram0400_r);
	DECLARE_WRITE8_MEMBER(ram0400_w);
	DECLARE_READ8_MEMBER(ram0800_r);
	DECLARE_WRITE8_MEMBER(ram0800_w);
	DECLARE_READ8_MEMBER(ram2000_r);
	DECLARE_WRITE8_MEMBER(ram2000_w);
	DECLARE_READ8_MEMBER(ram4000_r);
	DECLARE_WRITE8_MEMBER(ram4000_w);
	DECLARE_READ8_MEMBER(cec4000_r);
	DECLARE_READ8_MEMBER(cec8000_r);
	DECLARE_WRITE8_MEMBER(ram8000_w);
	DECLARE_READ8_MEMBER(auxram0000_r);
	DECLARE_WRITE8_MEMBER(auxram0000_w);
	DECLARE_READ8_MEMBER(auxram0200_r);
	DECLARE_WRITE8_MEMBER(auxram0200_w);
	DECLARE_READ8_MEMBER(auxram0400_r);
	DECLARE_WRITE8_MEMBER(auxram0400_w);
	DECLARE_READ8_MEMBER(auxram0800_r);
	DECLARE_WRITE8_MEMBER(auxram0800_w);
	DECLARE_READ8_MEMBER(auxram2000_r);
	DECLARE_WRITE8_MEMBER(auxram2000_w);
	DECLARE_READ8_MEMBER(auxram4000_r);
	DECLARE_WRITE8_MEMBER(auxram4000_w);
	DECLARE_READ8_MEMBER(c000_r);
	DECLARE_WRITE8_MEMBER(c000_w);
	DECLARE_READ8_MEMBER(c000_iic_r);
	DECLARE_WRITE8_MEMBER(c000_iic_w);
	DECLARE_READ8_MEMBER(c080_r);
	DECLARE_WRITE8_MEMBER(c080_w);
	DECLARE_READ8_MEMBER(c100_r);
	DECLARE_READ8_MEMBER(c100_int_r);
	DECLARE_READ8_MEMBER(c100_int_bank_r);
	DECLARE_READ8_MEMBER(c100_cec_r);
	DECLARE_READ8_MEMBER(c100_cec_bank_r);
	DECLARE_WRITE8_MEMBER(c100_w);
	DECLARE_READ8_MEMBER(c300_r);
	DECLARE_READ8_MEMBER(c300_int_r);
	DECLARE_READ8_MEMBER(c300_int_bank_r);
	DECLARE_READ8_MEMBER(c300_cec_r);
	DECLARE_READ8_MEMBER(c300_cec_bank_r);
	DECLARE_WRITE8_MEMBER(c300_w);
	DECLARE_READ8_MEMBER(c400_r);
	DECLARE_READ8_MEMBER(c400_int_r);
	DECLARE_READ8_MEMBER(c400_int_bank_r);
	DECLARE_READ8_MEMBER(c400_cec_r);
	DECLARE_READ8_MEMBER(c400_cec_bank_r);
	DECLARE_WRITE8_MEMBER(c400_w);
	DECLARE_WRITE8_MEMBER(c400_cec_w);
	DECLARE_READ8_MEMBER(c800_r);
	DECLARE_READ8_MEMBER(c800_int_r);
	DECLARE_READ8_MEMBER(c800_cec_r);
	DECLARE_READ8_MEMBER(c800_cec_bank_r);
	DECLARE_READ8_MEMBER(c800_b2_int_r);
	DECLARE_WRITE8_MEMBER(c800_w);
	DECLARE_READ8_MEMBER(inh_r);
	DECLARE_WRITE8_MEMBER(inh_w);
	DECLARE_READ8_MEMBER(lc_r);
	DECLARE_WRITE8_MEMBER(lc_w);
	DECLARE_READ8_MEMBER(lc_romswitch_r);
	DECLARE_WRITE8_MEMBER(lc_romswitch_w);
	DECLARE_WRITE_LINE_MEMBER(a2bus_irq_w);
	DECLARE_WRITE_LINE_MEMBER(a2bus_nmi_w);
	DECLARE_WRITE_LINE_MEMBER(a2bus_inh_w);
	DECLARE_READ_LINE_MEMBER(ay3600_shift_r);
	DECLARE_READ_LINE_MEMBER(ay3600_control_r);
	DECLARE_WRITE_LINE_MEMBER(ay3600_data_ready_w);
	DECLARE_WRITE_LINE_MEMBER(ay3600_ako_w);
	DECLARE_READ8_MEMBER(memexp_r);
	DECLARE_WRITE8_MEMBER(memexp_w);
	DECLARE_READ8_MEMBER(nsc_backing_r);

	void apple2cp(machine_config &config);
	void laser128ex2(machine_config &config);
	void spectred(machine_config &config);
	void laser128(machine_config &config);
	void apple2c_iwm(machine_config &config);
	void apple2c_mem(machine_config &config);
	void cec(machine_config &config);
	void mprof3(machine_config &config);
	void apple2e(machine_config &config);
	void apple2ep(machine_config &config);
	void apple2c(machine_config &config);
	void tk3000(machine_config &config);
	void apple2ee(machine_config &config);
	void apple2c_map(address_map &map);
	void apple2c_memexp_map(address_map &map);
	void apple2e_map(address_map &map);
	void c100bank_map(address_map &map);
	void c300bank_map(address_map &map);
	void c400bank_map(address_map &map);
	void c800bank_map(address_map &map);
	void inhbank_map(address_map &map);
	void laser128_map(address_map &map);
	void lcbank_map(address_map &map);
	void r0000bank_map(address_map &map);
	void r0200bank_map(address_map &map);
	void r0400bank_map(address_map &map);
	void r0800bank_map(address_map &map);
	void r2000bank_map(address_map &map);
	void r4000bank_map(address_map &map);
	void spectred_keyb_map(address_map &map);

	bool m_35sel, m_hdsel, m_intdrive;

private:
	int m_speaker_state;
	int m_cassette_state, m_cassette_out;

	double m_joystick_x1_time;
	double m_joystick_y1_time;
	double m_joystick_x2_time;
	double m_joystick_y2_time;

	uint16_t m_lastchar, m_strobe;
	uint8_t m_transchar;
	bool m_anykeydown;
	int m_repeatdelay;

	int m_inh_slot;
	int m_cnxx_slot;

	bool m_page2;
	bool m_an0, m_an1, m_an2, m_an3;

	bool m_vbl, m_vblmask;

	bool m_xy, m_x0edge, m_y0edge;
	bool m_x0, m_x1, m_y0, m_y1;
	bool m_xirq, m_yirq;
	int last_mx, last_my, count_x, count_y;

	bool m_intcxrom;
	bool m_80store;
	bool m_slotc3rom;
	bool m_altzp;
	bool m_ramrd, m_ramwrt;
	bool m_lcram, m_lcram2, m_lcprewrite, m_lcwriteenable;
	bool m_ioudis;
	bool m_romswitch;
	bool m_mockingboard4c;
	bool m_intc8rom;

	bool m_isiic, m_isiicplus, m_iscec, m_iscecm, m_iscec2000;
	uint8_t m_migram[0x800];
	uint16_t m_migpage;

	bool m_zipunlocked;
	int m_zipstage;

	uint8_t *m_ram_ptr, *m_rom_ptr, *m_cec_ptr;
	int m_ram_size;

	int m_cec_bank;

	uint8_t *m_aux_ptr, *m_aux_bank_ptr;

	int m_inh_bank;

	double m_x_calibration, m_y_calibration;

	device_a2bus_card_interface *m_slotdevice[8];
	device_a2eauxslot_card_interface *m_auxslotdevice;

	int m_irqmask;

	uint8_t m_exp_bankhior;
	int m_exp_addrmask;
	uint8_t m_exp_regs[0x10];
	uint8_t *m_exp_ram;
	int m_exp_wptr, m_exp_liveptr;

	void do_io(int offset, bool is_iic);
	uint8_t read_floatingbus();
	void update_slotrom_banks();
	void lc_update(int offset, bool writing);
	uint8_t read_slot_rom(int slotbias, int offset);
	void write_slot_rom(int slotbias, int offset, uint8_t data);
	uint8_t read_int_rom(int slotbias, int offset);
	void auxbank_update();
	void cec_lcrom_update();
	void raise_irq(int irq);
	void lower_irq(int irq);
	void update_iic_mouse();

	uint8_t m_cec_remap[0x40000];

	uint8_t mig_r(uint16_t offset);
	void mig_w(uint16_t offset, uint8_t data);

	offs_t dasm_trampoline(std::ostream &stream, offs_t pc, const util::disasm_interface::data_buffer &opcodes, const util::disasm_interface::data_buffer &params);
};


offs_t apple2e_state::dasm_trampoline(std::ostream &stream, offs_t pc, const util::disasm_interface::data_buffer &opcodes, const util::disasm_interface::data_buffer &params)
{
	return m_a2common->dasm_override(stream, pc, opcodes, params);
}

uint8_t apple2e_state::mig_r(uint16_t offset)
{
	//printf("mig_r @ %x\n", offset + 0xc00);
	// MIG RAM window
	if ((offset >= 0x200) && (offset < 0x220))
	{
		return m_migram[m_migpage + offset];
	}

	// increment MIG RAM window and return previous value
	if ((offset >= 0x220) && (offset < 0x240))
	{
		uint8_t rv = m_migram[m_migpage + offset];
		m_migpage += 0x20;
		m_migpage &= 0x7ff;
		return rv;
	}

	if ((offset >= 0x240) && (offset < 0x260))
	{
		m_hdsel = false;
		sony_set_sel_line(m_iicpiwm, 0);
	}

	if ((offset >= 0x260) && (offset < 0x280))
	{
		m_hdsel = true;
		sony_set_sel_line(m_iicpiwm, 0x80);
	}

	// reset MIG RAM window
	if (offset == 0x2a0)
	{
		m_migpage = 0;
	}

	return read_floatingbus();
}

void apple2e_state::mig_w(uint16_t offset, uint8_t data)
{
	//printf("mig_w %x @ %x\n", data, offset + 0xc00);

	if (offset == 0x40)
	{
		m_iicpiwm->device_reset();
		return;
	}

	if ((offset >= 0x80) && (offset < 0xa0))
	{
		//printf("MIG: enable internal drive on d2\n");
		m_intdrive = true;
		return;
	}

	if ((offset >= 0xc0) && (offset < 0xe0))
	{
		//printf("MIG: disable internal drive\n");
		m_intdrive = false;
		return;
	}

	// MIG RAM window
	if ((offset >= 0x200) && (offset < 0x220))
	{
		m_migram[m_migpage + offset] = data;
		return;
	}

	// increment MIG RAM window, but write value at old location first
	if ((offset >= 0x220) && (offset < 0x240))
	{
		m_migram[m_migpage + offset] = data;
		m_migpage += 0x20;
		m_migpage &= 0x7ff; // make sure we wrap
		return;
	}

	if ((offset >= 0x240) && (offset < 0x260))
	{
		m_35sel = false;
		return;
	}

	if ((offset >= 0x260) && (offset < 0x280))
	{
		m_35sel = true;
		return;
	}

	// reset MIG RAM window
	if (offset == 0x2a0)
	{
		m_migpage = 0;
	}
}

WRITE_LINE_MEMBER(apple2e_state::a2bus_irq_w)
{
	if (state == ASSERT_LINE)
	{
		raise_irq(IRQ_SLOT);
	}
	else
	{
		lower_irq(IRQ_SLOT);
	}
}

WRITE_LINE_MEMBER(apple2e_state::a2bus_nmi_w)
{
	m_maincpu->set_input_line(INPUT_LINE_NMI, state);
}

// TODO: this assumes /INH only on ROM, needs expansion to support e.g. phantom-slotting cards and etc.
WRITE_LINE_MEMBER(apple2e_state::a2bus_inh_w)
{
	if (state == ASSERT_LINE)
	{
		// assume no cards are pulling /INH
		m_inh_slot = -1;

		// scan the slots to figure out which card(s) are INHibiting stuff
		for (int i = 0; i <= 7; i++)
		{
			if (m_slotdevice[i])
			{
				// this driver only can inhibit from 0xd000-0xffff
				if ((m_slotdevice[i]->inh_start() == 0xd000) &&
					(m_slotdevice[i]->inh_end() == 0xffff))
				{
					if ((m_slotdevice[i]->inh_type() & INH_READ) == INH_READ)
					{
						if (m_inh_bank != 1)
						{
							m_upperbank->set_bank(1);
							m_inh_bank = 1;
						}
					}
					else
					{
						if (m_inh_bank != 0)
						{
							m_upperbank->set_bank(0);
							m_inh_bank = 0;
						}
					}

					m_inh_slot = i;
					break;
				}
			}
		}

		// if no slots are inhibiting, make sure ROM is fully switched in
		if ((m_inh_slot == -1) && (m_inh_bank != 0))
		{
			m_upperbank->set_bank(0);
			m_inh_bank = 0;
		}
	}
}

READ8_MEMBER(apple2e_state::memexp_r)
{
	uint8_t retval = m_exp_regs[offset];

	if (!m_exp_ram)
	{
		return read_floatingbus();
	}

	if (offset == 3)
	{
		if (m_exp_liveptr <= m_exp_addrmask)
		{
			retval = m_exp_ram[m_exp_liveptr];
		}
		else
		{
			retval = 0xff;
		}
		m_exp_liveptr++;
		m_exp_regs[0] = m_exp_liveptr & 0xff;
		m_exp_regs[1] = (m_exp_liveptr>>8) & 0xff;
		m_exp_regs[2] = ((m_exp_liveptr>>16) & 0xff) | m_exp_bankhior;
	}

	return retval;
}

WRITE8_MEMBER(apple2e_state::memexp_w)
{
	if (!m_exp_ram)
	{
		return;
	}

	switch (offset & 0xf)
	{
		case 0:
			m_exp_wptr &= ~0xff;
			m_exp_wptr |= data;
			m_exp_regs[0] = m_exp_wptr & 0xff;
			m_exp_regs[1] = (m_exp_wptr>>8) & 0xff;
			m_exp_regs[2] = ((m_exp_wptr>>16) & 0xff) | m_exp_bankhior;
			m_exp_liveptr