Subversion Repositories pentevo

Rev

Rev 71 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include "mytypes.h"
  4. #include "ps2.h"
  5. #include "zx.h"
  6. #include "pins.h"
  7. #include "spi.h"
  8.  
  9.  
  10. volatile UWORD ps2_shifter;
  11. volatile UBYTE ps2_count;
  12. volatile UBYTE ps2_timeout;
  13.  
  14.  
  15.  
  16.  
  17. void ps2_task(void)
  18. {
  19.         UBYTE byte;
  20.  
  21.         if( ps2_count!=0 ) return; // not received anything
  22.  
  23.         byte = ps2_decode();
  24.         if( byte ) // there is no zero byte in scancode tables so we can ignore and use it as 'nothing received'
  25.         {
  26.                 ps2_parse(byte);
  27.         }
  28.  
  29.  
  30.         ps2_count = 11; // re-init shift counter
  31.  
  32.         //release ps2 receiver (disabled by now)
  33.         EIFR = (1<<INTF4); // clr any spurious int which can happen when we pulldown clock pin
  34.         PS2KBCLK_DDR  &= ~(1<<PS2KBCLK);
  35.         PS2KBCLK_PORT |= (1<<PS2KBCLK);  //release clk pin
  36. }
  37.  
  38.  
  39.  
  40. UBYTE ps2_decode(void)
  41. {
  42.         UBYTE t,byte;
  43.  
  44.         if( ps2_count!=0 ) return 0x00; // have nothing received
  45.  
  46.         // check packet:
  47.         //ps2_shifter.hi - stp.par.7.6.5.4.3.2
  48.         //ps2_shifter.lo - 1.0.strt.x.x.x.x.x
  49.  
  50.         if( !( ps2_shifter&0x8000 ) ) return 0x00; // stopbit must be 1
  51.         if( ps2_shifter&0x0020 ) return 0x00; // startbit must be 0
  52.  
  53.  
  54.         byte = (UBYTE) ( 0x00FF & (ps2_shifter>>6) );
  55.  
  56.         t = byte ^ (byte>>4);
  57.         t = t ^ (t>>2);
  58.         t = t ^ (t>>1); // parity
  59.  
  60.         t = t ^ (UBYTE) ( ps2_shifter>>14 ); // compare parities
  61.  
  62.         if( !(t&1) ) return 0x00; // must be different
  63.  
  64.         return byte;
  65. }
  66.  
  67.  
  68. void ps2_parse(UBYTE recbyte)
  69. {
  70.         static UBYTE was_release = 0;
  71.         static UBYTE was_E0 = 0;
  72.  
  73.         static UBYTE last_scancode = 0;
  74.         static UBYTE last_scancode_E0 = 1;
  75.  
  76.         static UBYTE skipshit = 0;
  77.  
  78.  
  79.  
  80.         if( skipshit )
  81.         {
  82.                 skipshit--;
  83.                 return;
  84.         }
  85.  
  86.  
  87.         if( recbyte==0xFA ) return;
  88.         if( recbyte==0xFE ) return;
  89.         if( recbyte==0xEE ) return;
  90.         if( recbyte==0xAA ) return;
  91.  
  92.  
  93.         if( recbyte==0xE0 )
  94.         {
  95.                 was_E0 = 1;
  96.                 return;
  97.         }
  98.  
  99.  
  100.         if( recbyte==0xF0 )
  101.         {
  102.                 was_release = 1;
  103.                 return;
  104.         }
  105.  
  106.         if( recbyte==0xE1 ) // pause pressed
  107.         {
  108.                 skipshit=7;
  109.                 return; // skip next 7 bytes
  110.         }
  111.  
  112.  
  113.         if( (recbyte==last_scancode) && (was_E0==last_scancode_E0) )
  114.         {
  115.                 if( was_release )
  116.                 {
  117.                         last_scancode = 0x00;
  118.                         last_scancode_E0 = 1; // impossible scancode: E0 00
  119.                 }
  120.                 else // was depress
  121.                 {
  122.                         return;
  123.                 }
  124.         }
  125.  
  126.         if( !was_release )
  127.         {
  128.                 last_scancode = recbyte;
  129.                 last_scancode_E0 = was_E0;
  130.         }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.         if( (recbyte==0x12) && was_E0 ) // skip E0 12
  138.         {
  139.                 was_E0 = 0;
  140.                 was_release = 0;
  141.                 return;
  142.         }
  143.  
  144.  
  145.         to_zx( recbyte, was_E0, was_release ); // send valid scancode to zx decoding stage
  146.  
  147.         was_E0 = 0;
  148.         was_release = 0;
  149.  
  150.         return;
  151. }
  152.  
  153.