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

  1. /*
  2.  * table.c
  3.  *
  4.  * Table handling opcodes
  5.  *
  6.  */
  7.  
  8. #include "frotz.h"
  9. #include "s5api.h"
  10.  
  11. /*
  12.  * z_copy_table, copy a table or fill it with zeroes.
  13.  *
  14.  *    zargs[0] = address of table
  15.  *     zargs[1] = destination address or 0 for fill
  16.  *    zargs[2] = size of table
  17.  *
  18.  * Note: Copying is safe even when source and destination overlap; but
  19.  *       if zargs[1] is negative the table _must_ be copied forwards.
  20.  *
  21.  */
  22.  
  23. void z_copy_table (struct sg *g)
  24. {
  25.     zword addr;
  26.     zword size = g->zargs[2];
  27.     zbyte value;
  28.     short i;
  29.  
  30.     if (g->zargs[1] == 0)                      /* zero table */
  31.  
  32.     for (i = 0; i < size; i++)
  33.         storeb (g,(zword) (g->zargs[0] + i), 0);
  34.  
  35.     else if ((short) size < 0 || g->zargs[0] > g->zargs[1])    /* copy forwards */
  36.  
  37.     for (i = 0; i < (((short) size < 0) ? - (short) size : size); i++) {
  38.         addr = g->zargs[0] + i;
  39.         LOW_BYTE (addr, value)
  40.         storeb (g,(zword) (g->zargs[1] + i), value);
  41.     }
  42.  
  43.     else                        /* copy backwards */
  44.  
  45.     for (i = size - 1; i >= 0; i--) {
  46.         addr = g->zargs[0] + i;
  47.         LOW_BYTE (addr, value)
  48.         storeb (g,(zword) (g->zargs[1] + i), value);
  49.     }
  50.  
  51. }/* z_copy_table */
  52.  
  53. /*
  54.  * z_loadb, store a value from a table of bytes.
  55.  *
  56.  *    zargs[0] = address of table
  57.  *    zargs[1] = index of table entry to store
  58.  *
  59.  */
  60.  
  61. void z_loadb (struct sg *g)
  62. {
  63.     zword addr = g->zargs[0] + g->zargs[1];
  64.     zbyte value;
  65.  
  66.     LOW_BYTE (addr, value)
  67.  
  68.     store (g,value);
  69.  
  70. }/* z_loadb */
  71.  
  72. /*
  73.  * z_loadw, store a value from a table of words.
  74.  *
  75.  *    zargs[0] = address of table
  76.  *    zargs[1] = index of table entry to store
  77.  *
  78.  */
  79.  
  80. void z_loadw (struct sg *g)
  81. {
  82.     zword addr = g->zargs[0] + 2 * g->zargs[1];
  83.     zword value;
  84.  
  85.     LOW_WORD (addr, value)
  86.  
  87.     store (g,value);
  88.  
  89. }/* z_loadw */
  90.  
  91. /*
  92.  * z_scan_table, find and store the address of a target within a table.
  93.  *
  94.  *    zargs[0] = target value to be searched for
  95.  *    zargs[1] = address of table
  96.  *    zargs[2] = number of table entries to check value against
  97.  *    zargs[3] = type of table (optional, defaults to 0x82)
  98.  *
  99.  * Note: The table is a word array if bit 7 of zargs[3] is set; otherwise
  100.  *       it's a byte array. The lower bits hold the address step.
  101.  *
  102.  */
  103.  
  104. void z_scan_table (struct sg *g)
  105. {
  106.     zword addr = g->zargs[1];
  107.     short i;
  108.  
  109.     /* Supply default arguments */
  110.  
  111.     if (g->zargc < 4)
  112.     g->zargs[3] = 0x82;
  113.  
  114.     /* Scan byte or word array */
  115.  
  116.     for (i = 0; i < g->zargs[2]; i++) {
  117.  
  118.     if (g->zargs[3] & 0x80) {    /* scan word array */
  119.  
  120.         zword wvalue;
  121.  
  122.         LOW_WORD (addr, wvalue)
  123.  
  124.         if (wvalue == g->zargs[0])
  125.         goto finished;
  126.  
  127.     } else {        /* scan byte array */
  128.  
  129.         zbyte bvalue;
  130.  
  131.         LOW_BYTE (addr, bvalue)
  132.  
  133.         if (bvalue == g->zargs[0])
  134.         goto finished;
  135.  
  136.     }
  137.  
  138.     addr += g->zargs[3] & 0x7f;
  139.  
  140.     }
  141.  
  142.     addr = 0;
  143.  
  144. finished:
  145.  
  146.     store (g,addr);
  147.     branch (g,addr);
  148.  
  149. }/* z_scan_table */
  150.  
  151. /*
  152.  * z_storeb, write a byte into a table of bytes.
  153.  *
  154.  *    zargs[0] = address of table
  155.  *    zargs[1] = index of table entry
  156.  *    zargs[2] = value to be written
  157.  *
  158.  */
  159.  
  160. void z_storeb (struct sg *g)
  161. {
  162.  
  163.     storeb (g,(zword) (g->zargs[0] + g->zargs[1]), (unsigned char)(g->zargs[2]));
  164.  
  165. }/* z_storeb */
  166.  
  167. /*
  168.  * z_storew, write a word into a table of words.
  169.  *
  170.  *    zargs[0] = address of table
  171.  *    zargs[1] = index of table entry
  172.  *    zargs[2] = value to be written
  173.  *
  174.  */
  175.  
  176. void z_storew (struct sg *g)
  177. {
  178.  
  179.     storew (g,(zword) (g->zargs[0] + 2 * g->zargs[1]), g->zargs[2]);
  180.  
  181. }/* z_storew */
  182.