; eBASIC PIC16 Code Generator ; Copyright(c) 2004, Compubotics LLC ; www.Compubotics.com ; Symbol definitions INDF EQU 0x0 TMR0 EQU 0x1 PCL EQU 0x2 STATUS EQU 0x3 FSR EQU 0x4 PORTA EQU 0x5 PORTB EQU 0x6 PORTC EQU 0x7 PORTD EQU 0x8 PORTE EQU 0x9 PCLATH EQU 0xa INTCON EQU 0xb PIR1 EQU 0xc PIR2 EQU 0xd TMR1 EQU 0xe TMR1L EQU 0xe TMR1H EQU 0xf T1CON EQU 0x10 TMR2 EQU 0x11 T2CON EQU 0x12 SSPBUF EQU 0x13 SSPCON EQU 0x14 CCPR1L EQU 0x15 CCPR1H EQU 0x16 CCP1CON EQU 0x17 RCSTA EQU 0x18 TXREG EQU 0x19 RCREG EQU 0x1a CCPR2L EQU 0x1b CCPR2H EQU 0x1c CCP2CON EQU 0x1d ADRESH EQU 0x1e ADCON0 EQU 0x1f OPTION_REG EQU 0x81 TRISA EQU 0x85 TRISB EQU 0x86 TRISC EQU 0x87 TRISD EQU 0x88 TRISE EQU 0x89 PIE1 EQU 0x8c PIE2 EQU 0x8d PCON EQU 0x8e SSPCON2 EQU 0x91 PR2 EQU 0x92 SSPADD EQU 0x93 TXSTA EQU 0x98 SPBRG EQU 0x99 ADRESL EQU 0x9e ADCOM1 EQU 0x9f EEDATA EQU 0x10c EEADR EQU 0x10d EEDATH EQU 0x10e EEADRH EQU 0x10f EECON1 EQU 0x18c EECON2 EQU 0x18d _rega EQU 0x70 _regb EQU 0x72 _regc EQU 0x74 _regx EQU 0x76 _regsp EQU 0x78 _regs EQU 0x79 DIRS EQU 0x7a _sav_w EQU 0x7c _sav_status EQU 0x7d _sav_pclath EQU 0x7e OUTS EQU 0x6 TRISP EQU 0x86 RAND EQU 0x20 SEED EQU 0x22 CTR EQU 0x24 _stack EQU 0x60 _sysmem EQU 0x50 ORG 0x0 ; File: random.bas ; ' eBASIC Sample Program ; ' ; ' Random number generation ; ' ; ' Compubotics LLC ; ; ; #device "16f877" ; #clock 16000000 ; ; RAND VAR WORD ; SEED VAR WORD ; CTR var BYTE ; ; MAIN: nop clrf 0xa goto _startup ORG 0x4 retfie _STR001: retlw 0x52 retlw 0x41 retlw 0x4e retlw 0x44 retlw 0x4f retlw 0x4d retlw 0x20 retlw 0x4e retlw 0x75 retlw 0x6d retlw 0x62 retlw 0x65 retlw 0x72 retlw 0x20 retlw 0x47 retlw 0x65 retlw 0x6e retlw 0x65 retlw 0x72 retlw 0x61 retlw 0x74 retlw 0x69 retlw 0x6f retlw 0x6e retlw 0x0 _STR002: retlw 0x4e retlw 0x65 retlw 0x77 retlw 0x20 retlw 0x73 retlw 0x65 retlw 0x65 retlw 0x64 retlw 0x20 retlw 0x3d retlw 0x0 _STR003: retlw 0x53 retlw 0x65 retlw 0x71 retlw 0x75 retlw 0x65 retlw 0x6e retlw 0x63 retlw 0x65 retlw 0x20 retlw 0x63 retlw 0x6f retlw 0x6d retlw 0x70 retlw 0x6c retlw 0x65 retlw 0x74 retlw 0x65 retlw 0x0 _startup: movlw 0x83 bsf 0x3,5 movwf 0x81 bcf 0xb,5 movlw 0x39 bcf 0x3,5 movwf 0x10 movlw 0x60 movwf 0x78 call _uartSetup goto Main ; ; debug CR, "RANDOM Number Generation", CR MAIN: movlw 0xd call _uartTX movlw 0xa call _uartTX movlw 0x5 movwf 0x70 movlw 0x0 movwf 0x71 call _dbgStr movlw 0xd call _uartTX movlw 0xa call _uartTX ; ; while (1) ; ; ' get new seed value from TMR0 ; SEED = TMR0 _j000: bcf 0x3,5 bcf 0x3,6 movf 0x1,0 movwf 0x70 clrf 0x71 movf 0x70,0 movwf 0x22 movf 0x71,0 movwf 0x23 ; SEED = SEED * 3 movf 0x22,0 movwf 0x70 movf 0x23,0 movwf 0x71 movlw 0x3 movwf 0x72 movlw 0x0 movwf 0x73 call _mult16a movf 0x70,0 movwf 0x22 movf 0x71,0 movwf 0x23 ; ; debug CR, "New seed =", dec SEED, CR movlw 0xd call _uartTX movlw 0xa call _uartTX movlw 0x1e movwf 0x70 movlw 0x0 movwf 0x71 call _dbgStr movf 0x22,0 movwf 0x70 movf 0x23,0 movwf 0x71 call _dbgDec movlw 0xd call _uartTX movlw 0xa call _uartTX ; ; CTR = 0 movlw 0x0 movwf 0x24 ; ; ' generate 10 random numbers from seed ; while (CTR < 10) _j002: movf 0x24,0 movwf 0x70 clrf 0x71 movlw 0xa movwf 0x72 movlw 0x0 movwf 0x73 call _lcomplt16 clrf 0x72 clrf 0x73 call _comp16 btfsc 0x3,2 goto _j003 ; ; RANDOM SEED ' generate random number movf 0x22,0 movwf 0x70 movf 0x23,0 movwf 0x71 call _rand16 movf 0x70,0 movwf 0x22 movf 0x71,0 movwf 0x23 ; RAND = SEED // 10 ' put in the range of 0-9 movf 0x22,0 movwf 0x70 movf 0x23,0 movwf 0x71 movlw 0xa movwf 0x72 movlw 0x0 movwf 0x73 call _div16x movf 0x70,0 movwf 0x20 movf 0x71,0 movwf 0x21 ; debug dec RAND,cr ' display it movf 0x20,0 movwf 0x70 movf 0x21,0 movwf 0x71 call _dbgDec movlw 0xd call _uartTX movlw 0xa call _uartTX ; ; paUSE 997 movlw 0xe5 movwf 0x70 movlw 0x3 movwf 0x71 call _pause ; ; CTR = CTR + 1 movf 0x24,0 movwf 0x70 clrf 0x71 movlw 0x1 movwf 0x72 movlw 0x0 movwf 0x73 call _add16a movf 0x70,0 movwf 0x24 ; ; WEND goto _j002 ; ; debug CR, "Sequence complete", CR _j003: movlw 0xd call _uartTX movlw 0xa call _uartTX movlw 0x29 movwf 0x70 movlw 0x0 movwf 0x71 call _dbgStr movlw 0xd call _uartTX movlw 0xa call _uartTX ; ; wend goto _j000 ; ; END _j001: sleep goto $-1 _uartSetup: movlw 0x67 bsf 0x3,5 bcf 0x3,6 movwf 0x99 bsf 0x98,2 bsf 0x98,5 bcf 0x3,5 bsf 0x18,4 bsf 0x18,7 return _uartTX: bcf 0x3,5 bcf 0x3,6 btfss 0xc,4 goto $-1 movwf 0x19 return _inc16: incfsz 0x70,0 decf 0x71,1 incf 0x71,1 movwf 0x70 iorwf 0x71,0 return _dbgStr: call _j004 iorlw 0x0 btfsc 0x3,2 return call _uartTX call _inc16 goto _dbgStr _j004: movf 0x71,0 movwf 0xa movf 0x70,0 movwf 0x2 _add16x: movf 0x70,0 addwf 0x76,1 movf 0x71,0 btfsc 0x3,0 incf 0x71,0 addwf 0x77,1 return _MULT16A: clrf 0x76 clrf 0x77 clrf 0x74 clrf 0x75 movf 0x70,0 iorwf 0x71,0 btfss 0x3,2 goto _j005 retlw 0x0 _j005: movf 0x72,0 iorwf 0x73,0 btfsc 0x3,2 goto _j008 btfss 0x72,0 goto _j007 _j006: call _add16x btfss 0x3,0 goto _j007 rlf 0x74,1 rlf 0x75,1 _j007: bcf 0x3,0 rrf 0x73,1 rrf 0x72,1 bcf 0x3,0 rlf 0x70,1 rlf 0x71,1 goto _j005 _j008: movf 0x76,0 movwf 0x70 movf 0x77,0 movwf 0x71 retlw 0x0 _push: bcf 0xb,7 movwf 0x79 movf 0x78,0 movwf 0x4 movf 0x79,0 movwf 0x0 incf 0x78,1 bsf 0xb,7 return _pop: bcf 0xb,7 decf 0x78,1 movf 0x78,0 movwf 0x4 movf 0x0,0 bsf 0xb,7 return _shftl16: bcf 0x3,0 rlf 0x72,1 rlf 0x73,1 decfsz 0x74,1 goto _shftl16 return _shftr16: bcf 0x3,0 rrf 0x73,1 rrf 0x72,1 decfsz 0x74,1 goto _shftr16 return _sub16a: movf 0x72,0 subwf 0x70,1 movf 0x73,0 btfss 0x3,0 incf 0x73,0 subwf 0x71,1 return _add16a: movf 0x72,0 addwf 0x70,1 movf 0x73,0 btfsc 0x3,0 incf 0x73,0 addwf 0x71,1 return _div16x: clrf 0x76 clrf 0x77 movf 0x72,0 iorwf 0x73,0 btfsc 0x3,2 retlw 0xff movlw 0x1 movwf 0x74 movf 0x73,0 iorlw 0x0 btfss 0x3,2 goto _j009 movf 0x72,0 xorlw 0x1 btfss 0x3,2 goto _j009 movf 0x70,0 movwf 0x76 movf 0x71,0 movwf 0x77 clrf 0x70 clrf 0x71 retlw 0x0 _j009: movf 0x70,0 iorwf 0x71,0 btfss 0x3,2 goto _j010 retlw 0x0 _j010: btfsc 0x73,7 goto _j011 incf 0x74,1 movf 0x74,0 call _push movlw 0x1 movwf 0x74 call _shftl16 call _pop movwf 0x74 goto _j010 _j011: movf 0x72,0 call _push movf 0x73,0 call _push movf 0x76,0 movwf 0x72 movf 0x77,0 movwf 0x73 movf 0x74,0 call _push movlw 0x1 movwf 0x74 call _shftl16 call _pop movwf 0x74 movf 0x72,0 movwf 0x76 movf 0x73,0 movwf 0x77 call _pop movwf 0x73 call _pop movwf 0x72 call _sub16a btfsc 0x3,0 goto _j012 call _add16a goto _j013 _j012: bsf 0x76,0 _j013: decf 0x74,1 btfsc 0x3,2 retlw 0x0 movf 0x74,0 call _push movlw 0x1 movwf 0x74 call _shftr16 call _pop movwf 0x74 goto _j011 _compza: movlw 0x0 iorwf 0x71,0 btfss 0x3,2 return iorwf 0x70,1 return _neg16: comf 0x70,1 comf 0x71,1 goto _INC16 _dbgSDec: btfss 0x71,7 goto _dbgDec movlw 0x2d call _uartTX call _neg16 _dbgDec: clrf 0x50 clrf 0x51 clrf 0x52 clrf 0x53 clrf 0x54 movlw 0x5 movwf 0x75 _j014: decf 0x75,1 movlw 0xa movwf 0x72 clrf 0x73 call _div16x movlw 0x50 addwf 0x75,0 movwf 0x4 movf 0x70,0 addlw 0x30 movwf 0x0 movf 0x76,0 movwf 0x70 movf 0x77,0 movwf 0x71 call _compza btfsc 0x3,2 goto _j015 movf 0x75,0 iorlw 0x0 btfss 0x3,2 goto _j014 _j015: movlw 0x0 movwf 0x74 _j016: movlw 0x50 addwf 0x74,0 movwf 0x4 movf 0x0,0 btfss 0x3,2 call _uartTX incf 0x74,1 movf 0x74,0 xorlw 0x5 btfss 0x3,2 goto _j016 return _comp16: movf 0x73,0 subwf 0x71,0 btfss 0x3,2 return movf 0x72,0 subwf 0x70,0 return _lcomplt16: call _comp16 btfss 0x3,0 goto _j017 _j018: clrf 0x70 clrf 0x71 return _j017: movlw 0x1 movwf 0x70 clrf 0x71 return _lcomplteq16: call _comp16 btfss 0x3,2 goto _lcomplt16 goto _j017 _lcompgteq16: call _comp16 btfss 0x3,2 goto _LCOMPGT16 goto _j017 _lcompgt16: call _comp16 btfsc 0x3,2 goto _j018 btfsc 0x3,0 goto _j017 goto _j018 _LCOMPNE16: call _comp16 btfss 0x3,2 goto _j018 goto _j017 _LNOT16: clrf 0x70 clrf 0x71 call _comp16 btfss 0x3,2 goto _j017 goto _j018 _rand16: movlw 0xd movwf 0x72 clrf 0x73 call _mult16a movlw 0xeb movwf 0x72 movlw 0xc movwf 0x73 call _add16a return _dec16: decf 0x70,1 incfsz 0x70,0 incf 0x71,1 decf 0x71,1 movf 0x70,0 iorwf 0x71,0 return _pause: bcf 0xb,2 btfss 0xb,2 goto $-1 call _dec16 btfss 0x3,2 goto _pause return END ; end of code generation