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

  1. /* Title:   ->  $.!hp11.amiga.c.arc_hp11  */
  2.  
  3. /*  Modifications for ANSI C under RISC-OS:
  4.  *
  5.  *     Date               Modification
  6.  *  11-may-90   modifications of headers
  7.  *  11-may-90   removal of non-relevant definitions
  8.  *  23-may-90   modification of display() and sizes
  9.  *  09-jul-90   correcton of flag handling in ExecIns()
  10.  */
  11.  
  12. #define TRACE 0
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <math.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19.  
  20. #include "wimp.h"
  21. #include "wimpt.h"
  22. #include "win.h"
  23. #include "event.h"
  24. #include "baricon.h"
  25. #include "res.h"
  26. #include "resspr.h"
  27. #include "menu.h"
  28. #include "template.h"
  29. #include "dbox.h"
  30. #include "werr.h"
  31. #include "flex.h"
  32. #include "heap.h"
  33. #include "trace.h"
  34. #include "font.h"
  35. #include "bbc.h"
  36. #include "coords.h"
  37. #include "alarm.h"
  38. #include "saveas.h"
  39. #include "os.h"
  40.  
  41. #include "types.h"
  42. #include "hp11.h"
  43. #include "ami_amiga.h"
  44. #include "io.h"
  45. #include "support.h"
  46. #include "ins.h"
  47. #include "codes.h"
  48. #include "arc_hp11.h"
  49. #include "loadsave.h"
  50.  
  51. #define CHAROFFX 0 /* offset in display for first char */
  52. #define CHAROFFY 6
  53. #define CHARWIDTH 30 /* Size of char */
  54. #define CHARHEIGHT 26
  55.  
  56. #define CR 13
  57. #define BS 8
  58. #define ESC 27
  59.  
  60. /*---------------------------------------------------------------------------*/
  61. /*================                 CONSTANTS                   ==============*/
  62. /*---------------------------------------------------------------------------*/
  63. /* Menu items */
  64. #define hp11_menu_info     1
  65. #define hp11_menu_quit     2
  66.  
  67. #define HP11_POPUP_STACK   1
  68. #define HP11_POPUP_SAVE    2
  69. #define HP11_POPUP_RESET   3
  70. #define HP11_POPUP_SPEED   4
  71. #define HP11_POPUP_RADIX   5
  72.  
  73. #define HP11_POPUP_STATUS  1
  74. #define HP11_POPUP_PROGRAM 2
  75. #define HP11_POPUP_DISPLAY 3
  76.  
  77.  
  78. /* Info box field for the version string */
  79. #define hp11_info_field    4
  80.  
  81. /* Radix box fields */
  82. #define radix_point 0
  83. #define radix_comma 1
  84.  
  85. /* Speed box fields */
  86. #define speed_slow 0
  87. #define speed_fast 1
  88.  
  89. /*---------------------------------------------------------------------------*/
  90. /*================                GLOBAL DATA                  ==============*/
  91. /*---------------------------------------------------------------------------*/
  92. /* The top of the menu tree */
  93. menu hp11_menu;
  94. menu hp11_pop_up;
  95. menu hp11_popup_savemenu;
  96.  
  97. /* Handle for the hp11 dbox etc.*/
  98. dbox    hp11_dbox_handle, hp11_stack_dbox_handle;
  99. wimp_w  hp11_win_handle, hp11_save_window;
  100.  
  101. /* information about the display icon */
  102. wimp_icon    hp11_disp;
  103.  
  104. /* Annunciator icon info */
  105. wimp_i        hp11_display, hp11_user, hp11_f, hp11_g;
  106. wimp_i        hp11_rad, hp11_grad, hp11_pgrm;
  107.  
  108. /* Flag - is the window open */
  109. BOOL hp11_window_open = FALSE;
  110.  
  111. /* Application version */
  112. static char *hp11_Version_String = "0.5 (28 June 1990)";
  113.  
  114. BOOL hp11_show_stack  = FALSE;
  115.  
  116. struct Regs hp11r;
  117. int running, fast, error, skip;
  118. int PC, retStack[MAXSTACK], retCnt;
  119. int Flags;
  120.  
  121. int quit, on = TRUE; /* Set to false to quit */
  122.  
  123. enum AlarmType Pause = Paused, Wait = Waiting, ProgStep = ProgramStep;
  124. enum AlarmType *hp11_alarm = &ProgStep;
  125. int waiting, paused, ProgStepWait, ProgStepTime;
  126. BOOL oldrun;
  127.  
  128. int key;
  129. enum KeyStatus keyflag;
  130. int comma;
  131.  
  132. extern struct Border *hp11char[]; /* Character descriptions (as connected
  133.   lines) */
  134. static char *hp11_s; /* this global pointer will be set equal to the argument
  135.                         of Display(), so that the event handler can find it. */
  136. /*---------------------------------------------------------------------------*/
  137. /*================             WINDOW FUNCTIONS                ==============*/
  138. /*---------------------------------------------------------------------------*/
  139.  
  140. void hp11_close_window(void)
  141. {
  142.   dbox_hide(hp11_dbox_handle);
  143.   dbox_hide(hp11_stack_dbox_handle);
  144.   hp11_window_open = FALSE;
  145. }
  146.  
  147. static void hp11_redraw_window(wimp_w handle)
  148. {
  149.   void Display_2(wimp_redrawstr);
  150.   int            more;
  151.   wimp_redrawstr r;
  152.  
  153.   /* Start the redraw */
  154.   r.w = handle;
  155.   wimpt_noerr(wimp_redraw_wind(&r, &more));
  156.   while (more)                                 /* Do the redraw loop */
  157.   {
  158.     Display_2(r);
  159.     wimp_get_rectangle(&r, &more);
  160.   }
  161. }
  162.  
  163. /*---------------------------------------------------------------------------*/
  164. /*================              INITIALISATION                 ==============*/
  165. /*---------------------------------------------------------------------------*/
  166.  
  167. /*---------- Initialise the program, returning TRUE if it was all OK. -------*/
  168. BOOL ArcInit(void)
  169. {
  170.  
  171. /*-------------- Application and RISC_OSlib initialisation ------------------*/
  172.   wimpt_init("HP11");                          /* Main Wimp initialisation   */
  173.   res_init("HP11");                            /* Resources                  */
  174.   resspr_init();                               /* Application sprites        */
  175.   template_init();                             /* Templates                  */
  176.   dbox_init();                                 /* Dialogue boxes             */
  177.   flex_init();                                 /* flex system (for heap)     */
  178.   heap_init(TRUE);                             /* heap memory allocation     */
  179.   alarm_init();                                /* Alarm system               */
  180.  
  181. /*------------------------ Create the main window ---------------------------*/
  182.   hp11_dbox_handle = dbox_new("hp11");
  183.   if (hp11_dbox_handle == 0) return FALSE;         /* Window creation failed */
  184.   hp11_win_handle = (wimp_w)dbox_syshandle(hp11_dbox_handle);
  185.   hp11_dat.hp11_w = hp11_win_handle;
  186.  
  187. /*----------------------- Create the stack window ---------------------------*/
  188.   hp11_stack_dbox_handle = dbox_new("stack");
  189.   if (hp11_stack_dbox_handle == 0) return FALSE;   /* Window creation failed */
  190.  
  191. /*--------------------------- Create the menus ------------------------------*/
  192.   if(hp11_menu = menu_new("HP11", ">Info,Quit"), hp11_menu == NULL)
  193.     return FALSE;                                      /* Menu create failed */
  194.   if(hp11_pop_up = menu_new("HP11", "Stack,Save,Reset,>Speed,>Radix"),
  195.                   hp11_pop_up == NULL) return FALSE;
  196.   if(hp11_popup_savemenu = menu_new("Save", ">Status,>Program,>Display"),
  197.                   hp11_popup_savemenu == NULL) return FALSE;
  198.   menu_submenu(hp11_pop_up, HP11_POPUP_SAVE, hp11_popup_savemenu);
  199.  
  200. /*---------------------- Set up the icon on the icon bar --------------------*/
  201.   baricon("!hp11", (int)resspr_area(), hp11_iconclick);
  202.  
  203. /*------------------------ Declare the event handlers -----------------------*/
  204.   if (!event_attachmenu(win_ICONBAR, hp11_menu, hp11_menuproc, &hp11_dat))
  205.     return FALSE;                                   /* Unable to attach menu */
  206.   if (!event_attachmenu(hp11_win_handle, hp11_pop_up, hp11_popup_proc,
  207.     &hp11_dat)) return FALSE;                       /* Unable to attach menu */
  208.   win_register_event_handler(win_ICONBARLOAD, hp11_bar_load, &hp11_dat);
  209. /*------------------------------ All went ok --------------------------------*/
  210.   return TRUE;
  211. }
  212. /*---------------------------------------------------------------------------*/
  213. /*================               MAIN PROGRAM                  ==============*/
  214. /*---------------------------------------------------------------------------*/
  215.  
  216. /*---------------------------- Main entry point -----------------------------*/
  217. int main(int argc, char * argv[])
  218. {
  219.   os_filestr infile;
  220.  
  221.   if (! trace_is_on()) trace_on(); /* this compiles to nothing if TRACE is 0 */
  222. /*  tracef(" argc = %d\n argv[0] = %s\n argv[1] = %s\n", argc, argv[0],argv[1]);*/
  223.  
  224.   if (Init())
  225.     {
  226.     if (argc >= 2)
  227.       {                    /* load a file identified on the command line */
  228.       infile.action   = 12;               /* Load named file into memory */
  229.       infile.name     = argv[1];
  230.       infile.loadaddr = (int)&hp11r;               /* Where to load file */
  231.       infile.execaddr = 0;                       /* Load at address flag */
  232.       wimpt_complain(os_file(&infile));
  233.       dbox_showstatic(hp11_dbox_handle);
  234.       dbox_raw_eventhandler(hp11_dbox_handle, hp11_dbox_raw_handler,&hp11_dat);
  235.       dbox_eventhandler(hp11_dbox_handle, hp11_dbox_handler, &hp11_dat);
  236.       hp11_window_open = TRUE;
  237.       wimpt_complain(wimp_get_icon_info(hp11_win_handle, HP11_DISPLAY, 
  238.                                                                   &hp11_disp));
  239.       Disp();
  240.       }
  241.  
  242.     while (TRUE) event_process();                 /* The main event loop */
  243.     }
  244.   return 0;
  245. }
  246. /*---------------------------------------------------------------------------*/
  247. /*================              EVENT HANDLERS                 ==============*/
  248. /*---------------------------------------------------------------------------*/
  249. BOOL hp11_dbox_raw_handler(dbox hp11_d, void *ev, void *handle)
  250.   {                                        
  251.   wimp_eventstr *event;
  252.  
  253.   event = (wimp_eventstr *)ev;
  254.   switch (event->e) 
  255.     {
  256.     case wimp_EREDRAW:                  /* need to redraw the display window */
  257.       hp11_redraw_window(event->data.o.w);
  258.       return(TRUE);
  259.       break;
  260.  
  261.     case wimp_ECLOSE:                         /* hide the calculator display */
  262.       dbox_hide(hp11_dbox_handle);
  263.       dbox_hide(hp11_stack_dbox_handle);
  264.       hp11_window_open = FALSE;
  265.       return(TRUE);
  266.       break;
  267.  
  268.     case wimp_ESEND:
  269.     case wimp_ESENDWANTACK:
  270.       {
  271.       switch (event->data.msg.hdr.action)
  272.         {
  273.         case wimp_MDATASAVE:   /* import data */
  274.           hp11_load_ram(handle);
  275.           break;
  276.         case wimp_MDATALOAD:   /* insert data */
  277.         case wimp_MDATAOPEN:
  278.           hp11_load_file(handle);
  279.           break;
  280.         default: break;        /* ignore other messages */
  281.         }
  282.       }
  283.     }  
  284.   return (FALSE);    /* this will later include file transfer actions */
  285. }
  286.  
  287. void hp11_dbox_handler(dbox hp11_d, void *handle)
  288.   {
  289.   static int  code;
  290.   int  j;
  291.   enum KeyTypes type;
  292.   dbox_field i;
  293.  
  294.   handle = handle;  /* stops compiler warning about non-use of handle */
  295.  
  296.   /* respond to each type of calculator key click (reference by key-code) */
  297.   i = dbox_get(hp11_d);
  298.  
  299.   if (!waiting) {
  300.     if (i < 36) key = (int)(i-1);
  301.     else key = (int)i;
  302.     overflow = FALSE;
  303.     if (running || paused)                            /* Run or paused mode */
  304.       {
  305.       alarm_remove(ProgStepTime, (void *)hp11_alarm);
  306.       error = FALSE;
  307.       running = FALSE;                                /* User pressed a key */
  308.       oldrun = FALSE;
  309.       Disp();
  310.       }
  311.     else  /* normal mode operation */
  312.       {
  313.       if (error) {
  314.         error = FALSE;
  315.         Disp();
  316.         }
  317.       else {
  318.         type = ReadKey(&code);
  319.         if (type != Null) {
  320.           if (!inprog) {                        /* not in program entry mode */
  321.             switch (type) {                    /* Read an instruction/action */
  322.               case Action:
  323.                 ExecAction(code);            /* Execute corresponding action */
  324.                 break;
  325.               case Instruction:
  326.                 ExecIns(code);                      /* Interpret instruction */
  327.                 break;
  328.               default:
  329.                 break;
  330.               };
  331.             if (!error && !waiting) Disp();
  332.             }
  333.           else {                                    /* in program entry mode */
  334.             DisplayLine();
  335.             switch (type) {
  336.               case Instruction:                                   /* Save it */
  337.                 if (lastIns == MAXPROG) Error('4');           /* Memory full */
  338.                 else {
  339.                   for (j = lastIns; j > PC; j--) Prog[j + 1] = Prog[j]; 
  340.                                                           /* Move program up */
  341.                   lastIns++;
  342.                   Prog[++PC] = code;                    /* store instruction */
  343.                   retCnt = 0;                          /* Empty return stack */
  344.                 };
  345.                 break;
  346.               case Action:                                      /* Act on it */
  347.                 if (code >= IGTO_LINE) GTOLine(code - IGTO_LINE);
  348.                 else switch (code) {
  349.                   case ION: on = inprog = !RelKey(); break; /* Allow user to
  350.                                                              change his mind */
  351.                   case IP_R: case IRESET: 
  352.                     inprog = FALSE; DispPRGM(FALSE); break;/* exit prog mode */
  353.                   case IMEM: MEM(); break;
  354.                   case IBACK:                                 /* delete line */
  355.                     if (PC != 0) {
  356.                       for (j = PC; j < lastIns; j++) Prog[j] = Prog[j + 1]; 
  357.                                                               /* delete line */
  358.                       lastIns--;
  359.                       PC--;
  360.                       retCnt = 0;           /* empty stack when prog changed */
  361.                       }
  362.                     break;
  363.                   case ISST: if (PC++ == lastIns) PC = 0; break;
  364.                   case IBST: if (PC-- == 0) PC = lastIns; break;
  365.                   case IUSER: USER(); break;
  366.                   case ICLR_PRGM: lastIns = PC = 0; break;
  367.                   }
  368.                 break;
  369.               }
  370.             RelKey();
  371.             }
  372.           if (!error && !waiting) {
  373.             if (inprog) DisplayLine();
  374.             else Disp();
  375.             }
  376.           }
  377.         }
  378.       }
  379.     }
  380.   }
  381.  
  382. /*--- Display the program info box - called from the menu processor. ---*/
  383. static void hp11_info_about_program(void)
  384. {
  385.   dbox  d;  /* Dialogue box handle */
  386.  
  387.   if (d = dbox_new("ProgInfo"), d != NULL)        /* Create the dialogue box */
  388.   {
  389.     dbox_setfield(d, hp11_info_field, hp11_Version_String);
  390.                                                /* Fill in the version number */
  391.     dbox_show(d);                                   /* Show the dialogue box */
  392.     dbox_fillin(d);               /* Keep it on the screen as long as needed */
  393.     dbox_dispose(&d);                         /* Dispose of the dialogue box */
  394.   }
  395. }
  396.  
  397. /*--- Event handler for the menu. ---*/
  398. void hp11_menuproc(void *handle, char *hit)
  399. {
  400.   handle = handle; /* We don't need handle: this stops compiler warning */
  401.  
  402.   /* Find which menu item was hit and take action as appropriate */
  403.   switch (hit[0])
  404.   {
  405.     case hp11_menu_info:
  406.       hp11_info_about_program();
  407.       break;
  408.  
  409.     case hp11_menu_quit:
  410.       /* Exit from the program. The wimp gets rid of the window and icon */
  411.       exit(0);
  412.   }
  413. }
  414.  
  415. static void hp11_get_radix(void)
  416. {
  417.   dbox  d;  /* Dialogue box handle */
  418.   dbox_field button;
  419.  
  420.   if (d = dbox_new("Radix"), d != NULL)           /* Create the dialogue box */
  421.   {
  422.     if (comma) {                               /* set up the selected values */
  423.       dbox_setnumeric(d, radix_point, 0);
  424.       dbox_setnumeric(d, radix_comma, 1);
  425.       }
  426.     else {
  427.       dbox_setnumeric(d, radix_point, 1);
  428.       dbox_setnumeric(d, radix_comma, 0);
  429.       }
  430.     dbox_show(d);
  431.     do {
  432.       button = dbox_fillin(d);
  433.       comma = dbox_getnumeric(d, radix_comma);
  434.       Disp();
  435.       } while (dbox_persist());
  436.     dbox_dispose(&d);                         /* Dispose of the dialogue box */
  437.   }
  438. }
  439.  
  440. static void hp11_get_speed(void)
  441. {
  442.   dbox  d;  /* Dialogue box handle */
  443.   dbox_field button;
  444.  
  445.   if (d = dbox_new("Speed"), d != NULL)           /* Create the dialogue box */
  446.   {
  447.     if (fast) {                                /* set up the selected values */
  448.       dbox_setnumeric(d, speed_slow, 0);
  449.       dbox_setnumeric(d, speed_fast, 1);
  450.       }
  451.     else {
  452.       dbox_setnumeric(d, speed_slow, 1);
  453.       dbox_setnumeric(d, speed_fast, 0);
  454.       }
  455.     dbox_show(d);                            
  456.     do {
  457.       button = dbox_fillin(d);
  458.       fast = dbox_getnumeric(d, speed_fast);
  459.       } while (dbox_persist());
  460.     dbox_dispose(&d);                         /* Dispose of the dialogue box */
  461.   }
  462. }
  463.  
  464. /*--- Event handler for the pop-up menu. ---*/
  465. void hp11_popup_proc(void *handle, char *hit)
  466. {
  467.   hp11_data *d = (hp11_data *)handle;
  468.  
  469.   /* Find which menu item was hit and take action as appropriate */
  470.   switch (hit[0])
  471.   {
  472.     case HP11_POPUP_STACK:
  473.       if (hp11_show_stack)
  474.         {
  475.         menu_setflags(hp11_pop_up, HP11_POPUP_STACK, 0, 0);
  476.         hp11_show_stack = FALSE;
  477.         dbox_hide(hp11_stack_dbox_handle);
  478.         }
  479.       else
  480.         {
  481.         menu_setflags(hp11_pop_up, HP11_POPUP_STACK, 1, 0);
  482.         hp11_show_stack = TRUE;
  483.         Disp();                                    /* fill in contents */
  484.         dbox_showstatic(hp11_stack_dbox_handle);
  485.         }
  486.       break;
  487.  
  488.     case HP11_POPUP_SAVE:                        /* Initiate save operation */
  489.       switch (hit[1]) {
  490.         case HP11_POPUP_STATUS:      /* Complete status, including propgram */
  491.           hp11_save_window = hp11_win_handle;   /* this is to check if the   */
  492.                                                /* save is to the same window */
  493.           saveas(HP11_STATUSTYPE, d->stat, 2000, hp11_stat_saver, 
  494.                hp11_stat_sender, 0, handle);
  495.           hp11_save_window = -1;
  496.           break;
  497.         case HP11_POPUP_PROGRAM:                         /* Program listing */
  498.           saveas(HP11_PROGTYPE, d->prog, 2000, hp11_prog_saver, 
  499.                hp11_prog_sender, 0, handle);
  500.           break;
  501.         case HP11_POPUP_DISPLAY:                              /* X-register */
  502.           saveas(HP11_DISPLAYTYPE, d->disp, 2000, hp11_disp_saver, 
  503.                hp11_disp_sender, 0, handle);
  504.           break;
  505.         }
  506.       break;
  507.  
  508.     case HP11_POPUP_RESET:
  509.       HP11ColdReset();
  510.       break;
  511.  
  512.     case HP11_POPUP_SPEED:
  513.       if(hit[1] != 0) {  /* don't respond to menu entry clicks */
  514.         hp11_get_speed();
  515.         }
  516.       break;
  517.  
  518.     case HP11_POPUP_RADIX:
  519.       if(hit[1] != 0) {  /* don't respond to menu entry clicks */
  520.         hp11_get_radix();
  521.         }
  522.       break;
  523.   }
  524. }
  525.  
  526.  
  527. /*--- Event handler called on a left click on the icon. ---*/
  528. void hp11_iconclick(wimp_i icon)
  529.   {
  530.   icon = icon; /* We don't need the handle: this stops compiler warning */
  531.  
  532.   /* Open the window - only one allowed */
  533.   if (!hp11_window_open)
  534.     {
  535.     dbox_showstatic(hp11_dbox_handle);
  536.     dbox_raw_eventhandler(hp11_dbox_handle, hp11_dbox_raw_handler, &hp11_dat); 
  537.     dbox_eventhandler(hp11_dbox_handle, hp11_dbox_handler, &hp11_dat);
  538.     hp11_window_open = TRUE;
  539.     wimpt_complain(wimp_get_icon_info(hp11_win_handle,HP11_DISPLAY,&hp11_disp));    Disp();
  540.     if (hp11_show_stack)
  541.       {
  542.       dbox_showstatic(hp11_stack_dbox_handle);
  543.       }
  544.     }
  545.   }
  546.  
  547. /*---------------------------------------------------------------------------*/
  548. /*================            ORIGINAL FUNCTIONS               ==============*/
  549. /*---------------------------------------------------------------------------*/
  550.  
  551. void ExecAction(int act) /* Execute an Action in normal mode */
  552. {
  553.   if (act >= IGTO_LINE) { /* GTO .nnn where act = IGTO_LINE + nnn */
  554.     entering = FALSE; /* Digit entry disabled */
  555.     GTOLine(act - IGTO_LINE);
  556.   }
  557.   else switch (act) {
  558.     case ISST: SST(); break;
  559.     case IBST: BST(); break;
  560.     case IP_R: ProgramEntry(); break;
  561.     case IUSER: USER(); break;
  562.     case ICLR_PRGM: RTN(); break; /* Clear Prgm = RTN() in normal mode */
  563.     case ION: on = !RelKey(); break; /* Allow user to change his mind */
  564.     case IMEM: MEM(); break;
  565.     case ICLR_PREFIX: PREFIX(); break;
  566.     case IBACK:if (entering) EnterNum(-IBACK); /* Correct during digit entry
  567.  */
  568.             else CLX();
  569.             break;
  570.   }
  571. }
  572.  
  573. void ExecIns(ins) /* Execute an instruction (normal or run mode) */
  574. register int ins;
  575. {
  576.   skip = FALSE;
  577.  
  578.   if (ins < KCOMPLEX) (*insfunc[ins])(); /* miscellaneous ins */
  579.   else if (ins < KFLAGS + OCF) SF(ins - (KFLAGS + OSF));
  580.   else if (ins < KFLAGS + OSET) CF(ins - (KFLAGS + OCF));
  581.   else if (ins < KFIG) Set(ins - (KFLAGS + OSET));
  582.   else if (ins < KFIX) EnterNum(ins);
  583.   else if (ins < KSCI) FIX(ins - KFIX);
  584.   else if (ins < KENG) SCI(ins - KSCI);
  585.   else if (ins < KLBL) ENG(ins - KENG);
  586.   else if (ins < KGTO) ENABLE() /* LBL : ignore, just enable stack */;
  587.   else if (ins < KGSB) GTO(ins - KGTO);
  588.   else if (ins < KSTO) GSB(ins - KGSB);
  589.   else if (ins < KRCL) STO((ins - KSTO) % OPLUS,
  590.                    (enum StoTypes)((ins - KSTO) / OPLUS));
  591.     /* compute type of operation to do : there are 21 regs per operation,
  592.       + indirect, the codes are sequential : all the stos, all the pluses ...
  593.       PLUS is first so it is 22, hence the division to determine the operation */
  594.   else if (ins < KRCL + 22) RCL(ins - KRCL);
  595.  
  596.   X = Check(X); Y = Check(Y); /* Check the values in X & Y to conform to HP11
  597.     limits. The testing is done here to save code space so that all the
  598.  instructions
  599.     don't have to include the check */
  600. }
  601.  
  602. BOOL Init(void) /* Initialize the calculator, return FALSE if it fails */
  603. {
  604.   DEG(); /* Initial state, all other values are zero */
  605.   FIX(4);
  606.   keyflag = NewSeq;
  607.   inprog = FALSE;
  608.   waiting = FALSE;
  609.   ProgStepWait = FALSE;
  610.   PC = 0;
  611.   Rdoffset = 0;
  612.  
  613.   /* Computer specific intialisation, can load an initial program */
  614.   return(ArcInit());
  615. }
  616.  
  617. static void CleanUp(void)
  618. {
  619.   ArcCleanUp();
  620. }
  621.  
  622.  
  623. /* Wait for latest key to be released, returns true if button released
  624.   over key (valid only for mouse) */
  625. BOOL RelKey()
  626. {
  627.   return(TRUE);
  628. }
  629.  
  630. /* Display s */
  631. void Display(s)
  632. register char *s;
  633. {
  634.    wimp_wstate    hp11_wind;
  635.    wimp_redrawstr hp11_redraw;
  636.  
  637.    hp11_s = s;  /* set up global variable so event handler can find string */
  638.  
  639.    /* Find coords of display and mark as invalid.  This will cause the
  640.       Window Manager to clear the display and issue one or more redraw
  641.       requests.  These must be handled by the raw event handler. */
  642.  
  643.    wimp_get_wind_state(hp11_win_handle, &hp11_wind);
  644.    hp11_redraw.w = hp11_win_handle;
  645.    hp11_redraw.box.x0 = hp11_disp.box.x0;
  646.    hp11_redraw.box.y0 = hp11_disp.box.y0;
  647.    hp11_redraw.box.x1 = hp11_disp.box.x1;
  648.    hp11_redraw.box.y1 = hp11_disp.box.y1;
  649.    wimpt_noerr(wimp_force_redraw(&hp11_redraw));
  650.  
  651. /* now set a flag to indicate to event handler what should be redrawn */
  652. /* ie main display, stack etc */
  653.  
  654. }
  655.  
  656. void Display_2(wimp_redrawstr hp11_redraw)
  657. {
  658.   void     DrawChar(struct Border *, int, int);
  659.   int      posx, posy;
  660.   int      chr;
  661.   char    *save;
  662.   wimp_box disp;
  663.  
  664.   save = hp11_s;
  665.  
  666.   disp.x0 = hp11_disp.box.x0;
  667.   disp.y0 = hp11_disp.box.y0;
  668.   disp.x1 = hp11_disp.box.x1;
  669.   disp.y1 = hp11_disp.box.y1;
  670.   coords_box_toscreen(&disp, (coords_cvtstr *)&hp11_redraw.box); 
  671.   posx = disp.x0 + CHAROFFX;
  672.   posy = disp.y1 - CHAROFFY;
  673.  
  674.   while (*hp11_s) {
  675.     switch (*hp11_s) { /* Position of char in char array */
  676.       case '0': case '1': case '2': case '3': case '4': case '5':
  677.       case '6': case '7': case '8': case '9': chr = *hp11_s - '0'; break;
  678.       case '-': chr = 10; break;
  679.       case 'E': chr = 11; break;
  680.       case 'r': chr = 12; break;
  681.       case 'o': chr = 13; break;
  682.       case 'R': chr = 14; break;
  683.       case 'u': chr = 15; break;
  684.       case 'n': chr = 16; break;
  685.       case 'i': chr = 17; break;
  686.       case 'g': chr = 18; break;
  687.       case '.': chr = 19; break;
  688.       case ',': chr = 20; break;
  689.       case 'P': chr = 21; break;
  690.       default:  chr = -1; break;
  691.     }
  692.     if (chr != -1) DrawChar(hp11char[chr], posx, posy);
  693.     if (*hp11_s != '.' && *hp11_s != ',') posx += CHARWIDTH;
  694.     hp11_s++;
  695.   }
  696.   hp11_s = save;  /* restore pointer value for next function call */
  697. }
  698.  
  699. void DrawChar(struct Border *hp11char, int posx, int posy)
  700. {
  701.   int            i, x;
  702.   WORD          *pos;
  703.   struct Border *stroke;
  704.  
  705.   stroke = hp11char;
  706.   do {
  707.     pos = stroke->SegDef;
  708.     for (i=1; i<4; i++, pos++) {
  709.       x = *pos;                       
  710.       bbc_move(posx+x, posy-*(++pos));
  711.       x = *(++pos);                   
  712.       bbc_draw(posx+x, posy-*(++pos));
  713.     }
  714.     stroke = stroke->NextSeg;
  715.   } while (stroke != NULL);
  716. }
  717.  
  718. /* Display the indicators */
  719. void Dispf(on)
  720. int on;
  721. {
  722.   if(on)
  723.     wimp_set_icon_state(hp11_win_handle,HP11_f,wimp_ISELECTED,wimp_ISELECTED);
  724.   else
  725.     wimp_set_icon_state(hp11_win_handle, HP11_f, 0, wimp_ISELECTED);
  726. }
  727.  
  728. void Dispg(on)
  729. int on;
  730. {
  731.   if(on)
  732.     wimp_set_icon_state(hp11_win_handle,HP11_g,wimp_ISELECTED,wimp_ISELECTED);
  733.   else
  734.     wimp_set_icon_state(hp11_win_handle, HP11_g, 0, wimp_ISELECTED);
  735. }
  736.  
  737. void DispUSER(on)
  738. int on;
  739. {
  740.   if(on)
  741.     wimp_set_icon_state(hp11_win_handle, HP11_USER, wimp_ISELECTED, 
  742.                                                                wimp_ISELECTED);
  743.   else
  744.     wimp_set_icon_state(hp11_win_handle, HP11_USER, 0, wimp_ISELECTED);
  745. }
  746.  
  747. void DispG(on)
  748. int on;
  749. {
  750.   if(on)
  751.     wimp_set_icon_state(hp11_win_handle, HP11_GRAD, wimp_ISELECTED,
  752.                                                                wimp_ISELECTED);
  753.   else
  754.     wimp_set_icon_state(hp11_win_handle, HP11_GRAD, 0, wimp_ISELECTED);
  755. }
  756.  
  757. void DispRAD(on)
  758. int on;
  759. {
  760.   if(on)
  761.     wimp_set_icon_state(hp11_win_handle, HP11_RAD, wimp_ISELECTED,
  762.                                                                wimp_ISELECTED);
  763.   else
  764.     wimp_set_icon_state(hp11_win_handle, HP11_RAD, 0, wimp_ISELECTED);
  765. }
  766.  
  767. void DispPRGM(on)
  768. int on;
  769. {
  770.   if(on)
  771.     wimp_set_icon_state(hp11_win_handle, HP11_PGRM, wimp_ISELECTED,
  772.                                                                wimp_ISELECTED);
  773.   else
  774.     wimp_set_icon_state(hp11_win_handle, HP11_PGRM, 0, wimp_ISELECTED);
  775. }
  776.  
  777. void beep(void)
  778. {
  779.    bbc_vdu(7);
  780. }
  781.  
  782. void EditOn(void){}
  783. void EditOff(void){}
  784. void sleep(void){}
  785. void MenusOn(void){}
  786. void MenusOff(void){}
  787. void ArcCleanUp(void){}
  788.  
  789. void Wait50(long tick)
  790. {
  791.   enum AlarmType *handle;
  792.  
  793.   waiting = TRUE;
  794.   handle = &Wait;
  795.  
  796.   alarm_set(alarm_timenow() + 2*(int)tick, hp11_alarm_proc, (void *)handle);
  797. }
  798.  
  799. void hp11_alarm_proc(int called_at, void *handle)
  800. /* gets control when the alarm timer goes off. Has to decide why it
  801.    was called, and what to do about it */
  802. {
  803.   enum AlarmType *action  = (enum AlarmType *)handle;
  804.  
  805.   switch(*action) {
  806.     case Waiting:
  807.       waiting = FALSE;
  808.       if (inprog) DisplayLine();
  809.       else Disp();
  810.       break;
  811.     case Paused:
  812.       waiting = FALSE;
  813.       paused = FALSE;
  814.       running = oldrun;
  815.       if (running) {
  816.         if (fast) FastRun();
  817.         else SlowRun();
  818.         }
  819.       break;
  820.     case ProgramStep:
  821.       SlowRun();
  822.       break;
  823.     }
  824.   }
  825.  
  826. void SlowRun(void)
  827. {
  828.   Disp();
  829.   ExecIns(Prog[PC]);                               /* Exec current ins */
  830.   if (error || overflow) {
  831.     running = FALSE;                         /* An error occured, halt */
  832.     oldrun = FALSE;
  833.     }
  834.   else { 
  835.     if (skip) PC++;    /* A conditional instruction asked for the next
  836.                                                instruction to be skipped */
  837.     PC++;
  838.     while (PC > lastIns)         /* There is an implicit return at the */
  839.       {                          /* end of the program                 */
  840.       RTN();
  841.       PC++;
  842.       }
  843.     }
  844.   if (running) {
  845.     hp11_alarm = &ProgStep;
  846.     ProgStepTime = alarm_timenow() + 5;
  847.     alarm_set(ProgStepTime, hp11_alarm_proc, (void *)hp11_alarm);
  848.     }
  849.   else if (!error && !paused) Disp();
  850. }
  851.  
  852. void FastRun(void)
  853. {
  854.   while (running)
  855.   {
  856.   ExecIns(Prog[PC]);                                     /* Exec current ins */
  857.   if (error || overflow) {                         /* An error occured, halt */
  858.     running = FALSE;
  859.     oldrun = FALSE;
  860.     }
  861.   else
  862.     { 
  863.     if (skip) PC++;          /* A conditional instruction asked for the next
  864.                                                    instruction to be skipped */
  865.     PC++;
  866.     while (PC > lastIns)               /* There is an implicit return at the */
  867.       {                                /* end of the program                 */
  868.       RTN();
  869.       PC++;
  870.       }
  871.     }
  872.   }
  873.   Disp();
  874. }
  875.