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

What's hot (20)

PDF
System Calls
Anil Kumar Pugalia
 
PDF
Launch the First Process in Linux System
Jian-Hong Pan
 
PDF
llvm basic porting for risc v
Tsung-Chun Lin
 
PDF
A look into the sanitizer family (ASAN & UBSAN) by Akul Pillai
Cysinfo Cyber Security Community
 
PPTX
APASEC 2013 - ROP/JIT を使わずに DEP/ASLR を回避する手法を見てみた。
Satoshi Mimura
 
PDF
Play with FILE Structure - Yet Another Binary Exploit Technique
Angel Boy
 
PDF
Our answer to Uber
Alexander Korotkov
 
PPTX
Intel processor trace - What are Recorded?
Pipat Methavanitpong
 
PDF
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Victor Rentea
 
PDF
SFO15-TR9: PSCI, ACPI (and UEFI to boot)
Linaro
 
PPT
Dll injection
KarlFrank99
 
PDF
VerilatorとSystemCでSoftware Driven Verification
Mr. Vengineer
 
PDF
MySQL Parallel Replication: inventory, use-case and limitations
Jean-François Gagné
 
PDF
UM2019 Extended BPF: A New Type of Software
Brendan Gregg
 
PDF
SFO15-503: Secure storage in OP-TEE
Linaro
 
PDF
COSCUP 2023 - Make Your Own Ray Tracing GPU with FPGA
Owen Wu
 
PDF
from Binary to Binary: How Qemu Works
Zhen Wei
 
PDF
Solving PostgreSQL wicked problems
Alexander Korotkov
 
PDF
Software Craftsmanship @Code Camp Festival 2022.pdf
Victor Rentea
 
PDF
Understanding a kernel oops and a kernel panic
Joseph Lu
 
System Calls
Anil Kumar Pugalia
 
Launch the First Process in Linux System
Jian-Hong Pan
 
llvm basic porting for risc v
Tsung-Chun Lin
 
A look into the sanitizer family (ASAN & UBSAN) by Akul Pillai
Cysinfo Cyber Security Community
 
APASEC 2013 - ROP/JIT を使わずに DEP/ASLR を回避する手法を見てみた。
Satoshi Mimura
 
Play with FILE Structure - Yet Another Binary Exploit Technique
Angel Boy
 
Our answer to Uber
Alexander Korotkov
 
Intel processor trace - What are Recorded?
Pipat Methavanitpong
 
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Victor Rentea
 
SFO15-TR9: PSCI, ACPI (and UEFI to boot)
Linaro
 
Dll injection
KarlFrank99
 
VerilatorとSystemCでSoftware Driven Verification
Mr. Vengineer
 
MySQL Parallel Replication: inventory, use-case and limitations
Jean-François Gagné
 
UM2019 Extended BPF: A New Type of Software
Brendan Gregg
 
SFO15-503: Secure storage in OP-TEE
Linaro
 
COSCUP 2023 - Make Your Own Ray Tracing GPU with FPGA
Owen Wu
 
from Binary to Binary: How Qemu Works
Zhen Wei
 
Solving PostgreSQL wicked problems
Alexander Korotkov
 
Software Craftsmanship @Code Camp Festival 2022.pdf
Victor Rentea
 
Understanding a kernel oops and a kernel panic
Joseph Lu
 

Similar to COSCUP2023 RSA256 Verilator.pdf (20)

PDF
C++20 the small things - Timur Doumler
corehard_by
 
PDF
An Overview of SystemVerilog for Design and Verification
KapilRaghunandanTrip
 
DOCX
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
eugeniadean34240
 
PPT
ERTS UNIT 3.ppt
Pavithra525349
 
PDF
JVM code reading -- C2
ytoshima
 
PDF
XilinxのxsimでSoftware Driven Verification.pdf
Mr. Vengineer
 
PDF
Q4.11: Using GCC Auto-Vectorizer
Linaro
 
PDF
C++ amp on linux
Miller Lee
 
PPTX
C++ Code as Seen by a Hypercritical Reviewer
Andrey Karpov
 
PDF
A taste of GlobalISel
Igalia
 
PDF
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Doug Hawkins
 
PDF
ESL Anyone?
DVClub
 
PPT
Picmico
loges91
 
PDF
Cryptography and secure systems
Vsevolod Stakhov
 
PDF
CUDA by Example : Parallel Programming in CUDA C : Notes
Subhajit Sahu
 
PDF
Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)
Shinya Takamaeda-Y
 
PDF
Improving Java performance at JBCNConf 2015
Raimon Ràfols
 
PPTX
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Windows Developer
 
PDF
"Optimization of a .NET application- is it simple ! / ?", Yevhen Tatarynov
Fwdays
 
PDF
The Ring programming language version 1.7 book - Part 87 of 196
Mahmoud Samir Fayed
 
C++20 the small things - Timur Doumler
corehard_by
 
An Overview of SystemVerilog for Design and Verification
KapilRaghunandanTrip
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
eugeniadean34240
 
ERTS UNIT 3.ppt
Pavithra525349
 
JVM code reading -- C2
ytoshima
 
XilinxのxsimでSoftware Driven Verification.pdf
Mr. Vengineer
 
Q4.11: Using GCC Auto-Vectorizer
Linaro
 
C++ amp on linux
Miller Lee
 
C++ Code as Seen by a Hypercritical Reviewer
Andrey Karpov
 
A taste of GlobalISel
Igalia
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Doug Hawkins
 
ESL Anyone?
DVClub
 
Picmico
loges91
 
Cryptography and secure systems
Vsevolod Stakhov
 
CUDA by Example : Parallel Programming in CUDA C : Notes
Subhajit Sahu
 
Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)
Shinya Takamaeda-Y
 
Improving Java performance at JBCNConf 2015
Raimon Ràfols
 
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
Fwdays
 
The Ring programming language version 1.7 book - Part 87 of 196
Mahmoud Samir Fayed
 
Ad

More from Yodalee (8)

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

Recently uploaded (20)

PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PDF
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PDF
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Alan Turing - life and importance for all of us now
Pedro Concejero
 
PDF
this idjfk sgfdhgdhgdbhgbgrbdrwhrgbbhtgdt
WaleedAziz7
 
PDF
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
PDF
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PDF
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
PPTX
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
Distribution reservoir and service storage pptx
dhanashree78
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Alan Turing - life and importance for all of us now
Pedro Concejero
 
this idjfk sgfdhgdhgdbhgbgrbdrwhrgbbhtgdt
WaleedAziz7
 
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 

COSCUP2023 RSA256 Verilator.pdf

  • 1. Robust Verilog Testing Using Verilator & SystemC & C++17 Yodalee <[email protected]> Yu-Sheng Lin <[email protected]> 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