home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol117 / redef.mac < prev    next >
Encoding:
Text File  |  1985-02-10  |  7.5 KB  |  277 lines

  1.     Title REDEF - Key mapping util.
  2. ;    Greg Louis, 830330:0832
  3.     .z80
  4.     .comment "
  5. This program, when run, installs itself
  6. in 'safe memory' and reads in a set of
  7. key definitions from its standard
  8. input.  The program supports I/O
  9. redirection via subroutines init and
  10. getc: if the command line reads
  11.     REDEF <Z80.DFN
  12. then file Z80.DFN will be the source of
  13. key-definition input.
  14.  
  15. A 'key definition' has the form
  16. <key>string to be substituted^C
  17. (for example, Xexample^C would cause
  18. the word example to be input every time
  19. an uppercase X was pressed).  CR and LF
  20. characters following the control-C will
  21. be ignored (you can't redefine either
  22. of those keys), but CR and LF may be
  23. included in the substitution string.
  24. For more examples, look at file Z80.DFN
  25. which contains redefinitions for the
  26. more common M80 opcodes and pseudo-ops.
  27.  
  28. Once the program finishes its execution
  29. the redefinitions are in place and will
  30. apply until either a cold boot is
  31. performed or the keystroke sequence
  32. ` (hex 60,3) is typed.  The
  33. character ` (hex 60) acts as an escape
  34. character to allow entry of an other-
  35. wise 'redefined' key, e.g. `X if X has
  36. been redefined and the text needs an X
  37. in it.
  38.  
  39. The routines declared as external will
  40. be found in the TOOLS.ARC toolbox.
  41. To get this program running, alter the
  42. memory equates below to suit your
  43. memory map, assemble with M80, link
  44. with your version of TOOLS.REL
  45. (full source is in TOOLS.ARC on the
  46. Mississauga RCP/M System One) and
  47. enjoy.
  48. "
  49.     ext    init,movel,getc
  50.     ext    remark,error
  51.  
  52. goloc    equ    0f540h            ; Resident part of program
  53. maxloc    equ    0f5d0h            ; Upper limit (hardware dependent)
  54. maxsec    equ    8            ; Maximum redefinition file size
  55. ptrs    equ    0f100h            ; Start of safe memory
  56. table    equ    0f140h            ; Start of redefinitions
  57.     cseg
  58. start:    jp    instal            ; Around the resident part
  59. code:                    ; Install-time address of resident part
  60.     .phase    goloc            ; Run-time address of resident part
  61. freept:    ds    1            ; Number (0-31) of first free pointer
  62. indef:    ds    1            ; Flag true if input from a definition
  63. pntr:    ds    2            ; Where in definition
  64. jtbl:    ds    2            ; Storage for BIOS jump table addr
  65.  
  66. const:    ld    a,(indef)        ; Replacement console status routine
  67.     or    a            ; Always ready if reading from defn.
  68.     ret    nz
  69. oconst:    jp    $-$            ; Jump to old constat routine
  70. oconin:    jp    $-$            ; Old conin routine
  71. conin:    ld    a,(indef)        ; Replacement conin
  72.     or    a
  73.     jr    nz,give            ; If in defn., read from table
  74. keyin:    call    oconin            ; Get a keypress
  75.     ld    b,a            ; save it
  76.     ld    a,(freept)        ; get pointer count
  77.     ld    e,a
  78.     ld    a,b            ; get the keypress
  79.     cp    '`'            ; check for escape character
  80.     jr    z,esckey        ; ..which needs special processing
  81.     cp    ' '            ; can't be redef if ctrl char
  82.     ret    c
  83.     sub    32            ; get ordinal key number
  84.     ld    d,a
  85.     ld    hl,ptrs+1        ; begin looking for redef. key
  86. ckptr:    ld    a,(hl)
  87.     and    63            ; key number is lower 6 bits
  88.     cp    d
  89.     jr    z,stdef            ; if found, start definition
  90.     or    a            ; empty entry?
  91.     ld    a,b            ; get original key back in case
  92.     ret    z            ; send it along if so
  93.     dec    e            ; end of table?
  94.     ret    z            ; send original key if so
  95.     inc    hl            ; point to next entry
  96.     inc    hl
  97.     jr    ckptr            ; and round again
  98. ; Got a redefined key
  99. stdef:    ld    a,0ffh
  100.     ld    (indef),a        ; flag definition being read
  101.     ld    a,(hl)            ; get high bits + key number
  102.     dec    hl
  103.     ld    e,(hl)            ; and low bits
  104.     rlca                ; rotate address bits to
  105.     rlca                ; ..positions 0 and 1
  106.     and    3            ; isolate them
  107.     ld    d,a            ; de now has 10-bit table address
  108.     ld    hl,table        ; add it to base
  109.     add    hl,de
  110.     jr    give+3            ; ..and use the result as the first
  111. give:    ld    hl,(pntr)        ; pointer to char to give
  112.     ld    a,(hl)            ; get it
  113.     inc    hl            ; point to next char
  114.     ld    (pntr),hl        ; save the pointer
  115.     ld    b,a            ; save the char
  116.     ld    a,(hl)            ; get the one that will be next
  117.     cp    3            ; check for end of string
  118.     ld    a,b            ; get back the current char
  119.     ret    nz            ; send it along unless we're done
  120.     xor    a            ; clear the 'in definition' flag
  121.     ld    (indef),a
  122.     ld    a,b            ; get back the current char
  123.     ret
  124. ; Process escape (`) key
  125. esckey:    call    oconin            ; get another keypress
  126.     cp    3            ; was it ETX, ie should we cancel?
  127.     ret    nz            ; use the second keypress if not ETX
  128.     ld    hl,(oconst+1)        ; Cancel out: 
  129.     ex    de,hl            ; ..restore original BIOS jumps
  130.     ld    hl,(jtbl)
  131.     ld    (hl),e            ; ..constat
  132.     inc    hl
  133.     ld    (hl),d
  134.     ex    de,hl
  135.     ld    hl,(oconin+1)
  136.     ex    de,hl
  137.     inc    hl
  138.     inc    hl
  139.     ld    (hl),e            ;.. and conin
  140.     inc    hl
  141.     ld    (hl),d
  142.     jr    oconin            ;.. and jump to old conin.
  143. ephase    equ    $
  144.     if    ephase ge maxloc
  145.     .printx "Code too long"
  146.     endif
  147.     .dephase
  148. instal:    call    init            ; Set up redirection if wanted
  149.     ld    hl,(1)            ; Find BIOS jump table
  150.     inc    hl
  151.     inc    hl
  152.     inc    hl
  153.     inc    hl
  154.     inc    hl            ; high byte of CONIN jmp
  155.     ld    a,(hl)
  156.     and    0f0h            ; look at which 4k block
  157.     ld    l,a
  158.     ld    a,h            ; Same block as BIOS jump table?
  159.     and    0f0h
  160.     cp    l
  161.     jp    z,moveit
  162.     call    error            ; Bomb out if REDEF done already
  163.     db    'Type `^C; REDEF '    ; ..Test for this may have to be
  164.     db    'already loaded!',0    ; ..altered depending on mem map
  165. ; Put the resident code in place
  166. moveit:    ld    hl,code
  167.     ld    de,goloc
  168.     ld    bc,instal-code
  169.     call    movel
  170. ; Read the definition file
  171.     call    remark
  172.     db    'Reading '
  173.     db    'redefinitions...'
  174.     db    13,10,0
  175.     ld    hl,table
  176.     ld    de,maxsec shl 7            ; Max bytes
  177. rdlop:    call    getc    
  178.     cp    0dh                ; Ignore leading CR or LF
  179.     jr    z,rdlop
  180.     cp    0ah
  181.     jr    z,rdlop
  182. rdlop0:    ld    (hl),a                ; Save char
  183.     cp    1ah                ; Was it ctl-Z?
  184.     jr    z,eofl                ; End of input if so
  185.     dec    de                ; Mem full?
  186.     ld    a,d
  187.     or    e
  188.     jr    z,eofl                ; End of input if so
  189.     ld    a,(hl)
  190.     inc    hl
  191.     cp    3                ; End of definition?
  192.     jr    z,rdlop                ; Skip CR or LF if so
  193.     call    getc                ; ..else don't skip
  194.     jr    rdlop0
  195. ; done reading, construct pointer table
  196. eofl:    ld    (hl),1ah            ; Make sure end is marked
  197.     ld    de,table
  198.     ld    hl,ptrs
  199.     ld    bc,0                ; Byte counter
  200.     xor    a
  201.     ld    (freept),a            ; Entry counter
  202.     ld    (indef),a            ; In-definition flag
  203. ptlop:    ld    a,(de)                ; Get byte
  204.     inc    bc                ; count it
  205.     cp    1ah                ; done if ctrl-Z
  206.     jp    z,rdy
  207.     ld    a,(freept)            ; error if more than 32 entries
  208.     cp    32
  209.     jp    z,err
  210.     inc    a                ; count new entry
  211.     ld    (freept),a
  212.     ld    (hl),c                ; save low 8 bits
  213.     inc    hl
  214.     ld    a,(de)                ; get byte again
  215.     inc    de
  216.     sub    32                ; make it key number (6 bits)
  217.     ld    (hl),a                ; save temporarily
  218.     ld    a,b                ; get bits 8 and 9 of table
  219.     rrca                    ; ..addr and rotate to
  220.     rrca                    ; ..positions 14 and 15
  221.     and    0c0h
  222.     or    (hl)                ; or in the 6-bit char
  223.     ld    (hl),a                ; complete the pointer entry
  224.     inc    hl
  225. skplop:    ld    a,(de)
  226.     inc    de
  227.     inc    bc                ; skip and count
  228.     cp    1ah                ; end of entries?
  229.     jp    nz,skpl1
  230.     ld    a,3                ; mark end of string
  231.     ld    (de),a
  232.     call    remark
  233.     db    'Last entry '
  234.     db    'truncated',0
  235.     jp    rdy                ; and quit
  236. skpl1:    cp    3                ; end of string?
  237.     jr    nz,skplop            ; keep lookin if not
  238.     jp    ptlop                ; else start new pointer
  239. err:    call    remark
  240.     db    'Too many entries: '
  241.     db    'stop at 32',0
  242. ; ready to go
  243. rdy:    ld    hl,(1)
  244.     inc    hl
  245.     inc    hl
  246.     inc    hl
  247.     inc    hl                ; low byte of constat
  248.     ld    (jtbl),hl            ; save for cancel routine
  249.     ld    e,(hl)                ; get and install original
  250.     inc    hl                ; ..constat addr
  251.     ld    d,(hl)
  252.     ex    de,hl
  253.     ld    (oconst+1),hl
  254.     ex    de,hl
  255.     inc    hl
  256.     inc    hl
  257.     ld    e,(hl)                ; ..and conin addr
  258.     inc    hl
  259.     ld    d,(hl)
  260.     ex    de,hl
  261.     ld    (oconin+1),hl            ; ..in resident code
  262.     ex    de,hl
  263.     ld    de,conin            ; Install new conin
  264.     ld    (hl),d
  265.     dec    hl
  266.     ld    (hl),e
  267.     dec    hl
  268.     dec    hl
  269.     ld    de,const            ; ..and constat
  270.     ld    (hl),d
  271.     dec    hl
  272.     ld    (hl),e                ; ..in BIOS jump table
  273.     call    error                ; Not really an error
  274.     db    'Redefinitions '        ; ..Issues message and quits
  275.     db    'installed',0
  276.     end    start
  277.