Verilog Examples
Verilog Examples
Verilog Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
begin
begin
begin
begin
begin
35
binary_out = 6;
36
end if (encoder_in == 16'h0080) begin
37
binary_out = 7;
38
end if (encoder_in == 16'h0100) begin
39
binary_out = 8;
40
end if (encoder_in == 16'h0200) begin
41
binary_out = 9;
42
end if (encoder_in == 16'h0400) begin
43
binary_out = 10;
44
end if (encoder_in == 16'h0800) begin
45
binary_out = 11;
46
end if (encoder_in == 16'h1000) begin
47
binary_out = 12;
48
end if (encoder_in == 16'h2000) begin
49
binary_out = 13;
50
end if (encoder_in == 16'h4000) begin
51
binary_out = 14;
52
end if (encoder_in == 16'h8000) begin
53
binary_out = 15;
54
end
55
end
56 end
57
58 endmodule
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 begin
20
binary_out = 0;
21
if (enable) begin
22
case (encoder_in)
23
16'h0002 : binary_out = 1;
24
16'h0004 : binary_out = 2;
25
16'h0008 : binary_out = 3;
26
16'h0010 : binary_out = 4;
27
16'h0020 : binary_out = 5;
28
16'h0040 : binary_out = 6;
29
16'h0080 : binary_out = 7;
30
16'h0100 : binary_out = 8;
31
16'h0200 : binary_out = 9;
32
16'h0400 : binary_out = 10;
33
16'h0800 : binary_out = 11;
34
16'h1000 : binary_out = 12;
35
16'h2000 : binary_out = 13;
36
16'h4000 : binary_out = 14;
37
16'h8000 : binary_out = 15;
38
endcase
39
end
40 end
41
42 endmodule
Priority Encoders
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
);
output [3:0] binary_out ;
input enable ;
input [15:0] encoder_in ;
reg [3:0] binary_out ;
always @ (enable or encoder_in)
begin
binary_out = 0;
if (enable) begin
if (encoder_in[0] == 1) begin
binary_out = 1;
end else if (encoder_in[1] == 1) begin
binary_out = 2;
end else if (encoder_in[2] == 1) begin
binary_out = 3;
end else if (encoder_in[3] == 1) begin
binary_out = 4;
end else if (encoder_in[4] == 1) begin
binary_out = 5;
end else if (encoder_in[5] == 1) begin
binary_out = 6;
end else if (encoder_in[6] == 1) begin
binary_out = 7;
end else if (encoder_in[7] == 1) begin
binary_out = 8;
end else if (encoder_in[8] == 1) begin
binary_out = 9;
end else if (encoder_in[9] == 1) begin
binary_out = 10;
end else if (encoder_in[10] == 1) begin
binary_out = 11;
end else if (encoder_in[11] == 1) begin
binary_out = 12;
end else if (encoder_in[12] == 1) begin
binary_out = 13;
end else if (encoder_in[13] == 1) begin
binary_out = 14;
end else if (encoder_in[14] == 1) begin
binary_out = 15;
end
end
end
endmodule
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Decoders
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
);
//-----------Input Ports--------------input din_0, din_1, sel ;
//-----------Output Ports--------------output mux_out;
//------------Internal Variables-------wire mux_out;
//-------------Code Start----------------assign mux_out = (sel) ? din_1 : din_0;
endmodule //End Of Module mux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 endmodule //End Of Module mux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
D Flip-Flop
Asynchronous reset D- FF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Synchronous reset D- FF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
data
, // Data Input
clk
, // Clock Input
reset , // Reset input
q
// Q output
);
//-----------Input Ports--------------input data, clk, reset ;
//-----------Output Ports--------------output q;
//------------Internal Variables-------reg q;
//-------------Code Starts Here--------always @ ( posedge clk)
if (~reset) begin
q <= 1'b0;
end else begin
q <= data;
end
endmodule //End Of Module dff_sync_reset
T Flip-Flop
Asynchronous reset T - FF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
q
// Q output
);
//-----------Input Ports--------------input data, clk, reset ;
//-----------Output Ports--------------output q;
//------------Internal Variables-------reg q;
//-------------Code Starts Here--------always @ ( posedge clk or negedge reset)
if (~reset) begin
q <= 1'b0;
end else if (data) begin
q <= ! q;
end
endmodule //End Of Module tff_async_reset
Synchronous reset T - FF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 endmodule //End Of Module tff_async_reset
D latch
Regular D Latch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1 //-----------------------------------------------------
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
LFSR Up/Down
1 `define WIDTH 8
2 module lfsr_updown (
3 clk
,
// Clock input
4 reset
,
// Reset input
5 enable
,
// Enable input
6 up_down
,
// Up Down input
7 count
,
// Count output
8 overflow
// Overflow output
9 );
10
11 input clk;
12 input reset;
13 input enable;
14 input up_down;
15
16 output [`WIDTH-1 : 0] count;
17 output overflow;
18
19 reg [`WIDTH-1 : 0] count;
20
21 assign overflow = (up_down) ? (count ==
{{`WIDTH-1{1'b0}}, 1'b1}) :
22
(count ==
{1'b1, {`WIDTH-1{1'b0}}}) ;
23
24 always @(posedge clk)
25 if (reset)
26
count <= {`WIDTH{1'b0}};
1 module tb();
2 reg clk;
3 reg reset;
4 reg enable;
5 reg up_down;
6
7 wire [`WIDTH-1 : 0] count;
8 wire overflow;
9
10 initial begin
11
$monitor("rst %b en %b updown %b cnt %b overflow
%b",
12
reset,enable,up_down,count, overflow);
13
clk = 0;
14
reset = 1;
15
enable = 0;
16
up_down = 0;
17
#10 reset = 0;
18
#1 enable = 1;
19
#20 up_down = 1;
20
#30 $finish;
21 end
22
23 always #1 clk = ~clk;
24
25 lfsr_updown U(
26 .clk
( clk
),
27 .reset
( reset
),
28 .enable
( enable
),
29 .up_down ( up_down ),
30 .count
( count
),
31 .overflow ( overflow )
32 );
33
34 endmodule
Gray Counter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Divide by 2 Counter
Divide By 3 Counter
This module divides the input clock frequency by 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
;
;
;
;
;
;
21 // Posedge counter
22 always @ (posedge clk_in)
23 if (reset) begin
24
pos_cnt <= 0;
25 end else begin
26
pos_cnt <= (pos_cnt == 2) ? 0 : pos_cnt + 1;
27 end
28 // Neg edge counter
29 always @ (negedge clk_in)
30 if (reset) begin
31
neg_cnt <= 0;
32 end else begin
33
neg_cnt <= (neg_cnt == 2) ? 0 : neg_cnt + 1;
34 end
35
36 assign clk_out = ((pos_cnt ! = 2) && (neg_cnt ! =
2));
37
38 endmodule
39
40 // Testbench to check the divide_by_3 logic
41 module test();
42 reg reset, clk_in;
43 wire clk_out;
44 divide_by_3 U (
45
.clk_in (clk_in),
46
.reset (reset),
47
.clk_out (clk_out)
48 );
49
50 initial begin
51
clk_in = 0;
52
reset = 0;
53
#2 reset = 1;
54
#2 reset = 0;
55
#100 $finish;
56 end
57
58 always #1 clk_in = ~clk_in;
59
60 endmodule
47
toggle2 <= ~toggle2;
48 end else
begin
49
counter2 <= counter2 + 1;
50 end
51
52 assign clk_out = (counter1 <3 && counter2 < 3)
& enable;
53
54 endmodule
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
;
;
;
;
;
//--------------Internal variables----------------
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
oe
);
// Output Enable
parameter DATA_WIDTH = 8 ;
parameter ADDR_WIDTH = 8 ;
parameter RAM_DEPTH = 1 << ADDR_WIDTH;
//--------------Input Ports----------------------input
clk
cs
;
24 input
we
;
25 input
oe
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cs
21 input
we
22 input
oe
;
;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 input oe_1 ;
34
35 //--------------Inout Ports----------------------36 inout [data_0_WIDTH-1:0] data_0 ;
37 inout [data_0_WIDTH-1:0] data_1 ;
38
39 //--------------Internal variables---------------40 reg [data_0_WIDTH-1:0] data_0_out ;
41 reg [data_0_WIDTH-1:0] data_1_out ;
42 reg [data_0_WIDTH-1:0] mem [0:RAM_DEPTH-1];
43
44 //--------------Code Starts Here-----------------45 // Memory Write Block
46 // Write Operation : When we_0 = 1, cs_0 = 1
47 always @ (posedge clk)
48 begin : MEM_WRITE
49
if ( cs_0 && we_0 ) begin
50
mem[address_0] <= data_0;
51
end else if (cs_1 && we_1) begin
52
mem[address_1] <= data_1;
53
end
54 end
55
56
57
58 // Tri-State Buffer control
59 // output : When we_0 = 0, oe_0 = 1, cs_0 = 1
60 assign data_0 = (cs_0 && oe_0 && ! we_0) ?
data_0_out : 8'bz;
61
62 // Memory Read Block
63 // Read Operation : When we_0 = 0, oe_0 = 1, cs_0 = 1
64 always @ (posedge clk)
65 begin : MEM_READ_0
66
if (cs_0 && ! we_0 && oe_0) begin
67
data_0_out <= mem[address_0];
68
end else begin
69
data_0_out <= 0;
70
end
71 end
72
73 //Second Port of RAM
74 // Tri-State Buffer control
75 // output : When we_0 = 0, oe_0 = 1, cs_0 = 1
76 assign data_1 = (cs_1 && oe_1 && ! we_1) ?
data_1_out : 8'bz;
77 // Memory Read Block 1
78 // Read Operation : When we_1 = 0, oe_1 = 1, cs_1 = 1
79 always @ (posedge clk)
80 begin : MEM_READ_1
81
if (cs_1 && ! we_1 && oe_1) begin
82
data_1_out <= mem[address_1];
83
end else begin
84
data_1_out <= 0;
85
end
86 end
87
88 endmodule // End of Module ram_dp_sr_sw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
You can find the rom model and testbench here and
memory_list filehere.
rom_using_case
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 endmodule
Synchronous FIFO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(
91
92
93
94
95
96
97
98
99
100
101
102
103
.address_0
.data_0
.cs_0
.we_0
.oe_0
.address_1
.data_1
.cs_1
.we_1
.oe_1
);
(wr_pointer)
(data_in)
(wr_cs)
(wr_en)
(1'b0)
(rd_pointer)
(data_ram)
(rd_cs)
(1'b0)
(rd_en)
,
,
,
,
,
,
,
,
,
// address_0 input
// data_0 bi-directional
// chip select
// write enable
// output enable
// address_q input
// data_1 bi-directional
// chip select
// Read enable
// output enable
endmodule
Asynchronous FIFO
1 //==========================================
2 // Function : Asynchronous FIFO (w/ 2 asynchronous
clocks).
3 // Coder : Alex Claros F.
4 // Date : 15/May/2005.
5 // Notes : This implementation is based on the article
6 //
'Asynchronous FIFO in Virtex-II FPGAs'
7 //
writen by Peter Alfke. This TechXclusive
8 //
article can be downloaded from the
9 //
Xilinx website. It has some minor
modifications.
10 //=========================================
11
12 `timescale 1ns/1ps
13
14 module aFifo
15
#(parameter
DATA_WIDTH
= 8,
16
ADDRESS_WIDTH = 4,
17
FIFO_DEPTH
= (1 <<
ADDRESS_WIDTH))
18
//Reading port
19
(output reg [DATA_WIDTH-1:0]
Data_out,
20
output reg
Empty_out,
21
input wire
ReadEn_in,
22
input wire
RClk,
23
//Writing port.
24
input wire [DATA_WIDTH-1:0]
Data_in,
25
output reg
Full_out,
26
input wire
WriteEn_in,
27
input wire
WClk,
28
29
input wire
Clear_in);
30
31
// 32
reg
[DATA_WIDTH-1:0]
Mem [FIFO_DEPTH-1:0];
33
wire [ADDRESS_WIDTH-1:0]
pNextWordToWrite, pNextWordToRead;
34
wire
EqualAddresses;
35
wire
NextWriteAddressEn, NextReadAddressEn;
36
wire
Set_Status, Rst_Status;
37
reg
Status;
38
wire
PresetFull, PresetEmpty;
39
40
// 41
//Data ports logic:
42
//(Uses a dual-port RAM).
43
//'Data_out' logic:
44
always @ (posedge RClk)
45
if (ReadEn_in & ! Empty_out)
46
Data_out <=
Mem[pNextWordToRead];
47
48
//'Data_in' logic:
49
always @ (posedge WClk)
50
if (WriteEn_in & ! Full_out)
51
Mem[pNextWordToWrite] <=
Data_in;
52
53
//Fifo addresses support logic:
54
//'Next Addresses' enable logic:
55
assign NextWriteAddressEn = WriteEn_in &
~Full_out;
56
assign NextReadAddressEn = ReadEn_in &
~Empty_out;
57
58
//Addreses (Gray counters) logic:
59
GrayCounter GrayCounter_pWr
60
(.GrayCount_out(pNextWordToWrite),
61
62
.Enable_in(NextWriteAddressEn),
63
.Clear_in(Clear_in),
64
65
.Clk(WClk)
66
);
67
68
GrayCounter GrayCounter_pRd
69
(.GrayCount_out(pNextWordToRead),
70
.Enable_in(NextReadAddressEn),
71
.Clear_in(Clear_in),
72
.Clk(RClk)
73
);
74
75
76
//'EqualAddresses' logic:
77
assign EqualAddresses = (pNextWordToWrite
== pNextWordToRead);
78
79
//'Quadrant selectors' logic:
80
assign Set_Status =
(pNextWordToWrite[ADDRESS_WIDTH-2] ~^
pNextWordToRead[ADDRESS_WIDTH-1]) & 81
(pNextWordToWrite[ADDRESS_WIDTH-1] ^
pNextWordToRead[ADDRESS_WIDTH-2]);
82
83
assign Rst_Status =
(pNextWordToWrite[ADDRESS_WIDTH-2] ^
pNextWordToRead[ADDRESS_WIDTH-1]) & 84
(pNextWordToWrite[ADDRESS_WIDTH-1] ~^
pNextWordToRead[ADDRESS_WIDTH-2]);
85
86
//'Status' latch logic:
87
always @ (Set_Status, Rst_Status,
Clear_in) //D Latch w/ Asynchronous Clear & Preset.
88
if (Rst_Status | Clear_in)
89
Status = 0; //Going 'Empty'.
90
else if (Set_Status)
91
Status = 1; //Going 'Full'.
92
93
//'Full_out' logic for the writing port:
94
assign PresetFull = Status &
EqualAddresses; //'Full' Fifo.
95
96
always @ (posedge WClk, posedge
PresetFull) //D Flip-Flop w/ Asynchronous Preset.
97
if (PresetFull)
98
Full_out <= 1;
99
else
100
Full_out <= 0;
101
102
//'Empty_out' logic for the reading port:
103
assign PresetEmpty = ~Status &
EqualAddresses; //'Empty' Fifo.
104
105
always @ (posedge RClk, posedge
PresetEmpty) //D Flip-Flop w/ Asynchronous Preset.
106
if (PresetEmpty)
107
Empty_out <= 1;
108
else
109
Empty_out <= 0;
110
111 endmodule
1 //==========================================
2 // Function : Code Gray counter.
3 // Coder : Alex Claros F.
4 // Date : 15/May/2005.
5 //=======================================
6
7 `timescale 1ns/1ps
8
9 module GrayCounter
10
#(parameter
COUNTER_WIDTH = 4)
11
12
(output reg [COUNTER_WIDTH-1:0]
GrayCount_out, //'Gray' code count output.
13
14
input wire
Enable_in, //Count enable.
15
input wire
Clear_in,
//Count reset.
16
17
input wire
Clk);
18
19
// 20
reg
[COUNTER_WIDTH-1:0]
BinaryCount;
21
22
// 23
24
always @ (posedge Clk)
25
if (Clear_in) begin
26
BinaryCount
<=
{COUNTER_WIDTH{1'b 0}} + 1; //Gray count begins @ '1'
with
27
GrayCount_out <=
{COUNTER_WIDTH{1'b 0}};
// first 'Enable_in'.
28
end
29
else if (Enable_in) begin
30
BinaryCount
<= BinaryCount + 1;
31
GrayCount_out <=
{BinaryCount[COUNTER_WIDTH-1],
32
BinaryCount[COUNTER_WIDTH-2:0] ^
BinaryCount[COUNTER_WIDTH-1:1]};
33
end
34
35 endmodule
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Parity
Using Assign
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
output parity_out ;
input [7:0] data_in ;
wire parity_out ;
assign parity_out = (data_in[0] ^ data_in[1]) ^
(data_in[2] ^ data_in[3]) ^
(data_in[4] ^ data_in[5]) ^
(data_in[6] ^ data_in[7]);
endmodule
Using function- I
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 endmodule
Using function- II
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Serial CRC
Below code is 16-bit CRC-CCITT implementation, with
following features
Width = 16 bits
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Parallel CRC
Below code is 16-bit CRC-CCITT implementation, with
following features
Width = 16 bits
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Not Gate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
connect;
nmos
nmos
pmos
pmos
(y,ground,a);
(y,ground,b);
(y,connect,b);
(power,connect,a);
endmodule
1
2
3
4
5
6
7
8
9
10
11
12
13
14
module nand_switch(a,b,out);
input a,b;
output out;
supply0 vss;
supply1 vdd;
wire net1;
pmos
pmos
nmos
nmos
p1
p2
n1
n2
(vdd,out,a);
(vdd,out,b);
(vss,net1,a);
(net1,out,b);
endmodule
2:1 Mux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pmos N1
17
nmos N2
18
19
cmos C1
20
cmos C2
21
22 endmodule
D Latch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Transimission Gate
!((a+b+c).d)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// y = !((a+b+c).d)
pmos
pmos
pmos
pmos
p1
p2
p3
p4
(vdd,net1,a);
(net1,net2,b);
(net2,y,c);
(vdd,y,d);
nmos
nmos
nmos
nmos
n1
n2
n3
n4
(vss,net3,a);
(vss,net3,b);
(vss,net3,c);
(net3,y,d);
endmodule
Half Adder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Full Adder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Half Subtracter
Full Subtracter
2:1 Mux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 or U_or (y,asel,bsel);
17
18 endmodule
4:1 Mux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2 To 4 Decoder
1
2
3
4
5
6
7
8
and
and
and
and
9
10
11
12
13
14
a1
a2
a3
a4
(f0,n1,n2);
(f1,n1,y);
(f2,x,n2);
(f3,x,y);
endmodule
4 To 2 Encoder
1
2
3
4
5
6
7
8
or o1 (y[0],i1,i3);
or o2 (y[1],i2,i3);
endmodule
D Latch
1
2
3
4
5
6
7
8
9
10
11
12
module d_latch_gates(d,clk,q,q_bar);
input d,clk;
output q, q_bar;
wire n1,n2,n3;
not (n1,d);
nand (n2,d,clk);
nand (n3,n1,clk);
nand (q,q_bar,n2);
13 nand (q_bar,q,n3);
14
15 endmodule
D Flip Flop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module d_ff_gates(d,clk,q,q_bar);
input d,clk;
output q, q_bar;
wire n1,n2,n3,q_bar_n;
wire cn,dn,n4,n5,n6;
// First Latch
not (n1,d);
nand (n2,d,clk);
nand (n3,n1,clk);
nand (dn,q_bar_n,n2);
nand (q_bar_n,dn,n3);
// Second Latch
not (cn,clk);
not (n4,dn);
nand (n5,dn,cn);
nand (n6,n4,cn);
nand (q,q_bar,n5);
nand (q_bar,q,n6);
endmodule