Subversion Repositories KoE_projects

Rev

Blame | Last modification | View Log | Download | RSS feed | ?url?

  1. #include "pae_mcu.h"
  2.  
  3. int main(void)
  4. {
  5.         pentagon_init();
  6.         while(1) system_cycle();
  7. }
  8.  
  9. void system_cycle(void)
  10. {
  11.         if(condition_1)
  12.         {
  13.                 cycle_counter++;
  14.                 if(klava) { klava = false; upload_data(); }  
  15.                 if(reset)       {       reset = false; z80_reset();     }
  16.                 if(received_status == 0) { i2c_get_status(); cmos_ready = 0; }
  17.                 else
  18.                         if((cmos_ready > 128) && (received_status != 0)) { get_cmos_data(received_status); received_status = 0; }
  19.         }
  20.   if(condition_2)
  21.         {
  22.                 cycle_counter++;
  23.                 timer_flag = false;
  24.                 if((timeout == error_timeout) && keyboard_error) reset_error();
  25.                 if(mouse_read_error || mouse_write_error) mouse_restart();
  26.                 in_l = in_l_measure(); in_r = in_r_measure();
  27.                 if(condition_3)
  28.                 {
  29.                         myshko_flag = false; upload_data();
  30.                 }
  31.         }
  32.         if(condition_4)
  33.         {
  34.                 cycle_counter++;
  35.        
  36.         }
  37.         if(condition_5) { cycle_counter++;  }
  38.         if(condition_6) { cycle_counter++; upload_data(); }                    
  39.         if(condition_7)
  40.         {
  41.                 cycle_counter=0;
  42.         }
  43. }
  44.  
  45. void pentagon_init(void)
  46. {
  47. unsigned char index;
  48.  
  49.         PINSEL10 = 0;
  50.         SCS |= 1; // enable fast gpio
  51.         FIO1DIR |= (1<<21);       // sd_power
  52.         FIO1MASK &=~(1<<21);
  53.         FIO1CLR |=(1<<21);              // always enable
  54.        
  55.         pll_setup();
  56.         pll_pause;
  57.         rtc_setup();
  58.         adc_setup();
  59.         pwm_setup();
  60.         kempston_setup();
  61.  
  62.         // ps2 keyboard and mouse pins setup
  63.         PINSEL3 |= (1 << 21);
  64.         PINSEL3 |= (1 << 20); // cap0[0] for k_clk_i
  65.         PINSEL3 |= (1 << 23);
  66.         PINSEL3 |= (1 << 22); // cap0[1] for m_clk_i
  67.         PINSEL3 &= ~(1 << 13);
  68.         PINSEL3 &= ~(1 << 12); // gpio for k_data_o
  69.         FIO1DIR |= (1 << 22);
  70.         FIO1MASK &= ~(1 << 22);
  71.         FIO1CLR |= (1 << 22);
  72.         PINSEL3 &= ~(1 << 15);
  73.         PINSEL3 &= ~(1 << 14); // gpio for k_data_i
  74.         FIO1DIR &= ~(1 << 23);
  75.         FIO1MASK &= ~(1 << 23);
  76.         PINSEL3 &= ~(1 << 17);
  77.         PINSEL3 &= ~(1 << 16); // gpio for k_clk_o
  78.         FIO1DIR |= (1 << 24);
  79.         FIO1MASK &= ~(1 << 24);
  80.         FIO1CLR |= (1 << 24);
  81.         PINSEL3 &= ~(1 << 19);
  82.         PINSEL3 &= ~(1 << 18); // gpio for m_clk_o
  83.         FIO1DIR |= (1 << 25);
  84.         FIO1MASK &= ~(1 << 25);
  85.         FIO1CLR |= (1 << 25);
  86.         PINSEL3 &= ~(1 << 25);
  87.         PINSEL3 &= ~(1 << 24); // gpio for m_data_i
  88.         FIO1DIR &= ~(1 << 28);
  89.         FIO1MASK &= ~(1 << 28);
  90.         PINSEL3 &= ~(1 << 27);
  91.         PINSEL3 &= ~(1 << 26); // gpio for m_data_o
  92.         FIO1DIR |= (1 << 29);
  93.         FIO1MASK &= ~(1 << 29);
  94.         FIO1CLR |= (1 << 29); // data xx|--    
  95.         ps2_init();
  96.        
  97.         timer0_setup(); // timer0 - 50 Hz
  98.         timer1_setup(); // timer1 - 20 kHz
  99.  
  100.         // led pins    
  101.         FIO2DIR |= (1<<0);
  102.         FIO2DIR |= (1<<1);
  103.         FIO2MASK &= ~(1<<0);
  104.         FIO2MASK &= ~(1<<1);
  105.        
  106.         received_status = 0;
  107.         upload_firmwarez();     // fpga firmwarez upload
  108.         // int adjustment      
  109.         int_delay = 0;
  110.        
  111.         get_status_flag = true;
  112.         get_data_flag = true;
  113.         get_cmos_data_flag = 0xff;
  114.  
  115.         m_byte_number = 0;
  116.         keyboard_error = false;
  117.         mouse_read_error = 0;
  118.         mouse_write_error = 0;
  119.         mouse_wheel = 0;
  120.  
  121.         //cmos setup
  122.         transmit_cmos_data();
  123.         cmos_a = &cmos; cmos_a = cmos_a + 0xC;
  124.         mouse = *cmos_a; mouse = (mouse >> 24) & 0xff;
  125.  
  126.         ///////// if battery low
  127.         mouse = 3;
  128.         ///////// if battery low
  129.         mouse_setup();
  130.         if((mouse > 0) && (mouse < 8)) {m_b = 7; m_x = 127; m_y = 127; } else {m_b = 0xff; m_x = 0xff; m_y = 0xff;};
  131.        
  132.         shift_flag = false;
  133.         ctrl_flag = false;
  134.  
  135.         led0_off();
  136.         led1_off();
  137.  
  138.         reset = false;
  139.         for(index = 0; index < 5; index++) keyarray[index] = 0xff;     
  140.         z80_reset();
  141. }
  142.  
  143. void get_cmos_data(unsigned char status)
  144. {
  145. unsigned char index;
  146. unsigned long bf;
  147.  
  148.         get_cmos_data_flag = 0;
  149.         fpga_send_command(10);
  150.         while(send_command_flag != 0xff) {};
  151.         cmos_a = &cmos;
  152.         for(index = 0; index < 64; index++)
  153.         {
  154.                 bf=((buffer4cmos[4*index] << 24) | (buffer4cmos[4*index+1] << 16) | (buffer4cmos[4*index+2] << 8) | (buffer4cmos[4*index+3])); *cmos_a = bf;
  155.                 cmos_a++;       //=cmos_a+1;
  156.                 if ((status >> 1) & 1 ) RTC_SEC = ((buffer4cmos[0] & 0xf0) >> 4)*10 + (buffer4cmos[0] & 0x0f);
  157.                 if ((status >> 2) & 1 ) RTC_MIN = ((buffer4cmos[2] & 0xf0) >> 4)*10 + (buffer4cmos[2] & 0x0f);
  158.                 if ((status >> 3) & 1 ) RTC_HOUR = ((buffer4cmos[4] & 0xf0) >> 4)*10 + (buffer4cmos[4] & 0x0f);
  159.                 if ((status >> 4) & 1 ) RTC_DOW = buffer4cmos[6] - 1;
  160.                 if ((status >> 5) & 1 ) RTC_DOM = ((buffer4cmos[7] & 0xf0) >> 4)*10 + (buffer4cmos[7] & 0x0f);
  161.                 if ((status >> 6) & 1 ) RTC_MONTH = ((buffer4cmos[8] & 0xf0) >> 4)*10 + (buffer4cmos[8] & 0x0f);       
  162.                 if ((status >> 7) & 1 ) RTC_YEAR = 2000 + ((buffer4cmos[9] & 0xf0) >> 4)*10 + (buffer4cmos[9] & 0x0f);
  163.                 get_cmos_data_flag = 0xff;
  164.         }
  165. }
  166.  
  167. void transmit_cmos_data(void)
  168. {
  169. unsigned char index;
  170. unsigned long bf;
  171.  
  172.         index = 0;
  173.         cmos_a = &cmos;
  174.         while(index < 17)
  175.         {
  176.                 bf=*cmos_a;
  177.                 i2c_buffer[4*index] =  (bf >> 24) & 255;
  178.                 i2c_buffer[4*index+1] = (bf >> 16) & 255;
  179.                 i2c_buffer[4*index+2] = (bf >> 8) & 255;
  180.                 i2c_buffer[4*index+3] = bf & 255;
  181.                 index++;
  182.                 cmos_a++;
  183.         }
  184.         fpga_send_command(11);
  185.         while(send_command_flag != 0xff) {};
  186.         fpga_send_data(12,67);
  187.         while(send_data_flag != 0xff) {};
  188. }
  189.  
  190. void fpga_send_command(char command)
  191. {
  192.         I21CONSET = 1 << 6;
  193.         i2c_command = (command << 1);
  194.         send_command_flag = 0;
  195.         I21CONCLR = 0x000000FF;
  196.         I21CONSET = 0x00000040;
  197.         I21CONSET = 0x00000020;
  198. }
  199.  
  200. void fpga_send_data(char data, unsigned int skoka)
  201. {
  202.         I21CONSET = 1 << 6;
  203.         i2c_command = (data << 1);
  204.         send_data_flag = 0;
  205.         i2c_byte_number = 0;
  206.         i2c_number_of_bytes = skoka;
  207.         I21CONCLR = 0x000000FF;
  208.         I21CONSET = 0x00000040;
  209.         I21CONSET = 0x00000020;
  210. }
  211.  
  212. void i2c_get_status(void)
  213. {
  214.         I21CONSET = 1 << 6;
  215.         i2c_command = 9 << 1;
  216.         get_status_flag = false;
  217.         I21CONCLR = 0x000000FF;
  218.         I21CONSET = 0x00000040;
  219.         I21CONSET = 0x00000020;
  220. }
  221.  
  222. void i2c_get_data(void)
  223. {
  224.  
  225.         I21CONSET = 1 << 6;
  226.         i2c_command = 10 << 1;
  227.         get_data_flag = false;
  228.         i2c_byte_number = 0;
  229.         I21CONCLR = 0x000000FF;
  230.         I21CONSET = 0x00000040;
  231.         I21CONSET = 0x00000020;
  232. }
  233.  
  234. void gpio_fpga_send_command(char command)
  235. {
  236. unsigned char bitcounter;
  237. unsigned char a;
  238.  
  239.         command = (command << 1) & 254;
  240.         FIO0DIR |=(1<<0);
  241.         FIO0SET |=(1<<0); // SDA=1
  242.         FIO0SET |=(1<<1); // SCL=1
  243.         FIO0CLR |=(1<<0); // SDA=0 transmitting the start condition
  244.         FIO0CLR |=(1<<1); // SCL=0
  245.         for (bitcounter = 7; bitcounter != 255; bitcounter--)
  246.         {
  247.                 a = (command >> bitcounter) & 1;
  248.                 if (a) FIO0SET |= (1 << 0); else FIO0CLR |= (1 << 0);
  249.                 FIO0SET |= (1 << 1); // _|- scl
  250.                 FIO0CLR |= (1 << 1); // -|_ scl
  251.         }
  252.         FIO0DIR &= ~(1 << 0); // switch sda for read confirm from slave
  253.         FIO0SET |= (1 << 1); // _|- scl
  254.         FIO0CLR |= (1 << 1); // -|_ scl
  255.         FIO0SET |= (1 << 1); // _|- scl
  256. }
  257.  
  258. void gpio_fpga_send_data(char data, unsigned int skoka)
  259. {
  260. unsigned char bitcounter;
  261. unsigned int bytecounter;
  262. unsigned char index;
  263.  
  264.         data = (data << 1) & 254;
  265.         FIO0DIR |=(1<<0);
  266.         FIO0SET |=(1<<0); // SDA=1
  267.         FIO0SET |=(1<<1); // SCL=1
  268.         FIO0CLR |=(1<<0); // SDA=0 transmitting the start condition
  269.         FIO0CLR |=(1<<1); // SCL=0
  270.         for (bitcounter = 7; bitcounter != 255; bitcounter--)
  271.         {
  272.                 index = (data >> bitcounter) & 1;
  273.                 if (index) FIO0SET |= (1 << 0); else FIO0CLR |= (1 << 0);
  274.                 FIO0SET |= (1 << 1); // _|- scl
  275.                 FIO0CLR |= (1 << 1); // -|_ scl
  276.         }
  277.         FIO0DIR &= ~(1 << 0); // switch sda for read confirm from slave
  278.         FIO0SET |= (1 << 1); // _|- scl
  279.         FIO0CLR |= (1 << 1); // -|_ scl
  280.         FIO0DIR |= (1 << 0);
  281.         FIO0SET |= (1 << 0);    //?
  282.         for (bytecounter = 0; bytecounter < skoka; bytecounter++)
  283.         {
  284.                 for (bitcounter = 7; bitcounter != 255; bitcounter--)
  285.                 {
  286.                         index = ((i2c_buffer[bytecounter]) >> bitcounter) & 1;
  287.                         if(index) FIO0SET |= (1 << 0); else FIO0CLR |= (1 << 0);
  288.                         FIO0SET |= (1<<1); // _|- scl
  289.                         FIO0CLR |= (1<<1); // -|_ scl
  290.                 }
  291.                 FIO0DIR &= ~(1 << 0); // switch sda for read confirm from slave
  292.                 FIO0SET |= (1 << 1); // _|- scl
  293.                 FIO0CLR |= (1 << 1); // -|_ scl
  294.                 FIO0DIR |=(1 << 0);
  295.         }
  296. }
  297.  
  298. void upload_data(void)
  299. {
  300. unsigned char index;
  301.  
  302.         for(index = 0; index < 5; index++) i2c_buffer[index] = keyarray[index];
  303.         i2c_buffer[5] = (((RTC_SEC/10) & 0x0f) << 4) | (RTC_SEC%10);
  304.         i2c_buffer[6] = (((RTC_MIN/10) & 0x0f) << 4) | (RTC_MIN%10);
  305.         i2c_buffer[7] = (((RTC_HOUR/10) & 0x0f) << 4) | (RTC_HOUR%10);
  306.         i2c_buffer[8] = RTC_DOW+1;
  307.         i2c_buffer[9] = (((RTC_DOM/10) & 0x0f) << 4) | (RTC_DOM%10);
  308.         i2c_buffer[10] = (((RTC_MONTH/10) & 0x0f) << 4) | (RTC_MONTH%10);
  309.         i2c_buffer[11] = (((((RTC_YEAR - 2000) & 255)/10) & 0x0f) << 4) | (((RTC_YEAR - 2000) & 255)%10);
  310.         i2c_buffer[12] = m_b;
  311.         i2c_buffer[13] = m_x;
  312.         i2c_buffer[14] = m_y;
  313.         i2c_buffer[15] = kempston_read();
  314.         i2c_buffer[16] = in_l & 0xff;
  315.         i2c_buffer[17] = (in_l >> 8) & 0xff;
  316.         i2c_buffer[18] = in_r & 0xff;
  317.         i2c_buffer[19] = (in_r >> 8) & 0xff;
  318.         fpga_send_command(22);
  319.         while(send_command_flag != 0xff) {};
  320.         fpga_send_data(2,120);
  321.         while(send_data_flag != 0xff) {};
  322.         fpga_send_command(0);
  323.         while(send_command_flag != 0xff) {};
  324. }
  325.  
  326. void upload_firmwarez(void)
  327. {
  328.         gpio_i2c_setup();
  329.         if(!upload_from_sd("firmware/PaE.rbf")) upload_from_rom();
  330.         gpio_fpga_send_command(1);
  331.         gpio_fpga_send_command(0);
  332.         if(!screen_from_sd("firmware/logo.rom")) { data_base_addr = logo; rom_from_rom(5, logo_size); }
  333.         if(!rom_from_sd(64 + 8,"firmware/gluk.rom")) { data_base_addr = gluk; rom_from_rom(64 + 8, gluk_size); }
  334.         if(!rom_from_sd(64 + 10,"firmware/basic128.rom"))       { data_base_addr = basic128; rom_from_rom(64 + 10, basic128_size); }
  335.         if(!rom_from_sd(64 + 9,"firmware/trdos.rom"))   { data_base_addr = trdos; rom_from_rom(64 + 9, trdos_size); }
  336.         if(!rom_from_sd(64 + 11,"firmware/basic48.rom")) { data_base_addr = basic48; rom_from_rom(64 + 11, basic48_size); }
  337.         if(!rom_from_sd(64 + 12,"firmware/fatall.rom")) {data_base_addr = fatall; rom_from_rom(64 + 12, fatall_size); }
  338.         trd_to_ram("firmware/test.trd");
  339.         sd_spi_unsetup();
  340.         i2c_setup();
  341. }
  342.  
  343. char upload_from_sd(char* filename)
  344. {
  345. FATFS FatFs;            /* FatFs work area needed for each volume */
  346. FIL Fil;                        /* File object needed for each open file */    
  347. unsigned int read, index;
  348.        
  349.         fpga_init();
  350.         // Select FPGA to upload
  351.         FIO0CLR |= (1 << 25);                                   // FPGA_NCE = 0
  352.         fpga_pause;
  353.         if(f_mount(0, &FatFs) != FR_OK) return 0;
  354.         if(f_open (&Fil, filename, FA_READ) != FR_OK) return 0;
  355.         do
  356.         {
  357.                 f_read(&Fil, &buffer, BUFSIZE, &read);
  358.                 for(index = 0; index < read; index++) fpga_spi_send(buffer[index]);
  359.         } while (read==BUFSIZE);
  360.         f_close(&Fil);
  361.         fpga_pause;
  362.         return 1;
  363. }
  364.  
  365. char screen_from_sd(char* filename)
  366. {
  367. FATFS FatFs;            /* FatFs work area needed for each volume */
  368. FIL Fil;                        /* File object needed for each open file */    
  369. unsigned int read, index;
  370.  
  371.         i2c_buffer[0] = 5;     
  372.         gpio_fpga_send_data(6,1); // upnload zx screen, page01, address from 0
  373.         gpio_fpga_send_command(0);
  374.         i2c_byte_number=0;
  375.  
  376.         if(f_mount(0, &FatFs) != FR_OK) return 0;
  377.         if(f_open (&Fil, filename, FA_READ) != FR_OK) return 0;
  378.         do
  379.         {
  380.                 f_read(&Fil, &buffer, BUFSIZE, &read);
  381.                 for(index = 0; index < read; index++) {i2c_buffer[i2c_byte_number] = buffer[index]; i2c_byte_number++;}
  382.         } while (read==BUFSIZE);
  383.         f_close(&Fil);
  384.         gpio_fpga_send_command(3);
  385.         gpio_fpga_send_data(0,16385);
  386. return 0;
  387. }
  388.  
  389. char rom_from_sd(unsigned char page, char* filename)
  390. {
  391. FATFS FatFs;            /* FatFs work area needed for each volume */
  392. FIL Fil;                        /* File object needed for each open file */    
  393. unsigned int read, index;
  394.  
  395.         i2c_buffer[0] = page;  
  396.         gpio_fpga_send_data(6,1);
  397.         gpio_fpga_send_command(0);
  398.         i2c_byte_number=0;
  399.         if(f_mount(0, &FatFs) != FR_OK) return 0;
  400.         if(f_open (&Fil, filename, FA_READ) != FR_OK) return 0;
  401.         do
  402.         {
  403.                 f_read(&Fil, &buffer, BUFSIZE, &read);
  404.                 for(index = 0; index < read; index++) {i2c_buffer[i2c_byte_number] = buffer[index]; i2c_byte_number++;}
  405.         } while (read==BUFSIZE);
  406.         f_close(&Fil); 
  407.         gpio_fpga_send_command(3);
  408.         gpio_fpga_send_data(0,16385);
  409. return 1;
  410. }
  411.  
  412. char trd_to_ram(char* filename)
  413. {
  414. FATFS FatFs;            /* FatFs work area needed for each volume */
  415. FIL Fil;                        /* File object needed for each open file */    
  416. unsigned int read, index;
  417.  
  418.         a       =       246; addr       =       0;
  419.         i2c_buffer[0]=a;       
  420.         gpio_fpga_send_data(6,1);
  421.         gpio_fpga_send_command(3);
  422.         if(f_mount(0, &FatFs) != FR_OK) return 0;
  423.         if(f_open (&Fil, filename, FA_READ) != FR_OK) return 0;
  424.         do
  425.         {
  426.                 f_read(&Fil, &buffer, BUFSIZE, &read);
  427.                 for(index = 0; index < read; index++)
  428.                 {
  429.                         i2c_buffer[addr] = buffer[index];
  430.                         addr++;
  431.                         if(addr==16384)
  432.                         {
  433.                                 addr=0; a--; if(a==205) a=206;
  434.                                 gpio_fpga_send_data(0,16385);
  435.                                 i2c_buffer[0]=a;       
  436.                                 gpio_fpga_send_data(6,1);
  437.                                 gpio_fpga_send_command(3);
  438.                         }
  439.           }
  440.         } while (read==BUFSIZE);
  441.         f_close(&Fil);
  442.         gpio_fpga_send_command(0);       
  443.         return 1;
  444. }
  445.  
  446. /////////////////////// interrupts ////////////////////////////////////
  447. void timer0_irq(void) __irq
  448. {
  449. // 50 Hz interrupts + PS2 events interrupts
  450.        
  451.         if ((T0IR >> 4) & 1)
  452.         {
  453.                 T0IR |= (1 << 4); timeout = 0; if(!keyboard_error) interrupt(~(FIO1PIN >> 23) & 1);
  454.         }
  455.         if ((T0IR >> 5) & 1)
  456.         {
  457.                 T0IR |= (1 << 5); mouse_transaction(); // mouse interrupt
  458.         }
  459.         if (T0IR & 1)
  460.         {
  461.                 T0IR |= 1;
  462.                 if(test_counter < 60) test_counter++;
  463.                 timer_flag = true; led0_on(); led0_off();
  464.                 if(timeout < error_timeout) timeout++;
  465.                 if(m_timeout < error_timeout) m_timeout++;
  466.         }
  467.         VICVectAddr =0;
  468. }
  469.  
  470. void timer1_irq(void) __irq
  471. {
  472. // 20 kHz (50 us)
  473. static uint32_t div10;
  474.  
  475.         led0_on(); led0_off();
  476.         T1IR |= 1;
  477.         ps2_transmit_flag1++;
  478.         mouse_answer_timeout++;
  479.         fpga_timing++;
  480.        
  481.         Timer_div++;
  482.         if(Timer_div==35999)
  483.         {
  484.                 Timer_div = 0; Timer++;
  485.                 if (++div10 >= 10)
  486.                         {
  487.                 div10 = 0;
  488.                 disk_timerproc();               /* Disk timer function (100Hz) */
  489.                         }
  490.         }      
  491. VICVectAddr =0;
  492. }
  493.  
  494. void i2c_irq(void) __irq
  495. {
  496.  
  497.         switch (I21STAT)
  498.         {
  499.                 case (0x08): // start condition has been sent
  500.                         I21CONCLR = 0x20; // start_i2c flag reset
  501.                         if (!send_command_flag | !send_data_flag)
  502.                         {
  503.                                 I21DAT = i2c_command & ~1; // transmitting the slave address, "command" in our case
  504.                                 if(!send_command_flag) send_command_flag++;
  505.                                 if(!send_data_flag) send_data_flag++;
  506.                         }
  507.                         if (!get_status_flag || !get_data_flag) I21DAT = (i2c_command & ~1);
  508.                         break;
  509.                 case (0x18): // slave address + W (0 in first byte) has been sent
  510.                         if(send_command_flag == 1) { send_command_flag = 0xff; I21CONSET = 0x10; }
  511.                         if(send_data_flag == 1)
  512.                         {
  513.                                 I21DAT = i2c_buffer[i2c_byte_number];
  514.                                 i2c_byte_number++; send_data_flag++;
  515.                         } // transmitting the command protocol, "data" in our case
  516.                         if (!get_status_flag || !get_data_flag) I21CONSET = 0x20; // repeat start
  517.                         break;
  518.                 case (0x28): // data has been transmitted, ack received
  519.                         if ((send_data_flag == 2) && (i2c_byte_number <= i2c_number_of_bytes))
  520.                         {
  521.                                 I21DAT = i2c_buffer[i2c_byte_number];    i2c_byte_number++;} // transmitting the data
  522.                                 if ((send_data_flag == 2) && (i2c_byte_number > i2c_number_of_bytes))
  523.                                 {
  524.                                         I21CONSET = 0x10; send_data_flag=0xff;
  525.                                 } // stop
  526.                         break;
  527.                 case (0x10): // repeated start has been sent
  528.                         I21CONCLR = 0x20; // start_i2c flag reset
  529.                         if (!get_status_flag || !get_data_flag) { if (!get_data_flag) I21DAT = 25; else I21DAT = (i2c_command | 1); };
  530.                         break;
  531.                 case (0x40): // slave address + r was transmitted. ack has been received
  532.                         if (!get_status_flag) I21CONCLR = 4; // clear AAC flag. nack will be transmitted
  533.                         if (!get_data_flag) I21CONSET = 4; // set AAC flag. ack will be transmitted
  534.                 break;
  535.                 case (0x48): // slave address + r was transmitted. nack has been received
  536.                         I21CONSET = 0x10; // stop i2c
  537.                         get_status_flag = true;
  538.                         get_data_flag = true;
  539.                         break;
  540.                 case (0x58): // data has been received, nack has been transmitted
  541.                         if (!get_status_flag) { received_status = I21DAT; get_status_flag = true; }
  542.                         if(!get_data_flag)
  543.                         {
  544.                                 if (!get_cmos_data_flag)
  545.                                 {
  546.                                         get_data_flag = true;
  547.                                 }
  548.                         }
  549.                         I21CONSET = 0x10; // stop i2c
  550.                         break;
  551.                 case (0x50): // data has been received, ack has been transmitted
  552.                         if (!get_cmos_data_flag)
  553.                         {      
  554.                                 if (i2c_byte_number < 259)
  555.                                 {
  556.                                         i2c_byte_number++;
  557.                                         I21CONSET = 4; // set AAC flag. ack will be transmitted
  558.                                 }
  559.                                 else
  560.                                 {
  561.                                         i2c_byte_number++;
  562.                                         I21CONCLR = 4;
  563.                                 }
  564.                         }      
  565.                         break;
  566.                 default:        send_data_flag = 0xff;
  567.                         send_command_flag = 0xff;
  568.                         get_data_flag = true;
  569.                         get_status_flag = true;
  570.                         break;
  571.         }
  572.         I21CONCLR = 0x08; // clear the i2c interrupt flag
  573.         VICVectAddr =0;
  574. }
  575.  
  576. void z80_reset(void)
  577. {
  578.         fpga_send_command(1);
  579.         while(send_command_flag != 0xff) {};
  580.         fpga_send_command(7);
  581.         while(send_command_flag != 0xff) {};
  582.         fpga_send_command(8);
  583.         while(send_command_flag != 0xff) {};
  584.         fpga_send_command(5);
  585.         while(send_command_flag != 0xff) {};
  586.         fpga_send_command(0);
  587.         while(send_command_flag != 0xff) {};
  588. }
  589.  
  590. unsigned char kempston_read(void)
  591. {
  592.         return ((FIO2PIN >> 7) & 1) | (((FIO2PIN >> 6) & 1) << 1) | (((FIO2PIN >> 5) & 1) << 2) | (((FIO2PIN >> 4) & 1) << 3) | (((FIO0PIN >> 11) & 1) << 4);
  593. }
  594.  
  595. unsigned int in_l_measure(void)
  596. {
  597.         AD0CR &=~(0xff);
  598.         AD0CR |= 1; // select ad0[0]
  599.         AD0CR |= (1 << 24); //start the conversion
  600.         while(!((AD0GDR >> 31) & 1)) {};
  601.         AD0CR &= ~(1 << 24); //stop the conversion
  602.         return ((AD0GDR >> 6) & 1023);
  603. }
  604.  
  605. unsigned int in_r_measure(void)
  606. {
  607.         AD0CR &= ~(0xff);
  608.         AD0CR |= 2; // select ad0[1]
  609.         AD0CR |= (1 << 24); //start the conversion
  610.         while(!((AD0GDR >> 31) & 1)) {};
  611.         AD0CR &= ~(1 << 24); //stop the conversion
  612.         return ((AD0GDR >> 6) & 1023);
  613. }
  614.  
  615. void rom_from_rom(unsigned char page, unsigned long size)
  616. {
  617. unsigned int i, decompressed;
  618.  
  619.         i2c_buffer[0]=page;    
  620.         gpio_fpga_send_data(6,1);
  621.         gpio_fpga_send_command(0);
  622.         i2c_byte_number=0;
  623.         //---------------------------------------------------------------------------------------
  624.         // Prepare decompressing
  625.         arithmetic_decompress_init(ReadAddrFnc, size);
  626.         do
  627.         {
  628.                 arithmetic_decompress_chunk(buffer, BUFSIZE, &decompressed);
  629.                 for(i = 0; i < decompressed; i++)
  630.                 {
  631.                         i2c_buffer[i2c_byte_number] = buffer[i]; i2c_byte_number++;
  632.                 }
  633.         } while(decompressed > 0);
  634.         arithmetic_decompress_done();
  635.         //---------------------------------------------------------------------------------------
  636.         gpio_fpga_send_command(3);
  637.         gpio_fpga_send_data(0,16384);
  638. }
  639.  
  640. static unsigned char ReadAddrFnc(unsigned long addr)
  641. {
  642.         return data_base_addr[addr];
  643. }
  644.  
  645. void upload_from_rom(void)
  646. {
  647. unsigned int i, decompressed;
  648.  
  649.         fpga_init();
  650.         // Select FPGA to upload
  651.         FIO0CLR |= (1 << 25);   // FPGA_NCE = 0
  652.         fpga_pause;
  653.         data_base_addr = fpga;
  654.         arithmetic_decompress_init(ReadAddrFnc, fpga_size);
  655.         do
  656.         {
  657.                 arithmetic_decompress_chunk(buffer, BUFSIZE, &decompressed);
  658.                 for(i = 0; i < decompressed; i++)
  659.                 {
  660.                         fpga_spi_send(buffer[i]);
  661.                 }
  662.         } while(decompressed > 0);
  663.         arithmetic_decompress_done();    
  664.         fpga_pause;
  665. }
  666.  
  667. void i2c_setup(void)
  668. {
  669.         PCLKSEL0 &= ~(1 << 15);
  670.         PCLKSEL0 |= (1 << 14);   // clk for i2c
  671.         PINSEL0 |= (1 << 1);
  672.         PINSEL0 |= (1 << 0);
  673.         PINSEL0 |= (1 << 3);
  674.         PINSEL0 |= (1 << 2);
  675.         I21SCLH = 90;
  676.         I21SCLL = 90; // SCL clock frequency ~400kHz
  677.  
  678.         VICVectCntl19 = 14;    
  679.         VICVectAddr19   = (unsigned)i2c_irq; // address of the interrupt subroutine
  680.         VICIntEnable |= (1<<19);        // enable i2c interrupt
  681.  
  682.         send_command_flag = 0xff;
  683.         send_data_flag = 0xff;
  684.  
  685.         fpga_send_command(1);
  686.         while(send_command_flag != 0xff) {};
  687.  
  688.         fpga_send_command(7);
  689.         while(send_command_flag != 0xff) {};
  690. }
  691.  
  692. void spi_fpga_init(void)
  693. {
  694.         PINSEL0 |=(0x01UL<<31);
  695.         PINSEL0 |=(1<<30); // P0.15 -> SCK
  696.         PINSEL1 |=(1<<1);
  697.         PINSEL1 |=(1<<0); // P0.16 -> SSEL
  698.         PINSEL1 |=(1<<3);
  699.         PINSEL1 |=(1<<2); // P0.17 -> MISO
  700.         PINSEL1 |=(1<<5);
  701.         PINSEL1 |=(1<<4); // P0.18 -> MOSI
  702.         // enable SPI-Master
  703.         S0SPCR =0x60;  //  Master enable, datatransfer from the lsb, sck is active high
  704.         // set max speed
  705.         S0SPCCR=8;
  706. }
  707.  
  708. void fpga_init(void)
  709. {
  710.         spi_fpga_init();
  711.         PINSEL1 &= ~(1 << 23);
  712.         PINSEL1 &= ~(1 << 22);  // GPIO for P0.27 (FPGA_NSTATUS)
  713.         PINSEL1 &= ~(1 << 25);
  714.         PINSEL1 &= ~(1 << 24);  // GPIO for P0.28 (FPGA_CONFDONE)
  715.         PINSEL1 &= ~(1 << 19);
  716.         PINSEL1 &= ~(1 << 18); // GPIO for P0.25 (FPGA_NCE)
  717.         PINSEL3 &= ~(0x01UL << 31);
  718.         PINSEL3 &= ~(1 << 30);  // GPIO for P1.31 (FPGA_NCONFIG)
  719.         FIO0MASK &= ~(1 << 27);
  720.         FIO0DIR &= ~(1 << 27); // input for FPGA_NSTATUS
  721.         FIO0MASK &= ~(1 << 28);
  722.         FIO0DIR &= ~(1 << 28); // input for FPGA_CONFDONE
  723.         FIO0MASK &= ~(1 << 25);
  724.         FIO0DIR |= (1 << 25); // output for FPGA_NCE
  725.         FIO1MASK &= ~(0x01UL << 31);
  726.         FIO1DIR |= (0x01UL << 31); // output for FPGA_NCONFIG
  727.         // unselect fpga before programming
  728.         FIO0SET |= (1 << 25);   // FPGA_NCE = 1
  729.         // Start programming firmware (1->0->1 sequence)
  730.         FIO1SET |= (0x01UL << 31);
  731.         fpga_pause;
  732.         FIO1CLR |= (0x01UL << 31);
  733.         fpga_pause;
  734.         FIO1SET |= (0x01UL << 31);
  735.         fpga_pause;
  736.         // Wait until nSTATUS becomes 1
  737.         while(!(FIO0PIN & (1 << 27)));
  738. }
  739.  
  740. char fpga_spi_send(unsigned char spi_data)
  741. {
  742. unsigned char incom;
  743.  
  744.         S0SPDR = spi_data;
  745.         while(!(S0SPSR & (1 << 7)));
  746.         incom = S0SPDR;
  747.         return incom;
  748. }
  749.  
  750. void pwm_setup(void)
  751. {
  752.         PINSEL4 |= (1 << 4);
  753.         PINSEL4 &= ~(1 << 5);   //p2.2 -> pwm1[3]
  754.         PCLKSEL0 &= ~(1 << 13);
  755.         PCLKSEL0 |= (1 << 12);  // CCLK for PWM
  756.         PWM1PR = 0;
  757.         PWM1PC=0;
  758.         PWM1PCR &=~ (0 << 3);
  759.         PWM1PCR |=(1 << 11);
  760.         PWM1MR0 = 4; // stop
  761.         PWM1MR3 = 2; //start
  762.         PWM1LER |=(1 << 3);
  763.         PWM1MCR |= (1 << 1);
  764.         PWM1TCR = 0x00000002;
  765.         PWM1TCR = 0x00000009;  
  766. }
  767.  
  768. void timer0_setup(void)
  769. {
  770.         PCLKSEL0 &= ~(1 << 3);
  771.         PCLKSEL0 |= (1 << 2);
  772.         VICVectCntl4 = 0;       // priority level
  773.         VICVectAddr4    = (unsigned)timer0_irq; // address of interrupt subroutine
  774.         VICIntEnable |= (1 << 4);       // enable timer0 interrupt
  775.         T0MR0 = 1;
  776.         T0PR = 500000; // 50 Hz for timer0
  777.         T0CCR |= (1 << 0); // capture on cap0[0] rising edge
  778.         T0CCR &= ~(1 << 1); // capture on cap0[0] rising edge
  779.         T0CCR |= (1 << 2); // enable interrupt on cap0[0] event
  780.         T0CCR |= (1 << 3); // capture on cap0[1] rising edge
  781.         T0CCR &= ~(1 << 4); // capture on cap0[1] rising edge
  782.         T0CCR |= (1<<5); // enable interrupt on cap0[1] event
  783.         T0TCR = 2; // reset the counter
  784.         T0MCR |= 3; // interrupt
  785.         T0TCR = 1; // start the timer
  786.         timer_flag=false;
  787.         klava=0;
  788.         myshko_flag=false;
  789. }
  790.  
  791. void timer1_setup(void)
  792. {
  793.         PCLKSEL0 &= ~(1 << 5);
  794.         PCLKSEL0 |= (1 << 4);
  795.         VICVectCntl5 = 1;       // priority level
  796.         VICVectAddr5    = (unsigned)timer1_irq; // address of interrupt subroutine
  797.         VICIntEnable |= (1 << 5);       // enable timer1 interrupt
  798.         T1MR0 = 1;
  799.         T1PR = 1800; // 50 us for timer0
  800.         T1TCR = 2; // reset the counter
  801.         T1MCR |= 3; // interrupt
  802.         T1TCR = 1; // start the timer
  803. }
  804.  
  805. void pll_setup(void)
  806. {
  807.         CLKSRCSEL = 1; 
  808.         CCLKCFG = 3;
  809.         PLLCFG = 23;
  810.         PLLCFG |= (1 << 16);
  811.         PLLFEED = 0xAA;
  812.         PLLFEED = 0x55;
  813.         PLLCON = 1;
  814.         PLLFEED = 0xAA;
  815.         PLLFEED = 0x55;
  816.         while(!((PLLSTAT>>26) & 1)) {};
  817.         PLLCON |=(1 << 1);
  818.         PLLFEED = 0xAA;
  819.         PLLFEED = 0x55;
  820. }
  821.  
  822. void sd_spi_setup(void)
  823. {
  824.         PINSEL0 &= ~(1 << 11);
  825.         PINSEL0 &= ~(1 << 10); // GPIO P0.5 for SPI_CS
  826.         PINSEL0 |= (1 << 13);
  827.         PINSEL0 &= ~(1 << 12); // P0.6 -> SSEL1
  828.         PINSEL0 |= (1 << 15);
  829.         PINSEL0 &= ~(1 << 14); // P0.7 -> SCK1
  830.         PINSEL0 |= (1 << 17);
  831.         PINSEL0 &= ~(1 << 16); // P0.8 -> MISO1
  832.         PINSEL0 |= (1 << 19);
  833.         PINSEL0 &= ~(1 << 18); // P0.9 -> MOSI1
  834.         PCONP |= (1 << 8); // enable the SPI
  835.         PCLKSEL0 &= ~(1 << 17);
  836.         PCLKSEL0 &= ~(1 << 16); // PCLK_SPI = CCLK/4
  837. }
  838.  
  839. void sd_spi_unsetup(void)
  840. {
  841.         PINSEL0 &= ~(1 << 11);
  842.         PINSEL0 &= ~(1 << 10); // GPIO P0.5 for SPI_CS
  843.         FIO0DIR &= ~(1 << 5); // input
  844.         PINSEL0 &= ~(1 << 13);
  845.         PINSEL0 &= ~(1 << 12); // P0.6 -> GPIO
  846.         FIO0DIR &= ~(1 << 6); // input
  847.         PINSEL0 &= ~(1 << 15);
  848.         PINSEL0 &= ~(1 << 14); // P0.7 -> GPIO
  849.         FIO0DIR &= ~(1 << 7); // input
  850.         PINSEL0 &= ~(1 << 17);
  851.         PINSEL0 &= ~(1 << 16); // P0.8 -> GPIO
  852.         FIO0DIR &= ~(1 << 8); // input
  853.         PINSEL0 &= ~(1 << 19);
  854.         PINSEL0 &= ~(1 << 18); // P0.9 -> GPIO
  855.         FIO0DIR &= ~(1 << 9); // input
  856.         PCONP &= ~(1 << 8); // disable the SPI
  857. }
  858.  
  859. void kempston_setup(void)
  860. {
  861.         PINSEL0 &= ~(1 << 23);
  862.         PINSEL0 &= ~(1 << 22); // p0.11 -> gpio (fire) D4
  863.         FIO0DIR &= ~(1 << 11);
  864.         FIO0MASK &= ~(1 << 11);
  865.         PINSEL4 &= ~(1 << 9);
  866.         PINSEL4 &= ~(1 << 8); // p2.4 -> gpio (down) D3
  867.         FIO2DIR &= ~(1 << 4);
  868.         FIO2MASK &= ~(1 << 4);
  869.         PINSEL4 &= ~(1 << 11);
  870.         PINSEL4 &= ~(1 << 10); // p2.5 -> gpio (up) D2
  871.         FIO2DIR &= ~(1 << 5);
  872.         FIO2MASK &= ~(1 << 5);
  873.         PINSEL4 &= ~(1 << 13);
  874.         PINSEL4 &= ~(1 << 12); // p2.6 -> gpio (right) D1
  875.         FIO2DIR &= ~(1 << 6);
  876.         FIO2MASK &= ~(1 << 6);
  877.         PINSEL4 &= ~(1 << 15);
  878.         PINSEL4 &= ~(1 << 14); // p2.7 -> gpio (left)   D0
  879.         FIO2DIR &= ~(1 << 7);
  880.         FIO2MASK &= ~(1 << 7);
  881. }
  882.  
  883. void adc_setup(void)
  884. {
  885.         PCLKSEL0 &= ~(1 << 25);
  886.         PCLKSEL0 |= (1 << 24);
  887.         PINSEL1 &= ~(1 << 15); // ad0[0]
  888.         PINSEL1 |= (1 << 14);
  889.         PINSEL1 &= ~(1 << 17); // ad0[1]
  890.         PINSEL1 |= (1 << 16);
  891.         PCONP |= (1 << 12); //Enable power to AD block
  892.         AD0CR = 0;
  893.         AD0CR |= (15 << 8);
  894.         AD0CR |= (1 << 21); //disable pdn mode
  895. }
  896.  
  897. void gpio_i2c_setup(void)
  898. {
  899.         PINSEL0 &= ~(1 << 0);
  900.         PINSEL0 &= ~(1 << 1);
  901.         PINSEL0 &= ~(3 << 0);
  902.         PINSEL0 &= ~(2 << 1);
  903.         FIO0DIR |= (1 << 0);
  904.         FIO0MASK &= ~(1 << 0);
  905.         FIO0SET |= (1 << 0);
  906.         FIO0DIR |=(1 << 1);
  907.         FIO0MASK &= ~(1 << 1);
  908.         FIO0SET |= (1 << 1);
  909. }
  910.  
  911. void rtc_setup(void)
  912. {
  913.         PCONP |= (1 << 9);
  914.         RTC_ILR = 0;
  915.         RTC_CCR = 17; // clk from external circuit
  916.         RTC_CIIR = 0;
  917.         RTC_CISS = 0;
  918.         RTC_AMR = 255;
  919. }
  920.  
  921. void led0_on(void) { FIO2SET |= 1; }
  922.  
  923. void led0_off(void) { FIO2CLR |= 1; }
  924.  
  925. void led1_on(void) { FIO2SET |= (1 << 1); }
  926.  
  927. void led1_off(void) { FIO2CLR |= (1 << 1); }
  928.  
  929. void additional_action(unsigned char unpush, unsigned char scode)
  930. {
  931. unsigned char bitnumber;
  932.        
  933.         if (!unpush && scode == 0x09) { scode = 0; scode = 0; WDCLKSEL = 1; WDTC = 1000; WDFEED = 0xAA; WDFEED = 0x55; WDMOD = 3; while(1); }
  934.         if (!unpush)
  935.         {
  936.                 if (shift_flag)
  937.                 {
  938.                         for(bitnumber = 0; shifted[bitnumber][0] != scode && shifted[bitnumber][0]; bitnumber++);
  939.                         if (shifted[bitnumber][0] == scode)  lastkey[0] = shifted[bitnumber][1];
  940.                 }
  941.                 else
  942.                 {
  943.                         for(bitnumber = 0; unshifted[bitnumber][0] != scode && unshifted[bitnumber][0]; bitnumber++);
  944.                         if (unshifted[bitnumber][0] == scode)  lastkey[0] = unshifted[bitnumber][1];   
  945.                 }
  946.         }
  947. }
  948.  
  949. DWORD get_fattime (void)
  950. {
  951. //      RTCTime rtc;
  952.  
  953. /* Get local time */
  954. //      rtc_gettime(&rtc);
  955.  
  956. /* Pack date and time into a DWORD variable */
  957. //      return    ((DWORD)(rtc.year - 1980) << 25)
  958. //                      | ((DWORD)rtc.mon << 21)
  959. //                      | ((DWORD)rtc.mday << 16)
  960. //                      | ((DWORD)rtc.hour << 11)
  961. //                      | ((DWORD)rtc.min << 5)
  962. //                      | ((DWORD)rtc.sec >> 1);
  963.         return    ((DWORD)(2023 - 1980) << 25)
  964.                         | ((DWORD)01 << 21)
  965.                         | ((DWORD)01 << 16)
  966.                         | ((DWORD)01 << 11)
  967.                         | ((DWORD)01 << 5)
  968.                         | ((DWORD)01 >> 1);
  969. }
  970.