Question

In: Electrical Engineering

module traffic(clk, reset, P1, P2, P3, P4, PL); input clk; input reset; output[4:0] P1; output[4:0] P2;...

module traffic(clk, reset, P1, P2, P3, P4, PL);
input clk;
input reset;
output[4:0] P1;
output[4:0] P2;
output[4:0] P3; // four roads
output [4:0] P4;
output[3:0] PL; //Pl is pedestrian

reg [4:0] P1;
reg [4:0] P2;
reg [4:0] P3;
reg [4:0] P4;
reg [3:0] PL;
reg [4:0] sig;

always @(posedge clk or negedge reset)
begin
   if(reset == 1'b0)begin
       P1 <= 5'b00100;
       P2 <= 5'b00100;
       P3 <= 5'b00100;
       P4 <= 5'b00100;
       PL <= 4'b1111;
       sig <= 5'b00000;
   end
   else begin
       sig <= sig + 1;
      
       case(sig[4:0])
       5'b00000:begin
       P1 <= 5'b10011; //path 1 is greeen
       P2 <= 5'b00100; //all other paths are red
       P3 <= 5'b10011;
       P4 <= 5'b10011;
       PL <= 4'b1111;
       end
      
       5'b00100:begin
       P1 <= 5'b01000; //path 1 is yellow
       P2 <= 5'b00100; // all other paths are red
       P3 <= 5'b00100;
       P4 <= 5'b00100;
       PL <= 4'b1111;
       end

       5'b01000:begin
       P1 <= 5'b00100; //path 1 is red
       P2 <= 5'b10011; //path 2 is green
       P3 <= 5'b00100; // all other paths are red
       P4 <= 5'b00100;
       PL <= 4'b1111;
       end

       5'b01100:begin
       P1 <= 5'b00100;
       P2 <= 5'b01000; //path 2 is yellow
       P3 <= 5'b00100; // all other paths are red
       P4 <= 5'b00100;
       PL <= 4'b1111;
       end

       5'b10000:begin
       P1 <= 5'b00100;
       P2 <= 5'b00100; //path 3 is green
       P3 <= 5'b10011; // all other paths are red
       P4 <= 5'b00100;
       PL <= 4'b1111;
       end

       5'b10100:begin
       P1 <= 5'b00100;
       P2 <= 5'b00100; //path 3 is yellow
       P3 <= 5'b01000; // all other paths are red
       P4 <= 5'b00100;
       PL <= 4'b1111;
       end

       5'b11000:begin
       P1 <= 5'b00100;
       P2 <= 5'b00100; //all other paths are red
       P3 <= 5'b00100; // path 4 is yello
       P4 <= 5'b10011;
       PL <= 4'b1111;
       end

       5'b11100:begin
       P1 <= 5'b00100; //all other paths are red
       P2 <= 5'b00100;
       P3 <= 5'b00100;
       P4 <= 5'b00100;
       PL <= 4'b0000; //pedestrian green
       end

       5'b11111:sig<= 6'b00000;

       default:begin     
      
       end
   endcase

   end
end

endmodule

can anyone write a testbench for this code in Verilog.

Solutions

Expert Solution

TESTBENCH FOR THE GIVEN CODE:

module testbench_traffic;

reg clk;
reg reset;
wire [4:0] P1;
wire [4:0] P2;
wire [4:0] P3; // four roads
wire [4:0] P4;
wire [3:0] PL; //Pl is pedestrian
wire [4:0] sig;

initial begin clk=1'b0;reset=1'b0; end //since clk and reset are reg types,we are giving initial values
always #10 clk=~clk; //generate clock with time period 20ns
always #200 reset=~reset; //generate reset with some time period
traffic k(clk, reset, P1, P2, P3, P4, PL); //instantiate the module traffic

  

endmodule

EXPLANATION:

To write a test bench code

1.create a simulaton source.

2.declare all inputs as reg types.

3. declare all outputs as wire type.

4.give initial values to reg type inputs.

5.generate clock ,.

6.generate reset pulse.

6.instantiate the module,

7.use initial block again to change the values of the inputs with some delays,if needed



CODE:

SIMULATION WAVEFORMS:


Related Solutions

module traffic(a, b,clk, reset, P1, P2, PL1, PL2, ind); input a,b; input clk; //g=100 y=010 r=001...
module traffic(a, b,clk, reset, P1, P2, PL1, PL2, ind); input a,b; input clk; //g=100 y=010 r=001 input reset; //g for pedes = 000 red = 111 input ind; output[2:0] P1; output[2:0] P2; // two roads output[1:0] PL1; //Pl is pedestrian output[1:0] PL2; reg [2:0] P1; reg [2:0] P2; reg [1:0] PL1; reg [1:0] PL2; reg [2:0] sig; always @(posedge clk, posedge reset) if(reset) begin P1 <= 3'b100; P2 <= 3'b001; PL1 <= 3'b111; PL2 <= 3'b000; end else begin //...
A company produces and sells 4 types of products – P1, P2, P3, and P4. The...
A company produces and sells 4 types of products – P1, P2, P3, and P4. The table below summarizes the relevant data. Three types of raw materials – RM1, RM2, and RM3 are required for the manufacturing process. The availability and cost per unit for each raw material are specified under the columns Available and Cost per unit, respectively. The company is contractually obligated to produce at least a minimum quantity for each product; these are specified in the row...
1.) You are given 4 processes P1,P2, P3, P4 all ariving at the same time t=0,...
1.) You are given 4 processes P1,P2, P3, P4 all ariving at the same time t=0, in a dual-processor SMP. (The two CPUs are referred to as CPU-0 and CPU-1.) The service time for each process is 40s, 20s, 20s and 10s respectively (a) What are the possibilities for the turnaround time of the job that includes the execution of all 4 processes? You may assume that each service time is just CPU time and no process interferes with the...
Let pi = P(X = i) and suppose that p1 + p2 + p3 + p4...
Let pi = P(X = i) and suppose that p1 + p2 + p3 + p4 = 1. Suppose that E(X) = 2.5. (a) What values of p1, p2, p3, and p4 maximize Var(X)? (b) What values of p1, p2, p3, and p4 minimize Var(X)?
can i ask FSM verilog? ---------------------- module FSM(clk, rst, choice, out); input clk, rst, choice; output...
can i ask FSM verilog? ---------------------- module FSM(clk, rst, choice, out); input clk, rst, choice; output reg [1:0] out; reg [1:0] state, nextstate; parameter [1:0] S0 = 2’b00, S1 = 2’b01, S2 = 2’b10, S3 = 2’b11; always@(posedge clk) begin if (rst == 1’b0) begin state <= S0; end else end state <= nextstate; always@(state, rst, choice) begin case (state) S0 : begin out = 2’b00; if (rst == 1’b1) nextstate <= S1; end S1 : begin out = 2’b01;...
Consider following four processes arriving in the order of P1, P2, P3 and P4. Process   Arrival...
Consider following four processes arriving in the order of P1, P2, P3 and P4. Process   Arrival Time CPU burst Time P1             0.0     6 P2    0.0                              8 P3              0.0                                7 P4              0.0                                 3 What is the average waiting time for RR scheduling (time quantum = 5)? What is the average waiting time for SJF scheduling? What is the average waiting time for FCFS scheduling?
Serial Problem Business Solutions LO P1, P2, P3, P4, P5, P6 After the success of the...
Serial Problem Business Solutions LO P1, P2, P3, P4, P5, P6 After the success of the company’s first two months, Santana Rey continues to operate Business Solutions. The November 30, 2019, unadjusted trial balance of Business Solutions (reflecting its transactions for October and November of 2019) follows. No. Account Title Debit Credit 101 Cash $ 38,264 106 Accounts receivable 12,618 126 Computer supplies 2,545 128 Prepaid insurance 2,220 131 Prepaid rent 3,300 163 Office equipment 8,000 164 Accumulated depreciation—Office equipment...
Serial Problem Business Solutions LO P1, P2, P3, P4, P5, P6 After the success of the...
Serial Problem Business Solutions LO P1, P2, P3, P4, P5, P6 After the success of the company’s first two months, Santana Rey continues to operate Business Solutions. The November 30, 2019, unadjusted trial balance of Business Solutions (reflecting its transactions for October and November of 2019) follows. No. Account Title Debit Credit 101 Cash $ 38,264 106 Accounts receivable 12,618 126 Computer supplies 2,545 128 Prepaid insurance 2,220 131 Prepaid rent 3,300 163 Office equipment 8,000 164 Accumulated depreciation—Office equipment...
Serial Problem Business Solutions LO P1, P2, P3, P4, P5, P6 After the success of the...
Serial Problem Business Solutions LO P1, P2, P3, P4, P5, P6 After the success of the company’s first two months, Santana Rey continues to operate Business Solutions. The November 30, 2019, unadjusted trial balance of Business Solutions (reflecting its transactions for October and November of 2019) follows. No. Account Title Debit Credit 101 Cash $ 38,564 106 Accounts receivable 12,818 126 Computer supplies 2,645 128 Prepaid insurance 1,860 131 Prepaid rent 3,200 163 Office equipment 8,700 164 Accumulated depreciation—Office equipment...
Serial Problem Business Solutions LO P1, P2, P3, P4, P5 After the success of the company’s...
Serial Problem Business Solutions LO P1, P2, P3, P4, P5 After the success of the company’s first two months, Santana Rey continues to operate Business Solutions. The November 30, 2016, unadjusted trial balance of Business Solutions (reflecting its transactions for October and November of 2016) follows. No. Account Title Debit Credit 101 Cash $ 38,664 106 Accounts receivable 12,718 126 Computer supplies 2,545 128 Prepaid insurance 2,160 131 Prepaid rent 3,020 163 Office equipment 8,400 164 Accumulated depreciation—Office equipment $...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT