Create 16 D flip-flops. It's sometimes useful to only modify parts of a group of flip-flops. The byte-enable inputs control whether each byte of the 16 registers should be written to on that cycle. byteena[1] controls the upper byte d[15:8], while byteena[0] controls the lower byte d[7:0].
resetn is a synchronous, active-low reset.
All DFFs should be triggered by the positive edge of clk.
本题中需要创建一个 16 路 D触发器。部分情况下,只需要多路触发器中的一部分触发器工作,此时可以通过 ena 使能端进行控制。使能端 ena 信号有效时,触发器在时钟上升沿工作。
byteena 使能信号以 byte 为单位管理 8 路触发器在时钟边沿触发与否。byteena [1] 作为 d[15:8] 高位字节的使能端,byteena [0] 则控制 d 的低位字节。
resetn 为同步,低电平有效复位信号。
所有的触发器在时钟上升沿被触发。
module top_module (
input clk,
input resetn,
input [1:0] byteena,
input [15:0] d,
output [15:0] q
);
always @(posedge clk) begin
if(!resetn)
q <= 16'd0;
else begin
case(byteena) // 这里用if..else if..else if..else这种写法也是可以的
2'b00: q <= q;
2'b01: q[7:0] <= d[7:0];
2'b10: q[15:8] <= d[15:8];
2'b11: q <= d;
endcase
end
end
endmodule
有不明白的地方:
1、在过程语句中可以写 q <= q 原值赋给原值 这样的语句吗?为什么2'b01 和2'b10 两种情况不写另外8位保持原样,于是尝试了下面这种写法,还是ok的,也就是说在过程语句中可以 q[15:8] <= q[15:8] 这种写法:
module top_module (
input clk,
input resetn,
input [1:0] byteena,
input [15:0] d,
output [15:0] q
);
always @(posedge clk) begin
if(!resetn)
q <= 16'd0;
else begin
case(byteena)
2'b00: q <= q;
2'b01: begin q[7:0] <= d[7:0]; q[15:8] <= q[15:8]; end
2'b10: begin q[15:8] <= d[15:8]; q[7:0] <= q[7:0]; end
2'b11: q <= d;
endcase
end
end
endmodule
2、还有一个问题,就是在2'b01 和2'b10这两种情况下面都可以只写出q的一部分,不改变的部分就不写,于是我尝试了在2'b00 这种q值不改变的情况下不写,发现也是可以的:
module top_module (
input clk,
input resetn,
input [1:0] byteena,
input [15:0] d,
output [15:0] q
);
always @(posedge clk) begin
if(!resetn)
q <= 16'd0;
else begin
case(byteena) // 这里用if..else if..else if..else这种写法也是可以的
2'b00: ; // 因为这里q值不变,省略不写,这种尝试了一下也是ok的
2'b01: q[7:0] <= d[7:0];
2'b10: q[15:8] <= d[15:8];
2'b11: q <= d;
endcase
end
end
endmodule