Lecture 7,8
Prepared by: Engr. Qazi Zia , Assistant Professor EED, COMSATS Attock
What is RTL (register transfer level)?
combinational Register
Hint: It is important to note that, while coding at RTL, the non blocking
procedural assignment should be used only to model sequential logic and the
blocking procedural assignment to model combinational logic
RTL Coding Guideline: Avoid
Combinational Feedback
x 10
5 x 5
5 If first cloud is an
adder (add input and
0 feedback but
10 feedback is unknown),
x 2nd cloud is multiplier
x with integer 2 (it will
multiply unknown
output of 1st cloud
with 2, so produces
unknown output.)
How to use a register
reg [7:0]acc;
always@(acc)
acc = acc +1;
Any such code does not make sense in design and simulation. The simulator will
never come out of this block as the change in acc will bring it back into the
procedural block.
If logic demands any such functionality, a register should be used to break the
combinational logic
How to use register
module adder
#(parameter N = 8)
(input wire [N-1:0] a,
input wire [N-1:0] b,
output reg [N-1:0] y
);
always @(*)
begin
y = a + b;
end
endmodule
Example : N-bit comparator using relational
operators
module comp eq = 0;
#(parameter N = 8) lt = 0;
(input wire [N-1:0] x, if(x > y)
input wire [N-1:0] y, gt = 1;
output reg gt, if(x == y)
output reg eq, eq = 1;
output reg lt if(x < y)
); lt = 1;
always @(*) end
begin endmodule
gt = 0;
About Stimulus: Loading memory from a
file
System tasks $readmemb and $readmemh are used to load data from a text
file written in binary or hexadecimal, respectively, into specified memory.
The example here illustrates the use of these tasks. First memory needs to be
defined as:
reg [7:0] mem[0:63];
The following statement loads data from memory.dat file into mem:
$readmemb (“memory.dat”, mem);
Macros
The tag can then be used instead of a constant in the code. This gives better
readability to the code. The use of the ‘define tag is shown here:
if (ctrl == ‘DIFFERENCE)
Digital Signal Processing Design
Example: Filters
Digital filters are a very important part of DSP. In fact, their extraordinary performance is one of
the key reasons that DSP has become so popular.
filters have two uses: signal separation and signal restoration.
Signal separation is needed when a signal has been contaminated with interference, noise, or
other signals.
For example, imagine a device for measuring the electrical activity of a baby's heart (EKG) while still in
the womb. The raw signal will likely be corrupted by the breathing and heartbeat of the mother. A filter
might be used to separate these signals so that they can be individually analyzed.
Signal restoration is used when a signal has been distorted in some way.
For example, an audio recording made with poor equipment may be filtered to better represent the
sound as it actually occurred.
Another example is the deblurring of an image acquired with an improperly focused lens, or a shaky
camera.
Digital Signal Processing Design Example
Verilog task can be used to code functionality that is repeated multiple times
in a module. A task has input, output and inout and can have its local
variables.
All the variables defined in the module are also accessible in the task.
The task must be defined in the same module using task and end task
keywords.
To use a task in other modules, the task should be written in a separate file
and the file then should be included using an ‘include directive in these
modules.
A simple task example
module simple_task();
task convert;
input [7:0] temp_in;
output [7:0] temp_out;
begin
temp_out = (9/5) *( temp_in + 32) ;
end
endtask
endmodule
Global variables in task
module task_global();
reg [7:0] temp_out;
reg [7:0] temp_in;
task convert;
begin
temp_out = (9/5) *( temp_in + 32);
end
endtask
endmodule
Calling a task
Verilog function is in many respects like task as it also implements code that
can be called several times inside a module.
A function is defined in the module using function and endfunction keywords.
The function can compute only one output.
To compute this output, the function must have at least one input.
The output must be assigned to an implicit variable bearing the name and
range of the function. The range of the output is also specified with the
function declaration.
Function Example
A simple function
module simple_function();
function myfunction;
input a, b, c, d;
begin
myfunction = ((a+b) + (c-d));
end
endfunction
endmodule
Calling function
module MUX4to1( input [3:0] in, input [1:0] sel, output out);
reg out1, out2;
function MUX2to1;
input in1, in2;
input select;
assign MUX2to1 = select ? in2:in1;
endfunction
assign out1 = MUX2to1(in[0], in[1], sel[0]);
assign out2= MUX2to1(in[2], in[3], sel[0]);
assign out= MUX2to1(out1, out2, sel[1]);
endmodule
Stimulus