Skip to content

Commit

Permalink
chapter 3
Browse files Browse the repository at this point in the history
  • Loading branch information
sifferman committed Sep 19, 2023
1 parent 5965406 commit ad54a7a
Show file tree
Hide file tree
Showing 21 changed files with 319 additions and 11 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/code.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Test Code Examples

on: [push]

jobs:
Verify-Solution:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.x
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Download FuseSoC
run: |
python -m pip install --upgrade pip
pip install fusesoc
fusesoc --version
- name: Install gcc-10
run: |
sudo apt update
sudo apt install -y build-essential
sudo apt install -y gcc-10 g++-10 cpp-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10
gcc --version
- name: Download OSS
run: |
curl -JOL https://github.com/YosysHQ/oss-cad-suite-build/releases/download/2023-05-23/oss-cad-suite-linux-x64-20230523.tgz
tar -xzvf oss-cad-suite-linux-x64-20230523.tgz -C ./
rm -rf oss-cad-suite-linux-x64-20230523.tgz
echo "$GITHUB_WORKSPACE/oss-cad-suite/bin" >> $GITHUB_PATH
- name: Test C-like
run: |
cd $GITHUB_WORKSPACE/code/c-like
make
- name: Test Cache Lab
run: |
cd $GITHUB_WORKSPACE/code/cache_lab
make
10 changes: 10 additions & 0 deletions code/c-like/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

all: clean verilator

obj_dir/Vtb: tb.sv high.svh low.svh
verilator tb.sv --binary -Wall -Wno-fatal --top tb
verilator: obj_dir/Vtb
./obj_dir/Vtb

clean:
rm -rf obj_dir Vtb.vvp build
10 changes: 10 additions & 0 deletions code/c-like/high.svh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

function automatic logic [4:0] find_first_set32(logic [31:0] in);
logic [4:0] out = 0;
for (integer i = 1; i < 32; i++)
if (in[i])
out = i;
return out;
endfunction

assign out = find_first_set32(in);
18 changes: 18 additions & 0 deletions code/c-like/low.svh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

assign out =
in[31] ? 31 : in[30] ? 30 :
in[29] ? 29 : in[28] ? 28 :
in[27] ? 27 : in[26] ? 26 :
in[25] ? 25 : in[24] ? 24 :
in[23] ? 23 : in[22] ? 22 :
in[21] ? 21 : in[20] ? 20 :
in[19] ? 19 : in[18] ? 18 :
in[17] ? 17 : in[16] ? 16 :
in[15] ? 15 : in[14] ? 14 :
in[13] ? 13 : in[12] ? 12 :
in[11] ? 11 : in[10] ? 10 :
in[ 9] ? 9 : in[ 8] ? 8 :
in[ 7] ? 7 : in[ 6] ? 6 :
in[ 5] ? 5 : in[ 4] ? 4 :
in[ 3] ? 3 : in[ 2] ? 2 :
in[ 1] ? 1 : 0;
48 changes: 48 additions & 0 deletions code/c-like/tb.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module find_first_set32 #(
parameter string IMPLEMENTATION
) (
input logic [31:0] in,
output logic [4:0] out
);

if (IMPLEMENTATION == "LOW") begin : low
`include "low.svh"
end else if (IMPLEMENTATION == "HIGH") begin : high
`include "high.svh"
end else begin : bad
initial begin
$error("Expected valid values for IMPLEMENTATION are \"LOW\" or \"HIGH\". Received \"%s\".", IMPLEMENTATION);
$finish;
end
end

endmodule

module tb;

logic clk = 0;
always #1 clk <= ~clk;

logic [31:0] value;

find_first_set32 #(.IMPLEMENTATION("LOW")) l (.in(value));
find_first_set32 #(.IMPLEMENTATION("HIGH")) h (.in(value));

always @(negedge clk) begin
if (l.out !== h.out) begin
$error("Not equivalent for in=%0h: l=%d h=%d", value, l.out, h.out);
$finish;
end
end

integer i;
initial begin
for (i = 0; i < 100000; i++) begin
value = $urandom();
@(posedge clk);
end
$display("All equal.");
$finish;
end

endmodule
12 changes: 12 additions & 0 deletions code/cache_lab/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

all: clean verify

verify: build/cache.svh
diff cache.svh build/cache.svh

build/cache.svh:
mkdir -p build
curl -s "https://raw.githubusercontent.com/sifferman/labs-with-cva6/2b788a9511ce8e0282f4ee5a8cbae2135eb0c540/labs/caching/part2/starter/ucsbece154b_victim_cache.sv" | sed -n "148,168p" > $@

clean:
rm -rf build
21 changes: 21 additions & 0 deletions code/cache_lab/cache.svh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// DLL Structure //
// MRU - ... - way.mru - way - way.lru - ... - LRU //

typedef logic [$clog2(NR_ENTRIES)-1:0] way_index_t;

struct packed {
logic [TAG_SIZE-1:0] tag;
way_index_t lru; // less recently used
way_index_t mru; // more recently used
logic valid;
} dll_d[NR_ENTRIES], dll_q[NR_ENTRIES];

// lru register
way_index_t lru_d, lru_q, mru_d, mru_q;

// index to bump
way_index_t read_index, write_index;


// separate the data from the dll help with optimization
logic [LINE_WIDTH-1:0] data_d[NR_ENTRIES], data_q[NR_ENTRIES];
30 changes: 30 additions & 0 deletions code/digitaljs_online.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module piso #(
parameter [7:0] DATA_WIDTH = 16
) (
input logic clk,
input logic rst_n,
input logic load_i,
input logic [DATA_WIDTH-1:0] loaddata_i,
output logic serial_o
);

logic [DATA_WIDTH-1:0] data_d, data_q;

always_comb begin
if (load_i)
data_d = loaddata_i;
else
data_d = (data_q >> 1);
end

always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
data_q <= '0;
end else begin
data_q <= data_d;
end
end

assign serial_o = data_q[0];

endmodule
7 changes: 7 additions & 0 deletions code/opt.svh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
wire [2:0] a;
always_comb begin
out = 0;
for (integer i = 0; i < 3; i++)
if (a[i])
out = 1;
end
26 changes: 26 additions & 0 deletions figures/c-like.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

\begin{figure}[t]
\centering

\subfloat[
Using purely structural constructs to create MUXes can provide long and superfluous code.
]{
\begin{minipage}{0.8\textwidth}
\footnotesize
\inputminted[frame=single]{systemverilog}{code/c-like/low.svh}
\end{minipage}
}

\subfloat[
Using C-like constructs such as a \mintinline{systemverilog}{function}, \mintinline{systemverilog}{if} statement, and \mintinline{systemverilog}{for} loop can provide much cleaner code.
]{
\begin{minipage}{0.8\textwidth}
\footnotesize
\inputminted[frame=single]{systemverilog}{code/c-like/high.svh}
\end{minipage}
}

\caption{Comparison of purely structural Verilog versus C-like Verilog. To demonstrate this comparison, provided are two different implementations of the Find First Set operation.}
\label{fig:c-like}

\end{figure}
7 changes: 7 additions & 0 deletions figures/cache_lab.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

\begin{figure}[t]
\centering
\inputminted[frame=single]{systemverilog}{code/cache_lab/cache.svh}
\caption{Snippet of ``Labs with CVA6'' cache lab starter code \cite{labsWithCVA6}}
\label{fig:cache_lab}
\end{figure}
2 changes: 1 addition & 1 deletion figures/dc_vs_synplify.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
\begin{figure}[t]
\centering
\frame{\includegraphics[width=\linewidth]{figures/dc_vs_synplify.pdf}}
\caption{Differences in SystemVerilog Support in DC vs. Synplify-Pro from ``Synthesizing SystemVerilog: Busting the Myth that SystemVerilog is only for Verification''\cite{sutherland}}
\caption{Differences in SystemVerilog Support in DC vs. Synplify-Pro from ``Synthesizing SystemVerilog: Busting the Myth that SystemVerilog is only for Verification'' \cite{sutherland}}
\label{fig:dc_vs_synplify}
\end{figure}
Binary file added figures/digitaljs_online.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions figures/digitaljs_online.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

\begin{figure}[t]
\centering
\includegraphics[width=\linewidth]{figures/digitaljs_online.pdf}
\caption{Schematic for a Parallel-in Serial-out shift register generated by the netlist graph viewer DigitalJS Online \cite{DigitalJSOnline}}
\label{fig:digitaljs_online}
\end{figure}
34 changes: 34 additions & 0 deletions figures/opt.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

\begin{figure}[t]
\centering

\subfloat[
If any bits of \mintinline{systemverilog}{a} are set, then \mintinline{systemverilog}{out} is \mintinline{systemverilog}{1}.
]{
\begin{minipage}{0.8\textwidth}
\inputminted[frame=single]{systemverilog}{code/opt.svh}
\end{minipage}
}

\subfloat[
Vivado infers the code as one parallel LUT.
]{
\includegraphics[width=0.9\linewidth]{figures/opt/vivado.pdf}
}

\subfloat[
Yosys without optimizations enabled infers the code as a series of 2:1 MUXes.
]{
\includegraphics[width=0.7\linewidth]{figures/opt/yosys_noopt.pdf}
}

\subfloat[
Yosys with optimizations enabled infers the code as one parallel OR gate.
]{
\includegraphics[width=0.7\linewidth]{figures/opt/yosys_opt.pdf}
}

\caption{Comparison of differences in synthesis.}
\label{fig:opt}

\end{figure}
Binary file added figures/opt/vivado.pdf
Binary file not shown.
Binary file added figures/opt/yosys_noopt.pdf
Binary file not shown.
Binary file added figures/opt/yosys_opt.pdf
Binary file not shown.
Loading

0 comments on commit ad54a7a

Please sign in to comment.