Question
Verilog code help
Counter is a sequential circuit. A digital circuit which is used for a counting events (usually clock pulses) is known counte
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1)

VERILOG CODE FOR Half Adder, DFF, and Counter

module halfadder(
input A, B,
output S, Cout
);

assign S = A ^ B;
assign Cout = (A & B);

endmodule

module DFF (
input Clk,
input Rst,
input D,
output reg Q
);

always @ (posedge Clk or posedge Rst)
begin
if (Rst)
Q <= 1'd0;
else
Q <= D;
end

endmodule


module counter1 (
input Clk,
input Reset,
input En,
output [3:0] Count
);

wire [3:0] Carry, Sum;

halfadder HA0 (.A(Count[0]), .B(En), .S(Sum[0]), .Cout(Carry[0]));
halfadder HA1 (.A(Count[1]), .B(Carry[0]), .S(Sum[1]), .Cout(Carry[1]));
halfadder HA2 (.A(Count[2]), .B(Carry[1]), .S(Sum[2]), .Cout(Carry[2]));
halfadder HA3 (.A(Count[3]), .B(Carry[2]), .S(Sum[3]), .Cout(Carry[3]));

DFF DFF0 (.Clk(Clk), .Rst(Reset), .D(Sum[0]), .Q(Count[0]));
DFF DFF1 (.Clk(Clk), .Rst(Reset), .D(Sum[1]), .Q(Count[1]));
DFF DFF2 (.Clk(Clk), .Rst(Reset), .D(Sum[2]), .Q(Count[2]));
DFF DFF3 (.Clk(Clk), .Rst(Reset), .D(Sum[3]), .Q(Count[3]));

endmodule

TESTBENCH For UP Counter1

module testbench;
reg Clk;
reg Reset;
reg En;
wire [3:0] Count;

counter1 DUT (.Clk(Clk), .Reset(Reset), .En(En), .Count(Count));

always
#5 Clk = !Clk;

always @(posedge Clk)
$display("Clk = %b, Reset = %d, En = %b, Count = %d", Clk, Reset, En, Count);

initial
begin
Clk = 1'b0;
Reset = 1'b1;
En = 1'b0;
#20;
Reset = 1'b0;
En = 1'b1;
#100;
En = 1'b0;
#60;
En = 1'b1;
#100;
$finish;
end

initial
begin
$recordfile("file1.trn");
$recordvars();
end

endmodule


/*************** Output Of Program *******************
Clk = 1, Reset = 1, En = 0, Count = 0
Clk = 1, Reset = 1, En = 0, Count = 0
Clk = 1, Reset = 0, En = 1, Count = 0
Clk = 1, Reset = 0, En = 1, Count = 1
Clk = 1, Reset = 0, En = 1, Count = 2
Clk = 1, Reset = 0, En = 1, Count = 3
Clk = 1, Reset = 0, En = 1, Count = 4
Clk = 1, Reset = 0, En = 1, Count = 5
Clk = 1, Reset = 0, En = 1, Count = 6
Clk = 1, Reset = 0, En = 1, Count = 7
Clk = 1, Reset = 0, En = 1, Count = 8
Clk = 1, Reset = 0, En = 1, Count = 9
Clk = 1, Reset = 0, En = 0, Count = 10
Clk = 1, Reset = 0, En = 0, Count = 10
Clk = 1, Reset = 0, En = 0, Count = 10
Clk = 1, Reset = 0, En = 0, Count = 10
Clk = 1, Reset = 0, En = 0, Count = 10
Clk = 1, Reset = 0, En = 0, Count = 10
Clk = 1, Reset = 0, En = 1, Count = 10
Clk = 1, Reset = 0, En = 1, Count = 11
Clk = 1, Reset = 0, En = 1, Count = 12
Clk = 1, Reset = 0, En = 1, Count = 13
Clk = 1, Reset = 0, En = 1, Count = 14
Clk = 1, Reset = 0, En = 1, Count = 15
Clk = 1, Reset = 0, En = 1, Count = 0
Clk = 1, Reset = 0, En = 1, Count = 1
Clk = 1, Reset = 0, En = 1, Count = 2
Clk = 1, Reset = 0, En = 1, Count = 3
*************************************************/

Answer 3)

VERILOG Code for Half subtractor, DFF, Down Counter 2

module halfsubtractor(
input A, B,
output S, Cout
);

assign S = A ^ B;
assign Cout = (!A & B);

endmodule

module DFF (
input Clk,
input Rst,
input D,
output reg Q
);

always @ (posedge Clk or posedge Rst)
begin
if (Rst)
Q <= 1'd0;
else
Q <= D;
end

endmodule

module counter2 (
input Clk,
input Reset,
input En,
output [3:0] Count
);

wire [3:0] Carry, Diff;

halfsubtractor HS0 (.A(Count[0]), .B(En), .S(Diff[0]), .Cout(Carry[0]));
halfsubtractor HS1 (.A(Count[1]), .B(Carry[0]), .S(Diff[1]), .Cout(Carry[1]));
halfsubtractor HS2 (.A(Count[2]), .B(Carry[1]), .S(Diff[2]), .Cout(Carry[2]));
halfsubtractor HS3 (.A(Count[3]), .B(Carry[2]), .S(Diff[3]), .Cout(Carry[3]));

DFF DFF0 (.Clk(Clk), .Rst(Reset), .D(Diff[0]), .Q(Count[0]));
DFF DFF1 (.Clk(Clk), .Rst(Reset), .D(Diff[1]), .Q(Count[1]));
DFF DFF2 (.Clk(Clk), .Rst(Reset), .D(Diff[2]), .Q(Count[2]));
DFF DFF3 (.Clk(Clk), .Rst(Reset), .D(Diff[3]), .Q(Count[3]));

endmodule

TESTBENCH FOR DOWN COUNTER2

module testbench;
reg Clk;
reg Reset;
reg En;
wire [3:0] Count;

counter2 DUT (.Clk(Clk), .Reset(Reset), .En(En), .Count(Count));

always
#5 Clk = !Clk;

always @(posedge Clk)
$display("Clk = %b, Reset = %d, En = %b, Count = %d", Clk, Reset, En, Count);

initial
begin
Clk = 1'b0;
Reset = 1'b1;
En = 1'b0;
#20;
Reset = 1'b0;
En = 1'b1;
#100;
En = 1'b0;
#60;
En = 1'b1;
#100;
$finish;
end

initial
begin
$recordfile("file1.trn");
$recordvars();
end

endmodule

/*************** Output Of Program *******************
Clk = 1, Reset = 1, En = 0, Count = 0
Clk = 1, Reset = 1, En = 0, Count = 0
Clk = 1, Reset = 0, En = 1, Count = 0
Clk = 1, Reset = 0, En = 1, Count = 15
Clk = 1, Reset = 0, En = 1, Count = 14
Clk = 1, Reset = 0, En = 1, Count = 13
Clk = 1, Reset = 0, En = 1, Count = 12
Clk = 1, Reset = 0, En = 1, Count = 11
Clk = 1, Reset = 0, En = 1, Count = 10
Clk = 1, Reset = 0, En = 1, Count = 9
Clk = 1, Reset = 0, En = 1, Count = 8
Clk = 1, Reset = 0, En = 1, Count = 7
Clk = 1, Reset = 0, En = 0, Count = 6
Clk = 1, Reset = 0, En = 0, Count = 6
Clk = 1, Reset = 0, En = 0, Count = 6
Clk = 1, Reset = 0, En = 0, Count = 6
Clk = 1, Reset = 0, En = 0, Count = 6
Clk = 1, Reset = 0, En = 0, Count = 6
Clk = 1, Reset = 0, En = 1, Count = 6
Clk = 1, Reset = 0, En = 1, Count = 5
Clk = 1, Reset = 0, En = 1, Count = 4
Clk = 1, Reset = 0, En = 1, Count = 3
Clk = 1, Reset = 0, En = 1, Count = 2
Clk = 1, Reset = 0, En = 1, Count = 1
Clk = 1, Reset = 0, En = 1, Count = 0
Clk = 1, Reset = 0, En = 1, Count = 15
Clk = 1, Reset = 0, En = 1, Count = 14
Clk = 1, Reset = 0, En = 1, Count = 13
*****************************************************/

Add a comment
Know the answer?
Add Answer to:
Verilog code help Counter is a sequential circuit. A digital circuit which is used for a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Verilog code help Counter is a sequential circuit. A digital circuit which is used for a counting events (usually clock pulses) is known counter. Counter is most clear application of the usage of...

    Verilog code help Counter is a sequential circuit. A digital circuit which is used for a counting events (usually clock pulses) is known counter. Counter is most clear application of the usage of flip-flops. It is a group of flip-flops with a clock signal applied. Consider the following 4 bits up counter 1. Write mixed behavioral/ structural Verilog code for this counter (HA and Counter structural, D FF behavioral) 2. Write Verilog test bench for this this counter then run...

  • help me to finish the verilog code and test bench Part 2: Sequence Counter Design the...

    help me to finish the verilog code and test bench Part 2: Sequence Counter Design the sequence counter using Xilinx Vivado. Consider the required number of D flip-flops(4). A sample VERILOG source file is as shown: module Seq_COUNT(     ??? clock,     ??? wire [?:?] D,     ??? ??? [?:?] out     );     always @ (??? ???)     ???         // 3 bit Sequence Given is 0 2 4 6 1 3 5 7         out[N-1] <= some expression;...

  • .For the following circuit, do: RR3R2R, Ro G G3G2G,Go Write structural VHDL code. Create two files:...

    .For the following circuit, do: RR3R2R, Ro G G3G2G,Go Write structural VHDL code. Create two files: i) flip flop, ii) top file (where you will interconnect the flip flops and the logic gates). Provide a printout. (10 pts) Write a VHDL testbench according to the timing diagram shown below. Complete the timing diagram by simulating your circuit (Behavioral Simulation). The clock frequency must be 100 MHz with 50% duty cycle. Provide a printout. (15 pts) Ro R1 R2 Ro resetn...

  • WRITE THE CODE IN VERILOG: Instead of using Registers, USE D FLIP FLOPS and a clock....

    WRITE THE CODE IN VERILOG: Instead of using Registers, USE D FLIP FLOPS and a clock. Include the logic for a reset A sequential circuit with three D flip-flops A, B, and C, a trigger x, and an output z1, and zo. On this state machine diagram, the label of the states are in the order of (ABC), the transition is the one bit x, and the output is under the forward slash. x/z1zo. The start state is 001 0/01...

  • Use a behavioral Verilog model to design a 3-bit fault tolerant up-down counter. For each flip-fl...

    Use a behavioral Verilog model to design a 3-bit fault tolerant up-down counter. For each flip-flop (FF) include asynchronous reset and preset signals. Refer to Example 4.3 on page 160 for an example of a single FF with both reset and preset signals as well as with an enable signal. For this project, you don't need to use FFs with enables. You don't also need not-q (nq) in this assignment. Use active-high signals for reset and present signals. The example...

  • Consider the circuit in Figure 1. It is a 4-bit (QQ2Q3) synchronous counter which uses four T-typ...

    Consider the circuit in Figure 1. It is a 4-bit (QQ2Q3) synchronous counter which uses four T-type flip-flops. The counter increases its value on each positive edge of the clock if the Enable signal is asserted. The counter is reset to 0 by setting the Clear signal low. You are to implement an 8-bit counter of this type Enable T Q Clock Clear Figure 1. 4-bit synchronous counter (but you need to implement 8-bit counter in this lab) Specific notes:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT