home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 210 / 210.d81 / t.bits < prev    next >
Text File  |  2022-08-26  |  6KB  |  254 lines

  1. u
  2.           BITS: STATE TABLES
  3.  
  4.            by Scott E. Resh
  5.  
  6.  
  7.     Your first question may be "What
  8. exactly ARE state tables?" Well, state
  9. tables are a method of QUICKLY
  10. changing data from one state to
  11. another, by replacing numerous
  12. calculations with indexed addressing.
  13.  
  14.     The Run It file for this article
  15. demonstrates just how fast a whole
  16. screen's worth of color bytes can be
  17. changed -- in a predetermined pattern
  18. -- by using state tables. Pay close
  19. attention to the SEQUENCE of colors
  20. as they move.
  21.  
  22.     The colors are not in NUMERICAL
  23. sequence, i.e. white does not follow
  24. black, red does not follow white,
  25. etc.... Sometimes the traditional
  26. numerical pattern isn't what is
  27. wanted. With state tables, the
  28. pattern is up to the programmer.
  29. Using a custom pattern would be quite
  30. a bit slower than using the Black-
  31. White-Red-Cyan- etc. pattern if you
  32. didn't use the state table technique.
  33.  
  34.     Note that in the Run It file
  35. screen memory isn't changing at all --
  36. only color memory is being changed.
  37.  
  38.     Because there are 256 possible
  39. color combinations for a hi-res color
  40. byte, we need a state table consisting
  41. of 256 bytes.
  42.  
  43.     For example, let's take a color
  44. byte that says we have a RED
  45. foreground and a WHITE background. We
  46. want to change that to a PURPLE
  47. foreground and YELLOW background. This
  48. means going from 33 ($21) to 71 ($47).
  49.  
  50.     Where do these numbers come from?
  51. Look at the hex numbers. Red is $2,
  52. right? White is $1.  Red on white is
  53. therefore $21. Purple ($4) on yellow
  54. ($7) is $47. This is the way color
  55. memory is stored in your computer.
  56.  
  57.     After the PURPLE/YELLOW we want to
  58. go to a LIGHT GRAY on DARK GRAY
  59. combination, 71 ($47) to 251 ($FB).
  60. We can't add a constant to the color
  61. byte because the numeric difference
  62. between the color bytes is not the
  63. same! We could use a whole bunch of
  64. CMP and BNE commands, but that's a
  65. lousy idea for TWO reasons --
  66.  
  67.   (1) it could be VERY slow and
  68.  
  69.   (2) it would take at least 2560
  70. bytes of memory for the program!  Why
  71. 2560 bytes?
  72.  
  73.     Imagine --
  74.  
  75.      CMP #COLOR1 ; Is it color #1?
  76.      BNE N1      ; No, try next color
  77.      LDA #COLOR2 ; Yes, load new color
  78.      JMP PCOLOR  ; Jump to Put COLOR
  79.  N1: CMP #xx     ; Is it color #xx?
  80.  
  81.     Each color byte (all 256 of them!)
  82. would require 10 bytes of code.
  83.  
  84.     Now the fun stuff begins. Let's
  85. imagine that we have a state table
  86. consisting of 256 color bytes. Let's
  87. take our FIRST color byte RED/WHITE --
  88. 33 ($21), and use it as an INDEX into
  89. our state table. We want to change
  90. the state of our color byte from 33
  91. to 71. This means that position #33
  92. of our state table must hold a 71.
  93.  
  94.     Our color byte now holds PURPLE/
  95. YELLOW byte. Next we want to change
  96. to LIGHT GRAY/DARK GRAY as mentioned
  97. above. Numerically speaking, 71
  98. ($47) to 251 ($FB).
  99.  
  100.     This means that position #71 of
  101. our state table must hold a value of
  102. 251. Here is a part of the code --
  103.  
  104.       LDX #0           ; Init index
  105.  
  106.  LOOP:LDY COLORMEM,X   ; Get OLD color
  107.                        ;  byte
  108.  
  109.       LDA StateTable,Y ; Use as index
  110.                        ;  into states
  111.  
  112.       STA COLORMEM,X   ; Store NEW
  113.                        ;  color value
  114.  
  115.       INX              ; Loop back 256
  116.       BNE LOOP         ;  times
  117.  
  118.  
  119.     That simple little loop will
  120. replace 256 bytes of color memory
  121. with NEW values from our state table.
  122.  
  123.     If changing colors isn't your
  124. thing, then don't worry. This simple
  125. technique can be used for all kinds
  126. of data handling. Adventure games
  127. often use state tables to indicate
  128. where you will go when you leave a
  129. room in a particular direction.
  130.  
  131.     Or suppose that you wanted to make
  132. a mirror image (for whatever reason)
  133. of a hi-res screen. Keep in mind that
  134. a hi-res screen is made up of 8000
  135. bytes.  We need to do it FAST.
  136.  
  137.     Most people would do it by using a
  138. little ROtate loop for every byte --
  139.  
  140.       LDA Data ; Get byte to 'mirror'
  141.       STA Temp ; Put in z-page buffer
  142.       LDX #8   ; Init count register
  143.  LOOP:ROL Temp ; Rotate buffer LEFT
  144.       ROR A    ; Rotate Acc RIGHT
  145.       DEX      ; Loop back 8 times
  146.       BNE LOOP ;
  147.       STA Data ; Store mirrored byte
  148.  
  149.     A sequence of 8 ROL/ROR pairs
  150. instead of the loop, would speed
  151. things up. But it still wouldn't be
  152. as fast as state table substitution.
  153.  
  154.     While the above loop would have
  155. to be performed 8000 times, the
  156. following loop would only need 32
  157. iterations.
  158.  
  159.       LDX #0   ; Init index register
  160.  LOOP:LDY Screen,X ; Perform the
  161.       LDA TABLE,Y  ;  actual
  162.       STA Screen,X ;  substitution
  163.  
  164.       INX      ; Loop back 256 times
  165.       BNE LOOP ;
  166.  
  167.     The speedup is due to the fact
  168. that the state table would already
  169. contain the 256 rotated bytes.
  170.  
  171.     Don't let my little examples
  172. limit your creativity.  Take the
  173. concept of state tables and run with
  174. it.
  175.  
  176.     After you've taken the time to
  177. create a state table, they can be
  178. incredible time and space savers.
  179.  
  180.  SR
  181.  
  182.  
  183.  [Dave's Note:] Right you are, Scott.
  184. A zillion situations can be controlled
  185. by state tables. And here is the rule
  186. in a nut-shell:
  187.  
  188.  Your data becomes the pointer to the
  189. next data.
  190.  
  191.  This can be done in Basic by using
  192. arrays. Tables and arrays are
  193. conceptually identical. Let me write a
  194. bit of code and see what it does:
  195.  
  196.   10 a(0)=4
  197.   20 a(4)=1
  198.   30 a(1)=22
  199.   40 a(22)=5
  200.   50 a(5)=0
  201.  
  202.   60 i=0:        rem set pointer
  203.   70 ?chr$(64+a(i));: rem print char.
  204.   80 i=a(i): make data become pointer
  205.   90 if i>0 then 70 :if not 0, loop
  206.  100 print
  207.  
  208.  I have organized the array as a state
  209. table. If the state is 0, then the
  210. data is 4, which produces a printed
  211. "D" in line 70.
  212.  
  213.  In line 80, the data becomes the
  214. pointer, in the first case, 4. Loop to
  215. 70, and an "A" is printed.
  216.  
  217.  I hope you get the idea. Combine
  218. state table thinking with a random
  219. number and a bunch of sprites, and you
  220. can create "randomation". A sprite of
  221. a little guy faces forward. From that
  222. state, he can do one of three things:
  223.  
  224.  1) Turn Right
  225.  
  226.  2) Turn Left
  227.  
  228.  3) Not Change.
  229.  
  230.  Use a random of 1 - 3 to choose. Then
  231. from each other option, devise three
  232. possibilities. For example, from Face
  233. Left, you can
  234.  
  235.  1) Turn Front
  236.  
  237.  2) Not Change
  238.  
  239.  3) Not Change.
  240.  
  241.  Someone once told me, when I had a
  242. TRS-80 Model I, that "your imagination
  243. is your only limitation." I have since
  244. hedged on this blanket statement. The
  245. TRS-80 did not have color, sound, or
  246. even lower case characters.
  247.  
  248.  But these are only hardware
  249. limitations. Imagination can reach
  250. far beyond such minor considerations!
  251.  
  252.  DMM
  253.  
  254.  
  255.