;------------------
;| Popchar v2.0   |
;| By Logan Bauer |
;------------------

;  This is a simple program to demonstrate how to "pop" some chars into
;a program.  To use, simply run "popchar" from the ZShell menu.  When
;running you can press the LEFT and RIGHT arrows to choose a charactor.
;Press [ENTER] to copy that char to the buffer, press [DEL] delete one
;char from the buffer, press [CLEAR] to erase the whole buffer, and
;press [ESC] to quit.
;  After this is done edit your program. Now you can press [MORE] to see
;the DELc and UNDEL menu items.  Then press UNDEL to insert your chars.
;THE CHARS WILL STILL BE IN THE BUFFER.	(use it whenever you like)

;HINT:	If you put a "Stop" command and then the char of $FF on the next
;line in your program, that program will be EDIT LOCKED...  try it!


char	= $80DF 		; Just to hold the current chosen char
        ; $80E2

length	= $800F 		; the length of the DELc buffer (100 max!)
buffer	= $8010 		; the location of the buffer

#include "ti-85.h"

.org 0
.db  "Popchar v2.0",0

Start:
    ld	    a, 4			; graphics mode (just in case)
    out     (5), a			; set the mode to 4
    ROM_CALL(CLEARLCD)

    ld      a, (length)                 ; Get current length of buffer
    dec     a                           ; Used to choos char
    ld      d, 0
    ld      e, a                        ; save (length - 1) into E
    ld      hl, buffer                  ; get the address of the buffer into HL
    add     hl, de                      ; set hl to address+lenth (to get char)
    ld      a, (hl)                     ; Get the current char
    ld      (char), a                   ; save it

    xor     a                           ;
    ld      (CURSOR_ROW), a             ;
    ld      (CURSOR_COL), a             ;
    ld      hl, (PROGRAM_ADDR)          ; DISPLAY TITLE
    ld      de, Title                   ;
    add     hl, de                      ;
    ROM_CALL(D_ZT_STR)                  ;

    ld      bc, $020A                   ;
    CALL_(RowCol)                       ; DISPLAY THE CARRET
    ld      a, '^'                      ;
    ROM_CALL(TX_CHARPUT)                ;


DrawText:
    ld      bc, $0300                   ; Display at line 3, col 0
    CALL_(RowCol)                       ; Move cursor
    ld      a, (length)                 ; get current length
    or      a                           ; set regesters
    jr      z, DTdone                   ; zero length.
    ld      b, a                        ; save length in B for loop counter
    ld      hl, buffer                  ; get the address of the buffer
DTloop:
    ld      a, (hl)                     ; get char in HL
    CALL_(PrintChar)                    ; Display char or [space] for return
    inc     hl                          ; Next char
    djnz    DTloop                      ; repeat B times
DTdone:
    ld      a, ' '                      ; a = [space]
    ROM_CALL(TX_CHARPUT)                ; print space (if used del key)


Main:
    ld      bc, $0100                   ; Row 1, col 0
    CALL_(RowCol)                       ; Move cursor
    ld      a, (char)                   ; get current char
    ld      c, a                        ; save it in C
    add     a, -10                      ; subtract 10 to display before & after
    ld      b, 21                       ; loop counter
Loop:
    CALL_(PrintChar)                    ; Display char or [space] if return
    inc     a                           ; next char
    djnz    Loop                        ; repeat B times

    ld      a, (char)                   ; get current char
    ld      bc, $0007                   ; move to hex display position
    CALL_(RowCol)
    push    af                          ; save char
    CALL_(PrintHEX)                     ; Print the hex version
    pop     af                          ; get char again
    ld      c, 18                       ; move to char display position
    CALL_(RowCol)
    CALL_(PrintChar)                    ; print char of [space] if return


Key:
    call    GET_KEY			; A = key pressed
    or	    a				; just set the flags
    jr      z, Key                      ; repeat until a key is pressed (A<>0)
    cp      $37             ;EXIT
    ret     z				; Quit
    ld      hl, char
    cp      $09             ;ENTER
    jr      z, AddChar
    cp      $20             ;DEL
    jr      z, DelChar
    cp      $0F             ;CLEAR
    jr      z, ClearBuffer
    cp      $02             ;LEFT
    jr      z, Left                      ; Decrement char
    cp      $03             ;RIGHT
    jr      z, Right                     ; Increment char
    jr	    Key


Left:
    dec     (hl)                        ; prev char
    jr      Main                        ; go back

Right:
    inc     (hl)                        ; next char
    jr      Main                        ; go back

AddChar:
    ld      a, (length)                 ; current length
    cp      100                         ; reached maximum length?
    JUMP_Z(DrawText)                    ; if so, then go back
    ld      hl, buffer                  ; get location of buffer
    ld      d, 0                        ; load D with 0 (for  add HL,DE)
    ld      e, a                        ; loat E with A (for  add HL,DE)
    inc     a                           ; next position
    ld      (length), a                 ; save this into length
    add     hl, de                      ; add offset to buffer
    ld      a, (char)                   ; get the curren char
    ld      (hl), a                     ; save it into buffer pos
    JUMP_(DrawText)                     ; return

DelChar:
    ld      a, (length)                 ; Get current lenth
    or      a                           ; set regesters
    JUMP_Z(DrawText)                    ; return if zero
    dec     a                           ; decrease number of chars
    ld      (length), a                 ; save it
    JUMP_(DrawText)                     ; return

ClearBuffer:                            ; Pressed CLEAR
    xor     a                           ; a = 0
    ld      (length), a                 ; set length to 0
    ld      bc, $0300                   ; line 3, col 0
    CALL_(RowCol)
    ld      a, ' '                      ; a = [space]
    ld      b, 100                      ; b = 100 chars
CBloop:
    ROM_CALL(TX_CHARPUT)                ; display [space]
    djnz    CBloop                      ; repeat B times
    JUMP_(Main)                         ; go back


PrintHex:                               ; Print hex version of A
        push    af
        rrca
        rrca
        rrca
        rrca
        CALL_(HexDigit)
        pop     af
HexDigit:
        and   $0F
        add   a,'0'
       cp    ':'
        jr    c,PrintIt
        add   a,'A'-':'
PrintIt:
        ROM_CALL(TX_CHARPUT)
        ret


PrintChar:                          ; Print char or [space] if return
    push    af
    cp      $D6                     ; $D6 is the [RETURN] char
    jr      nz, PCnret
    ld      a, ' '
PCnret:
    ROM_CALL(TX_CHARPUT)
    pop     af
    ret

RowCol:                             ; sets row and col to b, c
    push    af
    ld      a, b
    ld      (CURSOR_ROW), a
    ld      a, c
    ld      (CURSOR_COL), a
    pop     af
    ret

Title: .db "Hex = $XX  Char = ",0

.end


