Problem 151-review2015_count1k
題目說明
構(gòu)建一個(gè)從 0 到 999(含)計(jì)數(shù)的計(jì)數(shù)器,周期為 1000 個(gè)周期。復(fù)位輸入是同步的,應(yīng)該將計(jì)數(shù)器復(fù)位為 0。
模塊端口聲明
moduletop_module( inputclk, inputreset, output[9:0]q);
題目解析
moduletop_module( inputlogicclk, inputlogicreset, outputlogic[9:0]q); always_ff@(posedgeclk)begin if(reset)begin q<=?10'd0?; ????end ????else?if?(q?==?10'd999)?begin ????????q?<=?10'd0?; ????end ????else?begin ????????q?<=?q?+?10'd1?; ????end ???? end endmodule

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:
注意圖中的Ref是參考波形,Yours是你的代碼生成的波形,網(wǎng)站會(huì)對(duì)比這兩個(gè)波形,一旦這兩者不匹配,仿真結(jié)果會(huì)變紅。
這一題就結(jié)束了。
Problem 152-review2015_shiftcount
題目說明
接下來的五道題目,每題構(gòu)建一個(gè)小型的電路,最終組裝成為一個(gè)復(fù)雜的計(jì)數(shù)器電路。
本題設(shè)計(jì)一個(gè) 4bit 位寬的移位寄存器,并具有計(jì)數(shù)器功能,可向下計(jì)數(shù)(即計(jì)數(shù)值遞減)。
當(dāng)移位使能 shift_ena 信號(hào)有效時(shí),數(shù)據(jù) data 移入移位寄存器,向高位移動(dòng)(most-significant-bit),即左移。
當(dāng)計(jì)數(shù)使能 count_ena 信號(hào)有效時(shí),寄存器中現(xiàn)有的數(shù)值遞減,向下計(jì)數(shù)。
由于系統(tǒng)不會(huì)同時(shí)使用移位以及計(jì)數(shù)功能,因此不需要考慮兩者使能信號(hào)皆有效的情況。
模塊端口聲明
moduletop_module( inputclk, inputshift_ena, inputcount_ena, inputdata, output[3:0]q);
題目解析
moduletop_module( inputlogicclk, inputlogicshift_ena, inputlogiccount_ena, inputlogicdata, outputlogic[3:0]q); always_ff@(posedgeclk)begin case(1'b1) shift_ena:begin q<=?{q[2:0]?,?data}?;? ????????end? ????????count_ena:?begin ????????????q?<=?q?-?4'd1?; ????????end ????????default:?begin ????????????q?<=?q?; ????????end? ????endcase end endmodule

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:
注意圖中的Ref是參考波形,Yours是你的代碼生成的波形,網(wǎng)站會(huì)對(duì)比這兩個(gè)波形,一旦這兩者不匹配,仿真結(jié)果會(huì)變紅。
這一題就結(jié)束了。
Problem 153-review2015_fsmseq
題目說明
接下來的五道題目,每題構(gòu)建一個(gè)小型的電路,最終組裝成為一個(gè)復(fù)雜的計(jì)數(shù)器電路。
構(gòu)建一個(gè)在輸入比特流中搜索序列 1101 的有限狀態(tài)機(jī)。當(dāng)找到序列時(shí),應(yīng)該將start_shifting設(shè)置為 1,直到復(fù)位??ㄔ谧罱K狀態(tài)旨在模擬在尚未實(shí)現(xiàn)的更大 FSM 中進(jìn)入其他狀態(tài)。我們將在接下來的幾個(gè)練習(xí)中擴(kuò)展這個(gè) FSM。
模塊端口聲明
moduletop_module( inputclk, inputreset,//Synchronousreset inputdata, outputstart_shifting);
題目解析
檢查一個(gè)序列檢測狀態(tài)機(jī)是否完備,一個(gè)簡單的方法是觀察所有狀態(tài),是否均包括輸入分別為 0/1 的情況下的跳轉(zhuǎn),比如 state[IDLE ] && ~data 和 state[IDLE ] && data 是否均存在于狀態(tài)跳轉(zhuǎn)條件中。
moduletop_module( inputlogicclk, inputlogicreset,//Synchronousreset inputlogicdata, outputlogicstart_shifting ); //definestate typedefenumlogic[2:0]{idle=3'd1,S0=3'd2, S1=3'd3,S2=3'd4,S3=3'd5 }state_def; state_defcur_state,next_state; //describestatetransitionusecombinationallogic always_combbegin case(cur_state) idle:begin next_state=data?S0:idle; end S0:begin next_state=data?S1:idle; end S1:begin next_state=data?S1:S2; end S2:begin next_state=data?S3:idle; end S3:begin next_state=S3; end default:begin next_state=idle; end endcase end //describestatesequencerusesequentiallogic 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?start_shifting?=?(cur_state?==?S3)?; endmodule

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:
注意圖中的Ref是參考波形,Yours是你的代碼生成的波形,網(wǎng)站會(huì)對(duì)比這兩個(gè)波形,一旦這兩者不匹配,仿真結(jié)果會(huì)變紅。
這一題就結(jié)束了。
Problem 154-review2015_fsmshift
題目說明
接下來的五道題目,每題構(gòu)建一個(gè)小型的電路,最終組裝成為一個(gè)復(fù)雜的計(jì)數(shù)器電路。
作為用于控制移位寄存器的 FSM 的一部分,我們希望能夠在檢測到正確的位模式時(shí)啟用移位寄存器正好 4 個(gè)時(shí)鐘周期。我們在Exams/review2015_fsmseq中處理序列檢測,因此 FSM 的這一部分僅處理啟用移位寄存器 4 個(gè)周期。
每當(dāng) FSM 復(fù)位時(shí),將shift_ena斷言4 個(gè)周期,然后永遠(yuǎn)為 0(直到復(fù)位)。
圖片來自HDLBits
模塊端口聲明
moduletop_module( inputclk, inputreset,//Synchronousreset outputshift_ena);
題目解析
題目要求 reset 信號(hào)移除后,立即 輸出使能信號(hào),因此需要組合邏輯輸出。
moduletop_module( inputlogicclk, inputlogicreset,//Synchronousreset outputlogicshift_ena); //definestate typedefenumlogic{S0=1'd0,S1=1'd1}state_def; state_defcur_state,next_state; //describestatesequencerusesequentiallogic always_ff@(posedgeclk)begin if(reset)begin cur_state<=?S0?; ????end ????else?begin ????????cur_state?<=?next_state?; ????end end //describe?state?transition?use?combinational?logic always_comb?begin? ????case?(cur_state) ????????S0:?begin ????????????next_state?=?reset???S0?:?S1?; ????????end ????????S1:?begin ????????????next_state?=?reset???S0?:?S1?; ????????end ????????default:?begin ????????????next_state?=?cur_state?; ????????end ????endcase end //define?counter?use?sequential?logic var?logic?[6:0]?counter?; always_ff?@(?posedge?clk?)?begin? ????if?(reset)?begin ????????counter?<=?7'd0?; ????end ????else?if?(next_state?==?S1)?begin ????????counter?<=?counter?+?7'd1?; ????end ????else?begin ????????counter?<=?counter?; ????end end //describe?output?decoder?use?sequential?logic assign?shift_ena?=?(cur_state?==?S0)?||?(cur_state?==?S1?&&?counter?7'd4)?; endmodule

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:
注意圖中無波形。
這一題就結(jié)束了。
Problem 155-review2015_fsm
題目說明
本題實(shí)現(xiàn)復(fù)雜計(jì)數(shù)器的第四個(gè)組件。
在此題之前,我們已經(jīng)分別實(shí)現(xiàn)了 FSM:Enable shift register 以及 FSM:1101 序列檢測器。接下來我們繼續(xù)前進(jìn),實(shí)現(xiàn)這個(gè)復(fù)雜計(jì)數(shù)器的完整 FSM。
復(fù)雜計(jì)數(shù)器需要如下這些功能特性:
在數(shù)據(jù)流中檢測到特定序列后啟動(dòng)計(jì)數(shù)器,該序列為: 1101將 4bits 數(shù)據(jù)輸入移位寄存器,作為計(jì)數(shù)器的初值等待計(jì)數(shù)器結(jié)束計(jì)數(shù)告知上層應(yīng)用計(jì)數(shù)完成,并等待用戶通過 ack 信號(hào)確認(rèn)在本題練習(xí)中,只需要實(shí)現(xiàn)控制狀態(tài)機(jī),不需要實(shí)現(xiàn)數(shù)據(jù)通路,比如計(jì)數(shù)器本身以及數(shù)據(jù)比較器等。
數(shù)據(jù)流從模塊的 data 信號(hào)輸入,當(dāng)檢測到 1101 序列后,狀態(tài)機(jī)需要置高輸出信號(hào) shft_ena 并保持 4 個(gè)周期(用于將接下來 4bit 數(shù)據(jù)輸入移位寄存器)。
之后,狀態(tài)機(jī)置高 counting 信號(hào),表示其正在等待計(jì)數(shù)器完成計(jì)數(shù),當(dāng)計(jì)數(shù)器完成計(jì)數(shù)輸出 done_counting 信號(hào)后,counting 信號(hào)置低。
再此后,狀態(tài)機(jī)置高 done 信號(hào)通知上層應(yīng)用計(jì)數(shù)器計(jì)數(shù)完成,等待 ack 信號(hào)置高后,狀態(tài)機(jī)清除 done 信號(hào),返回空閑狀態(tài)等待捕獲下一個(gè) 1101 序列。
本題給出了一個(gè)期望輸入輸出的例子。圖中的斜線代表當(dāng)前信號(hào)為 'X', 表示狀態(tài)機(jī)不關(guān)心該信號(hào)當(dāng)前的值。比如圖例中,一旦 FSM 檢測到 1101 序列后,在此次計(jì)數(shù)器事件完成前,對(duì)于當(dāng)前的數(shù)據(jù)流不再關(guān)心。
模塊端口聲明
moduletop_module( inputclk, inputreset,//Synchronousreset inputdata, outputshift_ena, outputcounting, inputdone_counting, outputdone, inputack);
題目解析
狀態(tài)轉(zhuǎn)移圖
moduletop_module( inputlogicclk, inputlogicreset,//Synchronousreset inputlogicdata, outputlogicshift_ena, outputlogiccounting, inputlogicdone_counting, outputlogicdone, inputlogicack ); //definestate typedefenumlogic[2:0]{idle=3'd1,S0=3'd2,S1=3'd3, S2=3'd4,shif=3'd5,count=3'd6, waite=3'd7 }state_def; state_defcur_state,next_state; //describestatetransitionusecombinationallogic always_combbegin case(cur_state) idle:begin next_state=data?S0:idle; end S0:begin next_state=data?S1:idle; end S1:begin next_state=data?S1:S2; end S2:begin next_state=data?shif:idle; end shif:begin next_state=counter7'd4???shif?:?count?; ????????end ????????count:?begin ????????????next_state?=?done_counting???waite?:?count?; ????????end ????????waite:?begin ????????????next_state?=?ack???idle?:?waite?; ????????end ????????default:?begin ????????????next_state?=?idle?; ????????end ????endcase end //define?counter?use?sequential?logic var?logic?[6:0]?counter?; always_ff?@(?posedge?clk?)?begin? ????if?(reset?||?next_state?!=?shif)?begin ????????counter?<=?7'd0?; ????end ????else?if?(next_state?==?shif)?begin ????????counter?<=?counter?+?7'd1?; ????end ????else?begin ????????counter?<=?counter?; ????end end //describe?state?sequencer?use?sequential?logic always_ff?@(?posedge?clk?)?begin? ????if?(reset)?begin ????????cur_state?<=?idle?; ????end ????else?begin ????????cur_state?<=?next_state?; ????end???? end //describe?output?decoder?use?combinational?logic assign?shift_ena?=?(cur_state?==?shif)?; assign?counting??=?(cur_state?==?count)?; assign?done?=?(cur_state?==?waite)?; endmodule

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:
注意圖中無參考波形。
這一題就結(jié)束了。
Problem 156-review2015_fancytimer
題目說明
終于到了完整構(gòu)建復(fù)雜計(jì)數(shù)器的時(shí)候,整體功能已經(jīng)在上題中討論,這里不再贅述。
在數(shù)據(jù)流中檢測到序列 1101 后,電路需要將接下來的 4bit 數(shù)據(jù)移入移位寄存器。4bit 數(shù)據(jù)決定了計(jì)數(shù)器的計(jì)數(shù)周期,稱為 delay[3:0]。首先到達(dá)的比特作為數(shù)據(jù)的高位。
之后,狀態(tài)機(jī)置高 counting 信號(hào),表示其正在等待計(jì)數(shù)器完成計(jì)數(shù)。在 FSM 中增加計(jì)數(shù)器狀態(tài),計(jì)數(shù)周期為 (delay[3:0] + 1 )* 1000 個(gè)時(shí)鐘周期。比如 delay = 0 時(shí),計(jì)數(shù)值為 1000 個(gè)周期。delay = 5 代表 6000 個(gè)周期。同時(shí)輸出 count 當(dāng)前剩余的計(jì)數(shù)周期,輸出當(dāng)前剩余計(jì)數(shù)周期的千位(比如,還剩1000個(gè)周期輸出 1,還剩 999 個(gè)周期時(shí)輸出 0)。當(dāng)計(jì)數(shù)停止后,count 的輸出可以為任意數(shù)。
當(dāng)計(jì)數(shù)完成后,電路置高 done 信號(hào)通知上層應(yīng)用計(jì)數(shù)器計(jì)數(shù)完成,等待 ack 信號(hào)置高后,狀態(tài)機(jī)清除 done 信號(hào),返回空閑狀態(tài)等待捕獲下一個(gè) 1101 序列。
本題給出了一個(gè)期望輸入輸出的例子。圖中的斜線代表當(dāng)前信號(hào)為 'X', 表示狀態(tài)機(jī)不關(guān)心該信號(hào)當(dāng)前的值。比如圖例中,一旦 FSM 檢測到 1101 序列并讀取 delay[3:0] 后,在此次計(jì)數(shù)器事件完成前,對(duì)于當(dāng)前的數(shù)據(jù)流不再關(guān)心。
在圖例中,電路計(jì)數(shù)周期為 2000 ,因?yàn)?delay[3:0] 數(shù)值為 4'b0001 。在后續(xù)的第二個(gè)計(jì)數(shù)周期中,因?yàn)?delay[3:0] = 4‘b1110,所以計(jì)數(shù)周期為 15000。
圖片來自HDLBits
模塊端口聲明
moduletop_module( inputclk, inputreset,//Synchronousreset inputdata, output[3:0]count, outputcounting, outputdone, inputack);
題目解析
moduletop_module( inputlogicclk, inputlogicreset,//Synchronousreset inputlogicdata, outputlogic[3:0]count, outputlogiccounting, outputlogicdone, inputlogicack ); //definestate typedefenumlogic[2:0]{idle=3'd1,S0=3'd2,S1=3'd3, S2=3'd4,shif=3'd5,count_t=3'd6, waite=3'd7 }state_def; state_defcur_state,next_state; //describestatetransitionusecombinationallogic always_combbegin case(cur_state) idle:begin next_state=data?S0:idle; end S0:begin next_state=data?S1:idle; end S1:begin next_state=data?S1:S2; end S2:begin next_state=data?shif:idle; end shif:begin next_state=counter_shif7'd4???shif?:?count_t?; ????????end ????????count_t:?begin ????????????next_state?=?(counter?==?(delay?+?1)*1000?-?1)???waite?:?count_t?; ????????end ????????waite:?begin ????????????next_state?=?ack???idle?:?waite?; ????????end ????????default:?begin ????????????next_state?=?idle?; ????????end ????endcase end //describe?state?sequencer?use?sequential?logic always_ff?@(?posedge?clk?)?begin? ????if?(reset)?begin ????????cur_state?<=?idle?; ????end ????else?begin ????????cur_state?<=?next_state?; ????end???? end //describe?output?decoder?use?combinational?logic assign?count?=?(cur_state?==?count_t)???(delay?-?counter_delay)?:4'd0?; assign?counting??=?(cur_state?==?count_t)?; assign?done?=?(cur_state?==?waite)?; //define?counter?for?shift?use?sequential?logic var?logic?[6:0]?counter_shif?; always_ff?@(?posedge?clk?)?begin? ????if?(reset?||?next_state?!=?shif)?begin ????????counter_shif?<=?7'd0?; ????end ????else?if?(next_state?==?shif)?begin ????????counter_shif?<=?counter_shif?+?7'd1?; ????end ????else?begin ????????counter_shif?<=?counter_shif?; ????end end //define?counter?for?count?use?sequential?logic var?logic?[15:0]?counter?; always_ff?@(?posedge?clk?)?begin? ????if?(reset?||?next_state?!=?count_t)?begin ????????counter?<=?16'd0?; ????end ????else?if?(cur_state?==?count_t)?begin ????????counter?<=?counter?+?16'd1?; ????end ????else?begin ????????counter?<=?16'd0?; ????end end //shift?data?into?delay?use?sequential?logic var?logic?[3:0]?delay?; always_ff?@(?posedge?clk?)?begin? ????if(reset)?begin ????????delay?<=?4'd0?; ????end ????else?begin ????????if?(cur_state?==?shif)?begin ????????????delay?<=?{delay[2:0]?,?data}?; ????????end ????end end //calculate?couter_delay?use?combinational?logic var?logic?[3:0]?counter_delay?; always@(*)begin ????if(counter?<=?999)begin ????????counter_delay?=?4'd0; ????end ????else?if(counter?>=1000&&counter<=?1999)begin ????????counter_delay?=?4'd1; ????end ????else?if(counter?>=2000&&counter<=?2999)begin ????????counter_delay?=?4'd2; ????end ????else?if(counter?>=3000&&counter<=?3999)begin ????????counter_delay?=?4'd3; ????end ????else?if(counter?>=4000&&counter<=?4999)begin ????????counter_delay?=?4'd4; ????end ????else?if(counter?>=5000&&counter<=?5999)begin ????????counter_delay?=?4'd5; ????end ????else?if(counter?>=6000&&counter<=?6999)begin ????????counter_delay?=?4'd6; ????end ????else?if(counter?>=7000&&counter<=?7999)begin ????????counter_delay?=?4'd7; ????end ????else?if(counter?>=8000&&counter<=?8999)begin ????????counter_delay?=?4'd8; ????end ????else?if(counter?>=9000&&counter<=?9999)begin ????????counter_delay?=?4'd9; ????end ????else?if(counter?>=10000&&counter<=?10999)begin ????????counter_delay?=?4'd10; ????end ????else?if(counter?>=11000&&counter<=?11999)begin ????????counter_delay?=?4'd11; ????end ????else?if(counter?>=12000&&counter<=?12999)begin ????????counter_delay?=?4'd12; ????end ????else?if(counter?>=13000&&counter<=?13999)begin ????????counter_delay?=?4'd13; ????end ????else?if(counter?>=14000&&counter<=?14999)begin ????????counter_delay?=?4'd14; ????end ????else?begin ????????counter_delay?=?4'd15; ????end end????? endmodule

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:
注意圖中無波形。
這一題就結(jié)束了。
Problem 157-review2015_fsmonehot
題目說明
給定以下具有 3 個(gè)輸入、3 個(gè)輸出和 10 個(gè)狀態(tài)的狀態(tài)機(jī):
圖片來自HDLBits
假定使用以下one-hot編碼, 通過檢查推導(dǎo)出下一狀態(tài)邏輯方程和輸出邏輯方程: (S, S1, S11, S110, B0, B1, B2, B3, Count, Wait) = (10'b0000000001, 10 'b0000000010, 10'b0000000100, ..., 10'b1000000000)
假設(shè)采用one-hot編碼,通過檢查推導(dǎo)狀態(tài)轉(zhuǎn)換和輸出邏輯方程。僅為該狀態(tài)機(jī)實(shí)現(xiàn)狀態(tài)轉(zhuǎn)換邏輯和輸出邏輯(組合邏輯部分)。(測試臺(tái)將使用非熱輸入進(jìn)行測試,以確保不會(huì)嘗試做更復(fù)雜的事情。請(qǐng)參閱fsm3onehot用于描述單熱狀態(tài)機(jī)“通過檢查”推導(dǎo)邏輯方程的含義。)
編寫生成以下等式的代碼:
B3_next ,B2 狀態(tài)的次態(tài)(原題寫的 B1,應(yīng)該為筆誤)
S_next
S1_next
Count_next
以及下列輸出信號(hào)
done
counting
shift_ena
模塊端口聲明
moduletop_module( inputd, inputdone_counting, inputack, input[9:0]state,//10-bitone-hotcurrentstate outputB3_next, outputS_next, outputS1_next, outputCount_next, outputWait_next, outputdone, outputcounting, outputshift_ena );
題目解析
moduletop_module( inputlogicd, inputlogicdone_counting, inputlogicack, inputlogic[9:0]state,//10-bitone-hotcurrentstate outputlogicB3_next, outputlogicS_next, outputlogicS1_next, outputlogicCount_next, outputlogicWait_next, outputlogicdone, outputlogiccounting, outputlogicshift_ena );// //Youmayusetheseparameterstoaccessstatebitsusinge.g.,state[B2]insteadofstate[6]. parameterlogic[3:0]S=4'd0,S1=4'd1,S11=4'd2,S110=4'd3, B0=4'd4,B1=4'd5,B2=4'd6,B3=4'd7, Count=4'd8,Wait=4'd9; assignB3_next=state[B2]; assignS_next=~d&state[S]|~d&state[S1]|~d&state[S110]|ack&state[Wait]; assignS1_next=d&state[S]; assignCount_next=state[B3]|~done_counting&state[Count]; assignWait_next=done_counting&state[Count]|~ack&state[Wait]; assigndone=state[Wait]; assigncounting=state[Count]; assignshift_ena=state[B0]|state[B1]|state[B2]|state[B3]; endmodule

點(diǎn)擊Submit,等待一會(huì)就能看到下圖結(jié)果:
注意圖中的Ref是參考波形,Yours是你的代碼生成的波形,網(wǎng)站會(huì)對(duì)比這兩個(gè)波形,一旦這兩者不匹配,仿真結(jié)果會(huì)變紅。
這一題就結(jié)束了。
總結(jié)
今天的幾道題就結(jié)束了,今天是大型電路的設(shè)計(jì)思路,分模塊設(shè)計(jì)。
審核編輯:劉清
-
計(jì)數(shù)器
+關(guān)注
關(guān)注
32文章
2291瀏覽量
96423 -
時(shí)序電路
+關(guān)注
關(guān)注
1文章
114瀏覽量
21949 -
狀態(tài)機(jī)
+關(guān)注
關(guān)注
2文章
493瀏覽量
28251 -
Verilog語言
+關(guān)注
關(guān)注
0文章
113瀏覽量
8555
原文標(biāo)題:HDLBits: 在線學(xué)習(xí) SystemVerilog(二十二)-Problem 151-157(構(gòu)建大型電路)
文章出處:【微信號(hào):Open_FPGA,微信公眾號(hào):OpenFPGA】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
[啟芯公開課] SystemVerilog for Verification
round robin 的 systemverilog 代碼
做FPGA工程師需要掌握SystemVerilog嗎?
(2)打兩拍systemverilog與VHDL編碼 精選資料分享
SystemVerilog Assertion Handbo
SystemVerilog的斷言手冊
基于事件結(jié)構(gòu)的SystemVerilog指稱語義
SystemVerilog的正式驗(yàn)證和混合驗(yàn)證
SystemVerilog中的Shallow Copy
Systemverilog中的Driving Strength講解

SystemVerilog在硬件設(shè)計(jì)部分有哪些優(yōu)勢

評(píng)論