home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / h / hp11 / Amiga_Code / c / kbd < prev    next >
Encoding:
Text File  |  1992-05-07  |  9.7 KB  |  360 lines

  1. /*->  c.kbd  */
  2.  
  3. /*  Modifications for ANSI C under RISC-OS:
  4.  *
  5.  *     Date               Modification
  6.  *  11-may-90   renamed headers
  7.  *   8-jun-90   removed declarations for key -- it's now global
  8.  *   8-jun-90   modified JMPDec() to make event driven
  9.  *   8-jun-90   deleted GetLine() -- merged with JMPDec()
  10.  *   8-jun-90   modified REGDec() to make event driven
  11.  */
  12. #include <stdlib.h>
  13.  
  14. #include "types.h"
  15. #include "hp11.h"
  16. #include "kbd.h"
  17. #include "codes.h"
  18. #include "io.h"
  19.  
  20. /* Macros to initialise one field of the keyboard structure to a particular
  21.    type.  This simpilfies (& clarifies) this initialisation. */
  22. #define CODE(code) {Instruction, (Decoder)(code) }
  23. #define ACT(act) {Action, (Decoder)(act) }
  24. #define PREFIX(adr) {Prefix, (adr) }
  25. #define INVALID() {Invalid, NULL }
  26.  
  27. /* Often used macros which return their agument signaling that it is an
  28.    instruction, action or error */
  29. #define RETINS(val) { *code = (val); return(Instruction); }
  30. #define RETACT(val) { *code = (val); return(Action); }
  31. #define RETERR(key) { *code = (key); return(Invalid); }
  32.  
  33. /* Keys which can follow GTO (or GSB). A -1 indicates am invalid sequence,
  34.    otherwise the value is the offset to add to KGTO to obtain the corresponding
  35.    instruction.  IGTO_LINE is different and valid only for GTO, it indicates 
  36.    a GTO .nnn action
  37.  */
  38. static BYTE gto_decode[NUMKEYS] = {
  39.    10, 11, 12, 13, 14, -1, 7, 8, 9, -1,
  40.    -1, -1, -1, -1, OIND_G, -1, 4, 5, 6, -1,
  41.    -1, -1, -1, -1, -1, -1, 1, 2, 3, -1,
  42.    -1, -1, -1, -1, -1, -1, 0, IGTO_LINE, -1, -1,
  43.    -1, -1
  44. };
  45.  
  46. /* For STO & RCL, cf above */
  47. static BYTE sto_decode[NUMKEYS] = {
  48.    -1, -1, -1, -1, -1, -1, 7, 8, 9, ODIV,
  49.    -1, -1, -1, OIND_R, OI, -1, 4, 5, 6, OMUL,
  50.    -1, -1, -1, -1, -1, KRANDOM, 1, 2, 3, OSUB,
  51.    -1, -1, -1, -1, -1, -1, 0, KPOINT, KSIGMA_PLUS, OPLUS,
  52.    -1, -1
  53. };
  54.  
  55. /* Global variable to allow REGdec to process through repeated calls */
  56. int Rdoffset;
  57.  
  58. /* Functions which take a numeric argument only (eg eng) can use the numbers
  59.   from gto_decode, considering as invalid what isn't a number between 1 & 10 */
  60. #define nb_decode gto_decode
  61.  
  62. /* Decoder routine for FIX, SCI, ENG, SF, CF, Set. code returns the
  63.   instruction/action/keycode, start is the offset for the instruction being
  64.   decoded (eg KFIX), max is the maximum value which can be accepted (eg 1 for
  65.   SF).  For SCI & ENG, a number beyond their max (7) is treated as if it was
  66.   the max value (So if you type 'f SCI 8' you will get 'f SCI 7' */
  67. static enum KeyTypes NBDec(int *code, int start, int max)
  68. {
  69.    register int dec;
  70.  
  71.    key = GetKey(); dec = nb_decode[key];
  72.  
  73.    if (dec >= 0 && dec <= 9) { /* Is a digit */
  74.       if (dec <= max) RETINS(start + dec) /* valid ins */
  75.       else if (start == KSCI || start == KENG) RETINS(start + max)
  76.         /* Special treatment for SCI & ENG */
  77.    }
  78.    RETERR(key);
  79. }
  80.  
  81. /* Decoding for HYP & ArcHYP */
  82. static enum KeyTypes HypDec(int *code, int start)
  83. {
  84.    key = GetKey();
  85.    if (key >= 12 /* SIN */ && key <= 14 /* TAN */) RETINS(start + key - 12)
  86.    else RETERR(key);
  87. }
  88.  
  89. /* Decoding for GTO, GSB & LBL */
  90. static enum KeyTypes JMPDec(int *code, int start)
  91. {
  92.    register int dec, kval;
  93.    static   int val = 0;
  94.    static   int count = 0;  /* counts number of keys which have been read in
  95.                                                         the current sequence */
  96.                /* NB this will have a problem if reset in middle of sequence */
  97.    if (count > 0) {    /* then we're in the middle of reading a GTO sequence */
  98.       key = GetKey(); dec = nb_decode[key];             /* Get numeric value */
  99.       if (dec >= 0 && dec <= 9) {                           /* It is a digit */
  100.          count++;
  101.          val = val * 10 + dec;
  102.          if (count > 3) {           /* we've reached the end of the sequence */
  103.             count = 0;
  104.             kval = val;
  105.             val = 0;
  106.             keyflag = NewSeq;
  107.             RETACT(IGTO_LINE + kval);
  108.          }
  109.          return(Null);                /* have'nt got a complete sequence yet */
  110.       }
  111.       else {                                            /* error in GTO .nnn */
  112.          count = 0;
  113.          val = 0;
  114.          keyflag = NewSeq;
  115.          RETERR(key);
  116.       }
  117.    }          /* all branches of the above have returned, so don't need else */
  118.    key = GetKey(); dec = gto_decode[key];
  119.    if (dec >= 0 && dec <= 15) RETINS(start + dec);         /* 0 to 9, A to E */
  120.    switch (dec) {
  121.       case IGTO_LINE:                                            /* GTO .nnn */
  122.          if (start == KGTO) {                      
  123.             count++;
  124.             val = 0;
  125.             return(Null);
  126.         }
  127.       case OIND_G:                                              /* GTO/GSB I */
  128.          if (start != KLBL) RETINS(start + OIND_G);
  129.    }
  130.    RETERR(key);
  131. }
  132.  
  133. /* Decoding for STO & RCL, deals with all possible STO's */
  134. static enum KeyTypes REGDec(int *code, int start)
  135. {
  136.    register int dec; 
  137.             int oldoff;
  138.  
  139.    key = GetKey();
  140.    dec = sto_decode[key];
  141.    oldoff = Rdoffset;
  142.  
  143.    if ((dec >= 0 && dec <= 9)                   /* 0 to 9 end an instruction */
  144.       ||        /* I & (i) end an instruction if no . was typed before. This
  145.                          is visible if the Rdoffset (ignoring + - * /) is 10 */
  146.       ((Rdoffset % OPLUS) != 10 && (dec == OI || dec == OIND_R)))
  147.       RETINS(start + Rdoffset + dec);
  148.    switch (dec) {                               /* Special cases & Rdoffsets */
  149.       case KRANDOM:                                            /* STO Random */
  150.          if (Rdoffset == 0 && start == KSTO) RETINS(KSTO_RANDOM); 
  151.       case KSIGMA_PLUS:                                      /* Recall stats */
  152.          if (Rdoffset == 0 && start == KRCL) RETINS(KRCL_SIGMA); 
  153.       case KPOINT:                                     /* Only one . allowed */
  154.          if ((Rdoffset % OPLUS) == 0) Rdoffset += 10;
  155.       case OPLUS: case ODIV: case OMUL: case OSUB:  /* /+-* only if none yet */
  156.          if (Rdoffset == 0 && start == KSTO) Rdoffset = dec;
  157.    }
  158.    if (Rdoffset == oldoff) RETERR(key);  /* if Rdoffset not changed then there 
  159.                                          was an error (a new key is required
  160.                                                     when the offset changes) */
  161.    return(Null);
  162. }
  163.  
  164. /* Decoding for prefixes */
  165. /* --------------------- */
  166. static enum KeyTypes FIXDec(int *code)
  167. {
  168.    return(NBDec(code, KFIX, 9));
  169. }
  170.  
  171. static enum KeyTypes SCIDec(int *code)
  172. {
  173.    return(NBDec(code, KSCI, 7));
  174. }
  175.  
  176. static enum KeyTypes ENGDec(int *code)
  177. {
  178.    return(NBDec(code, KENG, 7));
  179. }
  180.  
  181. static enum KeyTypes SFDec(int *code)
  182. {
  183.    return(NBDec(code, KFLAGS + OSF, 1));
  184. }
  185.  
  186. static enum KeyTypes SETDec(int *code)
  187. {
  188.    return(NBDec(code, KFLAGS + OSET, 1));
  189. }
  190.  
  191. static enum KeyTypes CFDec(int *code)
  192. {
  193.    return(NBDec(code, KFLAGS + OCF, 1));
  194. }
  195.  
  196. static enum KeyTypes HYPDec(int *code)
  197. {
  198.    return(HypDec(code, KHYP));
  199. }
  200.  
  201. static enum KeyTypes ARCHYPDec(int *code)
  202. {
  203.    return(HypDec(code, KARCHYP));
  204. }
  205.  
  206. static enum KeyTypes LBLDec(int *code)
  207. {
  208.    return(JMPDec(code, KLBL));
  209. }
  210.  
  211. static enum KeyTypes GTODec(int *code)
  212. {
  213.    return(JMPDec(code, KGTO));
  214. }
  215.  
  216. static enum KeyTypes GSBDec(int *code)
  217. {
  218.    return(JMPDec(code, KGSB));
  219. }
  220.  
  221. static enum KeyTypes STODec(int *code)
  222. {
  223.    return(REGDec(code, KSTO));
  224. }
  225.  
  226. static enum KeyTypes RCLDec(int *code)
  227. {
  228.    return(REGDec(code, KRCL));
  229. }
  230.  
  231. /* The main kbd, f & g */
  232. /* ------------------- */
  233. struct Key mainKbd[3 * NUMKEYS] = {
  234. /* First the main keyboard (unshifted). All the keys which can be entered
  235.   MUST not be INVALID(), otherwise the program enters an infinite loop */
  236.    CODE(KSQRT),
  237.    CODE(KEXP),
  238.    CODE(KEXP10),
  239.    CODE(KEXP_YX),
  240.    CODE(KINV),
  241.    CODE(KCHS),
  242.    CODE(KFIG + 7),
  243.    CODE(KFIG + 8),
  244.    CODE(KFIG + 9),
  245.    CODE(KDIV),
  246.    ACT(ISST),
  247.    PREFIX(GTODec),
  248.    CODE(KTRIG + OSIN),
  249.    CODE(KTRIG + OCOS),
  250.    CODE(KTRIG + OTAN),
  251.    CODE(KEEX),
  252.    CODE(KFIG + 4),
  253.    CODE(KFIG + 5),
  254.    CODE(KFIG + 6),
  255.    CODE(KMUL),
  256.    CODE(KR_S),
  257.    PREFIX(GSBDec),
  258.    CODE(KRDN),
  259.    CODE(KEXG_XY),
  260.    ACT(IBACK),
  261.    CODE(KENTER),
  262.    CODE(KFIG + 1),
  263.    CODE(KFIG + 2),
  264.    CODE(KFIG + 3),
  265.    CODE(KSUB),
  266.    ACT(ION),
  267.    INVALID(), /* Never tested : f */
  268.    INVALID(), /* Never tested : g */
  269.    PREFIX(STODec),
  270.    PREFIX(RCLDec),
  271.    INVALID(), /* This key does not exist : it is hidden by ENTER */
  272.    CODE(KFIG + 0),
  273.    CODE(KPOINT),
  274.    CODE(KSIGMA_PLUS),
  275.    CODE(KPLUS),
  276.    ACT(IRESET), /* These 2 are pseudo-keys */
  277.    ACT(IDISPLAY),
  278. /* now f codes, which can be INVALID() */
  279.    CODE(KGSB + OA),
  280.    CODE(KGSB + OB),
  281.    CODE(KGSB + OC),
  282.    CODE(KGSB + OD),
  283.    CODE(KGSB + OE),
  284.    CODE(KPI),
  285.    PREFIX(FIXDec),
  286.    PREFIX(SCIDec),
  287.    PREFIX(ENGDec),
  288.    CODE(KX_LE_Y),
  289.    PREFIX(LBLDec),
  290.    PREFIX(HYPDec),
  291.    CODE(KEXG_X_IND),
  292.    CODE(KRCL + OIND_R),
  293.    CODE(KRCL + OI),
  294.    CODE(KRECT),
  295.    CODE(KEXG_XI),
  296.    CODE(KDSE),
  297.    CODE(KISG),
  298.    CODE(KX_GT_Y),
  299.    CODE(KPSE),
  300.    CODE(KCLR_SIGMA),
  301.    ACT(ICLR_PRGM),
  302.    CODE(KCLR_REG),
  303.    ACT(ICLR_PREFIX),
  304.    CODE(KRANDOM),
  305.    CODE(KPERM),
  306.    CODE(KHMS),
  307.    CODE(KTO_RAD),
  308.    CODE(KX_NE_Y),
  309.    INVALID(), INVALID(), INVALID(), /* ON, f & g */
  310.    CODE(KFRAC),
  311.    ACT(IUSER),
  312.    INVALID(), /* dosen't exist */
  313.    CODE(KFACT),
  314.    CODE(KESTIMATE),
  315.    CODE(KLR),
  316.    CODE(KX_EQ_Y),
  317.    INVALID(), INVALID(),
  318. /* finally, g codes */
  319.    CODE(KSQR),
  320.    CODE(KLN),
  321.    CODE(KLOG),
  322.    CODE(KPERC),
  323.    CODE(KDELTA_PERC),
  324.    CODE(KABS),
  325.    CODE(KDEG),
  326.    CODE(KRAD),
  327.    CODE(KGRD),
  328.    CODE(KX_LT_0),
  329.    ACT(IBST),
  330.    PREFIX(ARCHYPDec),
  331.    CODE(KARC + OSIN),
  332.    CODE(KARC + OCOS),
  333.    CODE(KARC + OTAN),
  334.    CODE(KPOLAR),
  335.    PREFIX(SFDec),
  336.    PREFIX(CFDec),
  337.    PREFIX(SETDec),
  338.    CODE(KX_GT_0),
  339.    ACT(IP_R),
  340.    CODE(KRTN),
  341.    CODE(KRUP),
  342.    CODE(KRND),
  343.    CODE(KCLX),
  344.    CODE(KLSTX),
  345.    CODE(KCOMB),
  346.    CODE(KHR),
  347.    CODE(KTO_DEG),
  348.    CODE(KX_NE_0),
  349.    INVALID(), INVALID(), INVALID(),
  350.    CODE(KINT),
  351.    ACT(IMEM),
  352.    INVALID(),
  353.    CODE(KMEAN),
  354.    CODE(KSDEV),
  355.    CODE(KSIGMA_SUB),
  356.    CODE(KX_EQ_0),
  357.    INVALID(), INVALID()
  358. };
  359.  
  360.