HDLBits刷題(Shift Register&Finite State Machines))

第一題:Build a 4-bit shift register (right shift), with asynchronous reset, synchronous load, and enable.

  • areset: Resets shift register to zero.
  • load: Loads shift register with data[3:0] instead of shifting.
  • ena: Shift right (q[3] becomes zero, q[0] is shifted out and disappears).
  • q: The contents of the shift register.
    If both the load and ena inputs are asserted (1), the load input has higher priority.
module top_module(
    input clk,
    input areset,  // async active-high reset to zero
    input load,
    input ena,
    input [3:0] data,
    output reg [3:0] q);
    
    always@(posedge clk,posedge areset)
        if(areset)
            q <= 0;
        else if(load) 
            q <= data;
        else if(ena)
            q <= {1'd0,q[3:1]};   
            
endmodule
------------------------/*官網(wǎng)參考答案*/-----------------------------
module top_module(
    input clk,
    input areset,
    input load,
    input ena,
    input [3:0] data,
    output reg [3:0] q);
    
    // Asynchronous reset: Notice the sensitivity list.
    // The shift register has four modes:
    //   reset
    //   load
    //   enable shift
    //   idle -- preserve q (i.e., DFFs)
    always @(posedge clk, posedge areset) begin
        if (areset)     // reset
            q <= 0;
        else if (load)  // load
            q <= data;
        else if (ena)   // shift is enabled
            q <= q[3:1];    // Use vector part select to express a shift.
    end
    
endmodule

第二題:Build a 100-bit left/right rotator, with synchronous load and left/right enable. A rotator shifts-in the shifted-out bit from the other end of the register, unlike a shifter that discards the shifted-out bit and shifts in a zero. If enabled, a rotator rotates the bits around and does not modify/discard them.

  • load: Loads shift register with data[99:0] instead of rotating.
  • ena[1:0]: Chooses whether and which direction to rotate.
    2'b01 rotates right by one bit
    2'b10 rotates left by one bit
    2'b00 and 2'b11 do not rotate.
  • q: The contents of the rotator.
module top_module(
    input clk,
    input load,
    input [1:0] ena,
    input [99:0] data,
    output reg [99:0] q); 
    
   always@(posedge clk) begin
       if(load) q<= data;
        else begin
            case(ena)
                2'b01: q<={q[0],q[99:1]};
                2'b10: q<={q[98:0],q[99]};
                2'b00: q<=q;
                2'b11:q<=q;
            endcase
        end
   end

endmodule
-   --------------------------官網(wǎng)參考答案 --------------------
module top_module(
    input clk,
    input load,
    input [1:0] ena,
    input [99:0] data,
    output reg [99:0] q);
    
    // This rotator has 4 modes:
    //   load
    //   rotate left
    //   rotate right
    //   do nothing
    // I used vector part-select and concatenation to express a rotation.
    // Edge-sensitive always block: Use non-blocking assignments.
    always @(posedge clk) begin
        if (load)       // Load
            q <= data;
        else if (ena == 2'h1)   // Rotate right
            q <= {q[0], q[99:1]};
        else if (ena == 2'h2)   // Rotate left
            q <= {q[98:0], q[99]};
    end
endmodule

第三題:Build a 64-bit arithmetic shift register, with synchronous load. The shifter can shift both left and right, and by 1 or 8 bit positions, selected by amount.
An arithmetic right shift shifts in the sign bit of the number in the shift register (q[63] in this case) instead of zero as done by a logical right shift. Another way of thinking about an arithmetic right shift is that it assumes the number being shifted is signed and preserves the sign, so that arithmetic right shift divides a signed number by a power of two.
There is no difference between logical and arithmetic left shifts.

module top_module(
    input clk,
    input load,
    input ena,
    input [1:0] amount,
    input [63:0] data,
    output reg [63:0] q); 
    
    always@(posedge clk) 
        if(load)
            q <= data;
        else if(ena)
            case(amount)
                2'b00 : q <= {q[62:0],1'd0};
                2'b01 : q <= {q[55:0],8'd0};
                2'b10 : q <= {q[63],q[63:1]};
                2'b11 : q <= {{8{q[63]}},q[63:8]};
            endcase
            
endmodule

第四題:有序狀態(tài)機(jī)的一道入門題较锡。感覺自己對(duì)有限狀態(tài)機(jī)的理解還是不夠好煤杀!


image.png

題目給定的是莫爾型狀態(tài)機(jī)景馁,在這里,首先需要清楚一個(gè)概念 :即根據(jù)輸出信號(hào)的產(chǎn)生:有限狀態(tài)機(jī)分為兩種:

  • Mealy(米利型):Mealy型狀態(tài)機(jī)的輸出與當(dāng)前狀態(tài)和輸入有關(guān)系;
  • Moore(莫爾型) :Moore型狀態(tài)機(jī)僅與當(dāng)前狀態(tài)有關(guān)纸颜,而與輸出無關(guān)绍绘。
    對(duì)于狀態(tài)機(jī)的寫法,常用的有兩段式和三段式褐耳;
    不過一般建議用三段式編寫诈闺,比較方便易,讀
module top_module(
    input clk,
    input areset,    // Asynchronous reset to state B
    input in,
    output out);//  

    parameter A=0, B=1; 
    reg state, next_state;

    always @(*) begin    // This is a combinational always block
        case(state)
            A: if(in)
                    next_state <= A;
                else
                    next_state <= B;// State transition logic
            B: if(in)
                next_state <= B;
                else
                next_state <= A;
        endcase
    end

    always @(posedge clk, posedge areset) begin    // This is a sequential always block
        if(areset)
            state <= B;
        else
            state <= next_state;// State flip-flops with asynchronous reset
    end
            
    assign  out = state == B ? 1: 0;
    // Output logic
    // assign out = (state == ...);

endmodule
------------------------------------官網(wǎng)參考答案---------------------------------
module top_module (
    input clk,
    input in,
    input areset,
    output out
);

    // Give state names and assignments. I'm lazy, so I like to use decimal numbers.
    // It doesn't really matter what assignment is used, as long as they're unique.
    parameter A=0, B=1;
    reg state;      // Ensure state and next are big enough to hold the state encoding.
    reg next;
    
    
    // A finite state machine is usually coded in three parts:
    //   State transition logic   狀態(tài)轉(zhuǎn)移方程
    //   State flip-flops       激勵(lì)方程
    //   Output logic          輸出方程
    // It is sometimes possible to combine one or more of these blobs of code
    // together, but be careful: Some blobs are combinational circuits, while some
    // are clocked (DFFs).
    
    
    // Combinational always block for state transition logic. Given the current state and inputs,
    // what should be next state be?
    // Combinational always block: Use blocking assignments.
    always@(*) begin
        case (state)
            A: next = in ? A : B;
            B: next = in ? B : A;
        endcase
    end
    
    
    
    // Edge-triggered always block (DFFs) for state flip-flops. Asynchronous reset.
    always @(posedge clk, posedge areset) begin
        if (areset) state <= B;     // Reset to state B
        else state <= next;         // Otherwise, cause the state to transition
    end
        
        
        
    // Combinational output logic. In this problem, an assign statement is the simplest.
    // In more complex circuits, a combinational always block may be more suitable.
    assign out = (state==B);

    
endmodule

image.png
/*這里不能使用非阻塞賦值語句的原因沒有搞懂*/
// Note the Verilog-1995 module declaration syntax here:
module top_module(clk, reset, in, out);
    input clk;
    input reset;    // Synchronous reset to state B
    input in;
    output out;//  
    reg out;

    parameter A=0,B=1; // Fill in state name declarations

    reg present_state, next_state;

    always @(posedge clk) begin
        if (reset) begin  
            // Fill in reset logic
            out <= B;
            present_state <= B;
        end else begin
            case (present_state) 
                // Fill in state transition logic
                A:
                    if(in) next_state = A;
                    else next_state = B;
                B:
                    if(in) next_state = B;
                    else next_state = A;
               
            endcase
            

            // State flip-flops
            present_state = next_state;   

                case (present_state)
                // Fill in output logic
                A:out = A;
                B:out = B;
              
            endcase
        end
    end

endmodule

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末铃芦,一起剝皮案震驚了整個(gè)濱河市雅镊,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌刃滓,老刑警劉巖仁烹,帶你破解...
    沈念sama閱讀 212,222評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異咧虎,居然都是意外死亡卓缰,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,455評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門砰诵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來征唬,“玉大人,你說我怎么就攤上這事茁彭∽芎” “怎么了?”我有些...
    開封第一講書人閱讀 157,720評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵尉间,是天一觀的道長偿乖。 經(jīng)常有香客問我击罪,道長,這世上最難降的妖魔是什么贪薪? 我笑而不...
    開封第一講書人閱讀 56,568評(píng)論 1 284
  • 正文 為了忘掉前任媳禁,我火速辦了婚禮,結(jié)果婚禮上画切,老公的妹妹穿的比我還像新娘竣稽。我一直安慰自己,他們只是感情好霍弹,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,696評(píng)論 6 386
  • 文/花漫 我一把揭開白布毫别。 她就那樣靜靜地躺著,像睡著了一般典格。 火紅的嫁衣襯著肌膚如雪岛宦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,879評(píng)論 1 290
  • 那天耍缴,我揣著相機(jī)與錄音砾肺,去河邊找鬼。 笑死防嗡,一個(gè)胖子當(dāng)著我的面吹牛变汪,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蚁趁,決...
    沈念sama閱讀 39,028評(píng)論 3 409
  • 文/蒼蘭香墨 我猛地睜開眼裙盾,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了他嫡?” 一聲冷哼從身側(cè)響起番官,我...
    開封第一講書人閱讀 37,773評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎涮瞻,沒想到半個(gè)月后鲤拿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,220評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡署咽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,550評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了生音。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宁否。...
    茶點(diǎn)故事閱讀 38,697評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖缀遍,靈堂內(nèi)的尸體忽然破棺而出慕匠,到底是詐尸還是另有隱情,我是刑警寧澤域醇,帶...
    沈念sama閱讀 34,360評(píng)論 4 332
  • 正文 年R本政府宣布台谊,位于F島的核電站蓉媳,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏锅铅。R本人自食惡果不足惜酪呻,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,002評(píng)論 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望盐须。 院中可真熱鬧玩荠,春花似錦、人聲如沸贼邓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,782評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽塑径。三九已至女坑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間统舀,已是汗流浹背匆骗。 一陣腳步聲響...
    開封第一講書人閱讀 32,010評(píng)論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留绑咱,地道東北人绰筛。 一個(gè)月前我還...
    沈念sama閱讀 46,433評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像描融,于是被迫代替她去往敵國和親铝噩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,587評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容