📄 dessamp.bak
字号:
;-----------------------------------------------------------------------------
; DES sample code
; Copyright 1994, 1996, 1997 Eric L. Smith
;
; $Header$
;-----------------------------------------------------------------------------
do_serial equ 1
do_parallel equ 0
echo_input equ 1
do_parity equ 0
single_des equ 1
triple_des equ 1
processor 16f84
include "p16f84.inc"
__CONFIG _CP_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC
radix dec
errorlevel -224
errorlevel -305
;-----------------------------------------------------------------------------
; memory map
;-----------------------------------------------------------------------------
rambase equ 0x0c
rombase equ 0x000
;sboxbase equ 0x300
;-----------------------------------------------------------------------------
; port bits
;-----------------------------------------------------------------------------
if do_parallel
#define strobe PORTA,4
#define select PORTA,3
#define error PORTA,2
#define paper PORTA,1
#define ack PORTA,0
endif ; do_parallel
if do_serial
#define rxd PORTA,1
#define txd PORTA,2
endif ; do_serial
;-----------------------------------------------------------------------------
; RAM definitions
;-----------------------------------------------------------------------------
cblock rambase
cmd
parity
result
temp
endc
if do_serial&&echo_input
cblock
temp2
endc
endif
;-----------------------------------------------------------------------------
; error codes
;-----------------------------------------------------------------------------
noerr equ 0
parerr equ 1
cmderr equ 2
argerr equ 3
keyerr equ 4
;-----------------------------------------------------------------------------
; vectors
;-----------------------------------------------------------------------------
org rombase
goto reset
nop
nop
nop
goto reset
;-----------------------------------------------------------------------------
; tables - must be in page 0
;-----------------------------------------------------------------------------
; command dispatch table
docmd: clrf PCLATH
swapf cmd,w ; look at bits 4-6 for command
andlw 7
addwf PCL
goto version
goto echo
goto setkeyee
goto clrkeyee
if single_des
goto des
goto desinv
else
goto echo
goto echo
endif
if triple_des
goto triple
goto tripleinv
else
goto echo
goto echo
endif
;-----------------------------------------------------------------------------
; initialization code
;-----------------------------------------------------------------------------
reset:
if do_parallel
movlw 0xff ; all port pins are inputs (or inactive
tris PORTA ; open-drain outputs)
tris PORTB
clrf PORTA
clrf PORTB
endif
if do_serial
movlw 0x02 ; only rxd is an input
tris PORTA
movlw 0x00 ; all portb pins are outputs
tris PORTB
clrf PORTA
clrf PORTB
endif
bsf STATUS,RP0
clrf EECON1
bcf STATUS,RP0
;-----------------------------------------------------------------------------
; main loop
;-----------------------------------------------------------------------------
main: clrf result
call getcmd
if do_parity
btfss STATUS,Z
goto main9
endif ; do_parity
swapf cmd,W ; set up key pointer
movwf EEADR
rrf EEADR
movlw 0x38
andwf EEADR
call docmd
main9:
; replace the cmd with the result
movf result,W
movwf cmd
; compute parity
call parchk
movwf parity
call sendresult
goto main
;-----------------------------------------------------------------------------
; communication with PC
;-----------------------------------------------------------------------------
if do_parallel
getcmd:
movlw cmd ; get ten bytes from host
movwf FSR
movlw 10
movwf temp
getc1: btfsc select ; if select goes active, abort
goto getcmd
btfsc strobe ; wait for strobe to go true (low)
goto getc1
movf PORTB,w ; grab and stash the data
movwf INDF
getc2: btfsc select ; if select goes active, abort
goto getcmd
btfss strobe ; wait for strobe to go false (high)
goto getc2
movlw 0xfe ; generate an ack pulse
tris PORTA
movlw 8 ; delay 8*4*400nS (10 MHz)
ackp: addlw 0xff
btfss STATUS,Z
goto ackp
movlw 0xff
tris PORTA
incf FSR ; advance to next byte
decfsz temp
goto getc1
; test parity, return zero flag clear if error
call parchk
xorwf parity,w
btfsc STATUS,Z
return
movlw parerr
movwf result
bcf STATUS,Z
return
sendresult:
; $$$ not yet implemented
return
endif ; do_parallel
if do_serial
getcmd:
movlw '>'
call xmit
movlw cmd ; get ten bytes from host
movwf FSR
movlw 10
movwf temp
getc1: call getdig
movwf INDF
swapf INDF
call getdig
iorwf INDF
incf FSR ; advance to next byte
decfsz temp
goto getc1
if echo_input
movlw 0x0d
call xmit
movlw 0x0a
call xmit
endif ; echo_input
if do_parity
; test parity, return zero flag clear if error
call parchk
xorwf parity,W
btfsc STATUS,Z
return
movlw parerr
movwf result
bcf STATUS,Z
return
else
bsf STATUS,Z
return
endif ; do_parity
sendresult:
movlw cmd
movwf FSR
movlw 10
movwf temp
sr1: swapf INDF,W
call xmitdig
movf INDF,W
call xmitdig
incf FSR
decfsz temp
goto sr1
movlw 0x0d
call xmit
movlw 0x0a
call xmit
return
getdig:
call recv
if echo_input
movwf temp2
call xmit
movf temp2,W
endif ; echo_input
addlw 256-'0'
btfss STATUS,C
goto getdig
addlw 255-9
btfsc STATUS,C
goto getd1
addlw 10
return
getd1: addlw 255-12
btfsc STATUS,C
goto getdig
addlw 16
return
xmitdig:
andlw 0x0f
addlw 0xf6
btfsc STATUS,C
addlw 0x07
addlw 0x3a
goto xmit
include "uarthd.inc"
endif
;-----------------------------------------------------------------------------
; parity checking
;-----------------------------------------------------------------------------
parchk:
movlw cmd
movwf FSR
movlw 9
movwf temp
clrw
parck0: xorwf INDF,W
incf FSR
decfsz temp
goto parck0
return
;-----------------------------------------------------------------------------
; commands
;-----------------------------------------------------------------------------
version:
movlw 00h
movwf keyl
movlw 07h
movwf keyl+1
echo:
return
if triple_des
triple:
movlw sboxbase>>8 ; set up to call s-boxes
movwf PCLATH
call ip
call getkey
call descore
movlw 8
addwf EEADR
call getkey
call desinvcore
movlw 8
addwf EEADR
call getkey
call descore
movlw 256-16
addwf EEADR
call ipinv
return
tripleinv:
movlw sboxbase>>8 ; set up to call s-boxes
movwf PCLATH
call ip
movlw 16
addwf EEADR
call getkey
call desinvcore
movlw 256-8
addwf EEADR
call getkey
call descore
movlw 256-8
addwf EEADR
call getkey
call desinvcore
call ipinv
return
endif ; triple_des
if single_des
des:
movlw sboxbase>>8 ; set up to call s-boxes
movwf PCLATH
call ip
call getkey
call descore
call ipinv
return
desinv:
movlw sboxbase>>8 ; set up to call s-boxes
movwf PCLATH
call ip
call getkey
call desinvcore
call ipinv
return
endif ; single_des
;-----------------------------------------------------------------------------
; get key from EEPROM
; input: pointer to key in EEPROM in EEADR
; output: permuted key in keyl:keyr
;-----------------------------------------------------------------------------
getkeyee:
movlw keyl+0
movwf FSR
call eeread8
swapf keyl+3,W ; move low 4 bits of keyl+3
movwf keyr+3 ; into high 4 bits of keyr+3
movlw 0f0h ; mask off extra bits
andwf keyl+3
andwf keyr+3
return
;-----------------------------------------------------------------------------
; clear key
; input: pointer to key in EEPROM in EEADR
;
; if the index doesn't correspond to the first key of a valid set,
; return keyerr
;-----------------------------------------------------------------------------
clrkeyee:
movlw left+0
movwf FSR
movlw 7
movwf temp
clrkee0:
clrf INDF
incf FSR
decfsz temp
goto clrkee0
goto setkeyee9
;-----------------------------------------------------------------------------
; permute key and store to EEPROM
; input: key in keyl:keyr
; pointer to key in EEPROM in EEADR
;
; if the index doesn't correspond to an empty key, return keyerr
;-----------------------------------------------------------------------------
setkeyee:
movlw keyl
movwf FSR
movlw 8
movwf temp
setkeyee0:
rlf INDF
rrf left+0
rlf INDF
rrf left+1
rlf INDF
rrf left+2
rlf INDF
rrf left+3
rlf INDF
rrf left+6
rlf INDF
rrf left+5
rlf INDF
rrf left+4
incf FSR
decfsz temp
goto setkeyee0
setkeyee9:
movlw left+0
movwf FSR
; goto eewrite8 ; fall into eewrite8
;-----------------------------------------------------------------------------
; eewrite - write a range of RAM into EEPROM
; input:
; start address of RAM in fsr
; start address of EEPROM in EEADR
; byte count in temp
;-----------------------------------------------------------------------------
eewrite8:
movlw 8
movwf temp
eewrite:
bsf STATUS,RP0 ; turn on write enable
clrf EECON1
bsf EECON1,WREN
bcf STATUS,RP0
eew0:
movf INDF,W ; transfer the data
movwf EEDATA
bsf STATUS,RP0 ; initiate EEPROM write
bcf EECON1,EEIF ; clear the write completion flag
movlw 055h
movwf EECON2
movlw 0aah
movwf EECON2
bsf EECON1,WR
eew1: btfss EECON1,EEIF ; wait for write to complete
goto eew1
bcf STATUS,RP0
incf FSR ; advance pointers
incf EEADR
decfsz temp
goto eew0
bsf STATUS,RP0 ; turn off write enable
clrf EECON1
bcf STATUS,RP0
return
;-----------------------------------------------------------------------------
; eeread - read a range of EEPROM into RAM
; input:
; start address of RAM in fsr
; start address of EEPROM in EEADR
; byte count in temp
;-----------------------------------------------------------------------------
eeread8:
movlw 8
movwf temp
eeread:
eer0:
bsf STATUS,RP0 ; initiate EEPROM read
bsf EECON1,RD
bcf STATUS,RP0
movf EEDATA,W ; transfer the data
movwf INDF
incf FSR ; advance pointers
incf EEADR
decfsz temp
goto eer0
return
include "des.inc"
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -