Question

In: Electrical Engineering

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

// pass the next_state to current_state;

sig <= sig + 1;

  

  

case(sig[2:0])

3'b000:begin

P1 <= 3'b100; //path 1 is greeen

P2 <= 3'b001; //path 2 is red

PL1 <= 3'b111;

PL2 <= 3'b000;

end

  

3'b001:begin

P1 <= 3'b010; //path 1 is yellow

P2 <= 3'b001; // path 2 is red

PL1 <= 3'b000;

PL2 <= 3'b111;

end

3'b010:begin

P1 <= 3'b001; //path 1 is red

P2 <= 3'b100; //path 2 is green

PL1 <= 3'b000;

PL2 <= 3'b111;

end

3'b011:begin

P1 <= 5'b001; //path 1 is red

P2 <= 5'b010; //path 2 is yellow

PL1 <= 3'b000;

PL2 <= 3'b111;

end

3'b100:begin

P1 <= 3'b001; //path 1 is red

P2 <= 3'b001; //path 3 is red

PL1 <= 3'b000;

PL2 <= 3'b000;

end

5'b11111:sig<= 6'b00000;

default:begin   

  

end

endcase

end

endmodule

Can anyone write a testbench for this in Verilog, ind means indicator.

Solutions

Expert Solution

// the design has flaws a,b,ind are not used in the behavious of the design
// sig was no initialised hence the design wont work
// fixed the second flaw
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;
sig<=0;// design was flawed i used this to fic you design flaws

end

else begin

// pass the next_state to current_state;

sig <= sig + 1;

  

  

case(sig[2:0])

3'b000:begin

P1 <= 3'b100; //path 1 is greeen

P2 <= 3'b001; //path 2 is red

PL1 <= 3'b111;

PL2 <= 3'b000;

end

  

3'b001:begin

P1 <= 3'b010; //path 1 is yellow

P2 <= 3'b001; // path 2 is red

PL1 <= 3'b000;

PL2 <= 3'b111;

end

3'b010:begin

P1 <= 3'b001; //path 1 is red

P2 <= 3'b100; //path 2 is green

PL1 <= 3'b000;

PL2 <= 3'b111;

end

3'b011:begin

P1 <= 5'b001; //path 1 is red

P2 <= 5'b010; //path 2 is yellow

PL1 <= 3'b000;

PL2 <= 3'b111;

end

3'b100:begin

P1 <= 3'b001; //path 1 is red

P2 <= 3'b001; //path 3 is red

PL1 <= 3'b000;

PL2 <= 3'b000;

end

5'b11111:sig<= 6'b00000;

default:begin   

  

end

endcase
// $display("sig=%b ",sig);

end

endmodule

//////testbench:


module test();
reg a,b,clk,reset,ind;
wire [2:0] P1,P2;
wire [1:0] PL1,PL2;
  
traffic t1(.*);// .* automatic connection if the name of the ports are same as variable names in the testbench
initial
begin
clk=0;reset=0;a=0;b=0;ind=0;
#2 reset=1;
#4 reset=0;
#40
#4 a=0;b=0;ind=1;
#40 $finish;
end
initial
forever
#2 clk=~clk;
initial
begin
$monitor("a=%b b=%b ind=%b reset=%b P1=%b p2=%b PL1=%b PL2=%b",a,b,ind,reset,P1,P2,PL1,PL2);
end
endmodule

output:

19-12-03 00:35:02 EST] iverilog '-Wall' '-g2012' design.sv testbench.sv && unbuffer vvp a.out
a=0 b=0 ind=0 reset=0 P1=xxx p2=xxx PL1=xx PL2=xx
a=0 b=0 ind=0 reset=1 P1=100 p2=001 PL1=11 PL2=00
a=0 b=0 ind=0 reset=0 P1=100 p2=001 PL1=11 PL2=00
a=0 b=0 ind=0 reset=0 P1=010 p2=001 PL1=00 PL2=11
a=0 b=0 ind=0 reset=0 P1=001 p2=100 PL1=00 PL2=11
a=0 b=0 ind=0 reset=0 P1=001 p2=010 PL1=00 PL2=11
a=0 b=0 ind=0 reset=0 P1=001 p2=001 PL1=00 PL2=00
a=0 b=0 ind=0 reset=0 P1=100 p2=001 PL1=11 PL2=00
a=0 b=0 ind=0 reset=0 P1=010 p2=001 PL1=00 PL2=11
a=0 b=0 ind=0 reset=0 P1=001 p2=100 PL1=00 PL2=11
a=0 b=0 ind=1 reset=0 P1=001 p2=010 PL1=00 PL2=11
a=0 b=0 ind=1 reset=0 P1=001 p2=001 PL1=00 PL2=00
a=0 b=0 ind=1 reset=0 P1=100 p2=001 PL1=11 PL2=00
a=0 b=0 ind=1 reset=0 P1=010 p2=001 PL1=00 PL2=11
a=0 b=0 ind=1 reset=0 P1=001 p2=100 PL1=00 PL2=11
a=0 b=0 ind=1 reset=0 P1=001 p2=010 PL1=00 PL2=11
a=0 b=0 ind=1 reset=0 P1=001 p2=001 PL1=00 PL2=00
Done


Related Solutions

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;       ...
In a open economy. C = 25+0.92(Y-T) Y=6755 G=164 T=104 I=1406-13r NX=100-10e r*=19 If r* change...
In a open economy. C = 25+0.92(Y-T) Y=6755 G=164 T=104 I=1406-13r NX=100-10e r*=19 If r* change from 19 to 10 What will happen to Y, C, I, S?
In an open economic C = 25+0.92(Y-T) Y=6755 G=164 T=104 I=1406-13r NX=100-10e r*=19 If NX change...
In an open economic C = 25+0.92(Y-T) Y=6755 G=164 T=104 I=1406-13r NX=100-10e r*=19 If NX change to NX= 200-10e. (the exports have increased) What will happen to Y, C, S, I?
"Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence.
For Java"Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence. Create a for loop that compares the two strings starting from index 0. For each match, add one point to userScore. Upon a mismatch, exit the loop using a break statement. Ex: The following patterns yield a userScore of 4: simonPattern: R, R, G, B, R, Y, Y, B, G, Yimport java.util.Scanner;public class SimonSays...
(C++) "Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence
(C++) "Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence. Create a for loop that compares the two strings starting from index 0. For each match, add one point to userScore. Upon a mismatch, exit the loop using a break statement. Assume simonPattern and userPattern are always the same length. Ex: The following patterns yield a userScore of 4: simonPattern: RRGBRYYBGY userPattern: RRGBBRYBGY
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT