home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzS5_src.ZIP / VARIABLE.CPP < prev   
Encoding:
C/C++ Source or Header  |  1997-10-12  |  5.1 KB  |  292 lines

  1. /*
  2.  * variable.c
  3.  *
  4.  * Variable and stack related opcodes
  5.  *
  6.  */
  7.  
  8. #include "frotz.h"
  9. #include "s5api.h"
  10.  
  11. /*
  12.  * z_dec, decrement a variable.
  13.  *
  14.  *     zargs[0] = variable to decrement
  15.  *
  16.  */
  17.  
  18. void z_dec (struct sg *g)
  19. {
  20.     zword value;
  21.  
  22.     if (g->zargs[0] == 0)
  23.     (*(g->sp))--;
  24.     else if (g->zargs[0] < 16)
  25.     (*(g->fp - g->zargs[0]))--;
  26.     else {
  27.     zword addr = g->h_globals + 2 * (g->zargs[0] - 16);
  28.     LOW_WORD (addr, value)
  29.     value--;
  30.     SET_WORD (addr, value)
  31.     }
  32.  
  33. }/* z_dec */
  34.  
  35. /*
  36.  * z_dec_chk, decrement a variable and branch if now less than value.
  37.  *
  38.  *     zargs[0] = variable to decrement
  39.  *     zargs[1] = value to check variable against
  40.  *
  41.  */
  42.  
  43. void z_dec_chk (struct sg *g)
  44. {
  45.     zword value;
  46.  
  47.     if (g->zargs[0] == 0)
  48.     value = --(*(g->sp));
  49.     else if (g->zargs[0] < 16)
  50.     value = --(*(g->fp - g->zargs[0]));
  51.     else {
  52.     zword addr = g->h_globals + 2 * (g->zargs[0] - 16);
  53.     LOW_WORD (addr, value)
  54.     value--;
  55.     SET_WORD (addr, value)
  56.     }
  57.  
  58.     branch (g,(short) value < (short) (g->zargs[1]));
  59.  
  60. }/* z_dec_chk */
  61.  
  62. /*
  63.  * z_inc, increment a variable.
  64.  *
  65.  *     zargs[0] = variable to increment
  66.  *
  67.  */
  68.  
  69. void z_inc (struct sg *g)
  70. {
  71.     zword value;
  72.  
  73.     if (g->zargs[0] == 0)
  74.     (*(g->sp))++;
  75.     else if (g->zargs[0] < 16)
  76.     (*(g->fp - g->zargs[0]))++;
  77.     else {
  78.     zword addr = g->h_globals + 2 * (g->zargs[0] - 16);
  79.     LOW_WORD (addr, value)
  80.     value++;
  81.     SET_WORD (addr, value)
  82.     }
  83.  
  84. }/* z_inc */
  85.  
  86. /*
  87.  * z_inc_chk, increment a variable and branch if now greater than value.
  88.  *
  89.  *     zargs[0] = variable to increment
  90.  *     zargs[1] = value to check variable against
  91.  *
  92.  */
  93.  
  94. void z_inc_chk (struct sg *g)
  95. {
  96.     zword value;
  97.  
  98.     if (g->zargs[0] == 0)
  99.     value = ++(*(g->sp));
  100.     else if (g->zargs[0] < 16)
  101.     value = ++(*(g->fp - g->zargs[0]));
  102.     else {
  103.     zword addr = g->h_globals + 2 * (g->zargs[0] - 16);
  104.     LOW_WORD (addr, value)
  105.     value++;
  106.     SET_WORD (addr, value)
  107.     }
  108.  
  109.     branch (g,(short) value > (short) (g->zargs[1]));
  110.  
  111. }/* z_inc_chk */
  112.  
  113. /*
  114.  * z_load, store the value of a variable.
  115.  *
  116.  *    zargs[0] = variable to store
  117.  *
  118.  */
  119.  
  120. void z_load (struct sg *g)
  121. {
  122.     zword value;
  123.  
  124.     if (g->zargs[0] == 0)
  125.     value = *(g->sp);
  126.     else if (g->zargs[0] < 16)
  127.     value = *(g->fp - g->zargs[0]);
  128.     else {
  129.     zword addr = g->h_globals + 2 * (g->zargs[0] - 16);
  130.     LOW_WORD (addr, value)
  131.     }
  132.  
  133.     store (g,value);
  134.  
  135. }/* z_load */
  136.  
  137. /*
  138.  * z_pop, pop a value off the game stack and discard it.
  139.  *
  140.  *    no zargs used
  141.  *
  142.  */
  143.  
  144. void z_pop (struct sg *g)
  145. {
  146.  
  147.     (g->sp)++;
  148.  
  149. }/* z_pop */
  150.  
  151. /*
  152.  * z_pop_stack, pop n values off the game or user stack and discard them.
  153.  *
  154.  *    zargs[0] = number of values to discard
  155.  *    zargs[1] = address of user stack (optional)
  156.  *
  157.  */
  158.  
  159. void z_pop_stack (struct sg *g)
  160. {
  161.  
  162.     if (g->zargc == 2) {        /* it's a user stack */
  163.  
  164.     zword size;
  165.     zword addr = g->zargs[1];
  166.  
  167.     LOW_WORD (addr, size)
  168.  
  169.     size += g->zargs[0];
  170.     storew (g,addr, size);
  171.  
  172.     } else g->sp += g->zargs[0];    /* it's the game stack */
  173.  
  174. }/* z_pop_stack */
  175.  
  176. /*
  177.  * z_pull, pop a value off...
  178.  *
  179.  * a) ...the game or a user stack and store it (V6)
  180.  *
  181.  *    zargs[0] = address of user stack (optional)
  182.  *
  183.  * b) ...the game stack and write it to a variable (other than V6)
  184.  *
  185.  *    zargs[0] = variable to write value to
  186.  *
  187.  */
  188.  
  189. void z_pull (struct sg *g)
  190. {
  191.     zword value;
  192.  
  193.     if (g->h_version != V6) {    /* not a V6 game, pop stack and write */
  194.  
  195.     value = *(g->sp)++;
  196.  
  197.     if (g->zargs[0] == 0)
  198.         *(g->sp) = value;
  199.     else if (g->zargs[0] < 16)
  200.         *(g->fp - g->zargs[0]) = value;
  201.     else {
  202.         zword addr = g->h_globals + 2 * (g->zargs[0] - 16);
  203.         SET_WORD (addr, value)
  204.     }
  205.  
  206.     } else {            /* it's V6, but is there a user stack? */
  207.  
  208.     if (g->zargc == 1) {    /* it's a user stack */
  209.  
  210.         zword size;
  211.         zword addr = g->zargs[0];
  212.  
  213.         LOW_WORD (addr, size)
  214.  
  215.         size++;
  216.         storew (g,addr, size);
  217.  
  218.         addr += 2 * size;
  219.         LOW_WORD (addr, value)
  220.  
  221.     } else value = *(g->sp)++;    /* it's the game stack */
  222.  
  223.     store (g,value);
  224.  
  225.     }
  226.  
  227. }/* z_pull */
  228.  
  229. /*
  230.  * z_push, push a value onto the game stack.
  231.  *
  232.  *    zargs[0] = value to push onto the stack
  233.  *
  234.  */
  235.  
  236. void z_push (struct sg *g)
  237. {
  238.     *--(g->sp) = g->zargs[0];
  239.  
  240. }/* z_push */
  241.  
  242. /*
  243.  * z_push_stack, push a value onto a user stack then branch if successful.
  244.  *
  245.  *    zargs[0] = value to push onto the stack
  246.  *    zargs[1] = address of user stack
  247.  *
  248.  */
  249.  
  250. void z_push_stack (struct sg *g)
  251. {
  252.     zword size;
  253.     zword addr = g->zargs[1];
  254.  
  255.     LOW_WORD (addr, size)
  256.  
  257.     if (size != 0) {
  258.  
  259.     storew (g,(zword) (addr + 2 * size), g->zargs[0]);
  260.  
  261.     size--;
  262.     storew (g,addr, size);
  263.  
  264.     }
  265.  
  266.     branch (g,size);
  267.  
  268. }/* z_push_stack */
  269.  
  270. /*
  271.  * z_store, write a value to a variable.
  272.  *
  273.  *     zargs[0] = variable to be written to
  274.  *      zargs[1] = value to write
  275.  *
  276.  */
  277.  
  278. void z_store (struct sg *g)
  279. {
  280.     zword value = g->zargs[1];
  281.  
  282.     if (g->zargs[0] == 0)
  283.     *(g->sp) = value;
  284.     else if (g->zargs[0] < 16)
  285.     *(g->fp - g->zargs[0]) = value;
  286.     else {
  287.     zword addr = g->h_globals + 2 * (g->zargs[0] - 16);
  288.     SET_WORD (addr, value)
  289.     }
  290.  
  291. }/* z_store */
  292.