设计一个指令运算单元ALU,完成功能如下。
(1)操作类型1:将操作数1作为一个无符号二进制数,在七段管以十进制显示二进制序列等效值。
(2)操作类型2:实现操作数3、操作数4之间相加、减、乘的操作,在七段管以十/十六进制进制显示操作数和结果。操作数3和4为BCD码表示的2位十进制数(表示的值为00-99)。

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
module lab1(sw,hex0,hex1,hex2,hex3,hex4,hex5,hex6,hex7);
input[17:0] sw;
output[6:0] hex0,hex1,hex2,hex3,hex4,hex5,hex6,hex7;
reg[4:0] h7,h6,h5,h4,h3,h2,h1,h0;
reg[15:0] o1,o2,s;
reg[17:0] sw0;

always@(*)
begin
if(sw[3:0]>9) sw0[3:0]=9;
else sw0[3:0]=sw[3:0];
if(sw[7:4]>9) sw0[7:4]=9;
else sw0[7:4]=sw[7:4];
if(sw[11:8]>9) sw0[11:8]=9;
else sw0[11:8]=sw[11:8];
if(sw[15:12]>9) sw0[15:12]=9;
else sw0[15:12]=sw[15:12];

if(sw[17:16]==0)
begin
h7=16;
h6=16;
h5=16;
h0=sw[15:0]%10;
h1=sw[15:0]/10%10;
h2=sw[15:0]/100%10;
h3=sw[15:0]/1000%10;
h4=sw[15:0]/10000;
show(h0,hex0);
show(h1,hex1);
show(h2,hex2);
show(h3,hex3);
show(h4,hex4);
end
else if(sw[17:16]==1)
begin
o1=sw0[15:12]*10+sw0[11:8];
o2=sw0[7:4]*10+sw0[3:0];
s=o1+o2;
h7=sw0[15:12];
h6=sw0[11:8];
h5=sw0[7:4];
h0=s%10;
h1=s/10%10;
h2=s/100;
h3=16;
h4=sw0[3:0];
show(h0,hex0);
show(h1,hex1);
show(h2,hex2);
show(h3,hex3);
show(h4,hex4);
show(h5,hex5);
show(h6,hex6);
show(h7,hex7);
end
else if(sw[17:16]==2)
begin
o1=sw0[15:12]*10+sw0[11:8];
o2=sw0[7:4]*10+sw0[3:0];
s=o1*o2;
h7=sw0[15:12];
h6=sw0[11:8];
h5=sw0[7:4];
h0=s%10;
h1=s/10%10;
h2=s/100%10;
h3=s/1000%10;
h4=sw0[3:0];
show(h0,hex0);
show(h1,hex1);
show(h2,hex2);
show(h3,hex3);
show(h4,hex4);
show(h5,hex5);
show(h6,hex6);
show(h7,hex7);
end
else if(sw[17:16]==3)
begin
o1=sw0[15:12]*10+sw0[11:8];
o2=sw0[7:4]*10+sw0[3:0];
if(o1>=o2)
begin
s=o1-o2;
h7=sw0[15:12];
h6=sw0[11:8];
h5=sw0[7:4];
h0=s%10;
h1=s/10%10;
h2=s/100%10;
h3=16;
h4=sw0[3:0];
show(h0,hex0);
show(h1,hex1);
show(h2,hex2);
show(h3,hex3);
show(h4,hex4);
show(h5,hex5);
show(h6,hex6);
show(h7,hex7);
end
else
begin
s=o2-o1;
h7=sw0[15:12];
h6=sw0[11:8];
h5=sw0[7:4];
h0=s%10;
h1=s/10%10;
h2=s/100%10;
h3=10;
h4=sw0[3:0];
show(h0,hex0);
show(h1,hex1);
show(h2,hex2);
show(h3,hex3);
show(h4,hex4);
show(h5,hex5);
show(h6,hex6);
show(h7,hex7);
end
end
end

task show;
input reg[3:0] decc;
output reg[6:0] out;
if(decc==0) out=7'b1000000;
else if(decc==1) out=7'b1111001;
else if(decc==2) out=7'b0100100;
else if(decc==3) out=7'b0110000;
else if(decc==4) out=7'b0011001;
else if(decc==5) out=7'b0010010;
else if(decc==6) out=7'b0000010;
else if(decc==7) out=7'b1111000;
else if(decc==8) out=7'b0000000;
else if(decc==9) out=7'b0011000;
else if(decc==10)out=7'b0111111;
else out=7'b1111111;
endtask
endmodule