Advertisement
teleias

Verilog 2

Apr 21st, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. =================================================================
  2. //OG.v
  3. module OG(input q1, q2, q3, output o);
  4.     assign o = (q3);
  5. endmodule
  6. =================================================================
  7. //NSG.v
  8. module NSG(input x, q1, q2, q3, output d1, d2, d3);
  9.     assign d3 = (x & q2 & q1);
  10.     assign d2 = (x & q2 & q1) | ((~x) & q2 & (~q1));
  11.     assign d1 = (q2 & (~q1)) | (x & (~q1));
  12. endmodule
  13. =================================================================
  14. //FF.v
  15. module FF(input d, clk, r, output reg q);
  16.     always @(posedge clk, negedge r)
  17.         begin
  18.             if(clk)
  19.                 q <= d;
  20.             else if(!r)
  21.                 q <= r;
  22.         end
  23. endmodule
  24. =================================================================
  25. //FSM.v
  26. `include "OG.v"
  27. `include "NSG.v"
  28. `include "FF.v"
  29. module FSM(input x, clk, r, output o);
  30.     wire q1, q2, q3, d1, d2, d3, o;
  31.     NSG next(x, q1, q2, q3, d1, d2, d3);
  32.     FF ff1(d1, clik, r, q1);
  33.     FF ff2(d2, clik, r, q2);
  34.     FF ff3(d3, clik, r, q3);
  35.     OG out(q1, q2, q3, o);
  36. endmodule
  37. =================================================================
  38. //main.v
  39. `include "FSM.v"
  40. module main;
  41.     reg x, clk, r;
  42.     wire o;
  43.  
  44.     initial
  45.         begin
  46.             clk= 1;
  47.             r = 0;
  48.             x =   0;
  49.             #1;
  50.             r = 1;
  51.         end
  52.    
  53.     initial
  54.         begin
  55.             $monitor(" x=%x t=%3d o=%x ", x, $time, o);
  56.         end
  57.     initial
  58.         repeat( 16 )
  59.         begin
  60.             #1 clk = ~clk;
  61.         end
  62.     initial
  63.         //1011011010110100
  64.         begin
  65.             #2
  66.             #1 x = 1;
  67.             #1 x = 0;
  68.             #1 x = 1;
  69.             #1 x = 1;
  70.             #1 x = 0;
  71.             #1 x = 1;
  72.             #1 x = 1;
  73.             #1 x = 0;
  74.             #1 x = 1;
  75.             #1 x = 0;
  76.             #1 x = 1;
  77.             #1 x = 1;
  78.             #1 x = 0;
  79.             #1 x = 1;
  80.             #1 x = 0;
  81.             #1 x = 0;
  82.  
  83.         end
  84.  
  85. endmodule
  86. =================================================================
  87. Output
  88. x=0 t=1 o=0
  89. x=0 t=2 o=0
  90. x=1 t=4 o=0
  91. x=0 t=6 o=0
  92. x=1 t=8 o=0
  93. x=1 t=10 o=0
  94. x=0 t=12 o=0
  95. x=1 t=14 o=1
  96. x=1 t=16 o=0
  97. x=0 t=18 o=0
  98. x=1 t=20 o=1
  99. x=0 t=22 o=0
  100. x=1 t=24 o=0
  101. x=1 t=26 o=0
  102. x=0 t=28 o=1
  103. x=1 t=30 o=0
  104. x=0 t=32 o=0
  105. x=0 t=34 o=0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement