zoukankan      html  css  js  c++  java
  • 计算机组成原理_verilog学习_实验一答案(原创)

    //答案
    //计组实验1答案
    //1-1
    //第1关
    module fa_behavioral(a,b,ci,s,co);//考虑进位的加法器模块 
           input a,b;
           input ci;
           output  s;
           output co;
    // 请在下面添加代码,完成一位全加器功能
    /* Begin */
            assign {co, s} = a + b +ci;
    /* End */
    endmodule
    
    //第二关
    module adder(a,b,cin,cout,sum);
      parameter bit_width=8;
      output[bit_width-1:0] sum;
      output cout;
      input [bit_width-1:0] a,b;
      input cin;
    // 请在下面添加代码,完成n=8位的无符号二进制数加法器功能
    /* Begin */
      assign {cout, sum} = a + b + cin;
    /* End */
    endmodule
    
    //第三关
    module substractor(a,b,cin,cout,sum);
      parameter bit_width=8;
      output [bit_width-1:0] sum;
      output cout;
      input [bit_width-1:0] a,b;
      input cin;//carry
    // 请在下面添加代码,完成n位的无符号二进制数减法器功能
    /* Begin */
        assign sum = (a + ~b + 1 + cin);
        assign cout = (a < b);
    /* End */
    endmodule
    
    //第四关
    module add_sub(a,b,control,cout,overflow,sum);
      parameter bit_width=4;
      output reg[bit_width-1:0] sum;     output cout,overflow;
      input [bit_width-1:0] a,b;         input control;//carry
      reg overflow,cout;                   
      reg [bit_0] a2,b2,sum2;  
      always@(control or a or b)
        begin
           a2[bit_width]=a[bit_width-1];    //将a符号位扩展成2位并赋值给a2
           a2[bit_width-1:0]=a[bit_width-1:0];
          // 请在下面添加代码,将b符号位扩展成2位并赋值给b2
            /********** Begin *********/
           b2[bit_width]=b[bit_width-1];    //将b符号位扩展成2位并赋值给b2
           b2[bit_width-1:0]=b[bit_width-1:0];
            /********** End *********/ 
            if (control==0) {cout,sum2}=a2+b2;
            else  {cout,sum2}=a2+(~b2)+control;  
            if((sum2[bit_width]^sum2[bit_width-1])==1)   overflow=1;    
            else overflow=0;   //用双符号位判溢出
             sum[bit_width-1:0]=sum2[bit_width-1:0];   
        end           
    endmodule
    
    //1-2
    
    //第一关
    //设计一个输入输出均为高电平有效的3位二进制优先编码器
    //I[7]的优先权最高,I[0]的优先权最低
    module encoder8_3_test(I,Y);
    input [7:0] I;
    output reg[2:0] Y;
    always @(I)
        begin
            if(I >= 8'b00000000 && I < 8'b00000010)
                Y = 3'b000;
            if(I >= 8'b00000010 && I < 8'b00000100)
                Y = 3'b001;
            if(I >= 8'b00000100 && I < 8'b00001000)
                Y = 3'b010;
            if(I >= 8'b00001000 && I < 8'b00010000)
                Y = 3'b011;
            if(I >= 8'b00010000 && I < 8'b00100000)
                Y = 3'b100;
            if(I >= 8'b00100000 && I < 8'b01000000)
                Y = 3'b101;
            if(I >= 8'b01000000 && I < 8'b10000000)
                Y = 3'b110;
            if(I >= 8'b10000000)
                Y = 3'b111;
        end
    /* End */
    endmodule
    
    //第二关
    //设计具有一位使能端的3线-8线译码器。当使能端为0时,8位输出信号全为0;
    //如果一位使能信号为1,则输出高电平有效的译码信号。
    module decoder3e_test(a,ena,y);
      input [2:0] a;
      input ena;
      output reg[7:0] y; 
    
      // 请在下面添加代码,完成设计任务
    /* Begin */
    always @(ena or a)
        begin
        if (ena == 0)
            y = 8'b00000000;
        else
            case(a)
                3'b000: y = 8'b00000001;
                3'b001: y = 8'b00000010;
                3'b010: y = 8'b00000100;
                3'b011: y = 8'b00001000;
                3'b100: y = 8'b00010000;
                3'b101: y = 8'b00100000;
                3'b110: y = 8'b01000000;
                3'b111: y = 8'b10000000;
                default: y = 8'b00000000;
            endcase
        end
    /* End */
    endmodule
    
    作者:LightAc
    出处:https://www.cnblogs.com/lightac/
    联系:
    Email: dzz@stu.ouc.edu.cn
    QQ: 1171613053
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    Python笔试题(递归)
    MYSQL经典面试题
    Linux常用命令
    HTTP协议相关面试题
    Flask面试题
    史上最全DVWA 笔记
    ssh root Permission denied
    odoo Reference 选择model 然后选择record
    定体, 定压, 定温, 绝热 Q E A 公式
    Vmware Bluetooth
  • 原文地址:https://www.cnblogs.com/lightac/p/12710860.html
Copyright © 2011-2022 走看看