Finite state machine with data path:
FSM used to change the state, tell the datapath when and how to do different calculations, and datapath used to do different calculations.
Use FSMD to implement the function of c++ code which calculates the square root of a number:
while(sqaure<=data){ // control
square=sqaure+delta; //calculate
delta=delta+2;
}
return delta/2 - 1
Input of FSM: (square<=data), rst, clk
Output of FSM: calculate, clean, returnnum(control signals)
Input of DATA_PATH: datain, calculate, clean, returnnum
Output of DATA_PATH: dataout
|FSM |<-------------------------(square<=data)------------------------- | DATA_PATH |--------> dataout
|FSM | ----------------calculate, clean, returnnum------------------>| DATA_PATH |<-------- datain
FSM:
[email protected](posedge clk)begin
if(rst) cstate<=idle; //asynchronize rst
cstate<=nstate;
end
[email protected](*)begin
calculate=0;
return_num=0;
clean=0;
nstate=cstate;
case(cstate)
idle: if(start) nstate=s1;
s1: if(sqreg<=datain) calculate=1;
else begin count=0; nstate=s2; end
s2: begin returnnum=1; n_state=s3;end
s3: begin n_state=s0; clean=1;end
endcase
end
DATA_PATH:
[email protected](posedge clk)begin
if(!rst)begin dreg=0;sqreg=0;dout=0; end
else if(calculate) begin sqreg = sqreg+dreg; dreg = dreg+2;end
else if(returnnum) dout=(dreg>>1) -1;
else if(clean)begin dreg=0;sqreg=0; end
end