Lab0 Pre Lab
Lab0 Pre Lab
Lab 0: Pre-lab
Jaewoong Sim
Electrical and Computer Engineering
Seoul National University
Overview
• Goal
• Learn how to use Verilog
• Set up the Verilog coding environment
• Implement your register file and validate it
• We provide you skeleton code of register file, which you need to build on
2
Honor Code
DO NOT CHEAT!
3
Verilog 101
4
Hardware Description Language (HDL)
Two common hardware description languages
Verilog
• Developed by Gateway Design Automation in 1984; Gateway was acquired by
Cadence in 1990
• Became an IEEE Standard (IEEE 1364) in 1995
Verilog-95, Verilog 2001, Verilog 2005 (minor revision), SystemVerilog (Industry)
5
Best Way to Learn Verilog?
• Look at other Verilog code examples
• Do write code a lot!
• HDLBits
• [Link]
• Highly recommend solving the problem sets there
• Created by my former colleague Henry Wong
His Stanford seminar about x86 on FPGA is also interesting!
[Link]
a
y
b
Module
7
Implementing a Module in Verilog
module name
input/output ports
endmodule
Format
module module_name (module interface list);
[list of interface ports]
...
[net and variable declarations]
Items enclosed in square
...
brackets are optional
[functional specification of module]
...
endmodule
8
Software Programming vs Hardware Description
• Software Programming Languages
• SW code executes statements in program order on given hardware
• White space: blank space (\b), tabs (\t), and new line (\n)
• Comments
• // used for single line comments
• /* ….*/ used for multi-line comments
10
Lexical Conventions
Number Representation
12
Lexical Conventions
Number Representation – Negative Values
module signed_number;
reg [31:0] a;
initial begin
a = 14'h1234;
$display ("Current Value of a = %h", a);
a = -14'h1234;
$display ("Current Value of a = %h", a);
a = 32'hDEAD_BEEF;
$display ("Current Value of a = %h", a);
a = -32'hDEAD_BEEF;
$display ("Current Value of a = %h", a);
#10 $finish;
end
endmodule
13
Lexical Conventions
Number Representation
14
Lexical Conventions
Number Representation - Examples
Integer Stored as
1 00000000000000000000000000000001
8'hAA 10101010
6'b10_0011 100011
'hF 00000000000000000000000000001111
6'hCA 001010
6'hA 001010
16'bZ zzzzzzzzzzzzzzzz
8'bx xxxxxxxx
15
Lexical Conventions
• Other data types
• String: “Back to School and Have a Nice Semester”
• Real number: 3.4, 294.872, 1.44E(or e)9
Decimal and scientific notations accepted
At least one digit on each side of the decimal point
.2 (illegal)
16
Value Sets
• 0 and 1 represent logic values low and high, respectively
• z indicates the high-impedance condition of a node or net
• x indicates an unknown value of a net or node
Value Meaning
0 Logic 0, false condition
1 Logic 1, true condition
x Unknown logic value
z High impedance
17
Data Types
• Nets mean any hardware connection points (cannot store value!)
• Variables represent any data storage elements
Nets Variables
wire supply0 reg
tri supply1 integer
wand tri0 real
wor tri1 time
triand trireg realtime
trior
• Variables
• Can be referenced anywhere in a module
• Can be assigned value only within a procedural statement, task, or
function
• Cannot be an input or inout port in a module
19
Verilog Ports
• Ports connect a module to the outside world
Module
net
21
Port Connection Examples
22
Operator Precedence
23
Verilog Assignments
• There are three forms of assignments in Verilog
• Continuous Assignments
• Drive a value into a net
• Used to model combinational logic
• (Explicit) Use assign keyword
wire temp;
assign temp = a | b;
• (Implicit) combine declaration and assignment
wire temp = a | b;
• Procedural Assignments
• Assign values to variables declared as regs in always blocks/tasks/functions
• Usually used to model registers and FSMs
25
Module Modeling Levels
• Structural Level
• Gate level description of the circuit
• Connect built-in primitives, user-defined primitives, or other modules using
wires
• Describe a hierarchy
• Dataflow Level
• Describe hardware in terms of dataflow from inputs to outputs
• Use operators (+, -, &, |, …) that act on operands
• Use continuous assignment statements (keyword assign)
• Behavioral Level
• Typically used for sequential logic (but it can be used for combinational logic)
• Use procedural statements (keyword always)
• The target in procedural assignment statements must be a reg type
26
Structural Modeling
// gate-level description of half adder
module half_adder(input x,
• Half Adder: S = X⨁Y, C = XY input y,
output s,
X Y SUM CARRY output c);
1 1 0 1 endmodule
pre-defined in Verilog
// gate-level description of full adder
• Full Adder module full_adder(input x,
input y,
input c_in,
Full Adder output sum,
output c_out);
c_in sum // internal nodes
Half // outputs of both half adders
wire s1, c1, c2;
x s1 Adder c2
c_out // full adder body
Half
// instantiate the half adder
y Adder c1 half_adder ha_1 (x, y, s1, c1);
half_adder ha_2 (c_in, s1, sum, c2);
or (c_out, c1, c2);
endmodule 27
Dataflow Modeling
• combinational logic
module full_adder_dataflow(input x,
input y,
input c_in,
output sum,
output c_out);
// specify the function of a full adder
assign #5 {c_out, sum} = x + y + c_in;
continuous assignment endmodule
continuously driven
by something
Full Adder
c_in sum
Half
x s1 Adder c2 - Use operators to compute sum and c_out
c_out
Half - Use continuous assignment to drive
y Adder c1 values onto nets
28
Behavioral Modeling
• sequential & combinational logic
always block
Whenever the event in the sensitivity list occurs,
the always block is activated
module full_adder_behavioral(input x,
input y,
input c_in,
output reg sum,
output reg c_out);
29
Mixed-Style Modeling
module full_adder_mixed_style(input x,
input y,
input c_in,
output sum,
output reg c_out);
wire s1, c1, c2;
// structural modeling of HA 1.
xor xor_ha1(s1, x, y);
and and_ha1(c1, x, y);
// dataflow modeling of HA 2.
assign sum = c_in ^ s1;
assign c2 = c_in & s1;
30
Summary
• Number representation
• Format should be : <size>’<base format><number>
• Size : The size of the data with bit granularity
• Base format: radix (d : decimal , b : binary , h : hexadecimal )
• Number : The value of the data
assign a = 4’b0100; // 4bit binary, value: unsigned 4
assign a = 3’d100; // 3bit decimal, value: unsigned 100
assign a = 8’h0a; // 8bit hexadecimal, value: unsigned 11
reg_array_1[2] = register_2;
• Rule of thumb
• Sequential logic → use a clocked always block with Nonblocking assignments. (<=)
• Combinational logic → use an always block with Blocking assignments. (=)
• Try not to mix the two ( = , <= ) in the same always block.
36
Setting Up the Verilog Coding Environment
• We recommend you use lightweight, open-source tools as shown below
• It is okay to use other tools such as Xilinx Vivado if you want or have them
• Icarus Verilog
• Open-Source Verilog compiler and simulator
• GTKWave
• Waveform viewer
• Use when we debug the Verilog code
37
Setting Up the Verilog Coding Environment
Windows
• Reboot
• Download WSL 2 kernel update package (click the link below)
[Link]
• Set WSL2 as a default version & Download Linux
➢ wsl --set-default-version 2
38
Setting Up the Verilog Coding Environment
Windows
• WSL screenshots
39
Setting Up the Verilog Coding Environment
Windows (WSL)
X11 Forwarding
• To enabling X11 forwarding, we can use Xming
• Download and Install Xming
• [Link]
• Open installed Xming’s file location.
40
Setting Up the Verilog Coding Environment
Windows (WSL)
X11 Forwarding
• To enabling X11 forwarding, we can use Xming
• Open Xming’s property.
• Add “-ac” at the end of target.
• Execute Xming. It will run at the background.
41
Setting Up the Verilog Coding Environment
Windows (WSL)
X11 Forwarding
• To enabling X11 forwarding, we can use Xming
• Unable the firewall with this command outside the wsl environment.
➢ Set-NetFirewallRule -DisplayName "Xming X Server" -Enabled True -Profile Any
42
Setting Up the Verilog Coding Environment
Windows (WSL)
X11 Forwarding
• To enabling X11 forwarding, we can use Xming
• After installation, edit your configuration file (~/.bashrc or ~/.zshrc) at wsl
1) Check your shell: $ echo $SHELL
2) Open your configuration file: $ vim ~/.bashrc (or $ vim ~/.zshrc)
3) Write & Save: export DISPLAY=“nameserver_ip:0”
You can check the nameserver ip by cat /etc/[Link]
4) Apply your change: source ~/.bashrc
If you have any problem, refer this website
o [Link]
43
Setting Up the Verilog Coding Environment
Linux/Mac OS
Mac OS
• You can use “homebrew” to install icarus verilog
• Install Homebrew,
/bin/bash -c “$(curl -fsSL
[Link]
• Install icarus Verilog & GTKWave
brew install icarus-verilog
brew install gtkwave
• X11 forwarding: Install xquartz ([Link]
mountain-lion-mavericks-install-xquartz-server/)
44
Test Your Framework
• Type the code on the right
and save it as “hello.v”
45
Lab 0: Register File
46
Background
47
Background #1 : RISC Processor
• RISC (Reduced Instruction Set Computer)
• Computer design that reduces the number of CPU instructions in order to
simplify the internal structure
48
Background #1 : RISC Processor
• Fetch Stage : Reads the instruction from instruction memory
• PC (Program counter)
Register that holds the address of the next instruction (which is stored in instruction
memory)
• Instruction Memory
A part of memory that stores the instructions to be executed
• Instruction Register
Register that stores the instruction which is fetched from instruction memory
1) Fetch instruction from instruction memory
49
Background #1 : RISC Processor
• Execute stage: Executes the instruction fetched in fetch stage
• Register file
A group of registers that temporarily hold the data required by the instruction
• Decoder
A combinational logic that determines the action to be executed by decoding opcode
• Data Memory
A part of memory that stores the data
• ALU/Shifter
A combinational logic that executes ALU / shifter operations depending on the
decoder’s result
50
Background #2 : Register File
• Register file temporarily stores data required by instructions
• It stores operand, memory address, etc.
• It also passes data to the functional unit
Data_in r7 Data 7
… …
Addr_A Data_A
r2 Data 2
Register File
Addr_B Data_B
r1 Data 1
r0 Data 0
WR, CLK, RSTn
Example of register file with 16 registers
51
Background #2 : Register File
• Input / Output of register file
• Input
Addr_A , Addr_B : Input address
WR : Signal to notify register file that data write is in progress. Generated by decoder
CLK : Clock Signal
RSTn : Active-low Reset Signal
Data_in : Input data
• Output
Data_A : Data stored in Addr_A
Data_in
Data_B : Data stored in Addr_B
Addr_A Data_A
Register File
Addr_B Data_B
52
Register File Operation Example
• Behavior of register file with actual assembly instruction
• Read Operation
Read r3 / r4 from register file, then pass to ALU
• Write Operation
Write the result (r3+r4) to r4
Data_in
• Specification
• 8 registers (each register is 16bit wide)
r7
• Rising CLK edge triggered writes
r6
Addr_A r5 Data_A
r4
r3
r2 Data_B
Addr_B
r1
r0
CLK WR RSTn
53
Register File Operation Example
• Read Operation
Data in Addr_A register → Data_A
Data in Addr_B register → Data_B
Combinational logic (not affected by CLK)
Data_in
r7
r6
r5 Data_A a+b
Addr_A r4 b
0011
r3 a
AL
r2 U
Data_B
Addr_B r1
0100
r0
CLK WR RSTn
54
Register File Operation Example
• Write Operation
Destination : Addr_B register
Data: Data_in (from ALU adder in this example)
Condition : Rising edge CLK, Positive WR
Sequential logic (affected by CLK)
Data_in
r7
r6
r5 Data_A a+b
Addr_A r4 a+b
r3 a
AL
r2 U
Data_B
Addr_B r1
0100
r0
CLK WR RSTn
55
Design Your Register File
56
Specification of Register File
• You need to design a register file with 8 registers
• Each register should be 16 bit
r7
• Input / Output specification r6
• CLK, RSTn, WR (1-bit) Addr_A r5 Data_A
r4
• Addr_A, Addr_B (3-bit)
r3
• Data_in (16-bit) r2 Data_B
Addr_B
• Data_A, Data_B (16-bit) r1
r0
CLK WR RSTn
57
FIXME
• What you need to modify in testbench code
(Register_file_tb.v)
1. Name of a module instance
2. Name of ports
58
FIXME
• What you need to modify in source code (Register_file.v)
1. Sensitivity list of always block
2. Reset appropriately when reset signal is asserted
3. Write the data to appropriate register when write signal is asserted
4. Read two register values in a combinational method
59
Compile & Run
• How to compile & run your code
• Run “iverilog -o Register_file Register_file_tb.v Register_file.v” inside your project
directory
• this will generate a “Register_file” inside your project directory
• “vvp Register_file” will execute the test
• Then, you can check the results
60
Test Your Code
• If you implemented correctly, you will see…
• “Correctly Write!”
• “Correctly Read!”
61
Waveform
• Also you can check the waveform
• Running “vvp Register_file” will generate “Register_file.vcd” in your project
directory.
• Run “gtkwave Register_file.vcd” will execute gtkwave.
Click “Register_file_tb” at SST
Choose signal whose waveform you want to see
Click Zoom Fit
Now, you can see the waveforms
62
Result & Submission
63
Submission
• Due: 4/2 (Tuesday) 11:59 PM
• Late Policy
• 20% discounted per day (4/3 12:00 AM is a late submission!)
• After 4/6 12:00 AM, you will get a zero score for the assignment
• What to submit?
• Your code that we can simulate and run
• How to submit?
• Upload your compressed file (zip/tar/etc) to eTL
• Format: Your student ID_YOURLASTNAME_lab#
e.g.) 2024-12345_KIM_lab0.zip
├── Register_file.v
└── Register_file_tb.v
• Please make sure you follow the format.
10% penalty for the wrong format
64