用verilog语言写同步置位和同步复位的D触发器、JK触发器、SR触发器
时间: 2023-12-04 20:13:00 浏览: 249
D触发器Verilog描述
下面是同步置位和同步复位的 D 触发器、JK 触发器和 SR 触发器的 Verilog 代码实现:
同步置位 D 触发器:
```verilog
module sync_set_d_ff (
input wire clk,
input wire reset,
input wire set,
input wire d,
output reg q
);
always @(posedge clk) begin
if (reset) begin
q <= 1'b0;
end else if (set) begin
q <= 1'b1;
end else begin
q <= d;
end
end
endmodule
```
同步复位 D 触发器:
```verilog
module sync_reset_d_ff (
input wire clk,
input wire reset,
input wire clear,
input wire d,
output reg q
);
always @(posedge clk) begin
if (reset) begin
q <= 1'b0;
end else if (clear) begin
q <= 1'b1;
end else begin
q <= d;
end
end
endmodule
```
同步置位 JK 触发器:
```verilog
module sync_set_jk_ff (
input wire clk,
input wire reset,
input wire set,
input wire j,
input wire k,
output reg q
);
always @(posedge clk) begin
if (reset) begin
q <= 1'b0;
end else if (set) begin
q <= 1'b1;
end else if (j && k) begin
q <= ~q;
end else if (j) begin
q <= 1'b1;
end else if (k) begin
q <= 1'b0;
end
end
endmodule
```
同步复位 JK 触发器:
```verilog
module sync_reset_jk_ff (
input wire clk,
input wire reset,
input wire clear,
input wire j,
input wire k,
output reg q
);
always @(posedge clk) begin
if (reset) begin
q <= 1'b0;
end else if (clear) begin
q <= 1'b1;
end else if (j && k) begin
q <= ~q;
end else if (j) begin
q <= 1'b1;
end else if (k) begin
q <= 1'b0;
end
end
endmodule
```
同步置位 SR 触发器:
```verilog
module sync_set_sr_ff (
input wire clk,
input wire reset,
input wire set,
input wire s,
input wire r,
output reg q
);
always @(posedge clk) begin
if (reset) begin
q <= 1'b0;
end else if (set) begin
q <= 1'b1;
end else if (s && r) begin
q <= q;
end else if (s) begin
q <= 1'b1;
end else if (r) begin
q <= 1'b0;
end
end
endmodule
```
同步复位 SR 触发器:
```verilog
module sync_reset_sr_ff (
input wire clk,
input wire reset,
input wire clear,
input wire s,
input wire r,
output reg q
);
always @(posedge clk) begin
if (reset) begin
q <= 1'b0;
end else if (clear) begin
q <= 1'b1;
end else if (s && r) begin
q <= q;
end else if (s) begin
q <= 1'b1;
end else if (r) begin
q <= 1'b0;
end
end
endmodule
```
上述代码中,`clk` 为时钟信号,`reset` 和 `clear` 分别为同步置位和同步复位信号,`set` 和 `clear` 为置位和复位信号,`d`、`j`、`k`、`s` 和 `r` 分别为输入信号,`q` 为输出信号。
阅读全文