Content-Length: 918255 | pFad | http://github.com/karaketir16/project-euler-verilog/commit/72db6a0b065f93c1d9aedc95caebe696a6e7ddc4

5E problem 11 solved · karaketir16/project-euler-verilog@72db6a0 · GitHub
Skip to content

Commit 72db6a0

Browse files
committed
problem 11 solved
1 parent 9e37fc7 commit 72db6a0

File tree

12 files changed

+433
-12
lines changed

12 files changed

+433
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ As a computer engineer learning digital design, I wanted a fun and practical way
2424
| Problem 7 | Solved | Verified in simulation |
2525
| Problem 8 | Solved | Verified in simulation |
2626
| Problem 9 | Solved | Verified in simulation |
27-
| Problem 10 | Solved | Verified in simulation |
27+
| Problem 10 | Solved | Verified in simulation |
28+
| Problem 11 | Solved | Verified in simulation |
2829

2930
---
3031

dsim/p0011.mem

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
08 02 16 61 26 0F 00 28 00 4B 04 05 07 4E 34 0C 32 4D 5B 08
2+
31 31 63 28 11 51 12 39 3C 57 11 28 62 2B 45 30 04 38 3E 00
3+
51 31 1F 49 37 4F 0E 1D 5D 47 28 43 35 58 1E 03 31 0D 24 41
4+
34 46 5F 17 04 3C 0B 2A 45 18 44 38 01 20 38 47 25 02 24 5B
5+
16 1F 10 47 33 43 3F 59 29 5C 24 36 16 28 28 1C 42 21 0D 50
6+
18 2F 20 3C 63 03 2D 02 2C 4B 21 35 4E 24 54 14 23 11 0C 32
7+
20 62 51 1C 40 17 43 0A 1A 26 28 43 3B 36 46 42 12 26 40 46
8+
43 1A 14 44 02 3E 0C 14 5F 3F 5E 27 3F 08 28 5B 42 31 5E 15
9+
18 37 3A 05 42 49 63 1A 61 11 4E 4E 60 53 0E 58 22 59 3F 48
10+
15 24 17 09 4B 00 4C 2C 14 2D 23 0E 00 3D 21 61 22 1F 21 5F
11+
4E 11 35 1C 16 4B 1F 43 0F 5E 03 50 04 3E 10 0E 09 35 38 5C
12+
10 27 05 2A 60 23 1F 2F 37 3A 58 18 00 11 36 18 24 1D 55 39
13+
56 38 00 30 23 47 59 07 05 2C 2C 25 2C 3C 15 3A 33 36 11 3A
14+
13 50 51 44 05 5E 2F 45 1C 49 5C 0D 56 34 11 4D 04 59 37 28
15+
04 34 08 53 61 23 63 10 07 61 39 20 10 1A 1A 4F 21 1B 62 42
16+
58 24 44 57 39 3E 14 48 03 2E 21 43 2E 37 0C 20 3F 5D 35 45
17+
04 2A 10 49 26 19 27 0B 18 5E 48 12 08 2E 1D 20 28 3E 4C 24
18+
14 45 24 29 48 1E 17 58 22 3E 63 45 52 43 3B 55 4A 04 24 10
19+
14 49 23 1D 4E 1F 5A 01 4A 1F 31 47 30 56 51 10 17 39 05 36
20+
01 46 36 47 53 33 36 45 10 5C 21 30 3D 2B 34 01 59 13 43 30

dsim/project_euler.dpf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ simulations:
2727
options: '-top work.p0009_tb +acc+b -waves waves_p0009.mxd'
2828
- name: p0010
2929
options: '-top work.p0010_tb +acc+b -waves waves_p0010.mxd'
30+
- name: p0011
31+
options: '-top work.p0011_tb +acc+b -waves waves_p0011.mxd'
32+
- name: p0011_v2
33+
options: '-top work.p0011_v2_tb +acc+b -waves waves_p0011_v2.mxd'
3034
source_files:
3135
- language: verilog
3236
path: ..\filelist.txt

dsim/waves_p0011.mxd

21.3 KB
Binary file not shown.

dsim/waves_p0011_v2.mxd

8.26 KB
Binary file not shown.

filelist.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ src/p0007.v
1111
src/p0008.v
1212
src/p0009.v
1313
src/p0010.v
14+
src/p0011.v
15+
src/p0011_v2.v
1416

1517
tb/p0001_tb.v
1618
tb/p0002_tb.v
@@ -21,4 +23,6 @@ tb/p0006_tb.v
2123
tb/p0007_tb.v
2224
tb/p0008_tb.v
2325
tb/p0009_tb.v
24-
tb/p0010_tb.v
26+
tb/p0010_tb.v
27+
tb/p0011_tb.v
28+
tb/p0011_v2_tb.v

src/p0011.v

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
module p0011(
2+
input clk,
3+
output reg [31:0] result = 0,
4+
output reg done = 0,
5+
output reg error = 0
6+
);
7+
8+
localparam LEN = 20;
9+
10+
reg [7:0] rom [0:LEN*LEN - 1];
11+
12+
initial $readmemh("p0011.mem", rom);
13+
14+
function [7:0] rom_index;
15+
input integer i;
16+
input integer j;
17+
begin
18+
if (i < 0 || i >= LEN || j < 0 || j >= LEN) begin
19+
rom_index = 0;
20+
end
21+
else begin
22+
rom_index = rom[i*LEN + j];
23+
end
24+
end
25+
endfunction
26+
27+
integer i = 0;
28+
integer j = 0;
29+
30+
localparam state_down = 3'd0;
31+
localparam state_right = 3'd1;
32+
localparam state_diag = 3'd2;
33+
localparam state_diag_2 = 3'd3;
34+
35+
reg [2:0] state = state_down;
36+
reg [2:0] inner_state = 3'd0;
37+
reg [31:0] tmp = 1;
38+
39+
wire [31:0] debug_reg;
40+
assign debug_reg = rom_index(i + inner_state, j);
41+
42+
always @(posedge clk) begin
43+
if ( done )begin
44+
end
45+
else begin
46+
if (i < LEN - 3) begin
47+
if (j < LEN - 3) begin
48+
case (state)
49+
state_down: begin
50+
if (inner_state < 4) begin
51+
tmp <= tmp * rom_index(i + inner_state, j);
52+
inner_state <= inner_state + 1;
53+
end
54+
else begin
55+
result <= result > tmp ? result : tmp;
56+
inner_state <= 0;
57+
tmp <= 1;
58+
state <= state_right;
59+
end
60+
end
61+
state_right: begin
62+
if (inner_state < 4) begin
63+
tmp <= tmp * rom_index(i, j + inner_state);
64+
inner_state <= inner_state + 1;
65+
end
66+
else begin
67+
result <= result > tmp ? result : tmp;
68+
inner_state <= 0;
69+
tmp <= 1;
70+
state <= state_diag_2;
71+
end
72+
end
73+
state_diag_2: begin
74+
if (inner_state < 4) begin
75+
tmp <= tmp * rom_index(i + inner_state, j - inner_state);
76+
inner_state <= inner_state + 1;
77+
end
78+
else begin
79+
result <= result > tmp ? result : tmp;
80+
inner_state <= 0;
81+
tmp <= 1;
82+
state <= state_diag;
83+
end
84+
end
85+
state_diag: begin
86+
if (inner_state < 4) begin
87+
tmp <= tmp * rom_index(i + inner_state, j + inner_state);
88+
inner_state <= inner_state + 1;
89+
end
90+
else begin
91+
result <= result > tmp ? result : tmp;
92+
inner_state <= 0;
93+
tmp <= 1;
94+
state <= state_down;
95+
j <= j + 1;
96+
end
97+
end
98+
default: begin
99+
error <= 1;
100+
done <= 1;
101+
end
102+
endcase
103+
end
104+
else begin
105+
i <= i + 1;
106+
j <= 0;
107+
end
108+
end
109+
else begin
110+
done <= 1;
111+
end
112+
end
113+
end
114+
115+
endmodule

src/p0011_v2.v

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
module p0011_v2(
2+
input clk,
3+
output reg [31:0] result = 0,
4+
output reg done = 0,
5+
output reg error = 0
6+
);
7+
8+
localparam LEN = 20;
9+
10+
reg [7:0] rom [0:LEN*LEN - 1];
11+
12+
initial $readmemh("p0011.mem", rom);
13+
14+
15+
function [7:0] rom_index;
16+
input integer i;
17+
input integer j;
18+
begin
19+
if (i < 0 || i >= LEN || j < 0 || j >= LEN) begin
20+
rom_index = 0;
21+
end
22+
else begin
23+
rom_index = rom[i*LEN + j];
24+
end
25+
end
26+
endfunction
27+
28+
function [31:0] get_down;
29+
input integer i;
30+
input integer j;
31+
begin
32+
get_down = rom_index(i, j) * rom_index(i + 1, j) * rom_index(i + 2, j) * rom_index(i + 3, j);
33+
end
34+
endfunction
35+
36+
function [31:0] get_right;
37+
input integer i;
38+
input integer j;
39+
begin
40+
get_right = rom_index(i, j) * rom_index(i, j + 1) * rom_index(i, j + 2) * rom_index(i, j + 3);
41+
end
42+
endfunction
43+
44+
function [31:0] get_diag_down_right;
45+
input integer i;
46+
input integer j;
47+
begin
48+
get_diag_down_right = rom_index(i, j) * rom_index(i + 1, j + 1) * rom_index(i + 2, j + 2) * rom_index(i + 3, j + 3);
49+
end
50+
endfunction
51+
52+
function [31:0] get_diag_down_left;
53+
input integer i;
54+
input integer j;
55+
begin
56+
get_diag_down_left = rom_index(i, j) * rom_index(i + 1, j - 1) * rom_index(i + 2, j - 2) * rom_index(i + 3, j - 3);
57+
end
58+
endfunction
59+
60+
function [31:0] max_5;
61+
input reg [31:0] a;
62+
input reg [31:0] b;
63+
input reg [31:0] c;
64+
input reg [31:0] d;
65+
input reg [31:0] e;
66+
begin
67+
max_5 = a > b ? a : b;
68+
max_5 = max_5 > c ? max_5 : c;
69+
max_5 = max_5 > d ? max_5 : d;
70+
max_5 = max_5 > e ? max_5 : e;
71+
end
72+
endfunction
73+
74+
integer index_i = 0;
75+
integer index_j = 0;
76+
77+
78+
wire [31:0] down;
79+
wire [31:0] right;
80+
wire [31:0] diag_right;
81+
wire [31:0] diag_left;
82+
83+
assign down = get_down(index_i, index_j);
84+
assign right = get_right(index_i, index_j);
85+
assign diag_right = get_diag_down_right(index_i, index_j);
86+
assign diag_left = get_diag_down_left(index_i, index_j);
87+
88+
always @(posedge clk) begin
89+
if ( done )begin
90+
end
91+
else begin
92+
if (index_i < LEN - 3) begin
93+
if (index_j < LEN - 3) begin
94+
result <= max_5(down, right, diag_right, diag_left, result);
95+
index_j <= index_j + 1;
96+
end
97+
else begin
98+
index_i <= index_i + 1;
99+
index_j <= 0;
100+
end
101+
end
102+
else begin
103+
done <= 1;
104+
end
105+
end
106+
end
107+
108+
endmodule

tb/p0011_tb.v

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
`timescale 1ns/1ps
2+
3+
module p0011_tb();
4+
5+
reg clk = 0;
6+
wire [31:0] out;
7+
wire done;
8+
wire error;
9+
10+
always #0.5 clk=~clk;
11+
12+
p0011 u_p0011(
13+
.clk(clk),
14+
.result(out),
15+
.done(done),
16+
.error(error)
17+
);
18+
19+
initial begin
20+
@(posedge done or posedge error);
21+
#5;
22+
$display("out = %d", out);
23+
$display("done = %d", done);
24+
$display("error = %d", error);
25+
$stop;
26+
end
27+
28+
endmodule

tb/p0011_v2_tb.v

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
`timescale 1ns/1ps
2+
3+
module p0011_v2_tb();
4+
5+
reg clk = 0;
6+
wire [31:0] out;
7+
wire done;
8+
wire error;
9+
10+
always #0.5 clk=~clk;
11+
12+
p0011_v2 u_p0011_v2(
13+
.clk(clk),
14+
.result(out),
15+
.done(done),
16+
.error(error)
17+
);
18+
19+
initial begin
20+
@(posedge done or posedge error);
21+
#5;
22+
$display("out = %d", out);
23+
$display("done = %d", done);
24+
$display("error = %d", error);
25+
$stop;
26+
end
27+
28+
endmodule

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/karaketir16/project-euler-verilog/commit/72db6a0b065f93c1d9aedc95caebe696a6e7ddc4

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy