Unit 5
Unit 5
a) Datapath:-which consist of the functional units where all computations are carried out.
typically consist of registers,multiplexers,bus,adder,multipliers,counters and other
functional blocks
b) Control patch:-which implement a FSM and provides control signals to the data path
in a proper sequence.
In responce to the control signals ,various operations are carried out by the data path
Also takes inputs from the data patch regarding various status information
Example:
A=B+C
D=A-C
reg[15:0] A,B,C,D;
B C
D
Example 2: Multiplication by repeated addition
We consider a simple algorithm using repeated start
addition(assume B is non zero)
Read A,B
P=0
P=P+A no
A P B
B=B-1 B=0 yes stop
Comp
Adder
datapath control
path
control path
start s0
A=data_in s1
s0
B=data_in s2
P=0
s1
P=Z
B=B-1
s3
s2
no
Bout=0
s3
yes
done=1 s4
s4
Datapath code:
module mul_datapath(eqz,ldA,ldB,ldP,clrP,decB,dat_in,clk);
input ldA,ldB,ldP,clrP,decB,clk;
input[15:0] data_in;
output eqz;
wire [15:0] x,y,z,bout,bus;
A P B
pipo1 A(x,bus,ldA,clk);
pipo2 P(y,z,ldP,clrP,clk);
cntr B(bout,bus,ldB,decB,clk); Comp
add AD(z,x,y); Adder
comp CMP(eqz,bout);
endmodule
module pipo2(dout,din,ld,clr,clk);
input [15:0] din;
input ld,clr,clk;
output reg [15:0] dout; P pipo2 P(y,z,ldP,clrP,clk);
module add(out,in1,in2);
input [15:0] in1,in2;
output reg [15:0] out; Adder add AD(z,x,y);
always @(*)
out=in1+in2;
endmodule
module comp(eqz,data)
input [15:0] data;
output eqz;
comp CMP(eqz,bout);
assign eqz=(data==0);
endmodule B
module cntr(dout,din,ld,dec,clk);
Comp
input [15:0] din;
input ld,dec,clk;
output reg [15:0] dout;
always @(posedge clk)
if(ld) dout<=din; cntr B(bout,bus,ldB,decB,clk);
else if(dec) dout<=dout-1;
endmodule
Control path code:
module controller(ldA,ldB,ldP,clrP,decB,done,clk,eqz,start);
input clk,eqz,start; s0
output reg ldA,ldB,ldP,clrP,decB,done;
reg [2:0] state;
parameter S0=3'b000,s1=3'b001,s2=3'b010,s3=3'b011,s4=3'b100;
consider a controller for traffic at the intersection of a main highway and country road
There is a sensor to detect cars waiting on the country road.the sensor send a signal x
at input to the controller.x=1 if there are cars on the country road,otherwise x=0.
There are delays on transitions from s1 to s2,from s2 to s3,and from s4 to s0.the dealy
must be controllable
x=0
s0
s4 x=1 state signal
s0 hwy=G, cntry=R
s1 hwy=y, cntry=R
x=0 s1 hwy=R, cntry=R
s2
s3 hwy=R, cntry=G
s4 hwy=R, cntry=Y
s3
s2
x=1
verilog code:
module sig_control(hwy,cntry,x,clock,clear);
output [1:0] hwy,cntry; //2-bit output for 3 states of signals g,y,r
reg [1:0] hwy,cntry;
input x,clock,clear;
parameter red=2'd0, yellow=2'd1, green=2'd2;
parameter s0=3'd0, s1=3'd1, s2=3'd2, s3=3'd3, s4=3'd4;
reg [2:0] state; //internal state variables
reg [2:0] next_state;
always @(state)
begin
hwy=green; //default light assignment for highway light
cntry=red; //default light assignment for country light
case(state)
s0: ; //no change ,use deafult
s1: hwy=yellow;
s2: hwy=red;
s3: begin
hwy=red;
cntry=green;
end
s4: begin
hwy=red;
cntry=yellow;
end
endcase
end
//state machine using case statement
always @(state or x)
begin
case(state)
s0 : if(x)
next_state=s1;
else
next_state=s0;
s1 : begin
repeat(y2rdelay) @(posedge clock)
next_state=s2;
end
s2 : begin
repeat(r2gdelay) @(posedge clock)
next_state=s3;
end
s3 : if(x)
next_state=s3;
else
next_state=s4;
s4 : begin
repeat(y2rdealy) @(posedge clock)
next_state=s0;
end
deafult : next_state=s0;
endcase
end
endmodule
testbench code:
module stimuls;
wire [1:0] main_sig,cntry_sig;
reg car_on_cntry_rd;
reg clock,clear;
//instantiate signal controller
//set up monitor
intial
$monitor($time,"main sig=%b country sig=%b car_on_cntry_rd=%b",
main_sig, cntry_sig, car_on_cntry_rd);
//set up clock
intial
begin
clock = false;
forever #5 clock=~clock;
end
intial
begin
clear=true;
repeat (5) @(negedge clock);
clear=false;
end
//apply stimulas
intial
begin
car_on_cntry_rd=false;
repeat(20)@(negedge clock);
car_on_cntry_rd=true;
repeat(10) @(negedge clock);
car_on_cntry_rd=false;
repeat(20)@(negedge clock);
car_on_cntry_rd=true;
repeat(10)@(negedge clock);
$stop;
end
endmodule
Vending machine design
s5
s10
s15
verilog code:
module vending_machine(new_paper,coin,clk,rst);
output reg new_paper;
input [1:0] coin;
input clk,rst;
reg[1:0] state;
reg[1:0] next_state;
parameter s0=2'b00, s5=2'b01, s10=2'b10, s15=2'b11;
always@(state,coin)
begin
case(state)
s0 : begin
if(coin==2'b00) next_state=s0;
else if(coin==2'b01) next_state=s5;
else if (coin==2'b10) next_state=s10;
end
s5 : begin
if (coin==2'b00) next_state=s5;
else if(coin==2'b01)next_state=s10; s0
else if(coin==2'b10)next_state=s15;
end
s10:begin
if(coin==2'b00)next_state=s10; s5
else if(coin==2'b01)next_state=s15;
else if(coin==2'b10)next_state=s15;
end
s15:begin s10
next_state=s0;
end
default:next_state=s0;
s15
endcase
end
always@(state)
begin
case(state)
s0:new_paper<=1'b0;
s5:new_paper<=1'b0;
s10:new_paper<=1'b0;
s15:new_paper<=1'b1;
default : new_paper<=1'b0;
endcase
end
endmodule