15-Verilog and RTL-2x2
15-Verilog and RTL-2x2
COS / ELE 375 RTL: a language for describing the behavior of computers
in terms of step-wise register contents
Computer Architecture and Organization
Princeton University
Fall 2015
Bochao Wang
(Based on slides by David Penry and Neil Vachharajani)
(Prof. David August)
1 2
RTL, just like (most) programming languages: • Used to describe hardware for simulation and synthesis
• Precise and unambiguous • Why? Designs are too big to just draw schematics
3 4
Simulation vs. Synthesis Verilog Design Flow
5 6
7 8
Basic syntax
9 10
11 12
Signals Wires
• Signal: a “variable” that can have values assigned “ahead of time” • Wires need not be declared unless it is multiple wires
• Can be thought of as a wire or a group of wires
• Syntax:
module example(b, c);
<kind> [<width specifier>] name, name, …;
input b;
• Examples: output c;
reg [15:0] IR;
wire a; // not necessary
wire [3:0] a; a[3] is MSB
wire [1:8] b; b[1] is MSB //not recommend wire [3:0] d; // necessary
wire doit; …
assign c = b | a;
• Registers vs. wires:
• Reg: inside “always” block, lhs, e.g. IR = …
endmodule
• Wire: outside “always” block, lhs, e.g. assign a = …
13 14
15 16
Modules Module Overall Structure
• Allow you to organize the design hierarchically module the_design ( input, output );
• Allow structural reuse of the design Declarations: ports, constants, variables(wire, reg)
Instantiations of other modules
• Defining a module: Continuous assignment: assign y = ...
• Example: Processes(initial, always) {
module flop (clk, D, Q); // name, list of ports
procedural blocking assignment
input clk; // define directions of ports
input D; procedural nonblocking assignment
output Q; }
…. // code to do things
endmodule
endmodule
• Instantiating a module:
flop U1 (.clk(someclk), .D(someD), .Q(someQ));
17 18
• Continuous (assign = )
• Only to “wire”
• Always sensitive to things on the right-hand side a = b Do Sequentially (blocking, sequentially)
A <= b Do RHS first (nonblocking, grab at t=0)
• Blocking ( = )
• Made inside a process
• Only to “reg”
• Value of lhs changes immediately Which 3 are Shift Registers?
• Non-blocking ( <= ) Z<=Y; Y<=X;
• Made inside a process Y<=X; Z<=Y;
• Only to “reg”
• Value of lhs changes only after all rhs have been evaluated
Z=Y; Y=X;
• A time-spec after the statement says to wait before changing the value Y=X; Z=Y;
• Continue executing process even if there is a time-spec
19 20
Processes Process examples
21 22
#300;
rst_l = 1; // blocking • Wait for time:
# 300;
end
# 300 a = b; // can also put before a statement
23 24
Operators Statements
25 26
• Rather different from C switch statements: module mux4_to_1 (out, i0, i1, i2, i3, s1, s0);
case (myvar) output out;
input i0, i1, i2, i3;
1 : dothis = 1;
input s1, s0;
2 : dothat = 3;
reg out;
3 : dosomemore = 4; always @(s1 or s0 or i0 or i1 or i2 or i3)
default: noneoftheabove = 1; begin
endcase case ({s1, s0})
2'b00: out = i0;
2'b01: out = i1;
• Note that there are no fall-throughs 2'b10: out = i2;
• Multiple statements inside one case require begin/end 2'b11: out = i3;
around them default: out = 1'bx; // x is don’t care
endcase
end
endmodule
27 28
Parameters Using the mux module
• Increases ability to reuse modules • It’s a 4-input mux for busses with a ‘width’ parameter for the size
of the bus
29 30
• defined values are referenced as `name always @(posedge clk or negedge rst_l)
if (~rst_l) state <= `IDLE;
else
case (state)
`IDLE:
if (req)
if (wr) state <= `WRITE;
else state <= `READ;
else state <= `IDLE; // not strictly necessary
`READ:
state <= `DONE;
`WRITE:
state <= `DONE;
`DONE:
state <= `IDLE;
default:
state <= `IDLE;
endcase
31 32
Dealing with Synthesis My Rules
• Things you can’t synthesize reliably: • Make all flops (edge-sensitive always blocks) use a non-blocking
• Some for loops (hard to guess) assigment with #1 delay
• @() in the middle of a process • Easier to see what’s going on in waverforms
• Time specifiers
• force/release
• Make all other always blocks use blocking assignment and use
complete sensitivity lists
• Watch out for hidden latches! • always @(*) for next state logic combinational
always @(a)
• always @(posedge clk) for sequential
c = a | b;
• Actually creates a latch
• All things on the rhs must go on the sensitivity list unless you are • Do combinational logic in continuous assignments except for next
trying to create a state element state logic
33 34
Thank you
35 36
Using the regfile2 module Functions and tasks
• You should use this module for your register file. • Functions are like C functions, but cannot have side effects
• It synthesizes properly • Tasks do not have return values, but can have side effects
• It has a nice task for displaying the register file contents
• Examples:
• Interesting characteristics: function [31:0] add1;
input [31:0] x;
• One read ports, one read/write port
begin
• Port A writes if the enable bits are set (bytes are controlled add1 = x + 1;
individually); it will always read as well
end
• Port B can only read endfunction
• Clocked on the positive edge of the clock (you can’t do the write in first
half, read in second half trick from P & H) task addtask;
input [31:0] x;
• If you name your datapath DATA (the instance name, not the output [31:0] y;
module name), and make IR_Enable a signal indicating when the begin
IR is to be loaded with a new instruction, you can uncomment code y = x + 1;
in monitors.v to get automatic register dumps before each mycrazysignal = 3;
instruction. end
endtask
37 38
• Recording signals:
• $dumpfile(<file name>); – set the dumpfile name
• $dumpvars(<# levels>, <hierarchy>);
• Adds signals below a particular point in the hierarchy to the list of signals to
dump; only decends the hierarchy for the number of levels specified; 0
means no limit
• $dumpon; - turns on dumping
• $dumpoff; - turns off dumping
39 40