Lab 3
Lab 3
EXPERIMENT NO 3
Student Name: Abdur Rehman, M. Muneeb Khan Reg. No: 210312, 210276
LAB ASSESSMENT:
Data presentation
Experimental results
Conclusion
Code:
module task1(out,in,clk,rst);
parameter s0=0;
parameter s1=1;
parameter s2=2;
parameter s3=3;
input in, clk, rst;
output reg out;
reg[1:0] NS, CS;
always @(posedge clk or negedge rst)
begin
if(!rst)
begin
CS<=s0;
end
else
begin
CS<=NS;
end
end
always@(*)
begin
case(CS)
s0:
begin
if(in==0)
begin
NS=CS;
out=0;
end
else
begin
NS=s1;
out=0;
end
end
s1:
begin
if(in==0)
begin
NS=CS;
out=0;
end
else
begin
NS=s2;
out=0;
end
end
s2:
begin
if(in==0)
begin
NS=CS;
out=0;
end
else
begin
NS=s3;
out=0;
end
end
s3:
begin
if(in==0)
begin
NS=CS;
out = 0;
end
else
begin
NS=s0;
out=1;
end
end
endcase
end
endmodule
Simple traffic signal controller
Design a state machine for the given scenario on paper and write RTL Verilog for
it. Main road has normally a green light, and minor road a red light. If a car is
detected on minor road (sensor), semaphores changes values, a timer is started
which asserts a signal ‘TIMED’ at the end of counting. When TIMED is asserted,
the semaphores go back to default values.
module TrafficLightController(
input wire clk,
input wire reset,
input wire carDetected,
output reg [1:0] mainRoad, // 00: Red, 01: Green
output reg [1:0] minorRoad, // 00: Red, 01: Green
output reg timed
);
parameter RED = 2'b00;
parameter GREEN = 2'b01;
module traffic_light_tb;
reg clk;
reg reset;
reg carDetected;
wire [1:0] mainRoad;
wire [1:0] minorRoad;
wire timed;
TrafficLightController uut (
.clk(clk),
.reset(reset),
.carDetected(carDetected),
.mainRoad(mainRoad),
.minorRoad(minorRoad),
.timed(timed)
);
initial begin
clk = 0;
reset = 0;
carDetected = 0;
#100;
reset = 1; #50;
reset = 0; #50;
carDetected = 1; #100;
carDetected = 0; #100;
carDetected = 1; #100;
carDetected = 0; #100;
$finish;
end
Moore Machine:
module moore_fsm (
input wire clk,
input wire reset,
input wire serial_in,
output reg out
);
always @* begin
case (state)
S0: if (serial_in == 0) next_state = S1; else next_state = S0;
S1: if (serial_in == 0) next_state = S2; else next_state = S0;
S2: if (serial_in == 0) next_state = S3; else next_state = S0;
S3: if (serial_in == 0) next_state = S0; else next_state = S0;
default: next_state = S0;
endcase
end
always @* begin
case (state)
S0: out = 0;
S1: out = 0;
S2: out = 0;
S3: out = 1;
default: out = 0;
endcase
end
endmodule
moore_fsm uut (
.clk(clk),
.reset(reset),
.serial_in(serial_in),
.out(out)
);
initial begin
clk = 0;
reset = 0;
serial_in = 0;
#100;
reset = 1; #50;
serial_in = 0; #100;
reset = 0; #50;
serial_in = 0; #100;
reset = 1; #50;
serial_in = 1; #100;
reset = 0; #50;
serial_in = 1; #100;
reset = 1; #50;
serial_in = 0; #100;
reset = 0; #50;
serial_in = 1; #100;
reset = 1; #50;
serial_in = 1; #100;
reset = 0; #50;
serial_in = 0; #100;
end
always @* begin
case (state)
S0: if (serial_in == 0) next_state = S1; else next_state = S0;
S1: if (serial_in == 0) next_state = S2; else next_state = S0;
S2: if (serial_in == 0) next_state = S3; else next_state = S0;
S3: if (serial_in == 0) next_state = S0; else next_state = S0;
default: next_state = S0;
endcase
end
always @* begin
case (next_state)
S0: out = 0;
S1: out = 0;
S2: out = 0;
S3: out = 1;
default: out = 0;
endcase
end
endmodule
mealy_fsm uut (
.clk(clk),
.reset(reset),
.serial_in(serial_in),
.out(out)
);
initial begin
clk = 0;
reset = 0;
serial_in = 0;
#100;
reset = 1; #50;
serial_in = 0; #100;
reset = 0; #50;
serial_in = 0; #100;
reset = 1; #50;
serial_in = 1; #100;
reset = 0; #50;
serial_in = 1; #100;
reset = 1; #50;
serial_in = 0; #100;
reset = 0; #50;
serial_in = 1; #100;
reset = 1; #50;
serial_in = 1; #100;
reset = 0; #50;
serial_in = 0; #100;
end
A Moore Machine, on the other hand, is a type of finite state machine where the output is
determined only by its current state. This means the output changes only when the state changes,
typically at the clock edge.
A Flip-Flop is also a basic memory device, but it changes its output based on the applied input and
a clock or control signal. It is edge triggered, meaning it checks the inputs and changes the output
only at times defined by the clock signal.
3. Shifting Contents of Register:
When the contents of a register are shifted left, each bit moves one position to the left, and the
leftmost bit is lost. The rightmost bit (least significant bit) is filled with zero. This operation is
equivalent to multiplying the original number by 2.
When the contents of a register are shifted right, each bit moves one position to the right, and the
rightmost bit is lost. The leftmost bit (most significant bit) is filled with zero. This operation is
equivalent to dividing the original number by 2.
Conclusion:
In this lab, we learnt how to design and implement finite state machines and time-shared
architectures using FPGA Spartan6 Boards. We explored the principles of state machine operation
and the efficiency of time-shared architectures, enhancing our practical understanding of these
fundamental concepts in digital system design.