;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PIC16F628A. This file contains the basic code              *
;   building blocks to build upon.                                    *
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PIC data sheet for additional             *
;   information on the instruction set.                               *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    xxx.asm                                           *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:                                                          *
;    Company:                                                         *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P16F628A.INC                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************

	list      p=16f628A           ; list directive to define processor
	#include <p16F628A.inc>       ; processor specific variable definitions

	errorlevel  -302              ; suppress message 302 from list file

__CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT 

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.




;***** VARIABLE DEFINITIONS
w_temp        EQU     0x7E        ; variable used for context saving 
status_temp   EQU     0x7F        ; variable used for context saving
N0	EQU	020h
N1	EQU	021h
zad	EQU	022h
AA	EQU	023h
AB	EQU	024h
AC	EQU	025h
AD	EQU	026h
AE	EQU	027h




;**********************************************************************
	ORG     0x000             ; processor reset vector
	goto    main              ; go to beginning of program
	

	ORG     0x004             ; interrupt vector location
	movwf   w_temp            ; save off current W register contents
	movf	STATUS,w          ; move status register into W register
	movwf	status_temp       ; save off contents of STATUS register

; isr code can go here or be located as a call subroutine elsewhere


	movf    status_temp,w     ; retrieve copy of STATUS register
	movwf	STATUS            ; restore pre-isr STATUS register contents
	swapf   w_temp,f
	swapf   w_temp,w          ; restore pre-isr W register contents
	retfie                    ; return from interrupt


main
	MOVLW	B'00001111'
	MOVWF	TRISB
	MOVLW	B'00000000'
	MOVWF	TRISA
	MOVLW	B'00000000'
	MOVWF	INTCON
	BSF		PORTA,0
	MOVLW	B'00000000'
	MOVWF	ANSEL
	GOTO	prer1
srav
	BCF	AB,7
	BCF	AB,6
	MOVF	AB,0
	XORWF	AC,0
	BTFSS	STATUS,Z
	GOTO	nepravilno
	DECFSZ	AE,1
	MOVF	AA
	ADDWF	AB,1
	MOVF	AB,0
	MOVWF	AC
A1
	CALL	rasshifrovka1
	BCF	PORTA,1
	CALL	Otobrajenie
	BSF	PORTA,1
	CALL	rasshifrovka2
	BCF	PORTA,2
	CALL	Otobrajenie
	BSF	PORTA,2
	CALL	rasshifrovka3
	BCF	PORTA,3
	CALL	Otobrajenie
	BSF	PORTA,3
	CALL	rasshifrovka4
	BCF	PORTA,4
	CALL	Otobrajenie
	BSF	PORTA,4
	CALL	zaderjka
	BCF	PORTA,0
	BSF	PORTA,0
	GOTO	prer1
nepravilno
	ADDWF	AA,1
	MOVLW	010h
	MOVWF	AE
	MOVF	AA
	ADDWF	AB,1
	MOVF	AB,0
	MOVWF	AC
	GOTO	A1

zaderjka
	MOVLW	0C4h
	MOVWF	N0
cyl1
	MOVLW	0FFh
	MOVWF	N1
cyl2
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	DECFSZ	N0,1
	GOTO	cyl2
	DECFSZ	N1,1
	GOTO	cyl1

prer1
	BTFSC	PORTB,4
	GOTO	prer1_00
	BTFSC	PORTB,5
	GOTO	prer1_01
	BTFSC	PORTB,6
	GOTO	prer1_10
	BTFSC	PORTB,7
	GOTO	prer1_11
	GOTO	prer1
;..........................
prer2
	BTFSC	PORTB,4
	GOTO	prer2_00
	BTFSC	PORTB,5
	GOTO	prer2_01
	BTFSC	PORTB,6
	GOTO	prer2_10
	BTFSC	PORTB,7
	GOTO	prer2_11
	GOTO	prer1
;...........................
prer3
	BTFSC	PORTB,4
	GOTO	prer3_00
	BTFSC	PORTB,5
	GOTO	prer3_01
	BTFSC	PORTB,6
	GOTO	prer3_10
	BTFSC	PORTB,7
	GOTO	prer3_11
	GOTO	prer3
;..........................
prer4
	BTFSC	PORTB,4
	GOTO	prer4_00
	BTFSC	PORTB,5
	GOTO	prer4_01
	BTFSC	PORTB,6
	GOTO	prer4_10
	BTFSC	PORTB,7
	GOTO	prer4_11
	GOTO	prer4
;/////////////////////////////////////////////
prer1_00
	BCF	AC,0
	BCF	AC,1
	CALL	rasshifrovka1
	BCF	PORTA,1
	CALL	Otobrajenie
	BSF	PORTA,1
	call	otp
	GOTO prer2

prer1_01
	BCF	AC,0
	BSF	AC,1
	CALL	rasshifrovka1
	BCF	PORTA,1
	CALL	Otobrajenie
	BSF	PORTA,1
	call	otp
	GOTO prer2

prer1_10
	BSF	AC,0
	BCF	AC,1
	CALL	rasshifrovka1
	BCF	PORTA,1
	CALL	Otobrajenie
	BSF	PORTA,1
	call	otp
	GOTO prer2

prer1_11
	BSF	AC,0
	BSF	AC,1
	CALL	rasshifrovka1
	BCF	PORTA,1
	CALL	Otobrajenie
	BSF	PORTA,1
	call	otp
	GOTO prer2
;.........................
prer2_00
	BCF	AC,2
	BCF	AC,3
	CALL	rasshifrovka2
	BCF	PORTA,2
	CALL	Otobrajenie
	BSF	PORTA,2
	call	otp
	GOTO prer3

prer2_01
	BCF	AC,2
	BSF	AC,3
	CALL	rasshifrovka2
	BCF	PORTA,2
	CALL	Otobrajenie
	BSF	PORTA,2
	call	otp
	GOTO prer3

prer2_10
	BSF	AC,2
	BCF	AC,3
	CALL	rasshifrovka2
	BCF	PORTA,2
	CALL	Otobrajenie
	BSF	PORTA,2
	call	otp
	GOTO prer3

prer2_11
	BSF	AC,2
	BSF	AC,3
	CALL	rasshifrovka2
	BCF	PORTA,2
	CALL	Otobrajenie
	BSF	PORTA,2
	call	otp
	GOTO prer3
;.............................
prer3_00
	BCF	AC,4
	BCF	AC,5
	CALL	rasshifrovka3
	BCF	PORTA,3
	CALL	Otobrajenie
	BSF	PORTA,3
	call	otp
	GOTO prer4

prer3_01
	BCF	AC,4
	BSF	AC,5
	CALL	rasshifrovka3
	BCF	PORTA,3
	CALL	Otobrajenie
	BSF	PORTA,3
	call	otp
	GOTO prer4

prer3_10
	BSF	AC,4
	BCF	AC,5
	CALL	rasshifrovka3
	BCF	PORTA,3
	CALL	Otobrajenie
	BSF	PORTA,3
	call	otp
	GOTO prer4

prer3_11
	BSF	AC,4
	BSF	AC,5
	CALL	rasshifrovka3
	BCF	PORTA,3
	CALL	Otobrajenie
	BSF	PORTA,3
	call	otp
	GOTO prer4
;..........................
prer4_00
	BCF	AC,4
	BCF	AC,5
	CALL	rasshifrovka4
	BCF	PORTA,4
	CALL	Otobrajenie
	BSF	PORTA,4
	call	otp
	GOTO srav

prer4_01
	BCF	AC,4
	BSF	AC,5
	CALL	rasshifrovka4
	BCF	PORTA,4
	CALL	Otobrajenie
	BSF	PORTA,4
	call	otp
	GOTO srav

prer4_10
	BSF	AC,4
	BCF	AC,5
	CALL	rasshifrovka4
	BCF	PORTA,4
	CALL	Otobrajenie
	BSF	PORTA,4
	call	otp
	GOTO srav

prer4_11
	BSF	AC,4
	BSF	AC,5
	CALL	rasshifrovka4
	BCF	PORTA,4
	CALL	Otobrajenie
	BSF	PORTA,4
	call	otp
	GOTO srav

otp
	BTFSC	PORTB,4
	GOTO	otp
	BTFSC	PORTB,5
	GOTO	otp
	BTFSC	PORTB,6
	GOTO	otp
	BTFSC	PORTB,7
	GOTO	otp
	RETURN

Otobrajenie
	BTFSC	AD,0
	BSF	PORTA,5
	BSF	PORTA,7 ;"задвигает" информационный бит
	BCF	PORTA,7 ;готовит к "задвижению" следующего бита
	BCF	PORTA,5
	BTFSC	AD,1
	BSF	PORTA,5
	BSF	PORTA,7
	BCF	PORTA,7
	BCF	PORTA,5
	BTFSC	AD,2
	BSF	PORTA,5
	BSF	PORTA,7
	BCF	PORTA,7
	BCF	PORTA,5
	BTFSC	AD,3
	BSF	PORTA,5
	BSF	PORTA,5
	BSF	PORTA,7
	BCF	PORTA,7
	BCF	PORTA,5
	BTFSC	AD,4
	BSF	PORTA,5
	BSF	PORTA,7
	BCF	PORTA,7
	BCF	PORTA,5
	BTFSC	AD,5
	BSF	PORTA,5
	BSF	PORTA,7
	BCF	PORTA,7
	BCF	PORTA,5
	BTFSC	AD,6
	BSF	PORTA,5
	BSF	PORTA,7
	BCF	PORTA,7
	BCF	PORTA,5
	BSF	PORTB,0
	BCF	PORTB,0
	RETURN

rasshifrovka1
	BTFSS	39,0
	GOTO	opr1_1x
	GOTO	opr1_0x
opr1_0x	
	BTFSS	39,1
	GOTO	opr01
	GOTO	opr00
opr1_1x
	BTFSS	39,1
	GOTO	opr11
	GOTO	opr10
;///////////////////////////
rasshifrovka2
	BTFSS	39,2
	GOTO	opr2_1x
	GOTO	opr2_0x
opr2_0x	
	BTFSS	39,3
	GOTO	opr01
	GOTO	opr00
opr2_1x
	BTFSS	39,3
	GOTO	opr11
	GOTO	opr10
;//////////////////////////////////
rasshifrovka3
	BTFSS	39,4
	GOTO	opr3_1x
	GOTO	opr3_0x
opr3_0x	
	BTFSS	39,5
	GOTO	opr01
	GOTO	opr00
opr3_1x
	BTFSS	39,5
	GOTO	opr11
	GOTO	opr10
;////////////////////////////////////
rasshifrovka4
	BTFSS	39,6
	GOTO	opr4_1x
	GOTO	opr4_0x
opr4_0x	
	BTFSS	39,7
	GOTO	opr01
	GOTO	opr00
opr4_1x
	BTFSS	39,7
	GOTO	opr11
	GOTO	opr10
;/////////////////////////////////
opr00
	MOVLW	02Dh
	MOVWF	40
	RETURN
opr01
	MOVLW	057h
	MOVWF	40
	RETURN
opr10
	MOVLW	026h
	MOVWF	40
	RETURN
opr11
	MOVLW	06Bh
	MOVWF	40
	RETURN

	goto	main		  ;loop forever, remove this instruction, for test only


; initialize eeprom locations

	ORG	0x2100
	DE	0x00, 0x01, 0x02, 0x03


	END                       ; directive 'end of program'

