使用Quartus+modelsim完成设计

参数化译码器,由于输出Y使用的是独热码,所以可以直接用移位运算符实现。
module decodern #(
parameter n=3,
m=1<`timescale 1 ns/ 1 ns
module decodern_vlg_tst();
reg [2:0] in;
// wires
wire [7:0] y;
decodern i1 (
.in(in),
.y(y)
);
initial
begin
in=3'b010;
#10 in=3'b001;
#10 in=3'b111;
#10 $stop;
end
initial
$monitor($time,": in=%b \t -> \t y=%b ",in,y);
endmodule
输出结果与预期一致
Modelsim波形显示以及monitor输出


逻辑综合电路


参数化的编码器,主要问题点是实现优先编码,只识别最高位最先为“1”的位数,然后赋值给输出y
module encodern #(
parameter n=3,
m=1<0;i=i-1)
if(in[i]==1)
begin
y = i;
disable encoder;
//jump loop called encoder
end
else y = 0;
end
endmodule
`timescale 1 ns/ 1 ns
module encodern_vlg_tst();
reg [7:0] in;
// wires
wire [2:0] y;
// assign statements (if any)
encodern i1 (
// port map - connection between master ports and signals/registers
.in(in),
.y(y)
);
initial
begin
in = 8'b1000_0000;
#10 in = 8'b0111_1111;
#10 in = 8'b0010_1000;
#10 in = 8'b0000_0000;
#10 in = 8'b0000_0000;
#10 $stop;
end
initial $monitor($time, ": in:%b \t -> \t y:%b",in,y);
endmodule
输出结果与预期一致
Modelsim波形显示以及monitor输出


逻辑综合电路


对于四位格雷码,可以直接用一个Case解决,实现一个循环,同时注意是异步复位
| Q[3] | Q[2] | Q[1] | Q[0] |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 1 |
| 0 | 0 | 1 | 1 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 |
| 0 | 1 | 1 | 1 |
| 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 0 |
| 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 |
| 1 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 0 | 0 | 0 |
module Gray(
input clk,
input rst_n,
output reg[3:0]gray
);
always@(posedge clk or negedge rst_n)
if(!rst_n) gray <= 4'b0000;
else
case(gray)
4'b0000 : gray <= 4'b0001;
4'b0001 : gray <= 4'b0011;
4'b0011 : gray <= 4'b0010;
4'b0010 : gray <= 4'b0110;
4'b0110 : gray <= 4'b0111;
4'b0111 : gray <= 4'b0101;
4'b0101 : gray <= 4'b0100;
4'b0100 : gray <= 4'b1100;
4'b1100 : gray <= 4'b1101;
4'b1101 : gray <= 4'b1111;
4'b1111 : gray <= 4'b1110;
4'b1110 : gray <= 4'b1010;
4'b1010 : gray <= 4'b1011;
4'b1011 : gray <= 4'b1001;
4'b1001 : gray <= 4'b1000;
4'b1000 : gray <= 4'b0000;
default : gray <= 4'bx;
endcase
endmodule
`timescale 1 ns/ 1 ns
module Gray_vlg_tst();
reg clk;
reg rst_n;
// wires
wire [3:0] gray;
// assign statements (if any)
Gray i1 (
// port map - connection between master ports and signals/registers
.clk(clk),
.gray(gray),
.rst_n(rst_n)
);
initial
begin
rst_n = 0;//initial state
clk = 0;
#5 rst_n = 1;
#100 $stop;
end
always #5 clk = ~clk;
initial $monitor($time,"-> \t now state of gray is : %b",gray);
endmodule
输出结果与预期一致
Modelsim波形显示以及monitor输出


逻辑综合电路
