home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzS5_src.ZIP / MATH.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-11  |  4.0 KB  |  249 lines

  1. /*
  2.  * math.c
  3.  *
  4.  * Arithmetic, compare and logical opcodes
  5.  *
  6.  */
  7.  
  8. #include "frotz.h"
  9. #include "s5api.h"
  10. /*
  11.  * z_add, 16bit addition.
  12.  *
  13.  *    zargs[0] = first value
  14.  *    zargs[1] = second value
  15.  *
  16.  */
  17.  
  18. void z_add (struct sg *g)
  19. {
  20.  
  21.     store (g,(zword) ((short) g->zargs[0] + (short) g->zargs[1]));
  22.  
  23. }/* z_add */
  24.  
  25. /*
  26.  * z_and, bitwise AND operation.
  27.  *
  28.  *    zargs[0] = first value
  29.  *    zargs[1] = second value
  30.  *
  31.  */
  32.  
  33. void z_and (struct sg *g)
  34. {
  35.  
  36.     store (g,(zword) (g->zargs[0] & g->zargs[1]));
  37.  
  38. }/* z_and */
  39.  
  40. /*
  41.  * z_art_shift, arithmetic SHIFT operation.
  42.  *
  43.  *    zargs[0] = value
  44.  *    zargs[1] = #positions to shift left (positive) or right
  45.  *
  46.  */
  47.  
  48. void z_art_shift (struct sg *g)
  49. {
  50.  
  51.     if ((short) g->zargs[1] > 0)
  52.     store (g,(zword) ((short) g->zargs[0] << (short) g->zargs[1]));
  53.     else
  54.     store (g,(zword) ((short) g->zargs[0] >> - (short) g->zargs[1]));
  55.  
  56. }/* z_art_shift */
  57.  
  58. /*
  59.  * z_div, signed 16bit division.
  60.  *
  61.  *    zargs[0] = first value
  62.  *    zargs[1] = second value
  63.  *
  64.  */
  65.  
  66. void z_div (struct sg *g)
  67. {
  68.  
  69.     if (g->zargs[1] == 0)
  70.     runtime_error (g,"Division by zero");
  71.  
  72.     store (g,(zword) ((short) g->zargs[0] / (short) g->zargs[1]));
  73.  
  74. }/* z_div */
  75.  
  76. /*
  77.  * z_je, branch if the first value equals any of the following.
  78.  *
  79.  *    zargs[0] = first value
  80.  *    zargs[1] = second value (optional)
  81.  *    ...
  82.  *    zargs[3] = fourth value (optional)
  83.  *
  84.  */
  85.  
  86. void z_je (struct sg *g)
  87. {
  88.  
  89.     branch (g,
  90.     g->zargc > 1 && (g->zargs[0] == g->zargs[1] || (
  91.     g->zargc > 2 && (g->zargs[0] == g->zargs[2] || (
  92.     g->zargc > 3 && (g->zargs[0] == g->zargs[3]))))));
  93.  
  94. }/* z_je */
  95.  
  96. /*
  97.  * z_jg, branch if the first value is greater than the second.
  98.  *
  99.  *    zargs[0] = first value
  100.  *    zargs[1] = second value
  101.  *
  102.  */
  103.  
  104. void z_jg (struct sg *g)
  105. {
  106.  
  107.     branch (g,(short) g->zargs[0] > (short) g->zargs[1]);
  108.  
  109. }/* z_jg */
  110.  
  111. /*
  112.  * z_jl, branch if the first value is less than the second.
  113.  *
  114.  *    zargs[0] = first value
  115.  *    zargs[1] = second value
  116.  *
  117.  */
  118.  
  119. void z_jl (struct sg *g)
  120. {
  121.  
  122.     branch (g,(short) g->zargs[0] < (short) g->zargs[1]);
  123.  
  124. }/* z_jl */
  125.  
  126. /*
  127.  * z_jz, branch if value is zero.
  128.  *
  129.  *     zargs[0] = value
  130.  *
  131.  */
  132.  
  133. void z_jz (struct sg *g)
  134. {
  135.  
  136.     branch (g,(short) g->zargs[0] == 0);
  137.  
  138. }/* z_jz */
  139.  
  140. /*
  141.  * z_log_shift, logical SHIFT operation.
  142.  *
  143.  *     zargs[0] = value
  144.  *    zargs[1] = #positions to shift left (positive) or right (negative)
  145.  *
  146.  */
  147.  
  148. void z_log_shift (struct sg *g)
  149. {
  150.  
  151.     if ((short) g->zargs[1] > 0)
  152.     store (g,(zword) (g->zargs[0] << (short) g->zargs[1]));
  153.     else
  154.     store (g,(zword) (g->zargs[0] >> - (short) g->zargs[1]));
  155.  
  156. }/* z_log_shift */
  157.  
  158. /*
  159.  * z_mod, remainder after signed 16bit division.
  160.  *
  161.  *     zargs[0] = first value
  162.  *    zargs[1] = second value
  163.  *
  164.  */
  165.  
  166. void z_mod (struct sg *g)
  167. {
  168.  
  169.     if (g->zargs[1] == 0)
  170.     runtime_error (g,"Division by zero");
  171.  
  172.     store (g,(zword) ((short) g->zargs[0] % (short) g->zargs[1]));
  173.  
  174. }/* z_mod */
  175.  
  176. /*
  177.  * z_mul, 16bit multiplication.
  178.  *
  179.  *     zargs[0] = first value
  180.  *    zargs[1] = second value
  181.  *
  182.  */
  183.  
  184. void z_mul (struct sg *g)
  185. {
  186.  
  187.     store (g,(zword) ((short) g->zargs[0] * (short) g->zargs[1]));
  188.  
  189. }/* z_mul */
  190.  
  191. /*
  192.  * z_not, bitwise NOT operation.
  193.  *
  194.  *     zargs[0] = value
  195.  *
  196.  */
  197.  
  198. void z_not (struct sg *g)
  199. {
  200.  
  201.     store (g,(zword) ~(g->zargs[0]));
  202.  
  203. }/* z_not */
  204.  
  205. /*
  206.  * z_or, bitwise OR operation.
  207.  *
  208.  *    zargs[0] = first value
  209.  *    zargs[1] = second value
  210.  *
  211.  */
  212.  
  213. void z_or (struct sg *g)
  214. {
  215.  
  216.     store (g,(zword) (g->zargs[0] | g->zargs[1]));
  217.  
  218. }/* z_or */
  219.  
  220. /*
  221.  * z_sub, 16bit substraction.
  222.  *
  223.  *    zargs[0] = first value
  224.  *    zargs[1] = second value
  225.  *
  226.  */
  227.  
  228. void z_sub (struct sg *g)
  229. {
  230.  
  231.     store (g,(zword) ((short) g->zargs[0] - (short) g->zargs[1]));
  232.  
  233. }/* z_sub */
  234.  
  235. /*
  236.  * z_test, branch if all the flags of a bit mask are set in a value.
  237.  *
  238.  *    zargs[0] = value to be examined
  239.  *    zargs[1] = bit mask
  240.  *
  241.  */
  242.  
  243. void z_test (struct sg *g)
  244. {
  245.  
  246.     branch (g,(g->zargs[0] & g->zargs[1]) == g->zargs[1]);
  247.  
  248. }/* z_test */
  249.