VEDIT = 3                       ; edit level, change every time
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
;*                                                                         *
;*  Copyright (c) 1986 by Software Designs, PO Box 9227, Spokane WA 99209  *
;*  All rights reserved.  Phone: (509) 326-2480.                           *
;*  Written by Tom Faust, author of RELIEF Input Line Editor.              *
;*                                                                         *
;*   This program may be "freely" copied and used but may never be sold.   *
;*                                                                         *
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
;
;   Pgm Name:   CHR.LIT
;
;   Pgm Desc:   Display any character in ascii & decimal & (hex or octal).
;
;   Version:    1.0(3)
;
;   Hash Total: 130-170-010-302
;
; Patch Log
; Version          Date         Inits   Description of Change
; -------       ---------       -----   -------------------------------------
;  1.0          17 Nov 83       TGF     New.
;       (2)     11 Jan 86       TGF     Allow toggle of xlt mode.
;       (3)     26 Aug 86       TGF     Add new ^C message, OCT/HEX, etc.
;                                       Add notice of 'CHR ?' option.
;                                       Fix XLT toggle on/off message
;

       RADIX   8                       ; code is written in octal

       SEARCH  SYS
       SEARCH  SYSSYM
       SEARCH  TRM

;; program major and minor version = 1.0

       VMAJOR  = 1                     ; program version
       VMINOR  = 0

;; misc character equates

       CTL.C   =   3                   ; control-C
       CR      =  15                   ; carriage return
       SPA     =  40                   ; space char
       DEL     = 177                   ; del char

;; macro to test a bit in a word (non-destructive)

DEFINE  BITW    SRC,DST
       MOVW    DST,D7
       ANDW    SRC,D7
       ENDM

;; macro to define ascii text followed by a CR

DEFINE  ASCIIC  TEXT
       ASCII   TEXT
       BYTE    CR
       ENDM


BASE:   PHDR    -1,0,PH$REE!PH$REU

START:  JOBIDX  A0                      ; Point A6 to job control block
       MOV     JOBTRM(A0),A5           ; index A5 to terminal control block
       MOVW    JOBTYP(A0),D4           ; save current...
       ANDW    #J.HEX,D4               ;   ...octal/hex setting (save j.hex)
       SWAP    D4                      ;   ...in high order word
       MOVW    @A5,D4                  ; save current...
       ANDW    #T$XLT,D4               ;   ...XLT setting in low order word
       ANDW    #~T$XLT,@A5             ; be sure it's not set now

1$:     BYP
       CMPB    @A2,#'?                 ; request help?
       JEQ     USAGE
       CMPB    (A2)+,#'/               ; switch specified?
       BNE     10$
       CMPB    @A2,#'T                 ; transparent mode?
       BNE     2$
       ORW     #<T$XLT>,@a5            ;   then set translation mode
       BR      9$
2$:     CMPB    @A2,#'H                 ; request hex?
       BNE     3$
       ORW     #J.HEX,JOBTYP(A0)       ;   then set hex
       BR      9$
3$:     CMPB    @A2,#'O                 ; request octal?
       BNE     4$
       ANDW    #~J.HEX,JOBTYP(A0)      ;   then set octal
;       BR      9$
4$:
9$:     INC     A2
       BR      1$

10$:    ORW     #<T$DAT!T$ECS>,@a5      ; Set image mode & echo suppress
       TTYL    CRIGHT
       TYPECR  <From AMOS, type 'CHR ?' for usage>
       TYPECR  <Enter desired character, or type CTRL-C to end.>

RESTRT: TYPE    <Chr  Dec  >
       BITW    #J.HEX,JOBTYP(A0)       ; hex set?
       BNE     10$
       TYPE    <Oct>
       BR      20$
10$:    TYPE    <Hex>
20$:    TYPE    <  XLT >
       BITW    #T$XLT,@A5              ; is XLT set now?
       BEQ     30$
       TYPE    <On>
       BR      40$
30$:    TYPE    <Off>
40$:    CRLF


LOOP:   MOVW    #1,D0                   ; default space count
       KBD                             ; get a character
       PUSHB   D1
       CMPB    D1,#DEL                 ; > del?
       BLOS    10$
       TYPE    <+>
       SUBB    #200,D1                 ; adjust to control or printable char
       BR      11$
10$:    INCW    D0

11$:    CMPB    D1,#SPA                 ; less than a space?
       BGE     20$
       TYPE    <^>
       ADD     #100,D1                 ; adjust to printable character
       BR      21$
20$:    INCW    D0

21$:    CMPB    D1,#177                 ; del or greater?
       BLO     ECHO
       TYPE    DEL                     ; (special case)
       DECW    D0
       BR      NOECHO


ECHO:   TTY                             ; display the char
       DECW    D0
       BMI     NOECHO
10$:    TYPE    < >
       DBF     D0,10$


NOECHO: TYPE    < >
       POPB    D1
       DCVT    3,OT$TRM!OT$TSP         ; display decimal ascii value
       OCVT    3,OT$TRM!OT$LSP         ; display octal/hex ascii value
       CMPB    D1,#CTL.C               ; Ctrl-C?
       JNE     DOLOOP
       TYPESP  <   Y=Exit, H=Hex, O=Octal, T=XLT on|off, N=Continue:>
10$:    KBD                             ; get another char
       UCS
       CMPB    D1,#'T                  ; toggle XLT mode?
       BNE     15$
       XORW    #T$XLT,@A5              ; toggle translation mode
       BR      100$
15$:    CMPB    D1,#'Y
       BEQ     EXIT
       CMPB    D1,#3                   ; ctrlc again?
       BEQ     CTLC
       CMPB    D1,#'N
       BEQ     100$
       CMPB    D1,#'H                  ; request hex?
       BNE     20$
       ORW     #J.HEX,JOBTYP(A0)       ;   then set hex
       BR      100$
20$:    CMPB    D1,#'O                  ; request octal?
       BNE     30$
       ANDW    #~J.HEX,JOBTYP(A0)      ;   then set octal
       BR      100$
30$:
99$:    MOV     #7,D1
       TTY
       BR      10$
100$:   TTY                             ; display the N
       CRLF
       JMP     RESTRT
DOLOOP: CRLF
       JMP     LOOP


CTLC:   TYPE    ^C                      ; display ^C
       MOV     #7,D1                   ; and ring the bell
EXIT:   TTY                             ; display the Y or bell
       CRLF
EXIT2:  ANDW    #~T$XLT,@A5             ; reset t$xlt bit
       ORW     D4,@A5                  ; reset original XLT setting
       SWAP    D4                      ; shift j.hex into position
       ANDW    #~J.HEX,JOBTYP(A0)      ; reset j.hex bit
       ORW     D4,JOBTYP(A0)           ; reset original octal/hex setting
       EXIT


USAGE:  TTYI
       ASCIIC  "Usage: CHR {/H/O/T}"
       ASCIIC  "       /H = Display decimal and hex"
       ASCIIC  "       /O = Display decimal and octal"
       ASCIIC  "       /T = Begin in Fkey translation mode"
       ASCIIC  "       ^C to exit"
       ASCIIC
       ASCIIC  "       For character display:"
       ASCIIC  "         the symbol ^ signifies a control character"
       ASCIIC  "         the symbol + signifies the parity bit is set"
       ASCIIC
CRIGHT: ASCIIC  <"Copyright 1983,1986 Software Designs, Spokane, WA">
       BYTE    0
       EVEN
       JMP     EXIT2

       END