Skip to content

Commit 90bc53b

Browse files
committed
Add Verilog tests
1 parent 4f9cbae commit 90bc53b

File tree

8 files changed

+702
-0
lines changed

8 files changed

+702
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, 2019, Chris Fraire <[email protected]>.
23+
*/
24+
25+
package org.opengrok.indexer.analysis.verilog;
26+
27+
import java.io.BufferedReader;
28+
import java.io.InputStream;
29+
import java.io.InputStreamReader;
30+
import java.nio.charset.StandardCharsets;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
import org.junit.Test;
35+
import static org.junit.Assert.assertNotNull;
36+
import static org.opengrok.indexer.util.CustomAssertions.assertSymbolStream;
37+
38+
/**
39+
* Tests the {@link VerilogSymbolTokenizer} class.
40+
*/
41+
public class VerilogSymbolTokenizerTest {
42+
43+
/**
44+
* Test sample.v v. samplesymbols.txt
45+
* @throws Exception thrown on error
46+
*/
47+
@Test
48+
public void testVerilogSymbolStream() throws Exception {
49+
InputStream vRes = getClass().getClassLoader().getResourceAsStream(
50+
"analysis/verilog/sample.v");
51+
assertNotNull("despite sample.v as resource,", vRes);
52+
InputStream symRes = getClass().getClassLoader().getResourceAsStream(
53+
"analysis/verilog/samplesymbols.txt");
54+
assertNotNull("despite samplesymbols.txt as resource,", symRes);
55+
56+
List<String> expectedSymbols = new ArrayList<>();
57+
try (BufferedReader symRead = new BufferedReader(new InputStreamReader(
58+
symRes, StandardCharsets.UTF_8))) {
59+
String line;
60+
while ((line = symRead.readLine()) != null) {
61+
int hashOffset = line.indexOf('#');
62+
if (hashOffset != -1) {
63+
line = line.substring(0, hashOffset);
64+
}
65+
expectedSymbols.add(line.trim());
66+
}
67+
}
68+
69+
assertSymbolStream(VerilogSymbolTokenizer.class, vRes, expectedSymbols);
70+
}
71+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, 2019, Chris Fraire <[email protected]>.
23+
*/
24+
25+
package org.opengrok.indexer.analysis.verilog;
26+
27+
import org.junit.Test;
28+
import org.opengrok.indexer.analysis.AbstractAnalyzer;
29+
import org.opengrok.indexer.analysis.CtagsReader;
30+
import org.opengrok.indexer.analysis.Definitions;
31+
import org.opengrok.indexer.analysis.WriteXrefArgs;
32+
import org.opengrok.indexer.analysis.Xrefer;
33+
34+
import java.io.BufferedReader;
35+
import java.io.ByteArrayOutputStream;
36+
import java.io.IOException;
37+
import java.io.InputStream;
38+
import java.io.InputStreamReader;
39+
import java.io.PrintStream;
40+
import java.io.StringWriter;
41+
import java.io.Writer;
42+
import java.nio.charset.StandardCharsets;
43+
44+
import static org.junit.Assert.assertEquals;
45+
import static org.junit.Assert.assertNotNull;
46+
import static org.opengrok.indexer.util.CustomAssertions.assertLinesEqual;
47+
import static org.opengrok.indexer.util.StreamUtils.copyStream;
48+
49+
/**
50+
* Tests the {@link VerilogXref} class.
51+
*/
52+
public class VerilogXrefTest {
53+
54+
@Test
55+
public void sampleTest() throws IOException {
56+
writeAndCompare("analysis/verilog/sample.v",
57+
"analysis/verilog/sample_xref.html",
58+
getTagsDefinitions(), 81);
59+
}
60+
61+
@Test
62+
public void shouldCloseTruncatedStringSpan() throws IOException {
63+
writeAndCompare("analysis/verilog/truncated.v",
64+
"analysis/verilog/truncated_xref.html",
65+
null, 1);
66+
}
67+
68+
private void writeAndCompare(String sourceResource, String resultResource,
69+
Definitions defs, int expLOC) throws IOException {
70+
71+
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
72+
73+
InputStream sourceRes = getClass().getClassLoader().getResourceAsStream(
74+
sourceResource);
75+
assertNotNull(sourceResource + " should get-as-stream", sourceRes);
76+
int actLOC = writeVerilogXref(new PrintStream(bytesOut), sourceRes, defs);
77+
sourceRes.close();
78+
79+
InputStream resRes = getClass().getClassLoader().getResourceAsStream(
80+
resultResource);
81+
assertNotNull(resultResource + " should get-as-stream", resRes);
82+
byte[] expectedBytes = copyStream(resRes);
83+
resRes.close();
84+
bytesOut.close();
85+
86+
String outStr = new String(bytesOut.toByteArray(), StandardCharsets.UTF_8);
87+
String[] gotten = outStr.split("\n");
88+
89+
String expStr = new String(expectedBytes, StandardCharsets.UTF_8);
90+
String[] expected = expStr.split("\n");
91+
92+
assertLinesEqual("Verilog xref", expected, gotten);
93+
assertEquals("Verilog LOC", expLOC, actLOC);
94+
}
95+
96+
private int writeVerilogXref(PrintStream oss, InputStream iss,
97+
Definitions defs) throws IOException {
98+
99+
oss.print(getHtmlBegin());
100+
101+
Writer sw = new StringWriter();
102+
VerilogAnalyzerFactory fac = new VerilogAnalyzerFactory();
103+
AbstractAnalyzer analyzer = fac.getAnalyzer();
104+
analyzer.setScopesEnabled(true);
105+
analyzer.setFoldingEnabled(true);
106+
WriteXrefArgs writeArgs = new WriteXrefArgs(
107+
new InputStreamReader(iss, StandardCharsets.UTF_8), sw);
108+
writeArgs.setDefs(defs);
109+
Xrefer xref = analyzer.writeXref(writeArgs);
110+
oss.print(sw.toString());
111+
112+
oss.print(getHtmlEnd());
113+
return xref.getLOC();
114+
}
115+
116+
private Definitions getTagsDefinitions() throws IOException {
117+
InputStream res = getClass().getClassLoader().getResourceAsStream(
118+
"analysis/verilog/sampletags");
119+
assertNotNull("though sampletags should stream,", res);
120+
121+
BufferedReader in = new BufferedReader(new InputStreamReader(
122+
res, StandardCharsets.UTF_8));
123+
124+
CtagsReader rdr = new CtagsReader();
125+
String line;
126+
while ((line = in.readLine()) != null) {
127+
rdr.readLine(line);
128+
}
129+
return rdr.getDefinitions();
130+
}
131+
132+
private static String getHtmlBegin() {
133+
return "<!DOCTYPE html>\n" +
134+
"<html lang=\"en\">\n" +
135+
"<head>\n" +
136+
"<meta charset=\"UTF-8\">\n" +
137+
"<title>sampleFile - OpenGrok cross reference" +
138+
" for /sampleFile</title></head><body>\n";
139+
}
140+
141+
private static String getHtmlEnd() {
142+
return "</body>\n" +
143+
"</html>\n";
144+
}
145+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 SCARV Project - <[email protected]>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
//
26+
// SCARV Project
27+
//
28+
// University of Bristol
29+
//
30+
// RISC-V Cryptographic Instruction Set Extension
31+
//
32+
// Reference Implementation
33+
//
34+
//
35+
36+
localparam \SCARV_COP_INSN_SUCCESS = 3'b000;
37+
localparam SCARV_COP_INSN_ABORT = 3'b001;
38+
localparam SCARV_COP_INSN_BAD_INS = 3'b010;
39+
localparam SCARV_COP_INSN_BAD_LAD = 3'b100;
40+
localparam SCARV_COP_INSN_BAD_SAD = 3'b101;
41+
localparam SCARV_COP_INSN_LD_ERR = 3'b110;
42+
localparam SCARV_COP_INSN_ST_ERR = 3'b111;
43+
localparam \module = 3'b111;
44+
45+
localparam SCARV_COP_ICLASS_PACKED_ARITH = 4'b0001;
46+
localparam SCARV_COP_ICLASS_TWIDDLE = 4'b0010;
47+
localparam SCARV_COP_ICLASS_LOADSTORE = 4'b0011;
48+
localparam SCARV_COP_ICLASS_RANDOM = 4'b0100;
49+
localparam SCARV_COP_ICLASS_MOVE = 4'b0101;
50+
localparam SCARV_COP_ICLASS_MP = 4'b0110;
51+
localparam SCARV_COP_ICLASS_BITWISE = 4'b0111;
52+
localparam SCARV_COP_ICLASS_AES = 4'b1000;
53+
localparam SCARV_COP_ICLASS_SHA3 = 4'b1001;
54+
55+
localparam SCARV_COP_SCLASS_SHA3_XY = 5'b11000;
56+
localparam SCARV_COP_SCLASS_SHA3_X1 = 5'b11001;
57+
localparam SCARV_COP_SCLASS_SHA3_X2 = 5'b11010;
58+
localparam SCARV_COP_SCLASS_SHA3_X4 = 5'b11100;
59+
localparam SCARV_COP_SCLASS_SHA3_YX = 5'b11011;
60+
61+
localparam SCARV_COP_SCLASS_SCATTER_B = 5'd0 ;
62+
localparam SCARV_COP_SCLASS_GATHER_B = 5'd1 ;
63+
localparam SCARV_COP_SCLASS_SCATTER_H = 5'd2 ;
64+
localparam SCARV_COP_SCLASS_GATHER_H = 5'd3 ;
65+
localparam SCARV_COP_SCLASS_ST_W = 5'd4 ;
66+
localparam SCARV_COP_SCLASS_LD_W = 5'd5 ;
67+
localparam SCARV_COP_SCLASS_ST_H = 5'd6 ;
68+
localparam SCARV_COP_SCLASS_LH_CR = 5'd7 ;
69+
localparam SCARV_COP_SCLASS_ST_B = 5'd8 ;
70+
localparam SCARV_COP_SCLASS_LB_CR = 5'd9 ;
71+
72+
`ifdef FORMAL
73+
`include "fml_common.vh"
74+
`endif
75+
76+
//
77+
// module: scarv_cop_cprs
78+
//
79+
// The general purpose register file used by the COP.
80+
//
81+
module scarv_cop_cprs (
82+
83+
input wire g_clk , // Global clock
84+
output wire g_clk_req , // Clock request
85+
input wire g_resetn , // Synchronous active low reset.
86+
87+
`ifdef FORMAL
88+
`VTX_REGISTER_PORTS_OUT(cprs_snoop)
89+
`endif
90+
91+
input wire crs1_ren , // Port 1 read enable
92+
input wire [ 3:0] crs1_addr , // Port 1 address
93+
output wire [31:0] crs1_rdata , // Port 1 read data
94+
95+
input wire crs2_ren , // Port 2 read enable
96+
input wire [ 3:0] crs2_addr , // Port 2 address
97+
output wire [31:0] crs2_rdata , // Port 2 read data
98+
99+
input wire crs3_ren , // Port 3 read enable
100+
input wire [ 3:0] crs3_addr , // Port 3 address
101+
output wire [31:0] crs3_rdata , // Port 3 read data
102+
103+
input wire [ 3:0] crd_wen , // Port 4 write enable
104+
input wire [ 3:0] crd_addr , // Port 4 address
105+
input wire [31:0] crd_wdata // Port 4 write data
106+
107+
);
108+
109+
// Only need a clock when doing a write.
110+
assign g_clk_req = crd_wen;
111+
112+
// Storage for the registers
113+
reg [31:0] cprs [15:0];
114+
115+
`ifdef FORMAL
116+
`VTX_REGISTER_PORTS_ASSIGNR(cprs_snoop,cprs)
117+
`endif
118+
119+
//
120+
// Read port logic
121+
//
122+
123+
assign crs1_rdata = {32{crs1_ren}} & cprs[crs1_addr];
124+
assign crs2_rdata = {32{crs2_ren}} & cprs[crs2_addr];
125+
assign crs3_rdata = {32{crs3_ren}} & cprs[crs3_addr];
126+
127+
//
128+
// Generate logic for each register.
129+
//
130+
genvar i;
131+
generate for (i = 0; i < 16; i = i + 1) begin : gen_cprs
132+
133+
always @(posedge g_clk) begin
134+
135+
if(!g_resetn) begin
136+
`ifdef FORMAL
137+
// If running the yosys formal flow, allow initial
138+
// register values to be any constant value.
139+
#1 cprs[i] <= $anyconst;
140+
`else
141+
#1step cprs[i] <= 32'b0;
142+
`endif
143+
144+
end else if((|crd_wen) && (crd_addr == i)) begin
145+
if(crd_wen[3]) cprs[i][31:24] <= crd_wdata[31:24];
146+
if(crd_wen[2]) cprs[i][23:16] <= crd_wdata[23:16];
147+
if(crd_wen[1]) cprs[i][15: 8] <= crd_wdata[15: 8];
148+
if(crd_wen[0]) cprs[i][ 7: 0] <= crd_wdata[ 7: 0];
149+
end
150+
151+
end
152+
153+
end endgenerate
154+
155+
endmodule

0 commit comments

Comments
 (0)