#include <hidef.h>      /* common defines and macros */
#include <mc9s12dg256.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg256b"

#include "main_asm.h" /* interface to the assembly module */

# define  c       2867
# define  d       2554
# define  e       2276
# define  f       2148
# define  g       1914
# define  a       1705
# define  b       1519
# define  C       1434
# define  D       1277
# define  E       1138
# define  F       1074
# define  G       947
# define  A       853
# define  B       760
# define  Bf      800
# define  CC      717
# define  DD      639

char k;

int i;
int j;
int y;

int pitch;

int intron[32] = {F, G, C, G, A, CC, Bf, A, F, G, C, 0, C, C, D, F, F, F, G, C, G, A, CC, Bf, A, F, G, C, 0, F, F, F };
int introt[32] = {6, 8, 4, 6, 8, 2,   2, 4, 6, 8, 10,4, 2, 2, 2, 4, 2, 6, 8, 4, 6, 8, 2,   2, 4, 4, 6, 6, 4, 2, 4, 2 };
                                                                                                                       
           
            //We're no strangers to love// You know the rules...do I // A full co....what I'm thinking of// You wouldn't get....any other guy // I just want to tell....feeling // Gotta make you//understand//Never gonna give you up, never gonna let you down  never gonna run around and des you// Never gonna make you cry, never gonna say goodbye, never gonna tell lie, hurt you//                                                                                                                                                                                
int notes[120]={D, E, F, F, G, E, D, C, 0, D, D, E, F, D, C, CC, CC, G, 0, D, D, E, F, D, F, G, 0, E, D, C, 0, D, D, E, F, D, C, G, G, G, A, G, 0, F, G, A, F, G, G, G, A, G, C, 0, D, E, F, D, 0, G, A, G, 0, C, D, F, D, A, A, G, C, D, F, D, G, G, F, E, D, C, D, F, D, F, G, E, D, C, C, G, F, 0, C, D, F, D, A, A, G, C, D, F, D, CC, E, F, E, D, C, D, F, D, F, G, E, D, C, C, G, F};
int times[120]={4, 4, 4, 4, 4, 6, 2, 8, 4, 4, 4, 4, 4, 6, 4, 4, 4, 10, 4, 4, 4, 4, 4, 4, 4, 4, 1, 4, 4, 10, 4, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 8, 4, 16, 4, 4, 4, 4, 4, 4, 4, 8, 8, 4, 4, 4, 4, 4, 1, 4, 4, 8, 4, 2, 2, 2, 2, 4, 6, 8, 2, 2, 2, 2, 4, 6, 6, 2, 4, 2, 2, 2, 2, 6, 4, 6, 2, 6, 4, 6, 8, 4, 2, 2, 2, 2, 4, 6, 10, 2, 2, 2, 2, 6, 4, 6, 2, 4, 2, 2, 2, 2, 6, 4, 6, 2, 6, 4, 6, 8};

int noteloop[57]={C, D, F, D, A, A, G, C, D, F, D, G, G, F, E, D, C, D, F, D, F, G, E, D, C, C, G, F, 0, C, D, F, D, A, A, G, C, D, F, D, CC, E, F, E, D, C, D, F, D, F, G, E, D, C, C, G, F};
int looptime[57]={2, 2, 2, 2, 4, 6, 8, 2, 2, 2, 2, 4, 6, 6, 2, 4, 2, 2, 2, 2, 6, 4, 6, 2, 6, 4, 6, 8, 4, 2, 2, 2, 2, 4, 6, 10, 2, 2, 2, 2, 6, 4, 6, 2, 4, 2, 2, 2, 2, 6, 4, 6, 2, 6, 4, 6, 8};

char* texts[200] = {"We're", "no", "strangers", "to", "love", "", "", "", "you", "know", "the", "rules", "", "and", "so", "do", "I.", "", "A", "full", "com", "mitment's", "", "what", "I'm", "thinking", "", "", "of", "", "you", "wouldn't", "", "get", "this", "from", "any", "", "other", "", "guy", "", "I", "", "just", "wanna", "", "tell", "you", "how", "I'm", "feel", "ing", "", "Got", "ta", "make", "you", "", "un", "der", "stand !", "", "Nev", "er", "gon", "na", "give", "you", "up !", "Nev", "er", "gon", "na",  "let", "you", "down !", "", "", "Nev", "er", "gon", "na", "run", "a", "round", "", "and", "", "desert", "you !", "",  "Nev", "er", "gon", "na", "make", "you", "cry !", "Nev", "er", "gon", "na",  "say", "good", "bye !", "",   "", "Nev", "er", "gon", "na", "tell", "a", "lie,", "", "", "and", "hurt", "you."};
char textplace[200]= {0x02,  0x08,  0x44,       0x07, 0x46, 0x46,0x46,0x46,0x03,  0x07,  0x44,   0x48, 0x48, 0x01,  0x05, 0x08,0x0B,0x4B,0x04, 0x06,   0x40,  0x43,      0x43,  0x03,  0x08,   0x41,   0x41, 0x41,0x4A,0x4A, 0x01,  0x05,     0x05, 0x41,  0x45,   0x4A,  0x03,0x03, 0x07,  0x07, 0x47,0x47, 0x00,0x00, 0x02,   0x07,  0x07, 0x40,  0x45,  0x49,  0x03, 0x07, 0x0B, 0x0B, 0x00,  0x03,  0x06,  0x0B, 0x0B,0x40, 0x42, 0x45, 0x45,        0x02, 0x05, 0x08, 0x0B, 0x42,   0x47, 0x4B,  0x02, 0x05, 0x08, 0x0B,       0x41, 0x45, 0x49,   0x49, 0x49, 0x02, 0x05, 0x08, 0x0B,  0x40, 0x44, 0x45, 0x45,  0x4B,  0x4B, 0x02, 0x09, 0x02,     0x02, 0x05, 0x08, 0x0B, 0x41, 0x46,   0x4A,  0x02, 0x05, 0x08, 0x0B,     0x42,   0x46, 0x4A,  0x4A, 0x4A, 0x02, 0x05, 0x08, 0x0B,  0x41, 0x46, 0x48, 0x48, 0x48, 0x01, 0x05, 0x0A}; 
                                                                                                        
char* pitchdisp[16] = 
      {
       "d", "A", "B", "CC", "D", "E", "F", "g", "a", "b", "DD", "G", "C", "f", "c", "e"
      };

void interrupt 13 handler()
  {
 tone(pitch); 
  }

void main(void) {
  PLL_init();
  keypad_enable();
  lcd_init();
  
        set_lcd_addr(0x02);
        type_lcd("You've been"); 
        set_lcd_addr(0x42);
        type_lcd("'Rick Rolled'");    
  
  
     for (y=0; y<32; y++){
   
          pitch = intron[y];
          sound_init();
          if (pitch != 0)
              {
              sound_on();
              }
          ms_delay(70*introt[y]);
          sound_off();
          ms_delay(30);
   
   }
        
  
         clear_lcd();
        for (i=0; i<120; i++){
        
          if (textplace[i]<textplace[i-1]){clear_lcd();}
          set_lcd_addr(textplace[i]);
          type_lcd(texts[i]);
        
          pitch = notes[i];
          sound_init();
          if (pitch != 0)
              {
              sound_on();
              }
          ms_delay(70*times[i]);
          sound_off();
          ms_delay(30);
        }
        
        clear_lcd();
        set_lcd_addr(0x02);
        type_lcd("You've been"); 
        set_lcd_addr(0x42);
        type_lcd("'Rick Rolled'");        
        
         while(1){
         
        
   for (j=63; j<120; j++){
   
          pitch = notes[j];
          sound_init();
          if (pitch != 0)
              {
              sound_on();
              }
          ms_delay(50*times[j]);
          sound_off();
          ms_delay(30);
   }
   }
        

}
