目录
一、Avoiding latches
https://hdlbits.01xz.net/wiki/Always_nolatches
(1)题目
Suppose you're building a circuit to process scancodes from a PS/2 keyboard for a game. Given the last two bytes of scancodes received, you need to indicate whether one of the arrow keys on the keyboard have been pressed. This involves a fairly simple mapping, which can be implemented as a case statement (or if-elseif) with four cases.
翻译:
假设您正在构建一个电路来处理来自PS/2键盘的游戏扫描码。给定接收到的最后两个字节的扫描码,您需要指示是否按下了键盘上的一个箭头键。这涉及到一个相当简单的映射,它可以被实现为包含四种情况的case语句(或if-elseif)。
Scancode [15:0] Arrow key 16'he06b left arrow 16'he072 down arrow 16'he074 right arrow 16'he075 up arrow Anything else none Your circuit has one 16-bit input, and four outputs. Build this circuit that recognizes these four scancodes and asserts the correct output.
(2)分析+代码
这道题是一个简单的选择结构,需要注意的是在进行选择前要将left,down,right,up的值赋为0.
// synthesis verilog_input_version verilog_2001
module top_module (
input [15:0] scancode,
output reg left,
output reg down,
output reg right,
output reg up );
always @(scancode or left or down or right or up)begin
up=0;
down=0;
right=0;
left=0;
case(scancode)
16'he06b: left=1;
16'he072: down=1;
16'he074: right=1;
16'he075: up=1;
default: ;
endcase
end
endmodule
(3)结果
(4)零零碎碎
1.数据常量表达与位宽:<位宽>'<进制><数字>
B:二进制 O:八进制 H:十六进制 D:十进制
位宽是二进制时的宽度
2. always,case使用:在always中赋值的变量要使用寄存器型变量reg。
default后的内容可以为空但不能省略。
二、Always case2
https://hdlbits.01xz.net/wiki/Always_case2
(1)题目
A priority encoder is a combinational circuit that, when given an input bit vector, outputs the position of the first 1 bit in the vector. For example, a 8-bit priority encoder given the input 8'b10010000 would output 3'd4, because bit[4] is first bit that is high.
Build a 4-bit priority encoder. For this problem, if none of the input bits are high (i.e., input is zero), output zero. Note that a 4-bit number has 16 possible combinations.
翻译:
优先编码器是一种组合电路,当给定输入位矢量时,它输出矢量中第一个1位的位置。例如,给定输入8'b10010000的8位优先级编码器将输出3'd4,因为位[4]是第一个高的位。
构建一个4位优先编码器。对于这个问题,如果没有一个输入位是高的(即,输入为零),输出为零。请注意,一个4位数字有16种可能的组合。
(2)分析+代码
L3 | L2 | L1 | L0 | R1 | R0 |
0 | 0 | 0 | 1 | 0 | 0 |
0 | 0 | 1 | X | 0 | 1 |
0 | 1 | X | X | 1 | 0 |
1 | X | X | X | 1 | 1 |
- case是一一对应,即0、1、x、z分别对应0、1、x、z;当执行到对应项后,case就会退出
- casex是将高阻值(z)和不定值(x)都视为不关心的状态,即出现x或z会匹配任意0、1、x、z状态;
- casez是将高阻值(z)视为不关心的状态,即出现z会匹配任意0、1、x、z状态;
// synthesis verilog_input_version verilog_2001
module top_module (
input [3:0] in,
output reg [1:0] pos );
always@(in or pos)begin
casex(in[3:0])
4'bxxx1: pos=2'b00;
4'bxx1x: pos=2'b01;
4'bx1xx: pos=2'b10;
4'b1xxx: pos=2'b11;
default pos=2'b00;
endcase
end
endmodule