Skip to content

Commit 4053374

Browse files
committed
Added altera cookbook
1 parent 25b7479 commit 4053374

File tree

450 files changed

+184973
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

450 files changed

+184973
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2007 Altera Corporation. All rights reserved.
2+
// Altera products are protected under numerous U.S. and foreign patents,
3+
// maskwork rights, copyrights and other intellectual property laws.
4+
//
5+
// This reference design file, and your use thereof, is subject to and governed
6+
// by the terms and conditions of the applicable Altera Reference Design
7+
// License Agreement (either as signed by you or found at www.altera.com). By
8+
// using this reference design file, you indicate your acceptance of such terms
9+
// and conditions between you and Altera Corporation. In the event that you do
10+
// not agree with such terms and conditions, you may not use the reference
11+
// design file and please promptly destroy any copies you have made.
12+
//
13+
// This reference design file is being provided on an "as-is" basis and as an
14+
// accommodation and therefore all warranties, representations or guarantees of
15+
// any kind (whether express, implied or statutory) including, without
16+
// limitation, warranties of merchantability, non-infringement, or fitness for
17+
// a particular purpose, are specifically disclaimed. By making this reference
18+
// design file available, Altera expressly does not recommend, suggest or
19+
// require that this reference design file be used in combination with any
20+
// other product not provided by Altera.
21+
/////////////////////////////////////////////////////////////////////////////
22+
23+
24+
// baeckler - 02-13-2007
25+
//
26+
// 'base' is a one hot signal indicating the first request
27+
// that should be considered for a grant. Followed by higher
28+
// indexed requests, then wrapping around.
29+
//
30+
31+
module arbiter (
32+
req, grant, base
33+
);
34+
35+
parameter WIDTH = 16;
36+
37+
input [WIDTH-1:0] req;
38+
output [WIDTH-1:0] grant;
39+
input [WIDTH-1:0] base;
40+
41+
wire [2*WIDTH-1:0] double_req = {req,req};
42+
wire [2*WIDTH-1:0] double_grant = double_req & ~(double_req-base);
43+
assign grant = double_grant[WIDTH-1:0] | double_grant[2*WIDTH-1:WIDTH];
44+
45+
endmodule
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2007 Altera Corporation. All rights reserved.
2+
// Altera products are protected under numerous U.S. and foreign patents,
3+
// maskwork rights, copyrights and other intellectual property laws.
4+
//
5+
// This reference design file, and your use thereof, is subject to and governed
6+
// by the terms and conditions of the applicable Altera Reference Design
7+
// License Agreement (either as signed by you or found at www.altera.com). By
8+
// using this reference design file, you indicate your acceptance of such terms
9+
// and conditions between you and Altera Corporation. In the event that you do
10+
// not agree with such terms and conditions, you may not use the reference
11+
// design file and please promptly destroy any copies you have made.
12+
//
13+
// This reference design file is being provided on an "as-is" basis and as an
14+
// accommodation and therefore all warranties, representations or guarantees of
15+
// any kind (whether express, implied or statutory) including, without
16+
// limitation, warranties of merchantability, non-infringement, or fitness for
17+
// a particular purpose, are specifically disclaimed. By making this reference
18+
// design file available, Altera expressly does not recommend, suggest or
19+
// require that this reference design file be used in combination with any
20+
// other product not provided by Altera.
21+
/////////////////////////////////////////////////////////////////////////////
22+
23+
24+
//baeckler - 02-13-2007
25+
26+
module arbiter_tb ();
27+
28+
reg [15:0] req;
29+
reg [3:0] base;
30+
31+
wire [15:0] grant, grant_two;
32+
reg fail;
33+
34+
// weaker unit for testing
35+
reference_arbiter arb (.req(req),.base(base),.grant(grant));
36+
37+
// convert the encoded base to one hot
38+
// ideally it would be generated in one hot
39+
reg [15:0] decoded_base;
40+
always @(*) begin
41+
decoded_base = 0;
42+
decoded_base[base] = 1'b1;
43+
end
44+
45+
// device under test
46+
arbiter a2 (.req(req),.grant(grant_two),.base(decoded_base));
47+
defparam a2 .WIDTH = 16;
48+
49+
always begin
50+
#100
51+
req = $random & $random & $random;
52+
base = $random;
53+
54+
#5
55+
if (grant !== grant_two) begin
56+
$display ("Mismatch at time %d",$time);
57+
fail = 1;
58+
end
59+
end
60+
61+
initial begin
62+
fail = 0;
63+
#1000000 if (!fail) $display ("PASS");
64+
$stop();
65+
end
66+
67+
endmodule
68+
69+
70+
/////////////////////////////////////////
71+
// Less efficient easier to understand
72+
// unit for reference
73+
/////////////////////////////////////////
74+
module reference_arbiter (
75+
req,grant,base
76+
);
77+
78+
input [15:0] req;
79+
output [15:0] grant;
80+
input [3:0] base;
81+
82+
// rotate the request lines
83+
wire [15:0] b0 = base[0] ? {req[0],req[15:1]} : req[15:0] ;
84+
wire [15:0] b1 = base[1] ? {b0[1:0],b0[15:2]} : b0[15:0] ;
85+
wire [15:0] b2 = base[2] ? {b1[3:0],b1[15:4]} : b1[15:0] ;
86+
wire [15:0] b3 = base[3] ? {b2[7:0],b2[15:8]} : b2[15:0] ;
87+
88+
// pick the lowest one for a grant
89+
wire [15:0] rotated_grant = b3 & ~(b3-1);
90+
91+
// unrotate the grant
92+
wire [15:0] b4 = base[0] ? {rotated_grant[14:0],rotated_grant[15]} : rotated_grant[15:0] ;
93+
wire [15:0] b5 = base[1] ? {b4[13:0],b4[15:14]} : b4[15:0] ;
94+
wire [15:0] b6 = base[2] ? {b5[11:0],b5[15:12]} : b5[15:0] ;
95+
wire [15:0] b7 = base[3] ? {b6[7:0],b6[15:8]} : b6[15:0] ;
96+
97+
assign grant = b7;
98+
99+
endmodule
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2007 Altera Corporation. All rights reserved.
2+
// Altera products are protected under numerous U.S. and foreign patents,
3+
// maskwork rights, copyrights and other intellectual property laws.
4+
//
5+
// This reference design file, and your use thereof, is subject to and governed
6+
// by the terms and conditions of the applicable Altera Reference Design
7+
// License Agreement (either as signed by you or found at www.altera.com). By
8+
// using this reference design file, you indicate your acceptance of such terms
9+
// and conditions between you and Altera Corporation. In the event that you do
10+
// not agree with such terms and conditions, you may not use the reference
11+
// design file and please promptly destroy any copies you have made.
12+
//
13+
// This reference design file is being provided on an "as-is" basis and as an
14+
// accommodation and therefore all warranties, representations or guarantees of
15+
// any kind (whether express, implied or statutory) including, without
16+
// limitation, warranties of merchantability, non-infringement, or fitness for
17+
// a particular purpose, are specifically disclaimed. By making this reference
18+
// design file available, Altera expressly does not recommend, suggest or
19+
// require that this reference design file be used in combination with any
20+
// other product not provided by Altera.
21+
/////////////////////////////////////////////////////////////////////////////
22+
23+
module bitscan (req,sel);
24+
parameter WIDTH = 16;
25+
input [WIDTH-1:0] req;
26+
output [WIDTH-1:0] sel;
27+
28+
assign sel = req & ~(req-1);
29+
30+
endmodule
31+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2007 Altera Corporation. All rights reserved.
2+
// Altera products are protected under numerous U.S. and foreign patents,
3+
// maskwork rights, copyrights and other intellectual property laws.
4+
//
5+
// This reference design file, and your use thereof, is subject to and governed
6+
// by the terms and conditions of the applicable Altera Reference Design
7+
// License Agreement (either as signed by you or found at www.altera.com). By
8+
// using this reference design file, you indicate your acceptance of such terms
9+
// and conditions between you and Altera Corporation. In the event that you do
10+
// not agree with such terms and conditions, you may not use the reference
11+
// design file and please promptly destroy any copies you have made.
12+
//
13+
// This reference design file is being provided on an "as-is" basis and as an
14+
// accommodation and therefore all warranties, representations or guarantees of
15+
// any kind (whether express, implied or statutory) including, without
16+
// limitation, warranties of merchantability, non-infringement, or fitness for
17+
// a particular purpose, are specifically disclaimed. By making this reference
18+
// design file available, Altera expressly does not recommend, suggest or
19+
// require that this reference design file be used in combination with any
20+
// other product not provided by Altera.
21+
/////////////////////////////////////////////////////////////////////////////
22+
23+
24+
module bitscan_tb ();
25+
parameter WIDTH = 16;
26+
reg [WIDTH-1:0] req;
27+
wire [WIDTH-1:0] sel;
28+
29+
bitscan b (.req(req),.sel(sel));
30+
defparam b .WIDTH = WIDTH;
31+
32+
initial begin
33+
req = 16'h8000;
34+
end
35+
36+
integer n;
37+
reg [WIDTH-1:0] result;
38+
reg fail = 0;
39+
40+
always begin
41+
#100 req = $random & $random & $random;
42+
#10
43+
result = 0;
44+
for (n=0; n<WIDTH; n=n+1)
45+
begin
46+
if (req[n] == 1'b1) begin
47+
result[n] = 1'b1;
48+
n = WIDTH;
49+
end
50+
end
51+
#10 if (sel !== result) begin
52+
$display ("Mismatch at time %d",$time);
53+
fail = 1'b1;
54+
$stop();
55+
end
56+
end
57+
58+
initial begin
59+
#1000000 if (!fail) $display ("PASS");
60+
$stop();
61+
end
62+
63+
endmodule
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2007 Altera Corporation. All rights reserved.
2+
// Altera products are protected under numerous U.S. and foreign patents,
3+
// maskwork rights, copyrights and other intellectual property laws.
4+
//
5+
// This reference design file, and your use thereof, is subject to and governed
6+
// by the terms and conditions of the applicable Altera Reference Design
7+
// License Agreement (either as signed by you or found at www.altera.com). By
8+
// using this reference design file, you indicate your acceptance of such terms
9+
// and conditions between you and Altera Corporation. In the event that you do
10+
// not agree with such terms and conditions, you may not use the reference
11+
// design file and please promptly destroy any copies you have made.
12+
//
13+
// This reference design file is being provided on an "as-is" basis and as an
14+
// accommodation and therefore all warranties, representations or guarantees of
15+
// any kind (whether express, implied or statutory) including, without
16+
// limitation, warranties of merchantability, non-infringement, or fitness for
17+
// a particular purpose, are specifically disclaimed. By making this reference
18+
// design file available, Altera expressly does not recommend, suggest or
19+
// require that this reference design file be used in combination with any
20+
// other product not provided by Altera.
21+
/////////////////////////////////////////////////////////////////////////////
22+
23+
// baeckler - 12-12-2006
24+
// helper function to compute LOG base 2
25+
//
26+
// NOTE - This is a somewhat abusive definition of LOG2(v) as the
27+
// number of bits required to represent "v". So log2(256) will be
28+
// 9 rather than 8 (256 = 9'b1_0000_0000). I apologize for any
29+
// confusion this may cause.
30+
//
31+
32+
function integer log2;
33+
input integer val;
34+
begin
35+
log2 = 0;
36+
while (val > 0) begin
37+
val = val >> 1;
38+
log2 = log2 + 1;
39+
end
40+
end
41+
endfunction
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2007 Altera Corporation. All rights reserved.
2+
// Altera products are protected under numerous U.S. and foreign patents,
3+
// maskwork rights, copyrights and other intellectual property laws.
4+
//
5+
// This reference design file, and your use thereof, is subject to and governed
6+
// by the terms and conditions of the applicable Altera Reference Design
7+
// License Agreement (either as signed by you or found at www.altera.com). By
8+
// using this reference design file, you indicate your acceptance of such terms
9+
// and conditions between you and Altera Corporation. In the event that you do
10+
// not agree with such terms and conditions, you may not use the reference
11+
// design file and please promptly destroy any copies you have made.
12+
//
13+
// This reference design file is being provided on an "as-is" basis and as an
14+
// accommodation and therefore all warranties, representations or guarantees of
15+
// any kind (whether express, implied or statutory) including, without
16+
// limitation, warranties of merchantability, non-infringement, or fitness for
17+
// a particular purpose, are specifically disclaimed. By making this reference
18+
// design file available, Altera expressly does not recommend, suggest or
19+
// require that this reference design file be used in combination with any
20+
// other product not provided by Altera.
21+
/////////////////////////////////////////////////////////////////////////////
22+
23+
//baeckler - 11-14-2006
24+
25+
#include <stdio.h>
26+
27+
int log2 (int n)
28+
{
29+
int bits = 0;
30+
while (n)
31+
{
32+
n >>= 1;
33+
bits++;
34+
}
35+
return (bits);
36+
}
37+
38+
int main (void)
39+
{
40+
unsigned int num_ins = 6;
41+
unsigned int num_outs = log2(num_ins);
42+
43+
unsigned int num_cases = (1<<num_ins);
44+
unsigned int n = 0, k = 0;
45+
unsigned int out_val = 0;
46+
47+
fprintf (stdout,"//baeckler - 11-14-2006\n");
48+
fprintf (stdout,"// priority encoder\n");
49+
fprintf (stdout,"// no requests - output = 0\n");
50+
fprintf (stdout,"// request bit 0 (highest priority) - output = 1\n");
51+
fprintf (stdout,"// request bit %d (lowest priority) - output = %d\n",num_ins-1,num_ins);
52+
fprintf (stdout,"module prio_encode (reqs,out);\n");
53+
fprintf (stdout,"input [%d:0] reqs;\n",num_ins-1);
54+
fprintf (stdout,"output [%d:0] out;\n",num_outs-1);
55+
fprintf (stdout,"reg [%d:0] out;\n\n",num_outs-1);
56+
57+
fprintf (stdout," always @(*) begin\n");
58+
fprintf (stdout," case(reqs)\n");
59+
60+
fprintf (stdout," // 0 is special, no reqs\n");
61+
fprintf (stdout," %d'd%d: out = %d;\n\n",num_ins,0,0);
62+
63+
for (n=1; n<num_cases; n++)
64+
{
65+
out_val = 1;
66+
k = n;
67+
while (k && !(k&1))
68+
{
69+
k >>=1;
70+
out_val += 1;
71+
}
72+
fprintf (stdout," %d'd%d: out = %d;\n",num_ins,n,out_val);
73+
}
74+
fprintf (stdout," endcase\n");
75+
fprintf (stdout," end\n");
76+
fprintf (stdout,"endmodule\n");
77+
return (0);
78+
}

0 commit comments

Comments
 (0)