home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / sharew / elektro / lsim_21 / tlights.lsm < prev   
Encoding:
Text File  |  1989-04-23  |  12.5 KB  |  349 lines

  1. {------------------------------------------------------------------------------
  2.       T R A F F I C    L I G H T    C O N T R O L E R    E X A M P L E
  3. ------------------------------------------------------------------------------}
  4.  
  5.  
  6.  
  7. ! Reset = 1, C = 0, TL = 0, TS = 0, ST, HL1, HL0, FL1, FL0, Clk = 0 ; 
  8.  
  9. ! :, ::, :::;     { make a visual break in the timing diagram }
  10.  
  11. ! PalD0,  PalD1,  PalD2,  PalD3,  PalD4,
  12.   PalQ0,  PalQ1,  PalQ2,  PalQ3,  PalQ4,
  13.   Palq01, Palq11, Palq21, Palq31, Palq41 ;
  14.  
  15.  
  16.  
  17.         { -----------------------------------------------------------
  18.  
  19.  
  20.  
  21.         This example is based on the traffic light controller finite state
  22. machine described on pages 85 - 88 of "an introduction to VLSI systems" by
  23. Carver Mead and Lynn Conway.
  24.  
  25.         I have altered the implementation slightly to make it suit a PAL16R8
  26. rather than a chip layout. Mostly this consists of not using 2 signals: Y0
  27. and Y1. I have also added an active high synchronous Reset signal. The 
  28. system must be clocked twice to reset properly (to state 1). Apart from
  29. these changes I have stuck closely to the Mead/Conway original, and used the
  30. same signal names and functions.
  31.  
  32.  
  33.  
  34.         The addition of the Reset signal bears a little discussion. The
  35. light controller is a state machine. It works out its next state from its
  36. inputs, and the preceding state. If the preceding state is undetermined
  37. then the next state will also be undetermined. Without a Reset the power up
  38. state (stored in 5 flip-flops) is undetermined. It follows that all
  39. subsequent states are also undetermined.
  40.  
  41.         In strict terms this is true. Suppose that the real world system
  42. had started (by chance) in state 2. The next state would then be state 3. A
  43. normal sequence with completely determined states may well be established.
  44. Equally however the sequence may start at power up with any other state.
  45. Thus even though a normal sequence may be established it is impossible to
  46. say what the first, second, or third etc... states are.
  47.  
  48.         Lsim is cautious by design, and allows undetermined to propagate
  49. easily (see the operator truth tables). This results in a normal state
  50. sequence with a chance power up start point being shown as undetermined.
  51. Unfortunately not very useful for seeing if the logic is doing what it
  52. should. Try, for example, BADLIGHT.LSM which contains the same logic as
  53. this file but without the Reset signal. Also see the example SPENB.LSM.
  54.  
  55.         Adding a reset makes the power up state determined, and simulation
  56. easy. There is a case for making it harder for undetermined to propagate
  57. in a simulation. Then it might not be necessary to add resets to make the
  58. simulation easy. But this could also hide real world logical flaws.
  59.  
  60.         Actually my traffic light controller requires a reset in the real
  61. world, a fact which might not be so apparent from the simulation where it
  62. not for Lsim's cautious design. It has a 'stuck state' (HL1 = 1, HL0 = 1,
  63. FL1 = 1, FL0 = 1) which never changes to any other state. If the power up
  64. is left to chance it might come up in the stuck state, and remain there
  65. confusing drivers forever.
  66.  
  67.         The beautiful approach of the Mead/Conway design to avoiding the
  68. need for a reset is not general and is, of course, only applicable to
  69. systems having an integral power of 2 number of states. Other techniques for
  70. avoiding the need for a real world reset in a state machine may be more
  71. trouble than the reset itself. Besides which, quite often the power up state
  72. of a state machine can't be left to chance making a reset unavoidable.
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.         The traffic light controller is master of a crossroads where a rarely
  83. used farm road meets a popular highway. The crossroads incorporates car
  84. detectors for traffic waiting on the farm road.
  85.  
  86.  
  87.  
  88.                                 Farm road
  89.  
  90.                                 |       |
  91.                                 |       |
  92.                                 |       |
  93.                                 |       |
  94.                               HL|     C |FL
  95.             ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯         ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  96.  
  97. Highway
  98.  
  99.             ____________________         ____________________
  100.                               FL| C     |HL
  101.                                 |       |
  102.                                 |       |
  103.                                 |       |
  104.                                 |       |
  105.         Key
  106.         ===
  107.  
  108.          C  =  Car detector
  109.         HL  =  Traffic lights for highway
  110.         FL  =  Traffic lights for farmroad
  111.  
  112.  
  113.  
  114.         The car detector produces a signal C which is a 1 (logic high) when
  115. there is a car waiting on the farm road, and a 0 (logic low) otherwise.
  116.  
  117.         The highway traffic lights are controlled by 2 signals called HL0
  118. and HL1. These set the lights as follows:-
  119.  
  120.       HL1     HL0       Lights show
  121.       ===     ===       ===========
  122.  
  123.         0       0       Green
  124.         0       1       Red
  125.         1       0       Yellow
  126.  
  127. The traffic lights for the farm road are similarly controlled by the
  128. signals FL0 and FL1.
  129.  
  130.         The lights cycle through a pattern of 4 states like this:-
  131.  
  132. Highway lights          Farm road lights        State number
  133. ==============          ================        ============
  134.  
  135. Green                   Red                     0
  136. Yellow                  Red                     1
  137. Red                     Green                   2
  138. Red                     Yellow                  3
  139. Green                   Red                     0
  140.  
  141. and so on ...
  142.  
  143.  
  144.  
  145.         There is a timer in the Mead/Conway system which is controlled by 
  146. 3 signals. A 1 (logic high) on its input ST (Start Timer) resets the timer,
  147. making its outputs TS (Time out Short) and TL (Time out Long) go to 0
  148. (logic low). A short time after reset TS goes to 1, and stays there until
  149. the next ST. TL goes to 1 a longer time after reset. Thus the waveforms of
  150. the timer might look something like this:-
  151.  
  152.  
  153. ST   ______¯¯¯________________________________________________¯¯¯___________
  154.  
  155. TS   ¯¯¯¯¯¯_________¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯_________¯¯¯¯¯
  156. TL   ________________________________________________¯¯¯¯¯¯¯¯¯______________
  157.  
  158.            |        |                                |
  159.     ------>|        |<------ Short time out          |
  160.            |                                         |
  161.            |<------------ Long time out ------------>|
  162.  
  163.  
  164.  
  165.         The short time out is used to time the yellow period of the lights.
  166. The long time out is used for 2 things. It is the minimum time that the
  167. highway lights must stay green, and the maximum time that the farm road
  168. lights may stay green. The timer is reset at each change of state.
  169.  
  170.  
  171.  
  172.         The 'default' state of the lights is with the highway lights green
  173. (state 0).  The lights change to let farm road traffic through only if 2
  174. conditions are met. The highway lights must have been green the minimum
  175. amount of time (i.e. the long time out must have expired) and the car
  176. detector must have detected a car on the farm road.
  177.  
  178.         The farm road lights remain green either for the maximum amount of
  179. time (a long time out) or until the car detector indicates no more waiting
  180. traffic. Whichever happens soonest triggers the change back to the highway
  181. lights being green.
  182.  
  183.  
  184.  
  185.         We may now draw up a state transition table for the system:-
  186.  
  187.  
  188. PRESENT  STATE    INPUTS             NEXT  STATE    OUTPUT
  189. =======  =====    ======             ====  =====    ======
  190.  
  191. State             C    TL   TS       State          ST
  192. -----             -    --   --       -----          --
  193.  
  194. 0                 0                  0              0
  195. 0                      0             0              0
  196. 0                 1    1             1              1
  197.  
  198. 1                           0        1              0
  199. 1                           1        2              1
  200.  
  201. 2                 1    0             2              0
  202. 2                 0                  3              1
  203. 2                      1             3              1
  204.  
  205. 3                                    3              0
  206. 3                           1        0              1
  207.  
  208.  
  209.  
  210. By replacing the state numbers with the appropriate light control signals we
  211. can create the following table:-
  212.  
  213.  
  214. P R E S E N T   S T A T E                                N E X T   S T A T E
  215. =============   =========                                =======   =========
  216.  
  217. C   TL  TS  HL1 HL0 FL1 FL0 Product term                 ST  HL1 HL0 FL1 FL0
  218. -   --  --  --- --- --- --- ------- ----                 --  --- --- --- ---
  219.  
  220. 0           0   0   0   1   /C./HL1./HL0./FL1.FL0        0   0   0   0   1
  221.     0       0   0   0   1   /TL./HL1./HL0./FL1.FL0       0   0   0   0   1
  222. 1   1       0   0   0   1   C.TL./HL1./HL0./FL1.FL0      1   1   0   0   1
  223.  
  224.         0   1   0   0   1   /TS.HL1./HL0./FL1.FL0        0   1   0   0   1
  225.         1   1   0   0   1   TS.HL1./HL0./FL1.FL0         1   0   1   0   0
  226.  
  227. 1   0       0   1   0   0   C./TL./HL1.HL0./FL1./FL0     0   0   1   0   0
  228. 0           0   1   0   0   /C./HL1.HL0./FL1./FL0        1   0   1   1   0
  229.     1       0   1   0   0   TL./HL1.HL0./FL1./FL0        1   0   1   1   0
  230.  
  231.         0   0   1   1   0   /TS./HL1.HL0.FL1./FL0        0   0   1   1   0
  232.         1   0   1   1   0   TS./HL1.HL0.FL1./FL0         1   0   0   0   1
  233.  
  234.  
  235.  
  236.         On a PAL16R8 each output cell has a D type flip-flop. However the
  237. signal at the output pin of the PAL is the inverse of that clocked into the
  238. flip-flop. So when writing equations for the input to these flip-flops we
  239. must generate the inverse of the next state signal shown in the table above.
  240. Like this:-
  241.  
  242.  
  243.  
  244.         ------- PAL16R8 style logic for lights controller ------- }
  245.  
  246.  
  247.  
  248. ST      = /PalQ0 ;
  249.  
  250. PalD0   = /C./HL1./HL0./FL1.FL0
  251.         + /TL./HL1./HL0./FL1.FL0
  252.         + /TS.HL1./HL0./FL1.FL0
  253.         + C./TL./HL1.HL0./FL1./FL0
  254.         + /TS./HL1.HL0.FL1./FL0 ;
  255.  
  256.  
  257.  
  258. HL1     = /PalQ1 ;
  259.  
  260. PalD1   = /Reset./C./HL1./HL0./FL1.FL0
  261.         + /Reset./TL./HL1./HL0./FL1.FL0
  262.         + /Reset.TS.HL1./HL0./FL1.FL0
  263.         + /Reset.C./TL./HL1.HL0./FL1./FL0
  264.         + /Reset./C./HL1.HL0./FL1./FL0
  265.         + /Reset.TL./HL1.HL0./FL1./FL0
  266.         + /Reset./TS./HL1.HL0.FL1./FL0
  267.         + /Reset.TS./HL1.HL0.FL1./FL0 ;
  268.  
  269.  
  270.  
  271. HL0     = /PalQ2 ;
  272.  
  273. PalD2   = /C./HL1./HL0./FL1.FL0
  274.         + /TL./HL1./HL0./FL1.FL0
  275.         + C.TL./HL1./HL0./FL1.FL0
  276.         + /TS.HL1./HL0./FL1.FL0
  277.         + TS./HL1.HL0.FL1./FL0
  278.         + Reset ;
  279.  
  280.  
  281.  
  282. FL1     = /PalQ3 ;
  283.  
  284. PalD3   = /C./HL1./HL0./FL1.FL0
  285.         + /TL./HL1./HL0./FL1.FL0
  286.         + C.TL./HL1./HL0./FL1.FL0
  287.         + /TS.HL1./HL0./FL1.FL0
  288.         + TS.HL1./HL0./FL1.FL0
  289.         + C./TL./HL1.HL0./FL1./FL0
  290.         + TS./HL1.HL0.FL1./FL0
  291.         + Reset ;
  292.  
  293.  
  294.  
  295. FL0     = /PalQ4 ;
  296.  
  297. PalD4   = /Reset.TS.HL1./HL0./FL1.FL0
  298.         + /Reset.C./TL./HL1.HL0./FL1./FL0
  299.         + /Reset./C./HL1.HL0./FL1./FL0
  300.         + /Reset.TL./HL1.HL0./FL1./FL0
  301.         + /Reset./TS./HL1.HL0.FL1./FL0 ;
  302.  
  303.  
  304.  
  305.  
  306.         { --------------------------------------------------------
  307.  
  308.  
  309.  
  310.         As you can see the entire state machine logic fits easily into a
  311. single PAL. In fact on a PAL16R8 there are 4 spare inputs, and 3 spare
  312. outputs. Using a PAL16R6 would give even greater flexibility in what is
  313. left over.
  314.  
  315.         When running this simulation you will have to operate the timer and
  316. the clock 'by hand', as well as (of course) the car detector input. If you
  317. are really enthusiastic about it you could simulate the timer as a shift
  318. register driven off the clock, perhaps using the spare flip-flops in the
  319. PAL16R8 to divide the clock first. One of the spare flip-flops could be used
  320. as synchroniser for the car detector input.
  321.  
  322.  
  323.  
  324.         Since alas Lsim doesn't (yet) have a primitive operator for a flip-
  325. flop we must provide the logic for the flip-flops with gates. I have used
  326. the example D type flip-flop from the instruction manual.
  327.  
  328.  
  329.  
  330.       ---- flip-flop logic for 5 of the flip-flops in a PAL16R8 ---- }
  331.  
  332.  
  333.  
  334. PalQ0  = (/Clk + Palq01 + PalD0).(Clk./Palq01.PalD0 + PalQ0);
  335. Palq01 = Clk.(Palq01 + /(PalQ0 $ PalD0));
  336.  
  337. PalQ1  = (/Clk + Palq11 + PalD1).(Clk./Palq11.PalD1 + PalQ1);
  338. Palq11 = Clk.(Palq11 + /(PalQ1 $ PalD1));
  339.  
  340. PalQ2  = (/Clk + Palq21 + PalD2).(Clk./Palq21.PalD2 + PalQ2);
  341. Palq21 = Clk.(Palq21 + /(PalQ2 $ PalD2));
  342.  
  343. PalQ3  = (/Clk + Palq31 + PalD3).(Clk./Palq31.PalD3 + PalQ3);
  344. Palq31 = Clk.(Palq31 + /(PalQ3 $ PalD3));
  345.  
  346. PalQ4  = (/Clk + Palq41 + PalD4).(Clk./Palq41.PalD4 + PalQ4);
  347. Palq41 = Clk.(Palq41 + /(PalQ4 $ PalD4));
  348.  
  349.