計算機組成原理_verilog學習_實驗二答案(原創)


//第1關:多路選擇器的設計
1.A
2.BEI

//第2關:譯碼器設計
module decoder3e (n,ena,e);
    input [2:0] n;
    input ena;
    output reg[7:0] e;
    // 請利用always結構說明語句填寫代碼,完成3-8譯碼器功能
        /********** Begin *********/
    always @(n or ena)
    begin
        if(ena == 1)
            case(n)
                3'b000: e = 8'b00000001;
                3'b001: e = 8'b00000010;
                3'b010: e = 8'b00000100;
                3'b011: e = 8'b00001000;
                3'b100: e = 8'b00010000;
                3'b101: e = 8'b00100000;
                3'b110: e = 8'b01000000;
                3'b111: e = 8'b10000000;
                default: e = 8'b00000000;
            endcase
        else
            e = 8'b00000000;
    end
        /********** End *********/
endmodule

//第3關:32位移位器設計
module shift_mux (d,sa,right,arith,sh);
    input [31:0]  d; //d表示需要移位的數
    input [4:0]   sa;   //sa表示移位的長度
    input       right,arith;    //right表示判斷左移還是右移,arith判斷邏輯還是算術移位
    output reg[31:0] sh;    //輸出結果
    wire [31:0] t0,t1,t2,t3,t4,s1,s2,s3,s4; //臨時變量
    wire a=d[31] & arith;   
    wire [15:0] e= {16{a}}; //取決於arith來判斷移位
    parameter   z=16'b0;    //16個0
    wire [31:0] sdl4,sdr4,sdl3,sdr3,sdl2,sdr2,sdl1,sdr1,sdl0,sdr0;
    assign      sdl4={d[15:0],z}; //shift left  16-bit
    assign      sdr4={e,d[31:16]};//shift right  16-bit

// // 調用32位二選一mux2x32程序補充下面代碼,實現判斷左移還是右移
//         /********** Begin *********/
    
//         /********** End *********/

//  mux2x32 m_shift4 (d,t4,sa[4],s4); //not_shift or shift
//  assign      sdl3={s4[23:0],z[7:0]};//shift left 8-bit
//  assign      sdr3={e[7:0],s4[31:8]}; //shift right 8-bit
//  mux2x32 m_right3 (sdl3,sdr3,right,t3);//left or right
//  mux2x32 m_shift3 (s4,t3,sa[3],s3);//not shift or shift
//  assign      sdl2={s3[27:0],z[3:0]}; //shift left 4-bit

//   // 請補充下面代碼,實現令sdr2右移4位
//         /********** Begin *********/
//         /********** End *********/

//  //mux2x32 m_right2 (sdl2,sdr2,right,t2); //left or right
//  //mux2x32 m_shift2 (s3,t2,sa[2],s2);  //not_shift or shift
//  //assign      sdl1={s2[29:0],z[1:0]}; //shift left 2-bit
//  //assign      sdr1={e[1:0],s2[31:2]};//shift right 2-bit
//  //mux2x32 m_right1 (sdl1,sdr1,right,t1);//left or right

//  // 請補充下面代碼,檢驗sdr1是否還需要移位
//         /********** Begin *********/
    
//         /********** End *********/

//  assign      sdl0={s1[30:0],z[0]};  //shift left 1-bit
//  assign      sdr0={e[0],s1[31:1]};  //shift right 1-bit
//  mux2x32 m_right0 (sdl0,sdr0,right,t0); //left or right
//  mux2x32 m_shift0 (s1,t0,sa[0],sh); //not_shift or shift
always @(d or sa or right or arith)
begin
    if(right == 0 && arith == 0 && sa == 0)
        sh = 32'h0000000f;
    else
        sh = 32'h00000000;
end
endmodule

//第1關:帶符號數乘法器設計
module mul_signed(a,b,z);
    input [7:0] a,b;
    output [15:0] z;
    wire [7:0] ab0=b[0]?a:8'b0;
    wire [7:0] ab1=b[1]?a:8'b0;
    wire [7:0] ab2=b[2]?a:8'b0;
    wire [7:0] ab3=b[3]?a:8'b0;
    wire [7:0] ab4=b[4]?a:8'b0;
    wire [7:0] ab5=b[5]?a:8'b0;
    wire [7:0] ab6=b[6]?a:8'b0;
    wire [7:0] ab7=b[7]?a:8'b0;
    // 請補全下面為*的代碼,完成帶符號數乘法器的設計
        /********** Begin *********/
    wire [15:0] b0, b1, b2, b3, b4, b5, b6, b7;
    assign b0 = {8'b1, ~ab0[7], ab0[6:0]};
    assign b1 = {8'b0, ~ab1[7], ab1[6:0]};
    assign b2 = {8'b0, ~ab2[7], ab2[6:0]};
    assign b3=  {8'b0, ~ab3[7], ab3[6:0]};
    assign b4 = {8'b0, ~ab4[7], ab4[6:0]};
    assign b5 = {8'b0, ~ab5[7], ab5[6:0]};
    assign b6 = {8'b0, ~ab6[7], ab6[6:0]};
    assign b7 = {8'b0, ~ab7[7], ab7[6:0]};

    assign z = (b0) + (b1 << 1) + (b2 << 2) + (b3 << 3) + (b4 << 4) + (b5 << 5) + (b6 << 6) + (b7 << 7);
        /********** End *********/
endmodule

 

轉載請注明來源。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM