-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw6.v
125 lines (112 loc) · 3.42 KB
/
hw6.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//Declare the ports of Half adder module
module half_adder(A, B, S, C);
//what are the input ports.
input A;
input B;
//What are the output ports.
output S;
output C;
//reg S,C; NOT NEEDED will cause error
//Implement the Sum and Carry equations using Structural verilog
xor x1(S,A,B); //XOR operation (S is output not a input)
and a1(C,A,B); //AND operation
endmodule
//Declare the ports for the full adder module
module full_adder( A, B, Cin, S, Cout); // (Port names)
//what are the input ports.
input A;
input B;
input Cin;
//What are the output ports.
output S;
output Cout;
//reg A,B,Cin; //(Doesn't need to be reg, it will cause an error
wire Sa1, Ca1, Ca2;
//Two instances of half adders used to make a full adder
half_adder a1(.A(A),.B(B),.S(Sa1),.C(Ca1)); //S to Sa1 to match diagram
half_adder a2(.A(Sa1),.B(Cin),.S(S),.C(Ca2));
or o1(Cout,Ca1,Ca2);
endmodule
module my16bitadder(output [15:0] O, output c, input [15:0] A, B, input S);
//wires for connecting output to input
wire Cout0in1, Cout1in2, Cout2in3, Cout3in4, Cout4in5, Cout5in6, Cout6in7, Cout7in8,
Cout8in9, Cout9in10, Cout10in11, Cout11in12, Cout12in13, Cout13in14, Cout14in15;
full_adder Add0 ( A[0], B[0], S, O[0], Cout0in1);
full_adder Add1 ( A[1], B[1], Cout0in1, O[1], Cout1in2);
full_adder Add2 ( A[2], B[2], Cout1in2, O[2], Cout2in3);
full_adder Add3 ( A[3], B[3], Cout2in3, O[3], Cout3in4);
full_adder Add4 ( A[4], B[4], Cout3in4, O[4], Cout4in5);
full_adder Add5 ( A[5], B[5], Cout4in5, O[5], Cout5in6);
full_adder Add6 ( A[6], B[6], Cout5in6, O[6], Cout6in7);
full_adder Add7 ( A[7], B[7], Cout6in7, O[7], Cout7in8);
full_adder Add8 ( A[8], B[8], Cout7in8, O[8], Cout8in9);
full_adder Add9 ( A[9], B[9], Cout8in9, O[9], Cout9in10);
full_adder Add10 ( A[10], B[10], Cout9in10, O[10], Cout10in11);
full_adder Add11 ( A[11], B[11], Cout10in11, O[11], Cout11in12);
full_adder Add12 ( A[12], B[12], Cout11in12, O[12], Cout12in13);
full_adder Add13 ( A[13], B[13], Cout12in13, O[13], Cout13in14);
full_adder Add14 ( A[14], B[14], Cout13in14, O[14], Cout14in15);
full_adder Add15 ( A[15], B[15], Cout14in15, O[15], c);
endmodule
module my8bitmultiplier(output reg[15:0] O, output reg Done, output Cout, input [7:0] A, B, input Load,Clk,Reset,Cin);
reg[1:0] state;
reg[15:0] A_reg, B_reg, A_temp, O_reg, B_temp;
wire[15:0] O_temp;
my16bitadder dut1(O_temp, Cout, A_temp, B_temp, Cin);
always@(posedge Clk)
begin
if(Reset)
state <= 0;
else
case(state)
0: if(Load)begin
A_reg <= A;
B_reg <= B;
O_reg <= A;
state <= 1;
Done <= 0;
O <= 0;
end
1: begin
A_temp <= A_reg;
B_temp <= O_reg;
B_reg <= B_reg - 1;
state <= 2;
end
2: begin
O_reg <= O_temp;
if(B_reg>1) state <= 1;
else begin
state <= 3;
Done <= 1;
O <= O_temp;
end
end
3: begin
Done <= 0;
state <= 0;
end
endcase
end
endmodule
module my8bitmultiplier_tb;
reg clk, reset, load, cin;
reg [7:0] a, b;
wire done, cout;
wire[15:0] out;
my8bitmultiplier dut(out, done, cout, a, b, load, clk, reset, cin);
always #5 clk = ~clk;
initial
begin
clk = 0;
cin = 0;
reset = 1;
load = 1;
a = 8'd10;
b = 8'd10;
#10 reset = 0;
#10 load = 0;
#200 $display (" A = %d, B = %d, O = %d", a, b, out);
#10 $finish;
end
endmodule