/******************************************************************************* *hostutils.c Robert Buels, rmb32@cornell.edu *ps/2 host emulator, useful in debuggings ps2.c *April, 2002 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * ******************************************************************************/ #include <90s8515.h> #include #asm .equ PORTA=0x1b .equ PORTB=0x18 .equ PINA=0x19 .equ DDRA=0x1a .def temp=r17 .def delay=r18 .def output=r19 .def input=r19 .def temp2=r20 .def parity=r21 .def ptemp=r22 .equ CLOCK=PORTA,1 .MACRO __SETDATA cbi DDRA,0 ;DDRA.0 = 0 sbi PORTA,0 ;PORTA.0 = 1 .ENDM .MACRO __CLEARDATA sbi DDRA,0 ;DDRA.0 = 1 cbi PORTA,0 ;PORTA.0 = 0 .ENDM .MACRO __SETCLOCK cbi DDRA,1 sbi PORTA,1 .ENDM .MACRO __CLEARCLOCK sbi DDRA,1 cbi PORTA,1 .ENDM #endasm #pragma regalloc- #define TIMER0RELOAD 131 unsigned char buttonState; unsigned char deltaX, deltaY, deltaZ; unsigned char hostCommand,mouseResponse; unsigned char sendResult; char transmitMode; unsigned char sampleTime,sampleReload; #pragma regalloc+ void receiveByte(); void sendData(); void sendByte(); void processCommand(); void readN64(); void init() { PORTA = 0xff; DDRA = 0x00; //port A: PS2 in-out DDRB = 0xff; //port B: control to 1200 DDRC = 0x00; DDRD = 0x00; //port D: input from 1200 sampleTime = 10; sampleReload = 10; // buttonState = deltaX = deltaY = 0b01001011; } void main() { init(); while(1) { /* #asm("__CLEARCLOCK") while(!(~PIND)) PORTB=~0x01; #asm("__CLEARDATA") delay_us(84); #asm("__SETCLOCK") delay_us(5); while(PINA.1) { PORTB=~0x0A; } #asm("__SETDATA") */ PORTB=~0x01; while(!(~PIND)) {} PORTB=~0xFF; switch (~PIND) { case 0x01: hostCommand = 0xFF; break; //reset case 0x02: hostCommand = 0xF4; break; //enable data reporting case 0x04: hostCommand = 0xF5; break; //disable data reporting case 0x08: hostCommand = 0xF3; break; //set sample rate case 0x10: hostCommand = 0xE8; break; //get device ID case 0x20: hostCommand = 0x00; break; //200 case 0x40: hostCommand = 0x01; break; //100 case 0x80: hostCommand = 0x02; break; //80 default: hostCommand = 0xFF; break; } sendByte(); delay_ms(500); } } void sendByte() { #asm("__CLEARCLOCK") delay_ms(200); #asm lds output,_hostCommand __CLEARDATA __DELAY_USB 112 ; delay 84 us like the Dell computers in the lab __SETCLOCK ldi delay,0xFF out PORTB,delay mov temp,output ldi temp2,8 ByteOutLoop: rcall __WaitForClockLow rcall __OutBit lsr temp rcall __WaitForClockHigh dec temp2 brne ByteOutLoop ;; calculate parity mov temp,output mov parity,output swap parity eor temp,parity mov parity,temp lsr temp lsr temp eor temp,parity mov parity,temp lsr temp eor temp,parity ; now lsb of parity is even parity com temp ; invert it to get odd parity rcall __WaitForClockLow rcall __OutBit ; send our parity rcall __WaitForClockHigh rcall __WaitForClockLow rjmp ByteOutEnd ;;outputs a single bit to the ps2 line as a host would __OutBit: sbrs temp,0 rjmp OutBitClear rjmp OutBitSet OutBitClear: __CLEARDATA rjmp OutBit2: OutBitSet: __SETDATA OutBit2: ret ByteOutEnd: __SETDATA #endasm } #asm __WaitForClockLow: __DELAY_USB 7 ; 5 us WaitForClockLowLoop: sbic PINA,1 rjmp WaitForClockLowLoop ret __WaitForClockHigh: __DELAY_USB 7 ; 5 us WaitForClockHighLoop: sbis PINA,1 rjmp WaitForClockHighLoop ret #endasm