INCLUDE "WRISTAPP.I" org $100 jmp test idx EQU $60 rot_cnt EQU $61 k EQU $62 src1 EQU $63 src2 EQU $64 dest EQU $65 ; BYTES 60-67 are persisent RAM for our use ; BYTES 110-435 are copied in from the EEPROM when we run org $110 START EQU * ;0x110 The main entry point - WRIST_MAIN jmp main ;0x113 Called when we are suspended for any reason - WRIST_SUSPEND rts nop nop ;0x116 Called to handle any timers or time events - WRIST_DOTIC rts nop nop ;0x119 Called when the COMM app starts and we have timers pending - WRIST_INCOMM rts nop nop ;0x11c Called when the COMM app loads new data - WRIST_NEWDATA rts nop nop ;0x11f The state table get routine - WRIST_GETSTATE lda state_table0,X rts ;0x123 The state handler for state 0 jmp handle_st0_event ;0x126 Offset of state zero into the state table db state_table0 - state_table0 hello_string: timex6 "HELLO " ; state table state_table0: db 0 db EVT_ENTER, TIM_ONCE, 0 ; Initial state db EVT_RESUME, TIM_ONCE, 0 ; Resume from a nested app db EVT_DNNEXT, TIM_ONCE, 0 ; Next button db EVT_MODE, TIM_ONCE, $FF ; Mode button - quit db EVT_END ; ; (7) This is the main initialization routine which is called when we first get the app into memory ; main: lda #$c0 ; We want button beeps and to indicate that we have been loaded sta $96 rts handle_st0_event: bset 1,$8f ; Indicate that we can be suspended ; lda BTNSTATE ; Get the event jsr CLEARALL ; Clear the screen lda #hello_string-start jsr PUT6TOP lda #SYS8_MODE ; Get the system offset for the 'MODE' string jsr PUTMSGBOT ; and put it on the bottom line rts test: jsr sha1 stop sha1: ldx #0 sha1_copy_h_to_ae_loop: lda H,x sta VA,x incx cpx #20 bne sha1_copy_h_to_ae_loop lda #0 sta idx sha1_loop: ; a contains idx cmp #20 bhs sha1_k2 lda #(K1-base) sta k ; T1 = (B & C) | ((~B) & D) lda #(T2-base) sta dest lda #(VB-base) sta src1 lda #(FFFFFFFF-base) jsr xor4 ;T2=~B lda #(T2-base) sta src1 lda #(VD-base) jsr and4 ;T2=T2&D bra sha1_t2_or_bandc ; T1=T2 | (B&C) sha1_k2: cmp #40 bhs sha1_k3 lda #(K2-base) sta k bra sha1_k24 ; T1 = B ^ C ^ D sha1_k3: cmp #60 bhs sha1_k4 ; T1 = (B & C) | (B & D) | (C & D) lda #(K3-base) sta k lda #(T1-base) sta dest lda #(VD-base) sta src1 lda #(VB-base) jsr and4 ; T1=B&D lda #(T2-base) sta dest lda #(VC-base) jsr and4 ; T2=C&D lda #(T2-base) sta src1 lda #(T1-base) jsr or4 ; T2=T1|T2 sha1_t2_or_bandc: lda #(T1-base) sta dest lda #(VB-base) sta src1 lda #(VC-base) jsr and4 ; T1 = B&C lda #(T1-base) sta src1 lda #(T2-base) jsr or4 ; T1= T1|T2 bra sha1_shuffle sha1_k4: lda #(K4-base) sta k sha1_k24: ; T1 = B ^ C ^ D lda #(T1-base) sta dest lda #(VB-base) sta src1 lda #(VC-base) jsr xor4 lda #(T1-base) sta src1 lda #(VD-base) jsr xor4 sha1_shuffle: ; T1 is src1 and dest here lda #(VE-base) jsr add4 ; T1=T1+E lda k jsr add4 ; T1=T1+k ; get data here lda #(ZERO-base) jsr add4 ; T1=T1+data lda #(VD-base) jsr copy_down ; E=D lda #(VC-base) jsr copy_down ; D=C lda #(VB-base) jsr copy_down ; C=B lda #30 jsr rot_left ; C=ROTLEFT(C,30) lda #(VA-base) jsr copy_down ; B=A lda #(VA-base) sta dest sta src1 lda #5 jsr rot_left ; A=ROTLEFT(A,5) lda #(T1-base) jsr add4 ; A=A+T1 ldx #0 sha1_add0: clc sha1_add1: lda VA,x adc H,x sta VA,x incx txa and #3 bne sha1_add1 cpx #20 bne sha1_add0 lda idx add #1 cmp #80 bhs sha1_done jmp sha1_loop sha1_done: rts copy_down: sta src1 add #4 sta dest lda #(ZERO-base) add4: sta src2 lda #$d9 ; adc $ffff,a sta op4_src2 bra op4_binary and4: sta src2 lda #$d4 ; and $ffff,a sta op4_src2 bra op4_binary xor4: sta src2 lda #$d8 ; eor $ffff,a sta op4_src2 bra op4_binary or4: sta src2 lda #$da ; or $ffff,a sta op4_src2 op4_binary: ldx #0 lda #base & $ff add src2 sta op4_src2+2 txa adc #base>>8 sta op4_src2+1 lda #base & $ff add src1 sta op4_src1+2 txa adc #base>>8 sta op4_src1+1 lda #base & $ff add dest sta op4_dst+2 txa adc #base>>8 sta op4_dst+1 clc op4_1: op4_src1: lda $fffe,x op4_src2: adc $fffe,x op4_dst: sta $fffe,x incx txa and #3 bne op4_1 rts rot_left: ldx dest sta rot_cnt rot_loop: lda base,x rola lda base+1,x rola sta base+1,x lda base+2,x rola sta base+2,x lda base+3,x rola sta base+3,x lda base,x rola sta base,x dec rot_cnt bne rot_loop rts data: H: DB $01 DB $23 DB $45 DB $67 DB $89 DB $AB DB $CD DB $EF DB $FE DB $DC DB $BA DB $98 DB $76 DB $54 DB $32 DB $10 DB $F0 DB $E1 DB $D2 DB $C3 base: K1: DB $99 DB $79 DB $82 DB $5A K2: DB $A1 DB $EB DB $D9 DB $6E K3: DB $DC DB $BC DB $1B DB $8F K4: DB $D6 DB $C1 DB $62 DB $CA K5: DB $D0 DB $83 DB $8C DB $46 FFFFFFFF: DB $FF DB $FF DB $FF DB $FF bss: VA: DW 0 DW 0 VB: DW 0 DW 0 VC: DW 0 DW 0 VD: DW 0 DW 0 VE: DW 0 DW 0 T1: DW 0 DW 0 T2: DW 0 DW 0 ZERO: DW 0 DW 0