TỔNG HỢP ÔN TẬP THIẾT KẾ VI MẠCH SỐ
CODE VHDL:
1. MULTIPLEXER A
module MultiplexerA (a, b, s, w);
input a,b,s;
output w;
wire a_sel, b_sel, s_bar;
not U1 (s_bar,s);
and U2(a_sel,a ,s_bar);
and U3(b_sel,b,s);
or U4(w, a_sel, b_sel);
endmodule
2. MULTIPLEXER B
module MultiplexerB (a,b,s,w);
input a,b,s;
output w;
assign w = (a & ~ s) | (b & s);
endmodule
3. MULTIPLEXER C
module MultiplexerC (a,b,s,w);
input a,b,s;
output w;
assign w = s ? b : a;
endmodule
4. MULTIPLEXER D
module MultiplexerD (a,b,s,w);
input a,b,s;
output w;
reg w;
always @ (a,b,s) begin
if (s) w = b;
else w = a;
end
endmodule
5. MULTIPLEXER E
module MultiplexerE (a, b, s, w);
wire s_bar;
input a, b, s;
output w;
not U1 (s_bar, s);
ANDOR U2 (a, s_bar, s, b, w);
endmodule
6. Counterupdown8
module Counterupdown8(reset, clk, count, pause, direction);
input reset, clk , pause, direction;
output [7:0] count ;
reg [7:0] count;
always @ (negedge clk) begin
if (reset) count <= 8'b00000000;
else if (pause) count <= count;
else if (direction) count <= count +1;
else count <= count - 1 ;
end
endmodule
7. Counter4
module Counter4 (reset, clk, count);
input reset, clk;
output [3:0] count;
reg [3:0] count;
always @ (negedge clk) begin
if (reset) count <= 4'b0000;
else count <= count + 1;
end
endmodule
//counter 4 bit dem tu 0000 den 1111
8. Fulladder (fulladder1bit)
module fulladder (a,b,cin,sum,cout);
input a,b,cin;
output sum, cout;
assign sum = a ^ b ^ cin ;
assign cout = (a & b) | (a & cin) | (b & cin);
endmodule
9. Fulladder4bit
module fulladder4bit(a,b,cin,sum,cout);
input [3:0] a,b;
input cin;
output [3:0] sum;
output cout;
wire c1, c2, c3;
fulladder fa0(a[0], b[0], cin, sum[0], c1);
fulladder fa1(a[1], b[1], c1, sum[1], c2);
fulladder fa2(a[2], b[2], c2, sum[2], c3);
fulladder fa3(a[3], b[3], c3, sum[3], cout);
endmodule
10. Mux41
module Mux41 (data0, data1, data2, data3, s0, s1, yout );
input data0, data1, data2, data3, s0, s1;
output [3:0] yout;
wire w0, w1;
assign w0 = s0 ? data1 : data0;
assign w1 = s0 ? data3 : data2;
assign yout = s1 ? w1 : w0;
endmodule
11. Mux8
module Mux8 (sel,data1,data0,bus1);
input sel;
input [7:0] data1, data0;
output [7:0] bus1;
assign #6 bus1 = sel ? data1 : data0;
endmodule
12. ALU8
module ALU8 (input [7:0] left, right, input [1:0] mode, output reg [7:0] ALUout);
always @(left, right, mode) begin
case (mode)
0: ALUout = left + right;
1: ALUout = left - right;
2: ALUout = left & right;
3: ALUout = left | right;
default: ALUout = 8'bX;
endcase
end
endmodule
13. ALU
module ALU (Inbus, Aside, Bside, select_source, Function, Outbus);
input select_source ;
input [7:0] Inbus, Aside, Bside;
input [1:0] Function;
output [7:0] Outbus;
wire[7:0] ABinput;
Mux8 U1 ( select_source, Aside, Bside, ABinput );
ALU8 U2 ( Inbus, ABinput, Function, Outbus );
endmodule
14. ALU_Mux
module ALU_Mux(input [7:0] left, right, input [1:0] mode, input sel,
output reg [7:0] ALUout);
wire [7:0] ALUresult;
wire [7:0] input1, input0;
wire [7:0] bus1;
ALU8 ALU_inst ( .left(left), .right(right), .mode(mode), .ALUout(ALUresult) );
Mux8 Mux_inst ( .sel(sel), .data1(ALUresult), .data0(right), .bus1(bus1) );
assign input1 = bus1;
assign input0 = right;
always @ (sel) begin
if (sel)
ALUout = input1;
else
ALUout = input0;
end
endmodule
15. ShiftRegister8
module ShiftRegister8 (input sl, sr, clk, input [7:0] ParIn, input [1:0] m, output reg
[7:0] ParOut);
always @ (negedge clk) begin
case (m)
0: ParOut <= ParOut;
1: ParOut <= {sl, ParOut [7:1]};
2: ParOut <= {ParOut [6:0], sr};
3: ParOut <= ParIn;
default: ParOut <= 8'bX;
endcase
end
endmodule
16. ShiftRegister16
module ShiftRegister16 (input sl, sr, clk, input [15:0] ParIn, input [1:0] m, output
reg [15:0] ParOut);
always @ (negedge clk) begin
case (m)
0: ParOut <= ParOut;
1: ParOut <= {sl, ParOut [15:1]};
2: ParOut <= {ParOut [14:0], sr};
3: ParOut <= ParIn;
default: ParOut <= 16'bX;
endcase
end
endmodule
17. Shift_reg (basic shift_reg 4 bit)
module Shift_Reg( input [3:0] d, input clk, ld, rst, s_in, input [1:0] l_r, output reg
[3:0] q);
always @ (negedge clk) begin
if (rst) q <= 4'b0000;
else
if (ld) q<=d;
else
case (l_r)
0: q <= {s_in, q [3:1]};
1: q <= {q [2:0], s_in};
default: q <= 4'bX;
endcase
end
endmodule
18. DETECTOR11101
module Detector11101 (input a, clk, reset, output w);
parameter [2:0] s0=3'b000, s1=3'b001, s2=3'b010, s3=3'b011,s4=3'b100,s5=3'b101;
reg [2:0] current;
always @ (posedge clk) begin
if (reset) current = s0;
else
case (current)
s0: if (a) current <= s1; else current <= s0;
s1: if (a) current <= s2; else current <= s0;
s2: if (a) current <= s3; else current <= s0;
s3: if (a) current <= s3; else current <= s4;
s4: if (a) current <= s5; else current <= s0;
s5: if (a) current <= s2; else current <= s0;
endcase
end
assign w = (current == s5) ? 1: 0;
endmodule
MỘT SỐ CODE VHDL TRONG BÀI QUÁ TRÌNH GIỮA KÌ
19. Bộ cộng có nhớ Full Adder 8 bit
20. Bộ trừ 2 số 1 bit có nhớ
21. Mạch so sánh hai số nhị phân 4 bit, sử dụng lệnh assign
22. Thanh ghi dịch cơ bản 16 bit. Vẽ mạch RTL.
RTL:
23. Thanh ghi dịch song song sang nối tiếp 8 bit với khả năng đặt dữ liệu.
Vẽ mạch RTL.
RTL:
24. Thanh ghi dịch nối tiếp sang song song 8 bit với khả năng đặt dữ liệu.
Vẽ mạch RTL.
RTL:
25. Mạch phát hiện chuỗi bit “10011”. Vẽ FSM.
FSM:
26. Mạch đếm Johnson 16 bit.
27. Mạch đếm lên/ xuống 8 bit có ngõ điều khiển up/down, load, pause,
reset.
28. Mạch đếm 4 bit có các trạng thái sau: 0,2,4,6,8,9,7,5,3,1,0,2..(lặp lại..)
29. Mạch Mux 2->1 bằng 3 cách: sử dụng lệnh assign; sử dụng modulo
ANDOR; sử dụng cấu trúc lệnh điều khiển.
SỬ DỤNG LỆNH ASSIGN:
SỬ DỤNG MODULO ANDOR:
SỬ DỤNG CẤU TRÚC LỆNH ĐIỀU KHIỂN:
30. Mux 4->1 từ mạch Mux 2->1 câu 11.
31. FIFO cơ bản có dung lượng 16x8Byte. Vẽ RTL
Bài 17: LIFO cơ bản có dung lượng 8x8Byte. Vẽ RTL.
SƠ ĐỒ RTL:
1.
2.
3.
4.
5.
6.
7.
CODE VERILOG:
1. KHAI BÁO:
2. KHỐI WRITE
3. KHỐI READ
4. KHỐI POINTER
5. BỘ ĐẾM
32. Mạch tạo mã gold.
1. sub_a
2. sub_b
3. gold_code