home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff274.lzh / HP11 / kbd.c < prev    next >
C/C++ Source or Header  |  1989-11-16  |  8KB  |  340 lines

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