101-1學期 硬體描述語言 作業(4)
4A037052_蘇偉諺
1.    
(A) 請問一位元半加器與一位元全加器之差別為何?
全加器:要考慮進位輸出位元與進位輸入位元
        =>被加數+加數+進位輸入
半家器:不考慮進位輸出位元與進位輸入位元
        =>被加數+加數
(B) 請分別寫出一位元半加器(模組名為HA)與全加器(模組名為FA)之verilog模組。
====================================
//全加器
module
ha_1(A,B,S,C);
input
A,B;
output
S,C;
assign #1
S=A^B;//A XOR B
assign #1
C=A&B;//A and B
endmodule
====================================
//半加器
module
fa_1(cin,A,B,S,C);
input
cin,A,B;
output
S,C;
assign #1
S=cin^A^B;
assign #1
C=(A&B)|(A|B)&cin;
endmodule
    =====================================
2.    
請以verilog設計一2對1多工器模組名為mux2_1,再以此2對1多工器模組設計一4對1多工器(模組名為mux4_1)。
      ===========二對一多工器============
module
mux2_1(a,b,s,y);
          input a,b,s;
          output y;
          assign #0 y=s?b:a;
endmodule
===========四對一多工器============
module
mux4_1(a,c,b,d,s0,s1,y);
        input
a,c,b,d,s0,s1;
        output y;
        wire y0,y1;
        mux2_1
U1(a,b,s0,y0);
        mux2_1
U2(c,d,s0,y1);
        mux2_1
U3(y0,y1,s1,y);
endmodule
//二對一多工器測試平台
=================================
3.    
請設計一除以9計數器可計數0~8之時脈次數。
=================程式=================
module
cnt10(clk,reset,q);
        input  
clk,reset;
        output 
[3:0] q;
        reg    
[3:0] q;
        always@(posedge clk or posedge reset)
                if(reset)
                        q=0;
                else if(q==4'd8)
                                q= #1 0;
                        else
                                q= #1 q+1'b1;
endmodule
================測試平台================
`timescale
1ns/100ps
`include
"cnt9.v"
module  testcnt10;
        reg    
clk,reset;
        wire   
[3:0] q;
cnt10
U1(clk,reset,q);
initial
clk=0;
always #10
clk=~clk;
initial
begin
        #0     
reset=1;
        #10    
reset=0;
        #1000  
$finish;
        end
initial
begin
         $dumpfile("testcnt10.vcd");
         $dumpvars;
         end
endmodule
===========GTKWAVE波形圖============

 
沒有留言:
張貼留言
文章有誤或有問題麻煩您留言告知! 謝謝您~~