本周刷题覆盖范围:Verilog Language刷完,组合电路部分刷完,时序电路部分没刷,reading simulations部分中finding bugs部分刷完Build a circuit from a simulation waveform部分差四道题(组合部分刷完,时序部分没做)
本周将总结分为三部分:零散知识点、verilog代码细节分析以及代码风格学习。
一.零散知识点:
BDC码:二进制表示十进制
Nor 或非门
Nand 与非门
complement numbers 补码数(原码反码补码部分上周讲过)
a (signed) overflow 符号位判断是否溢出
二.verilog代码细节分析:
位切片代码的使用
module top_module(
input [1023:0] in,
input [7:0] sel,
output [3:0] out
);
assign out = in[4*sel +: 4];
assign out=in[sel*4+3:sel*4];//这种是错误的写法
endmodule
向量索引需要向量的索引长度不变,这就导致像这种很容易想到的索引操作
[ sel *4+3 : sel *4 ]报错 “… is not a constant” (由于上下界均为变量)。
在这里位切片就很好用,位切片的用法如下:
[M -: N] // negative offset from bit index M, N bit result
[M +: N] // positive offset from bit index M, N bit result
三.代码风格学习:
1.注意代码的简化
module top_module(
input [99:0] a, b,
input cin,
output cout,
output [99:0] sum
);
assign {cout, sum} = a + b + cin;
endmodule
module top_module(
input [99:0] a, b,
input cin,
output cout,
output [99:0] sum );
wire [100:0]sum1;
assign sum=sum1[99:0];
assign sum1=a+b+cin;
assign cout=(sum1[100])?1:0;
endmodule
对比前后两组代码都能实现功能,但是代码1明显比代码2更简单
2.代码参数化
平时写短代码时体会不到参数化的意义,现在开始做毕设,每一个例化电路代码都很长,一旦改一点就要一行一行找代码改,如果提前进行参数化,把用到的位宽等参数提前用parameter写好就可以不用这么麻烦
wire post_frame_vsync;
wire post_frame_href;
wire post_frame_clken;
parameter IMG_WIDTH = 'd512 ; //图像宽度
parameter IMG_HEIGHT = 'd511 ; //图像高度
parameter IMG_GRAY = 9'd256; //总共有256个灰度级
像这种没有提前用parameter写好的,一旦位宽要进行更改就得一行一行找
reg [7:0] clear_cnt; //清零地址信号