没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
HDLBits 学习笔记
一、
Implicit nets
Implicit nets are often a source of hard-to-detect bugs. In Verilog, net-type signals can
be implicitly created by anstatement or by attaching something undeclared
to a module port. Implicit nets are always one-bit wires and causes bugs if you had
intended to use a vector. Disabling creation of implicit nets can be done using
thedirective.
!"####
""#$%"
#&%%"$$#'($%
")
*""*")
Addingwould make the second line of code an error,
which makes the bug more visible.
Unpacked vs. Packed Arrays
You may have noticed that indeclarations, the vector indices are writtenbefore the
vector name. This declares the "packed" dimensions of the array, where the bits are
"packed" together into a blob (this is relevant in a simulator, but not in hardware).
Theunpackeddimensions are declaredafterthe name. They are generally used to
declare memory arrays. Since ECE253 didn't cover memory arrays, we have not used
packed arrays in this course.
Seehttp://www.asic-world.com/systemverilog/data_types10.htmlfor more details.
+$$,,,-.$***/%"
.)
$$/0.$***#%"
)
Accessing Vector Elements: Part-Select
Accessing an entire vector is done using the vector name. For example:
takes the entire 4-bit vectoraand assigns it to the entire 8-bit vectorw(declarations
are taken from above). If the lengths of the right and left sides don't match, it is zero-
extended or truncated as appropriate.
The part-select operator can be used to access a portion of a vector:
1*2"
3#*"3
3##)))*"3
4%#%"4
" 5)6%$$***
)
" *772"")
" 82""2")
""#)
二、
9"$
Given an 8-bit input vector [7:0], reverse its bit ordering.
Classical mistake:
module top_module(
input [7:0] in,
output [7:0] out
);
assign out[7:0]=in[0:7];
endmodule
+ +does not work because Verilog does not allow
vector bit ordering to be <ipped.
三、
A Bit of Practice
One common place to see a replication operator is when sign-extending a smaller
number to a larger one, while preserving its signed value. This is done by replicating
the sign bit (the most signi>cant bit) of the smaller number to the left. For example,
sign-extending2!"0##(5) to 8 bits results in/!"0000##(5), while sign-
extending2!"1##(-3) to 8 bits results in/!"1111###(-3).
Error:
module top_module (
input [7:0] in,
output [31:0] out );
// assign out = { replicate-sign-bit , the-input };
assign out={24{in[7]},in}; TWO { }!!!!细节 里面也要包起来
Endmodule
四、
Module addsub
An adder-subtractor can be built from an adder by optionally negating one of
the inputs, which is equivalent to inverting the input then adding 1. The net
result is a circuit that can do two operations: (a + b + 0) and (a + ~b + 1).
SeeWikipediaif you want a more detailed explanation of how this circuit
works.
Build the adder-subtractor below.
You are provided with a 16-bit adder module, which you need to instantiate
twice:
$#-'#,:a#,:b:cin
#,:sum:cout:(
Use a 32-bit wide XOR gate to invert the"input whenever"is 1. (This can
also be viewed as" #XORed with sub replicated 32 times.
Seereplication operator.). Also connect the"input to the carry-in of the
adder.
This is equivalent to using a continuousment with a conditional
operator:
'(:<3:
However, the proceduralstatement provides a new way to make mistakes.
The circuit is combinational only ifis always assigned a value.
六、
module top_module (
input [15:0] scancode,
output reg left,
output reg down,
output reg right,
output reg up );
always @(*) begin
up = 1'b0; down = 1'b0; left = 1'b0; right = 1'b0;
case (scancode)
16'he06b:left = 1'b1;
16'he072:down = 1'b1;
16'he074:right = 1'b1;
16'he075:up = 1'b1;
default:begin up = 1'b0; down = 1'b0; left = 1'b0; right = 1'b0; end
endcase
end
Endmodule
这个代码有不错的参考价值,特别是 case 最开始赋值那里。
剩余29页未读,继续阅读
すもも141
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0