99精品伊人亚洲|最近国产中文炮友|九草在线视频支援|AV网站大全最新|美女黄片免费观看|国产精品资源视频|精彩无码视频一区|91大神在线后入|伊人终合在线播放|久草综合久久中文

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線(xiàn)課程
  • 觀(guān)看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

合理高效地使用狀態(tài)機(jī)是數(shù)字電路中的重要技能

OpenFPGA ? 來(lái)源:OpenFPGA ? 2023-02-22 09:17 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

1題目說(shuō)明

在許多(較舊的)串行通信協(xié)議中,每個(gè)數(shù)據(jù)字節(jié)都與一個(gè)起始位和一個(gè)停止位一起發(fā)送,以幫助接收器從位流中分隔字節(jié)。一種常見(jiàn)的方案是使用一個(gè)起始位 (0)、8 個(gè)數(shù)據(jù)位和 1 個(gè)停止位 (1)。當(dāng)沒(méi)有傳輸任何內(nèi)容(空閑)時(shí),該線(xiàn)路也處于邏輯 1。

設(shè)計(jì)一個(gè)有限狀態(tài)機(jī),當(dāng)給定比特流時(shí),該機(jī)器將識(shí)別何時(shí)正確接收了字節(jié)。它需要識(shí)別起始位,等待所有 8 個(gè)數(shù)據(jù)位,然后驗(yàn)證停止位是否正確。如果停止位沒(méi)有按預(yù)期出現(xiàn),則 FSM 必須等到它找到停止位,然后才能?chē)L試接收下一個(gè)字節(jié)。

一些時(shí)序圖

無(wú)錯(cuò)誤:

63413946-b249-11ed-bfe3-dac502259ad0.png

未找到停止位。第一個(gè)字節(jié)被丟棄:

636dd820-b249-11ed-bfe3-dac502259ad0.png

模塊端口聲明

moduletop_module(
inputclk,
inputin,
inputreset,//Synchronousreset
outputdone
);

2題目解析

串口接收問(wèn)題,題目沒(méi)給狀態(tài)圖,需要自己繪制:

638b53f0-b249-11ed-bfe3-dac502259ad0.png

moduletop_module(
inputlogicclk,
inputlogicin,
inputlogicreset,//Synchronousreset
outputlogicdone
);

//definestate
typedefenumlogic[3:0]{idle=4'd0,start=4'd1,
receive_1=4'd2,receive_2=4'd3,
receive_3=4'd4,receive_4=4'd5,
receive_5=4'd6,receive_6=4'd7,
receive_7=4'd8,receive_8=4'd9,
stop=4'd10,waite=4'd11
}state_def;

state_defcur_state,next_state;

varlogic[3:0]state_cout;
//describestatetransitionlogicusecombinationallogic

always_combbegin
case(cur_state)
idle:begin
if(!in)begin
next_state=start;
end
elsebegin
next_state=idle;
end
end

start:begin
next_state=receive_1;
end

receive_1:begin
next_state=receive_2;
end

receive_2:begin
next_state=receive_3;
end

receive_3:begin
next_state=receive_4;
end

receive_4:begin
next_state=receive_5;
end

receive_5:begin
next_state=receive_6;
end

receive_6:begin
next_state=receive_7;
end

receive_7:begin
next_state=receive_8;
end

receive_8:begin
if(!in)begin
next_state=waite;
end
elsebegin
next_state=stop;
end
end

stop:begin
if(!in)begin
next_state=start;
end
elsebegin
next_state=idle;
end
end

waite:begin
if(!in)begin
next_state=waite;
end
elsebegin
next_state=idle;
end
end
default:begin
next_state=idle;
end
endcase
end

//descibestatesequencerusesequentiallogic

always_ff@(posedgeclk)begin
if(reset)begin
cur_state<=?idle?;
????end????
????else?begin
????????cur_state?<=?next_state?;
????end
end


//describe?output?decoder?use?combinational?logic

assign?done?=?(cur_state?==?stop)?;?

endmodule
63ba56fa-b249-11ed-bfe3-dac502259ad0.png

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:

63e6a070-b249-11ed-bfe3-dac502259ad0.png

注意圖中無(wú)波形。

這一題就結(jié)束了。

Part3Problem 135-Fsm_serialdata

3題目說(shuō)明

上一題用一個(gè)有限狀態(tài)機(jī)可以識(shí)別串行比特流中的字節(jié)何時(shí)被正確接收,添加一個(gè)數(shù)據(jù)路徑將輸出正確接收的數(shù)據(jù)字節(jié)。out_byte在done為1時(shí)需要有效,否則不關(guān)心。

請(qǐng)注意,串行協(xié)議首先發(fā)送最低有效位。

一些時(shí)序圖

無(wú)錯(cuò)誤:

64350d6e-b249-11ed-bfe3-dac502259ad0.png

模塊端口聲明

moduletop_module(
inputclk,
inputin,
inputreset,//Synchronousreset
output[7:0]out_byte,
outputdone
);

4題目解析

狀態(tài)機(jī)與上題一致。

moduletop_module(
inputlogicclk,
inputlogicin,
inputlogicreset,//Synchronousreset
output[7:0]out_byte,
outputlogicdone
);

//definestate
typedefenumlogic[3:0]{idle=4'd0,start=4'd1,
receive_1=4'd2,receive_2=4'd3,
receive_3=4'd4,receive_4=4'd5,
receive_5=4'd6,receive_6=4'd7,
receive_7=4'd8,receive_8=4'd9,
stop=4'd10,waite=4'd11
}state_def;

state_defcur_state,next_state;

varlogic[3:0]state_cout;
//describestatetransitionlogicusecombinationallogic

always_combbegin
case(cur_state)
idle:begin
if(!in)begin
next_state=start;
end
elsebegin
next_state=idle;
end
end

start:begin
next_state=receive_1;
end

receive_1:begin
next_state=receive_2;
end

receive_2:begin
next_state=receive_3;
end

receive_3:begin
next_state=receive_4;
end

receive_4:begin
next_state=receive_5;
end

receive_5:begin
next_state=receive_6;
end

receive_6:begin
next_state=receive_7;
end

receive_7:begin
next_state=receive_8;
end

receive_8:begin
if(!in)begin
next_state=waite;
end
elsebegin
next_state=stop;
end
end

stop:begin
if(!in)begin
next_state=start;
end
elsebegin
next_state=idle;
end
end

waite:begin
if(!in)begin
next_state=waite;
end
elsebegin
next_state=idle;
end
end
default:begin
next_state=idle;
end
endcase
end

//descibestatesequencerusesequentiallogic

always_ff@(posedgeclk)begin
if(reset)begin
cur_state<=?idle?;
????end????
????else?begin
????????cur_state?<=?next_state?;
????end
end


//describe?output?decoder?use?combinational?logic

assign?done?=?(cur_state?==?stop)?;
assign?out_byte?=?done???out_bytes_temp?:?8'd0?;

var?logic?[7:0]?out_bytes_temp?;
always_ff?@(?posedge?clk?)?begin?
????if?(next_state?==?receive_1)?begin
????????out_bytes_temp[0]?<=?in?;
????end
????else?if?(next_state?==?receive_2)?begin
????????out_bytes_temp[1]?<=?in?;
????end
????else?if?(next_state?==?receive_3)?begin
????????out_bytes_temp[2]?<=?in?;
????end
????else?if?(next_state?==?receive_4)?begin
????????out_bytes_temp[3]?<=?in?;
????end?
????else?if?(next_state?==?receive_5)?begin
????????out_bytes_temp[4]?<=?in?;
????end
????else?if?(next_state?==?receive_6)?begin
????????out_bytes_temp[5]?<=?in?;
????end
????else?if?(next_state?==?receive_7)?begin
????????out_bytes_temp[6]?<=?in?;
????end
????else?if?(next_state?==?receive_8)?begin
????????out_bytes_temp[7]?<=?in?;
????end
????
end

endmodule

645b7ce2-b249-11ed-bfe3-dac502259ad0.png

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:

648fa80a-b249-11ed-bfe3-dac502259ad0.png

注意圖中無(wú)波形。

這一題就結(jié)束了。

Part4Problem 136-Fsm_serialdp

5題目說(shuō)明

這題仍然是在前面的基礎(chǔ)上進(jìn)行進(jìn)化,增添了奇偶校驗(yàn)位。奇偶校驗(yàn)(Parity Check)是一種校驗(yàn)代碼傳輸正確性的方法。根據(jù)被傳輸?shù)囊唤M二進(jìn)制代碼的數(shù)位中“1”的個(gè)數(shù)是奇數(shù)或偶數(shù)來(lái)進(jìn)行校驗(yàn)。采用奇數(shù)的稱(chēng)為奇校驗(yàn),反之,稱(chēng)為偶校驗(yàn)。奇偶校驗(yàn)是在傳輸中保障數(shù)據(jù)接收正確的常用方法,也是最初級(jí)的校驗(yàn)方式。

該題采用的是奇校驗(yàn)的方式,并且提供了奇偶校驗(yàn)?zāi)K。原本 start 和 stop 位之間的8 bit 變?yōu)榱? bit,新增的1 bit 為奇校驗(yàn)位,從而使得這9 bit 中“1”的數(shù)量為奇數(shù)個(gè),即題目中提供的奇偶校驗(yàn)?zāi)K輸出為1時(shí)表面數(shù)據(jù)正確,否則數(shù)據(jù)錯(cuò)誤不予接收。波形圖如下所示。

moduleparity(
inputclk,
inputreset,
inputin,
outputregodd);

always@(posedgeclk)
if(reset)odd<=?0;
????????else?if?(in)?odd?<=?~odd;

endmodule

請(qǐng)注意,串行協(xié)議首先發(fā)送最低有效位,然后在 8 個(gè)數(shù)據(jù)位之后發(fā)送奇偶校驗(yàn)位。

一些時(shí)序圖

64b3dc98-b249-11ed-bfe3-dac502259ad0.png

模塊端口聲明

moduletop_module(
inputclk,
inputin,
inputreset,//Synchronousreset
output[7:0]out_byte,
outputdone
);

6題目解析

moduletop_module(
inputlogicclk,
inputlogicin,
inputlogicreset,//Synchronousreset
output[7:0]out_byte,
outputlogicdone
);


//ModifyFSManddatapathfromFsm_serialdata

//definestate
typedefenumlogic[3:0]{idle=4'd0,start=4'd1,
receive_1=4'd2,receive_2=4'd3,
receive_3=4'd4,receive_4=4'd5,
receive_5=4'd6,receive_6=4'd7,
receive_7=4'd8,receive_8=4'd9,
stop=4'd10,waite=4'd11,parity=4'd12
}state_def;

state_defcur_state,next_state;

wirelogicodd;
//describestatetransitionlogicusecombinationallogic

always_combbegin
case(cur_state)
idle:begin
if(!in)begin
next_state=start;
end
elsebegin
next_state=idle;
end
end

start:begin
next_state=receive_1;
end

receive_1:begin
next_state=receive_2;
end

receive_2:begin
next_state=receive_3;
end

receive_3:begin
next_state=receive_4;
end

receive_4:begin
next_state=receive_5;
end

receive_5:begin
next_state=receive_6;
end

receive_6:begin
next_state=receive_7;
end

receive_7:begin
next_state=receive_8;
end

receive_8:begin
next_state=parity;
end

parity:begin
if(!in)begin
next_state=waite;
end
elsebegin
next_state=stop;
end
end

stop:begin
if(!in)begin
next_state=start;
end
elsebegin
next_state=idle;
end
end

waite:begin
if(!in)begin
next_state=waite;
end
elsebegin
next_state=idle;
end
end

default:begin
next_state=idle;
end
endcase
end

//descibestatesequencerusesequentiallogic

always_ff@(posedgeclk)begin
if(reset)begin
cur_state<=?idle?;
????end????
????else?begin
????????cur_state?<=?next_state?;
????end
end


//describe?output?decoder?use?combinational?logic

assign?reset_en?=?(reset?==?1'd1)?||?(next_state?==?stop)?||?(next_state?==?idle)?||?(next_state?==?start)?;

wire?logic?reset_en?;
var?logic?[7:0]?out_bytes_temp?;
always_ff?@(?posedge?clk?)?begin?
????if?(next_state?==?receive_1)?begin
????????out_bytes_temp[0]?<=?in?;
????end
????else?if?(next_state?==?receive_2)?begin
????????out_bytes_temp[1]?<=?in?;
????end
????else?if?(next_state?==?receive_3)?begin
????????out_bytes_temp[2]?<=?in?;
????end
????else?if?(next_state?==?receive_4)?begin
????????out_bytes_temp[3]?<=?in?;
????end?
????else?if?(next_state?==?receive_5)?begin
????????out_bytes_temp[4]?<=?in?;
????end
????else?if?(next_state?==?receive_6)?begin
????????out_bytes_temp[5]?<=?in?;
????end
????else?if?(next_state?==?receive_7)?begin
????????out_bytes_temp[6]?<=?in?;
????end
????else?if?(next_state?==?receive_8)?begin
????????out_bytes_temp[7]?<=?in?;
????end
????
end

always_ff?@(?posedge?clk?)?begin?
????if?(reset)?begin
????????out_byte?<=?8'd0?;
????????done?<=?1'd0?;
????end
????else?if?(next_state?==?stop?&&?odd?==?1'd1)?begin
????????out_byte?<=?out_bytes_temp?;
????????done?<=?1'd1?;
????end
????else?begin
????????out_byte?<=?8'd0?;
????????done?<=?1'd0?;
????end
????
end

???//?New:?Add?parity?checking.?

parity?u_parity?(?.clk(clk),
??????????????????.reset(reset_en),
??????????????????.in(in),
??????????????????.odd(odd)
????????????????);

????

endmodule
65094a34-b249-11ed-bfe3-dac502259ad0.png

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:

6536a93e-b249-11ed-bfe3-dac502259ad0.png

注意圖中無(wú)波形。

這一題就結(jié)束了。

Part5Problem 137-Fsm_hdlc

7題目說(shuō)明

同步HDLC幀涉及從連續(xù)的比特流中解碼尋找某一幀(即數(shù)據(jù)包)的開(kāi)始和結(jié)束位置的位模式。(對(duì)位模式不太理解的可以參見(jiàn)https://zhuanlan.zhihu.com/p/46317118)。如果接收到連續(xù)的6個(gè)1(即01111110),即是幀邊界的“標(biāo)志”。同時(shí)為了避免輸入的數(shù)據(jù)流中意外包含這個(gè)幀邊界“標(biāo)志”,數(shù)據(jù)的發(fā)送方必須在數(shù)據(jù)中連續(xù)的5個(gè)1之后插入一個(gè)0,而數(shù)據(jù)的接收方必須將這個(gè)多余的0檢測(cè)出來(lái)并丟棄掉。同時(shí),如果輸入檢測(cè)到了了連續(xù)7個(gè)或更多的1時(shí),接收方還需要發(fā)出錯(cuò)誤信號(hào)。

創(chuàng)建一個(gè)有限狀態(tài)機(jī)來(lái)識(shí)別這三個(gè)序列:

0111110 : 信號(hào)位需要被丟棄(disc)。

01111110:標(biāo)記幀的開(kāi)始/結(jié)束 ( flag )。

01111111...:錯(cuò)誤(7 個(gè)或更多 1)(錯(cuò)誤)。

當(dāng) FSM 被重置時(shí),它應(yīng)該處于一種狀態(tài),就像之前的輸入為 0 一樣。

以下是說(shuō)明所需操作的一些示例序列。

丟棄0111110:

65616e94-b249-11ed-bfe3-dac502259ad0.png

圖片來(lái)自HDLBits

標(biāo)志01111110:

657bc398-b249-11ed-bfe3-dac502259ad0.png

圖片來(lái)自HDLBits

重置和錯(cuò)誤01111111...:

65a14da2-b249-11ed-bfe3-dac502259ad0.png

圖片來(lái)自HDLBits

實(shí)現(xiàn)這個(gè)狀態(tài)機(jī)。

模塊端口聲明

moduletop_module(
inputclk,
inputreset,//Synchronousreset
inputin,
outputdisc,
outputflag,
outputerr);

8題目解析

1、請(qǐng)使用10個(gè)狀態(tài)以?xún)?nèi)的摩爾機(jī)。

2、狀態(tài)圖:

65c77c34-b249-11ed-bfe3-dac502259ad0.png

moduletop_module(
inputlogicclk,
inputlogicreset,//Synchronousreset
inputlogicin,
outputlogicdisc,
outputlogicflag,
outputlogicerr
);

//definestate
typedefenumlogic[2:0]{detect_0=3'd0,receive_1=3'd1,
receive_2=3'd2,receive_3=3'd3,
receive_4=3'd4,receive_5=3'd5,
receive_6=3'd6,receive_7=3'd7
}state_def;

state_defcur_state,next_state;

//describestatetransitionlogicusecombinationallogic
always_combbegin
case(cur_state)
detect_0:begin
next_state=in?receive_1:detect_0;
end

receive_1:begin
next_state=in?receive_2:detect_0;
end

receive_2:begin
next_state=in?receive_3:detect_0;
end

receive_3:begin
next_state=in?receive_4:detect_0;
end

receive_4:begin
next_state=in?receive_5:detect_0;
end

receive_5:begin
next_state=in?receive_6:detect_0;
end

receive_6:begin
next_state=in?receive_7:detect_0;
end

receive_7:begin
next_state=in?receive_7:detect_0;
end

default:begin
next_state=detect_0;
end
endcase
end

//describestatesequecerusesequentiallogic

always_ff@(posedgeclk)begin
if(reset)begin
cur_state<=?detect_0?;
????????end
????????else?begin
????????????cur_state?<=?next_state?;
????????end
????end

????//describe?output?decoder?use?sequential?and?combinational?logic

????always_ff?@(?posedge?clk?)?begin?
????????if?(reset)?begin
????????????disc?<=?1'd0?;
????????????flag?<=?1'd0?;
????????end
????????else?begin
????????????case?(1'd1)
????????????????(cur_state?==?receive_5)?&&?(next_state?==?detect_0):?disc?<=?1'd1?;
????????????????(cur_state?==?receive_6)?&&?(next_state?==?detect_0):?flag?<=?1'd1?;
????????????????default?:?begin
????????????????????disc?<=?1'd0?;
????????????????????flag?<=?1'd0?;
????????????????end
????????????endcase
????????end
????end

????assign?err??=?(cur_state?==?receive_7)?;

endmodule

65ec9dde-b249-11ed-bfe3-dac502259ad0.png

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:

660a8d9e-b249-11ed-bfe3-dac502259ad0.png

注意圖中無(wú)波形。

這一題就結(jié)束了。

Part6Problem 138-ece241_2013_q8

9題目說(shuō)明

設(shè)計(jì)一個(gè)單輸入單輸出串行 2 的互補(bǔ)摩爾狀態(tài)機(jī)。輸入 (x) 是一系列位(每個(gè)時(shí)鐘周期一個(gè)),從數(shù)字的最低有效位開(kāi)始,輸出 (Z) 是輸入的 2 的補(bǔ)碼。機(jī)器將接受任意長(zhǎng)度的輸入數(shù)字。該電路需要異步復(fù)位。轉(zhuǎn)換在Reset釋放時(shí)開(kāi)始,在Reset置位時(shí)停止。

例如:

6652bcae-b249-11ed-bfe3-dac502259ad0.png

模塊端口聲明

moduletop_module(
inputclk,
inputareset,
inputx,
outputz
);

10題目解析

米里型的輸出由當(dāng)前狀態(tài)和輸入信號(hào)的組合邏輯實(shí)現(xiàn),輸出信號(hào)與輸入信號(hào)同步。

而摩爾型狀態(tài)機(jī)的輸出僅由當(dāng)前狀態(tài)決定,與輸入信號(hào)異步,往往存在延遲。

moduletop_module(
inputlogicclk,
inputlogicaresetn,//Asynchronousactive-lowreset
inputlogicx,
outputlogicz);

//definestate
typedefenumlogic[1:0]{idle=2'd0,state_1=2'd1,state_2=2'd2}state_def;

state_defcur_state,next_state;

//describestatesequecerusesequentiallogic
always_ff@(posedgeclkornegedgearesetn)begin
if(!aresetn)begin
cur_state<=?idle?;
????????end
????????else?begin
????????????cur_state?<=?next_state?;
????????end
????end

????//describe?state?transition?logic?use?combinational?logic

????always_comb?begin?
????????case?(cur_state)
????????????idle:?begin
????????????????next_state?=?x???state_1?:?idle?;
????????????end?

????????????state_1:?begin
????????????????next_state?=?x???state_1?:?state_2?;
????????????end

????????????state_2:?begin
????????????????next_state?=?x???state_1?:?idle?;
????????????end
????????????default:?begin
????????????????next_state?=?idle?;
????????????end
????????endcase
????end

????//describe?output?decoder?use?combinational?logic
????assign?z?=?(cur_state?==?state_2)?&&?(x?==?1'd1)?;
????
endmodule

666e822c-b249-11ed-bfe3-dac502259ad0.png

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:

6698b07e-b249-11ed-bfe3-dac502259ad0.png

注意圖中的Ref是參考波形,Yours是你的代碼生成的波形,網(wǎng)站會(huì)對(duì)比這兩個(gè)波形,一旦這兩者不匹配,仿真結(jié)果會(huì)變紅。

這一題就結(jié)束了。

Part7Problem 139-ece241_2014_q5a

11題目說(shuō)明

設(shè)計(jì)一個(gè)單輸入單輸出串行 2 的互補(bǔ)摩爾狀態(tài)機(jī)。輸入 (x) 是一系列位(每個(gè)時(shí)鐘周期一個(gè)),從數(shù)字的最低有效位開(kāi)始,輸出 (Z) 是輸入的 2 的補(bǔ)碼。機(jī)器將接受任意長(zhǎng)度的輸入數(shù)字。該電路需要異步復(fù)位。轉(zhuǎn)換在Reset釋放時(shí)開(kāi)始,在Reset置位時(shí)停止。

例如:

6652bcae-b249-11ed-bfe3-dac502259ad0.png

圖片來(lái)自HDLBits

模塊端口聲明

moduletop_module(
inputclk,
inputareset,
inputx,
outputz
);

12題目解析

moduletop_module(
inputlogicclk,
inputlogicareset,
inputlogicx,
outputlogicz
);

//definestate

typedefenumlogic[1:0]{S0=2'd0,S1=2'd1,S2=2'd2}state_def;

state_defcur_state,next_state;

//describestatetransitionusecombinationallogic

always_combbegin
case(cur_state)
S0:begin
next_state=x?S1:S0;
end

S1:begin
next_state=x?S2:S1;
end

S2:begin
next_state=x?S2:S1;
end
default:begin
next_state=S0;
end
endcase
end

//describestatesequencerusesequentiallogic

always_ff@(posedgeclkorposedgeareset)begin
if(areset)begin
cur_state<=?S0?;
????end
????else?begin
????????cur_state?<=?next_state?;
????end
end


//describe?output?decoder?use?combinational?logic

assign?z?=?(cur_state?==?S1)?;
?
endmodule

66d718be-b249-11ed-bfe3-dac502259ad0.png

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:

66f8412e-b249-11ed-bfe3-dac502259ad0.png

注意圖中的Ref是參考波形,Yours是你的代碼生成的波形,網(wǎng)站會(huì)對(duì)比這兩個(gè)波形,一旦這兩者不匹配,仿真結(jié)果會(huì)變紅。

這一題就結(jié)束了。

Part8Problem 140-23_ece241_2014_q5b

13題目說(shuō)明

本題和上一題 Serial two's complementer (Moore FSM) 一樣,使用狀態(tài)機(jī)實(shí)現(xiàn)一個(gè)二進(jìn)制補(bǔ)碼生成器,不同的是此題使用米里型狀態(tài)機(jī)實(shí)現(xiàn)。

6762541a-b249-11ed-bfe3-dac502259ad0.png

圖片來(lái)自HDLBits

模塊端口聲明

moduletop_module(
inputclk,
inputareset,
inputx,
outputz
);

14題目解析

存在兩個(gè)狀態(tài),復(fù)位狀態(tài) A,在輸入 x 為 1 后狀態(tài)轉(zhuǎn)移為 B,并保持在狀態(tài) B。

狀態(tài) A 中輸出 z 與輸入 x 相同;狀態(tài) B 中輸出 z 與輸入 x 相反。

moduletop_module(
inputlogicclk,
inputlogicareset,
inputlogicx,
outputlogicz
);

//definestate

typedefenumlogic{S0=1'd0,S1=1'd1}state_def;

state_defcur_state,next_state;

//describestatetransitionusecombinationallogic

always_combbegin
case(cur_state)
S0:begin
next_state=x?S1:S0;
end

S1:begin
next_state=S1;
end

default:begin
next_state=S0;
end
endcase
end

//describestatesequencerusesequentiallogic

always_ff@(posedgeclkorposedgeareset)begin
if(areset)begin
cur_state<=?S0?;
????end
????else?begin
????????cur_state?<=?next_state?;
????end
end


//describe?output?decoder?use?combinational?logic

assign?z?=?((cur_state?==?S0)?&&?(x?==?1'd1))?||?((cur_state?==?S1?)?&&?(x?==?1'd0));
?
endmodule

678b5efa-b249-11ed-bfe3-dac502259ad0.png

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:

67e4e484-b249-11ed-bfe3-dac502259ad0.png

注意圖中的Ref是參考波形,Yours是你的代碼生成的波形,網(wǎng)站會(huì)對(duì)比這兩個(gè)波形,一旦這兩者不匹配,仿真結(jié)果會(huì)變紅。

這一題就結(jié)束了。

Part9Problem 141-2014_q3fsm

15題目說(shuō)明

考慮具有輸入s和w的有限狀態(tài)機(jī)。假設(shè) FSM 開(kāi)始于稱(chēng)為A的重置狀態(tài),如下所示。只要s = 0, FSM 就保持在狀態(tài)A ,當(dāng)s = 1 時(shí),它會(huì)移動(dòng)到狀態(tài)B。一旦進(jìn)入狀態(tài)B,F(xiàn)SM在接下來(lái)的三個(gè)時(shí)鐘周期內(nèi)檢查輸入w的值。如果在這些時(shí)鐘周期中恰好有兩個(gè)時(shí)鐘周期內(nèi)w = 1,則 FSM 必須 在下一個(gè)時(shí)鐘周期內(nèi)將輸出z設(shè)置為 1。否則z必須為 0。FSM 繼續(xù)檢查w對(duì)于接下來(lái)的三個(gè)時(shí)鐘周期,依此類(lèi)推。下面的時(shí)序圖說(shuō)明了不同w值所需的z值。

使用盡可能少的狀態(tài)。請(qǐng)注意,s輸入僅在狀態(tài)A中使用,因此只需考慮w輸入。

6829ad08-b249-11ed-bfe3-dac502259ad0.png

圖片來(lái)自HDLBits

模塊端口聲明

moduletop_module(
inputclk,
inputreset,//Synchronousreset
inputs,
inputw,
outputz
);

16題目解析

值得注意的是:需要三個(gè)周期中 exactly 兩個(gè)周期為 1 。

moduletop_module(
inputlogicclk,
inputlogicreset,//Synchronousreset
inputlogics,
inputlogicw,
outputlogicz
);

//definestate

typedefenumlogic{A=1'd0,B=1'd1}state_def;

state_defcur_state,next_state;

//describestatetransitionusecombinationallogic

always_combbegin
case(cur_state)
A:begin
next_state=s?B:A;
end

B:begin
next_state=B;
end

default:begin
next_state=A;
end
endcase
end

//describestatesequencerusesequentiallogic

always_ff@(posedgeclk)begin
if(reset)begin
cur_state<=?A?;
????end
????else?begin
????????cur_state?<=?next_state?;
????end
end

//define?counter?use?sequential?and?combinational?logic

var?logic?[1:0]?count?,?count_c;

wire?logic?resetn?;

assign?resetn?=?(count_c?==?2'd3)???1'd0?:?1'd1?;

always_ff?@(?posedge?clk?)?begin?
????if(reset)?begin
????????count?<=?2'd0?;
????end
????else?if?(!resetn)?begin
????????????if(w?==?1'd1)?begin
?????????????count?<=?2'd1?;
????????????end
????????????else?begin
????????????count?<=?2'd0?;
????????????end
????end
????else?begin
????????if?(cur_state?==?B?&&?w?==?1'd1)?begin
????????????count?<=?count?+?2'd1?;
????????end
????????else?begin
????????????count?<=?count?;
????????end
????????
????end
end

always_ff?@(?posedge?clk?)?begin?
????if?(reset)?begin
????????count_c?<=?2'd0?;
????end
????else?begin
????????if?(cur_state?==?B?&&?count_c?==?2'd3)?begin
????????count_c?<=?2'd1?;
????????end
????????else?if?(cur_state?==?B)?begin
????????????count_c?<=?count_c?+?2'd1?;
????????end
????????else?begin
????????count_c?<=?count_c?;
????????end
????end
????
end


//describe?output?decoder?use?combinational?logic

always_ff@(posedge?clk)?begin?
????if(reset)?begin
????????z?<=?1'd0?;
????end
????else?begin
??????case(?1'd1?)
??????(cur_state?==?B?&&?count_c?==?2'd2?&&?count?==?1?&&?w?==?1)?:?begin
????????z?<=?1'd1?;
??????end
??????(cur_state?==?B?&&?count_c?==?2'd2?&&?count?==?2?&&?w?==?0)?:?begin
????????z?<=?1'd1?;
??????end
??????default?:?begin
????????z?<=?1'd0?;
??????end
??????endcase
????end
end

endmodule

68513d28-b249-11ed-bfe3-dac502259ad0.png

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:

686ddffa-b249-11ed-bfe3-dac502259ad0.png

注意圖中無(wú)波形。

這一題就結(jié)束了。

Part10Problem 142-2014_q3bfsm

17題目說(shuō)明

這是一道簡(jiǎn)單的根據(jù)狀態(tài)轉(zhuǎn)移實(shí)現(xiàn)狀態(tài)機(jī)的題目,實(shí)現(xiàn)完整的三段式狀態(tài)機(jī)

688c1de4-b249-11ed-bfe3-dac502259ad0.png

圖片來(lái)自HDLBits

模塊端口聲明

moduletop_module(
inputclk,
inputreset,//Synchronousreset
inputx,
outputz
);

18題目解析

moduletop_module(
inputlogicclk,
inputlogicreset,//Synchronousreset
inputlogicx,
outputlogicz
);

//definestate

typedefenumlogic[2:0]{S0=3'b000,S1=3'b001,
S2=3'b010,S3=3'b011,
S4=3'b100
}state_def;

state_defcur_state,next_state;

//describestatetransitionusecombinationallogic

always_combbegin
case(cur_state)
S0:begin
next_state=x?S1:S0;
end

S1:begin
next_state=x?S4:S1;
end

S2:begin
next_state=x?S1:S2;
end

S3:begin
next_state=x?S2:S1;
end

S4:begin
next_state=x?S4:S3;
end

default:begin
next_state=S0;
end
endcase
end

//describestatesequencerusesequentiallogic

always_ff@(posedgeclk)begin
if(reset)begin
cur_state<=?S0?;
????end
????else?begin
????????cur_state?<=?next_state?;
????end
end


//describe?output?decoder?use?combinational?logic

assign?z?=?(cur_state?==?S3)?||?(cur_state?==?S4)?;

endmodule


68b21d82-b249-11ed-bfe3-dac502259ad0.png

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:

68daecbc-b249-11ed-bfe3-dac502259ad0.png

注意圖中無(wú)參考波形。

這一題就結(jié)束了。

Part11總結(jié)

今天的幾道題就結(jié)束了,對(duì)于狀態(tài)機(jī)的理解還是有益處的,三段式狀態(tài)機(jī)是題目一直推崇的,類(lèi)似狀態(tài)機(jī)的公示,可以“套”進(jìn)去。





審核編輯:劉清

聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀(guān)點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 通信協(xié)議
    +關(guān)注

    關(guān)注

    28

    文章

    1035

    瀏覽量

    41174
  • 接收器
    +關(guān)注

    關(guān)注

    15

    文章

    2571

    瀏覽量

    73943
  • 數(shù)字電路
    +關(guān)注

    關(guān)注

    193

    文章

    1639

    瀏覽量

    81921
  • 狀態(tài)機(jī)
    +關(guān)注

    關(guān)注

    2

    文章

    493

    瀏覽量

    28249

原文標(biāo)題:HDLBits: 在線(xiàn)學(xué)習(xí) SystemVerilog(二十)-Problem 134-142(狀態(tài)機(jī)三)

文章出處:【微信號(hào):Open_FPGA,微信公眾號(hào):OpenFPGA】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    FPGA工程師:如何在FPGA實(shí)現(xiàn)狀態(tài)機(jī)?

    安全高效狀態(tài)機(jī)設(shè)計(jì)對(duì)于任何使用FPGA的工程師而言都是一項(xiàng)重要技能。選擇Moore狀態(tài)機(jī)、Mealy
    發(fā)表于 03-29 15:02 ?1.4w次閱讀
    FPGA工程師:如何在FPGA<b class='flag-5'>中</b>實(shí)現(xiàn)<b class='flag-5'>狀態(tài)機(jī)</b>?

    FPGA---如何寫(xiě)好狀態(tài)機(jī),詳細(xì)下載pdf

    的基礎(chǔ)上,重點(diǎn)討論如何寫(xiě)好狀態(tài)機(jī)。由于篇幅比較長(zhǎng),如何寫(xiě)好狀態(tài)機(jī)分成三篇呈現(xiàn)。話(huà)不多說(shuō),上貨。狀態(tài)機(jī)是一種思想方法相信大多數(shù)工科學(xué)生在學(xué)習(xí)數(shù)字電路時(shí)都學(xué)習(xí)過(guò)
    發(fā)表于 09-28 10:29

    狀態(tài)機(jī)高效寫(xiě)法

    狀態(tài)機(jī)高效寫(xiě)法
    發(fā)表于 01-21 06:41

    如何寫(xiě)好狀態(tài)機(jī)

    如何寫(xiě)好狀態(tài)機(jī):狀態(tài)機(jī)是邏輯設(shè)計(jì)的重要內(nèi)容,狀態(tài)機(jī)的設(shè)計(jì)水平直接反應(yīng)工程師的邏輯功底,所以許多公司的硬件和邏輯工程師面試,
    發(fā)表于 06-14 19:24 ?97次下載

    狀態(tài)機(jī)思路在單片機(jī)程序設(shè)計(jì)的應(yīng)用

    狀態(tài)機(jī)思路在單片機(jī)程序設(shè)計(jì)的應(yīng)用 狀態(tài)機(jī)的概念狀態(tài)機(jī)是軟件編程的一個(gè)
    發(fā)表于 02-09 11:25 ?1w次閱讀
    <b class='flag-5'>狀態(tài)機(jī)</b>思路在單片<b class='flag-5'>機(jī)</b>程序設(shè)計(jì)<b class='flag-5'>中</b>的應(yīng)用

    狀態(tài)機(jī)思路在單片機(jī)程序設(shè)計(jì)的應(yīng)用

    狀態(tài)機(jī)思路在單片機(jī)程序設(shè)計(jì)的應(yīng)用 狀態(tài)機(jī)的概念       狀態(tài)機(jī)是軟件編程
    發(fā)表于 03-18 15:00 ?1437次閱讀
    <b class='flag-5'>狀態(tài)機(jī)</b>思路在單片<b class='flag-5'>機(jī)</b>程序設(shè)計(jì)<b class='flag-5'>中</b>的應(yīng)用

    基于RTL綜合策略的狀態(tài)機(jī)優(yōu)化方案

    有限狀態(tài)機(jī)及其設(shè)計(jì)技術(shù)是數(shù)字系統(tǒng)設(shè)計(jì)重要組成部分,是實(shí)現(xiàn)高效率、高可靠性邏輯控制的重要途徑。
    發(fā)表于 01-05 10:34 ?2631次閱讀
    基于RTL綜合策略的<b class='flag-5'>狀態(tài)機(jī)</b>優(yōu)化方案

    狀態(tài)機(jī)原理及用法

    狀態(tài)機(jī)原理及用法狀態(tài)機(jī)原理及用法狀態(tài)機(jī)原理及用法
    發(fā)表于 03-15 15:25 ?0次下載

    狀態(tài)機(jī)概述 如何理解狀態(tài)機(jī)

    本篇文章包括狀態(tài)機(jī)的基本概述以及通過(guò)簡(jiǎn)單的實(shí)例理解狀態(tài)機(jī)
    的頭像 發(fā)表于 01-02 18:03 ?1.1w次閱讀
    <b class='flag-5'>狀態(tài)機(jī)</b>概述  如何理解<b class='flag-5'>狀態(tài)機(jī)</b>

    什么是狀態(tài)機(jī) 狀態(tài)機(jī)的描述三種方法

    狀態(tài)機(jī) 1、狀態(tài)機(jī)是許多數(shù)字系統(tǒng)的核心部件,是一類(lèi)重要的時(shí)序邏輯電路。通常包括三個(gè)部分:一是下一個(gè)狀態(tài)
    的頭像 發(fā)表于 11-16 17:39 ?2.7w次閱讀

    FPGA:狀態(tài)機(jī)簡(jiǎn)述

    是FPGA設(shè)計(jì)中一種非常重要、非常根基的設(shè)計(jì)思想,堪稱(chēng)FPGA的靈魂,貫穿FPGA設(shè)計(jì)的始終。 02. 狀態(tài)機(jī)簡(jiǎn)介 什么是狀態(tài)機(jī)狀態(tài)機(jī)通過(guò)不同的
    的頭像 發(fā)表于 11-05 17:58 ?8053次閱讀
    FPGA:<b class='flag-5'>狀態(tài)機(jī)</b>簡(jiǎn)述

    如何合理高效地使用狀態(tài)機(jī)呢?

    今天還是更新狀態(tài)機(jī),狀態(tài)機(jī)基本是整個(gè)HDL的核心,合理、高效地使用狀態(tài)機(jī),是
    的頭像 發(fā)表于 02-12 10:21 ?1174次閱讀

    基于FPGA的狀態(tài)機(jī)設(shè)計(jì)

    狀態(tài)機(jī)的基礎(chǔ)知識(shí)依然強(qiáng)烈推薦mooc上華科的數(shù)字電路與邏輯設(shè)計(jì),yyds!但是數(shù)電基礎(chǔ)一定要和實(shí)際應(yīng)用結(jié)合起來(lái),理論才能發(fā)揮真正的價(jià)值。我們知道FPGA是并行執(zhí)行的,如果我們想要處理具有前后順序的事件就需要引入狀態(tài)機(jī)。
    的頭像 發(fā)表于 07-28 10:02 ?1397次閱讀
    基于FPGA的<b class='flag-5'>狀態(tài)機(jī)</b>設(shè)計(jì)

    什么是狀態(tài)機(jī)?狀態(tài)機(jī)的種類(lèi)與實(shí)現(xiàn)

    狀態(tài)機(jī),又稱(chēng)有限狀態(tài)機(jī)(Finite State Machine,F(xiàn)SM)或米利狀態(tài)機(jī)(Mealy Machine),是一種描述系統(tǒng)狀態(tài)變化的模型。在芯片設(shè)計(jì)
    的頭像 發(fā)表于 10-19 10:27 ?1.2w次閱讀

    觸發(fā)器和狀態(tài)機(jī)的關(guān)系是什么

    觸發(fā)器和狀態(tài)機(jī)數(shù)字電路設(shè)計(jì)中有著緊密的關(guān)系,它們共同構(gòu)成了時(shí)序邏輯電路的基礎(chǔ),用于實(shí)現(xiàn)數(shù)據(jù)的存儲(chǔ)、處理和傳輸。
    的頭像 發(fā)表于 08-12 11:24 ?945次閱讀