;*********************************************************;
;                                                         ;
; Lock 83(+)                                              ;
; Version 1.6                                             ;
; Copyright ©1999-2000 Jimsoft Software/TCPA              ;
; Written and Programmed by James Vernon <james@calc.org> ;
; http://tcpa.calc.org                                    ;
; ICQ#: 71589304                                          ;
;                                                         ;
; You may use parts of the code as long as you give me    ;
; credit. Changes are to be for personal use only! You    ;
; are not allowed to distribute a modified source without ;
; my permission!                                          ;
;                                                         ;
;*********************************************************;


#include "ion.inc"


#ifdef TI83P
 .org sram-2
 .db $BB,$6D
#else
 .org sram
#endif

 ret
 .db 2                          ; Module detection number


Start:
 sub 49                         ; Was [GRAPH] pressed?
 jr z,TestPowerOff              ; If so, turn off if enabled
 dec a                          ; Was [TRACE] pressed?
 ld a,1                         ; If we exit back to Ion, there's no need to restart
 ret nz                         ; If not, there's nothing to do but leave

ChangePassword:
 call IniScreen
 ld a,(PassSlot)
 or a
 jr z,Enable

 ld hl,StrEnterOld
 call Puts
 call GetPassword
 call ComparePasswords
 jr nc,ExitWithRestart

 bcall(_newline)

Enable:
 ld hl,StrEnterNew
 call Puts
 call GetPassword

 ld hl,TempPass
 ld de,PassSlot
 ld bc,10
 ldir

ExitWithRestart:
 xor a
 ret

TestPowerOff:
 ld a,(PassSlot)
 or a
 ret z

PowerOff:
 call CalcOff

 call IniScreen
 bcall(_getcsc)
 ld hl,StrEnterPassword
 call Puts
 call GetPassword
 call ComparePasswords
 jr nc,PowerOff
 ret

GetPassword:
 ld b,10
 ld hl,TempPass
 push hl
ClearTempLoop:
 ld (hl),0
 inc hl
 djnz ClearTempLoop
 pop hl

GetPassLoop:
 push hl
 ld de,0

GetPassKey:
 inc de
 ld a,d
 cp 255
 call nc,CalcOff
 bcall(_getcsc)
 or a
 jr z,GetPassKey
 push af
 cp 56
 jr z,DelChar
 cp 9
 jr z,GetPasswordFinished

 ld a,'*'
 bcall(_putc)
 pop af
 pop hl
 ld (hl),a
 inc hl
 jr GetPassLoop

GetPasswordFinished:
 pop af
 pop hl
 ret

DelChar:
 pop af
 ld a,(curcol)
 or a
 jr z,GetPassKey
 dec a
 ld (curcol),a
 ld a,' '
 bcall(_putmap)
 pop hl
 dec hl
 ld (hl),0
 jr GetPassLoop

ComparePasswords:
 ld hl,TempPass
 ld de,PassSlot
 ld b,10

CompareLoop:
 ld a,(de)
 cp (hl)
 jr nz,Wrong
 inc de
 inc hl
 djnz CompareLoop
 scf
 ret

Wrong:
 or a
 ret

IniScreen:
 bcall(_clrscrnfull)
 bcall(_homeup)
 ret

Puts:
 bcall(_puts)
 bcall(_newline)
 ret

CalcOff:
 ei
 ld a,2
 out ($10),a
 ld a,1
 out (3),a
 halt
 ld a,%1011
 out (3),a
 ld a,3
 out ($10),a
 ld de,0
 ret


StrEnterOld:
 .db "Old:",0

StrEnterNew:
 .db "New:",0

StrEnterPassword:
 .db "Password:",0

PassSlot:
 .db 0,0,0,0,0,0,0,0,0,0

TempPass:            ; Temporarily stores passwords after module

.end
