In: Computer Science
Verilog code with explanation please
// Netflix has been engaged by movie studios to advertise new movies.
// Netflix will show visitors one of 4 ads based on the kind of movie
// they last watched.
// The following characteristics of the last watched movie are
// considered:
// - Whether the movie was animated? (A = 1 means the movie was
// animated; otherwise A = 0)
// - Whether the starring actor was female (F = 1) or male (F = 0)?
// - The type of movie: (T = 2'b00 (action movie), 2'b01 (romance),
// 2'b10 (comedy), 2'b11 (thriller))
// The ad served is chosen by the following rules:
// - "A Good Day to Die Hard" (M = 2'b00) will be shown to viewers of
// action movies and thrillers, unless they are animated or had a
// female starring actor.
// - "Safe Haven" (M = 2'b01) will be selected for people who had
// viewed romance movies or movies with a female starring actor that
// are not comedies.
// - When the previous two movie ads aren't shown, "Escape from Planet
// Earth" (M = 2'b10) will be shown to people viewing animated movies,
// comedies, or action movies.
// - Otherwise, "Saving Lincoln" (M = 2'b11) will be shown.
module movies(M, A, F, T);
output [1:0] M;
input A, F;
input [1:0] T;
endmodule // movies
Verilog code:
//=======================================
module movies(M, A, F, T);
output [1:0] M;
input A, F;
input [1:0] T;
reg [1:0] movie;
always @(*) // always combo loop
begin
// Action movie or thriller movie but not animated or starred by
female
if(((T==2'b00) || (T==2'b11)) && ((A!=1'b1) &&
(F!=1'b1)))
begin
movie = 2'b00;
end
// Viwed romantic movie or female stared movie but not
commedy
else if((T==2'b01) || ( F==1'b1 &&
T!=2'b10 ))
begin
movie = 2'b01;
end
// People viwed animated movie or commedies or action movie
else if((A==1'b1) || (T==2'b10) ||
(T==2'b00))
begin
movie = 2'b10;
end
// otherwise
else
begin
movie = 2'b11;
end
end
assign M = movie; // assigning movie to output
endmodule // movies
//=======================================
tested with testbench:
//=======================================
// Code your testbench here
// or browse Examples
module TB();
reg A,F;
reg [1:0] T;
wire [1:0] M;
initial
begin
// All 16 case assignment
A=0;F=0; T=2'b00;
#1 T=2'b01;
#1 T=2'b10;
#1 T=2'b11;
#1 T=2'b00; F = 1;
#1 T=2'b01;
#1 T=2'b10;
#1 T=2'b11;
#1 T=2'b00;F=0; A=1;
#1 T=2'b01;
#1 T=2'b10;
#1 T=2'b11;
#1 T=2'b00; F = 1;
#1 T=2'b01;
#1 T=2'b10;
#1 T=2'b11;
end
initial
begin
// print out input and output of module movies
$display(" Advertisment A F T M");
$monitor("
%b %b %b %b",A,F,T,M);
end
movies umovies
(
.M(M),
.A(A),
.F(F),
.T(T)
);
endmodule
//=======================================
Simulation log print:
Theoretical Truth table:
A | F | T | M |
0 | 0 | 00 | 00 |
0 | 0 | 01 | 01 |
0 | 0 | 10 | 10 |
0 | 0 | 11 | 00 |
0 | 1 | 00 | 01 |
0 | 1 | 01 | 01 |
0 | 1 | 10 | 10 |
0 | 1 | 11 | 01 |
1 | 0 | 00 | 10 |
1 | 0 | 01 | 01 |
1 | 0 | 10 | 10 |
1 | 0 | 11 | 10 |
1 | 1 | 00 | 01 |
1 | 1 | 01 | 01 |
1 | 1 | 10 | 10 |
1 | 1 | 11 | 01 |