;VS8x - TI Presentation Link Adapter Driver ; (C) 2010 by Brandon Wilson. All rights reserved. ;Portions in util.asm, usb8x.inc, equates.inc (C) Dan Englender. include "settings.inc" ;Specific settings for this application NOLIST include "ti83plus.inc" LIST include "equates.inc" ;Equates and macros to be used include "header.asm" GLOBALS ON include "usb8x.inc" ;usb8x equates SEGMENT MAIN Var USBDriverCall,32 ; 32; RAM calls for U_CALL and callback routine Var USBDriverBuf,128 ;128; Static buffer for USB driver's usage Var numOptions,1 ; 1; number of menu options Var firmwarePage,1 ; 1; page of firmware data Var firmwareAddress,2 ; 2; address of firmware data Var firmwareSize,4 ; 4; size of firmware EXTERN DispHexHL,DispHexA,IPutS,IPutC,PutSApp,INewLine,IGetKey ;----------------------------------------------------------------------------- Init: DisplayMainMenu: ;Display main menu ld hl,mainMenu DisplayMenu: push hl B_CALL ClrLCDFull B_CALL HomeUp pop hl call PutSApp call INewLine ld a,(hl) ld (numOptions),a ld b,a inc hl $$: call PutSApp call INewLine inc hl inc hl djnz $B $$: call IGetKey cp kQuit jr z,ExitApp cp kClear jr z,ExitApp cp k1 jp m,$B cp k9+1 jp p,$B sub k1-1 ld b,a ld a,(numOptions) cp b jr c,$B ld d,b ld hl,mainMenu ld bc,4000h xor a cpir $$: cpir inc hl inc hl dec d jr nz,$B dec hl ld d,(hl) dec hl ld e,(hl) ex de,hl jp (hl) UpgradeTIPresenter: ;Select Flash application containing firmware upgrade B_CALL ClrLCDFull B_CALL HomeUp ld hl,sSelectFirmware call PutSApp call INewLine B_CALL ZeroOP1 B_CALL FindAppUp jr c,DisplayMainMenu DisplayFirmware: ld hl,0105h ld (curRow),hl ld a,Lleft call IPutC ld hl,0405h ld (curRow),hl ld hl,OP1+1 call PutSApp B_CALL EraseEOL ld hl,0D05h ld (curRow),hl ld a,Lconvert call IPutC selectFirmwareKeyLoop: call IGetKey cp kEnter jr z,UpgradeFirmware cp kLeft jr z,doFirmwareLeft cp kRight jr z,doFirmwareRight jr selectFirmwareKeyLoop doFirmwareLeft: B_CALL FindAppDn jr DisplayFirmware doFirmwareRight: B_CALL FindAppUp jr DisplayFirmware UpgradeFirmware: B_CALL FindApp jr c,DisplayMainMenu ld (firmwarePage),a ld hl,4080h ld (firmwareAddress),hl ;Get the total size of the firmware ld hl,4002h ld de,appData ld bc,4 B_CALL FlashToRam ld ix,appData ld d,(ix+0) ld e,(ix+1) ld h,(ix+2) ld l,(ix+3) ld bc,122 call DEHLMinusBC ;DEHL now contains the size of the firmware ld (firmwareSize),hl ld (firmwareSize+2),de ;Upgrade TI Presenter firmware ld hl,headerPacket ld de,appData ld bc,16 ldir ld hl,upgradeDone call APP_PUSH_ERRORH B_CALL ClrLCDFull B_CALL HomeUp ld hl,sUpgrading call PutSApp call INewLine B_CALL RunIndicOn ;Send ready packet call SendReadyPacket ;Send hardware info packet call SendHardwareInfoPacket ;Send continue...I guess... call SendContinue ;Receive data packet (presumably hardware info) call Get4Bytes ld a,(header+1) cp 15h jr nz,invalidPacket ld hl,ioData call GetRestOfDataPacket ;Start sending the OS header and data packets ld bc,0 upgradeLoop: push bc ;Send the header packet ld hl,appData ld bc,10 ld a,06h call SendPacket ;I think this is a continue packet... call Get4Bytes ;Send an acknowledgement and move on call SendAck ;Send data packet call GetFirmwareData ld hl,appBackUpScreen ld bc,256 ld a,15h call SendPacket ld hl,(firmwareSize) ld de,(firmwareSize+2) ld bc,256 call DEHLMinusBC ld (firmwareSize),hl ld (firmwareSize+2),de ld a,l or h or e or d pop bc inc bc ld (appData+(headerPacketChunk-headerPacket)),bc jr nz,upgradeLoop ;Send End-Of-Transmission packet call SendEOT ld hl,sUpgraded call PutSApp call INewLine call APP_POP_ERRORH jr $F upgradeDone: ld hl,sError call PutSApp call INewLine $$: B_CALL RunIndicOff res indicOnly,(iy+indicFlags) ld hl,sPressAnyKey call PutSApp B_CALL GetKey jr DisplayMainMenu DEHLMinusBC: or a sbc hl,bc ret nc dec de ret GetFirmwareData: ;Get 256 bytes of firmware data to appData ld a,(firmwarePage) ld hl,(firmwareAddress) ld de,appBackUpScreen ld bc,256 call AppFlashToRam ld (firmwarePage),a ld (firmwareAddress),hl ret AppFlashToRam: ld ix,_AppFlashToRam RunRamCode: push af in a,(6) ld (appPage),a pop af push bc push de push hl push ix pop hl ld c,(hl) inc hl ld b,(hl) inc hl ld de,ramCode push de ldir pop ix pop hl pop de pop bc jp ramCode _AppFlashToRam: DW _AppFlashToRamEnd-_AppFlashToRamStart _AppFlashToRamStart: and 7Fh out (6),a bit 7,h jr nz,ldirRet _AppFlashToRamLoop: bit 7,h jr z,$F in a,(6) dec a out (6),a ld hl,4000h $$: ldi ld a,b or c jr nz,_AppFlashToRamLoop jr $F ldirRet: ldir $$: in a,(6) ld b,a ld a,(appPage) out (6),a ld a,b ret _AppFlashToRamEnd: DispHex16: ld b,16 $$: ld a,(hl) inc hl call DispHexA djnz $B ret headerPacket: DB 00h,01h,23h,00h,00h,00h,00h headerPacketChunk: DB 00h,00h DB 00h,00h,00h,00h,00h,00h,00h SendPacket: push hl push bc push af ld a,33h B_CALL SendAByte pop af B_CALL SendAByte pop bc push bc ld a,c B_CALL SendAByte pop bc push bc ld a,b B_CALL SendAByte pop bc pop hl ld de,0 ld (header+2),de $$: push hl push bc ld a,(hl) call SendChecksumByte pop bc pop hl inc hl dec bc ld a,b or c jr nz,$B ld a,(header+2) B_CALL SendAByte ld a,(header+3) B_CALL SendAByte jr Get4Bytes SendChecksumByte: ld hl,(header+2) ld b,0 ld c,a add hl,bc ld (header+2),hl B_CALL SendAByte ret invalidPacket: B_JUMP JError SendEOT: ld hl,9233h ld (header),hl ld hl,0 ld (header+2),hl call Send4Bytes jr Get4Bytes GetRestOfDataPacket: ld bc,(header+2) $$: push bc push hl B_CALL RecAByteIO pop hl ld (hl),a inc hl pop bc dec bc ld a,b or c jr nz,$B push hl B_CALL RecAByteIO pop hl ld (hl),a inc hl push hl B_CALL RecAByteIO pop hl ld (hl),a SendAck: ld hl,5633h ld (header),hl ld hl,0 ld (header+2),hl Send4Bytes: ld a,(header) B_CALL SendAByte ld a,(header+1) B_CALL SendAByte ld a,(header+2) B_CALL SendAByte ld a,(header+3) B_CALL SendAByte ret SendContinue: ld hl,0933h ld (header),hl ld hl,0 ld (header+2),hl call Send4Bytes jr Get4Bytes SendHardwareInfoPacket: ld hl,2D33h ld (header),hl ld hl,0 ld (header+2),hl call Send4Bytes jr Get4Bytes SendReadyPacket: ld hl,6800h ld (header),hl ld hl,0 ld (header+2),hl call Send4Bytes Get4Bytes: B_CALL Rec1stByteNC ld (header),a B_CALL RecAByteIO ld (header+1),a B_CALL RecAByteIO ld (header+2),a B_CALL RecAByteIO ld (header+3),a ret PicDemo: call Initializeusb8x jr nc,$F B_CALL ClrLCDFull B_CALL HomeUp ld hl,sNotInstalled call PutSApp call IGetKey jr DisplayMainMenu $$: ;Display Pic variable or default image B_CALL ClrLCDFull B_CALL HomeUp ;Initialize Presentation Link USB adapter call InitializePresentationLink jr c,ExitAppError ;Clear the screen call PresentationLinkClear ;Prompt for the Pic variable ld hl,sEnterPicNumber call IPutS B_CALL ZeroOP1 B_CALL GetKey cp k0 jr c,DisplayCheckeredPattern cp k9+1 jr nc,DisplayCheckeredPattern ;Display picture's data, if it exists sub k0 jr z,$F add a,tPic1-1 jr storePicName $$: ld a,tPic0 storePicName: ld (OP1+2),a ld a,PictObj ld (OP1),a ld a,tVarPict ld (OP1+1),a rst rFINDSYM jr c,DisplayCheckeredPattern push de ld hl,plotSScreen ld bc,768 B_CALL MemClear pop hl ld c,(hl) inc hl ld b,(hl) inc hl ld de,plotSScreen push de ldir pop hl call PresentationLinkFastCopy jr $F DisplayCheckeredPattern: ;Display a checkered test pattern instead ld hl,plotSScreen ld de,plotSScreen+1 ld (hl),10101010b ld bc,767 push hl ldir pop hl call PresentationLinkFastCopy $$: B_CALL NewLine ld hl,sSuccess call IPutS B_CALL GetKey jr DisplayMainMenu ExitApp: B_CALL ClrLCDFull B_CALL HomeUp B_CALL ClrTxtShd bit usb8xInitted,(iy+vs8xFlags) jr z,$F U_CALL HostKill U_CALL DriverKill $$: B_JUMP JForceCmdNoChar ExitAppNoDrv: B_CALL ClrLCDFull B_CALL HomeUp B_CALL ClrTxtShd B_JUMP JForceCmdNoChar ExitAppError: B_CALL ClrLCDFull B_CALL HomeUp ld a,'E' B_CALL PutC U_CALL GetErrorCode ld h,b ld l,c call DispHexHL B_CALL GetKey B_CALL ClrLCDFull B_CALL HomeUp B_CALL ClrTxtShd U_CALL HostKill U_CALL DriverKill B_JUMP JForceCmdNoChar DummyCallback: ret DummyCallbackEnd: mainMenu: DB "VS8x v",VER_STRING,0 DB 3 DB "1) Pic Demo",0 DW PicDemo DB "2) Upgrade",0 DW UpgradeTIPresenter DB "3) Quit",0 DW ExitApp sEnterPicNumber: DB "Enter Pic " DB "number ",LlBrack,"0-9]:",0 sSuccess: DB "Success! Press " DB "any key...",0 sSelectFirmware: DB "Select Flash " DB "application " DB "containing " DB "firmware update:",0 sUpgrading: DB "Upgrading",0CEh,0 sUpgraded: DB "Upgraded!",0 sPressAnyKey: DB "Press any key",0CEh,0 sError: DB "ERROR!",0 sNotInstalled: DB "usb8x does not " DB "appear to be on " DB "the calculator. " DB "Transfer it and " DB "try again.",0 Initializeusb8x: res usb8xInitted,(iy+vs8xFlags) ;Initialize usb8x, if it's currently oncalc ld hl,DummyCallback ld de,DummyCallbackLoc ld bc,DummyCallbackEnd-DummyCallback ldir U_CALL_INIT DummyCallbackLoc ret c ld hl,USBDriverBuf U_CALL DriverInit ret c set usb8xInitted,(iy+vs8xFlags) ret InitializePresentationLink: ;Initializes the USB Presentation Link adapter device and driver. ;Inputs: None ;Outputs: Returns carry flag set if problems ;Disable and kill the adapter if it's already connected and initialized res 3,(iy+41h) rst 28h DW 5311h xor a out (5Ah),a in a,(5Ah) ;Initialize the Presentation Link adapter U_CALL HostInit ret c ;Set the configuration ld b,1 U_CALL ConfigureDevice ret c ;Set up outgoing pipe ld bc,02h*256+pipeBulk ld de,2*256+0 U_CALL SetupOutPipe ;Initialize the "LCD driver" ld a,40h call WriteInstruction ld a,05h call WriteInstruction ld a,01h call WriteInstruction ld a,03h call WriteInstruction ld a,16h call WriteInstruction ld a,08h call WriteInstruction ld a,0C0h jr WriteInstruction PresentationLinkClear: ;Clears the Presentation Link adapter's display. ;Inputs: None ;Outputs: None ;Destroys plotSScreen. ;Returns carry flag set if problems ;Clear the display ld hl,plotSScreen ld de,plotSScreen+1 ld (hl),0 ld bc,767 push hl ldir pop hl ;NOTE: FALL THROUGH TO PresentationLinkFastCopy PresentationLinkFastCopy: ;Copies a 768-byte buffer to the Presentation Link adapter for displaying on a TV or projector. ;Inputs: HL => 768-byte buffer (such as plotSScreen) ;Outputs: Returns carry flag set if problems ld b,64 xor a call WriteInstruction ld a,40h call WriteInstruction ld a,05h call WriteInstruction ld a,01h call WriteInstruction ld a,07h call WriteInstruction ld a,80h-1 TIPFC_1: inc a push af push bc call WriteInstruction ld a,20h call WriteInstruction ld b,12 $$: ld a,(hl) call WriteData inc hl djnz $B pop bc pop af djnz TIPFC_1 ld a,05h ;NOTE: FALL THROUGH TO WriteInstruction WriteInstruction: ;Writes value to Presentation Link adapter's "LCD instruction" port. ;Inputs: A: value ;Outputs: Returns carry flag set if problems ;Preserves all registers (except A). push bc ld b,0 jr $F WriteData: ;Writes value to Presentation Link adapter's "LCD data" port. ;Inputs: A: value ;Outputs: Returns carry flag set if problems ;Preserves all registers (except A). push bc ld b,1 $$: call 000Bh push hl push de ld hl,appData ld (hl),a inc hl ld (hl),b dec hl ld b,2 ld c,02h U_CALL SendData pop de pop hl pop bc ret