Verilog Examples
Verilog Examples
Synchronous reset D- FF
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
Asynchronous reset T - FF
Synchronous reset T - FF
Regular D Latch
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 == {{`WIDTH1{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}};
27 else if (enable) begin
28
if (up_down) begin
29
count
<=
{~(^(count
&
`WIDTH'b01100011)),count[`WIDTH-1:1]};
30
end else begin
31
count <= {count[`WIDTH-2:0],~(^(count &
`WIDTH'b10110001))};
32
end
33 end
34
35 endmodule
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 == {{`WIDTH1{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}};
27 else if (enable) begin
28
if (up_down) begin
29
count
<=
{~(^(count
&
`WIDTH'b01100011)),count[`WIDTH-1:1]};
30
end else begin
31
count <= {count[`WIDTH-2:0],~(^(count &
`WIDTH'b10110001))};
32
end
33 end
34
35 endmodule
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
22
always @ (posedge clk)
23
if (rst)
24
count <= 0;
25
else if (enable)
26
count <= count + 1;
27
28
assign out = { count[7], (count[7] ^ count[6]),
(count[6] ^
29
count[5]),(count[5] ^ count[4]),
(count[4] ^
30
count[3]),(count[3] ^ count[2]),
(count[2] ^
31
count[1]),(count[1] ^ count[0]) };
32
33 endmodule
Divide by 2 Counter
Divide By 3 Counter
This module divides the input clock frequency by 3
7 module divide_by_3 (
8 clk_in
, //Input Clock
9 reset
, // Reset Input
10 clk_out
// Output Clock
11 );
12 //-----------Input Ports--------------13 input clk_in;
14 input reset;
15 //-----------Output Ports--------------16 output clk_out;
17 //------------Internal Variables-------18 reg [1:0] pos_cnt;
19 reg [1:0] neg_cnt;
20 //-------------Code Start----------------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
! =
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
input
oe
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
parameter ADDR_WIDTH = 8 ;
parameter RAM_DEPTH = 1 << ADDR_WIDTH;
//--------------Input Ports----------------------input [ADDR_WIDTH-1:0] address ;
input
cs
input
we
input
oe
-7 module ram_dp_sr_sw (
8 clk
, // Clock Input
9 address_0 , // address_0 Input
10 data_0
, // data_0 bi-directional
11 cs_0
, // Chip Select
12 we_0
, // Write Enable/Read Enable
13 oe_0
, // Output Enable
14 address_1 , // address_1 Input
15 data_1
, // data_1 bi-directional
16 cs_1
, // Chip Select
17 we_1
, // Write Enable/Read Enable
18 oe_1
// Output Enable
19 );
20
21 parameter data_0_WIDTH = 8 ;
22 parameter ADDR_WIDTH = 8 ;
23 parameter RAM_DEPTH = 1 << ADDR_WIDTH;
24
25 //--------------Input Ports----------------------26 input [ADDR_WIDTH-1:0] address_0 ;
27 input cs_0 ;
28 input we_0 ;
29 input oe_0 ;
30 input [ADDR_WIDTH-1:0] address_1 ;
31 input cs_1 ;
32 input we_1 ;
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
20 parameter DATA_WIDTH = 8 ;
21 parameter ADDR_WIDTH = 8 ;
22 parameter RAM_DEPTH = 1 << ADDR_WIDTH;
23
24 //--------------Input Ports----------------------25 input [ADDR_WIDTH-1:0] address_0 ;
26 input cs_0 ;
27 input we_0 ;
28 input oe_0 ;
29 input [ADDR_WIDTH-1:0] address_1 ;
30 input cs_1 ;
31 input we_1 ;
32 input oe_1 ;
33
34 //--------------Inout Ports----------------------35 inout [DATA_WIDTH-1:0] data_0 ;
36 inout [DATA_WIDTH-1:0] data_1 ;
37
38 //--------------Internal variables---------------39 reg [DATA_WIDTH-1:0] data_0_out ;
40 reg [DATA_WIDTH-1:0] data_1_out ;
41 reg [DATA_WIDTH-1:0] mem [0:RAM_DEPTH-1];
42
43 //--------------Code Starts Here-----------------44 // Memory Write Block
45 // Write Operation : When we_0 = 1, cs_0 = 1
46 always @ (address_0 or cs_0 or we_0 or data_0
47 or address_1 or cs_1 or we_1 or data_1)
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 // Tri-State Buffer control
57 // output : When we_0 = 0, oe_0 = 1, cs_0 = 1
58 assign data_0 = (cs_0 && oe_0 &&
! we_0) ?
data_0_out : 8'bz;
59
60 // Memory Read Block
61 // Read Operation : When we_0 = 0, oe_0 = 1, cs_0 =
1
62 always @ (address_0 or cs_0 or we_1 or oe_0)
63 begin : MEM_READ_0
64
if (cs_0 && ! we_0 && oe_0) begin
65
data_0_out <= mem[address_0];
66
end else begin
67
data_0_out <= 0;
68
end
69 end
70
71 //Second Port of RAM
72 // Tri-State Buffer control
73 // output : When we_0 = 0, oe_0 = 1, cs_0 = 1
74 assign data_1 = (cs_1 && oe_1 &&
! we_1) ?
data_1_out : 8'bz;
75 // Memory Read Block 1
76 // Read Operation : When we_1 = 0, oe_1 = 1, cs_1 =
1
77 always @ (address_1 or cs_1 or we_1 or oe_1)
78 begin : MEM_READ_1
79
if (cs_1 && ! we_1 && oe_1) begin
80
data_1_out <= mem[address_1];
81
end else begin
82
data_1_out <= 0;
83
end
84 end
85
86 endmodule // End of Module ram_dp_ar_aw
You can find the rom model and testbench here and memory_list file
here.
rom_using_case
41
42 endmodule
Synchronous FIFO
46
47 //-----------Code Start--------------------------48 always @ (posedge clk or posedge rst)
49 begin : WRITE_POINTER
50
if (rst) begin
51
wr_pointer <= 0;
52
end else if (wr_cs && wr_en ) begin
53
wr_pointer <= wr_pointer + 1;
54
end
55 end
56
57 always @ (posedge clk or posedge rst)
58 begin : READ_POINTER
59
if (rst) begin
60
rd_pointer <= 0;
61
end else if (rd_cs && rd_en ) begin
62
rd_pointer <= rd_pointer + 1;
63
end
64 end
65
66 always @ (posedge clk or posedge rst)
67 begin : READ_DATA
68
if (rst) begin
69
data_out <= 0;
70
end else if (rd_cs && rd_en ) begin
71
data_out <= data_ram;
72
end
73 end
74
75 always @ (posedge clk or posedge rst)
76 begin : STATUS_COUNTER
77
if (rst) begin
78
status_cnt <= 0;
79
// Read but no write.
80
end else if ((rd_cs && rd_en) &&
! (wr_cs &&
wr_en)
81
&& (status_cnt ! = 0)) begin
82
status_cnt <= status_cnt - 1;
83
// Write but no read.
84
end else if ((wr_cs && wr_en) &&
! (rd_cs &&
rd_en)
85
&& (status_cnt ! = RAM_DEPTH)) begin
86
status_cnt <= status_cnt + 1;
87
end
88 end
89
90 ram_dp_ar_aw #(DATA_WIDTH,ADDR_WIDTH)DP_RAM (
91 .address_0 (wr_pointer) , // address_0 input
92 .data_0
(data_in)
, // data_0 bi-directional
93 .cs_0
(wr_cs)
, // chip select
94 .we_0
(wr_en)
, // write enable
95 .oe_0
(1'b0)
, // output enable
96 .address_1 (rd_pointer) , // address_q input
97 .data_1
(data_ram)
, // data_1 bi-directional
98 .cs_1
(rd_cs)
, // chip select
99 .we_1
(1'b0)
, // Read enable
100 .oe_1
(rd_en)
// output enable
101 );
102
103 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
PARITY
48
49
50
51
52
53
54
55
56
57
58
59
60
Using Assign
Using function- I
Using function- II
9 parity_out
// 1 bit parity out
10 );
11 output parity_out ;
12 input [7:0] data_in ;
13
14 wire parity_out ;
15 function parity;
16
input [31:0] data;
17
integer i;
18
begin
19
parity = 0;
20
for (i = 0; i < 32; i = i + 1) begin
21
parity = parity ^ data[i];
22
end
23
end
24 endfunction
25
26 always @ (data_in)
27 begin
28
parity_out = parity(data_in);
29 end
30
31 endmodule
SERIAL CRC
Width = 16 bits
Truncated polynomial =
0x1021
Input data
reflected
No XOR is performed
on the output CRC
is
NOT
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
if (reset) begin
lfsr <= 16'hFFFF;
end else if (enable) begin
if (init) begin
lfsr <= 16'hFFFF;
end else begin
lfsr[0] <= data_in ^ lfsr[15];
lfsr[1] <= lfsr[0];
lfsr[2] <= lfsr[1];
lfsr[3] <= lfsr[2];
lfsr[4] <= lfsr[3];
lfsr[5] <= lfsr[4] ^ data_in ^ lfsr[15];
lfsr[6] <= lfsr[5];
lfsr[7] <= lfsr[6];
lfsr[8] <= lfsr[7];
lfsr[9] <= lfsr[8];
lfsr[10] <= lfsr[9];
lfsr[11] <= lfsr[10];
lfsr[12] <= lfsr[11] ^ data_in ^ lfsr[15];
lfsr[13] <= lfsr[12];
lfsr[14] <= lfsr[13];
lfsr[15] <= lfsr[14];
end
end
endmodule
Parallel CRC
Below code is 16-bit CRCCCITT implementation, with
following features
Width = 16 bits
Truncated polynomial =
0x1021
Input data
reflected
is
NOT
No XOR is performed
on the output CRC
52
53 endmodule
Not Gate
1 module nand_switch(a,b,out);
2 input a,b;
3 output out;
4
5 supply0 vss;
6 supply1 vdd;
7 wire net1;
8
9 pmos p1 (vdd,out,a);
10 pmos p2 (vdd,out,b);
11 nmos n1 (vss,net1,a);
12 nmos n2 (net1,out,b);
13
14 endmodule
2:1 Mux
D Latch
Transimission Gate
1
module
t_gate_switch
(L,R,nC,C);
2 inout L;
3 inout R;
4 input nC;
5 input C;
6
7
//Syntax:
keyword
unique_name
(drain.
source, gate);
8 pmos p1 (L,R,nC);
9 nmos p2 (L,R,C);
10
11 endmodule
file
!((a+b+c).d)
1
module
misc1
(a,b,c,d,y);
2 input a, b,c,d;
3 output y;
4
5 wire net1,net2,net3;
6
7 supply1 vdd;
8 supply0 vss;
9
10 // y = !((a+b+c).d)
11
12 pmos p1 (vdd,net1,a);
13 pmos p2 (net1,net2,b);
14 pmos p3 (net2,y,c);
15 pmos p4 (vdd,y,d);
16
17 nmos n1 (vss,net3,a);
18 nmos n2 (vss,net3,b);
19 nmos n3 (vss,net3,c);
20 nmos n4 (net3,y,d);
21
22 endmodule
Half Adder
10
11 and U_carry (carry,x,y);
12 xor U_sum (sum,x,y);
13
14 endmodule
Full Adder
Full Subtracter
2:1 Mux
13
14
15
16
17
18
19
20
not (inv_x,x);
and U_borrow1 (borrow1,inv_x,y),
U_borrow2 (borrow2,inv_x,z),
U_borrow3 (borrow3,y,z);
xor U_diff (difference,borrow1,borrow2,borrows);
endmodule
4:1 Mux
12 wire mux_1,mux_2;
13
14 mux_2to1_gates U_mux1 (a,b,sel[0],mux_1),
15
U_mux2 (c,d,sel[0],mux_2),
16
U_mux3 (mux_1,mux_2,sel[1],y);
17
18 endmodule
2 To 4 Decoder
Two Input OR
1
module
decoder_2to4_gates
(x,y,f0,f1,f2,f3);
2 input x,y;
3 output f0,f1,f2,f3;
4
5 wire n1,n2;
6
7 not i1 (n1,x);
8 not i2 (n2,y);
9 and a1 (f0,n1,n2);
10 and a2 (f1,n1,y);
11 and a3 (f2,x,n2);
12 and a4 (f3,x,y);
13
14 endmodule
2:1 Mux
1
primitive
mux_21_udp(out, sel, i0,
i1);
2 output out;
3 input sel, i0, i1;
4 table
5
// sel i0 i1
out
6
0
0
?
:
0 ; // 1
7
0
1
?
:
1 ; // 2
8
1
?
0
:
0 ; // 3
9
1
?
1
:
1 ; // 4
10
?
0
0
:
0 ; // 5
11
?
1
1
:
1 ; // 6
12 endtable
13 endprimitive
D Latch
D Flip Flop
SR Flip Flop
JK Flip Flop
21
endtable
22 endprimitive
Verilog Code
1
2
3
4
5
6
7
8
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
input
tx_enable
output
tx_out
output
tx_empty
input
rxclk
input
uld_rx_data
output [7:0] rx_data
input
rx_enable
input
rx_in
output
rx_empty
;
;
;
;
;
;
;
;
;
// Internal Variables
reg [7:0]
tx_reg
reg
tx_empty
reg
tx_over_run
reg [3:0]
tx_cnt
reg
tx_out
reg [7:0]
rx_reg
reg [7:0]
rx_data
reg [3:0]
rx_sample_cnt
reg [3:0]
rx_cnt
reg
rx_frame_err
reg
rx_over_run
reg
rx_empty
reg
rx_d1
reg
rx_d2
reg
rx_busy
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
// UART RX Logic
always @ (posedge rxclk or posedge reset)
if (reset) begin
rx_reg
<= 0;
rx_data
<= 0;
rx_sample_cnt <= 0;
rx_cnt
<= 0;
rx_frame_err <= 0;
rx_over_run
<= 0;
rx_empty
<= 1;
rx_d1
<= 1;
rx_d2
<= 1;
rx_busy
<= 0;
end else begin
// Synchronize the asynch signal
rx_d1 <= rx_in;
rx_d2 <= rx_d1;
// Uload the rx data
if (uld_rx_data) begin
rx_data <= rx_reg;
rx_empty <= 1;
end
// Receive data only when rx is enabled
if (rx_enable) begin
// Check if just received start of frame
if ( ! rx_busy && ! rx_d2) begin
rx_busy
<= 1;
rx_sample_cnt <= 1;
rx_cnt
<= 0;
end
84
// Start of frame detected, Proceed with rest of
data
85
if (rx_busy) begin
86
rx_sample_cnt <= rx_sample_cnt + 1;
87
// Logic to sample at middle of data
88
if (rx_sample_cnt == 7) begin
89
if ((rx_d2 == 1) && (rx_cnt == 0)) begin
90
rx_busy <= 0;
91
end else begin
92
rx_cnt <= rx_cnt + 1;
93
// Start storing the rx data
94
if (rx_cnt > 0 && rx_cnt < 9) begin
95
rx_reg[rx_cnt - 1] <= rx_d2;
96
end
97
if (rx_cnt == 9) begin
98
rx_busy <= 0;
99
// Check if End of frame received
correctly
100
if (rx_d2 == 0) begin
101
rx_frame_err <= 1;
102
end else begin
103
rx_empty
<= 0;
104
rx_frame_err <= 0;
105
// Check if last rx data was not
unloaded,
106
rx_over_run <= (rx_empty) ? 0 :
1;
107
end
108
end
109
end
110
end
111
end
112
end
113
if ( ! rx_enable) begin
114
rx_busy <= 0;
115
end
116 end
117
118 // UART TX Logic
119 always @ (posedge txclk or posedge reset)
120 if (reset) begin
121
tx_reg
<= 0;
122
tx_empty
<= 1;
123
tx_over_run
<= 0;
124
tx_out
<= 1;
125
tx_cnt
<= 0;
126 end else begin
127
if (ld_tx_data) begin
128
if ( ! tx_empty) begin
129
tx_over_run <= 0;
130
end else begin
131
tx_reg
<= tx_data;
132
tx_empty <= 0;
133
end
134
end
135
if (tx_enable && ! tx_empty) begin
136
tx_cnt <= tx_cnt + 1;
137
if (tx_cnt == 0) begin
138
tx_out <= 0;
139
end
140
if (tx_cnt > 0 && tx_cnt < 9) begin
141
tx_out <= tx_reg[tx_cnt -1];
142
end
143
if (tx_cnt == 9) begin
144
tx_out <= 1;
145
tx_cnt <= 0;
146
tx_empty <= 1;
147
end
148
end
149
if ( ! tx_enable) begin
150
tx_cnt <= 0;
151
end
152 end
153
154 endmodule
30 wire
[1:0]
gnt
;
31 wire
comreq
;
32 wire
beg
;
33 wire
[1:0]
lgnt
;
34 wire
lcomreq
;
35 reg
lgnt0
;
36 reg
lgnt1
;
37 reg
lgnt2
;
38 reg
lgnt3
;
39 reg
lasmask
;
40 reg
lmask0
;
41 reg
lmask1
;
42 reg
ledge
;
43
44
//--------------Code
Starts
Here----------------------45 always @ (posedge clk)
46 if (rst) begin
47
lgnt0 <= 0;
48
lgnt1 <= 0;
49
lgnt2 <= 0;
50
lgnt3 <= 0;
51 end else begin
52
lgnt0 <=(~lcomreq & ~lmask1 & ~lmask0 & ~req3 &
~req2 & ~req1 & req0)
53
| (~lcomreq & ~lmask1 & lmask0 & ~req3 &
~req2 & req0)
54
| (~lcomreq & lmask1 & ~lmask0 & ~req3 &
req0)
55
| (~lcomreq & lmask1 & lmask0 & req0 )
56
| ( lcomreq & lgnt0 );
57
lgnt1 <=(~lcomreq & ~lmask1 & ~lmask0 & req1)
58
| (~lcomreq & ~lmask1 & lmask0 & ~req3 &
~req2 & req1 & ~req0)
59
| (~lcomreq & lmask1 & ~lmask0 & ~req3 &
req1 & ~req0)
60
| (~lcomreq & lmask1 & lmask0 & req1 &
~req0)
61
| ( lcomreq & lgnt1);
62
lgnt2 <=(~lcomreq & ~lmask1 & ~lmask0 & req2 &
~req1)
63
| (~lcomreq & ~lmask1 & lmask0 & req2)
64
| (~lcomreq & lmask1 & ~lmask0 & ~req3 &
req2 & ~req1 & ~req0)
65
| (~lcomreq & lmask1 & lmask0 & req2 &
~req1 & ~req0)
66
| ( lcomreq & lgnt2);
67
lgnt3 <=(~lcomreq & ~lmask1 & ~lmask0 & req3 &
~req2 & ~req1)
68
| (~lcomreq & ~lmask1 & lmask0 & req3 &
~req2)
69
| (~lcomreq & lmask1 & ~lmask0 & req3)
70
| (~lcomreq & lmask1 & lmask0 & req3 &
~req2 & ~req1 & ~req0)
71
| ( lcomreq & lgnt3);
72 end
73
= lgnt1;
= lgnt0;
Testbench Code
1 `include "arbiter.v"
2 module top ();
3
4 reg
clk;
5 reg
rst;
6 reg
req3;
7 reg
req2;
8 reg
req1;
9 reg
req0;
10 wire
gnt3;
11 wire
gnt2;
12 wire
gnt1;
13 wire
gnt0;
14
15 // Clock generator
16 always #1 clk = ~clk;
17
18 initial begin
19
$dumpfile ("arbiter.vcd");
20
$dumpvars();
21
clk = 0;
22
rst = 1;
23
req0 = 0;
24
req1 = 0;
25
req2 = 0;
26
req3 = 0;
27
#10 rst = 0;
28
repeat (1) @ (posedge clk);
29
req0 <= 1;
30
repeat (1) @ (posedge clk);
31
req0 <= 0;
32
repeat (1) @ (posedge clk);
33
req0 <= 1;
34
req1 <= 1;
35
repeat (1) @ (posedge clk);
36
req2 <= 1;
37
req1 <= 0;
38
repeat (1) @ (posedge clk);
39
req3 <= 1;
40
req2 <= 0;
41
repeat (1) @ (posedge clk);
42
req3 <= 0;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
29
binary_out
30
end else if
31
binary_out
32
end else if
33
binary_out
34
end else if
35
binary_out
36
end else if
37
binary_out
38
end else if
39
binary_out
40
end else if
41
binary_out
42
end else if
43
binary_out
44
end else if
45
binary_out
46
end else if
47
binary_out
48
end else if
49
binary_out
50
end else if
51
binary_out
52
end
53
end
54 end
55
56 endmodule
= 4;
(encoder_in[4] == 1) begin
= 5;
(encoder_in[5] == 1) begin
= 6;
(encoder_in[6] == 1) begin
= 7;
(encoder_in[7] == 1) begin
= 8;
(encoder_in[8] == 1) begin
= 9;
(encoder_in[9] == 1) begin
= 10;
(encoder_in[10] == 1) begin
= 11;
(encoder_in[11] == 1) begin
= 12;
(encoder_in[12] == 1) begin
= 13;
(encoder_in[13] == 1) begin
= 14;
(encoder_in[14] == 1) begin
= 15;
28
4'h5 :
29
4'h6 :
30
4'h7 :
31
4'h8 :
32
4'h9 :
33
4'hA :
34
4'hB :
35
4'hC :
36
4'hD :
37
4'hE :
38
4'hF :
39
endcase
40
end
41 end
42
43 endmodule
decoder_out
decoder_out
decoder_out
decoder_out
decoder_out
decoder_out
decoder_out
decoder_out
decoder_out
decoder_out
decoder_out
=
=
=
=
=
=
=
=
=
=
=
16'h0020;
16'h0040;
16'h0080;
16'h0100;
16'h0200;
16'h0400;
16'h0800;
16'h1000;
16'h2000;
16'h4000;
16'h8000;
1 //----------------------------------------------------
19
20
21
22
23
24
25
26
27
28
29
52
end if (encoder_in == 16'h8000) begin
53
binary_out = 15;
54
end
55
end
56 end
57
58 endmodule
38
endcase
39
end
40 end
41
42 endmodule