Alex9090

Untitled

May 30th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module fact;
  2.  
  3. task factorial;
  4.     input [3:0] n;
  5.     output [31:0] outfact;
  6.     integer count;
  7.     begin
  8.       outfact = 1;
  9.       for(count =n; count > 0; count= count-1)
  10.       outfact = outfact * count;
  11.       end
  12. endtask
  13.  
  14. initial
  15.   begin: init1
  16.     reg [3:0] n;
  17.     reg [31:0] result;
  18.     n = 4'b0101;
  19.     factorial(n, result);
  20.     $display("n=%d fact=%d", n, result);
  21.   end
  22. endmodule
  23.  
  24.  
  25. ((B or C) and (notA)) or (not(A or B))
  26.  
  27. module XOR(a, b, w1);
  28.  
  29.   input a, b;
  30.   output reg w1;
  31.  
  32.   always @(a or b)
  33.   begin
  34.     w1 = a ^ b;
  35.   end
  36.  
  37. endmodule
  38.  
  39. module AND(c, d, w2);
  40.  
  41.   input c, d;
  42.   output reg w2;
  43.  
  44.   always @(c or d)
  45.   begin
  46.     w2 = c & d;
  47.   end
  48. endmodule
  49.  
  50. module OR(e, f, w3);
  51.  
  52.   input e, f;
  53.   output reg w3;
  54.  
  55.   always @(e or f)
  56.   begin
  57.     w3 = e | f;
  58.   end
  59. endmodule
  60.  
  61. module NOR(h, i, w4);
  62.     input h, i;
  63.     output reg w4;
  64.    
  65.     always @(h or i)
  66.     begin
  67.       w4 = ~(h | i);
  68.     end
  69. endmodule      
  70.    
  71. module something(a, b, c, out);
  72.  
  73.   input a, b, c;
  74.   output out;
  75.   wire w1, w2, w3;
  76.   OR or1(b, c, w1);
  77.   AND and1(w1, ~a, w2);
  78.   NOR nor1(a, b, w3);
  79.   OR or2(w2, w3, out);
  80.  
  81.  
  82. endmodule
  83.  
  84. module Testbench;
  85.   reg a, b, c;
  86.   wire cout;
  87.  
  88.  something p(a, b, c, cout);
  89.  initial
  90.   begin
  91.    a = 0;
  92.     b = 0;
  93.     c = 0;
  94.     #1 $display("S=%b", cout);
  95.    
  96.    a = 1;
  97.     b = 1;
  98.     c = 1;
  99.     #1 $display("S=%b ", cout);
  100.    
  101. end
  102.  endmodule
  103.  
  104.  
  105.  
  106.  
  107. Inmultire 2 numere 3 biti -> rezultat 4 biti
  108.  
  109. module XOR(a, b, w1);
  110.  
  111.   input a, b;
  112.   output reg w1;
  113.  
  114.   always @(a or b)
  115.   begin
  116.     w1 = a ^ b;
  117.   end
  118.  
  119. endmodule
  120.  
  121. module AND(c, d, w2);
  122.  
  123.   input c, d;
  124.   output reg w2;
  125.  
  126.   always @(c or d)
  127.   begin
  128.     w2 = c & d;
  129.   end
  130. endmodule
  131.  
  132. module OR(e, f, w3);
  133.  
  134.   input e, f;
  135.   output reg w3;
  136.  
  137.   always @(e or f)
  138.   begin
  139.     w3 = e | f;
  140.   end
  141. endmodule
  142.  
  143. module NOR(h, i, w4);
  144.     input h, i;
  145.     output reg w4;
  146.    
  147.     always @(h or i)
  148.     begin
  149.       w4 = ~(h | i);
  150.     end
  151. endmodule      
  152.    
  153. module something(a0, a1, b0, b1, c0, c1, c2, c3);
  154.  
  155.   input a0, a1, b0, b1;
  156.   output c0, c1, c2, c3;
  157.   wire w1, w3, w4;
  158.   AND and1(a0, b1, w1);
  159.   AND and2(a0, b0, c0);
  160.   AND and3(a1, b0, w3);
  161.   AND and4(a1, b1, w4);
  162.   XOR xor1(w1, w3, c1);
  163.   AND and5(w1, w3, w5);
  164.   XOR xor2(w5, w4, c2);
  165.   AND And6(w4, w5, c3);
  166.  
  167. endmodule
  168.  
  169. module Testbench;
  170.   reg a, a0, b0, b1;
  171.   wire c0, c1, c2, c3;
  172.  
  173.  something p(a, a0, b0, b1, c0,c1,c2,c3);
  174.  initial
  175.   begin
  176.    a  = 0;
  177.    a0 = 1;
  178.    b0 = 1;
  179.    b1 = 1;
  180.     #1 $display("S=%b, S=%b, S=%b, S=%b", c0, c1, c2, c3);
  181.    
  182.      a  = 1;
  183.    a0 = 1;
  184.    b0 = 1;
  185.    b1 = 1;
  186.     #1 $display("S=%b, S=%b, S=%b, S=%b", c0, c1, c2, c3);
  187.    
  188. end
  189.  endmodule
  190.  
  191. module XOR(a, b, w1);
  192.  
  193.   input a, b;
  194.   output reg w1;
  195.  
  196.   always @(a or b)
  197.   begin
  198.     w1 = a ^ b;
  199.   end
  200.  
  201. endmodule
  202.  
  203. module AND(w1, cin,w2);
  204.  
  205.   input w1, cin;
  206.   output reg w2;
  207.  
  208.   always @(w1 or cin)
  209.   begin
  210.     w2 = w1 & cin;
  211.   end
  212. endmodule
  213.  
  214. module OR(w2, w3, cout);
  215.  
  216.   input w2, w3;
  217.   output reg cout;
  218.  
  219.   always @(w2 or w3)
  220.   begin
  221.     cout = w2 | w3;
  222.   end
  223. endmodule
  224.  
  225. module full_adder(a, b, cin, s, cout);
  226.  
  227.   input a, b, cin;
  228.   output s, cout;
  229.  
  230.   XOR xor1(a, b, w1);
  231.   XOR xor2(w1, cin, s);
  232.   AND and1(w1, cin, w2);
  233.   AND and2(a, b, w3);
  234.   OR or1(w2, w3, cout);
  235.  
  236.  
  237. endmodule
  238.  
  239. module Testbench;
  240.   reg a, b, cin;
  241.   wire s, cout;
  242.  
  243.   full_adder p(a, b, cin, s, cout);
  244.  initial
  245.   begin
  246.    a = 0;
  247.     b = 0;
  248.     cin = 0;
  249.     #1 $display("S=%b, C=%b\n", s, cout);
  250.    
  251.     a = 0;
  252.     b = 1;
  253.     #1 $display("S=%b, C=%b\n", s, cout);
  254.    
  255.     a=1;
  256.     b=0;
  257.     #1 $display("S=%b, C=%b\n", s, cout);
  258.    
  259.     a=1;
  260.     b=1;
  261.     #1 $display("S=%b, C=%b\n", s, cout);
  262.    
  263.     a = 0;
  264.     b = 0;
  265.     cin = 1;
  266.     #1 $display("S=%b, C=%b\n", s, cout);
  267.    
  268.     a = 0;
  269.     b = 1;
  270.     #1 $display("S=%b, C=%b\n", s, cout);
  271.    
  272.     a=1;
  273.     b=0;
  274.     #1 $display("S=%b, C=%b\n", s, cout);
  275.    
  276.     a=1;
  277.     b=1;
  278.     #1 $display("S=%b, C=%b\n", s, cout);
  279. end
  280.  endmodule
  281.  
  282.  
  283.  
  284.  
  285. FULL ADDER CU NAND-URI
  286.  
  287.  
  288. module NAND(a, b, w);
  289.  
  290.   input a, b;
  291.   output reg w;
  292.  
  293.   always @(a or b)
  294.   begin
  295.     w = a ~& b;
  296.   end
  297. endmodule
  298.  
  299. module full_adder(a, b, cin, s, cout);
  300.  
  301.   input a, b, cin;
  302.   output s, cout;
  303.  
  304.   NAND nand1(b, cin, w3);
  305.   NAND nand2(b, w3, w4);
  306.   NAND nand3(w3, cin, w5);
  307.   NAND nand4(w4, w5, w6);
  308.   NAND nand5(a, w6, w7);
  309.   NAND nand6(a, w7, w1);
  310.   NAND nand7(w7, w6, w8);
  311.   NAND nand8(w7, w3, cout);
  312.   NAND nand9(w1, w8, s);
  313.  
  314. endmodule
  315.  
  316. module Testbench;
  317.   reg a, b, cin;
  318.   wire s, cout;
  319.  
  320.   full_adder p(a, b, cin, s, cout);
  321.  initial
  322.   begin
  323.    a = 0;
  324.     b = 0;
  325.     cin = 0;
  326.     #1 $display("S=%b, C=%b\n", s, cout);
  327.    
  328.     a = 0;
  329.     b = 1;
  330.     #1 $display("S=%b, C=%b\n", s, cout);
  331.    
  332.     a=1;
  333.     b=0;
  334.     #1 $display("S=%b, C=%b\n", s, cout);
  335.    
  336.     a=1;
  337.     b=1;
  338.     #1 $display("S=%b, C=%b\n", s, cout);
  339.    
  340.     a = 0;
  341.     b = 0;
  342.     cin = 1;
  343.     #1 $display("S=%b, C=%b\n", s, cout);
  344.    
  345.     a = 0;
  346.     b = 1;
  347.     #1 $display("S=%b, C=%b\n", s, cout);
  348.    
  349.     a=1;
  350.     b=0;
  351.     #1 $display("S=%b, C=%b\n", s, cout);
  352.    
  353.     a=1;
  354.     b=1;
  355.     #1 $display("S=%b, C=%b\n", s, cout);
  356. end
  357.  endmodule
  358.  
  359.  
  360. FULL ADDER 4 BIT
  361.  
  362.  
  363. module NAND(a, b, w);
  364.  
  365.   input a, b;
  366.   output reg w;
  367.  
  368.   always @(a or b)
  369.   begin
  370.     w = a ~& b;
  371.   end
  372. endmodule
  373.  
  374. module full_adder(a, b, cin, s, cout);
  375.  
  376.   input a, b, cin;
  377.   output s, cout;
  378.  
  379.   NAND nand1(b, cin, w3);
  380.   NAND nand2(b, w3, w4);
  381.   NAND nand3(w3, cin, w5);
  382.   NAND nand4(w4, w5, w6);
  383.   NAND nand5(a, w6, w7);
  384.   NAND nand6(a, w7, w1);
  385.   NAND nand7(w7, w6, w8);
  386.   NAND nand8(w7, w3, cout);
  387.   NAND nand9(w1, w8, s);
  388.  
  389. endmodule
  390.  
  391. module bit4_full_adder(cin, a0, a1, a2, a3, b0, b1, b2, b3, s0, s1, s2, s3, cout);
  392.  
  393.   input cin, a0, a1, a2, a3, b0, b1, b2, b3;
  394.   output s0, s1, s2, s3, cout;
  395.  
  396.   full_adder(a0, b0, cin
  397.  
  398. endmodule
  399.  
  400. module Testbench;
  401.   reg a, b, cin;
  402.   wire s, cout;
  403.  
  404.   full_adder p(a, b, cin, s, cout);
  405.  
  406.  initial
  407.   begin
  408.    a = 0;
  409.     b = 0;
  410.     cin = 0;
  411.     #1 $display("S=%b, C=%b\n", s, cout);
  412.    
  413.     a = 0;
  414.     b = 1;
  415.     #1 $display("S=%b, C=%b\n", s, cout);
  416.    
  417.     a=1;
  418.     b=0;
  419.     #1 $display("S=%b, C=%b\n", s, cout);
  420.    
  421.     a=1;
  422.     b=1;
  423.     #1 $display("S=%b, C=%b\n", s, cout);
  424.    
  425.     a = 0;
  426.     b = 0;
  427.     cin = 1;
  428.     #1 $display("S=%b, C=%b\n", s, cout);
  429.    
  430.     a = 0;
  431.     b = 1;
  432.     #1 $display("S=%b, C=%b\n", s, cout);
  433.    
  434.     a=1;
  435.     b=0;
  436.     #1 $display("S=%b, C=%b\n", s, cout);
  437.    
  438.     a=1;
  439.     b=1;
  440.     #1 $display("S=%b, C=%b\n", s, cout);
  441. end
  442.  endmodule
  443.  
  444. module NAND(in1, in2, out);
  445.  
  446. input in1, in2;
  447. output out;
  448.  
  449. assign out = ~(in1 & in2);
  450.  
  451. endmodule
  452.  
  453. module AND(in1, in2, out);
  454.  
  455. input in1, in2;
  456. output out;
  457. wire w1;
  458.  
  459. NAND NAND1(in1, in2, w1);
  460. NAND NAND2(w1, w1, out);
  461.  
  462. endmodule
  463.  
  464. module Testbench;
  465. reg a,b;
  466.  
  467. wire out1, out2;
  468. initial begin
  469.       a=0; b=0;
  470.       #1 a=1 ;
  471.       #1 b=1 ;
  472.       #1 a=0 ;
  473. end
  474.  
  475. initial begin
  476.  
  477. $monitor( "Time=%0d a=%b b=%b out1=%b out2=%b", $time, a, b, out1, out2);
  478. end
  479.  
  480. AND and_gate(a,b,out1);
  481. NAND nand_gate(a,b,out2);
  482. endmodule
  483.  
  484.  
  485.  
  486.  
  487. ADUNARE
  488.  
  489. module Adder(A,B,Result);
  490.  
  491.   input [3:0] A;
  492.   input [3:0] B;
  493.   output [3:0] Result;
  494.   reg [3:0] Result;
  495.  
  496.   always @(A or B)
  497.   begin
  498.     Result <= A+B;
  499.   end
  500.  
  501. endmodule
  502.  
  503. module Testbench;
  504.  
  505.   reg [3:0] A_t;
  506.   reg [3:0] B_t;
  507.   wire [3:0] Result_t;
  508.  
  509.   Adder Adder_1(A_t, B_t, Result_t);
  510.  
  511.   initial
  512.   begin
  513.     //case 0
  514.     A_t <= 0; B_t <=0;
  515.     #1 $display("Result_t=%b", Result_t);
  516.    
  517.     //case 1
  518.    
  519.     A_t <= 0; B_t <= 1;
  520.     #1 $display("Result_t=%b", Result_t);
  521.    
  522.     //case 2
  523.     A_t <=  1; B_t <= 0;
  524.     #1 $display("Result_t=%b", Result_t);
  525.    
  526.     //case 3
  527.    
  528.     A_t <= 10; B_t <= 10;
  529.     #1 $display("Result_t=%b", Result_t);
  530.     end
  531.    
  532.    endmodule
  533.  
  534.  
  535. DECODER 2X4
  536.  
  537.  
  538. module Decoder(A, B, D);
  539.  
  540.   input A, B;
  541.   output [3:0] D;
  542.   reg [3:0] D;
  543.  
  544.   always @(A or B)
  545.  
  546.   begin
  547.     if( A == 0 && B == 0)
  548.       D <= 4'b0001;
  549.      
  550.     else if ( A == 0 && B == 1 )
  551.       D <= 4'b0010;
  552.      
  553.     else if ( A == 1 && B == 0 )
  554.       D <= 4'b0100;
  555.      
  556.     else if ( A == 1 && B == 1)
  557.       D <= 4'b1000;   //nr binar realizat pe 4 biti
  558.     end
  559. endmodule
  560.  
  561. module Testbench;
  562.  
  563. reg A_t;
  564.   reg B_t;
  565.   wire [3:0] D_t;
  566.  
  567.   Decoder Decoder_1(A_t, B_t, D_t);
  568.  
  569.   initial
  570.   begin
  571.     //case 0
  572.     A_t <= 0; B_t <=0;
  573.     #1 $display("D_t=%b", D_t);
  574.    
  575.     //case 1
  576.    
  577.     A_t <= 0; B_t <= 1;
  578.     #1 $display("D_t=%b", D_t);
  579.    
  580.     //case 2
  581.     A_t <=  1; B_t <= 0;
  582.     #1 $display("D_t=%b", D_t);
  583.    
  584.     //case 3
  585.    
  586.     A_t <= 1; B_t <= 1;
  587.     #1 $display("D_t=%b", D_t);
  588.     end
  589.    
  590.    endmodule
  591.  
  592.  
  593.  
  594. HALF ADDER
  595.  
  596.  
  597. module XOR(A,B,S);
  598.     input A, B;
  599.     output reg S;
  600.    
  601.     always @(A or B)
  602.     begin
  603.       S = A^B;
  604.     end
  605.     endmodule
  606.    
  607.     module AND(A,B,S);
  608.     input A,B;
  609.     output reg S;
  610.    
  611.     always @(A or B)
  612.     begin
  613.         S = A&B;
  614.     end
  615. endmodule
  616.  
  617. module half_adder;
  618.  
  619.       reg A,B;
  620.       output S,C;
  621.      
  622.       XOR myXOR(A,B,S);
  623.       AND myAND(A,B,C);
  624.      
  625.       initial
  626.       begin
  627.     A = 0;
  628.     B = 0;
  629.     #1 $display("S=%b, C=%b\n", S, C);
  630.    
  631.     A = 0;
  632.     B = 1;
  633.     #1 $display("S=%b, C=%b\n", S, C);
  634.    
  635.     A=1;
  636.     B=0;
  637.     #1 $display("S=%b, C=%b\n", S, C);
  638.    
  639.     A=1;
  640.     B=1;
  641.     #1 $display("S=%b, C=%b\n", S, C);
  642.    
  643. end
  644. endmodule
Advertisement
Add Comment
Please, Sign In to add comment