SlideShare a Scribd company logo
Robust Verilog Testing Using
Verilator & SystemC & C++17
Yodalee <lc85301@gmail.com>
Yu-Sheng Lin <johnjohnlys@gmail.com>
Take RSA256 as an example
1
License
2
Outline
● Verilator is a good, opensource SystemVerilog (SV) simulation tool
○ Verilator compiles SystemVerilog into C++ class
○ Control the signals in C++ testbench is tedious
● Array and struct can simplify SV coding
○ Use C++17 to build structs, arrays works the same as SV
● Design patterns for signals using SystemC
○ Unify the control interface
○ Mapping them to SystemC sc_fifo
● Case study: RSA 256
3
WhyVerilator
● Open-sourced SystemVerilog simulation tool.
○ https://www.veripool.org/verilator/
○ https://github.com/verilator/verilator/
● Fast
● Decent SV support
● Free license.
○ Enable massively-parallel simulation.
○ Suitable for CI.
4
How Verilator works
5
module Mod(
input [7:0] i_data,
output output_ok,
output [12:0] o_data [2]
);
struct VMod {
u8 i_data;
u8 output_ok;
u16 o_data[2];
};
VMod m;
m.i_data = 45;
while (not m.output_ok) {}
EXPECT_EQ(m->o_data[0], 30);
Verilog
User C++ testbench
Generated C++
Simulation Binary
./run.exe
Gtest: 100 != 30
Challenges
6
module Mod(
input [7:0] i_data,
output output_ok,
output [12:0] o_data [2]
);
struct VMod {
u8 i_data;
u8 output_ok;
u16 o_data[2];
};
VMod m;
m.i_data = 45;
while (not m.output_ok) {}
EXPECT_EQ(m->o_data[0], 30);
Verilog
User C++ testbench
Generated C++
Simulation Binary
./run.exe
Gtest: 100 != 30
Too many signals to control
Solution: use struct
Bitwidth information loss
Use Struct to Simplify SV & Challenges
7
Verilog
Bitwidth information loss
Struct information loss [2]
Support struct v5.000+ [1]
module Mod(
input [7:0] i_data,
input [13:0] i_data2,
input i_data3
);
typedef struct {
logic [7:0] data;
logic [13:0] data2;
logic data3;
} ModIn ;
module Mod(
input ModIn i
);
SystemVerilog
class VMod {
u32 i;
};
Generated C++
[1] We use v5.006 right now.
[2] Verilator has intensive issue and PR about this.
rewrite
We need int/array/struct that works for SV and C++
● Challange: Information loss when converting from SV to Verilator
C++
○ Bitwidth & struct information
● Why?
○ The interface that Verilator will convert to is not standardized.
● Solution: Abstraction
○ Build a SV-compatible type system with C++17
8
typedef struct {
logic [7:0] data;
logic [13:0] data2;
logic data3;
} ModIn ;
class ModIn {
u32 i;
};
User code Adaptor
Generated C++
SV-C++ interface
3 weapons mimicking the SV typing system
● vuint<11>
○ logic [10:0] sig;
○ Replace sc_uint
● varray<vuint<11>, 3, 4>
○ logic [10:0] sig [3][4];
○ std::array with multiple dimension
● vstruct (macro)
○ typedef struct packed { ... } iStruct;
○ C++17 based type reflection struct supporting $bits, $pack
9
← Example later
● Why reinvent sc_uint<int>?
● sc_uint is virtual, we cannot memcpy
● sc_uint must link against libsystemc
● You need sc_biguint<int> for wide integers.
● vuint is strictly typed
○ vuint<10> == vuint<11> is disallowed, required by many Lint tools
● C++11 type deduction system and varadic length template
○ auto val = Concat(vuint<4>, vuint<99>, vuint<2>)
Arbitrary bitwidth integer
10
vuint<11> v
| |
logic [10:0] v
Array
● Just like std::array, but high-dim.
● Also support array of struct.
11
varray<vuint<11>,2,3> v
| |
logic [10:0] v [2][3];
or
logic [0:1][0:2][10:0] v ;
We treat them the same
vstruct (macro) : Verilog struct
● C++17 magic!
● We want to fully utilize C++ standard.
● In SystemVerilog:
○ $bit(oStruct) == 36
○ logic [$bit(oStruct)-1:0] v;
● Our C++ API allows us to:
○ vuint<bit<oStruct>> value;
○ vuint<36> value = packed(oStruct);
○ Also support: unpack, print json, Verilator I/O
struct iStruct {
vuint<3> a;
vuint<10> b;
};
struct oStruct {
vuint<10> sig;
varray<iStruct, 2> c;
};
12
vstruct enabled by type reflection (oversimplified)
● All you need is 1 line of macro for every struct.
● Based on boost::hana, you can loop through the struct...
struct oStruct {
vuint<10> sig;
varray<iStruct, 2> c;
MY_MACRO(sig, c);
};
bit<T>
constexpr unsigned b = 0;
for (int i = 0; i < T::N; ++i)
{
b += bit<get<i>()>;
}
return b;
packed(t)
return Concat(
packed(get<i>()) for i in range(N)
);
auto& get<0>() { return sig; }
auto& get<1>() { return c; }
constexpr unsigned N = 2;
13
Expand to...
(pseudocode)
Implement
Example using vuint/varray/vstruct
parameter MOD_WIDTH = 256;
parameter INT_WIDTH = 32;
typedef logic [MOD_WIDTH-1:0] KeyType;
typedef logic [INT_WIDTH-1:0] IntType;
typedef struct packed {
IntType power;
KeyType modulus;
} TwoPowerIn;
constexpr unsigned MOD_WIDTH = 256;
constexpr unsigned INT_WIDTH = 32;
typedef vuint<MOD_WIDTH> KeyType;
typedef vuint<INT_WIDTH> IntType;
struct TwoPowerIn {
IntType power;
KeyType modulus;
MY_MACRO(power, modulus)
};
14
C++ Model SystemVerilog Package
The 1:1 SV-C++ mapping is beautiful
How Verilator works
15
module Mod(
input [7:0] i_data,
output output_ok,
output [12:0] o_data [2]
);
struct VMod {
u8 i_data;
u8 output_ok;
u16 o_data[2];
};
VMod m;
m.i_data = 45;
while (not m.output_ok) {}
EXPECT_EQ(m->o_data[0], 30);
Verilog
User C++ testbench
Generated C++
Simulation Binary
./run.exe
Gtest: 100 != 30
How to drive
module?
How to feed data?
Verilog can have any
kind of interface
The Valid/Ready Protocol
16
● Used in ARM AXI, Intel Avalon... specification
● Cycle 1: Sender set valid to 0, no data to transfer.
● Cycle 2: Sender set valid to 1, having data to transfer, but Receiver
set ready as 0, so hold valid and data.
● Cycle 3: Receiver set ready to 1, so Sender can set valid to 0 in the
next cycle or send the next data.
Valid
Ready
Sender Receiver
The Valid/Ready Protocol
17
Sender should hold
valid before Receiver
accept it
Sender should not
change data before
Receiver accept it
Receiver can freely
set/reset the ready
We can abstract valid ready protocol by SystemC sc_fifo
● sc_fifo full → Valid = 1, Ready = 0
● sc_fifo empty → Valid = 0, Ready = 1
The Valid/Ready Protocol
18
Valid
Ready
Sender Receiver Sender Receiver
sc_fifo
SystemCAbstraction of Module
Limit module to be one input, one output.
SC_MODULE(Montgomery) {
sc_in_clk clk;
sc_fifo_in<MontgomeryIn> data_in;
sc_fifo_out<MontgomeryOut> data_out;
SC_CTOR(Montgomery) {
SC_THREAD(Thread);
}
void Thread();
};
Montgomery::Thread() {
while (true) {
MontgomeryIn in = data_in.read();
KeyType a = in.a;
KeyType b = in.b;
KeyType round_result(0);
…
data_out.write(round_result));
}
}
Define input/output with
structure or alias
19
sc_fifo as input/output interface
Translate SystemC to Verilog
Montgomery::Thread() {
while (true) {
MontgomeryIn in =
data_in.read();
KeyType a = in.a;
KeyType b = in.b;
KeyType round_result(0);
…
data_out.write(round_result));
}
}
20
The SystemC module is fully tested and translated to Verilog
Keep the module simple, otherwise it will be difficult to translate.
module Montgomery(
// input data
input i_valid,
output i_ready,
input MontgomeryIn i_in,
);
always_ff @(posedge clk) begin
if (i_ready && i_valid) begin
a <= {2'b0, i_in.a};
b <= {2'b0, i_in.b};
round_result <= 'b0;
end
end
…
sc_fifo translate to valid/ready
and optional data
Verilog Testbench
● Wrap the DUT class generated by Verilator
○ Assume that DUT has i_valid/i_ready and o_valid/o_ready
○ Testbench generates the clock with sc_clock
○ The driver/monitor implement before_clk and after_clk to control the
valid/ready
21
Testbench
Driver DUT
i_ready
i_valid
o_ready
o_valid
Monitor
Test Methodology
22
● At least 2 set of input/output data
○ More is better
○ Never just test 1 set of input/output
● Random
○ Deterministic input/output for basic correctness
○ Random input/output to find more issues
https://github.com/yodalee/rsa256
4 modules all with single input/single output.
Golden data generated by C model (or Python script)
1 to 1 mapping from SystemC to Verilog
The RSA256 Implementation
23
RSA256
plaintext
key
modulus ciphertext
TwoPower RSAMont
Montgomery
Conclusion
24
● The data type in SystemC is not suitable for simulating
Verilog
○ We create the vint, varray and vstructure.
○ The data type can directly map to SystemVerilog
● We design, implement and test RSA 256 modules, and
validate with Verilator
○ Abstraction over the interface, the designer (a.k.a me) can
focus on test data.
Some (Possible) Future Work
25
● Replace SystemC with pure C++ framework
● Support complex interface like AXI
● Really tapeout the chip with Skywater service
Q&A
26

More Related Content

Similar to COSCUP2023 RSA256 Verilator.pdf

C++ amp on linux
C++ amp on linuxC++ amp on linux
C++ amp on linux
Miller Lee
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical Reviewer
Andrey Karpov
 
A taste of GlobalISel
A taste of GlobalISelA taste of GlobalISel
A taste of GlobalISel
Igalia
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Doug Hawkins
 
ESL Anyone?
ESL Anyone? ESL Anyone?
ESL Anyone? DVClub
 
Cryptography and secure systems
Cryptography and secure systemsCryptography and secure systems
Cryptography and secure systems
Vsevolod Stakhov
 
CUDA by Example : Parallel Programming in CUDA C : Notes
CUDA by Example : Parallel Programming in CUDA C : NotesCUDA by Example : Parallel Programming in CUDA C : Notes
CUDA by Example : Parallel Programming in CUDA C : Notes
Subhajit Sahu
 
Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)
Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)
Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)
Shinya Takamaeda-Y
 
Improving Java performance at JBCNConf 2015
Improving Java performance at JBCNConf 2015Improving Java performance at JBCNConf 2015
Improving Java performance at JBCNConf 2015
Raimon Ràfols
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Windows Developer
 
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
Fwdays
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
Mahmoud Samir Fayed
 
C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"
GlobalLogic Ukraine
 
Building Hierarchy
Building HierarchyBuilding Hierarchy
Building Hierarchy
Mohamed Samy
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
Mahmoud Samir Fayed
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
Ji Hun Kim
 
Introduction to CUDA C: NVIDIA : Notes
Introduction to CUDA C: NVIDIA : NotesIntroduction to CUDA C: NVIDIA : Notes
Introduction to CUDA C: NVIDIA : Notes
Subhajit Sahu
 
Applying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedApplying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing Speed
Pascal-Louis Perez
 

Similar to COSCUP2023 RSA256 Verilator.pdf (20)

C++ amp on linux
C++ amp on linuxC++ amp on linux
C++ amp on linux
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical Reviewer
 
A taste of GlobalISel
A taste of GlobalISelA taste of GlobalISel
A taste of GlobalISel
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
ESL Anyone?
ESL Anyone? ESL Anyone?
ESL Anyone?
 
Picmico
PicmicoPicmico
Picmico
 
Cryptography and secure systems
Cryptography and secure systemsCryptography and secure systems
Cryptography and secure systems
 
CUDA by Example : Parallel Programming in CUDA C : Notes
CUDA by Example : Parallel Programming in CUDA C : NotesCUDA by Example : Parallel Programming in CUDA C : Notes
CUDA by Example : Parallel Programming in CUDA C : Notes
 
Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)
Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)
Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)
 
Improving Java performance at JBCNConf 2015
Improving Java performance at JBCNConf 2015Improving Java performance at JBCNConf 2015
Improving Java performance at JBCNConf 2015
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
 
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov"Optimization of a .NET application- is it simple ! / ?",  Yevhen Tatarynov
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
 
C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"
 
Building Hierarchy
Building HierarchyBuilding Hierarchy
Building Hierarchy
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
 
Introduction to CUDA C: NVIDIA : Notes
Introduction to CUDA C: NVIDIA : NotesIntroduction to CUDA C: NVIDIA : Notes
Introduction to CUDA C: NVIDIA : Notes
 
Applying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedApplying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing Speed
 
Ac2
Ac2Ac2
Ac2
 

More from Yodalee

rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdfrrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
Yodalee
 
Gameboy emulator in rust and web assembly
Gameboy emulator in rust and web assemblyGameboy emulator in rust and web assembly
Gameboy emulator in rust and web assembly
Yodalee
 
Make A Shoot ‘Em Up Game with Amethyst Framework
Make A Shoot ‘Em Up Game with Amethyst FrameworkMake A Shoot ‘Em Up Game with Amethyst Framework
Make A Shoot ‘Em Up Game with Amethyst Framework
Yodalee
 
Build Yourself a Nixie Tube Clock
Build Yourself a Nixie Tube ClockBuild Yourself a Nixie Tube Clock
Build Yourself a Nixie Tube Clock
Yodalee
 
Use PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language ParserUse PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language Parser
Yodalee
 
Introduction to nand2 tetris
Introduction to nand2 tetrisIntroduction to nand2 tetris
Introduction to nand2 tetris
Yodalee
 
Office word skills
Office word skillsOffice word skills
Office word skills
Yodalee
 
Git: basic to advanced
Git: basic to advancedGit: basic to advanced
Git: basic to advanced
Yodalee
 

More from Yodalee (8)

rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdfrrxv6 Build a Riscv xv6 Kernel in Rust.pdf
rrxv6 Build a Riscv xv6 Kernel in Rust.pdf
 
Gameboy emulator in rust and web assembly
Gameboy emulator in rust and web assemblyGameboy emulator in rust and web assembly
Gameboy emulator in rust and web assembly
 
Make A Shoot ‘Em Up Game with Amethyst Framework
Make A Shoot ‘Em Up Game with Amethyst FrameworkMake A Shoot ‘Em Up Game with Amethyst Framework
Make A Shoot ‘Em Up Game with Amethyst Framework
 
Build Yourself a Nixie Tube Clock
Build Yourself a Nixie Tube ClockBuild Yourself a Nixie Tube Clock
Build Yourself a Nixie Tube Clock
 
Use PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language ParserUse PEG to Write a Programming Language Parser
Use PEG to Write a Programming Language Parser
 
Introduction to nand2 tetris
Introduction to nand2 tetrisIntroduction to nand2 tetris
Introduction to nand2 tetris
 
Office word skills
Office word skillsOffice word skills
Office word skills
 
Git: basic to advanced
Git: basic to advancedGit: basic to advanced
Git: basic to advanced
 

Recently uploaded

Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
top1002
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 

Recently uploaded (20)

Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 

COSCUP2023 RSA256 Verilator.pdf

  • 1. Robust Verilog Testing Using Verilator & SystemC & C++17 Yodalee <lc85301@gmail.com> Yu-Sheng Lin <johnjohnlys@gmail.com> Take RSA256 as an example 1
  • 3. Outline ● Verilator is a good, opensource SystemVerilog (SV) simulation tool ○ Verilator compiles SystemVerilog into C++ class ○ Control the signals in C++ testbench is tedious ● Array and struct can simplify SV coding ○ Use C++17 to build structs, arrays works the same as SV ● Design patterns for signals using SystemC ○ Unify the control interface ○ Mapping them to SystemC sc_fifo ● Case study: RSA 256 3
  • 4. WhyVerilator ● Open-sourced SystemVerilog simulation tool. ○ https://www.veripool.org/verilator/ ○ https://github.com/verilator/verilator/ ● Fast ● Decent SV support ● Free license. ○ Enable massively-parallel simulation. ○ Suitable for CI. 4
  • 5. How Verilator works 5 module Mod( input [7:0] i_data, output output_ok, output [12:0] o_data [2] ); struct VMod { u8 i_data; u8 output_ok; u16 o_data[2]; }; VMod m; m.i_data = 45; while (not m.output_ok) {} EXPECT_EQ(m->o_data[0], 30); Verilog User C++ testbench Generated C++ Simulation Binary ./run.exe Gtest: 100 != 30
  • 6. Challenges 6 module Mod( input [7:0] i_data, output output_ok, output [12:0] o_data [2] ); struct VMod { u8 i_data; u8 output_ok; u16 o_data[2]; }; VMod m; m.i_data = 45; while (not m.output_ok) {} EXPECT_EQ(m->o_data[0], 30); Verilog User C++ testbench Generated C++ Simulation Binary ./run.exe Gtest: 100 != 30 Too many signals to control Solution: use struct Bitwidth information loss
  • 7. Use Struct to Simplify SV & Challenges 7 Verilog Bitwidth information loss Struct information loss [2] Support struct v5.000+ [1] module Mod( input [7:0] i_data, input [13:0] i_data2, input i_data3 ); typedef struct { logic [7:0] data; logic [13:0] data2; logic data3; } ModIn ; module Mod( input ModIn i ); SystemVerilog class VMod { u32 i; }; Generated C++ [1] We use v5.006 right now. [2] Verilator has intensive issue and PR about this. rewrite
  • 8. We need int/array/struct that works for SV and C++ ● Challange: Information loss when converting from SV to Verilator C++ ○ Bitwidth & struct information ● Why? ○ The interface that Verilator will convert to is not standardized. ● Solution: Abstraction ○ Build a SV-compatible type system with C++17 8 typedef struct { logic [7:0] data; logic [13:0] data2; logic data3; } ModIn ; class ModIn { u32 i; }; User code Adaptor Generated C++ SV-C++ interface
  • 9. 3 weapons mimicking the SV typing system ● vuint<11> ○ logic [10:0] sig; ○ Replace sc_uint ● varray<vuint<11>, 3, 4> ○ logic [10:0] sig [3][4]; ○ std::array with multiple dimension ● vstruct (macro) ○ typedef struct packed { ... } iStruct; ○ C++17 based type reflection struct supporting $bits, $pack 9 ← Example later
  • 10. ● Why reinvent sc_uint<int>? ● sc_uint is virtual, we cannot memcpy ● sc_uint must link against libsystemc ● You need sc_biguint<int> for wide integers. ● vuint is strictly typed ○ vuint<10> == vuint<11> is disallowed, required by many Lint tools ● C++11 type deduction system and varadic length template ○ auto val = Concat(vuint<4>, vuint<99>, vuint<2>) Arbitrary bitwidth integer 10 vuint<11> v | | logic [10:0] v
  • 11. Array ● Just like std::array, but high-dim. ● Also support array of struct. 11 varray<vuint<11>,2,3> v | | logic [10:0] v [2][3]; or logic [0:1][0:2][10:0] v ; We treat them the same
  • 12. vstruct (macro) : Verilog struct ● C++17 magic! ● We want to fully utilize C++ standard. ● In SystemVerilog: ○ $bit(oStruct) == 36 ○ logic [$bit(oStruct)-1:0] v; ● Our C++ API allows us to: ○ vuint<bit<oStruct>> value; ○ vuint<36> value = packed(oStruct); ○ Also support: unpack, print json, Verilator I/O struct iStruct { vuint<3> a; vuint<10> b; }; struct oStruct { vuint<10> sig; varray<iStruct, 2> c; }; 12
  • 13. vstruct enabled by type reflection (oversimplified) ● All you need is 1 line of macro for every struct. ● Based on boost::hana, you can loop through the struct... struct oStruct { vuint<10> sig; varray<iStruct, 2> c; MY_MACRO(sig, c); }; bit<T> constexpr unsigned b = 0; for (int i = 0; i < T::N; ++i) { b += bit<get<i>()>; } return b; packed(t) return Concat( packed(get<i>()) for i in range(N) ); auto& get<0>() { return sig; } auto& get<1>() { return c; } constexpr unsigned N = 2; 13 Expand to... (pseudocode) Implement
  • 14. Example using vuint/varray/vstruct parameter MOD_WIDTH = 256; parameter INT_WIDTH = 32; typedef logic [MOD_WIDTH-1:0] KeyType; typedef logic [INT_WIDTH-1:0] IntType; typedef struct packed { IntType power; KeyType modulus; } TwoPowerIn; constexpr unsigned MOD_WIDTH = 256; constexpr unsigned INT_WIDTH = 32; typedef vuint<MOD_WIDTH> KeyType; typedef vuint<INT_WIDTH> IntType; struct TwoPowerIn { IntType power; KeyType modulus; MY_MACRO(power, modulus) }; 14 C++ Model SystemVerilog Package The 1:1 SV-C++ mapping is beautiful
  • 15. How Verilator works 15 module Mod( input [7:0] i_data, output output_ok, output [12:0] o_data [2] ); struct VMod { u8 i_data; u8 output_ok; u16 o_data[2]; }; VMod m; m.i_data = 45; while (not m.output_ok) {} EXPECT_EQ(m->o_data[0], 30); Verilog User C++ testbench Generated C++ Simulation Binary ./run.exe Gtest: 100 != 30 How to drive module? How to feed data? Verilog can have any kind of interface
  • 16. The Valid/Ready Protocol 16 ● Used in ARM AXI, Intel Avalon... specification ● Cycle 1: Sender set valid to 0, no data to transfer. ● Cycle 2: Sender set valid to 1, having data to transfer, but Receiver set ready as 0, so hold valid and data. ● Cycle 3: Receiver set ready to 1, so Sender can set valid to 0 in the next cycle or send the next data. Valid Ready Sender Receiver
  • 17. The Valid/Ready Protocol 17 Sender should hold valid before Receiver accept it Sender should not change data before Receiver accept it Receiver can freely set/reset the ready
  • 18. We can abstract valid ready protocol by SystemC sc_fifo ● sc_fifo full → Valid = 1, Ready = 0 ● sc_fifo empty → Valid = 0, Ready = 1 The Valid/Ready Protocol 18 Valid Ready Sender Receiver Sender Receiver sc_fifo
  • 19. SystemCAbstraction of Module Limit module to be one input, one output. SC_MODULE(Montgomery) { sc_in_clk clk; sc_fifo_in<MontgomeryIn> data_in; sc_fifo_out<MontgomeryOut> data_out; SC_CTOR(Montgomery) { SC_THREAD(Thread); } void Thread(); }; Montgomery::Thread() { while (true) { MontgomeryIn in = data_in.read(); KeyType a = in.a; KeyType b = in.b; KeyType round_result(0); … data_out.write(round_result)); } } Define input/output with structure or alias 19 sc_fifo as input/output interface
  • 20. Translate SystemC to Verilog Montgomery::Thread() { while (true) { MontgomeryIn in = data_in.read(); KeyType a = in.a; KeyType b = in.b; KeyType round_result(0); … data_out.write(round_result)); } } 20 The SystemC module is fully tested and translated to Verilog Keep the module simple, otherwise it will be difficult to translate. module Montgomery( // input data input i_valid, output i_ready, input MontgomeryIn i_in, ); always_ff @(posedge clk) begin if (i_ready && i_valid) begin a <= {2'b0, i_in.a}; b <= {2'b0, i_in.b}; round_result <= 'b0; end end … sc_fifo translate to valid/ready and optional data
  • 21. Verilog Testbench ● Wrap the DUT class generated by Verilator ○ Assume that DUT has i_valid/i_ready and o_valid/o_ready ○ Testbench generates the clock with sc_clock ○ The driver/monitor implement before_clk and after_clk to control the valid/ready 21 Testbench Driver DUT i_ready i_valid o_ready o_valid Monitor
  • 22. Test Methodology 22 ● At least 2 set of input/output data ○ More is better ○ Never just test 1 set of input/output ● Random ○ Deterministic input/output for basic correctness ○ Random input/output to find more issues
  • 23. https://github.com/yodalee/rsa256 4 modules all with single input/single output. Golden data generated by C model (or Python script) 1 to 1 mapping from SystemC to Verilog The RSA256 Implementation 23 RSA256 plaintext key modulus ciphertext TwoPower RSAMont Montgomery
  • 24. Conclusion 24 ● The data type in SystemC is not suitable for simulating Verilog ○ We create the vint, varray and vstructure. ○ The data type can directly map to SystemVerilog ● We design, implement and test RSA 256 modules, and validate with Verilator ○ Abstraction over the interface, the designer (a.k.a me) can focus on test data.
  • 25. Some (Possible) Future Work 25 ● Replace SystemC with pure C++ framework ● Support complex interface like AXI ● Really tapeout the chip with Skywater service