Professional Documents
Culture Documents
Bi 1:
Thit k mch cng hai s BCD 2-digit, A1A0 v B1B0, kt qu tng l gi tr
s BCD 3-digit S2S1S0. S dng SW15-8 v SW 7-0 biu din hai s BCD
A1A0 v B1B0. Gi tr ca A1A0 c hin th trn led 7-on HEX7 v HEX6;
gi tr B1B0 c hin th trn HEX5 v HEX4. Hin th tng S2S1S0 ln cc led
7-on HEX2, HEX1, HEX0. V thit k ch p dng cng cc s BCD, kim tra
trng hp nu cc gi tr input ko phi l BCD v th hin li bng cch bt sng
LED.
module LAB3_10520477_PART1 (SW, LEDG, LEDR, HEX7, HEX6, HEX5, HEX4, HEX3,
HEX2, HEX1, HEX0);
input [17:0] SW;
output [8:0] LEDR, LEDG;
output [0:6] HEX7, HEX6, HEX5, HEX4, HEX3, HEX2, HEX1, HEX0;
assign LEDR[8:0] = SW[8:0];
assign HEX3 = 7'b1111111;
// show inputs A and B
b2d_7seg H7 (SW[15:12], HEX7);
b2d_7seg H6 (SW[11:8], HEX6);
b2d_7seg H5 (SW[7:4], HEX5);
b2d_7seg H4 (SW[3:0], HEX4);
input a, b, ci;
output co, s;
wire d;
assign d = a ^ b;
assign s = d ^ ci;
assign co = (b & ~d) | (d & ci);
endmodule
endmodule
Bi 2:
Thit k mch nhn mng (array multiplier) hai s nh phn 4-bit khng du A v
B. Thit lp gi tr cho s A bng cc SW 11-8 v hin th ln HEX6. Thit lp
gi tr cho s B bng cc SW 3-0 v hin th ln HEX4. Kt qu C=A*B s
c hin th ln HEX1 v HEX0
Mch Decoder_7seg:
module Decoder_7seg(SW, HEX);
input [3:0] SW;
output [6:0] HEX;
reg [6:0] HEX;
always @(SW)
case (SW)
4'h0:HEX = 7'b1000000;
4'h1:HEX = 7'b1111001;
4'h2:HEX = 7'b0100100;
4'h3:HEX = 7'b0110000;
4'h4:HEX = 7'b0011001;
4'h5:HEX = 7'b0010010;
4'h6:HEX = 7'b0000010;
4'h7:HEX = 7'b1111000;
4'h8:HEX = 7'b0000000;
4'h9:HEX = 7'b0010000;
4'hA:HEX = 7'b0001000;
4'hB:HEX = 7'b0000011;
4'hC:HEX = 7'b1000110;
4'hD:HEX = 7'b0100001;
6
4'hE:HEX = 7'b0000110;
4'hF:HEX = 7'b0001110;
default HEX = 7'b1111111;
endcase
endmodule
Code chnh
Bi 3:
Code delay:
module Delay_1s (Clock, Clock_1);
input Clock;
output Clock_1;
parameter m = 26;
8
//dem tu 0-24999999
slow_count = 26'd 0;
else
slow_count <= slow_count + 1'b1;
end
always @ (posedge Clock)
if (slow_count == 0)
tmp = ~tmp;
assign Clock_1 = tmp;
endmodule
Code chnh:
module Lab3_10520477_part3 (CLOCK_50, KEY, HEX1, HEX0);
input CLOCK_50;
input [0:0]KEY;//KEY[0] : RESET
output [6:0] HEX1, HEX0;
wire Clk, Reset;
reg [7:0] Q;
assign Reset = KEY[0];
else
begin
Q <= Q+1;
if(Q[3:0]==4'b1010)
Q[3:0] <=0;
if(Q[7:4]==4'b1010)
Q[7:4] <=0;
end
end
Delay_1s (CLOCK_50,Clk);
Decoder7seg (Q[7:4], HEX1);
Decoder7seg (Q[3:0], HEX0);
endmodule
Bi 4:
Hin thc mt ng h hin th gi trong ngy. ng h s th hin gi tr gi (t
0 n 23) ln cc led 7-on HEX7-6, gi tr pht (t 0 n 60) ln cc led
HEX5-4, v gi tr pht (t 0 n 60) ln cc led HEX3-2. S dng cc SW15-0
reset li gi tr gi v pht cho ng h.
module LAB3_10520498_PART4 (CLOCK_50, KEY, SW, HEX7, HEX6, HEX5, HEX4,
HEX3, HEX2);
input CLOCK_50;
input [0:0]KEY;
input [15:0]SW;
output [0:6]HEX7, HEX6, HEX5, HEX4, HEX3, HEX2;
wire Clk;
reg [3:0]h1,h0,m1,m0,s1,s0;
always @(posedge Clk)
10
if (KEY[0]==0)
begin
h1 <= SW[15:12];
h0 <= SW[11:8];
m1 <= SW[7:4];
m0 <= SW[3:0];
end
else begin
s0 = s0 + 1;
if (s0 == 4'b1010)
begin
s0 = 0;
s1 = s1 + 1;
if (s1 == 4'b0110)
begin
s1 = 0;
m0 = m0 + 1;
if (m0 == 4'b1010)
begin
m0 = 0;
m1 = m1 + 1;
if (m1 == 4'b0110)
begin
m1 = 0;
h0 = h0 + 1;
if (h0 == 4'b0100)
begin
if(h1 == 4'b0010)
begin
h0 = 0;
11
h1 = 0;
end
end
else if (h0 == 4'b1010)
begin
h0 = 0;
h1 = h1 + 1;
end
end
end
end
end
end
Delay_1s (CLOCK_50,Clk);
Decoder7seg(h1,HEX7);
Decoder7seg(h0,HEX6);
Decoder7seg(m1,HEX5);
Decoder7seg(m0,HEX4);
Decoder7seg(s1,HEX3);
Decoder7seg(s0,HEX2);
endmodule
always @(SW)
case (SW)
12
//dem tu 0-49999999
slow_count = 26'd 0;
else
slow_count <= slow_count + 1'b1;
end
always @ (posedge Clock)
if (slow_count == 0)
13
tmp = ~tmp;
assign Clock_1 = tmp;
endmodule
14