Coding Interview Questions
Coding Interview Questions
module tb_three_times_table;
2. Write a Verilog code to implement a counter that cycles through two distinct ranges based
on a mode: initially counting from 5 to 12, and then, upon reaching the end of this range,
switching to count from 0 to 15.
module counter (
input wire clk, // Clock input
input wire reset, // Reset signal input
output reg [3:0] count // Output: 4-bit counter (5 to 12, then 0 to 15)
);
endmodule
module tb_counter;
// Inputs
reg clk;
reg reset;
// Outputs
wire [3:0] count;
// Clock generation
always begin
#5 clk = ~clk; // Toggle the clock every 5 time units
end
// Testbench logic
initial begin
// Initialize inputs
clk = 0;
reset = 0;
endmodule
3. Write a Verilog module that determines if a given number is even or odd, and outputs a
result of 1 for even numbers and 0 for odd numbers using function.
module Test;
// Define a function named isEvenOrOdd which takes an input 'a' and returns
a bit indicating if the number is even or odd
function bit isEvenOrOdd(input logic a);
bit result; // Declare a variable 'result' of type bit
// Call the isEvenOrOdd function with input_value and store the result
result = isEvenOrOdd(input_value);
4. Write a SystemVerilog code to identify and display numbers divisible by 8 within the range
of 0 to 100?
// Define a SystemVerilog class named DivisibleByEight
class DivisibleByEight;
// Method to find and display numbers divisible by 8
static function void findDivisibleByEight();
// Loop through numbers from 0 to 100
for (int i = 0; i <= 100; i++) begin
// Check if the current number is divisible by 8
if (i % 8 == 0)
// If divisible by 8, display the number
$display("Number %0d is divisible by 8", i);
end
endfunction
endclass
// Testbench module
module TestBench;
// Initial block to execute at the beginning of simulation
initial begin
// Call the findDivisibleByEight method of the DivisibleByEight class
DivisibleByEight::findDivisibleByEight();
// Finish simulation
$finish;
end
endmodule
5. Write a SystemVerilog program that utilizes a function to calculate the sum of two numbers
and determine whether the result is odd or even.
class packet;
rand bit[31:0] data; // Declare a random 32-bit data variable
rand bit[31:0] addr; // Declare a random 32-bit address variable
rand bit [31:0] sum; // Declare a random 32-bit sum variable
property data;
// Waits for a rising edge on both a and b signals, disables if reset is
low
@(posedge(a && b)) disable iff (reset == 0)
// Upon event, execute the following sequence
(1, set()); // Call the set task
endproperty
// Wait for 20 ns
#20;
join_any
// If y becomes 1 within 20ns, assert the condition
am_aby: assert(y == 1)
// Display the time taken for y to go high
$display("y went to 1 after %t", $realtime - t);
else
// If y does not become 1 within 20ns, display a message
$display("y did not go to 1 within 20ns");
endtask
initial begin
repeat(2) begin // Generate and display the random value twice
s.randomize(); // Randomize the object s according to its constraints
$display("%b", s.x); // Display the generated 10-bit number in binary
end
end
endmodule
10. Write a constraint for an array of numbers such that the size of array ranges from 6 to 15
elements, and even index locations should have odd numbers and odd index locations
should have even numbers, numbers rage is between 16 to 127.
// Define a SystemVerilog class named packet
class packet;
// Declare a rand array of 7-bit elements
rand bit[6:0] array[];
/or/