
9.1 门级描述

p131 9.1 门级逻辑描述的D触发器
module flop(data,clock,clear,q,qb);
input data,clear,clock;
output q,qb;
nand #10 nd1(a,data,clock,clear),
nd2(b,ndata,,clock),
nd4(d,c,b,clear),
nd5(e,c,nclock),
nd6(f,d,nclock),
nd8(qb,q,f,clock);
nand #9 nd3(c,a,d),
nd7(q,qb,e);
not #10 iv1(ndata,data),
iv2(nclock,clock);
endmodule

p132 9.2 用触发器组成异步清零的4位寄存器
`include "flop.v"
module hardreg(d,clk,clrb,q);
input [3:0] d;
input clk,clrb;
output [3:0] q;
flop f1(d[0],clk,clrb,q[0]),
f2(d[1],clk,clrb,q[1]),
f3(d[2],clk,clrb,q[2]),
f4(d[3],clk,clrb,q[3]);
endmodule

9.2 行为描述建模
p133 9.3 行为描述用触发器组成异步清零的4位寄存器
module hardreg(d,clk,clrb,q);
input [3:0] d;
input clk,clrb;
output [3:0] q;
always@(posedge clk or posedge clrb) begin
if(clrb) q<=0;
else q<=d;
end
endmodule
p135 9.4 对4位寄存器全面测试
`include "flop.v"
`include "hardreg.v"
module hardreg_top;
reg clock,clearb;
reg [3:0] data;
wire [3:0] qout;
`define stim #100 data=4'b
event end_first_pass;
hardreg reg_4bit(.d(data),.clk(clock),.clrb(clearb),.q(qout));
initial begin
clock= 0;
clearb= 1;
end
always #5 clk=~clk;
always@(end_first_pass) begin
clearb= ~clearb;
end
always@(posedge clock) begin
$display ("at time %0d,clearb=%b,data=%d,qout=%d",$time,clearb,data,qout);
end
initial begin
repeat(4) begin
data=4'b0000;
`stim 0001;
`stim 0010;
`stim 0011;
`stim 0100;
`stim 0101;
......
`stim 1111;
#200 ->end_first_pass;
end
$finish;
end
endmodule