FPGA学习笔记26--非流水和流水的8位加法器

module adder8(
output reg cout,
output reg[7:0]sum,
input [7:0]ina,
input [7:0]inb,
input cin,
input clk); 


reg[7:0] tempa,tempb; 


reg tempc; 

always @(posedge clk) 

     begin 

      tempa=ina;  tempb=inb;  tempc=cin;            //输入数据锁存 

      end 

 always @(posedge clk) 

     begin 

      {cout,sum}=tempa+tempb+tempc; 

      end 

endmodule 

module pipeline(
output reg cout,
output reg [7:0]sum,
input [7:0]ina,
input [7:0]inb,
input cin,
input clk); 
reg[7:0] tempa,tempb; 
reg tempci,firstco,secondco,thirdco; 
    reg[1:0] firsts,thirda,thirdb; 

    reg[3:0] seconda,secondb,seconds; 

    reg[5:0] firsta,firstb,thirds; 

    always @(posedge clk) 

         begin 

         tempa=ina;  tempb=inb;  tempci=cin;          
         end 

    always @(posedge clk) 

         begin 

         {firstco,firsts}=tempa[1:0]+tempb[1:0]+tempci; 

                                              //第一级加(低2位) 

         firsta=tempa[7:2];                   //未参加计算的数据缓存 

         firstb=tempb[7:2]; 

         end 

    always @(posedge clk) 

         begin 

         {secondco,seconds}={firsta[1:0]+firstb[1:0]+firstco,firsts}; 

                                         //第二级加(第2、3位相加) 

         seconda=firsta[5:2];                     //4-7位数据缓存 

         secondb=firstb[5:2]; 

         end 

    always @(posedge clk) 

         begin 

         {thirdco,thirds}={seconda[1:0]+secondb[1:0]+secondco,seconds}; 

                                         //第三级加(第 4、5位相加) 

         thirda=seconda[3:2];            //6,7位数据缓存 

         thirdb=secondb[3:2]; 

         end 

    always @(posedge clk) 

         begin 

         {cout,sum}={thirda[1:0]+thirdb[1:0]+thirdco,thirds}; 

                                         //第四级加(高两位相加) 

         end 

    endmodule 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值