home *** CD-ROM | disk | FTP | other *** search
- /*-> c.kbd */
-
- /* Modifications for ANSI C under RISC-OS:
- *
- * Date Modification
- * 11-may-90 renamed headers
- * 8-jun-90 removed declarations for key -- it's now global
- * 8-jun-90 modified JMPDec() to make event driven
- * 8-jun-90 deleted GetLine() -- merged with JMPDec()
- * 8-jun-90 modified REGDec() to make event driven
- */
- #include <stdlib.h>
-
- #include "types.h"
- #include "hp11.h"
- #include "kbd.h"
- #include "codes.h"
- #include "io.h"
-
- /* Macros to initialise one field of the keyboard structure to a particular
- type. This simpilfies (& clarifies) this initialisation. */
- #define CODE(code) {Instruction, (Decoder)(code) }
- #define ACT(act) {Action, (Decoder)(act) }
- #define PREFIX(adr) {Prefix, (adr) }
- #define INVALID() {Invalid, NULL }
-
- /* Often used macros which return their agument signaling that it is an
- instruction, action or error */
- #define RETINS(val) { *code = (val); return(Instruction); }
- #define RETACT(val) { *code = (val); return(Action); }
- #define RETERR(key) { *code = (key); return(Invalid); }
-
- /* Keys which can follow GTO (or GSB). A -1 indicates am invalid sequence,
- otherwise the value is the offset to add to KGTO to obtain the corresponding
- instruction. IGTO_LINE is different and valid only for GTO, it indicates
- a GTO .nnn action
- */
- static BYTE gto_decode[NUMKEYS] = {
- 10, 11, 12, 13, 14, -1, 7, 8, 9, -1,
- -1, -1, -1, -1, OIND_G, -1, 4, 5, 6, -1,
- -1, -1, -1, -1, -1, -1, 1, 2, 3, -1,
- -1, -1, -1, -1, -1, -1, 0, IGTO_LINE, -1, -1,
- -1, -1
- };
-
- /* For STO & RCL, cf above */
- static BYTE sto_decode[NUMKEYS] = {
- -1, -1, -1, -1, -1, -1, 7, 8, 9, ODIV,
- -1, -1, -1, OIND_R, OI, -1, 4, 5, 6, OMUL,
- -1, -1, -1, -1, -1, KRANDOM, 1, 2, 3, OSUB,
- -1, -1, -1, -1, -1, -1, 0, KPOINT, KSIGMA_PLUS, OPLUS,
- -1, -1
- };
-
- /* Global variable to allow REGdec to process through repeated calls */
- int Rdoffset;
-
- /* Functions which take a numeric argument only (eg eng) can use the numbers
- from gto_decode, considering as invalid what isn't a number between 1 & 10 */
- #define nb_decode gto_decode
-
- /* Decoder routine for FIX, SCI, ENG, SF, CF, Set. code returns the
- instruction/action/keycode, start is the offset for the instruction being
- decoded (eg KFIX), max is the maximum value which can be accepted (eg 1 for
- SF). For SCI & ENG, a number beyond their max (7) is treated as if it was
- the max value (So if you type 'f SCI 8' you will get 'f SCI 7' */
- static enum KeyTypes NBDec(int *code, int start, int max)
- {
- register int dec;
-
- key = GetKey(); dec = nb_decode[key];
-
- if (dec >= 0 && dec <= 9) { /* Is a digit */
- if (dec <= max) RETINS(start + dec) /* valid ins */
- else if (start == KSCI || start == KENG) RETINS(start + max)
- /* Special treatment for SCI & ENG */
- }
- RETERR(key);
- }
-
- /* Decoding for HYP & ArcHYP */
- static enum KeyTypes HypDec(int *code, int start)
- {
- key = GetKey();
- if (key >= 12 /* SIN */ && key <= 14 /* TAN */) RETINS(start + key - 12)
- else RETERR(key);
- }
-
- /* Decoding for GTO, GSB & LBL */
- static enum KeyTypes JMPDec(int *code, int start)
- {
- register int dec, kval;
- static int val = 0;
- static int count = 0; /* counts number of keys which have been read in
- the current sequence */
- /* NB this will have a problem if reset in middle of sequence */
- if (count > 0) { /* then we're in the middle of reading a GTO sequence */
- key = GetKey(); dec = nb_decode[key]; /* Get numeric value */
- if (dec >= 0 && dec <= 9) { /* It is a digit */
- count++;
- val = val * 10 + dec;
- if (count > 3) { /* we've reached the end of the sequence */
- count = 0;
- kval = val;
- val = 0;
- keyflag = NewSeq;
- RETACT(IGTO_LINE + kval);
- }
- return(Null); /* have'nt got a complete sequence yet */
- }
- else { /* error in GTO .nnn */
- count = 0;
- val = 0;
- keyflag = NewSeq;
- RETERR(key);
- }
- } /* all branches of the above have returned, so don't need else */
- key = GetKey(); dec = gto_decode[key];
- if (dec >= 0 && dec <= 15) RETINS(start + dec); /* 0 to 9, A to E */
- switch (dec) {
- case IGTO_LINE: /* GTO .nnn */
- if (start == KGTO) {
- count++;
- val = 0;
- return(Null);
- }
- case OIND_G: /* GTO/GSB I */
- if (start != KLBL) RETINS(start + OIND_G);
- }
- RETERR(key);
- }
-
- /* Decoding for STO & RCL, deals with all possible STO's */
- static enum KeyTypes REGDec(int *code, int start)
- {
- register int dec;
- int oldoff;
-
- key = GetKey();
- dec = sto_decode[key];
- oldoff = Rdoffset;
-
- if ((dec >= 0 && dec <= 9) /* 0 to 9 end an instruction */
- || /* I & (i) end an instruction if no . was typed before. This
- is visible if the Rdoffset (ignoring + - * /) is 10 */
- ((Rdoffset % OPLUS) != 10 && (dec == OI || dec == OIND_R)))
- RETINS(start + Rdoffset + dec);
- switch (dec) { /* Special cases & Rdoffsets */
- case KRANDOM: /* STO Random */
- if (Rdoffset == 0 && start == KSTO) RETINS(KSTO_RANDOM);
- case KSIGMA_PLUS: /* Recall stats */
- if (Rdoffset == 0 && start == KRCL) RETINS(KRCL_SIGMA);
- case KPOINT: /* Only one . allowed */
- if ((Rdoffset % OPLUS) == 0) Rdoffset += 10;
- case OPLUS: case ODIV: case OMUL: case OSUB: /* /+-* only if none yet */
- if (Rdoffset == 0 && start == KSTO) Rdoffset = dec;
- }
- if (Rdoffset == oldoff) RETERR(key); /* if Rdoffset not changed then there
- was an error (a new key is required
- when the offset changes) */
- return(Null);
- }
-
- /* Decoding for prefixes */
- /* --------------------- */
- static enum KeyTypes FIXDec(int *code)
- {
- return(NBDec(code, KFIX, 9));
- }
-
- static enum KeyTypes SCIDec(int *code)
- {
- return(NBDec(code, KSCI, 7));
- }
-
- static enum KeyTypes ENGDec(int *code)
- {
- return(NBDec(code, KENG, 7));
- }
-
- static enum KeyTypes SFDec(int *code)
- {
- return(NBDec(code, KFLAGS + OSF, 1));
- }
-
- static enum KeyTypes SETDec(int *code)
- {
- return(NBDec(code, KFLAGS + OSET, 1));
- }
-
- static enum KeyTypes CFDec(int *code)
- {
- return(NBDec(code, KFLAGS + OCF, 1));
- }
-
- static enum KeyTypes HYPDec(int *code)
- {
- return(HypDec(code, KHYP));
- }
-
- static enum KeyTypes ARCHYPDec(int *code)
- {
- return(HypDec(code, KARCHYP));
- }
-
- static enum KeyTypes LBLDec(int *code)
- {
- return(JMPDec(code, KLBL));
- }
-
- static enum KeyTypes GTODec(int *code)
- {
- return(JMPDec(code, KGTO));
- }
-
- static enum KeyTypes GSBDec(int *code)
- {
- return(JMPDec(code, KGSB));
- }
-
- static enum KeyTypes STODec(int *code)
- {
- return(REGDec(code, KSTO));
- }
-
- static enum KeyTypes RCLDec(int *code)
- {
- return(REGDec(code, KRCL));
- }
-
- /* The main kbd, f & g */
- /* ------------------- */
- struct Key mainKbd[3 * NUMKEYS] = {
- /* First the main keyboard (unshifted). All the keys which can be entered
- MUST not be INVALID(), otherwise the program enters an infinite loop */
- CODE(KSQRT),
- CODE(KEXP),
- CODE(KEXP10),
- CODE(KEXP_YX),
- CODE(KINV),
- CODE(KCHS),
- CODE(KFIG + 7),
- CODE(KFIG + 8),
- CODE(KFIG + 9),
- CODE(KDIV),
- ACT(ISST),
- PREFIX(GTODec),
- CODE(KTRIG + OSIN),
- CODE(KTRIG + OCOS),
- CODE(KTRIG + OTAN),
- CODE(KEEX),
- CODE(KFIG + 4),
- CODE(KFIG + 5),
- CODE(KFIG + 6),
- CODE(KMUL),
- CODE(KR_S),
- PREFIX(GSBDec),
- CODE(KRDN),
- CODE(KEXG_XY),
- ACT(IBACK),
- CODE(KENTER),
- CODE(KFIG + 1),
- CODE(KFIG + 2),
- CODE(KFIG + 3),
- CODE(KSUB),
- ACT(ION),
- INVALID(), /* Never tested : f */
- INVALID(), /* Never tested : g */
- PREFIX(STODec),
- PREFIX(RCLDec),
- INVALID(), /* This key does not exist : it is hidden by ENTER */
- CODE(KFIG + 0),
- CODE(KPOINT),
- CODE(KSIGMA_PLUS),
- CODE(KPLUS),
- ACT(IRESET), /* These 2 are pseudo-keys */
- ACT(IDISPLAY),
- /* now f codes, which can be INVALID() */
- CODE(KGSB + OA),
- CODE(KGSB + OB),
- CODE(KGSB + OC),
- CODE(KGSB + OD),
- CODE(KGSB + OE),
- CODE(KPI),
- PREFIX(FIXDec),
- PREFIX(SCIDec),
- PREFIX(ENGDec),
- CODE(KX_LE_Y),
- PREFIX(LBLDec),
- PREFIX(HYPDec),
- CODE(KEXG_X_IND),
- CODE(KRCL + OIND_R),
- CODE(KRCL + OI),
- CODE(KRECT),
- CODE(KEXG_XI),
- CODE(KDSE),
- CODE(KISG),
- CODE(KX_GT_Y),
- CODE(KPSE),
- CODE(KCLR_SIGMA),
- ACT(ICLR_PRGM),
- CODE(KCLR_REG),
- ACT(ICLR_PREFIX),
- CODE(KRANDOM),
- CODE(KPERM),
- CODE(KHMS),
- CODE(KTO_RAD),
- CODE(KX_NE_Y),
- INVALID(), INVALID(), INVALID(), /* ON, f & g */
- CODE(KFRAC),
- ACT(IUSER),
- INVALID(), /* dosen't exist */
- CODE(KFACT),
- CODE(KESTIMATE),
- CODE(KLR),
- CODE(KX_EQ_Y),
- INVALID(), INVALID(),
- /* finally, g codes */
- CODE(KSQR),
- CODE(KLN),
- CODE(KLOG),
- CODE(KPERC),
- CODE(KDELTA_PERC),
- CODE(KABS),
- CODE(KDEG),
- CODE(KRAD),
- CODE(KGRD),
- CODE(KX_LT_0),
- ACT(IBST),
- PREFIX(ARCHYPDec),
- CODE(KARC + OSIN),
- CODE(KARC + OCOS),
- CODE(KARC + OTAN),
- CODE(KPOLAR),
- PREFIX(SFDec),
- PREFIX(CFDec),
- PREFIX(SETDec),
- CODE(KX_GT_0),
- ACT(IP_R),
- CODE(KRTN),
- CODE(KRUP),
- CODE(KRND),
- CODE(KCLX),
- CODE(KLSTX),
- CODE(KCOMB),
- CODE(KHR),
- CODE(KTO_DEG),
- CODE(KX_NE_0),
- INVALID(), INVALID(), INVALID(),
- CODE(KINT),
- ACT(IMEM),
- INVALID(),
- CODE(KMEAN),
- CODE(KSDEV),
- CODE(KSIGMA_SUB),
- CODE(KX_EQ_0),
- INVALID(), INVALID()
- };
-
-