Advertisement
Guest User

6.4 Finite State Machine modeling.py

a guest
Oct 19th, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. # <nbformat>3.0</nbformat>
  3.  
  4. # <codecell>
  5.  
  6. from myhdl import *
  7.  
  8. ACTIVE_LOW = 0
  9. FRAME_SIZE = 8
  10.  
  11. t_State = enum('SEARCH', 'CONFIRM', 'SYNC')
  12.  
  13. # <codecell>
  14.  
  15. def FramerCtrl(SOF, state, syncFlag, clk, reset):
  16.     """ Framing control FSM.
  17.    SOF      -- start-of-frame output bit
  18.    state    -- FramerState output
  19.    syncFlag -- sync pattern found indication input
  20.    clk      -- clock input
  21.    reset_n  -- active low reset
  22.    """
  23.     index = Signal(0) # position in frame
  24.    
  25.     @always_seq(clk.posedge, reset=reset)
  26.     def FSM():
  27.         index.next = (index + 1) % FRAME_SIZE
  28.         SOF.next = 0
  29.         if state == t_State.SEARCH:
  30.             index.next = 1
  31.             if syncFlag:
  32.                 state.next = t_State.CONFIRM
  33.        
  34.         elif state == t_State.CONFIRM:
  35.             if index == 0:
  36.                 if syncFlag:
  37.                     state.next = t_State.SYNC
  38.                 else:
  39.                     state.next = t_State.SEARCH
  40.        
  41.         elif state == t_State.SYNC:
  42.             if index == 0:
  43.                 if not syncFlag:
  44.                     state.next = t_State.SEARCH
  45.             SOF.next = (index == FRAME_SIZE-1)
  46.         else:
  47.             raise ValueError("Undefined state")
  48.            
  49.     return FSM
  50.  
  51. # <codecell>
  52.  
  53. def testbench():
  54.     SOF = Signal(bool(0))
  55.     syncFlag = Signal(bool(0))
  56.     clk = Signal(bool(0))
  57.     reset = ResetSignal(0, active=ACTIVE_LOW, async=True)
  58.     state = Signal(t_State.SEARCH)
  59.     framectrl = FramerCtrl(SOF, state, syncFlag, clk, reset)
  60.    
  61.     @always(delay(10))
  62.     def clkgen():
  63.         clk.next = not clk
  64.        
  65.     @instance
  66.     def stimulus():
  67.         for i in range(3):
  68.             yield clk.posedge
  69.         for n in (12, 8, 8, 4):
  70.             syncFlag.next = 1
  71.             yield clk.posedge
  72.             syncFlag.next = 0
  73.             for i in range(n-1):
  74.                 yield clk.posedge
  75.         raise StopSimulation
  76.  
  77.     return framectrl, clkgen, stimulus
  78.    
  79. tb_fsm = traceSignals(testbench)
  80. sim = Simulation(tb_fsm)
  81. sim.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement